]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/List.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / List.hs
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
11
12 import Data.Proxy (Proxy(..))
13 import Data.Type.Equality ((:~:)(Refl))
14
15 import Language.Symantic.Type
16 import Language.Symantic.Trans.Common
17 import Language.Symantic.Expr.Root
18 import Language.Symantic.Expr.Error
19 import Language.Symantic.Expr.From
20 import Language.Symantic.Expr.Lambda
21
22 -- * Class 'Sym_List_Lam'
23 -- | Symantic.
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]
28
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
37 list_filter
38 :: repr (Lambda lam a Bool)
39 -> repr [a]
40 -> repr [a]
41
42 default list_filter
43 :: Trans t repr
44 => t repr (Lambda lam a Bool)
45 -> t repr [a]
46 -> t repr [a]
47 list_filter = trans_map2 list_filter
48
49 -- * Type 'Expr_List'
50 -- | Expression.
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
56
57 instance Constraint_Type1 Functor (Type_List root) where
58 constraint_type1 _c (Type_Type1 _ _) = Just Dict
59 instance Constraint_Type1 Applicative (Type_List root) where
60 constraint_type1 _c (Type_Type1 _ _) = Just Dict
61
62 -- | Parsing utility to check that the given type is a 'Type_List'
63 -- or raise 'Error_Expr_Type_mismatch'.
64 check_type_list
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)
72 )
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
78 Just ty_l -> k ty_l
79 Nothing -> Left $
80 error_expr ex $
81 Error_Expr_Type_mismatch ast
82 (Exists_Type (type_list $ type_var0 SZero
83 :: Type_Root_of_Expr ex [Var0]))
84 (Exists_Type ty)
85
86 -- | Parse 'list_empty'.
87 list_empty_from
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
96 ) => ast
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 $
102 const list_empty
103
104 -- | Parse 'list_cons'.
105 list_cons_from
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)
109 , Expr_from ast 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
115 ) => ast -> ast
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)
126
127 -- | Parse 'list'.
128 list_from
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
135 , Expr_from ast 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
142 ) => ast -> [ast]
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
149 where
150 go :: Type_Root_of_Expr root ty_a
151 -> [Forall_Repr_with_Context root hs ty_a]
152 -> [ast]
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
162
163 -- | Parse 'list_filter'.
164 list_filter_from
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)
169 , Expr_from ast 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
178 ) => ast -> ast
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)