]> Git — Sourcephile - haskell/symantic.git/blob - Language/LOL/Symantic/Expr/Lambda.hs
init
[haskell/symantic.git] / Language / LOL / Symantic / Expr / Lambda.hs
1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE GADTs #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE OverloadedStrings #-}
7 {-# LANGUAGE Rank2Types #-}
8 {-# LANGUAGE ScopedTypeVariables #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE UndecidableInstances #-}
11 -- | Expression for /lambda abstraction/s
12 -- in /Higher-Order Abstract Syntax/ (HOAS).
13 module Language.LOL.Symantic.Expr.Lambda where
14
15 import Data.Proxy (Proxy(..))
16 import Data.Type.Equality ((:~:)(Refl))
17
18 import Language.LOL.Symantic.AST
19 import Language.LOL.Symantic.Type
20 import Language.LOL.Symantic.Expr.Common
21 import Language.LOL.Symantic.Repr.Dup
22 import Language.LOL.Symantic.Trans.Common
23
24 -- * Class 'Sym_Lambda'
25
26 -- | Symantic.
27 --
28 -- NOTE: argument @arg@ and result @res@ of 'Lambda'
29 -- wrapped inside 'lam': to control the calling
30 -- in the 'Repr_Host' instance.
31 --
32 -- NOTE: the default definitions supplied for:
33 -- 'app', 'inline', 'val' and 'lazy'
34 -- are there to avoid boilerplate code
35 -- when writting 'Trans' instances which
36 -- do not need to alterate those methods.
37 class (lam ~ Lambda_from_Repr repr) => Sym_Lambda lam repr where
38 -- | This type constructor is used like
39 -- the functional dependency: @repr -> lam@
40 -- (ie. knowing @repr@ one can determine @lam@)
41 -- in order to avoid to introduce a 'Proxy' @lam@
42 -- in 'let_inline', 'let_val' and 'let_lazy'.
43 --
44 -- Distinguishing between @lam@ and @repr@ is used to maintain
45 -- the universal polymorphism of @repr@ in 'Expr_from',
46 -- the downside with this however is that
47 -- to be an instance of 'Sym_Lambda' for all @lam@,
48 -- the @repr@ type of an interpreter
49 -- has to be parameterized by @lam@,
50 -- even though it does not actually need @lam@ to do its work.
51 --
52 -- Basically this means having sometimes to add a type annotation
53 -- to the interpreter call to specify @lam@.
54 type Lambda_from_Repr repr :: {-lam-}(* -> *)
55
56 -- | Lambda application.
57 app :: repr (Lambda lam arg res) -> repr arg -> repr res
58
59 -- | /Call-by-name/ lambda.
60 inline :: (repr arg -> repr res) -> repr (Lambda lam arg res)
61 -- | /Call-by-value/ lambda.
62 val :: (repr arg -> repr res) -> repr (Lambda lam arg res)
63 -- | /Call-by-need/ lambda (aka. /lazyness/): lazy shares its argument, no matter what.
64 lazy :: (repr arg -> repr res) -> repr (Lambda lam arg res)
65
66 default app :: Trans t repr => t repr (Lambda lam arg res) -> t repr arg -> t repr res
67 default inline :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
68 default val :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
69 default lazy :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
70 app f x = trans_lift $ trans_apply f `app` trans_apply x
71 inline f = trans_lift $ inline $ trans_apply . f . trans_lift
72 val f = trans_lift $ val $ trans_apply . f . trans_lift
73 lazy f = trans_lift $ lazy $ trans_apply . f . trans_lift
74
75 -- | Convenient 'inline' wrapper.
76 let_inline
77 :: Sym_Lambda lam repr
78 => repr var -> (repr var -> repr res) -> repr res
79 let_inline x y = inline y `app` x
80 -- | Convenient 'val' wrapper.
81 let_val
82 :: Sym_Lambda lam repr
83 => repr var -> (repr var -> repr res) -> repr res
84 let_val x y = val y `app` x
85 -- | Convenient 'lazy' wrapper.
86 let_lazy
87 :: Sym_Lambda lam repr
88 => repr var -> (repr var -> repr res) -> repr res
89 let_lazy x y = lazy y `app` x
90
91 infixl 5 `app`
92
93 instance -- Sym_Lambda Dup
94 ( Sym_Lambda lam r1
95 , Sym_Lambda lam r2
96 ) => Sym_Lambda lam (Dup r1 r2) where
97 type Lambda_from_Repr (Dup r1 r2) = Lambda_from_Repr r1
98 app (f1 `Dup` f2) (x1 `Dup` x2) = app f1 x1 `Dup` app f2 x2
99 inline f = dup1 (inline f) `Dup` dup2 (inline f)
100 val f = dup1 (val f) `Dup` dup2 (val f)
101 lazy f = dup1 (lazy f) `Dup` dup2 (lazy f)
102
103 -- * Type 'Expr_Lambda'
104 -- | Expression.
105 data Expr_Lambda (lam:: * -> *) (root:: *)
106 type instance Root_of_Expr (Expr_Lambda lam root) = root
107 type instance Type_of_Expr (Expr_Lambda lam root) = Type_Fun lam
108 type instance Sym_of_Expr (Expr_Lambda lam root) repr = Sym_Lambda lam repr
109 type instance Error_of_Expr ast (Expr_Lambda lam root)
110 = Error_Expr_Lambda ast
111 -- NOTE: require UndecidableInstances.
112
113 instance -- Expr_from AST Expr_Lambda
114 ( Type_from AST (Type_Root_of_Expr root)
115 , Expr_from AST root
116
117 , Type_Root_Lift (Type_Fun lam) (Type_Root_of_Expr root)
118 -- NOTE: require UndecidableInstances.
119 , Error_Type_Lift (Error_Type_Fun AST)
120 (Error_of_Type AST (Type_Root_of_Expr root))
121 -- NOTE: require UndecidableInstances.
122 , Error_Expr_Lift (Error_Expr_Lambda AST)
123 (Error_of_Expr AST root)
124 -- NOTE: require UndecidableInstances.
125 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
126 ( Type_Root_of_Expr root)
127 AST)
128 (Error_of_Expr AST root)
129 -- NOTE: require UndecidableInstances.
130 , Error_Expr_Unlift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
131 ( Type_Root_of_Expr root)
132 AST)
133 (Error_of_Expr AST root)
134 -- NOTE: require UndecidableInstances.
135
136 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
137 -- NOTE: require UndecidableInstances.
138
139 , Root_of_Expr root ~ root
140
141 , Implicit_HBool (Is_Last_Expr (Expr_Lambda lam root) root)
142 -- NOTE: require UndecidableInstances.
143 ) => Expr_from AST (Expr_Lambda lam root) where
144 expr_from px_ex ctx ast k =
145 case ast of
146 AST "var" asts ->
147 case asts of
148 [AST name []] -> go ctx k
149 where
150 go
151 :: forall hs ret
152 . Context (Var (Type_Root_of_Expr root)) hs
153 -> ( forall h
154 . Type_Root_of_Expr root h
155 -> Forall_Repr_with_Context (Expr_Lambda lam root) hs h
156 -> Either (Error_of_Expr AST root) ret )
157 -> Either (Error_of_Expr AST root) ret
158 go c k' =
159 case c of
160 Context_Empty -> Left $ error_expr_lift $
161 Error_Expr_Lambda_Var_unbound name ast
162 Var n ty `Context_Alt_Next` _ | n == name ->
163 k' ty $ Forall_Repr_with_Context $
164 \(repr `Context_Alt_Next` _) -> repr
165 _ `Context_Alt_Next` ctx' ->
166 go ctx' $ \ty (Forall_Repr_with_Context repr) ->
167 k' ty $ Forall_Repr_with_Context $
168 \(_ `Context_Alt_Next` c') -> repr c'
169 _ -> Left $ error_expr px_ex $
170 Error_Expr_Fun_Wrong_number_of_arguments 1 ast
171 AST "app" asts ->
172 case asts of
173 [ast_lam, ast_arg_actual] ->
174 expr_from (Proxy::Proxy root) ctx ast_lam $
175 \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) ->
176 expr_from (Proxy::Proxy root) ctx ast_arg_actual $
177 \(ty_arg_actual::Type_Root_of_Expr root h_arg_actual)
178 (Forall_Repr_with_Context arg_actual) ->
179 case type_unlift $ unType_Root ty_lam of
180 Just (ty_arg_expected `Type_Fun` ty_res
181 :: Type_Fun lam (Type_Root_of_Expr root) h_lam) ->
182 case ty_arg_expected `type_eq` ty_arg_actual of
183 Just Refl ->
184 k ty_res $ Forall_Repr_with_Context $
185 \c -> lam c `app` arg_actual c
186 Nothing -> Left $ error_expr px_ex $
187 Error_Expr_Fun_Argument_mismatch
188 (Exists_Type ty_arg_expected)
189 (Exists_Type ty_arg_actual) ast
190 Nothing -> Left $ error_expr_lift $
191 Error_Expr_Lambda_Not_a_lambda ast
192 _ -> Left $ error_expr px_ex $
193 Error_Expr_Type (error_type_lift $
194 Error_Type_Fun_Wrong_number_of_arguments 2 ast) ast
195 AST "inline" asts -> lambda_from asts inline
196 AST "val" asts -> lambda_from asts val
197 AST "lazy" asts -> lambda_from asts lazy
198 AST "let_inline" asts -> let_from asts let_inline
199 AST "let_val" asts -> let_from asts let_val
200 AST "let_lazy" asts -> let_from asts let_lazy
201 _ -> Left $
202 case hbool :: HBool (Is_Last_Expr (Expr_Lambda lam root) root) of
203 HTrue -> error_expr px_ex $ Error_Expr_Unsupported ast
204 HFalse -> error_expr px_ex $ Error_Expr_Unsupported_here ast
205 where
206 lambda_from asts
207 (lam::forall repr arg res. Sym_Lambda lam repr
208 => (repr arg -> repr res) -> repr (Lambda lam arg res)) =
209 case asts of
210 [AST name [], ast_ty_arg, ast_body] ->
211 case type_from
212 (Proxy::Proxy (Type_Root_of_Expr root))
213 ast_ty_arg
214 (Right . Exists_Type) of
215 Left err -> Left $ error_expr px_ex $ Error_Expr_Type err ast
216 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
217 expr_from (Proxy::Proxy root)
218 (Var name ty_arg `Context_Alt_Next` ctx) ast_body $
219 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
220 k (ty_arg `type_fun` ty_res
221 :: Root_of_Type (Type_Root_of_Expr root)
222 (Lambda lam h_arg h_res)) $
223 Forall_Repr_with_Context $
224 \c -> lam $
225 \arg -> res (arg `Context_Alt_Next` c)
226 _ -> Left $ error_expr px_ex $
227 Error_Expr_Fun_Wrong_number_of_arguments 3 ast
228 let_from asts
229 (let_::forall repr var res. Sym_Lambda lam repr
230 => repr var -> (repr var -> repr res) -> repr res) =
231 case asts of
232 [AST name [], ast_var, ast_body] ->
233 expr_from (Proxy::Proxy root) ctx ast_var $
234 \(ty_var::Type_Root_of_Expr root h_var) (Forall_Repr_with_Context var) ->
235 expr_from (Proxy::Proxy root)
236 (Var name ty_var `Context_Alt_Next` ctx) ast_body $
237 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
238 k ty_res $ Forall_Repr_with_Context $
239 \c -> let_ (var c) $
240 \arg -> res (arg `Context_Alt_Next` c)
241 _ -> Left $ error_expr px_ex $
242 Error_Expr_Fun_Wrong_number_of_arguments 3 ast
243
244 -- * Type 'Error_Expr_Lambda'
245 data Error_Expr_Lambda ast
246 = Error_Expr_Lambda_Not_a_lambda ast
247 | Error_Expr_Lambda_Var_unbound Var_Name ast
248 deriving (Eq, Show)