Add closures and fix lets in monomorphizer

This commit is contained in:
Martin Fredin 2023-05-06 22:49:08 +02:00
parent 677a200a15
commit 72e599d5de
26 changed files with 1440 additions and 692 deletions

View file

@ -0,0 +1,6 @@
add : Int -> Int -> Int -> Int
add x y z = x + y + z
main = add 8 6 2

View file

@ -0,0 +1,7 @@
apply : (Int -> Int) -> Int -> Int
apply f y = f y
main = apply (\y. y + y) 5

View file

@ -0,0 +1,10 @@
apply : (Int -> Int) -> Int -> Int
apply f z = f z
main =
let x = 10 in
apply (\y. y + x) 6

View file

@ -0,0 +1,15 @@
data List a where
Nil : List a
Cons : a -> List a -> List a
foldr : (a -> b -> b) -> b -> List a -> b
foldr f y xs = case xs of
Nil => y
Cons x xs => f x (foldr f y xs)
main = let z = 2 in foldr (\x.\y. x + y + z) 0 (Cons 1000 (Cons 100 Nil))

View file

@ -0,0 +1,25 @@
data List (a) where
Nil : List (a)
Cons : a -> List (a) -> List (a)
map : (a -> b) -> List (a) -> List (b)
map f xs = case xs of
Nil => Nil
Cons x xs => Cons (f x) (map f xs)
add : Int -> Int -> Int
add x y = x + y
foldr : (a -> b -> b) -> b -> List (a) -> b
foldr f y xs = case xs of
Nil => y
Cons x xs => f x (foldr f y xs)
f : List (Int)
f = ((\x.\ys. map (\y. add y x) ys) 4 (Cons 1 (Cons 2 Nil)))
-- [5, 6]
main : Int
main = foldr add 0 f

View file

@ -0,0 +1,21 @@
data List a where
Nil : List a
Cons : a -> List a -> List a
map : (a -> b) -> List a -> List b
map f xs = case xs of
Nil => Nil
Cons x xs => Cons (f x) (map f xs)
f : List Int
f = (\x.\ys. map (\y. y + x) ys) 4 (Cons 1 (Cons 2 Nil))
-- [5, 6]
sum : List Int -> Int
sum xs = case xs of
Nil => 0
Cons x xs => x + sum xs
main = sum f

View file

@ -0,0 +1,3 @@
main = let x = 10 in 6 + x

View file

@ -0,0 +1,16 @@
data List a where
Nil : List a
Cons : a -> List a -> List a
map : (a -> b) -> List a -> List b
map f xs = case xs of
Nil => Nil
Cons x xs => Cons (f x) (map f xs)
sum : List Int -> Int
sum xs = case xs of
Nil => 0
Cons x xs => x + (sum xs)
main = let y = 10 in sum (map (\x. x + y) (Cons 2 (Cons 4 Nil)))

View file

@ -0,0 +1,7 @@
f = 10
main = f + 6