Incorporated most of main, as well as started on quickcheck

This commit is contained in:
sebastianselander 2023-02-27 11:12:05 +01:00
parent 06e65de235
commit 2f45f39435
19 changed files with 1252 additions and 1090 deletions

View file

@ -1,21 +0,0 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Grammar.Abs
import System.Exit (exitFailure)
import Test.Hspec
import TypeChecker.AlgoW
main :: IO ()
main = do
print "RUNNING TESTS BROTHER"
exitFailure
-- hspec $ do
-- describe "the algorithm W" $ do
-- it "infers EInt as type Int" $ do
-- fmap fst (run (inferExp (EInt 1))) `shouldBe` Right (TMono "Int")
-- it "throws an exception if a variable is inferred with an empty env" $ do
-- run (inferExp (EId "x")) `shouldBe` Left "Unbound variable: x"
-- it "throws an exception if the annotated type does not match the inferred type" $ do
-- fmap fst (run (inferExp (EAnn (EInt 3) (TPol "a")))) `shouldBe` Right (TMono "bad")

56
tests/Tests.hs Normal file
View file

@ -0,0 +1,56 @@
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use <$>" #-}
module Main where
import Control.Monad.Except
import Grammar.Abs
import Test.QuickCheck
import TypeChecker.TypeChecker
import qualified TypeChecker.TypeCheckerIr as T
main :: IO ()
main = do
quickCheck prop_isInt
quickCheck prop_idAbs_generic
newtype AbsExp = AE Exp deriving Show
newtype EIntExp = EI Exp deriving Show
instance Arbitrary EIntExp where
arbitrary = genInt
instance Arbitrary AbsExp where
arbitrary = genLambda
getType :: Infer (Type, T.Exp) -> Either Error Type
getType ie = case run ie of
Left err -> Left err
Right (t,e) -> return t
genInt :: Gen EIntExp
genInt = EI . ELit . LInt <$> arbitrary
genLambda :: Gen AbsExp
genLambda = do
str <- arbitrary @String
let str' = Ident str
return $ AE $ EAbs str' (EId str')
prop_idAbs_generic :: AbsExp -> Bool
prop_idAbs_generic (AE e) = case getType (inferExp e) of
Left _ -> False
Right t -> isGenericArr t
prop_isInt :: EIntExp -> Bool
prop_isInt (EI e) = case getType (inferExp e) of
Left _ -> False
Right t -> t == int
int :: Type
int = TMono "Int"
isGenericArr :: Type -> Bool
isGenericArr (TArr (TPol a) (TPol b)) = a == b
isGenericArr _ = False