{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} module Expr.Int.Test where import Test.Tasty import Test.Tasty.HUnit import Control.Arrow (left) -- import Data.Functor.Identity (Identity) -- import Control.Monad.IO.Class (MonadIO(..)) import Data.Proxy (Proxy(..)) import Data.Text (Text) import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding (and, not, or) import Language.LOL.Symantic.Repr import Language.LOL.Symantic.Expr import Language.LOL.Symantic.Type -- import Language.LOL.Symantic.Trans import AST.Test -- * Class 'Sym_Int_Vars' -- | A few boolean variables. class Sym_Int_Vars repr where x :: repr Int y :: repr Int z :: repr Int instance Sym_Int_Vars (Repr_Text fun) where x = Repr_Text $ \_p _v -> "x" y = Repr_Text $ \_p _v -> "y" z = Repr_Text $ \_p _v -> "z" -- * Expressions e1 = int 1 `add` int 0 e2 = (int 1 `add` int 0) `add` neg (int 1 `add` int 1) e3 = (int 1 `add` neg (int 0)) `add` (int 1 `add` neg (int 1)) e4 = int 0 `add` neg (int 1) e5 = int 1 `add` neg x e6 = x `add` y e7 = (x `add` y) `add` z e8 = x `add` (y `add` int 1) -- * Tests (==>) ast expected = testCase (show ast) $ case expr_lambda_int_from (Proxy::Proxy IO) ast of Left err -> Left err @?= snd `left` expected Right (Exists_Type_and_Repr ty (Forall_Repr r)) -> case expected of Left (_, err) -> Right ("…"::String) @?= Left err Right (ty_expected::Type_Root_of_Expr (Expr_Lambda_Int IO) h, _::h, _::Text) -> (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $ case ty `type_eq` ty_expected of Nothing -> return $ Left $ error_expr (Proxy::Proxy (Expr_Lambda_Int IO)) $ Error_Expr_Type_mismatch ast (Exists_Type ty) (Exists_Type ty_expected) Just Refl -> do h <- host_from_expr r return $ Right ( ty , h , text_from_expr r -- , (text_from_expr :: Repr_Text IO h -> String) r ) tests :: TestTree tests = testGroup "Int" $ [ AST "int" [AST "1" []] ==> Right ( type_int , 1 , "1" ) , AST "bool" [AST "True" []] ==> Left (Proxy::Proxy Bool, Error_Expr_Alt_Curr $ Error_Expr_Unsupported $ AST "bool" [AST "True" []]) , AST "add" [ AST "int" [AST "1" []] , AST "int" [AST "1" []] ] ==> Right ( type_int , 2 , "1 + 1" ) , let ast = AST "app" [ AST "int" [AST "1" []] , AST "int" [AST "1" []] ] in ast ==> Left (Proxy::Proxy Int, Error_Expr_Alt_Curr $ Error_Expr_Type_mismatch ast (Exists_Type (type_var SZero `type_fun` type_var (SSucc SZero) ::Type_Fun_Int IO (IO Zero -> IO (Succ Zero)))) (Exists_Type type_int)) , AST "app" [ AST "val" [ AST "x" [] , AST "Int" [] , AST "var" [AST "x" []] ] , AST "int" [AST "1" []] ] ==> Right ( type_int , 1 , "(\\x0 -> x0) 1" ) , AST "app" [ AST "val" [ AST "x" [] , AST "Int" [] , AST "add" [ AST "var" [AST "x" []] , AST "int" [AST "1" []] ] ] , AST "int" [AST "1" []] ] ==> Right ( type_int , 2 , "(\\x0 -> x0 + 1) 1" ) , AST "let_val" [ AST "x" [] , AST "int" [AST "1" []] , AST "add" [ AST "var" [AST "x" []] , AST "int" [AST "1" []] ] ] ==> Right ( type_int , 2 , "let x0 = 1 in x0 + 1" ) ]