1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE TypeOperators #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 -- | Expression for 'List'.
10 module Language.Symantic.Expr.List where
12 import Data.Proxy (Proxy(..))
13 import Data.Type.Equality ((:~:)(Refl))
15 import Language.Symantic.Type
16 import Language.Symantic.Trans.Common
17 import Language.Symantic.Expr.Common
18 import Language.Symantic.Expr.Lambda
19 import Language.Symantic.Expr.Functor
20 import Language.Symantic.Expr.Applicative
22 -- * Class 'Sym_List_Lam'
24 class Sym_List repr where
25 list_empty :: repr [a]
26 list_cons :: repr a -> repr [a] -> repr [a]
27 list :: [repr a] -> repr [a]
29 default list_empty :: Trans t repr => t repr [a]
30 default list_cons :: Trans t repr => t repr a -> t repr [a] -> t repr [a]
31 default list :: Trans t repr => [t repr a] -> t repr [a]
32 list_empty = trans_lift list_empty
33 list_cons = trans_map2 list_cons
34 list l = trans_lift $ list (trans_apply Prelude.<$> l)
35 -- | Symantic requiring a 'Lambda'.
36 class Sym_List_Lam lam repr where
38 :: repr (Lambda lam a Bool)
44 => t repr (Lambda lam a Bool)
47 list_filter = trans_map2 list_filter
51 data Expr_List (lam:: * -> *) (root:: *)
52 type instance Root_of_Expr (Expr_List lam root) = root
53 type instance Type_of_Expr (Expr_List lam root) = Type_List
54 type instance Sym_of_Expr (Expr_List lam root) repr = (Sym_List repr, Sym_List_Lam lam repr)
55 type instance Error_of_Expr ast (Expr_List lam root) = No_Error_Expr
57 instance Constraint_Type1 Functor_with_Lambda (Type_List root) where
58 constraint_type1 _c (Type_Type1 _ _) = Just Dict
59 instance Constraint_Type1 Applicative_with_Lambda (Type_List root) where
60 constraint_type1 _c (Type_Type1 _ _) = Just Dict
62 -- | Parsing utility to check that the given type is a 'Type_List'
63 -- or raise 'Error_Expr_Type_mismatch'.
65 :: forall ast ex root ty h ret.
66 ( root ~ Root_of_Expr ex
67 , ty ~ Type_Root_of_Expr ex
68 , Lift_Type Type_List (Type_of_Expr root)
69 , Unlift_Type Type_List (Type_of_Expr root)
70 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
71 (Error_of_Expr ast root)
73 => Proxy ex -> ast -> ty h
74 -> (Type_List ty h -> Either (Error_of_Expr ast root) ret)
75 -> Either (Error_of_Expr ast root) ret
76 check_type_list ex ast ty k =
77 case unlift_type $ unType_Root ty of
81 Error_Expr_Type_mismatch ast
82 (Exists_Type (type_list $ type_var0 SZero
83 :: Type_Root_of_Expr ex [Var0]))
86 -- | Parse 'list_empty'.
88 :: forall root lam ty ty_root ast hs ret.
89 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
90 , ty_root ~ Type_Root_of_Expr root
91 , Type_from ast ty_root
92 , Lift_Type_Root Type_List ty_root
93 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
94 (Error_of_Expr ast root)
95 , Root_of_Expr root ~ root
97 -> Expr_From ast (Expr_List lam root) hs ret
98 list_empty_from ast_ty_a ex ast _ctx k =
99 either (\err -> Left $ error_expr ex $ Error_Expr_Type err ast) id $
100 type_from (Proxy::Proxy ty_root) ast_ty_a $ \ty_a -> Right $
101 k (type_list ty_a) $ Forall_Repr_with_Context $
104 -- | Parse 'list_cons'.
106 :: forall root lam ty ast hs ret.
107 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
108 , Eq_Type (Type_Root_of_Expr root)
110 , Lift_Type Type_List (Type_of_Expr root)
111 , Unlift_Type Type_List (Type_of_Expr root)
112 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
113 (Error_of_Expr ast root)
114 , Root_of_Expr root ~ root
116 -> Expr_From ast (Expr_List lam root) hs ret
117 list_cons_from ast_a ast_l ex ast ctx k =
118 expr_from (Proxy::Proxy root) ast_a ctx $
119 \(ty_a::Type_Root_of_Expr root h_a) (Forall_Repr_with_Context a) ->
120 expr_from (Proxy::Proxy root) ast_l ctx $
121 \(ty_l::Type_Root_of_Expr root h_l) (Forall_Repr_with_Context l) ->
122 check_type_list ex ast ty_l $ \(Type_Type1 _ ty_l_a) ->
123 check_eq_type ex ast ty_a ty_l_a $ \Refl ->
124 k (type_list ty_a) $ Forall_Repr_with_Context $
125 \c -> list_cons (a c) (l c)
129 :: forall root lam ex ty ty_root ast hs ret.
130 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
131 , ty_root ~ Type_Root_of_Expr root
132 , ex ~ Expr_List lam root
133 , Eq_Type (Type_Root_of_Expr root)
134 , Type_from ast ty_root
136 , Lift_Type Type_List (Type_of_Expr root)
137 , Unlift_Type Type_List (Type_of_Expr root)
138 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
139 (Error_of_Expr ast root)
140 , Root_of_Expr root ~ root
141 , Root_of_Type ty_root ~ ty_root
143 -> Expr_From ast ex hs ret
144 list_from ast_ty_a ast_as =
145 case type_from (Proxy::Proxy ty_root)
146 ast_ty_a (Right . Exists_Type) of
147 Left err -> \ex ast _ctx _k -> Left $ error_expr ex $ Error_Expr_Type err ast
148 Right (Exists_Type ty_a) -> go ty_a [] ast_as
150 go :: Type_Root_of_Expr root ty_a
151 -> [Forall_Repr_with_Context root hs ty_a]
153 -> Expr_From ast (Expr_List lam root) hs ret
154 go ty_a as [] _ex _ast _ctx k =
155 k (type_list ty_a) $ Forall_Repr_with_Context $
156 \c -> list ((\(Forall_Repr_with_Context a) -> a c) Prelude.<$> reverse as)
157 go ty_a as (ast_x:ast_xs) ex ast ctx k =
158 expr_from (Proxy::Proxy root) ast_x ctx $
159 \(ty_x::Type_Root_of_Expr root h_x) x ->
160 check_eq_type ex ast ty_a ty_x $ \Refl ->
161 go ty_a (x:as) ast_xs ex ast ctx k
163 -- | Parse 'list_filter'.
165 :: forall root lam ty ty_root ast hs ret.
166 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
167 , ty_root ~ Type_of_Expr root
168 , Eq_Type (Type_Root_of_Expr root)
170 , Lift_Type Type_Bool ty_root
171 , Lift_Type (Type_Fun lam) ty_root
172 , Unlift_Type (Type_Fun lam) ty_root
173 , Lift_Type Type_List ty_root
174 , Unlift_Type Type_List ty_root
175 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
176 (Error_of_Expr ast root)
177 , Root_of_Expr root ~ root
179 -> Expr_From ast (Expr_List lam root) hs ret
180 list_filter_from ast_f ast_l ex ast ctx k =
181 expr_from (Proxy::Proxy root) ast_f ctx $
182 \(ty_f::Type_Root_of_Expr root h_f) (Forall_Repr_with_Context f) ->
183 expr_from (Proxy::Proxy root) ast_l ctx $
184 \(ty_l::Type_Root_of_Expr root h_l) (Forall_Repr_with_Context l) ->
185 check_type_fun ex ast ty_f $ \(Type_Type2 Proxy ty_f_a ty_f_b
186 :: Type_Fun lam (Type_Root_of_Expr root) h_f) ->
187 check_eq_type ex ast type_bool ty_f_b $ \Refl ->
188 check_type_list ex ast ty_l $ \(Type_Type1 _ ty_l_a) ->
189 check_eq_type ex ast ty_f_a ty_l_a $ \Refl ->
190 k ty_l $ Forall_Repr_with_Context $
191 \c -> list_filter (f c) (l c)