1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-} -- For ReprKind
4 {-# LANGUAGE PatternSynonyms #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE ViewPatterns #-}
7 module Symantic.Data where
9 import Data.Bool (Bool)
10 import Data.Either (Either)
11 import Data.Kind (Constraint)
12 import Data.Maybe (Maybe)
13 import Type.Reflection (Typeable, (:~~:)(..), eqTypeRep, typeRep)
14 import qualified Data.Eq as Eq
15 import qualified Data.Function as Fun
16 import qualified Data.Maybe as Maybe
19 import Symantic.Derive
22 data SomeData repr a =
24 ( Derivable (Data able repr)
26 ) => SomeData (Data able repr a)
28 type instance Derived (SomeData repr) = repr
29 instance Derivable (SomeData repr) where
30 derive (SomeData x) = derive x
33 -- TODO: neither data families nor data instances
34 -- can have phantom roles with GHC-9's RoleAnnotations,
35 -- hence 'Data.Coerce.coerce' cannot be used on them for now.
36 -- https://gitlab.haskell.org/ghc/ghc/-/issues/8177
37 -- https://gitlab.haskell.org/ghc/ghc/-/wikis/roles#proposal-roles-for-type-families
39 (able :: ReprKind -> Constraint)
40 :: ReprKind -> ReprKind
41 type instance Derived (Data able repr) = repr
43 -- | Convenient utility to pattern-match a 'SomeData'.
44 pattern Data :: Typeable able => Data able repr a -> SomeData repr a
45 pattern Data x <- (unSomeData -> Maybe.Just x)
47 -- | @(unSomeData c :: 'Maybe' ('Data' able repr a))@
48 -- extract the data-constructor from the given 'SomeData'
49 -- iif. it belongs to the @('Data' able repr a)@ data-instance.
53 SomeData repr a -> Maybe (Data able repr a)
54 unSomeData (SomeData (c::Data c repr a)) =
55 case typeRep @able `eqTypeRep` typeRep @c of
56 Maybe.Just HRefl -> Maybe.Just c
57 Maybe.Nothing -> Maybe.Nothing
60 data instance Data Abstractable repr a where
61 (:@) :: SomeData repr (a->b) -> SomeData repr a -> Data Abstractable repr b
62 Lam :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
63 Lam1 :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
64 Var :: repr a -> Data Abstractable repr a
65 -- FIXME: add constructors
68 ) => Derivable (Data Abstractable repr) where
70 f :@ x -> derive f .@ derive x
71 Lam f -> lam (\x -> derive (f (SomeData (Var x))))
72 Lam1 f -> lam1 (\x -> derive (f (SomeData (Var x))))
76 ) => Abstractable (SomeData repr) where
77 f .@ x = SomeData (f :@ x)
78 lam f = SomeData (Lam f)
79 lam1 f = SomeData (Lam1 f)
85 ) => Functionable (SomeData repr) where
86 ($) = lam1 (\f -> lam1 (\x -> f .@ x))
87 (.) = lam1 (\f -> lam1 (\g -> lam1 (\x -> f .@ (g .@ x))))
88 const = lam1 (\x -> lam1 (\_y -> x))
89 flip = lam1 (\f -> lam1 (\x -> lam1 (\y -> f .@ y .@ x)))
93 data instance Data Anythingable repr a where
94 Anything :: repr a -> Data Anythingable repr a
97 ) => Derivable (Data Anythingable repr) where
99 Anything x -> anything x
100 instance Anythingable (SomeData repr)
101 instance Anythingable (Data Anythingable repr)
104 data instance Data Bottomable repr a where
105 Bottom :: Data Bottomable repr a
106 instance Bottomable repr => Derivable (Data Bottomable repr) where
107 derive Bottom{} = bottom
110 data instance Data (Constantable c) repr a where
111 Constant :: {-Typeable c =>-} c -> Data (Constantable c) repr c
112 instance Constantable c repr => Derivable (Data (Constantable c) repr) where
114 Constant x -> constant x
116 ( Constantable c repr
118 ) => Constantable c (SomeData repr) where
119 constant c = SomeData (Constant c)
120 instance {-Typeable c =>-} Constantable c (Data (Constantable c) repr) where
124 data instance Data Eitherable repr a where
125 Left :: Data Eitherable repr (l -> Either l r)
126 Right :: Data Eitherable repr (r -> Either l r)
127 instance Eitherable repr => Derivable (Data Eitherable repr) where
133 ) => Eitherable (SomeData repr) where
135 right = SomeData Right
136 instance Eitherable (Data Eitherable repr) where
141 data instance Data Equalable repr a where
142 Equal :: Eq.Eq a => Data Equalable repr (a -> a -> Bool)
143 instance Equalable repr => Derivable (Data Equalable repr) where
148 ) => Equalable (SomeData repr) where
149 equal = SomeData Equal
150 instance Equalable (Data Equalable repr) where
154 data instance Data IfThenElseable repr a where
156 SomeData repr Bool ->
159 Data IfThenElseable repr a
160 instance IfThenElseable repr => Derivable (Data IfThenElseable repr) where
162 IfThenElse test ok ko -> ifThenElse (derive test) (derive ok) (derive ko)
164 ( IfThenElseable repr
165 ) => IfThenElseable (SomeData repr) where
166 ifThenElse test ok ko = SomeData (IfThenElse test ok ko)
167 instance IfThenElseable repr => IfThenElseable (Data IfThenElseable repr) where
168 ifThenElse test ok ko = IfThenElse (SomeData test) (SomeData ok) (SomeData ko)
171 data instance Data Listable repr a where
172 Cons :: Data Listable repr (a -> [a] -> [a])
173 Nil :: Data Listable repr [a]
175 instance Listable repr => Derivable (Data Listable repr) where
181 ) => Listable (SomeData repr) where
184 instance Listable (Data Listable repr) where
189 data instance Data Maybeable repr a where
190 Nothing :: Data Maybeable repr (Maybe a)
191 Just :: Data Maybeable repr (a -> Maybe a)
192 instance Maybeable repr => Derivable (Data Maybeable repr) where
198 ) => Maybeable (SomeData repr) where
199 nothing = SomeData Nothing
201 instance Maybeable (Data Maybeable repr) where