mirror of
https://github.com/anoma/juvix.git
synced 2024-12-04 06:23:13 +03:00
c9b8cdd5e9
This implements a basic version of the algorithm from: Luc Maranget, [Compiling pattern matching to good decision trees](http://moscova.inria.fr/~maranget/papers/ml05e-maranget.pdf). No heuristics are used - the first column is always chosen. * Closes #1798 * Closes #1225 * Closes #1926 * Adds a global `--no-coverage` option which turns off coverage checking in favour of generating runtime failures * Changes the representation of Match patterns in JuvixCore to achieve a more streamlined implementation * Adds options to the Core pipeline
61 lines
1.7 KiB
Plaintext
61 lines
1.7 KiB
Plaintext
-- typed match
|
|
|
|
type List {
|
|
cons : Π A : Type, A -> List A -> List A;
|
|
nil : Π A : Type, List A;
|
|
};
|
|
|
|
def lgen : Int → List Int := \(n : Int)
|
|
if n = 0 then nil Int else cons Int n (lgen (n - 1));
|
|
|
|
def sum2 : List Int → List Int := \(x : List Int) {
|
|
match x : List Int with : List Int {
|
|
cons _ x y@(cons _ z _) := cons Int (x + z) (sum2 y);
|
|
_ := x
|
|
}
|
|
};
|
|
|
|
def sum : List Int → Int := \(x : List Int) {
|
|
match x : List Int with : Int {
|
|
cons _ x y := x + sum y;
|
|
_ := 0
|
|
}
|
|
};
|
|
|
|
type Tree {
|
|
leaf : Π A : Type, A -> Tree A;
|
|
node : Π A : Type, Tree A -> Tree A -> Tree A;
|
|
};
|
|
|
|
def gen : Int → Tree Int := \(n : Int)
|
|
if n <= 0 then leaf Int 1 else node Int (gen (n - 2)) (gen (n - 1));
|
|
|
|
def g : Π A : Type, Tree A → Tree A := \(A : Type) \(t : Tree A) {
|
|
match t : Tree A with : Tree A {
|
|
leaf (A : Type) (x : A) := t;
|
|
node (A : Type) (node (B : Type) (l1 : Tree B) (l2 : Tree A)) (r : Tree A) := r;
|
|
node (A' : Type) (l : Tree A) (r : Tree A') := node A' r l;
|
|
}
|
|
};
|
|
|
|
def f : Tree Int → Int := \(t : Tree Int) match t : Tree Int with : Int {
|
|
leaf _ (x : Int) := x;
|
|
node _ (l : Tree Int) r :=
|
|
match (g Int l : Tree Int), (g Int r : Tree Int) with : Int {
|
|
leaf _ _, leaf _ _ := 0 - 6;
|
|
node (A : Type) (l : Tree A) (r : Tree Int), leaf _ _ := ((f l + f r) * 2) % 20000;
|
|
node (A : Type) (l1 : Tree A) (r1 : Tree A), node (B : Type) (l2 : Tree A) (r2 : Tree B) := ((f l1 + f r1) * (f l2 + f r2)) % 20000;
|
|
_, node _ l r := ((f l + f r) * (0 - 3)) % 20000;
|
|
}
|
|
};
|
|
|
|
def writeLn := \x write x >> write "\n";
|
|
|
|
writeLn (sum (sum2 (lgen 5))) >>
|
|
writeLn (f (gen 10)) >>
|
|
writeLn (f (gen 15)) >>
|
|
writeLn (f (gen 16)) >>
|
|
writeLn (f (gen 17)) >>
|
|
writeLn (f (gen 18)) >>
|
|
writeLn (f (gen 20))
|