Updated bug list & started working on more tests

This commit is contained in:
sebastianselander 2023-03-06 13:04:07 +01:00
parent f5b5f11903
commit 6947614fba
11 changed files with 80 additions and 59 deletions

View file

@ -10,6 +10,13 @@ clean:
test: test:
cabal test cabal test
ctest:
cabal run language sample-programs/basic-1
cabal run language sample-programs/basic-2
cabal run language sample-programs/basic-3
cabal run language sample-programs/basic-4
cabal run language sample-programs/basic-5
# compile a specific file # compile a specific file
run FILE: run FILE:
cabal run language {{FILE}} cabal run language {{FILE}}

View file

@ -1,14 +1 @@
indentation: 4
function-arrows: trailing
comma-style: leading
import-export-style: diff-friendly
indent-wheres: false indent-wheres: false
record-brace-space: true
newlines-between-decls: 1
haddock-style: multi-line
haddock-style-module:
let-style: auto
in-style: right-align
respectful: false
fixities: []
unicode: never

View file

@ -1,2 +1,2 @@
f : _Int -> _Int ;
f = \x. x+1; f = \x. x+1 ;

View file

@ -1,3 +1,5 @@
add : _Int -> _Int -> _Int ;
add x = \y. x+y; add x = \y. x+y;
main = (\z. z+z) ((add 4) 6); main : _Int ;
main = (\z. z+z) ((add 4) 6) ;

View file

@ -1,2 +1,2 @@
main : _Int ;
main = (\x. x+x+3) ((\x. x) 2) main = (\x. x+x+3) ((\x. x) 2) ;

View file

@ -1,2 +1,2 @@
f : _Int -> _Int ;
f x = let g = (\y. y+1) in g (g x) f x = let g = (\y. y+1) in g (g x)

View file

@ -1,9 +1,14 @@
id x = x; -- double : _Int -> _Int ;
-- double n = n + n;
add x y = x + y; apply : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c ;
apply f x = \y. f x y ;
double n = n + n; id : 'a -> 'a ;
id x = x ;
apply f x = \y. f x y; add : _Int -> _Int -> _Int ;
add x y = x + y ;
main = apply (id add) ((\x. x + 1) 1) (double 3); main : _Int -> _Int -> _Int ;
main = (id add) 1 2 ;

View file

@ -1,8 +1,22 @@
## Bugs ## Bugs
### Polymorphic type variables are global? None known at this moment
## Fixed bugs
* 1
```hs
fmap : ('a -> 'b) -> Maybe ('a) -> Maybe ('b) ;
fmap f x =
case x of {
Just x => Just (f x) ;
Nothing => Nothing
}
```
* 2
This doesn't work (occurs check failed, can't unify `(a -> a) = a`
```hs ```hs
data Maybe ('a) where { data Maybe ('a) where {
Nothing : Maybe ('a) Nothing : Maybe ('a)
@ -29,16 +43,3 @@ id x = x ;
main : Maybe ('a -> 'a) ; main : Maybe ('a -> 'a) ;
main = Just id; main = Just id;
``` ```
UPDATE: Might have found a fix. Need to be tested.
### The function f is not carried into the case-expression
Code example that does not type check
```hs
fmap : ('a -> 'b) -> Maybe ('a) -> Maybe ('b) ;
fmap f x =
case x of {
Just x => Just (f x) ;
Nothing => Nothing
}
```

View file

@ -61,7 +61,7 @@ freshenData (Data (Constr name ts) constrs) = do
where where
freshenType :: Ident -> Type -> Type freshenType :: Ident -> Type -> Type
freshenType iden = \case freshenType iden = \case
(TPol a) -> TPol iden (TPol _) -> TPol iden
(TArr a b) -> TArr (freshenType iden a) (freshenType iden b) (TArr a b) -> TArr (freshenType iden a) (freshenType iden b)
(TConstr (Constr a ts)) -> (TConstr (Constr a ts)) ->
TConstr (Constr a (map (freshenType iden) ts)) TConstr (Constr a (map (freshenType iden) ts))

View file

@ -1,5 +1,6 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use <$>" #-} {-# HLINT ignore "Use <$>" #-}
{-# HLINT ignore "Use camelCase" #-} {-# HLINT ignore "Use camelCase" #-}
@ -7,14 +8,19 @@ module Main where
import Data.Either (isLeft, isRight) import Data.Either (isLeft, isRight)
import Data.Map (Map) import Data.Map (Map)
import qualified Data.Map as M import Data.Map qualified as M
import Grammar.Abs import Grammar.Abs
import Test.Hspec import Test.Hspec
import Test.QuickCheck import Test.QuickCheck
import TypeChecker.TypeChecker import TypeChecker.TypeChecker
import qualified TypeChecker.TypeCheckerIr as T import TypeChecker.TypeCheckerIr (
import TypeChecker.TypeCheckerIr (Ctx (..), Env (..), Error, Infer, Ctx (..),
Poly (..)) Env (..),
Error,
Infer,
Poly (..),
)
import TypeChecker.TypeCheckerIr qualified as T
main :: IO () main :: IO ()
main = hspec $ do main = hspec $ do
@ -67,6 +73,7 @@ infer_eabs = describe "algoW used on EAbs" $ do
it "should infer the argument type as polymorphic if it is not used in the lambda" $ do it "should infer the argument type as polymorphic if it is not used in the lambda" $ do
let lambda = EAbs "x" (ELit (LInt 0)) let lambda = EAbs "x" (ELit (LInt 0))
getType lambda `shouldSatisfy` isArrowPolyToMono getType lambda `shouldSatisfy` isArrowPolyToMono
it "should infer a variable as function if used as one" $ do it "should infer a variable as function if used as one" $ do
let lambda = EAbs "f" (EAbs "x" (EApp (EId "f") (EId "x"))) let lambda = EAbs "f" (EAbs "x" (EApp (EId "f") (EId "x")))
let isOk (Right (TArr (TArr (TPol _) (TPol _)) (TArr (TPol _) (TPol _)))) = True let isOk (Right (TArr (TArr (TPol _) (TPol _)) (TArr (TPol _) (TPol _)))) = True
@ -81,6 +88,18 @@ infer_eapp = describe "algoW used on EApp" $ do
let ctx = Ctx (M.singleton (Ident (x :: String)) t) let ctx = Ctx (M.singleton (Ident (x :: String)) t)
getTypeC env ctx (EApp (EId (Ident x)) (EId (Ident x))) `shouldBe` Left "Occurs check failed" getTypeC env ctx (EApp (EId (Ident x)) (EId (Ident x))) `shouldBe` Left "Occurs check failed"
churf_id :: Bind
churf_id = Bind "id" (TArr (TPol "a") (TPol "a")) "id" ["x"] (EId "x")
churf_add :: Bind
churf_add = Bind "add" (TArr (TMono "Int") (TArr (TMono "Int") (TMono "Int"))) "add" ["x", "y"] (EAdd (EId "x") (EId "y"))
churf_main :: Bind
churf_main = Bind "main" (TArr (TMono "Int") (TArr (TMono "Int") (TMono "Int"))) "main" [] (EApp (EId "id") (EId "add"))
test_bug :: IO ()
test_bug = undefined
isArrowPolyToMono :: Either Error Type -> Bool isArrowPolyToMono :: Either Error Type -> Bool
isArrowPolyToMono (Right (TArr (TPol _) (TMono _))) = True isArrowPolyToMono (Right (TArr (TPol _) (TMono _))) = True
isArrowPolyToMono _ = False isArrowPolyToMono _ = False