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