1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE KindSignatures #-}
7 {-# LANGUAGE LambdaCase #-}
8 {-# LANGUAGE MultiParamTypeClasses #-}
9 {-# LANGUAGE PatternSynonyms #-}
10 {-# LANGUAGE RankNTypes #-}
11 {-# LANGUAGE ScopedTypeVariables #-}
12 {-# LANGUAGE StandaloneDeriving #-}
13 {-# LANGUAGE TypeApplications #-}
14 {-# LANGUAGE TypeFamilies #-}
15 {-# LANGUAGE ViewPatterns #-}
16 module Symantic.Typed.Data where
18 import Data.Bool (Bool)
19 import Data.Either (Either)
20 import Data.Kind (Constraint, Type)
21 import Data.Maybe (Maybe)
22 import Type.Reflection (Typeable, (:~~:)(..), eqTypeRep, typeRep)
23 import qualified Data.Eq as Eq
24 import qualified Data.Maybe as Maybe
25 import qualified Data.Function as Fun
27 import Symantic.Typed.Lang
28 import Symantic.Typed.Transformable
31 data SomeData repr a =
33 ( Derive (Data able repr)
35 ) => SomeData (Data able repr a)
37 type instance Derived (SomeData repr) = repr
38 instance Derive (SomeData repr) where
39 derive (SomeData x) = derive x
41 -- ** Type 'TypedRepr'
42 type TypedRepr = Type -> Type
45 -- TODO: neither data families nor data instances
46 -- can have phantom roles with GHC-9's RoleAnnotations,
47 -- hence 'Data.Coerce.coerce' cannot be used on them for now.
48 -- https://gitlab.haskell.org/ghc/ghc/-/issues/8177
49 -- https://gitlab.haskell.org/ghc/ghc/-/wikis/roles#proposal-roles-for-type-families
51 (able :: TypedRepr -> Constraint)
52 :: TypedRepr -> TypedRepr
53 type instance Derived (Data able repr) = repr
55 -- | Convenient utility to pattern-match a 'SomeData'.
56 pattern Data :: Typeable able => Data able repr a -> SomeData repr a
57 pattern Data x <- (unSomeData -> Maybe.Just x)
59 -- | @(unSomeData c :: 'Maybe' ('Data' able repr a))@
60 -- extract the data-constructor from the given 'SomeData'
61 -- iif. it belongs to the @('Data' able repr a)@ data-instance.
65 SomeData repr a -> Maybe (Data able repr a)
66 unSomeData (SomeData (c::Data c repr a)) =
67 case typeRep @able `eqTypeRep` typeRep @c of
68 Maybe.Just HRefl -> Maybe.Just c
69 Maybe.Nothing -> Maybe.Nothing
72 data instance Data Abstractable repr a where
73 (:@) :: SomeData repr (a->b) -> SomeData repr a -> Data Abstractable repr b
74 Lam :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
75 Lam1 :: (SomeData repr a -> SomeData repr b) -> Data Abstractable repr (a->b)
76 Var :: repr a -> Data Abstractable repr a
77 -- FIXME: add constructors
80 ) => Derive (Data Abstractable repr) where
82 f :@ x -> derive f .@ derive x
83 Lam f -> lam (\x -> derive (f (SomeData (Var x))))
84 Lam1 f -> lam1 (\x -> derive (f (SomeData (Var x))))
88 ) => Abstractable (SomeData repr) where
89 f .@ x = SomeData (f :@ x)
90 lam f = SomeData (Lam f)
91 lam1 f = SomeData (Lam1 f)
93 ($) = lam1 (\f -> lam1 (\x -> f .@ x))
94 (.) = lam1 (\f -> lam1 (\g -> lam1 (\x -> f .@ (g .@ x))))
95 const = lam1 (\x -> lam1 (\_y -> x))
96 flip = lam1 (\f -> lam1 (\x -> lam1 (\y -> f .@ y .@ x)))
100 data instance Data Anythingable repr a where
101 Anything :: repr a -> Data Anythingable repr a
105 Derive (Data Anythingable repr) where
107 Anything x -> anything x
108 instance Anythingable (SomeData repr)
109 instance Anythingable (Data Anythingable repr)
112 class Bottomable repr where
114 data instance Data Bottomable repr a where
115 Bottom :: Data Bottomable repr a
116 instance Bottomable repr => Derive (Data Bottomable repr) where
117 derive Bottom{} = bottom
120 data instance Data (Constantable c) repr a where
121 Constant :: {-Typeable c =>-} c -> Data (Constantable c) repr c
122 instance Constantable c repr => Derive (Data (Constantable c) repr) where
124 Constant x -> constant x
126 ( Constantable c repr
128 ) => Constantable c (SomeData repr) where
129 constant c = SomeData (Constant c)
130 instance {-Typeable c =>-} Constantable c (Data (Constantable c) repr) where
134 data instance Data Eitherable repr a where
135 Left :: Data Eitherable repr (l -> Either l r)
136 Right :: Data Eitherable repr (r -> Either l r)
137 instance Eitherable repr => Derive (Data Eitherable repr) where
143 ) => Eitherable (SomeData repr) where
145 right = SomeData Right
146 instance Eitherable (Data Eitherable repr) where
151 data instance Data Equalable repr a where
152 Equal :: Eq.Eq a => Data Equalable repr (a -> a -> Bool)
153 instance Equalable repr => Derive (Data Equalable repr) where
158 ) => Equalable (SomeData repr) where
159 equal = SomeData Equal
160 instance Equalable (Data Equalable repr) where
164 data instance Data IfThenElseable repr a where
166 SomeData repr Bool ->
169 Data IfThenElseable repr a
170 instance IfThenElseable repr => Derive (Data IfThenElseable repr) where
172 IfThenElse test ok ko -> ifThenElse (derive test) (derive ok) (derive ko)
174 ( IfThenElseable repr
175 ) => IfThenElseable (SomeData repr) where
176 ifThenElse test ok ko = SomeData (IfThenElse test ok ko)
177 instance IfThenElseable repr => IfThenElseable (Data IfThenElseable repr) where
178 ifThenElse test ok ko = IfThenElse (SomeData test) (SomeData ok) (SomeData ko)
181 data instance Data Listable repr a where
182 Cons :: Data Listable repr (a -> [a] -> [a])
183 Nil :: Data Listable repr [a]
185 instance Listable repr => Derive (Data Listable repr) where
191 ) => Listable (SomeData repr) where
194 instance Listable (Data Listable repr) where
199 data instance Data Maybeable repr a where
200 Nothing :: Data Maybeable repr (Maybe a)
201 Just :: Data Maybeable repr (a -> Maybe a)
202 instance Maybeable repr => Derive (Data Maybeable repr) where
208 ) => Maybeable (SomeData repr) where
209 nothing = SomeData Nothing
211 instance Maybeable (Data Maybeable repr) where