{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} module Expr.Bool.Test where import Test.Tasty import Test.Tasty.HUnit -- import Data.Functor.Identity (Identity) -- import Control.Monad.IO.Class (MonadIO(..)) import Data.Proxy (Proxy(..)) import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding (and, not, or) import Language.LOL.Symantic.Repr import Language.LOL.Symantic.AST import Language.LOL.Symantic.Expr import Language.LOL.Symantic.Type import Language.LOL.Symantic.Trans -- * 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_String fun) where x = Repr_String $ \_p _v -> "x" y = Repr_String $ \_p _v -> "y" z = Repr_String $ \_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 `and` bool False e2 = (bool True `and` bool False) `or` (bool True `and` bool True) e3 = (bool True `or` bool False) `and` (bool True `or` bool True) e4 = bool True `and` not (bool False) e5 = bool True `and` not x e6 = x `xor` y e7 = (x `xor` y) `xor` z e8 = x `xor` (y `xor` bool True) -- * Tests (==>) :: forall h raw root. ( Eq h, Eq raw, Show h, Show raw , root ~ Expr_Lambda_Bool IO , Expr_from raw (Expr_Lambda IO root) , Expr_from raw (Expr_Bool root) ) => raw -> (Type_Fun_Bool IO h, h, String) -> TestTree (==>) raw expected@(ty_expected::Type_Fun_Bool IO h, _::h, _::String) = testCase (show raw) $ (>>= (@?= Right expected)) $ case expr_lambda_bool_from (Proxy::Proxy IO) raw of Left err -> return $ Left err Right (Exists_Type_and_Repr ty (Forall_Repr r)) -> case ty `type_eq` ty_expected of Nothing -> return $ Left $ error_lambda_lift $ Error_Expr_Type (error_type_lift $ Error_Type_Unsupported raw) raw Just Refl -> do h <- host_from_expr r return $ Right ( ty , h , string_from_expr r -- , (string_from_expr :: Repr_String IO h -> String) r ) where error_lambda_lift :: (root ~ Expr_Lambda_Bool IO) => Error_Expr_Lambda (Error_of_Type raw (Type_Root_of_Expr root)) (Type_Root_of_Expr root) raw -> Error_of_Expr raw root error_lambda_lift = error_expr_lift tests :: TestTree tests = testGroup "Bool" $ [ AST "bool" [AST "True" []] ==> ( type_bool , True , "True" ) , AST "xor" [ AST "bool" [AST "True" []] , AST "bool" [AST "True" []] ] ==> ( type_bool , False , "(True | True) & !(True & True)" ) , AST "app" [ AST "val" [ AST "x" [] , AST "Bool" [] , AST "var" [AST "x" []] ] , AST "bool" [AST "True" []] ] ==> ( type_bool , True , "(\\x0 -> x0) True" ) , AST "app" [ AST "val" [ AST "x" [] , AST "Bool" [] , AST "xor" [ AST "var" [AST "x" []] , AST "bool" [AST "True" []] ] ] , AST "bool" [AST "True" []] ] ==> ( type_bool , False , "(\\x0 -> (x0 | True) & !(x0 & True)) True" ) ]