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