Skip to content

Commit f1c587d

Browse files
committed
recursion etc
1 parent 056b4dc commit f1c587d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Elara/TypeInfer/Type.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ substitution = Substitution . one
9292
class Substitutable (a :: k -> Kind.Type) where
9393
substitute :: UniqueTyVar -> Monotype loc -> a loc -> a loc
9494

95-
substituteAll :: Substitution loc -> a loc -> a loc
96-
substituteAll (Substitution s) a =
97-
foldr (uncurry substitute) a (Map.toList s)
95+
substituteAll :: Eq (a loc) => Substitution loc -> a loc -> a loc
96+
substituteAll (Substitution s) a = fix a
97+
where
98+
fix t =
99+
let t' = foldl' (\acc (k, v) -> substitute k v acc) t (Map.toList s)
100+
in if t == t' then t else fix t'
98101

99102
-- instance Substitutable Type where
100103
-- substitute tv t (Forall tv' c m) = Forall tv' (substitute tv t c) (substitute tv t m)

test/Infer.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ spec = describe "Infers types correctly" $ do
2929
lambdaTests
3030
letInTests
3131
ifElseTests
32+
recursionTests
3233
it "infers literals" prop_literalTypesInvariants
3334
Unify.spec
3435

@@ -48,7 +49,7 @@ literalTests = describe "Literal Type Inference" $ do
4849
$(shouldMatch [p|(Scalar ScalarFloat)|]) ty
4950

5051
lambdaTests :: Spec
51-
lambdaTests = describe "Lambda Type Inference" $ do
52+
lambdaTests = withoutRetries $ describe "Lambda Type Inference" $ do
5253
it "infers lambda type correctly" $ do
5354
let expr = loadShuntedExpr "\\x -> x"
5455
res <- pipelineResShouldSucceed expr
@@ -67,13 +68,13 @@ lambdaTests = describe "Lambda Type Inference" $ do
6768

6869
expr === Scalar ScalarInt
6970

70-
withoutRetries $ it "infers nested identity function correctly" $ property $ do
71+
it "infers nested identity function correctly" $ property $ do
7172
expr <- inferFully "(\\x -> (\\y -> y) x) 42"
7273

7374
expr === Scalar ScalarInt
7475

7576
letInTests :: Spec
76-
letInTests = describe "Let In Type Inference" $ do
77+
letInTests =withoutRetries $ describe "Let In Type Inference" $ do
7778
it "infers let in type correctly" $ property $ do
7879
expr <- inferFully "let x = 42 in x"
7980

@@ -89,18 +90,19 @@ letInTests = describe "Let In Type Inference" $ do
8990

9091
expr === Scalar ScalarInt
9192

93+
recursionTests :: Spec
94+
recursionTests = withoutRetries $ describe "recursion tests" $ do
9295
it "recursion" $ property $ do
9396
expr <- inferFully "let loop x = if x == 0 then x else loop (x - 1) in loop"
9497

95-
expr === Scalar ScalarInt
98+
expr === Function (Scalar ScalarInt) (Scalar ScalarInt)
9699

97100
ifElseTests :: Spec
98101
ifElseTests = describe "If Else Type Inference" $ do
99102
it "infers if else type correctly" $ property $ do
100103
expr <- inferFully "if True then 42 else 43"
101104

102105
expr === Scalar ScalarInt
103-
104106

105107
inferFully exprSrc = do
106108
let expr = loadShuntedExpr exprSrc

0 commit comments

Comments
 (0)