1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=11 #-}
4 -- | Symantic for 'Map'.
5 module Language.Symantic.Compiling.Map where
7 import Control.Monad (liftM, liftM2, liftM3)
8 import Data.Map.Strict (Map)
9 import qualified Data.Map.Strict as Map
11 import Data.Text (Text)
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude hiding (either)
15 import Language.Symantic.Parsing
16 import Language.Symantic.Typing
17 import Language.Symantic.Compiling.Term
18 import Language.Symantic.Interpreting
19 import Language.Symantic.Transforming.Trans
22 class Sym_Map term where
23 map_fromList :: Ord k => term [(k, a)] -> term (Map k a)
24 map_mapWithKey :: term (k -> a -> b) -> term (Map k a) -> term (Map k b)
25 map_lookup :: Ord k => term k -> term (Map k a) -> term (Maybe a)
26 map_keys :: term (Map k a) -> term [k]
27 map_member :: Ord k => term k -> term (Map k a) -> term Bool
28 map_insert :: Ord k => term k -> term a -> term (Map k a) -> term (Map k a)
29 map_delete :: Ord k => term k -> term (Map k a) -> term (Map k a)
30 map_difference :: Ord k => term (Map k a) -> term (Map k b) -> term (Map k a)
31 map_foldrWithKey :: term (k -> a -> b -> b) -> term b -> term (Map k a) -> term b
33 default map_fromList :: (Trans t term, Ord k) => t term [(k, a)] -> t term (Map k a)
34 default map_mapWithKey :: Trans t term => t term (k -> a -> b) -> t term (Map k a) -> t term (Map k b)
35 default map_lookup :: (Trans t term, Ord k) => t term k -> t term (Map k a) -> t term (Maybe a)
36 default map_keys :: Trans t term => t term (Map k a) -> t term [k]
37 default map_member :: (Trans t term, Ord k) => t term k -> t term (Map k a) -> t term Bool
38 default map_insert :: (Trans t term, Ord k) => t term k -> t term a -> t term (Map k a) -> t term (Map k a)
39 default map_delete :: (Trans t term, Ord k) => t term k -> t term (Map k a) -> t term (Map k a)
40 default map_difference :: (Trans t term, Ord k) => t term (Map k a) -> t term (Map k b) -> t term (Map k a)
41 default map_foldrWithKey :: Trans t term => t term (k -> a -> b -> b) -> t term b -> t term (Map k a) -> t term b
43 map_fromList = trans_map1 map_fromList
44 map_mapWithKey = trans_map2 map_mapWithKey
45 map_lookup = trans_map2 map_lookup
46 map_keys = trans_map1 map_keys
47 map_member = trans_map2 map_member
48 map_insert = trans_map3 map_insert
49 map_delete = trans_map2 map_delete
50 map_difference = trans_map2 map_difference
51 map_foldrWithKey = trans_map3 map_foldrWithKey
53 type instance Sym_of_Iface (Proxy Map) = Sym_Map
54 type instance Consts_of_Iface (Proxy Map) = Proxy Map ': Consts_imported_by Map
55 type instance Consts_imported_by Map =
70 instance Sym_Map HostI where
71 map_fromList = liftM Map.fromList
72 map_mapWithKey = liftM2 Map.mapWithKey
73 map_lookup = liftM2 Map.lookup
74 map_keys = liftM Map.keys
75 map_member = liftM2 Map.member
76 map_insert = liftM3 Map.insert
77 map_delete = liftM2 Map.delete
78 map_difference = liftM2 Map.difference
79 map_foldrWithKey = liftM3 Map.foldrWithKey
80 instance Sym_Map TextI where
81 map_fromList = textI_app1 "Map.fromList"
82 map_mapWithKey = textI_app2 "Map.mapWithKey"
83 map_lookup = textI_app2 "Map.lookup"
84 map_keys = textI_app1 "Map.keys"
85 map_member = textI_app2 "Map.member"
86 map_insert = textI_app3 "Map.insert"
87 map_delete = textI_app2 "Map.delete"
88 map_difference = textI_app2 "Map.difference"
89 map_foldrWithKey = textI_app3 "Map.foldrWithKey"
90 instance (Sym_Map r1, Sym_Map r2) => Sym_Map (DupI r1 r2) where
91 map_fromList = dupI1 (Proxy @Sym_Map) map_fromList
92 map_mapWithKey = dupI2 (Proxy @Sym_Map) map_mapWithKey
93 map_lookup = dupI2 (Proxy @Sym_Map) map_lookup
94 map_keys = dupI1 (Proxy @Sym_Map) map_keys
95 map_member = dupI2 (Proxy @Sym_Map) map_member
96 map_insert = dupI3 (Proxy @Sym_Map) map_insert
97 map_delete = dupI2 (Proxy @Sym_Map) map_delete
98 map_difference = dupI2 (Proxy @Sym_Map) map_difference
99 map_foldrWithKey = dupI3 (Proxy @Sym_Map) map_foldrWithKey
101 instance Const_from Text cs => Const_from Text (Proxy Map ': cs) where
102 const_from "Map" k = k (ConstZ kind)
103 const_from s k = const_from s $ k . ConstS
104 instance Show_Const cs => Show_Const (Proxy Map ': cs) where
105 show_const ConstZ{} = "Map"
106 show_const (ConstS c) = show_const c
108 instance -- Proj_ConC
110 , Proj_Consts cs (Consts_imported_by Map)
113 ) => Proj_ConC cs (Proxy Map) where
114 proj_conC _ (TyConst q :$ (TyConst c :$ _k))
115 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
116 , Just Refl <- proj_const c (Proxy::Proxy Map)
118 _ | Just Refl <- proj_const q (Proxy::Proxy Functor) -> Just Con
119 | Just Refl <- proj_const q (Proxy::Proxy Foldable) -> Just Con
120 | Just Refl <- proj_const q (Proxy::Proxy Traversable) -> Just Con
122 proj_conC _ (t@(TyConst q) :$ (TyConst c :$ k :$ a))
123 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
124 , Just Refl <- proj_const c (Proxy::Proxy Map)
126 _ | Just Refl <- proj_const q (Proxy::Proxy Eq)
127 , Just Con <- proj_con (t :$ k)
128 , Just Con <- proj_con (t :$ a) -> Just Con
129 | Just Refl <- proj_const q (Proxy::Proxy Ord)
130 , Just Con <- proj_con (t :$ k)
131 , Just Con <- proj_con (t :$ a) -> Just Con
132 | Just Refl <- proj_const q (Proxy::Proxy Monoid)
133 , Just Con <- proj_con (ty @Ord :$ k) -> Just Con
135 proj_conC _c _q = Nothing
136 data instance TokenT meta (ts::[*]) (Proxy Map)
137 = Token_Term_Map_fromList (EToken meta ts)
138 | Token_Term_Map_mapWithKey (EToken meta ts)
139 | Token_Term_Map_lookup (EToken meta ts) (EToken meta ts)
140 | Token_Term_Map_keys (EToken meta ts)
141 | Token_Term_Map_member (EToken meta ts) (EToken meta ts)
142 | Token_Term_Map_insert (EToken meta ts) (EToken meta ts)
143 | Token_Term_Map_delete (EToken meta ts) (EToken meta ts)
144 | Token_Term_Map_difference (EToken meta ts) (EToken meta ts)
145 | Token_Term_Map_foldrWithKey (EToken meta ts)
146 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Map))
147 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Map))
148 instance -- Term_fromI
149 ( Inj_Const (Consts_of_Ifaces is) Map
150 , Inj_Const (Consts_of_Ifaces is) (->)
151 , Inj_Const (Consts_of_Ifaces is) Bool
152 , Inj_Const (Consts_of_Ifaces is) Ord
153 , Inj_Const (Consts_of_Ifaces is) Maybe
154 , Inj_Const (Consts_of_Ifaces is) []
155 , Inj_Const (Consts_of_Ifaces is) (,)
156 , Proj_Con (Consts_of_Ifaces is)
158 ) => Term_fromI is (Proxy Map) where
159 term_fromI tok ctx k =
161 Token_Term_Map_fromList tok_l ->
162 -- fromList :: Ord k => [(k, a)] -> Map k a
163 term_from tok_l ctx $ \ty_l (TermLC l) ->
164 check_type1 (ty @[]) (At (Just tok_l) ty_l) $ \Refl ty_l_t2 ->
165 check_type2 (ty @(,)) (At (Just tok_l) ty_l_t2) $ \Refl ty_k ty_a ->
166 check_con (At (Just tok_l) (ty @Ord :$ ty_k)) $ \Con ->
167 k ((ty @Map :$ ty_k) :$ ty_a) $ TermLC $
168 \c -> map_fromList (l c)
169 Token_Term_Map_mapWithKey tok_k2a2b ->
170 -- map_mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
171 term_from tok_k2a2b ctx $ \ty_k2a2b (TermLC k2a2b) ->
172 check_type2 (ty @(->)) (At (Just tok_k2a2b) ty_k2a2b) $ \Refl ty_k ty_a2b ->
173 check_type2 (ty @(->)) (At (Just tok_k2a2b) ty_a2b) $ \Refl ty_a ty_b ->
174 k ((ty @Map :$ ty_k) :$ ty_a ~> (ty @Map :$ ty_k) :$ ty_b) $ TermLC $
175 \c -> lam $ map_mapWithKey (k2a2b c)
176 Token_Term_Map_lookup tok_k tok_m ->
177 -- lookup :: Ord k => k -> Map k a -> Maybe a
178 term_from tok_k ctx $ \ty_k (TermLC k_) ->
179 term_from tok_m ctx $ \ty_m (TermLC m) ->
180 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k ty_m_a ->
182 (At (Just tok_k) ty_k)
183 (At (Just tok_m) ty_m_k) $ \Refl ->
184 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
185 k (ty @Maybe :$ ty_m_a) $ TermLC $
186 \c -> map_lookup (k_ c) (m c)
187 Token_Term_Map_keys tok_m ->
188 -- keys :: Map k a -> [k]
189 term_from tok_m ctx $ \ty_m (TermLC m) ->
190 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k _ty_m_a ->
191 k (ty @[] :$ ty_m_k) $ TermLC $
193 Token_Term_Map_member tok_k tok_m ->
194 -- member :: Ord k => k -> Map k a -> Bool
195 term_from tok_k ctx $ \ty_k (TermLC k_) ->
196 term_from tok_m ctx $ \ty_m (TermLC m) ->
197 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k _ty_m_a ->
199 (At (Just tok_k) ty_k)
200 (At (Just tok_m) ty_m_k) $ \Refl ->
201 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
202 k (ty @Bool) $ TermLC $
203 \c -> map_member (k_ c) (m c)
204 Token_Term_Map_insert tok_k tok_a ->
205 -- insert :: Ord k => k -> a -> Map k a -> Map k a
206 term_from tok_k ctx $ \ty_k (TermLC k_) ->
207 term_from tok_a ctx $ \ty_a (TermLC a) ->
208 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
209 k ((ty @Map :$ ty_k) :$ ty_a ~> (ty @Map :$ ty_k) :$ ty_a) $ TermLC $
210 \c -> lam $ map_insert (k_ c) (a c)
211 Token_Term_Map_delete tok_k tok_m ->
212 -- delete :: Ord k => k -> Map k a -> Map k a
213 term_from tok_k ctx $ \ty_k (TermLC k_) ->
214 term_from tok_m ctx $ \ty_m (TermLC m) ->
215 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k ty_m_a ->
217 (At (Just tok_k) ty_k)
218 (At (Just tok_m) ty_m_k) $ \Refl ->
219 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
220 k (((ty @Map) :$ ty_k) :$ ty_m_a) $ TermLC $
221 \c -> map_delete (k_ c) (m c)
222 Token_Term_Map_difference tok_ma tok_mb ->
223 -- difference :: Ord k => Map k a -> Map k b -> Map k a
224 term_from tok_ma ctx $ \ty_ma (TermLC ma) ->
225 term_from tok_mb ctx $ \ty_mb (TermLC mb) ->
226 check_type2 (ty @Map) (At (Just tok_ma) ty_ma) $ \Refl ty_ma_k ty_ma_a ->
227 check_type2 (ty @Map) (At (Just tok_mb) ty_mb) $ \Refl ty_mb_k _ty_mb_b ->
229 (At (Just tok_ma) ty_ma_k)
230 (At (Just tok_mb) ty_mb_k) $ \Refl ->
231 check_con (At (Just tok_ma) (ty @Ord :$ ty_ma_k)) $ \Con ->
232 k ((ty @Map :$ ty_ma_k) :$ ty_ma_a) $ TermLC $
233 \c -> map_difference (ma c) (mb c)
234 Token_Term_Map_foldrWithKey tok_f ->
235 -- foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
236 term_from tok_f ctx $ \ty_f (TermLC f) ->
237 check_type2 (ty @(->)) (At (Just tok_f) ty_f) $ \Refl ty_k ty_fabb ->
238 check_type2 (ty @(->)) (At (Just tok_f) ty_fabb) $ \Refl ty_a ty_fbb ->
239 check_type2 (ty @(->)) (At (Just tok_f) ty_fbb) $ \Refl ty_b ty_b' ->
241 (At (Just tok_f) ty_b)
242 (At (Just tok_f) ty_b') $ \Refl ->
243 k (ty_b ~> (ty @Map :$ ty_k) :$ ty_a ~> ty_b) $ TermLC $
244 \c -> lam $ \b -> lam $ \m -> map_foldrWithKey (f c) b m