1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-04 23:27:15 +03:00

Recursive let tests

This commit is contained in:
Erin van der Veen 2022-04-20 12:15:36 +02:00
parent 686d87643f
commit 6151e20586
6 changed files with 51 additions and 7 deletions

View File

@ -1,9 +1,9 @@
{
run = {
f = fun n =>
if n == 0 || n == 1 then
1
else
f (n - 1) + f (n - 2)
}.f
run =
let rec f = fun n =>
if n == 0 || n == 1 then
1
else
f (n - 1) + f (n - 2)
in f
}

View File

@ -163,3 +163,19 @@ fn nested_records() {
HashMap::from([("a", vec!["c"]), ("b", vec!["a", "c"]), ("c", vec!["b"]),])
));
}
#[test]
fn recursive_let() {
assert!(check_stat_vars(
"{
a = let rec b = b + a + h in b,
b = let rec a = a + b in f (a + 1) z,
c = let rec foo = b in let rec bar = c in if a.r then [b] else {foo = c}
}",
HashMap::from([
("a", vec!["a"]),
("b", vec!["b"]),
("c", vec!["a", "b", "c"])
])
));
}

View File

@ -117,3 +117,8 @@ fn importing() {
fn overriding() {
check_file("overriding.ncl");
}
#[test]
fn recursive_let() {
check_file("recursive_let.ncl");
}

View File

@ -0,0 +1,7 @@
let Assert = fun l x => x || %blame% l in
[
let rec f = fun n => if n == 0 then n else f (n - 1) in f 10 == 0,
let rec fib = fun n => if n == 0 || n == 1 then 1 else fib (n - 1) + fib (n - 2) in fib 5 == 8,
]
|> array.foldl (fun x y => (x | Assert) && y) true

View File

@ -74,6 +74,15 @@ let typecheck = [
fun x => switch {`blo => `bla, `ble => `bli, _ => `bla} x in
f `bli,
# recursive let bindings
let rec f : forall a. a -> Num -> a = fun x n =>
if n == 0 then x else if f "0" n == "1" then f x (n - 1) else f x (f 1 n) in
(f "0" 2 : Str),
let rec f : Num -> Num = fun x => if x == 0 then x else f (x - 1) in
(f 10 : Num),
let rec repeat : forall a. Num -> a -> Array a = fun n x =>
if n <= 0 then [] else repeat (n - 1) x @ [x] in (repeat 3 "foo" : Array Str),
# static records
({bla = 1} : {bla : Num}),
({blo = true, bla = 1} : {bla : Num, blo : Bool}),

View File

@ -230,3 +230,10 @@ fn piecewise_signature() {
Err(TypecheckError::TypeMismatch(..))
);
}
#[test]
fn recursive_let() {
assert_matches!(
type_check_expr("let rec f : Num -> Num = fun x => f \"hoi\" in null"),
Err(TypecheckError::TypeMismatch(..))
);
}