{-# 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.Expr.Trans -- * Class 'Expr_Bool_Vars' -- | A few boolean variables. class Expr_Bool_Vars repr where x :: repr Bool y :: repr Bool z :: repr Bool instance Expr_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 ( Expr_Bool_Vars repr , Expr_Lit repr ) => Expr_Bool_Vars (Trans_Bool_Const repr) where x = trans_lift x y = trans_lift y z = trans_lift z -} -- * 'Expr's 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) (==>) :: forall h raw. ( Eq h, Eq raw, Show h, Show raw , Sym_from raw (Expr_Lambda IO (Expr_Lambda_Bool IO)) , Sym_from raw (Expr_Bool (Expr_Lambda_Bool IO))) => 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 $ Nothing -- Just $ "Type mismatch" Just Refl -> do h <- host_repr r return $ Right ( ty , h , string_repr r -- , (string_repr :: Repr_String IO h -> String) r ) 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" ) ]