]> 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 -- | Parse a /lambda variable/.
109 var_from
110 :: forall ast lam root hs ret.
111 ( Type_from ast (Type_Root_of_Expr root)
112 , Error_Expr_Lift (Error_Expr_Lambda ast)
113 (Error_of_Expr ast root)
114 , Root_of_Expr root ~ root
115 ) => Text -> Expr_From ast (Expr_Lambda lam root) hs ret
116 var_from name _ex ast = go
117 where
118 go :: forall ex hs'. (ex ~ (Expr_Lambda lam root))
119 => Context (Lambda_Var (Type_Root_of_Expr ex)) hs'
120 -> ( forall h. Type_Root_of_Expr ex h
121 -> Forall_Repr_with_Context ex hs' h
122 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret )
123 -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret
124 go c k' =
125 case c of
126 Context_Empty -> Left $ error_expr_lift $
127 Error_Expr_Lambda_Var_unbound name ast
128 Lambda_Var n ty `Context_Next` _ | n == name ->
129 k' ty $ Forall_Repr_with_Context $
130 \(repr `Context_Next` _) -> repr
131 _ `Context_Next` ctx' ->
132 go ctx' $ \ty (Forall_Repr_with_Context repr) ->
133 k' ty $ Forall_Repr_with_Context $
134 \(_ `Context_Next` c') -> repr c'
135
136 -- | Parse a /lambda application/.
137 app_from
138 :: forall ty ast lam root hs ret.
139 ( ty ~ Type_Root_of_Expr root
140 , Type_from ast ty
141 , Expr_from ast root
142 , Type_Root_Lift (Type_Fun lam) ty
143 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
144 (Error_of_Expr ast root)
145 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
146 , Root_of_Expr root ~ root
147 ) => ast -> ast
148 -> Expr_From ast (Expr_Lambda lam root) hs ret
149 app_from ast_lam ast_arg_actual ex ast ctx k =
150 expr_from (Proxy::Proxy root) ast_lam ctx $
151 \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) ->
152 expr_from (Proxy::Proxy root) ast_arg_actual ctx $
153 \(ty_arg_actual::Type_Root_of_Expr root h_arg_actual)
154 (Forall_Repr_with_Context arg_actual) ->
155 case type_unlift $ unType_Root ty_lam of
156 Nothing -> Left $
157 error_expr ex $
158 Error_Expr_Type_mismatch ast
159 (Exists_Type (type_var SZero `type_fun` type_var (SSucc SZero)
160 :: Type_Root_of_Expr (Expr_Lambda lam root) (lam Zero -> lam (Succ Zero))))
161 (Exists_Type ty_lam)
162 Just (ty_arg_expected `Type_Fun` ty_res
163 :: Type_Fun lam (Type_Root_of_Expr root) h_lam) ->
164 when_type_eq ex ast
165 ty_arg_expected ty_arg_actual $ \Refl ->
166 k ty_res $ Forall_Repr_with_Context $
167 \c -> lam c `app` arg_actual c
168
169 -- | Parse a /lambda abstraction/.
170 lam_from
171 :: forall ty ast lam root hs ret.
172 ( ty ~ Type_Root_of_Expr root
173 , Type_from ast ty
174 , Expr_from ast root
175 , Type_Root_Lift (Type_Fun lam) ty
176 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
177 (Error_of_Expr ast root)
178 , Root_of_Expr root ~ root
179 ) => (forall repr arg res
180 . Sym_Lambda lam repr
181 => (repr arg -> repr res)
182 -> repr (Lambda lam arg res))
183 -> Text -> ast -> ast
184 -> Expr_From ast (Expr_Lambda lam root) hs ret
185 lam_from lam name ast_ty_arg ast_body ex ast ctx k =
186 case type_from
187 (Proxy::Proxy (Type_Root_of_Expr root))
188 ast_ty_arg (Right . Exists_Type) of
189 Left err -> Left $ error_expr ex $ Error_Expr_Type err ast
190 Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) ->
191 expr_from (Proxy::Proxy root) ast_body
192 (Lambda_Var name ty_arg `Context_Next` ctx) $
193 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
194 k (ty_arg `type_fun` ty_res
195 :: Root_of_Type (Type_Root_of_Expr root)
196 (Lambda lam h_arg h_res)) $
197 Forall_Repr_with_Context $
198 \c -> lam $
199 \arg -> res (arg `Context_Next` c)
200
201 -- | Parse a /let/.
202 let_from
203 :: forall ty ast lam root hs ret.
204 ( ty ~ Type_Root_of_Expr root
205 , Type_from ast ty
206 , Expr_from ast root
207 , Type_Root_Lift (Type_Fun lam) ty
208 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
209 (Error_of_Expr ast root)
210 , Root_of_Expr root ~ root
211 ) => (forall repr var res. Sym_Lambda lam repr
212 => repr var -> (repr var -> repr res) -> repr res)
213 -> Text -> ast -> ast
214 -> Expr_From ast (Expr_Lambda lam root) hs ret
215 let_from let_ name ast_var ast_body _ex _ast ctx k =
216 expr_from (Proxy::Proxy root) ast_var ctx $
217 \(ty_var::Type_Root_of_Expr root h_var) (Forall_Repr_with_Context var) ->
218 expr_from (Proxy::Proxy root) ast_body
219 (Lambda_Var name ty_var `Context_Next` ctx) $
220 \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) ->
221 k ty_res $ Forall_Repr_with_Context $
222 \c -> let_ (var c) $
223 \arg -> res (arg `Context_Next` c)
224
225 -- * Type 'Error_Expr_Lambda'
226 data Error_Expr_Lambda ast
227 = Error_Expr_Lambda_Var_unbound Lambda_Var_Name ast
228 deriving (Eq, Show)