1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE Rank2Types #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeFamilies #-}
8 -- | Expression for /lambda abstraction/s
9 -- in /Higher-Order Abstract Syntax/ (HOAS).
10 module Language.LOL.Symantic.Expr.Lambda where
12 import Data.Proxy (Proxy(..))
13 import Data.Type.Equality ((:~:)(Refl))
14 import Data.Text (Text)
16 import Language.LOL.Symantic.Type
17 import Language.LOL.Symantic.Expr.Common
18 import Language.LOL.Symantic.Repr.Dup
19 import Language.LOL.Symantic.Trans.Common
21 -- * Class 'Sym_Lambda'
25 -- NOTE: argument @arg@ and result @res@ of 'Lambda'
26 -- wrapped inside 'lam': to control the calling
27 -- in the 'Repr_Host' instance.
29 -- NOTE: the default definitions supplied for:
30 -- 'app', 'inline', 'val' and 'lazy'
31 -- are there to avoid boilerplate code
32 -- when writting 'Trans' instances which
33 -- do not need to alterate those methods.
34 class (lam ~ Lambda_from_Repr repr) => Sym_Lambda lam repr where
35 -- | This type constructor is used like
36 -- the functional dependency: @repr -> lam@
37 -- (ie. knowing @repr@ one can determine @lam@)
38 -- in order to avoid to introduce a 'Proxy' @lam@
39 -- in 'let_inline', 'let_val' and 'let_lazy'.
41 -- Distinguishing between @lam@ and @repr@ is used to maintain
42 -- the universal polymorphism of @repr@ in 'Expr_from',
43 -- the downside with this however is that
44 -- to be an instance of 'Sym_Lambda' for all @lam@,
45 -- the @repr@ type of an interpreter
46 -- has to be parameterized by @lam@,
47 -- even though it does not actually need @lam@ to do its work.
49 -- Basically this means having sometimes to add a type annotation
50 -- to the interpreter call to specify @lam@.
51 type Lambda_from_Repr repr :: {-lam-}(* -> *)
53 -- | Lambda application.
54 app :: repr (Lambda lam arg res) -> repr arg -> repr res
56 -- | /Call-by-name/ lambda.
57 inline :: (repr arg -> repr res) -> repr (Lambda lam arg res)
58 -- | /Call-by-value/ lambda.
59 val :: (repr arg -> repr res) -> repr (Lambda lam arg res)
60 -- | /Call-by-need/ lambda (aka. /lazyness/): lazy shares its argument, no matter what.
61 lazy :: (repr arg -> repr res) -> repr (Lambda lam arg res)
63 default app :: Trans t repr => t repr (Lambda lam arg res) -> t repr arg -> t repr res
64 default inline :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
65 default val :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
66 default lazy :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res)
67 app f x = trans_lift $ trans_apply f `app` trans_apply x
68 inline f = trans_lift $ inline $ trans_apply . f . trans_lift
69 val f = trans_lift $ val $ trans_apply . f . trans_lift
70 lazy f = trans_lift $ lazy $ trans_apply . f . trans_lift
72 -- | Convenient 'inline' wrapper.
74 :: Sym_Lambda lam repr
75 => repr var -> (repr var -> repr res) -> repr res
76 let_inline x y = inline y `app` x
77 -- | Convenient 'val' wrapper.
79 :: Sym_Lambda lam repr
80 => repr var -> (repr var -> repr res) -> repr res
81 let_val x y = val y `app` x
82 -- | Convenient 'lazy' wrapper.
84 :: Sym_Lambda lam repr
85 => repr var -> (repr var -> repr res) -> repr res
86 let_lazy x y = lazy y `app` x
90 instance -- Sym_Lambda Dup
93 ) => Sym_Lambda lam (Dup r1 r2) where
94 type Lambda_from_Repr (Dup r1 r2) = Lambda_from_Repr r1
95 app (f1 `Dup` f2) (x1 `Dup` x2) = app f1 x1 `Dup` app f2 x2
96 inline f = dup1 (inline f) `Dup` dup2 (inline f)
97 val f = dup1 (val f) `Dup` dup2 (val f)
98 lazy f = dup1 (lazy f) `Dup` dup2 (lazy f)
100 -- * Type 'Expr_Lambda'
102 data Expr_Lambda (lam:: * -> *) (root:: *)
103 type instance Root_of_Expr (Expr_Lambda lam root) = root
104 type instance Type_of_Expr (Expr_Lambda lam root) = Type_Fun lam
105 type instance Sym_of_Expr (Expr_Lambda lam root) repr = Sym_Lambda lam repr
106 type instance Error_of_Expr ast (Expr_Lambda lam root) = Error_Expr_Lambda ast
109 :: forall ast lam root hs ret.
110 ( Type_from ast (Type_Root_of_Expr root)
111 , Error_Expr_Lift (Error_Expr_Lambda ast)
112 (Error_of_Expr ast root)
113 , Root_of_Expr root ~ root
114 ) => Text -> Expr_From ast (Expr_Lambda lam root) hs ret
115 var_from name _ex ast = go
117 go :: forall ex hs'. (ex ~ (Expr_Lambda lam root))
118 => Context (Lambda_Var (Type_Root_of_Expr ex)) hs'
119 -> ( forall h. Type_Root_of_Expr ex h
120 -> Forall_Repr_with_Context ex hs' h
121 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
122 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
125 Context_Empty -> Left $ error_expr_lift $
126 Error_Expr_Lambda_Var_unbound name ast
127 Lambda_Var n ty `Context_Next` _ | n == name ->
128 k' ty $ Forall_Repr_with_Context $
129 \(repr `Context_Next` _) -> repr
130 _ `Context_Next` ctx' ->
131 go ctx' $ \ty (Forall_Repr_with_Context repr) ->
132 k' ty $ Forall_Repr_with_Context $
133 \(_ `Context_Next` c') -> repr c'
136 :: forall ty ast lam root hs ret.
137 ( ty ~ Type_Root_of_Expr root
140 , Type_Root_Lift (Type_Fun lam) ty
141 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
142 (Error_of_Expr ast root)
143 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
144 , Root_of_Expr root ~ root
146 -> Expr_From ast (Expr_Lambda lam root) hs ret
147 app_from ast_lam ast_arg_actual ex ast ctx k =
148 expr_from (Proxy::Proxy root) ast_lam ctx $
149 \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) ->
150 expr_from (Proxy::Proxy root) ast_arg_actual ctx $
151 \(ty_arg_actual::Type_Root_of_Expr root h_arg_actual)
152 (Forall_Repr_with_Context arg_actual) ->
153 case type_unlift $ unType_Root ty_lam of
156 Error_Expr_Type_mismatch ast
157 (Exists_Type (type_var SZero `type_fun` type_var (SSucc SZero)
158 :: Type_Root_of_Expr (Expr_Lambda lam root) (lam Zero -> lam (Succ Zero))))
160 Just (ty_arg_expected `Type_Fun` ty_res
161 :: Type_Fun lam (Type_Root_of_Expr root) h_lam) ->
163 ty_arg_expected ty_arg_actual $ \Refl ->
164 k ty_res $ Forall_Repr_with_Context $
165 \c -> lam c `app` arg_actual c
168 :: forall ty ast lam root hs ret.
169 ( ty ~ Type_Root_of_Expr root
172 , Type_Root_Lift (Type_Fun lam) ty
173 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
174 (Error_of_Expr ast root)
175 , Root_of_Expr root ~ root
176 ) => (forall repr arg res
177 . Sym_Lambda lam repr
178 => (repr arg -> repr res)
179 -> repr (Lambda lam arg res))
180 -> Text -> ast -> ast
181 -> Expr_From ast (Expr_Lambda lam root) hs ret
182 lam_from lam name ast_ty_arg ast_body ex ast ctx k =
184 (Proxy::Proxy (Type_Root_of_Expr root))
185 ast_ty_arg (Right . Exists_Type) of
186 Left err -> Left $ error_expr ex $ Error_Expr_Type err ast
187 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
188 expr_from (Proxy::Proxy root) ast_body
189 (Lambda_Var name ty_arg `Context_Next` ctx) $
190 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
191 k (ty_arg `type_fun` ty_res
192 :: Root_of_Type (Type_Root_of_Expr root)
193 (Lambda lam h_arg h_res)) $
194 Forall_Repr_with_Context $
196 \arg -> res (arg `Context_Next` c)
199 :: forall ty ast lam root hs ret.
200 ( ty ~ Type_Root_of_Expr root
203 , Type_Root_Lift (Type_Fun lam) ty
204 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
205 (Error_of_Expr ast root)
206 , Root_of_Expr root ~ root
207 ) => (forall repr var res. Sym_Lambda lam repr
208 => repr var -> (repr var -> repr res) -> repr res)
209 -> Text -> ast -> ast
210 -> Expr_From ast (Expr_Lambda lam root) hs ret
211 let_from let_ name ast_var ast_body _ex _ast ctx k =
212 expr_from (Proxy::Proxy root) ast_var ctx $
213 \(ty_var::Type_Root_of_Expr root h_var) (Forall_Repr_with_Context var) ->
214 expr_from (Proxy::Proxy root) ast_body
215 (Lambda_Var name ty_var `Context_Next` ctx) $
216 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
217 k ty_res $ Forall_Repr_with_Context $
219 \arg -> res (arg `Context_Next` c)
221 -- * Type 'Error_Expr_Lambda'
222 data Error_Expr_Lambda ast
223 = Error_Expr_Lambda_Var_unbound Lambda_Var_Name ast