1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=8 #-}
4 -- | Symantic for 'MonoFunctor'.
5 module Language.Symantic.Lib.MonoFunctor where
7 import Control.Monad (liftM2)
8 import Data.Map.Strict (Map)
9 import Data.MonoTraversable (MonoFunctor)
10 import qualified Data.MonoTraversable as MT
12 import Data.Text (Text)
13 import Data.Type.Equality ((:~:)(Refl))
14 import GHC.Exts (Constraint)
15 import qualified System.IO as IO
17 import Language.Symantic.Helper.Data.Type.List hiding (Map)
18 import Language.Symantic.Parsing
19 import Language.Symantic.Parsing.Grammar
20 import Language.Symantic.Typing
21 import Language.Symantic.Compiling
22 import Language.Symantic.Interpreting
23 import Language.Symantic.Transforming.Trans
25 -- * Class 'Sym_MonoFunctor'
26 class Sym_MonoFunctor term where
27 omap :: MonoFunctor o => term (MT.Element o -> MT.Element o) -> term o -> term o
28 default omap :: (Trans t term, MonoFunctor o)
29 => t term (MT.Element o -> MT.Element o) -> t term o -> t term o
30 omap = trans_map2 omap
32 type instance Sym_of_Iface (Proxy MonoFunctor) = Sym_MonoFunctor
33 type instance Consts_of_Iface (Proxy MonoFunctor) = Proxy MonoFunctor ': Consts_imported_by MonoFunctor
34 type instance Consts_imported_by MonoFunctor =
43 ] ++ Consts_imported_by IO
45 instance Sym_MonoFunctor HostI where
47 instance Sym_MonoFunctor TextI where
49 instance (Sym_MonoFunctor r1, Sym_MonoFunctor r2) => Sym_MonoFunctor (DupI r1 r2) where
50 omap = dupI2 (Proxy @Sym_MonoFunctor) omap
53 ( Read_TypeNameR Type_Name cs rs
54 , Inj_Const cs MonoFunctor
55 ) => Read_TypeNameR Type_Name cs (Proxy MonoFunctor ': rs) where
56 read_typenameR _cs (Type_Name "MonoFunctor") k = k (ty @MonoFunctor)
57 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
58 instance Show_Const cs => Show_Const (Proxy MonoFunctor ': cs) where
59 show_const ConstZ{} = "MonoFunctor"
60 show_const (ConstS c) = show_const c
62 -- * Type 'Fam_MonoElement'
66 type instance Fam Fam_MonoElement '[h] = MT.Element (UnProxy h)
68 instance -- Constraint
69 Proj_FamC cs Fam_MonoElement (c::Constraint)
70 instance -- k -> Constraint
71 Proj_FamC cs Fam_MonoElement (c::k -> Constraint)
73 Proj_FamC cs Fam_MonoElement ()
75 Proj_FamC cs Fam_MonoElement Bool
77 Proj_FamC cs Fam_MonoElement Char
79 Proj_FamC cs Fam_MonoElement Int
81 Proj_FamC cs Fam_MonoElement Integer
84 , Inj_Const cs (MT.Element Text)
85 ) => Proj_FamC cs Fam_MonoElement Text where
86 proj_famC _c _fam (TyConst c `TypesS` TypesZ)
87 | Just Refl <- eq_skind (kind_of_const c) SKiType
88 , Just Refl <- proj_const c (Proxy @Text)
89 = Just (TyConst inj_const::Type cs (MT.Element Text))
90 proj_famC _c _fam _ty = Nothing
93 ) => Proj_FamC cs Fam_MonoElement [] where
94 proj_famC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
95 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
96 , Just Refl <- proj_const c (Proxy @[])
98 proj_famC _c _fam _ty = Nothing
101 ) => Proj_FamC cs Fam_MonoElement IO where
102 proj_famC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
103 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
104 , Just Refl <- proj_const c (Proxy @IO)
106 proj_famC _c _fam _ty = Nothing
107 instance -- IO.Handle
108 Proj_FamC cs Fam_MonoElement IO.Handle
109 instance -- IO.IOMode
110 Proj_FamC cs Fam_MonoElement IO.IOMode
112 ( Proj_Const cs Maybe
113 ) => Proj_FamC cs Fam_MonoElement Maybe where
114 proj_famC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
115 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
116 , Just Refl <- proj_const c (Proxy @Maybe)
118 proj_famC _c _fam _ty = Nothing
121 ) => Proj_FamC cs Fam_MonoElement (->) where
122 proj_famC _c _fam ((TyConst c :$ _ty_r :$ ty_a) `TypesS` TypesZ)
123 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
124 , Just Refl <- proj_const c (Proxy @(->))
126 proj_famC _c _fam _ty = Nothing
129 ) => Proj_FamC cs Fam_MonoElement (,) where
130 proj_famC _c _fam ((TyConst c :$ _ty_a :$ ty_b) `TypesS` TypesZ)
131 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
132 , Just Refl <- proj_const c (Proxy @(,))
134 proj_famC _c _fam _ty = Nothing
136 ( Proj_Const cs Either
137 ) => Proj_FamC cs Fam_MonoElement Either where
138 proj_famC _c _fam ((TyConst c :$ _ty_l :$ ty_r) `TypesS` TypesZ)
139 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
140 , Just Refl <- proj_const c (Proxy @Either)
142 proj_famC _c _fam _ty = Nothing
145 ) => Proj_FamC cs Fam_MonoElement Map where
146 proj_famC _c _fam ((TyConst c :$ _ty_k :$ ty_a) `TypesS` TypesZ)
147 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
148 , Just Refl <- proj_const c (Proxy @Map)
150 proj_famC _c _fam _ty = Nothing
152 instance -- Proj_ConC
153 ( Proj_Const cs MonoFunctor
154 , Proj_Consts cs (Consts_imported_by MonoFunctor)
155 ) => Proj_ConC cs (Proxy MonoFunctor) where
156 proj_conC _ (TyConst q :$ typ)
157 | Just Refl <- eq_skind (kind_of_const q) (SKiType `SKiArrow` SKiConstraint)
158 , Just Refl <- proj_const q (Proxy @MonoFunctor)
161 | Just Refl <- proj_const c (Proxy @Text) -> Just Con
163 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
165 _ | Just Refl <- proj_const c (Proxy @[]) -> Just Con
166 | Just Refl <- proj_const c (Proxy @IO) -> Just Con
167 | Just Refl <- proj_const c (Proxy @Maybe) -> Just Con
169 TyConst c :$ _a :$ _b
170 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
172 _ | Just Refl <- proj_const c (Proxy @(->)) -> Just Con
173 | Just Refl <- proj_const c (Proxy @(,)) -> Just Con
174 | Just Refl <- proj_const c (Proxy @Either) -> Just Con
175 | Just Refl <- proj_const c (Proxy @Map) -> Just Con
178 proj_conC _c _q = Nothing
179 data instance TokenT meta (ts::[*]) (Proxy MonoFunctor)
180 = Token_Term_MonoFunctor_omap (EToken meta ts) (EToken meta ts)
181 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy MonoFunctor))
182 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy MonoFunctor))
184 ( Inj_Const (Consts_of_Ifaces is) MonoFunctor
185 , Inj_Const (Consts_of_Ifaces is) (->)
186 , Proj_Con (Consts_of_Ifaces is)
187 , Proj_Fam (Consts_of_Ifaces is) Fam_MonoElement
189 ) => CompileI is (Proxy MonoFunctor) where
192 Token_Term_MonoFunctor_omap tok_f tok_o ->
193 -- omap :: (Element o -> Element o) -> o -> o
194 compileO tok_f ctx $ \ty_f (TermO f) ->
195 compileO tok_o ctx $ \ty_o (TermO m) ->
196 check_type2 (ty @(->)) (At (Just tok_f) ty_f) $ \Refl ty_f_a ty_f_b ->
198 (At (Just tok_f) ty_f_a)
199 (At (Just tok_f) ty_f_b) $ \Refl ->
200 check_con (At (Just tok_f) (ty @MonoFunctor :$ ty_o)) $ \Con ->
201 check_fam (At (Just tok_o) Fam_MonoElement) (ty_o `TypesS` TypesZ) $ \ty_o_e ->
204 (At (Just tok_f) ty_f_a) $ \Refl ->
206 \c -> omap (f c) (m c)
207 instance -- TokenizeT
208 Inj_Token meta ts MonoFunctor =>
209 TokenizeT meta ts (Proxy MonoFunctor) where
210 tokenizeT _t = mempty
211 { tokenizers_infix = tokenizeTMod []
212 [ tokenize2 "omap" infixN5 Token_Term_MonoFunctor_omap
215 instance Gram_Term_AtomsT meta ts (Proxy MonoFunctor) g