Skip to content

Instantly share code, notes, and snippets.

@jkoppel
Created Jun 16, 2017
Embed
What would you like to do?
adt Prog {
Var1 {}
Var2 {}
Const {int val; }
Add { Prog left; Prog right; }
Mul { Prog left; Prog right; }
}
int interpret(Prog prog, int x, int y) {
switch(prog) {
case Var1: return x;
case Var2: return y;
case Const: return prog.val;
case Add: return interpret(prog.left, x, y) + interpret(prog.right, x, y);
case Mul: return interpret(prog.left, x, y) * interpret(prog.right, x, y);
}
}
int size(Prog prog) {
switch (prog) {
case Var1: return 1;
case Var2: return 1;
case Const: return 1;
case Add: return 1 + size(prog.left) + size(prog.right);
case Mul: return 1 + size(prog.left) + size(prog.right);
}
}
// x + 2 * y + x * x + 5
// x * y + 5
harness void main() {
Prog p = ??;
//minimize(size(p));
//assert(interpret(p,0,0) == 5);
//assert(interpret(p,0,1) == 5);
//assert(interpret(p,1,0) == 5);
//assert(interpret(p,1,1) == 6);
//assert(interpret(p,2,2) == 9);
assert(interpret(p,0,0) == 5);
assert(interpret(p,0,1) == 7);
assert(interpret(p,1,0) == 7);
assert(interpret(p,1,1) == 9);
assert(interpret(p,2,0) == 11);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment