mirror of
https://github.com/anoma/juvix.git
synced 2024-12-04 06:23:13 +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>
54 lines
1.2 KiB
Plaintext
54 lines
1.2 KiB
Plaintext
-- streams without memoization
|
|
|
|
type Unit {
|
|
unit : Unit;
|
|
};
|
|
|
|
type list {
|
|
nil : list;
|
|
cons : Any -> list -> list;
|
|
};
|
|
|
|
type Stream {
|
|
scons : Any -> (Unit -> Stream) -> Stream;
|
|
};
|
|
|
|
def force : (Unit -> Stream) -> Stream := \f f unit;
|
|
|
|
def filter : (Any -> Bool) -> (Unit -> Stream) -> Unit -> Stream := \p \s \_
|
|
case force s of {
|
|
scons h t :=
|
|
if p h then
|
|
scons h (filter p t)
|
|
else
|
|
force (filter p t)
|
|
};
|
|
|
|
def take : Int -> (Unit -> Stream) -> list := \n \s
|
|
if n = 0 then
|
|
nil
|
|
else
|
|
case force s of {
|
|
scons h t := cons h (take (n - 1) t)
|
|
};
|
|
|
|
def nth : Int -> (Unit -> Stream) -> Any := \n \s
|
|
case force s of {
|
|
scons h t := if n = 0 then h else nth (n - 1) t
|
|
};
|
|
|
|
def numbers : Int -> Unit -> Stream := \n \_ scons n (numbers (n + 1));
|
|
|
|
def indivisible : Int -> Int -> Bool := \n \x if x % n = 0 then false else true;
|
|
def eratostenes : (Unit -> Stream) -> Unit -> Stream := \s \_
|
|
case force s of {
|
|
scons n t :=
|
|
scons n (eratostenes (filter (indivisible n) t))
|
|
};
|
|
def primes : Unit -> Stream := eratostenes (numbers 2);
|
|
|
|
def writeLn := \x write x >> write "\n";
|
|
|
|
writeLn (take 10 primes) >>
|
|
writeLn (nth 100 primes)
|