mirror of
https://github.com/anoma/juvix.git
synced 2024-11-30 14:13:27 +03:00
98b1daec7d
Print JuvixCore InfoTable in such a way that it can be parsed back by the JuvixCore parser. * Depends on PR #1832 * Depends on PR #1862 * Closes #1841 * Adds "JuvixCore print" tests which read the files from Core/positive/*.jvc, print them, read them back and check if the evaluation results are preserved. --------- Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
42 lines
1.1 KiB
Plaintext
42 lines
1.1 KiB
Plaintext
-- lists
|
|
|
|
type list {
|
|
nil : list;
|
|
cons : Any -> list -> list;
|
|
};
|
|
|
|
def head := \l case l of { cons h _ := h };
|
|
def tail := \l case l of { cons _ t := t };
|
|
def null := \l case l of { nil := true; cons _ _ := false };
|
|
def map := \f \l case l of { nil := nil; cons h t := cons (f h) (map f t) };
|
|
def foldl := \f \acc \l case l of { nil := acc; cons h t := foldl f (f acc h) t };
|
|
def foldr := \f \acc \l case l of { nil := acc; cons h t := f h (foldr f acc t) };
|
|
def filter := \f \l
|
|
case l of {
|
|
nil := nil;
|
|
cons h t :=
|
|
if f h then
|
|
cons h (filter f t)
|
|
else
|
|
filter f t
|
|
};
|
|
def rev := foldl (\acc \x cons x acc) nil;
|
|
|
|
def gen := \n if n = 0 then nil else cons n (gen (n - 1));
|
|
|
|
def sum := \n foldl (+) 0 (gen n);
|
|
def sum' := \n foldr (+) 0 (gen n);
|
|
|
|
def foldl' := \f \acc \l if null l then acc else foldl' f (f acc (head l)) (tail l);
|
|
def sum'' := \n foldl' (+) 0 (gen n);
|
|
|
|
def writeLn := \x write x >> write "\n";
|
|
|
|
writeLn (gen 10) >>
|
|
writeLn (rev (gen 10)) >>
|
|
writeLn (filter (\x x > 5) (gen 10)) >>
|
|
writeLn (rev (map (\x x - 1) (gen 10))) >>
|
|
writeLn (sum 1000) >>
|
|
writeLn (sum' 1000) >>
|
|
writeLn (sum'' 1000)
|