]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/AST/Test.hs
init
[haskell/symantic.git] / Language / Symantic / AST / Test.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 -- | Abstract Syntax Tree.
11 module AST.Test where
12
13 import Test.Tasty
14 -- import Test.Tasty.HUnit
15
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
21 import Language.Symantic.Type
22 import Language.Symantic.Expr as Expr
23
24 tests :: TestTree
25 tests = testGroup "AST" $
26 [
27 ]
28
29 -- * Type 'AST'
30 data AST
31 = AST Text [AST]
32 deriving (Eq)
33 -- | Custom 'Show' instance a little bit more readable
34 -- than the automatically derived one.
35 instance Show AST where
36 showsPrec p ast@(AST f args) =
37 let n = Text.unpack f in
38 case ast of
39 AST _ [] -> showString n
40 AST "->" [a] ->
41 showParen (p >= prec_arrow) $
42 showString ("("++n++") ") .
43 showsPrec prec_arrow a
44 AST "->" [a, b] ->
45 showParen (p >= prec_arrow) $
46 showsPrec prec_arrow a .
47 showString (" "++n++" ") .
48 showsPrec prec_arrow b
49 _ ->
50 showString n .
51 showString "(" .
52 showString (List.intercalate ", " $ show Prelude.<$> args) .
53 showString ")"
54 where prec_arrow = 1
55
56 -- ** Parsing utilities
57 from_ast0
58 :: forall ty ast ex hs ret.
59 ( ty ~ Type_Root_of_Expr ex
60 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
61 (Error_of_Expr ast (Root_of_Expr ex))
62 ) => [ast]
63 -> Expr_From ast ex hs ret
64 -> Expr_From ast ex hs ret
65 from_ast0 asts k' ex ast ctx k =
66 case asts of
67 [] -> k' ex ast ctx k
68 _ -> Left $ error_expr ex $
69 Error_Expr_Wrong_number_of_arguments ast 0
70
71 from_ast1
72 :: forall ty ast ex hs ret.
73 ( ty ~ Type_Root_of_Expr ex
74 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
75 (Error_of_Expr ast (Root_of_Expr ex))
76 ) => [ast] -> (ast -> Expr_From ast ex hs ret)
77 -> Expr_From ast ex hs ret
78 from_ast1 asts k' ex ast ctx k =
79 case asts of
80 [ast_0] -> k' ast_0 ex ast ctx k
81 _ -> Left $ error_expr ex $
82 Error_Expr_Wrong_number_of_arguments ast 1
83
84 from_ast2
85 :: forall ty ast ex hs ret.
86 ( ty ~ Type_Root_of_Expr ex
87 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
88 (Error_of_Expr ast (Root_of_Expr ex))
89 ) => [ast] -> (ast -> ast -> Expr_From ast ex hs ret)
90 -> Expr_From ast ex hs ret
91 from_ast2 asts k' ex ast ctx k =
92 case asts of
93 [ast_0, ast_1] -> k' ast_0 ast_1 ex ast ctx k
94 _ -> Left $ error_expr ex $
95 Error_Expr_Wrong_number_of_arguments ast 2
96
97 from_ast3
98 :: forall ty ast ex hs ret.
99 ( ty ~ Type_Root_of_Expr ex
100 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
101 (Error_of_Expr ast (Root_of_Expr ex))
102 ) => [ast] -> (ast -> ast -> ast -> Expr_From ast ex hs ret)
103 -> Expr_From ast ex hs ret
104 from_ast3 asts k' ex ast ctx k =
105 case asts of
106 [ast_0, ast_1, ast_2] -> k' ast_0 ast_1 ast_2 ex ast ctx k
107 _ -> Left $ error_expr ex $
108 Error_Expr_Wrong_number_of_arguments ast 3
109
110 lit_from_AST
111 :: forall root ty lit ex ast hs ret.
112 ( ty ~ Type_Root_of_Expr ex
113 , root ~ Root_of_Expr ex
114 , ast ~ AST
115 , Read lit
116 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
117 (Error_of_Expr ast root)
118 ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit)
119 -> ty lit -> [ast]
120 -> Expr_From ast ex hs ret
121 lit_from_AST op ty_lit asts ex ast ctx k =
122 case asts of
123 [AST lit []] -> lit_from op ty_lit lit ex ast ctx k
124 _ -> Left $ error_expr ex $
125 Error_Expr_Wrong_number_of_arguments ast 1
126
127 op1_from_AST
128 :: forall root ty lit ex ast hs ret.
129 ( ty ~ Type_Root_of_Expr ex
130 , root ~ Root_of_Expr ex
131 , ast ~ AST
132 , Eq_Type (Type_Root_of_Expr root)
133 , Expr_from ast root
134 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
135 (Error_of_Expr ast root)
136 , Root_of_Expr root ~ root
137 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit)
138 -> ty lit -> [ast]
139 -> Expr_From ast ex hs ret
140 op1_from_AST op ty_lit asts ex ast ctx k =
141 case asts of
142 [ast_x] -> op1_from op ty_lit ast_x ex ast ctx k
143 _ -> Left $ error_expr ex $
144 Error_Expr_Wrong_number_of_arguments ast 1
145
146 op2_from_AST
147 :: forall root ty lit ex ast hs ret.
148 ( ty ~ Type_Root_of_Expr ex
149 , root ~ Root_of_Expr ex
150 , ast ~ AST
151 , Eq_Type (Type_Root_of_Expr root)
152 , Expr_from ast root
153 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
154 (Error_of_Expr ast root)
155 , Root_of_Expr root ~ root
156 ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit)
157 -> ty lit -> [ast]
158 -> Expr_From ast ex hs ret
159 op2_from_AST op ty_lit asts ex ast ctx k =
160 case asts of
161 [ast_x, ast_y] -> op2_from op ty_lit ast_x ast_y ex ast ctx k
162 _ -> Left $ error_expr ex $
163 Error_Expr_Wrong_number_of_arguments ast 2
164
165 instance -- Type_from AST Type_Var0
166 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
167 , Implicit_HBool (Is_Last_Type (Type_Var0 root) root)
168 ) => Type_from AST (Type_Var0 root) where
169 type_from ty ast _k =
170 Left $ error_type_unsupported ty ast
171 -- NOTE: no support so far.
172 instance -- Type_from AST Type_Var1
173 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
174 , Implicit_HBool (Is_Last_Type (Type_Var1 root) root)
175 ) => Type_from AST (Type_Var1 root) where
176 type_from ty ast _k =
177 Left $ error_type_unsupported ty ast
178 -- NOTE: no support so far.
179 instance -- Type_from AST Type_Unit
180 ( Lift_Type_Root Type_Unit root
181 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
182 , Implicit_HBool (Is_Last_Type (Type_Unit root) root)
183 ) => Type_from AST (Type_Unit root) where
184 type_from ty ast k =
185 case ast of
186 AST "()" asts ->
187 case asts of
188 [] -> k type_unit
189 _ -> Left $ lift_error_type $
190 Error_Type_Wrong_number_of_arguments ast 0
191 _ -> Left $ error_type_unsupported ty ast
192 instance -- Type_from AST Type_Bool
193 ( Lift_Type_Root Type_Bool root
194 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
195 , Implicit_HBool (Is_Last_Type (Type_Bool root) root)
196 ) => Type_from AST (Type_Bool root) where
197 type_from ty ast k =
198 case ast of
199 AST "Bool" asts ->
200 case asts of
201 [] -> k type_bool
202 _ -> Left $ lift_error_type $
203 Error_Type_Wrong_number_of_arguments ast 0
204 _ -> Left $ error_type_unsupported ty ast
205 instance -- Type_from AST Type_Int
206 ( Lift_Type_Root Type_Int root
207 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
208 , Implicit_HBool (Is_Last_Type (Type_Int root) root)
209 ) => Type_from AST (Type_Int root) where
210 type_from ty ast k =
211 case ast of
212 AST "Int" asts ->
213 case asts of
214 [] -> k type_int
215 _ -> Left $ lift_error_type $
216 Error_Type_Wrong_number_of_arguments ast 0
217 _ -> Left $ error_type_unsupported ty ast
218 instance -- Type_from AST Type_Ordering
219 ( Lift_Type_Root Type_Ordering root
220 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
221 , Implicit_HBool (Is_Last_Type (Type_Ordering root) root)
222 ) => Type_from AST (Type_Ordering root) where
223 type_from ty ast k =
224 case ast of
225 AST "Ordering" asts ->
226 case asts of
227 [] -> k type_ordering
228 _ -> Left $ lift_error_type $
229 Error_Type_Wrong_number_of_arguments ast 0
230 _ -> Left $ error_type_unsupported ty ast
231 instance -- Type_from AST Type_Fun
232 ( Eq_Type root
233 , Type_from AST root
234 , Lift_Type_Root (Type_Fun lam) root
235 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
236 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
237 , Root_of_Type root ~ root
238 , Implicit_HBool (Is_Last_Type (Type_Fun lam root) root)
239 ) => Type_from AST (Type_Fun lam root) where
240 type_from ty ast k =
241 case ast of
242 AST "->" asts ->
243 case asts of
244 [ast_arg, ast_res] -> type_fun_from ty ast_arg ast_res k
245 _ -> Left $ lift_error_type $
246 Error_Type_Wrong_number_of_arguments ast 2
247 _ -> Left $ error_type_unsupported ty ast
248 instance -- Type_from AST Type_Maybe
249 ( Eq_Type root
250 , Type_from AST root
251 , Lift_Type_Root Type_Maybe root
252 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
253 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
254 , Root_of_Type root ~ root
255 , Implicit_HBool (Is_Last_Type (Type_Maybe root) root)
256 ) => Type_from AST (Type_Maybe root) where
257 type_from ty ast k =
258 case ast of
259 AST "Maybe" asts ->
260 case asts of
261 [ast_a] ->
262 type_from (Proxy::Proxy root) ast_a $ \ty_a ->
263 k (type_maybe ty_a)
264 _ -> Left $ lift_error_type $
265 Error_Type_Wrong_number_of_arguments ast 1
266 _ -> Left $ error_type_unsupported ty ast
267 instance -- Type_from AST Type_List
268 ( Eq_Type root
269 , Type_from AST root
270 , Lift_Type_Root Type_List root
271 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
272 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
273 , Root_of_Type root ~ root
274 , Implicit_HBool (Is_Last_Type (Type_List root) root)
275 ) => Type_from AST (Type_List root) where
276 type_from ty ast k =
277 case ast of
278 AST "[]" asts ->
279 case asts of
280 [ast_a] ->
281 type_from (Proxy::Proxy root) ast_a $ \ty_a ->
282 k (type_list ty_a)
283 _ -> Left $ lift_error_type $
284 Error_Type_Wrong_number_of_arguments ast 1
285 _ -> Left $ error_type_unsupported ty ast
286
287 instance -- Type1_from AST Type_Bool
288 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
289 , Implicit_HBool (Is_Last_Type (Type_Bool root) root)
290 ) => Type1_from AST (Type_Bool root)
291 instance -- Type1_from AST Type_Int
292 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
293 , Implicit_HBool (Is_Last_Type (Type_Int root) root)
294 ) => Type1_from AST (Type_Int root)
295 instance -- Type1_from AST Type_Unit
296 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
297 , Implicit_HBool (Is_Last_Type (Type_Unit root) root)
298 ) => Type1_from AST (Type_Unit root)
299 instance -- Type1_from AST Type_Ordering
300 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
301 , Implicit_HBool (Is_Last_Type (Type_Ordering root) root)
302 ) => Type1_from AST (Type_Ordering root)
303 instance -- Type1_from AST Type_Var0
304 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
305 , Implicit_HBool (Is_Last_Type (Type_Var0 root) root)
306 ) => Type1_from AST (Type_Var0 root)
307 instance -- Type1_from AST Type_Var1
308 ( Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
309 , Implicit_HBool (Is_Last_Type (Type_Var1 root) root)
310 ) => Type1_from AST (Type_Var1 root)
311 instance -- Type1_from AST Type_Maybe
312 ( Type_from AST root
313 , Lift_Type_Root Type_Maybe root
314 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
315 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
316 , Root_of_Type root ~ root
317 , Implicit_HBool (Is_Last_Type (Type_Maybe root) root)
318 ) => Type1_from AST (Type_Maybe root) where
319 type1_from ty ast k =
320 case ast of
321 AST "Maybe" asts ->
322 case asts of
323 [] -> k (Proxy::Proxy Maybe) type_maybe
324 _ -> Left $ lift_error_type $
325 Error_Type_Wrong_number_of_arguments ast 0
326 _ -> Left $ error_type_unsupported ty ast
327 instance -- Type1_from AST Type_List
328 ( Eq_Type root
329 , Type_from AST root
330 , Lift_Type_Root Type_List root
331 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
332 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
333 , Root_of_Type root ~ root
334 , Implicit_HBool (Is_Last_Type (Type_List root) root)
335 ) => Type1_from AST (Type_List root) where
336 type1_from ty ast k =
337 case ast of
338 AST "[]" asts ->
339 case asts of
340 [] -> k (Proxy::Proxy []) type_list
341 _ -> Left $ lift_error_type $
342 Error_Type_Wrong_number_of_arguments ast 0
343 _ -> Left $ error_type_unsupported ty ast
344 instance -- Type1_from AST Type_Fun
345 ( Eq_Type root
346 , Type_from AST root
347 , Lift_Type_Root (Type_Fun lam) root
348 , Lift_Error_Type (Error_Type AST) (Error_of_Type AST root)
349 , Unlift_Error_Type (Error_Type AST) (Error_of_Type AST root)
350 , Root_of_Type root ~ root
351 , Implicit_HBool (Is_Last_Type (Type_Fun lam root) root)
352 ) => Type1_from AST (Type_Fun lam root) where
353 type1_from ty ast k =
354 case ast of
355 AST "->" asts ->
356 case asts of
357 [ast_arg] ->
358 type_from (Proxy::Proxy root) ast_arg $ \(ty_arg::root h_arg) ->
359 k (Proxy::Proxy (Lambda lam h_arg)) $
360 type_fun ty_arg
361 _ -> Left $ lift_error_type $
362 Error_Type_Wrong_number_of_arguments ast 1
363 _ -> Left $ error_type_unsupported ty ast
364
365 instance -- Expr_from AST Expr_Bool
366 ( Eq_Type (Type_Root_of_Expr root)
367 , Type_from AST (Type_Root_of_Expr root)
368 , Expr_from AST root
369 , Lift_Type_Root Type_Bool (Type_Root_of_Expr root)
370 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
371 , Unlift_Type Type_Bool (Type_of_Expr root)
372 , Root_of_Expr root ~ root
373 , Implicit_HBool (Is_Last_Expr (Expr_Bool root) root)
374 ) => Expr_from AST (Expr_Bool root) where
375 expr_from ex ast =
376 case ast of
377 AST "bool" asts -> lit_from_AST bool type_bool asts ex ast
378 AST "not" asts -> op1_from_AST Expr.not type_bool asts ex ast
379 AST "&&" asts -> op2_from_AST (Expr.&&) type_bool asts ex ast
380 AST "||" asts -> op2_from_AST (Expr.||) type_bool asts ex ast
381 AST "xor" asts -> op2_from_AST Expr.xor type_bool asts ex ast
382 _ -> \_ctx _k -> Left $ error_expr_unsupported ex ast
383 instance -- Expr_from AST Expr_If
384 ( Eq_Type (Type_Root_of_Expr root)
385 , Type_from AST (Type_Root_of_Expr root)
386 , Expr_from AST root
387 , Lift_Type_Root Type_Bool (Type_Root_of_Expr root)
388 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
389 , Root_of_Expr root ~ root
390 , Implicit_HBool (Is_Last_Expr (Expr_If root) root)
391 ) => Expr_from AST (Expr_If root) where
392 expr_from ex ast ctx k =
393 case ast of
394 AST "if" asts -> from_ast3 asts if_from ex ast ctx k
395 _ -> Left $ error_expr_unsupported ex ast
396 instance -- Expr_from AST Expr_When
397 ( Eq_Type (Type_Root_of_Expr root)
398 , Type_from AST (Type_Root_of_Expr root)
399 , Expr_from AST root
400 , Lift_Type_Root Type_Bool (Type_Root_of_Expr root)
401 , Lift_Type_Root Type_Unit (Type_Root_of_Expr root)
402 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
403 , Root_of_Expr root ~ root
404 , Implicit_HBool (Is_Last_Expr (Expr_When root) root)
405 ) => Expr_from AST (Expr_When root) where
406 expr_from ex ast ctx k =
407 case ast of
408 AST "when" asts -> from_ast2 asts when_from ex ast ctx k
409 _ -> Left $ error_expr_unsupported ex ast
410 instance -- Expr_from AST Expr_Int
411 ( Eq_Type (Type_Root_of_Expr root)
412 , Type_from AST (Type_Root_of_Expr root)
413 , Expr_from AST root
414 , Lift_Type_Root Type_Int (Type_Root_of_Expr root)
415 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
416 , Unlift_Type Type_Int (Type_of_Expr root)
417 , Root_of_Expr root ~ root
418 , Implicit_HBool (Is_Last_Expr (Expr_Int root) root)
419 ) => Expr_from AST (Expr_Int root) where
420 expr_from ex ast =
421 case ast of
422 AST "int" asts -> lit_from_AST int type_int asts ex ast
423 AST "abs" asts -> op1_from_AST Expr.abs type_int asts ex ast
424 AST "negate" asts -> op1_from_AST Expr.negate type_int asts ex ast
425 AST "+" asts -> op2_from_AST (Expr.+) type_int asts ex ast
426 AST "-" asts -> op2_from_AST (Expr.-) type_int asts ex ast
427 AST "*" asts -> op2_from_AST (Expr.*) type_int asts ex ast
428 AST "mod" asts -> op2_from_AST Expr.mod type_int asts ex ast
429 _ -> \_ctx _k -> Left $ error_expr_unsupported ex ast
430 instance -- Expr_from AST Expr_Lambda
431 ( Eq_Type (Type_Root_of_Expr root)
432 , Type_from AST (Type_Root_of_Expr root)
433 , Expr_from AST root
434 , Lift_Type_Root (Type_Fun lam) (Type_Root_of_Expr root)
435 , Lift_Error_Expr (Error_Expr_Lambda AST) (Error_of_Expr AST root)
436 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
437 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
438 , Root_of_Expr root ~ root
439 , Implicit_HBool (Is_Last_Expr (Expr_Lambda lam root) root)
440 ) => Expr_from AST (Expr_Lambda lam root) where
441 expr_from ex ast ctx k =
442 case ast of
443 AST "var" asts ->
444 case asts of
445 [AST name []] -> var_from name ex ast ctx k
446 _ -> Left $ error_expr ex $
447 Error_Expr_Wrong_number_of_arguments ast 1
448 AST "app" asts -> from_ast2 asts app_from ex ast ctx k
449 AST "inline" asts -> go_lam asts inline
450 AST "val" asts -> go_lam asts val
451 AST "lazy" asts -> go_lam asts lazy
452 AST "let_inline" asts -> go_let asts let_inline
453 AST "let_val" asts -> go_let asts let_val
454 AST "let_lazy" asts -> go_let asts let_lazy
455 _ -> Left $ error_expr_unsupported ex ast
456 where
457 go_lam asts
458 (lam::forall repr arg res. Sym_Lambda lam repr
459 => (repr arg -> repr res) -> repr (Lambda lam arg res)) =
460 case asts of
461 [AST name [], ast_ty_arg, ast_body] ->
462 lam_from lam name ast_ty_arg ast_body ex ast ctx k
463 _ -> Left $ error_expr ex $
464 Error_Expr_Wrong_number_of_arguments ast 3
465 go_let asts
466 (let_::forall repr var res. Sym_Lambda lam repr
467 => repr var -> (repr var -> repr res) -> repr res) =
468 case asts of
469 [AST name [], ast_var, ast_body] ->
470 let_from let_ name ast_var ast_body ex ast ctx k
471 _ -> Left $ error_expr ex $
472 Error_Expr_Wrong_number_of_arguments ast 3
473 instance -- Expr_from AST Expr_Maybe
474 ( Eq_Type (Type_Root_of_Expr root)
475 , Type_from AST (Type_Root_of_Expr root)
476 , Expr_from AST root
477 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
478 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
479 , Lift_Type Type_Maybe (Type_of_Expr root)
480 , Unlift_Type Type_Maybe (Type_of_Expr root)
481 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
482 , Root_of_Expr root ~ root
483 , Implicit_HBool (Is_Last_Expr (Expr_Maybe lam root) root)
484 ) => Expr_from AST (Expr_Maybe lam root) where
485 expr_from ex ast ctx k =
486 case ast of
487 AST "maybe" asts -> from_ast3 asts maybe_from ex ast ctx k
488 AST "nothing" asts -> from_ast1 asts nothing_from ex ast ctx k
489 AST "just" asts -> from_ast1 asts just_from ex ast ctx k
490 _ -> Left $ error_expr_unsupported ex ast
491 instance -- Expr_from AST Expr_Eq
492 ( Eq_Type (Type_Root_of_Expr root)
493 , Type_from AST (Type_Root_of_Expr root)
494 , Lift_Type Type_Bool (Type_of_Expr root)
495 , Constraint_Type Eq (Type_Root_of_Expr root)
496 , Expr_from AST root
497 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
498 , Root_of_Expr root ~ root
499 , Implicit_HBool (Is_Last_Expr (Expr_Eq root) root)
500 ) => Expr_from AST (Expr_Eq root) where
501 expr_from ex ast ctx k =
502 case ast of
503 AST "==" asts -> from_ast2 asts eq_from ex ast ctx k
504 _ -> Left $ error_expr_unsupported ex ast
505 instance -- Expr_from AST Expr_Ord
506 ( Eq_Type (Type_Root_of_Expr root)
507 , Type_from AST (Type_Root_of_Expr root)
508 , Lift_Type Type_Ordering (Type_of_Expr root)
509 , Constraint_Type Ord (Type_Root_of_Expr root)
510 , Expr_from AST root
511 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
512 , Root_of_Expr root ~ root
513 , Implicit_HBool (Is_Last_Expr (Expr_Ord root) root)
514 ) => Expr_from AST (Expr_Ord root) where
515 expr_from ex ast ctx k =
516 case ast of
517 AST "compare" asts -> from_ast2 asts compare_from ex ast ctx k
518 _ -> Left $ error_expr_unsupported ex ast
519 instance -- Expr_from AST Expr_List
520 ( Eq_Type (Type_Root_of_Expr root)
521 , Type_from AST (Type_Root_of_Expr root)
522 , Expr_from AST root
523 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
524 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
525 , Lift_Type Type_List (Type_of_Expr root)
526 , Unlift_Type Type_List (Type_of_Expr root)
527 , Lift_Type Type_Bool (Type_of_Expr root)
528 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
529 , Root_of_Expr root ~ root
530 , Implicit_HBool (Is_Last_Expr (Expr_List lam root) root)
531 ) => Expr_from AST (Expr_List lam root) where
532 expr_from ex ast ctx k =
533 case ast of
534 AST "[]" asts -> from_ast1 asts list_empty_from ex ast ctx k
535 AST ":" asts -> from_ast2 asts list_cons_from ex ast ctx k
536 AST "list_filter" asts -> from_ast2 asts list_filter_from ex ast ctx k
537 AST "list" asts ->
538 case asts of
539 ast_ty_a:asts' -> list_from ast_ty_a asts' ex ast ctx k
540 _ -> Left $ error_expr ex $
541 Error_Expr_Wrong_number_of_arguments ast 1
542 _ -> Left $ error_expr_unsupported ex ast
543 instance -- Expr_from AST Expr_Map
544 ( Eq_Type (Type_Root_of_Expr root)
545 , Type_from AST (Type_Root_of_Expr root)
546 , Expr_from AST root
547 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
548 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
549 , Lift_Type Type_Map (Type_of_Expr root)
550 , Unlift_Type Type_Map (Type_of_Expr root)
551 , Lift_Type Type_List (Type_of_Expr root)
552 , Unlift_Type Type_List (Type_of_Expr root)
553 , Lift_Type Type_Tuple2 (Type_of_Expr root)
554 , Unlift_Type Type_Tuple2 (Type_of_Expr root)
555 , Constraint_Type Ord (Type_Root_of_Expr root)
556 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
557 , Root_of_Expr root ~ root
558 , Implicit_HBool (Is_Last_Expr (Expr_Map lam root) root)
559 ) => Expr_from AST (Expr_Map lam root) where
560 expr_from ex ast ctx k =
561 case ast of
562 AST "map_from_list" asts -> from_ast1 asts map_from_list_from ex ast ctx k
563 AST "map_map" asts -> from_ast2 asts map_map_from ex ast ctx k
564 _ -> Left $ error_expr_unsupported ex ast
565 instance -- Expr_from AST Expr_Functor
566 ( Eq_Type (Type_Root_of_Expr root)
567 , Type_from AST (Type_Root_of_Expr root)
568 , Expr_from AST root
569 , String_from_Type (Type_Root_of_Expr root)
570 -- , Lift_Type Type_Var1 (Type_of_Expr root)
571 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
572 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
573 , Unlift_Type1 (Type_of_Expr root)
574 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
575 , Constraint_Type1 Functor_with_Lambda (Type_Root_of_Expr root)
576 , Root_of_Expr root ~ root
577 , Implicit_HBool (Is_Last_Expr (Expr_Functor lam root) root)
578 ) => Expr_from AST (Expr_Functor lam root) where
579 expr_from ex ast ctx k =
580 case ast of
581 AST "fmap" asts -> from_ast2 asts fmap_from ex ast ctx k
582 AST "<$>" asts -> from_ast2 asts fmap_from ex ast ctx k
583 _ -> Left $ error_expr_unsupported ex ast
584 instance -- Expr_from AST Expr_Applicative
585 ( Eq_Type (Type_Root_of_Expr root)
586 , Type1_from AST (Type_Root_of_Expr root)
587 , Expr_from AST root
588 , String_from_Type (Type_Root_of_Expr root)
589 -- , Lift_Type Type_Var1 (Type_of_Expr root)
590 , Lift_Type (Type_Fun lam) (Type_of_Expr root)
591 , Unlift_Type (Type_Fun lam) (Type_of_Expr root)
592 , Eq_Type1 (Type_Root_of_Expr root)
593 , Unlift_Type1 (Type_of_Expr root)
594 , Lift_Error_Expr (Error_Expr_of_Root AST root) (Error_of_Expr AST root)
595 , Constraint_Type1 Applicative_with_Lambda (Type_Root_of_Expr root)
596 , Root_of_Expr root ~ root
597 , Implicit_HBool (Is_Last_Expr (Expr_Applicative lam root) root)
598 ) => Expr_from AST (Expr_Applicative lam root) where
599 expr_from ex ast ctx k =
600 case ast of
601 AST "pure" asts -> from_ast2 asts pure_from ex ast ctx k
602 AST "<*>" asts -> from_ast2 asts ltstargt_from ex ast ctx k
603 _ -> Left $ error_expr_unsupported ex ast