]> 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 instance Constraint_Type1 Traversable (Type_List root) where
62 constraint_type1 _c Type_Type1{} = Just Dict
63
64 -- | Parsing utility to check that the given type is a 'Type_List'
65 -- or raise 'Error_Expr_Type_mismatch'.
66 check_type_list
67 :: forall ast ex root ty h ret.
68 ( root ~ Root_of_Expr ex
69 , ty ~ Type_Root_of_Expr ex
70 , Lift_Type Type_List (Type_of_Expr root)
71 , Unlift_Type Type_List (Type_of_Expr root)
72 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
73 (Error_of_Expr ast root)
74 )
75 => Proxy ex -> ast -> ty h
76 -> (Type_List ty h -> Either (Error_of_Expr ast root) ret)
77 -> Either (Error_of_Expr ast root) ret
78 check_type_list ex ast ty k =
79 case unlift_type $ unType_Root ty of
80 Just ty_l -> k ty_l
81 Nothing -> Left $
82 error_expr ex $
83 Error_Expr_Type_mismatch ast
84 (Exists_Type (type_list $ type_var0 SZero
85 :: Type_Root_of_Expr ex [Var0]))
86 (Exists_Type ty)
87
88 -- | Parse 'list_empty'.
89 list_empty_from
90 :: forall root lam ty ty_root ast hs ret.
91 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
92 , ty_root ~ Type_Root_of_Expr root
93 , Type_from ast ty_root
94 , Lift_Type_Root Type_List ty_root
95 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
96 (Error_of_Expr ast root)
97 , Root_of_Expr root ~ root
98 ) => ast
99 -> Expr_From ast (Expr_List lam root) hs ret
100 list_empty_from ast_ty_a ex ast _ctx k =
101 either (\err -> Left $ error_expr ex $ Error_Expr_Type err ast) id $
102 type_from (Proxy::Proxy ty_root) ast_ty_a $ \ty_a -> Right $
103 k (type_list ty_a) $ Forall_Repr_with_Context $
104 const list_empty
105
106 -- | Parse 'list_cons'.
107 list_cons_from
108 :: forall root lam ty ast hs ret.
109 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
110 , Eq_Type (Type_Root_of_Expr root)
111 , Expr_from ast root
112 , Lift_Type Type_List (Type_of_Expr root)
113 , Unlift_Type Type_List (Type_of_Expr root)
114 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
115 (Error_of_Expr ast root)
116 , Root_of_Expr root ~ root
117 ) => ast -> ast
118 -> Expr_From ast (Expr_List lam root) hs ret
119 list_cons_from ast_a ast_l ex ast ctx k =
120 expr_from (Proxy::Proxy root) ast_a ctx $
121 \(ty_a::Type_Root_of_Expr root h_a) (Forall_Repr_with_Context a) ->
122 expr_from (Proxy::Proxy root) ast_l ctx $
123 \(ty_l::Type_Root_of_Expr root h_l) (Forall_Repr_with_Context l) ->
124 check_type_list ex ast ty_l $ \(Type_Type1 _ ty_l_a) ->
125 check_eq_type ex ast ty_a ty_l_a $ \Refl ->
126 k (type_list ty_a) $ Forall_Repr_with_Context $
127 \c -> list_cons (a c) (l c)
128
129 -- | Parse 'list'.
130 list_from
131 :: forall root lam ex ty ty_root ast hs ret.
132 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
133 , ty_root ~ Type_Root_of_Expr root
134 , ex ~ Expr_List lam root
135 , Eq_Type (Type_Root_of_Expr root)
136 , Type_from ast ty_root
137 , Expr_from ast root
138 , Lift_Type Type_List (Type_of_Expr root)
139 , Unlift_Type Type_List (Type_of_Expr root)
140 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
141 (Error_of_Expr ast root)
142 , Root_of_Expr root ~ root
143 , Root_of_Type ty_root ~ ty_root
144 ) => ast -> [ast]
145 -> Expr_From ast ex hs ret
146 list_from ast_ty_a ast_as =
147 case type_from (Proxy::Proxy ty_root)
148 ast_ty_a (Right . Exists_Type) of
149 Left err -> \ex ast _ctx _k -> Left $ error_expr ex $ Error_Expr_Type err ast
150 Right (Exists_Type ty_a) -> go ty_a [] ast_as
151 where
152 go :: Type_Root_of_Expr root ty_a
153 -> [Forall_Repr_with_Context root hs ty_a]
154 -> [ast]
155 -> Expr_From ast (Expr_List lam root) hs ret
156 go ty_a as [] _ex _ast _ctx k =
157 k (type_list ty_a) $ Forall_Repr_with_Context $
158 \c -> list ((\(Forall_Repr_with_Context a) -> a c) Prelude.<$> reverse as)
159 go ty_a as (ast_x:ast_xs) ex ast ctx k =
160 expr_from (Proxy::Proxy root) ast_x ctx $
161 \(ty_x::Type_Root_of_Expr root h_x) x ->
162 check_eq_type ex ast ty_a ty_x $ \Refl ->
163 go ty_a (x:as) ast_xs ex ast ctx k
164
165 -- | Parse 'list_filter'.
166 list_filter_from
167 :: forall root lam ty ty_root ast hs ret.
168 ( ty ~ Type_Root_of_Expr (Expr_List lam root)
169 , ty_root ~ Type_of_Expr root
170 , Eq_Type (Type_Root_of_Expr root)
171 , Expr_from ast root
172 , Lift_Type Type_Bool ty_root
173 , Lift_Type (Type_Fun lam) ty_root
174 , Unlift_Type (Type_Fun lam) ty_root
175 , Lift_Type Type_List ty_root
176 , Unlift_Type Type_List ty_root
177 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
178 (Error_of_Expr ast root)
179 , Root_of_Expr root ~ root
180 ) => ast -> ast
181 -> Expr_From ast (Expr_List lam root) hs ret
182 list_filter_from ast_f ast_l ex ast ctx k =
183 expr_from (Proxy::Proxy root) ast_f ctx $
184 \(ty_f::Type_Root_of_Expr root h_f) (Forall_Repr_with_Context f) ->
185 expr_from (Proxy::Proxy root) ast_l ctx $
186 \(ty_l::Type_Root_of_Expr root h_l) (Forall_Repr_with_Context l) ->
187 check_type_fun ex ast ty_f $ \(Type_Type2 Proxy ty_f_a ty_f_b
188 :: Type_Fun lam (Type_Root_of_Expr root) h_f) ->
189 check_eq_type ex ast type_bool ty_f_b $ \Refl ->
190 check_type_list ex ast ty_l $ \(Type_Type1 _ ty_l_a) ->
191 check_eq_type ex ast ty_f_a ty_l_a $ \Refl ->
192 k ty_l $ Forall_Repr_with_Context $
193 \c -> list_filter (f c) (l c)