1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE Rank2Types #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE UndecidableInstances #-}
10 -- | Abstract Syntax Tree.
14 -- import Test.Tasty.HUnit
16 import qualified Data.List as List
17 import Data.Proxy (Proxy(..))
18 import Data.Text (Text)
19 import qualified Data.Text as Text
20 import Prelude hiding (and, not, or)
22 import Language.LOL.Symantic.Type
23 import Language.LOL.Symantic.Expr
26 tests = testGroup "AST" $
34 -- | Custom 'Show' instance a little bit more readable
35 -- than the automatically derived one.
36 instance Show AST where
37 showsPrec p ast@(AST f args) =
38 let n = Text.unpack f in
40 AST _ [] -> showString n
42 showParen (p >= prec_arrow) $
43 showString ("("++n++") ") .
44 showsPrec prec_arrow a
46 showParen (p >= prec_arrow) $
47 showsPrec prec_arrow a .
48 showString (" "++n++" ") .
49 showsPrec prec_arrow b
53 showString (List.intercalate ", " $ show <$> args) .
57 instance -- Type_from AST Type_Var
58 ( Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
59 , Implicit_HBool (Is_Last_Type (Type_Var root) root)
60 ) => Type_from AST (Type_Var root) where
62 Left $ error_type_unsupported ty ast
63 -- NOTE: no support so far.
64 instance -- Type_from AST Type_Unit
65 ( Type_Root_Lift Type_Unit root
66 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
67 , Implicit_HBool (Is_Last_Type (Type_Unit root) root)
68 ) => Type_from AST (Type_Unit root) where
74 _ -> Left $ error_type_lift $
75 Error_Type_Wrong_number_of_arguments ast 0
76 _ -> Left $ error_type_unsupported ty ast
77 instance -- Type_from AST Type_Bool
78 ( Type_Root_Lift Type_Bool root
79 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
80 , Implicit_HBool (Is_Last_Type (Type_Bool root) root)
81 ) => Type_from AST (Type_Bool root) where
87 _ -> Left $ error_type_lift $
88 Error_Type_Wrong_number_of_arguments ast 0
89 _ -> Left $ error_type_unsupported ty ast
90 instance -- Type_from AST Type_Int
91 ( Type_Root_Lift Type_Int root
92 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
93 , Implicit_HBool (Is_Last_Type (Type_Int root) root)
94 ) => Type_from AST (Type_Int root) where
100 _ -> Left $ error_type_lift $
101 Error_Type_Wrong_number_of_arguments ast 0
102 _ -> Left $ error_type_unsupported ty ast
103 instance -- Type_from AST Type_Fun
106 , Type_Root_Lift (Type_Fun lam) root
107 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
108 , Error_Type_Unlift (Error_Type AST) (Error_of_Type AST root)
109 , Root_of_Type root ~ root
110 , Implicit_HBool (Is_Last_Type (Type_Fun lam root) root)
111 ) => Type_from AST (Type_Fun lam root) where
116 [ast_arg, ast_res] -> type_fun_from ty ast_arg ast_res k
117 _ -> Left $ error_type_lift $
118 Error_Type_Wrong_number_of_arguments ast 2
119 _ -> Left $ error_type_unsupported ty ast
120 instance -- Type_from AST Type_Maybe
123 , Type_Root_Lift Type_Maybe root
124 , Error_Type_Lift (Error_Type AST) (Error_of_Type AST root)
125 , Error_Type_Unlift (Error_Type AST) (Error_of_Type AST root)
126 , Root_of_Type root ~ root
127 , Implicit_HBool (Is_Last_Type (Type_Maybe root) root)
128 ) => Type_from AST (Type_Maybe root) where
134 type_from (Proxy::Proxy root) ast_a $ \(ty_a::root h_a) ->
135 k (type_root_lift $ Type_Maybe ty_a
137 _ -> Left $ error_type_lift $
138 Error_Type_Wrong_number_of_arguments ast 1
139 _ -> Left $ error_type_unsupported ty ast
140 instance -- Expr_from AST Expr_Bool
141 ( Type_from AST (Type_Root_of_Expr root)
143 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
144 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
145 ( Type_Root_of_Expr root)
147 (Error_of_Expr AST root)
148 , Type_Unlift Type_Bool (Type_of_Expr root)
149 , Root_of_Expr root ~ root
150 , Implicit_HBool (Is_Last_Expr (Expr_Bool root) root)
151 ) => Expr_from AST (Expr_Bool root) where
154 AST "bool" asts -> lit_from_AST bool type_bool asts ex ast
155 AST "not" asts -> op1_from_AST not type_bool asts ex ast
156 AST "and" asts -> op2_from_AST and type_bool asts ex ast
157 AST "or" asts -> op2_from_AST or type_bool asts ex ast
158 AST "xor" asts -> op2_from_AST xor type_bool asts ex ast
159 _ -> \_ctx _k -> Left $ error_expr_unsupported ex ast
160 instance -- Expr_from AST Expr_If
161 ( Type_from AST (Type_Root_of_Expr root)
163 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
164 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
165 ( Type_Root_of_Expr root)
167 (Error_of_Expr AST root)
168 , Root_of_Expr root ~ root
169 , Implicit_HBool (Is_Last_Expr (Expr_If root) root)
170 ) => Expr_from AST (Expr_If root) where
171 expr_from ex ast ctx k =
175 [ast_cond, ast_ok, ast_ko] ->
176 if_from ast_cond ast_ok ast_ko ex ast ctx k
177 _ -> Left $ error_expr ex $
178 Error_Expr_Wrong_number_of_arguments ast 3
179 _ -> Left $ error_expr_unsupported ex ast
180 instance -- Expr_from AST Expr_When
181 ( Type_from AST (Type_Root_of_Expr root)
183 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
184 , Type_Root_Lift Type_Unit (Type_Root_of_Expr root)
185 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
186 ( Type_Root_of_Expr root)
188 (Error_of_Expr AST root)
189 , Root_of_Expr root ~ root
190 , Implicit_HBool (Is_Last_Expr (Expr_When root) root)
191 ) => Expr_from AST (Expr_When root) where
192 expr_from ex ast ctx k =
196 [ast_cond, ast_ok] ->
197 when_from ast_cond ast_ok ex ast ctx k
198 _ -> Left $ error_expr ex $
199 Error_Expr_Wrong_number_of_arguments ast 2
200 _ -> Left $ error_expr_unsupported ex ast
201 instance -- Expr_from AST Expr_Int
202 ( Type_from AST (Type_Root_of_Expr root)
204 , Type_Root_Lift Type_Int (Type_Root_of_Expr root)
205 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
206 ( Type_Root_of_Expr root)
208 (Error_of_Expr AST root)
210 , Type_Unlift Type_Int (Type_of_Expr root)
211 , Root_of_Expr root ~ root
212 , Implicit_HBool (Is_Last_Expr (Expr_Int root) root)
213 ) => Expr_from AST (Expr_Int root) where
216 AST "int" asts -> lit_from_AST int type_int asts ex ast
217 AST "neg" asts -> op1_from_AST neg type_int asts ex ast
218 AST "add" asts -> op2_from_AST add type_int asts ex ast
219 _ -> \_ctx _k -> Left $ error_expr_unsupported ex ast
220 instance -- Expr_from AST Expr_Lambda
221 ( Type_from AST (Type_Root_of_Expr root)
223 , Type_Root_Lift (Type_Fun lam) (Type_Root_of_Expr root)
224 , Error_Expr_Lift (Error_Expr_Lambda AST)
225 (Error_of_Expr AST root)
226 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
227 ( Type_Root_of_Expr root)
229 (Error_of_Expr AST root)
230 , Error_Expr_Unlift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
231 ( Type_Root_of_Expr root)
233 (Error_of_Expr AST root)
234 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
235 , Root_of_Expr root ~ root
236 , Implicit_HBool (Is_Last_Expr (Expr_Lambda lam root) root)
237 ) => Expr_from AST (Expr_Lambda lam root) where
238 expr_from ex ast ctx k =
242 [AST name []] -> var_from name ex ast ctx k
243 _ -> Left $ error_expr ex $
244 Error_Expr_Wrong_number_of_arguments ast 1
247 [ast_lam, ast_arg_actual] ->
248 app_from ast_lam ast_arg_actual ex ast ctx k
249 _ -> Left $ error_expr ex $
250 Error_Expr_Wrong_number_of_arguments ast 2
251 AST "inline" asts -> go_lam asts inline
252 AST "val" asts -> go_lam asts val
253 AST "lazy" asts -> go_lam asts lazy
254 AST "let_inline" asts -> go_let asts let_inline
255 AST "let_val" asts -> go_let asts let_val
256 AST "let_lazy" asts -> go_let asts let_lazy
257 _ -> Left $ error_expr_unsupported ex ast
260 (lam::forall repr arg res. Sym_Lambda lam repr
261 => (repr arg -> repr res) -> repr (Lambda lam arg res)) =
263 [AST name [], ast_ty_arg, ast_body] ->
264 lam_from lam name ast_ty_arg ast_body ex ast ctx k
265 _ -> Left $ error_expr ex $
266 Error_Expr_Wrong_number_of_arguments ast 3
268 (let_::forall repr var res. Sym_Lambda lam repr
269 => repr var -> (repr var -> repr res) -> repr res) =
271 [AST name [], ast_var, ast_body] ->
272 let_from let_ name ast_var ast_body ex ast ctx k
273 _ -> Left $ error_expr ex $
274 Error_Expr_Wrong_number_of_arguments ast 3
275 instance -- Expr_from AST Expr_Maybe
276 ( Type_from AST (Type_Root_of_Expr root)
278 , Type_Root_Lift (Type_Fun lam) (Type_Root_of_Expr root)
279 , Type_Root_Lift Type_Maybe (Type_Root_of_Expr root)
280 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
281 ( Type_Root_of_Expr root)
283 (Error_of_Expr AST root)
284 , Error_Expr_Unlift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
285 ( Type_Root_of_Expr root)
287 (Error_of_Expr AST root)
288 , Type_Unlift (Type_Fun lam) (Type_of_Expr root)
289 , Type_Unlift Type_Maybe (Type_of_Expr root)
290 , Root_of_Expr root ~ root
291 , Implicit_HBool (Is_Last_Expr (Expr_Maybe lam root) root)
292 ) => Expr_from AST (Expr_Maybe lam root) where
293 expr_from ex ast ctx k =
297 [ast_n, ast_j, ast_m] -> maybe_from ast_n ast_j ast_m ex ast ctx k
298 _ -> Left $ error_expr ex $
299 Error_Expr_Wrong_number_of_arguments ast 3
300 AST "nothing" asts ->
302 [ast_ty_a] -> nothing_from ast_ty_a ex ast ctx k
303 _ -> Left $ error_expr ex $
304 Error_Expr_Wrong_number_of_arguments ast 1
307 [ast_a] -> just_from ast_a ex ast ctx k
308 _ -> Left $ error_expr ex $
309 Error_Expr_Wrong_number_of_arguments ast 1
310 _ -> Left $ error_expr_unsupported ex ast
311 instance -- Expr_from AST Expr_Eq
312 ( Type_from AST (Type_Root_of_Expr root)
313 , Type_Root_Lift Type_Bool (Type_Root_of_Expr root)
314 , Type_Constraint Eq (Type_Root_of_Expr root)
316 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
317 ( Type_Root_of_Expr root)
319 (Error_of_Expr AST root)
320 , Error_Expr_Unlift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
321 ( Type_Root_of_Expr root)
323 (Error_of_Expr AST root)
324 , Root_of_Expr root ~ root
325 , Implicit_HBool (Is_Last_Expr (Expr_Eq root) root)
326 ) => Expr_from AST (Expr_Eq root) where
327 expr_from ex ast ctx k =
331 [ast_x, ast_y] -> eq_from ast_x ast_y ex ast ctx k
332 _ -> Left $ error_expr ex $
333 Error_Expr_Wrong_number_of_arguments ast 2
334 _ -> Left $ error_expr_unsupported ex ast
337 :: forall root ty lit ex ast hs ret.
338 ( ty ~ Type_Root_of_Expr ex
339 , root ~ Root_of_Expr ex
342 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
343 (Error_of_Expr ast root)
344 ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit)
346 -> Expr_From ast ex hs ret
347 lit_from_AST op ty_lit asts ex ast ctx k =
349 [AST lit []] -> lit_from op ty_lit lit ex ast ctx k
350 _ -> Left $ error_expr ex $
351 Error_Expr_Wrong_number_of_arguments ast 1
354 :: forall root ty lit ex ast hs ret.
355 ( ty ~ Type_Root_of_Expr ex
356 , root ~ Root_of_Expr ex
358 , Type_Eq (Type_Root_of_Expr root)
360 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
361 (Error_of_Expr ast root)
362 , Root_of_Expr root ~ root
363 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit)
365 -> Expr_From ast ex hs ret
366 op1_from_AST op ty_lit asts ex ast ctx k =
368 [ast_x] -> op1_from op ty_lit ast_x ex ast ctx k
369 _ -> Left $ error_expr ex $
370 Error_Expr_Wrong_number_of_arguments ast 1
373 :: forall root ty lit ex ast hs ret.
374 ( ty ~ Type_Root_of_Expr ex
375 , root ~ Root_of_Expr ex
377 , Type_Eq (Type_Root_of_Expr root)
379 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
380 (Error_of_Expr ast root)
381 , Root_of_Expr root ~ root
382 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit)
384 -> Expr_From ast ex hs ret
385 op2_from_AST op ty_lit asts ex ast ctx k =
387 [ast_x, ast_y] -> op2_from op ty_lit ast_x ast_y ex ast ctx k
388 _ -> Left $ error_expr ex $
389 Error_Expr_Wrong_number_of_arguments ast 2