1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE NoMonomorphismRestriction #-}
5 {-# LANGUAGE Rank2Types #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeOperators #-}
8 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
9 module Compiling.Bool.Test where
12 import Test.Tasty.HUnit
14 import qualified Control.Arrow as Arrow
15 import qualified Control.Monad as Monad
16 -- import Control.Monad.IO.Class (MonadIO(..))
17 import Data.Proxy (Proxy(..))
18 import Data.Text (Text)
19 import Data.Type.Equality ((:~:)(Refl))
20 import Prelude hiding ((&&), not, (||))
22 import Language.Symantic.Typing
23 import Language.Symantic.Compiling
24 import Language.Symantic.Interpreting
26 -- * Class 'Sym_Bool_Vars'
28 -- | A few boolean variables.
29 class Sym_Bool_Vars repr where
33 instance Sym_Bool_Vars TextI where
34 x = TextI $ \_p _v -> "x"
35 y = TextI $ \_p _v -> "y"
36 z = TextI $ \_p _v -> "z"
38 instance -- Trans_Boo_Const
41 ) => Sym_Bool_Vars (Trans_Bool_Const repr) where
48 te1 = bool True && bool False
49 te2 = (bool True && bool False) || (bool True && bool True)
50 te3 = (bool True || bool False) && (bool True || bool True)
51 te4 = bool True && not (bool False)
52 te5 = bool True && not x
54 te7 = (x `xor` y) `xor` z
55 te8 = x `xor` (y `xor` bool True)
62 type Consts = Consts_of_Ifaces Ifaces
64 (==>) (ast::Syntax Text) expected =
66 case root_term_from ast of
67 Left err -> Left err @?= Prelude.snd `Arrow.left` expected
68 Right (ETerm ty (Term te)::ETerm Ifaces) ->
70 Left (_, err) -> Right ("…"::Text) @?= Left err
71 Right (ty_expected::Type Consts h, _::h, _::Text) ->
72 (Monad.>>= (@?= (\(_::Type Consts h, err) -> err) `Arrow.left` expected)) $
73 case ty `eq_type` ty_expected of
74 Nothing -> Monad.return $ Left $
75 Error_Term_Type_mismatch
76 (At Nothing $ EType ty)
77 (At Nothing $ EType ty_expected)
79 let h = host_from_term te
85 -- , (text_from_expr :: Repr_Text h -> Text) r
89 tests = testGroup "Bool" $
90 [ syTrue ==> Right (tyBool, True, "True")
91 , Syntax "xor" [syTrue, syTrue] ==> Right
92 (tyBool, False, "True `xor` True")
94 (syLam (Syntax "x" []) syBool
97 (tyBool, True, "(\\x0 -> x0) True")
99 (syLam (Syntax "x" []) syBool
105 (tyBool, False, "(\\x0 -> x0 `xor` True) True" )
106 , syLet (Syntax "x" []) syTrue
111 (tyBool, False, "let x0 = True in x0 `xor` True")
112 , testGroup "Error_Term" $
113 [ syApp syTrue syTrue ==> Left (tyBool,
114 Error_Term_Type_is_not_an_application $ At (Just syTrue) $
118 (Syntax "x" []) syBool
124 syBool ==> Left (tyBool,
125 Error_Term_unknown $ At (Just syBool) $
127 , let sy = Syntax " "
128 [ syLam (Syntax "x" []) syBool
130 ] in sy ==> Left (tyBool,
131 Error_Term_Syntax $ Error_Syntax_more_arguments_needed $
133 , let sy = Syntax " "
134 [ syLam (Syntax "x" []) syBool
138 ] in sy ==> Left (tyBool,
139 Error_Term_Syntax $ Error_Syntax_too_many_arguments $
142 syLam (Syntax "x" []) (syBool .> syBool)
143 (Syntax " " [Syntax "x" [], syTrue]) in
144 syApp sy syTrue ==> Left (tyBool,
145 Error_Term_Type_mismatch
146 (At (Just sy) $ EType $ (tyBool ~> tyBool))
147 (At (Just $ syTrue) $ EType tyBool)