From 6713eaff4e90f63904e9c3f36889a5204aebd2f7 Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Thu, 19 Jan 2023 08:13:39 +0000 Subject: [PATCH] letrec tests --- tests/succeed/letrec/factorial.fathom | 14 ++++++++++++++ tests/succeed/letrec/factorial.snap | 14 ++++++++++++++ tests/succeed/letrec/fibonacci.fathom | 10 ++++++++++ tests/succeed/letrec/fibonacci.snap | 11 +++++++++++ tests/succeed/letrec/parity.fathom | 16 ++++++++++++++++ tests/succeed/letrec/parity.snap | 14 ++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 tests/succeed/letrec/factorial.fathom create mode 100644 tests/succeed/letrec/factorial.snap create mode 100644 tests/succeed/letrec/fibonacci.fathom create mode 100644 tests/succeed/letrec/fibonacci.snap create mode 100644 tests/succeed/letrec/parity.fathom create mode 100644 tests/succeed/letrec/parity.snap diff --git a/tests/succeed/letrec/factorial.fathom b/tests/succeed/letrec/factorial.fathom new file mode 100644 index 000000000..bebafe849 --- /dev/null +++ b/tests/succeed/letrec/factorial.fathom @@ -0,0 +1,14 @@ +// FIXME: using (x - 1) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485) + +letrec + factorial : U32 -> U32 + = fun (x : U32) => match x { + 0 => 1, + _ => x * factorial (u32_sub x 1), + }, + factorial_iter : U32 -> U32 -> U32 + = fun (acc: U32) (x : U32) => match x { + 0 => acc, + _ => factorial_iter (acc * x) (u32_sub x 1), + }; +(factorial, factorial_iter) diff --git a/tests/succeed/letrec/factorial.snap b/tests/succeed/letrec/factorial.snap new file mode 100644 index 000000000..0ef71fe95 --- /dev/null +++ b/tests/succeed/letrec/factorial.snap @@ -0,0 +1,14 @@ +stdout = ''' +letrec + factorial : U32 -> U32 = fun x => match x { + 0 => 1, + _ => x * factorial (x - (1 : U32)), + }, + factorial_iter : U32 -> U32 -> U32 = fun acc x => match x { + 0 => acc, + _ => factorial_iter (acc * x) (x - (1 : U32)), + }, +; +(factorial, factorial_iter) : (U32 -> U32, U32 -> U32 -> U32) +''' +stderr = '' diff --git a/tests/succeed/letrec/fibonacci.fathom b/tests/succeed/letrec/fibonacci.fathom new file mode 100644 index 000000000..5eb00ea3b --- /dev/null +++ b/tests/succeed/letrec/fibonacci.fathom @@ -0,0 +1,10 @@ +// FIXME: using (x - 1) or (x - 2) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485) + +letrec + fibonacci : U32 -> U32 + = fun (x : U32) => match x { + 0 => 0, + 1 => 1, + _ => (fibonacci (u32_sub x 1)) + (fibonacci (u32_sub x 2)), + }; +fibonacci diff --git a/tests/succeed/letrec/fibonacci.snap b/tests/succeed/letrec/fibonacci.snap new file mode 100644 index 000000000..6690c6f87 --- /dev/null +++ b/tests/succeed/letrec/fibonacci.snap @@ -0,0 +1,11 @@ +stdout = ''' +letrec + fibonacci : U32 -> U32 = fun x => match x { + 0 => 0, + 1 => 1, + _ => fibonacci (x - (1 : U32)) + fibonacci (x - (2 : U32)), + }, +; +fibonacci : U32 -> U32 +''' +stderr = '' diff --git a/tests/succeed/letrec/parity.fathom b/tests/succeed/letrec/parity.fathom new file mode 100644 index 000000000..266947892 --- /dev/null +++ b/tests/succeed/letrec/parity.fathom @@ -0,0 +1,16 @@ +// FIXME: using (x - 1) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485) + +letrec + is_even : U32 -> Bool + = fun (x : U32) => match x { + 0 => true, + _ => is_odd (u32_sub x 1), + }, + + is_odd : U32 -> Bool + = fun (x : U32) => match x { + 0 => false, + _ => is_even (u32_sub x 1), + }, +; +(is_even, is_odd) diff --git a/tests/succeed/letrec/parity.snap b/tests/succeed/letrec/parity.snap new file mode 100644 index 000000000..edcb12866 --- /dev/null +++ b/tests/succeed/letrec/parity.snap @@ -0,0 +1,14 @@ +stdout = ''' +letrec + is_even : U32 -> Bool = fun x => match x { + 0 => true, + _ => is_odd (x - (1 : U32)), + }, + is_odd : U32 -> Bool = fun x => match x { + 0 => false, + _ => is_even (x - (1 : U32)), + }, +; +(is_even, is_odd) : (U32 -> Bool, U32 -> Bool) +''' +stderr = ''