]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Compiling/Map.hs
Clarify names, and add commentaries.
[haskell/symantic.git] / Language / Symantic / Compiling / Map.hs
1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=12 #-}
4 -- | Symantic for 'Map'.
5 module Language.Symantic.Compiling.Map where
6
7 import Control.Monad (liftM, liftM2, liftM3)
8 import Data.Map.Strict (Map)
9 import qualified Data.Map.Strict as Map
10 import Data.Proxy
11 import Data.Text (Text)
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude hiding (either)
14
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
20
21 -- * Class 'Sym_Map'
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
32
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
42
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
52
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 =
56 [ Proxy (->)
57 , Proxy []
58 , Proxy (,)
59 , Proxy Bool
60 , Proxy Eq
61 , Proxy Foldable
62 , Proxy Functor
63 , Proxy Maybe
64 , Proxy Monad
65 , Proxy Monoid
66 , Proxy Ord
67 , Proxy Traversable
68 , Proxy Show
69 ]
70
71 instance Sym_Map HostI where
72 map_fromList = liftM Map.fromList
73 map_mapWithKey = liftM2 Map.mapWithKey
74 map_lookup = liftM2 Map.lookup
75 map_keys = liftM Map.keys
76 map_member = liftM2 Map.member
77 map_insert = liftM3 Map.insert
78 map_delete = liftM2 Map.delete
79 map_difference = liftM2 Map.difference
80 map_foldrWithKey = liftM3 Map.foldrWithKey
81 instance Sym_Map TextI where
82 map_fromList = textI_app1 "Map.fromList"
83 map_mapWithKey = textI_app2 "Map.mapWithKey"
84 map_lookup = textI_app2 "Map.lookup"
85 map_keys = textI_app1 "Map.keys"
86 map_member = textI_app2 "Map.member"
87 map_insert = textI_app3 "Map.insert"
88 map_delete = textI_app2 "Map.delete"
89 map_difference = textI_app2 "Map.difference"
90 map_foldrWithKey = textI_app3 "Map.foldrWithKey"
91 instance (Sym_Map r1, Sym_Map r2) => Sym_Map (DupI r1 r2) where
92 map_fromList = dupI1 (Proxy @Sym_Map) map_fromList
93 map_mapWithKey = dupI2 (Proxy @Sym_Map) map_mapWithKey
94 map_lookup = dupI2 (Proxy @Sym_Map) map_lookup
95 map_keys = dupI1 (Proxy @Sym_Map) map_keys
96 map_member = dupI2 (Proxy @Sym_Map) map_member
97 map_insert = dupI3 (Proxy @Sym_Map) map_insert
98 map_delete = dupI2 (Proxy @Sym_Map) map_delete
99 map_difference = dupI2 (Proxy @Sym_Map) map_difference
100 map_foldrWithKey = dupI3 (Proxy @Sym_Map) map_foldrWithKey
101
102 instance Const_from Text cs => Const_from Text (Proxy Map ': cs) where
103 const_from "Map" k = k (ConstZ kind)
104 const_from s k = const_from s $ k . ConstS
105 instance Show_Const cs => Show_Const (Proxy Map ': cs) where
106 show_const ConstZ{} = "Map"
107 show_const (ConstS c) = show_const c
108
109 instance -- Proj_ConC
110 ( Proj_Const cs Map
111 , Proj_Consts cs (Consts_imported_by Map)
112 , Proj_Con cs
113 , Inj_Const cs Ord
114 ) => Proj_ConC cs (Proxy Map) where
115 proj_conC _ (TyConst q :$ (TyConst c :$ _k))
116 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
117 , Just Refl <- proj_const c (Proxy::Proxy Map)
118 = case () of
119 _ | Just Refl <- proj_const q (Proxy::Proxy Functor) -> Just Con
120 | Just Refl <- proj_const q (Proxy::Proxy Foldable) -> Just Con
121 | Just Refl <- proj_const q (Proxy::Proxy Traversable) -> Just Con
122 _ -> Nothing
123 proj_conC _ (t@(TyConst q) :$ (TyConst c :$ k :$ a))
124 | Just Refl <- eq_skind (kind_of_const c) (SKiType `SKiArrow` SKiType `SKiArrow` SKiType)
125 , Just Refl <- proj_const c (Proxy::Proxy Map)
126 = case () of
127 _ | Just Refl <- proj_const q (Proxy::Proxy Eq)
128 , Just Con <- proj_con (t :$ k)
129 , Just Con <- proj_con (t :$ a) -> Just Con
130 | Just Refl <- proj_const q (Proxy::Proxy Ord)
131 , Just Con <- proj_con (t :$ k)
132 , Just Con <- proj_con (t :$ a) -> Just Con
133 | Just Refl <- proj_const q (Proxy::Proxy Monoid)
134 , Just Con <- proj_con (ty @Ord :$ k) -> Just Con
135 | Just Refl <- proj_const q (Proxy::Proxy Show)
136 , Just Con <- proj_con (t :$ k)
137 , Just Con <- proj_con (t :$ a) -> Just Con
138 _ -> Nothing
139 proj_conC _c _q = Nothing
140 data instance TokenT meta (ts::[*]) (Proxy Map)
141 = Token_Term_Map_fromList (EToken meta ts)
142 | Token_Term_Map_mapWithKey (EToken meta ts)
143 | Token_Term_Map_lookup (EToken meta ts) (EToken meta ts)
144 | Token_Term_Map_keys (EToken meta ts)
145 | Token_Term_Map_member (EToken meta ts) (EToken meta ts)
146 | Token_Term_Map_insert (EToken meta ts) (EToken meta ts)
147 | Token_Term_Map_delete (EToken meta ts) (EToken meta ts)
148 | Token_Term_Map_difference (EToken meta ts) (EToken meta ts)
149 | Token_Term_Map_foldrWithKey (EToken meta ts)
150 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Map))
151 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Map))
152 instance -- CompileI
153 ( Inj_Const (Consts_of_Ifaces is) Map
154 , Inj_Const (Consts_of_Ifaces is) (->)
155 , Inj_Const (Consts_of_Ifaces is) Bool
156 , Inj_Const (Consts_of_Ifaces is) Ord
157 , Inj_Const (Consts_of_Ifaces is) Maybe
158 , Inj_Const (Consts_of_Ifaces is) []
159 , Inj_Const (Consts_of_Ifaces is) (,)
160 , Proj_Con (Consts_of_Ifaces is)
161 , Compile is
162 ) => CompileI is (Proxy Map) where
163 compileI tok ctx k =
164 case tok of
165 Token_Term_Map_fromList tok_l ->
166 -- fromList :: Ord k => [(k, a)] -> Map k a
167 compileO tok_l ctx $ \ty_l (TermO l) ->
168 check_type1 (ty @[]) (At (Just tok_l) ty_l) $ \Refl ty_l_t2 ->
169 check_type2 (ty @(,)) (At (Just tok_l) ty_l_t2) $ \Refl ty_k ty_a ->
170 check_con (At (Just tok_l) (ty @Ord :$ ty_k)) $ \Con ->
171 k ((ty @Map :$ ty_k) :$ ty_a) $ TermO $
172 \c -> map_fromList (l c)
173 Token_Term_Map_mapWithKey tok_k2a2b ->
174 -- map_mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
175 compileO tok_k2a2b ctx $ \ty_k2a2b (TermO k2a2b) ->
176 check_type2 (ty @(->)) (At (Just tok_k2a2b) ty_k2a2b) $ \Refl ty_k ty_a2b ->
177 check_type2 (ty @(->)) (At (Just tok_k2a2b) ty_a2b) $ \Refl ty_a ty_b ->
178 k ((ty @Map :$ ty_k) :$ ty_a ~> (ty @Map :$ ty_k) :$ ty_b) $ TermO $
179 \c -> lam $ map_mapWithKey (k2a2b c)
180 Token_Term_Map_lookup tok_k tok_m ->
181 -- lookup :: Ord k => k -> Map k a -> Maybe a
182 compileO tok_k ctx $ \ty_k (TermO k_) ->
183 compileO tok_m ctx $ \ty_m (TermO m) ->
184 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k ty_m_a ->
185 check_type
186 (At (Just tok_k) ty_k)
187 (At (Just tok_m) ty_m_k) $ \Refl ->
188 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
189 k (ty @Maybe :$ ty_m_a) $ TermO $
190 \c -> map_lookup (k_ c) (m c)
191 Token_Term_Map_keys tok_m ->
192 -- keys :: Map k a -> [k]
193 compileO tok_m ctx $ \ty_m (TermO m) ->
194 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k _ty_m_a ->
195 k (ty @[] :$ ty_m_k) $ TermO $
196 \c -> map_keys (m c)
197 Token_Term_Map_member tok_k tok_m ->
198 -- member :: Ord k => k -> Map k a -> Bool
199 compileO tok_k ctx $ \ty_k (TermO k_) ->
200 compileO tok_m ctx $ \ty_m (TermO m) ->
201 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k _ty_m_a ->
202 check_type
203 (At (Just tok_k) ty_k)
204 (At (Just tok_m) ty_m_k) $ \Refl ->
205 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
206 k (ty @Bool) $ TermO $
207 \c -> map_member (k_ c) (m c)
208 Token_Term_Map_insert tok_k tok_a ->
209 -- insert :: Ord k => k -> a -> Map k a -> Map k a
210 compileO tok_k ctx $ \ty_k (TermO k_) ->
211 compileO tok_a ctx $ \ty_a (TermO a) ->
212 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
213 k ((ty @Map :$ ty_k) :$ ty_a ~> (ty @Map :$ ty_k) :$ ty_a) $ TermO $
214 \c -> lam $ map_insert (k_ c) (a c)
215 Token_Term_Map_delete tok_k tok_m ->
216 -- delete :: Ord k => k -> Map k a -> Map k a
217 compileO tok_k ctx $ \ty_k (TermO k_) ->
218 compileO tok_m ctx $ \ty_m (TermO m) ->
219 check_type2 (ty @Map) (At (Just tok_m) ty_m) $ \Refl ty_m_k ty_m_a ->
220 check_type
221 (At (Just tok_k) ty_k)
222 (At (Just tok_m) ty_m_k) $ \Refl ->
223 check_con (At (Just tok_k) (ty @Ord :$ ty_k)) $ \Con ->
224 k (((ty @Map) :$ ty_k) :$ ty_m_a) $ TermO $
225 \c -> map_delete (k_ c) (m c)
226 Token_Term_Map_difference tok_ma tok_mb ->
227 -- difference :: Ord k => Map k a -> Map k b -> Map k a
228 compileO tok_ma ctx $ \ty_ma (TermO ma) ->
229 compileO tok_mb ctx $ \ty_mb (TermO mb) ->
230 check_type2 (ty @Map) (At (Just tok_ma) ty_ma) $ \Refl ty_ma_k ty_ma_a ->
231 check_type2 (ty @Map) (At (Just tok_mb) ty_mb) $ \Refl ty_mb_k _ty_mb_b ->
232 check_type
233 (At (Just tok_ma) ty_ma_k)
234 (At (Just tok_mb) ty_mb_k) $ \Refl ->
235 check_con (At (Just tok_ma) (ty @Ord :$ ty_ma_k)) $ \Con ->
236 k ((ty @Map :$ ty_ma_k) :$ ty_ma_a) $ TermO $
237 \c -> map_difference (ma c) (mb c)
238 Token_Term_Map_foldrWithKey tok_f ->
239 -- foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
240 compileO tok_f ctx $ \ty_f (TermO f) ->
241 check_type2 (ty @(->)) (At (Just tok_f) ty_f) $ \Refl ty_k ty_fabb ->
242 check_type2 (ty @(->)) (At (Just tok_f) ty_fabb) $ \Refl ty_a ty_fbb ->
243 check_type2 (ty @(->)) (At (Just tok_f) ty_fbb) $ \Refl ty_b ty_b' ->
244 check_type
245 (At (Just tok_f) ty_b)
246 (At (Just tok_f) ty_b') $ \Refl ->
247 k (ty_b ~> (ty @Map :$ ty_k) :$ ty_a ~> ty_b) $ TermO $
248 \c -> lam $ \b -> lam $ \m -> map_foldrWithKey (f c) b m