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.Ratio (Ratio)
14 import Data.Type.Equality ((:~:)(Refl))
15 import GHC.Exts (Constraint)
16 import qualified System.IO as IO
18 import Language.Symantic.Helper.Data.Type.List hiding (Map)
19 import Language.Symantic.Parsing
20 import Language.Symantic.Typing
21 import Language.Symantic.Compiling
22 import Language.Symantic.Interpreting
23 import Language.Symantic.Transforming
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 TyConsts_of_Iface (Proxy MonoFunctor) = Proxy MonoFunctor ': TyConsts_imported_by MonoFunctor
34 type instance TyConsts_imported_by MonoFunctor =
43 ] ++ TyConsts_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 @Sym_MonoFunctor omap
53 ( Read_TyNameR TyName cs rs
54 , Inj_TyConst cs MonoFunctor
55 ) => Read_TyNameR TyName cs (Proxy MonoFunctor ': rs) where
56 read_TyNameR _cs (TyName "MonoFunctor") k = k (ty @MonoFunctor)
57 read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k
58 instance Show_TyConst cs => Show_TyConst (Proxy MonoFunctor ': cs) where
59 show_TyConst TyConstZ{} = "MonoFunctor"
60 show_TyConst (TyConstS c) = show_TyConst c
62 -- * Type 'TyFam_MonoElement'
63 data TyFam_MonoElement
66 type instance TyFam TyFam_MonoElement '[h] = MT.Element (UnProxy h)
68 instance -- Constraint
69 Proj_TyFamC cs TyFam_MonoElement (c::Constraint)
70 instance -- k -> Constraint
71 Proj_TyFamC cs TyFam_MonoElement (c::k -> Constraint)
73 ( Proj_TyConst cs (->)
74 ) => Proj_TyFamC cs TyFam_MonoElement (->) where
75 proj_TyFamC _c _fam ((TyConst c :$ _ty_r :$ ty_a) `TypesS` TypesZ)
76 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
77 , Just Refl <- proj_TyConst c (Proxy @(->))
79 proj_TyFamC _c _fam _ty = Nothing
82 ) => Proj_TyFamC cs TyFam_MonoElement [] where
83 proj_TyFamC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
84 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType)
85 , Just Refl <- proj_TyConst c (Proxy @[])
87 proj_TyFamC _c _fam _ty = Nothing
89 Proj_TyFamC cs TyFam_MonoElement ()
92 ) => Proj_TyFamC cs TyFam_MonoElement (,) where
93 proj_TyFamC _c _fam ((TyConst c :$ _ty_a :$ ty_b) `TypesS` TypesZ)
94 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
95 , Just Refl <- proj_TyConst c (Proxy @(,))
97 proj_TyFamC _c _fam _ty = Nothing
99 Proj_TyFamC cs TyFam_MonoElement Bool
101 Proj_TyFamC cs TyFam_MonoElement Char
103 ( Proj_TyConst cs Either
104 ) => Proj_TyFamC cs TyFam_MonoElement Either where
105 proj_TyFamC _c _fam ((TyConst c :$ _ty_l :$ ty_r) `TypesS` TypesZ)
106 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
107 , Just Refl <- proj_TyConst c (Proxy @Either)
109 proj_TyFamC _c _fam _ty = Nothing
111 Proj_TyFamC cs TyFam_MonoElement Int
113 Proj_TyFamC cs TyFam_MonoElement Integer
116 ) => Proj_TyFamC cs TyFam_MonoElement IO where
117 proj_TyFamC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
118 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType)
119 , Just Refl <- proj_TyConst c (Proxy @IO)
121 proj_TyFamC _c _fam _ty = Nothing
122 instance -- IO.Handle
123 Proj_TyFamC cs TyFam_MonoElement IO.Handle
124 instance -- IO.IOMode
125 Proj_TyFamC cs TyFam_MonoElement IO.IOMode
127 ( Proj_TyConst cs Map
128 ) => Proj_TyFamC cs TyFam_MonoElement Map where
129 proj_TyFamC _c _fam ((TyConst c :$ _ty_k :$ ty_a) `TypesS` TypesZ)
130 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
131 , Just Refl <- proj_TyConst c (Proxy @Map)
133 proj_TyFamC _c _fam _ty = Nothing
135 ( Proj_TyConst cs Maybe
136 ) => Proj_TyFamC cs TyFam_MonoElement Maybe where
137 proj_TyFamC _c _fam ((TyConst c :$ ty_a) `TypesS` TypesZ)
138 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType)
139 , Just Refl <- proj_TyConst c (Proxy @Maybe)
141 proj_TyFamC _c _fam _ty = Nothing
143 Proj_TyFamC cs TyFam_MonoElement Ordering
145 Proj_TyFamC cs TyFam_MonoElement Ratio
147 ( Proj_TyConst cs Text
148 , Inj_TyConst cs (MT.Element Text)
149 ) => Proj_TyFamC cs TyFam_MonoElement Text where
150 proj_TyFamC _c _fam (TyConst c `TypesS` TypesZ)
151 | Just Refl <- eq_skind (kind_of_TyConst c) SKiType
152 , Just Refl <- proj_TyConst c (Proxy @Text)
153 = Just (TyConst inj_TyConst::Type cs (MT.Element Text))
154 proj_TyFamC _c _fam _ty = Nothing
156 instance -- Proj_TyConC
157 ( Proj_TyConst cs MonoFunctor
158 , Proj_TyConsts cs (TyConsts_imported_by MonoFunctor)
159 ) => Proj_TyConC cs (Proxy MonoFunctor) where
160 proj_TyConC _ (TyConst q :$ typ)
161 | Just Refl <- eq_skind (kind_of_TyConst q) (SKiType `SKiArrow` SKiConstraint)
162 , Just Refl <- proj_TyConst q (Proxy @MonoFunctor)
165 | Just Refl <- proj_TyConst c (Proxy @Text) -> Just TyCon
167 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType)
169 _ | Just Refl <- proj_TyConst c (Proxy @[]) -> Just TyCon
170 | Just Refl <- proj_TyConst c (Proxy @IO) -> Just TyCon
171 | Just Refl <- proj_TyConst c (Proxy @Maybe) -> Just TyCon
173 TyConst c :$ _a :$ _b
174 | Just Refl <- eq_skind (kind_of_TyConst c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
176 _ | Just Refl <- proj_TyConst c (Proxy @(->)) -> Just TyCon
177 | Just Refl <- proj_TyConst c (Proxy @(,)) -> Just TyCon
178 | Just Refl <- proj_TyConst c (Proxy @Either) -> Just TyCon
179 | Just Refl <- proj_TyConst c (Proxy @Map) -> Just TyCon
182 proj_TyConC _c _q = Nothing
183 data instance TokenT meta (ts::[*]) (Proxy MonoFunctor)
184 = Token_Term_MonoFunctor_omap (EToken meta ts) (EToken meta ts)
185 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy MonoFunctor))
186 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy MonoFunctor))
188 ( Inj_TyConst cs MonoFunctor
189 , Inj_TyConst cs (->)
191 , Proj_TyFam cs TyFam_MonoElement
193 ) => CompileI cs is (Proxy MonoFunctor) where
196 Token_Term_MonoFunctor_omap tok_f tok_o ->
197 -- omap :: (Element o -> Element o) -> o -> o
198 compileO tok_f ctx $ \ty_f (TermO f) ->
199 compileO tok_o ctx $ \ty_o (TermO m) ->
200 check_TyEq2 (ty @(->)) (At (Just tok_f) ty_f) $ \Refl ty_f_a ty_f_b ->
202 (At (Just tok_f) ty_f_a)
203 (At (Just tok_f) ty_f_b) $ \Refl ->
204 check_TyCon (At (Just tok_f) (ty @MonoFunctor :$ ty_o)) $ \TyCon ->
205 check_TyFam (At (Just tok_o) TyFam_MonoElement) (ty_o `TypesS` TypesZ) $ \ty_o_e ->
208 (At (Just tok_f) ty_f_a) $ \Refl ->
210 \c -> omap (f c) (m c)
211 instance -- TokenizeT
212 Inj_Token meta ts MonoFunctor =>
213 TokenizeT meta ts (Proxy MonoFunctor) where
214 tokenizeT _t = mempty
215 { tokenizers_infix = tokenizeTMod []
216 [ tokenize2 "omap" infixN5 Token_Term_MonoFunctor_omap
219 instance Gram_Term_AtomsT meta ts (Proxy MonoFunctor) g