]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Bool/Test.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Bool / 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.Bool.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, (||))
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_Bool_Vars'
29
30 -- | A few boolean variables.
31 class Sym_Bool_Vars repr where
32 x :: repr Bool
33 y :: repr Bool
34 z :: repr Bool
35 instance Sym_Bool_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 instance -- Trans_Boo_Const
40 ( Sym_Bool repr
41 , Sym_Bool_Vars repr
42 ) => Sym_Bool_Vars (Trans_Bool_Const repr) where
43 x = trans_lift x
44 y = trans_lift y
45 z = trans_lift z
46
47 -- * Expressions
48 e1 = bool True && bool False
49 e2 = (bool True && bool False) || (bool True && bool True)
50 e3 = (bool True || bool False) && (bool True || bool True)
51 e4 = bool True && not (bool False)
52 e5 = bool True && not x
53 e6 = x `xor` y
54 e7 = (x `xor` y) `xor` z
55 e8 = x `xor` (y `xor` bool True)
56
57 -- * Tests
58 type Ex lam = Expr_Root
59 ( Expr_Lambda_App lam
60 .|. Expr_Lambda_Val lam
61 .|. Expr_Bool
62 )
63 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
64
65 (==>) ast expected =
66 testCase (show ast) $
67 case ex_from ast of
68 Left err -> Left err @?= snd `left` expected
69 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
70 case expected of
71 Left (_, err) -> Right ("…"::String) @?= Left err
72 Right (ty_expected::Type_Root_of_Expr (Ex IO) h, _::h, _::Text) ->
73 (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $
74 case ty `eq_type` ty_expected of
75 Nothing -> return $ Left $
76 error_expr (Proxy::Proxy (Ex IO)) $
77 Error_Expr_Type_mismatch ast
78 (Exists_Type ty)
79 (Exists_Type ty_expected)
80 Just Refl -> do
81 h <- host_from_expr r
82 return $
83 Right
84 ( ty
85 , h
86 , text_from_expr r
87 -- , (text_from_expr :: Repr_Text IO h -> String) r
88 )
89
90 tests :: TestTree
91 tests = testGroup "Bool" $
92 [ AST "bool" [AST "True" []] ==> Right
93 ( type_bool
94 , True
95 , "True" )
96 , AST "int" [AST "1" []] ==> Left (Proxy::Proxy Int,
97 Error_Expr_Alt_Curr $
98 Error_Expr_Unsupported $ AST "int" [AST "1" []])
99 , AST "xor"
100 [ AST "bool" [AST "True" []]
101 , AST "bool" [AST "True" []]
102 ] ==> Right
103 ( type_bool
104 , False
105 , "xor True True" )
106 , AST "app"
107 [ AST "val"
108 [ AST "x" []
109 , AST "Bool" []
110 , AST "var" [AST "x" []]
111 ]
112 , AST "bool" [AST "True" []]
113 ] ==> Right
114 ( type_bool
115 , True
116 , "(\\x0 -> x0) True" )
117 , let ast = AST "app"
118 [ AST "bool" [AST "True" []]
119 , AST "bool" [AST "True" []]
120 ] in ast ==> Left (Proxy::Proxy Bool,
121 Error_Expr_Alt_Curr $
122 Error_Expr_Type_mismatch ast
123 (Exists_Type (type_var0 SZero `type_fun` type_var0 (SSucc SZero)
124 ::Type_Root (Type_Var0 :|: Type_Var1 :|: Type_Fun IO :|: Type_Bool)
125 (Lambda IO Var0 Var0)))
126 (Exists_Type type_bool))
127 , AST "app"
128 [ AST "val"
129 [ AST "x" []
130 , AST "Bool" []
131 , AST "xor"
132 [ AST "var" [AST "x" []]
133 , AST "bool" [AST "True" []]
134 ]
135 ]
136 , AST "bool" [AST "True" []]
137 ] ==> Right
138 ( type_bool
139 , False
140 , "(\\x0 -> xor x0 True) True" )
141 , AST "let_val"
142 [ AST "x" []
143 , AST "bool" [AST "True" []]
144 , AST "xor"
145 [ AST "var" [AST "x" []]
146 , AST "bool" [AST "True" []]
147 ]
148 ] ==> Right
149 ( type_bool
150 , False
151 , "let x0 = True in xor x0 True" )
152 ]