1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE InstanceSigs #-}
7 {-# LANGUAGE MultiParamTypeClasses #-}
8 {-# LANGUAGE OverloadedStrings #-}
9 {-# LANGUAGE Rank2Types #-}
10 {-# LANGUAGE ScopedTypeVariables #-}
11 {-# LANGUAGE TypeFamilies #-}
12 {-# LANGUAGE TypeOperators #-}
13 {-# LANGUAGE UndecidableInstances #-}
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
15 {-# OPTIONS_GHC -fconstraint-solver-iterations=7 #-}
16 -- | Symantic for 'MonoFunctor'.
17 module Language.Symantic.Compiling.MonoFunctor where
19 import Control.Monad (liftM2)
20 import Data.Map.Strict (Map)
21 import Data.MonoTraversable (MonoFunctor)
22 import qualified Data.MonoTraversable as MT
24 import Data.String (IsString)
25 import Data.Text (Text)
26 import Data.Type.Equality ((:~:)(Refl))
27 import GHC.Exts (Constraint)
28 import qualified System.IO as IO
30 import Language.Symantic.Typing
31 import Language.Symantic.Compiling.Term
32 import Language.Symantic.Interpreting
33 import Language.Symantic.Transforming.Trans
34 import Language.Symantic.Lib.Data.Type.List hiding (Map)
36 -- * Class 'Sym_MonoFunctor'
37 class Sym_MonoFunctor term where
38 omap :: MonoFunctor o => term (MT.Element o -> MT.Element o) -> term o -> term o
39 default omap :: (Trans t term, MonoFunctor o)
40 => t term (MT.Element o -> MT.Element o) -> t term o -> t term o
41 omap = trans_map2 omap
43 type instance Sym_of_Iface (Proxy MonoFunctor) = Sym_MonoFunctor
44 type instance Consts_of_Iface (Proxy MonoFunctor) = Proxy MonoFunctor ': Consts_imported_by MonoFunctor
45 type instance Consts_imported_by MonoFunctor =
54 ] ++ Consts_imported_by IO
56 instance Sym_MonoFunctor HostI where
58 instance Sym_MonoFunctor TextI where
59 omap = textI_app2 "omap"
60 instance (Sym_MonoFunctor r1, Sym_MonoFunctor r2) => Sym_MonoFunctor (DupI r1 r2) where
61 omap = dupI2 sym_MonoFunctor omap
63 instance Const_from Text cs => Const_from Text (Proxy MonoFunctor ': cs) where
64 const_from "MonoFunctor" k = k (ConstZ kind)
65 const_from s k = const_from s $ k . ConstS
66 instance Show_Const cs => Show_Const (Proxy MonoFunctor ': cs) where
67 show_const ConstZ{} = "MonoFunctor"
68 show_const (ConstS c) = show_const c
70 -- * Type 'Fam_MonoElement'
72 type instance Fam Fam_MonoElement h = MT.Element h
73 instance -- Constraint
74 Proj_FamC cs Fam_MonoElement (Proxy (c::Constraint))
75 instance -- k -> Constraint
76 Proj_FamC cs Fam_MonoElement (Proxy (c::k -> Constraint))
78 Proj_FamC cs Fam_MonoElement (Proxy ())
80 Proj_FamC cs Fam_MonoElement (Proxy Bool)
82 Proj_FamC cs Fam_MonoElement (Proxy Char)
84 ( Proj_Const cs String
85 , Inj_Const cs (Fam Fam_MonoElement String)
86 ) => Proj_FamC cs Fam_MonoElement (Proxy String) where
87 proj_famC _c _fam (TyConst c)
88 | Just Refl <- eq_skind (kind_of_const c) SKiType
89 , Just Refl <- proj_const c (Proxy::Proxy String)
90 = Just $ Just (TyConst inj_const::Type cs (Fam Fam_MonoElement String))
91 proj_famC _c _fam _ty = Nothing
94 , Inj_Const cs (Fam Fam_MonoElement Text)
95 ) => Proj_FamC cs Fam_MonoElement (Proxy Text) where
96 proj_famC _c _fam (TyConst c)
97 | Just Refl <- eq_skind (kind_of_const c) SKiType
98 , Just Refl <- proj_const c (Proxy::Proxy Text)
99 = Just $ Just (TyConst inj_const::Type cs (Fam Fam_MonoElement Text))
100 proj_famC _c _fam _ty = Nothing
103 ) => Proj_FamC cs Fam_MonoElement (Proxy []) where
104 proj_famC _c _fam (TyConst c :$ ty_a)
105 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
106 , Just Refl <- proj_const c (Proxy::Proxy [])
108 proj_famC _c _fam _ty = Nothing
111 ) => Proj_FamC cs Fam_MonoElement (Proxy IO) where
112 proj_famC _c _fam (TyConst c :$ ty_a)
113 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
114 , Just Refl <- proj_const c (Proxy::Proxy IO)
116 proj_famC _c _fam _ty = Nothing
117 instance -- IO.Handle
118 Proj_FamC cs Fam_MonoElement (Proxy IO.Handle)
119 instance -- IO.IOMode
120 Proj_FamC cs Fam_MonoElement (Proxy IO.IOMode)
122 ( Proj_Const cs Maybe
123 ) => Proj_FamC cs Fam_MonoElement (Proxy Maybe) where
124 proj_famC _c _fam (TyConst c :$ ty_a)
125 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
126 , Just Refl <- proj_const c (Proxy::Proxy Maybe)
128 proj_famC _c _fam _ty = Nothing
131 ) => Proj_FamC cs Fam_MonoElement (Proxy (->)) where
132 proj_famC _c _fam (TyConst c :$ _ty_r :$ ty_a)
133 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
134 , Just Refl <- proj_const c (Proxy::Proxy (->))
136 proj_famC _c _fam _ty = Nothing
139 ) => Proj_FamC cs Fam_MonoElement (Proxy (,)) where
140 proj_famC _c _fam (TyConst c :$ _ty_a :$ ty_b)
141 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
142 , Just Refl <- proj_const c (Proxy::Proxy (,))
144 proj_famC _c _fam _ty = Nothing
146 ( Proj_Const cs Either
147 ) => Proj_FamC cs Fam_MonoElement (Proxy Either) where
148 proj_famC _c _fam (TyConst c :$ _ty_l :$ ty_r)
149 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
150 , Just Refl <- proj_const c (Proxy::Proxy Either)
152 proj_famC _c _fam _ty = Nothing
155 ) => Proj_FamC cs Fam_MonoElement (Proxy Map) where
156 proj_famC _c _fam (TyConst c :$ _ty_k :$ ty_a)
157 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
158 , Just Refl <- proj_const c (Proxy::Proxy Map)
160 proj_famC _c _fam _ty = Nothing
162 instance -- Proj_ConC
163 ( Proj_Const cs MonoFunctor
164 , Proj_Consts cs (Consts_imported_by MonoFunctor)
165 ) => Proj_ConC cs (Proxy MonoFunctor) where
166 proj_conC _ (TyConst q :$ ty)
167 | Just Refl <- eq_skind (kind_of_const q) (SKiType `SKiArrow` SKiConstraint)
168 , Just Refl <- proj_const q (Proxy::Proxy MonoFunctor)
171 | Just Refl <- proj_const c (Proxy::Proxy String) -> Just Con
172 | Just Refl <- proj_const c (Proxy::Proxy Text) -> Just Con
174 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType)
176 _ | Just Refl <- proj_const c (Proxy::Proxy []) -> Just Con
177 | Just Refl <- proj_const c (Proxy::Proxy IO) -> Just Con
178 | Just Refl <- proj_const c (Proxy::Proxy Maybe) -> Just Con
180 TyConst c :$ _a :$ _b
181 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
183 _ | Just Refl <- proj_const c (Proxy::Proxy (->)) -> Just Con
184 | Just Refl <- proj_const c (Proxy::Proxy (,)) -> Just Con
185 | Just Refl <- proj_const c (Proxy::Proxy Either) -> Just Con
188 proj_conC _c _q = Nothing
189 instance -- Term_fromI
191 , Lexem ast ~ LamVarName
192 , Inj_Const (Consts_of_Ifaces is) MonoFunctor
193 , Inj_Const (Consts_of_Ifaces is) (->)
194 , Proj_Con (Consts_of_Ifaces is)
195 , Proj_Fam (Consts_of_Ifaces is) Fam_MonoElement
197 ) => Term_fromI is (Proxy MonoFunctor) ast where
198 term_fromI ast ctx k =
199 case ast_lexem ast of
201 _ -> Left $ Error_Term_unsupported
204 -- omap :: (Element mono -> Element mono) -> mono -> mono
205 from_ast2 ast $ \ast_f ast_m as ->
206 term_from ast_f ctx $ \ty_f (TermLC f) ->
207 term_from ast_m ctx $ \ty_m (TermLC m) ->
208 check_type2 tyFun ast_f ty_f $ \Refl ty_f_a ty_f_b ->
209 check_type (At (Just ast_f) ty_f_a) (At (Just ast_f) ty_f_b) $ \Refl ->
210 check_constraint (At (Just ast_f) (tyMonoFunctor :$ ty_m)) $ \Con ->
211 check_family (Proxy::Proxy Fam_MonoElement) (At (Just ast) ty_m) $ \ty_m_ele ->
212 check_type (At Nothing ty_m_ele) (At (Just ast_f) ty_f_a) $ \Refl ->
214 \c -> omap (f c) (m c)
216 -- | The 'MonoFunctor' 'Type'
217 tyMonoFunctor :: Inj_Const cs MonoFunctor => Type cs MonoFunctor
218 tyMonoFunctor = TyConst inj_const
220 sym_MonoFunctor :: Proxy Sym_MonoFunctor
221 sym_MonoFunctor = Proxy
223 syMonoFunctor :: IsString a => [Syntax a] -> Syntax a
224 syMonoFunctor = Syntax "MonoFunctor"