]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Lambda.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Lambda.hs
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.Symantic.Expr.Lambda where
11
12 import Data.Proxy (Proxy(..))
13 import Data.Type.Equality ((:~:)(Refl))
14 import Data.Text (Text)
15
16 import Language.Symantic.Type
17 import Language.Symantic.Expr.Common
18 import Language.Symantic.Repr.Dup
19 import Language.Symantic.Trans.Common
20
21 -- * Class 'Sym_Lambda'
22
23 -- | Symantic.
24 --
25 -- NOTE: argument @arg@ and result @res@ of 'Lambda'
26 -- wrapped inside 'lam': to control the calling
27 -- in the 'Repr_Host' instance.
28 --
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'.
40 --
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.
48 --
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-}(* -> *)
52
53 -- | /Lambda application/.
54 app :: repr (Lambda lam arg res) -> repr arg -> repr res
55
56 -- | /Call-by-name/ /lambda abstraction/.
57 inline :: (repr arg -> repr res) -> repr (Lambda lam arg res)
58 -- | /Call-by-value/ /lambda abstraction/.
59 val :: (repr arg -> repr res) -> repr (Lambda lam arg res)
60 -- | /Call-by-need/ /lambda abstraction/ (aka. /lazyness/): lazy shares its argument, no matter what.
61 lazy :: (repr arg -> repr res) -> repr (Lambda lam arg res)
62
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
71
72 -- | Convenient 'inline' wrapper.
73 let_inline
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.
78 let_val
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.
83 let_lazy
84 :: Sym_Lambda lam repr
85 => repr var -> (repr var -> repr res) -> repr res
86 let_lazy x y = lazy y `app` x
87
88 infixl 5 `app`
89
90 instance -- Sym_Lambda Dup
91 ( Sym_Lambda lam r1
92 , Sym_Lambda lam r2
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)
99
100 -- * Type 'Expr_Lambda'
101 -- | Expression.
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
107
108 -- | Parsing utility to check that the given type is a 'Type_Fun'
109 -- or raise 'Error_Expr_Type_mismatch'.
110 check_type_fun
111 :: forall ast ex root lam ty h ret.
112 ( root ~ Root_of_Expr ex
113 , ty ~ Type_Root_of_Expr ex
114 , Type_Lift (Type_Fun lam) (Type_of_Expr root)
115 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
116 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
117 (Error_of_Expr ast root)
118 )
119 => Proxy ex -> ast -> ty h
120 -> (Type_Fun lam ty h -> Either (Error_of_Expr ast root) ret)
121 -> Either (Error_of_Expr ast root) ret
122 check_type_fun ex ast ty k =
123 case type_unlift $ unType_Root ty of
124 Just ty_f -> k ty_f
125 Nothing -> Left $
126 error_expr ex $
127 Error_Expr_Type_mismatch ast
128 (Exists_Type (type_var SZero `type_fun` type_var (SSucc SZero)
129 :: Type_Root_of_Expr ex (lam Zero -> lam (Succ Zero))))
130 (Exists_Type ty)
131
132 -- | Parse a /lambda variable/.
133 var_from
134 :: forall ast lam root hs ret.
135 ( Type_from ast (Type_Root_of_Expr root)
136 , Error_Expr_Lift (Error_Expr_Lambda ast)
137 (Error_of_Expr ast root)
138 , Root_of_Expr root ~ root
139 ) => Text -> Expr_From ast (Expr_Lambda lam root) hs ret
140 var_from name _ex ast = go
141 where
142 go :: forall ex hs'. (ex ~ (Expr_Lambda lam root))
143 => Context (Lambda_Var (Type_Root_of_Expr ex)) hs'
144 -> ( forall h. Type_Root_of_Expr ex h
145 -> Forall_Repr_with_Context ex hs' h
146 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
147 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
148 go c k' =
149 case c of
150 Context_Empty -> Left $ error_expr_lift $
151 Error_Expr_Lambda_Var_unbound name ast
152 Lambda_Var n ty `Context_Next` _ | n == name ->
153 k' ty $ Forall_Repr_with_Context $
154 \(repr `Context_Next` _) -> repr
155 _ `Context_Next` ctx' ->
156 go ctx' $ \ty (Forall_Repr_with_Context repr) ->
157 k' ty $ Forall_Repr_with_Context $
158 \(_ `Context_Next` c') -> repr c'
159
160 -- | Parse 'app'.
161 app_from
162 :: forall ty ast lam root hs ret.
163 ( ty ~ Type_Root_of_Expr root
164 , Type_from ast ty
165 , Expr_from ast root
166 , Type_Root_Lift (Type_Fun lam) ty
167 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
168 (Error_of_Expr ast root)
169 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
170 , Root_of_Expr root ~ root
171 ) => ast -> ast
172 -> Expr_From ast (Expr_Lambda lam root) hs ret
173 app_from ast_lam ast_arg_actual ex ast ctx k =
174 expr_from (Proxy::Proxy root) ast_lam ctx $
175 \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) ->
176 expr_from (Proxy::Proxy root) ast_arg_actual ctx $
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 Nothing -> Left $
181 error_expr ex $
182 Error_Expr_Type_mismatch ast
183 (Exists_Type (type_var SZero `type_fun` type_var (SSucc SZero)
184 :: Type_Root_of_Expr (Expr_Lambda lam root) (lam Zero -> lam (Succ Zero))))
185 (Exists_Type ty_lam)
186 Just (ty_arg_expected `Type_Fun` ty_res
187 :: Type_Fun lam (Type_Root_of_Expr root) h_lam) ->
188 check_eq_type ex ast
189 ty_arg_expected ty_arg_actual $ \Refl ->
190 k ty_res $ Forall_Repr_with_Context $
191 \c -> lam c `app` arg_actual c
192
193 -- | Parse given /lambda abstraction/.
194 lam_from
195 :: forall ty ast lam root hs ret.
196 ( ty ~ Type_Root_of_Expr root
197 , Type_from ast ty
198 , Expr_from ast root
199 , Type_Root_Lift (Type_Fun lam) ty
200 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
201 (Error_of_Expr ast root)
202 , Root_of_Expr root ~ root
203 ) => (forall repr arg res
204 . Sym_Lambda lam repr
205 => (repr arg -> repr res)
206 -> repr (Lambda lam arg res))
207 -> Text -> ast -> ast
208 -> Expr_From ast (Expr_Lambda lam root) hs ret
209 lam_from lam name ast_ty_arg ast_body ex ast ctx k =
210 case type_from
211 (Proxy::Proxy (Type_Root_of_Expr root))
212 ast_ty_arg (Right . Exists_Type) of
213 Left err -> Left $ error_expr ex $ Error_Expr_Type err ast
214 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
215 expr_from (Proxy::Proxy root) ast_body
216 (Lambda_Var name ty_arg `Context_Next` ctx) $
217 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
218 k (ty_arg `type_fun` ty_res
219 :: Root_of_Type (Type_Root_of_Expr root)
220 (Lambda lam h_arg h_res)) $
221 Forall_Repr_with_Context $
222 \c -> lam $
223 \arg -> res (arg `Context_Next` c)
224
225 -- | Parse given /let/.
226 let_from
227 :: forall ty ast lam root hs ret.
228 ( ty ~ Type_Root_of_Expr root
229 , Type_from ast ty
230 , Expr_from ast root
231 , Type_Root_Lift (Type_Fun lam) ty
232 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
233 (Error_of_Expr ast root)
234 , Root_of_Expr root ~ root
235 ) => (forall repr var res. Sym_Lambda lam repr
236 => repr var -> (repr var -> repr res) -> repr res)
237 -> Text -> ast -> ast
238 -> Expr_From ast (Expr_Lambda lam root) hs ret
239 let_from let_ name ast_var ast_body _ex _ast ctx k =
240 expr_from (Proxy::Proxy root) ast_var ctx $
241 \(ty_var::Type_Root_of_Expr root h_var) (Forall_Repr_with_Context var) ->
242 expr_from (Proxy::Proxy root) ast_body
243 (Lambda_Var name ty_var `Context_Next` ctx) $
244 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
245 k ty_res $ Forall_Repr_with_Context $
246 \c -> let_ (var c) $
247 \arg -> res (arg `Context_Next` c)
248
249 -- * Type 'Error_Expr_Lambda'
250 data Error_Expr_Lambda ast
251 = Error_Expr_Lambda_Var_unbound Lambda_Var_Name ast
252 deriving (Eq, Show)