1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
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
15 import Data.Proxy (Proxy(..))
16 import Data.Type.Equality ((:~:)(Refl))
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
24 -- * Class 'Sym_Lambda'
28 -- NOTE: argument @arg@ and result @res@ of 'Lambda'
29 -- wrapped inside 'lam': to control the calling
30 -- in the 'Repr_Host' instance.
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'.
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.
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-}(* -> *)
56 -- | Lambda application.
57 app :: repr (Lambda lam arg res) -> repr arg -> repr res
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)
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
75 -- | Convenient 'inline' wrapper.
77 :: Sym_Lambda lam repr
78 => repr arg -> (repr arg -> repr res) -> repr res
79 let_inline x y = inline y `app` x
80 -- | Convenient 'val' wrapper.
82 :: Sym_Lambda lam repr
83 => repr arg -> (repr arg -> repr res) -> repr res
84 let_val x y = val y `app` x
85 -- | Convenient 'lazy' wrapper.
87 :: Sym_Lambda lam repr
88 => repr arg -> (repr arg -> repr res) -> repr res
89 let_lazy x y = lazy y `app` x
93 instance -- Sym_Lambda Dup
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)
103 -- * Type 'Expr_Lambda'
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 (Error_of_Type ast (Type_Root_of_Expr root))
111 (Type_Root_of_Expr root)
113 -- NOTE: require UndecidableInstances.
115 instance -- Expr_from AST Expr_Lambda
116 ( Type_from AST (Type_Root_of_Expr root)
119 , Type_Root_Lift (Type_Fun lam) (Type_Root_of_Expr root)
120 , Error_Type_Lift (Error_Type_Fun AST)
121 (Error_of_Type AST (Type_Root_of_Expr root))
122 , Error_Expr_Lift (Error_Expr_Lambda (Error_of_Type AST (Type_Root_of_Expr root))
123 ( Type_Root_of_Expr root)
125 (Error_of_Expr AST root)
126 , Error_Expr_Lift (Error_Expr AST) (Error_of_Expr AST root)
127 , Error_Expr_Unlift (Error_Expr AST) (Error_of_Expr AST root)
129 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
131 , Root_of_Expr root ~ root
133 , Implicit_HBool (Is_Last_Expr (Expr_Lambda lam root) root)
134 ) => Expr_from AST (Expr_Lambda lam root) where
135 expr_from _px_ex ctx ast k =
139 [AST name []] -> go ctx k
143 . Context (Var (Type_Root_of_Expr root)) hs
145 . Type_Root_of_Expr root h
146 -> Forall_Repr_with_Context (Expr_Lambda lam root) hs h
147 -> Either (Error_of_Expr AST root) ret )
148 -> Either (Error_of_Expr AST root) ret
151 Context_Empty -> Left $ error_lambda_lift $
152 Error_Expr_Lambda_Var_unbound name ast
153 Var n ty `Context_Next` _ | n == name ->
154 k' ty $ Forall_Repr_with_Context $
155 \(repr `Context_Next` _) -> repr
156 _ `Context_Next` ctx' ->
157 go ctx' $ \ty (Forall_Repr_with_Context repr) ->
158 k' ty $ Forall_Repr_with_Context $
159 \(_ `Context_Next` c') -> repr c'
160 _ -> Left $ error_lambda_lift $
161 Error_Expr_Fun_Wrong_number_of_arguments 1 ast
164 [ast_lam, ast_arg_actual] ->
165 -- handle_error_expr_unsuported_from_root $
166 expr_from (Proxy::Proxy root) ctx ast_lam $
167 \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) ->
168 expr_from (Proxy::Proxy root) ctx ast_arg_actual $
169 \(ty_arg_actual::Type_Root_of_Expr root h_arg_actual)
170 (Forall_Repr_with_Context arg_actual) ->
171 case type_unlift $ unType_Root ty_lam of
172 Just (ty_arg_expected `Type_Fun` ty_res
173 :: Type_Fun lam (Type_Root_of_Expr root) h_lam) ->
174 case ty_arg_expected `type_eq` ty_arg_actual of
176 k ty_res $ Forall_Repr_with_Context $
177 \c -> lam c `app` arg_actual c
178 Nothing -> Left $ error_lambda_lift $
179 Error_Expr_Fun_Argument_mismatch
180 (Exists_Type ty_arg_expected)
181 (Exists_Type ty_arg_actual) ast
182 Nothing -> Left $ error_lambda_lift $
183 Error_Expr_Lambda_Not_a_lambda ast
184 _ -> Left $ error_lambda_lift $
185 Error_Expr_Type (error_type_lift $
186 Error_Type_Fun_Wrong_number_of_arguments 2 ast) ast
187 AST "inline" asts -> lambda_from asts inline
188 AST "val" asts -> lambda_from asts val
189 AST "lazy" asts -> lambda_from asts lazy
190 -- AST "let_inline" asts -> let_from asts let_inline
192 case hbool :: HBool (Is_Last_Expr (Expr_Lambda lam root) root) of
193 HTrue -> error_expr_lift $ Error_Expr_Unsupported_from_root ast
194 HFalse -> error_expr_lift $ Error_Expr_Unsupported ast
197 handle_error_expr_unsuported_from_root =
199 case error_expr_unlift err of
200 Just (Error_Expr_Unsupported (a::AST)) ->
201 error_expr_lift $ Error_Expr_Unsupported_from_root a
205 (lam::forall repr arg res. Sym_Lambda lam repr
206 => (repr arg -> repr res) -> repr (Lambda lam arg res)) =
208 [AST name [], ast_ty_arg, ast_body] ->
210 (Proxy::Proxy (Type_Root_of_Expr root))
212 (Right . Exists_Type) of
213 Left err -> Left $ error_lambda_lift $ Error_Expr_Type err ast
214 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
215 -- handle_error_expr_unsuported_from_root $
216 expr_from (Proxy::Proxy root)
217 (Var name ty_arg `Context_Next` ctx) ast_body $
218 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
219 k (ty_arg `type_fun` ty_res
220 :: Root_of_Type (Type_Root_of_Expr root)
221 (Lambda lam h_arg h_res)) $
222 Forall_Repr_with_Context $
223 \c -> lam $ \arg -> res (arg `Context_Next` c)
224 _ -> Left $ error_lambda_lift $
225 Error_Expr_Fun_Wrong_number_of_arguments 3 ast
227 (let_::forall repr arg res. Sym_Lambda lam repr
228 => (repr arg -> repr res) -> repr (Lambda lam arg res)) =
230 [AST name [], ast_ty_arg, ast_body] ->
231 case type_from (Proxy::Proxy (Type_Root_of_Expr root)) ast_ty_arg
232 (Right . Exists_Type) of
233 Left Nothing -> Left Nothing
234 Left (Just err) -> Left $ Just $ error_lambda_lift $ Error_Expr_Type err ast
235 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
236 expr_from (Proxy::Proxy root)
237 (Var name ty_arg `Context_Next` ctx) ast_body $
238 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
239 k (ty_arg `type_fun` ty_res
240 :: Root_of_Type (Type_Root_of_Expr root)
241 (Lambda lam h_arg h_res)) $
242 Forall_Repr_with_Context $
243 \c -> let_ $ \arg -> res (arg `Context_Next` c)
244 _ -> Left $ Just $ error_lambda_lift $
245 Error_Expr_Fun_Wrong_number_of_arguments 3 ast
248 :: Error_Expr_Lambda (Error_of_Type AST (Type_Root_of_Expr root)) (Type_Root_of_Expr root) AST
249 -> Error_of_Expr AST root
250 error_lambda_lift = error_expr_lift
252 -- * Type 'Error_Expr_Lambda'
253 data Error_Expr_Lambda err_ty ty ast
254 = Error_Expr_Lambda_Not_a_lambda ast
255 | Error_Expr_Lambda_Var_unbound Var_Name ast
256 | Error_Expr_Fun_Wrong_number_of_arguments Int ast
257 | Error_Expr_Fun_Argument_mismatch (Exists_Type ty) (Exists_Type ty) ast
258 | Error_Expr_Type err_ty ast