]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Int/Test.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Int / Test.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# LANGUAGE Rank2Types #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeOperators #-}
7 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
8 module Expr.Int.Test where
9
10 import Test.Tasty
11 import Test.Tasty.HUnit
12
13 import Control.Arrow (left)
14 -- import Data.Functor.Identity (Identity)
15 -- import Control.Monad.IO.Class (MonadIO(..))
16 import Data.Proxy (Proxy(..))
17 import Data.Text (Text)
18 import Data.Type.Equality ((:~:)(Refl))
19 import Prelude hiding ((&&), not, (||), (+), negate)
20
21 import Language.Symantic.Repr
22 import Language.Symantic.Expr
23 import Language.Symantic.Type
24 -- import Language.Symantic.Trans
25
26 import AST.Test
27
28 -- * Class 'Sym_Int_Vars'
29
30 -- | A few boolean variables.
31 class Sym_Int_Vars repr where
32 x :: repr Int
33 y :: repr Int
34 z :: repr Int
35 instance Sym_Int_Vars (Repr_Text fun) where
36 x = Repr_Text $ \_p _v -> "x"
37 y = Repr_Text $ \_p _v -> "y"
38 z = Repr_Text $ \_p _v -> "z"
39
40 -- * Expressions
41 e1 = int 1 + int 0
42 e2 = (int 1 + int 0) + negate (int 1 + int 1)
43 e3 = (int 1 + negate (int 0)) + (int 1 + negate (int 1))
44 e4 = int 0 + negate (int 1)
45 e5 = int 1 + negate x
46 e6 = x + y
47 e7 = (x + y) + z
48 e8 = x + (y + int 1)
49
50 -- * Tests
51 type Ex lam = Expr_Root
52 ( Expr_Lambda_App lam
53 .|. Expr_Lambda_Val lam
54 .|. Expr_Int
55 )
56 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
57
58 (==>) ast expected =
59 testCase (show ast) $
60 case ex_from ast of
61 Left err -> Left err @?= snd `left` expected
62 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
63 case expected of
64 Left (_, err) -> Right ("…"::String) @?= Left err
65 Right (ty_expected::Type_Root_of_Expr (Ex IO) h, _::h, _::Text) ->
66 (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $
67 case ty `eq_type` ty_expected of
68 Nothing -> return $ Left $
69 error_expr (Proxy::Proxy (Ex IO)) $
70 Error_Expr_Type_mismatch ast
71 (Exists_Type ty)
72 (Exists_Type ty_expected)
73 Just Refl -> do
74 h <- host_from_expr r
75 return $
76 Right
77 ( ty
78 , h
79 , text_from_expr r
80 -- , (text_from_expr :: Repr_Text IO h -> String) r
81 )
82
83 tests :: TestTree
84 tests = testGroup "Int" $
85 [ AST "int" [AST "1" []] ==> Right
86 ( type_int
87 , 1
88 , "1" )
89 , AST "bool" [AST "True" []] ==> Left (Proxy::Proxy Bool,
90 Error_Expr_Alt_Curr $
91 Error_Expr_Unsupported $ AST "bool" [AST "True" []])
92 , AST "+"
93 [ AST "int" [AST "1" []]
94 , AST "int" [AST "1" []]
95 ] ==> Right
96 ( type_int
97 , 2
98 , "1 + 1" )
99 , let ast = AST "app"
100 [ AST "int" [AST "1" []]
101 , AST "int" [AST "1" []]
102 ] in ast ==> Left (Proxy::Proxy Int,
103 Error_Expr_Alt_Curr $
104 Error_Expr_Type_mismatch ast
105 (Exists_Type (type_var0 SZero `type_fun` type_var0 (SSucc SZero)
106 ::Type_Root (Type_Var0 :|: Type_Var1 :|: Type_Fun IO :|: Type_Int)
107 (Lambda IO Var0 Var0)))
108 (Exists_Type type_int))
109 , AST "app"
110 [ AST "val"
111 [ AST "x" []
112 , AST "Int" []
113 , AST "var" [AST "x" []]
114 ]
115 , AST "int" [AST "1" []]
116 ] ==> Right
117 ( type_int
118 , 1
119 , "(\\x0 -> x0) 1" )
120 , AST "app"
121 [ AST "val"
122 [ AST "x" []
123 , AST "Int" []
124 , AST "+"
125 [ AST "var" [AST "x" []]
126 , AST "int" [AST "1" []]
127 ]
128 ]
129 , AST "int" [AST "1" []]
130 ] ==> Right
131 ( type_int
132 , 2
133 , "(\\x0 -> x0 + 1) 1" )
134 , AST "let_val"
135 [ AST "x" []
136 , AST "int" [AST "1" []]
137 , AST "+"
138 [ AST "var" [AST "x" []]
139 , AST "int" [AST "1" []]
140 ]
141 ] ==> Right
142 ( type_int
143 , 2
144 , "let x0 = 1 in x0 + 1" )
145 ]