{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} module Expr.Bool.Test where import Test.Tasty import Test.Tasty.HUnit import qualified Control.Arrow as Arrow import qualified Control.Monad as Monad -- import Control.Monad.IO.Class (MonadIO(..)) import Data.Proxy (Proxy(..)) import Data.Text (Text) import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding ((&&), not, (||)) import Language.Symantic.Repr import Language.Symantic.Expr import Language.Symantic.Type import Language.Symantic.Trans import AST.Test -- * Class 'Sym_Bool_Vars' -- | A few boolean variables. class Sym_Bool_Vars repr where x :: repr Bool y :: repr Bool z :: repr Bool instance Sym_Bool_Vars Repr_Text where x = Repr_Text $ \_p _v -> "x" y = Repr_Text $ \_p _v -> "y" z = Repr_Text $ \_p _v -> "z" instance -- Trans_Boo_Const ( Sym_Bool repr , Sym_Bool_Vars repr ) => Sym_Bool_Vars (Trans_Bool_Const repr) where x = trans_lift x y = trans_lift y z = trans_lift z -- * Expressions e1 = bool True && bool False e2 = (bool True && bool False) || (bool True && bool True) e3 = (bool True || bool False) && (bool True || bool True) e4 = bool True && not (bool False) e5 = bool True && not x e6 = x `xor` y e7 = (x `xor` y) `xor` z e8 = x `xor` (y `xor` bool True) -- * Tests type Ex = Expr_Root ( Expr_Lambda .|. Expr_Bool ) ex_from = root_expr_from (Proxy::Proxy Ex) (==>) ast expected = testCase (show ast) $ case ex_from ast of Left err -> Left err @?= Prelude.snd `Arrow.left` expected Right (Exists_Type0_and_Repr ty (Forall_Repr r)) -> case expected of Left (_, err) -> Right ("…"::String) @?= Left err Right (ty_expected::Type_Root_of_Expr Ex h, _::h, _::Text) -> (Monad.>>= (@?= (\(_::Proxy h, err) -> err) `Arrow.left` expected)) $ case ty `type0_eq` ty_expected of Nothing -> Monad.return $ Left $ error_expr (Proxy::Proxy Ex) $ Error_Expr_Type_mismatch ast (Exists_Type0 ty) (Exists_Type0 ty_expected) Just Refl -> do let h = host_from_expr r Monad.return $ Right ( ty , h , text_from_expr r -- , (text_from_expr :: Repr_Text h -> Text) r ) tests :: TestTree tests = testGroup "Bool" $ [ AST "bool" [AST "True" []] ==> Right ( type_bool , True , "True" ) , AST "int" [AST "1" []] ==> Left (Proxy::Proxy Int, Error_Expr_Alt_Curr $ Error_Expr_Unsupported $ AST "int" [AST "1" []]) , AST "xor" [ AST "bool" [AST "True" []] , AST "bool" [AST "True" []] ] ==> Right ( type_bool , False , "xor True True" ) , AST "$" [ AST "\\" [ AST "x" [] , AST "Bool" [] , AST "var" [AST "x" []] ] , AST "bool" [AST "True" []] ] ==> Right ( type_bool , True , "(\\x0 -> x0) True" ) , let ast = AST "$" [ AST "bool" [AST "True" []] , AST "bool" [AST "True" []] ] in ast ==> Left (Proxy::Proxy Bool, Error_Expr_Alt_Curr $ Error_Expr_Type_mismatch ast (Exists_Type0 (type_var0 SZero `type_fun` type_var0 (SSucc SZero) ::Type_Root (Type_Var0 :|: Type_Var1 :|: Type_Fun :|: Type_Bool) ((->) Var0 Var0))) (Exists_Type0 type_bool)) , AST "$" [ AST "\\" [ AST "x" [] , AST "Bool" [] , AST "xor" [ AST "var" [AST "x" []] , AST "bool" [AST "True" []] ] ] , AST "bool" [AST "True" []] ] ==> Right ( type_bool , False , "(\\x0 -> xor x0 True) True" ) , AST "let" [ AST "x" [] , AST "bool" [AST "True" []] , AST "xor" [ AST "var" [AST "x" []] , AST "bool" [AST "True" []] ] ] ==> Right ( type_bool , False , "let x0 = True in xor x0 True" ) ]