1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 -- | Expression for 'Map'.
8 module Language.Symantic.Expr.Map where
10 import Data.Map.Strict as Map
11 import Data.Proxy (Proxy(..))
12 import Data.Type.Equality ((:~:)(Refl))
14 import Language.Symantic.Type
15 import Language.Symantic.Trans.Common
16 import Language.Symantic.Expr.Root
17 import Language.Symantic.Expr.Error
18 import Language.Symantic.Expr.From
19 import Language.Symantic.Expr.Lambda
20 -- import Language.Symantic.Expr.Bool
21 import Language.Symantic.Expr.List
22 import Language.Symantic.Expr.Tuple
24 -- * Class 'Sym_Map_Lam'
26 class Sym_Map repr where
27 map_from_list :: Ord k => repr [(k, a)] -> repr (Map k a)
29 default map_from_list :: (Trans t repr, Ord k) => t repr [(k, a)] -> t repr (Map k a)
30 map_from_list = trans_map1 map_from_list
32 class Sym_Map_Lam lam repr where
34 :: repr (Lambda lam a b)
40 => t repr (Lambda lam a b)
43 map_map = trans_map2 map_map
45 -- | Parsing utility to check that the given type is a 'Type_List'
46 -- or raise 'Error_Expr_Type_mismatch'.
48 :: forall ast ex root ty h ret.
49 ( root ~ Root_of_Expr ex
50 , ty ~ Type_Root_of_Expr ex
51 , Lift_Type Type_Map (Type_of_Expr root)
52 , Unlift_Type Type_Map (Type_of_Expr root)
53 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
54 (Error_of_Expr ast root)
56 => Proxy ex -> ast -> ty h
57 -> (Type_Map ty h -> Either (Error_of_Expr ast root) ret)
58 -> Either (Error_of_Expr ast root) ret
59 check_type_map ex ast ty k =
60 case unlift_type $ unType_Root ty of
64 Error_Expr_Type_mismatch ast
65 (Exists_Type (type_map (type_var0 SZero) (type_var0 $ SSucc SZero)
66 :: Type_Root_of_Expr ex (Map Var0 Var0)))
69 -- | Parse 'map_from_list'.
71 :: forall root lam ty ty_root ast hs ret.
72 ( ty ~ Type_Root_of_Expr (Expr_Map lam root)
73 , ty_root ~ Type_of_Expr root
75 , Lift_Type Type_List ty_root
76 , Unlift_Type Type_List ty_root
77 , Lift_Type Type_Map ty_root
78 , Lift_Type Type_Tuple2 ty_root
79 , Unlift_Type Type_Tuple2 ty_root
80 , Constraint_Type Ord ty
81 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
82 (Error_of_Expr ast root)
83 , Root_of_Expr root ~ root
85 -> Expr_From ast (Expr_Map lam root) hs ret
86 map_from_list_from ast_l ex ast ctx k =
87 expr_from (Proxy::Proxy root) ast_l ctx $
88 \(ty_l::Type_Root_of_Expr root h_l) (Forall_Repr_with_Context l) ->
89 check_type_list ex ast ty_l $ \(Type_Type1 _ ty_l_t) ->
90 check_type_tuple2 ex ast ty_l_t $ \(Type_Type2 Proxy ty_k ty_a) ->
91 check_constraint_type ex (Proxy::Proxy Ord) ast ty_k $ \Dict ->
92 k (type_map ty_k ty_a) $ Forall_Repr_with_Context $
93 \c -> map_from_list (l c)
97 :: forall root lam ty ty_root ast hs ret.
98 ( ty ~ Type_Root_of_Expr (Expr_Map lam root)
99 , ty_root ~ Type_of_Expr root
102 , Lift_Type (Type_Fun lam) ty_root
103 , Unlift_Type (Type_Fun lam) ty_root
104 , Lift_Type Type_Map ty_root
105 , Unlift_Type Type_Map ty_root
106 , Constraint_Type Ord ty
107 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
108 (Error_of_Expr ast root)
109 , Root_of_Expr root ~ root
111 -> Expr_From ast (Expr_Map lam root) hs ret
112 map_map_from ast_f ast_m ex ast ctx k =
113 expr_from (Proxy::Proxy root) ast_f ctx $
114 \(ty_f::Type_Root_of_Expr root h_f) (Forall_Repr_with_Context f) ->
115 expr_from (Proxy::Proxy root) ast_m ctx $
116 \(ty_m::Type_Root_of_Expr root h_m) (Forall_Repr_with_Context m) ->
117 check_type_fun ex ast ty_f $ \(Type_Type2 Proxy ty_f_a ty_f_b
118 :: Type_Fun lam (Type_Root_of_Expr root) h_f) ->
119 check_type_map ex ast ty_m $ \(Type_Type2 Proxy ty_m_k ty_m_a) ->
120 check_eq_type ex ast ty_f_a ty_m_a $ \Refl ->
121 check_constraint_type ex (Proxy::Proxy Ord) ast ty_m_k $ \Dict ->
122 k (type_map ty_m_k ty_f_b) $ Forall_Repr_with_Context $
123 \c -> map_map (f c) (m c)
127 data Expr_Map (lam:: * -> *) (root:: *)
128 type instance Root_of_Expr (Expr_Map lam root) = root
129 type instance Type_of_Expr (Expr_Map lam root) = Type_Map
130 type instance Sym_of_Expr (Expr_Map lam root) repr = (Sym_Map repr, Sym_Map_Lam lam repr)
131 type instance Error_of_Expr ast (Expr_Map lam root) = No_Error_Expr