1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-} -- For ReprKind
4 {-# LANGUAGE PatternSynonyms #-}
5 {-# LANGUAGE RankNTypes #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE ViewPatterns #-}
8 module Symantic.Data where
10 import Data.Bool (Bool)
11 import Data.Either (Either)
12 import Data.Kind (Constraint)
13 import Data.Maybe (Maybe)
14 import Type.Reflection (Typeable, (:~~:)(..), eqTypeRep, typeRep)
15 import qualified Data.Eq as Eq
16 import qualified Data.Maybe as Maybe
17 import qualified Data.Function as Fun
20 import Symantic.Derive
23 data SomeData repr a =
25 ( Derivable (Data able repr)
27 ) => SomeData (Data able repr a)
29 type instance Derived (SomeData repr) = repr
30 instance Derivable (SomeData repr) where
31 derive (SomeData x) = derive x
34 -- TODO: neither data families nor data instances
35 -- can have phantom roles with GHC-9's RoleAnnotations,
36 -- hence 'Data.Coerce.coerce' cannot be used on them for now.
37 -- https://gitlab.haskell.org/ghc/ghc/-/issues/8177
38 -- https://gitlab.haskell.org/ghc/ghc/-/wikis/roles#proposal-roles-for-type-families
40 (able :: ReprKind -> Constraint)
41 :: ReprKind -> ReprKind
42 type instance Derived (Data able repr) = repr
44 -- | Convenient utility to pattern-match a 'SomeData'.
45 pattern Data :: Typeable able => Data able repr a -> SomeData repr a
46 pattern Data x <- (unSomeData -> Maybe.Just x)
48 -- | @(unSomeData c :: 'Maybe' ('Data' able repr a))@
49 -- extract the data-constructor from the given 'SomeData'
50 -- iif. it belongs to the @('Data' able repr a)@ data-instance.
54 SomeData repr a -> Maybe (Data able repr a)
55 unSomeData (SomeData (c::Data c repr a)) =
56 case typeRep @able `eqTypeRep` typeRep @c of
57 Maybe.Just HRefl -> Maybe.Just c
58 Maybe.Nothing -> Maybe.Nothing
61 data instance Data Abstractable repr a where
62 (:@) :: SomeData repr (a->b) -> SomeData repr a -> Data Abstractable repr b
63 Lam :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
64 Lam1 :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
65 Var :: repr a -> Data Abstractable repr a
66 -- FIXME: add constructors
69 ) => Derivable (Data Abstractable repr) where
71 f :@ x -> derive f .@ derive x
72 Lam f -> lam (\x -> derive (f (SomeData (Var x))))
73 Lam1 f -> lam1 (\x -> derive (f (SomeData (Var x))))
77 ) => Abstractable (SomeData repr) where
78 f .@ x = SomeData (f :@ x)
79 lam f = SomeData (Lam f)
80 lam1 f = SomeData (Lam1 f)
82 ($) = lam1 (\f -> lam1 (\x -> f .@ x))
83 (.) = lam1 (\f -> lam1 (\g -> lam1 (\x -> f .@ (g .@ x))))
84 const = lam1 (\x -> lam1 (\_y -> x))
85 flip = lam1 (\f -> lam1 (\x -> lam1 (\y -> f .@ y .@ x)))
89 data instance Data Anythingable repr a where
90 Anything :: repr a -> Data Anythingable repr a
94 Derivable (Data Anythingable repr) where
96 Anything x -> anything x
97 instance Anythingable (SomeData repr)
98 instance Anythingable (Data Anythingable repr)
101 data instance Data Bottomable repr a where
102 Bottom :: Data Bottomable repr a
103 instance Bottomable repr => Derivable (Data Bottomable repr) where
104 derive Bottom{} = bottom
107 data instance Data (Constantable c) repr a where
108 Constant :: {-Typeable c =>-} c -> Data (Constantable c) repr c
109 instance Constantable c repr => Derivable (Data (Constantable c) repr) where
111 Constant x -> constant x
113 ( Constantable c repr
115 ) => Constantable c (SomeData repr) where
116 constant c = SomeData (Constant c)
117 instance {-Typeable c =>-} Constantable c (Data (Constantable c) repr) where
121 data instance Data Eitherable repr a where
122 Left :: Data Eitherable repr (l -> Either l r)
123 Right :: Data Eitherable repr (r -> Either l r)
124 instance Eitherable repr => Derivable (Data Eitherable repr) where
130 ) => Eitherable (SomeData repr) where
132 right = SomeData Right
133 instance Eitherable (Data Eitherable repr) where
138 data instance Data Equalable repr a where
139 Equal :: Eq.Eq a => Data Equalable repr (a -> a -> Bool)
140 instance Equalable repr => Derivable (Data Equalable repr) where
145 ) => Equalable (SomeData repr) where
146 equal = SomeData Equal
147 instance Equalable (Data Equalable repr) where
151 data instance Data IfThenElseable repr a where
153 SomeData repr Bool ->
156 Data IfThenElseable repr a
157 instance IfThenElseable repr => Derivable (Data IfThenElseable repr) where
159 IfThenElse test ok ko -> ifThenElse (derive test) (derive ok) (derive ko)
161 ( IfThenElseable repr
162 ) => IfThenElseable (SomeData repr) where
163 ifThenElse test ok ko = SomeData (IfThenElse test ok ko)
164 instance IfThenElseable repr => IfThenElseable (Data IfThenElseable repr) where
165 ifThenElse test ok ko = IfThenElse (SomeData test) (SomeData ok) (SomeData ko)
168 data instance Data Listable repr a where
169 Cons :: Data Listable repr (a -> [a] -> [a])
170 Nil :: Data Listable repr [a]
172 instance Listable repr => Derivable (Data Listable repr) where
178 ) => Listable (SomeData repr) where
181 instance Listable (Data Listable repr) where
186 data instance Data Maybeable repr a where
187 Nothing :: Data Maybeable repr (Maybe a)
188 Just :: Data Maybeable repr (a -> Maybe a)
189 instance Maybeable repr => Derivable (Data Maybeable repr) where
195 ) => Maybeable (SomeData repr) where
196 nothing = SomeData Nothing
198 instance Maybeable (Data Maybeable repr) where