(* AST for a K-like language *) use "arr.sml"; (* TODO: open, nto use, for compiled version *) datatype ast = Leaf of arr | Nilad of unit -> arr | Monad of (arr -> arr) * ast | Dyad of ((arr * arr) -> arr) * ast * ast; (* TODO: higher-arity functions, projections &c. Unless you just want to do an APL rather than a K? *) fun eval_ast(t) = case t of Leaf(a) => a | Nilad(f) => f() | Monad(f,r) => f(eval_ast(r)) | Dyad(f,l,r) => f(eval_ast(l),eval_ast(r)); (* TODO: codegen_ast (eval_ast evaluates it in ML, but we need to output ASM!) *) (* (* Test data *) val x = S(N(0w2)); val y = S(N(0w3)); val a0 = A(x,y); val a1 = A(A(A(x,y),x),y); fun f() = S(N(0w4)); (* nilad *) fun g(x) = A(x,x); (* monad *) fun h(x,y) = A(x,y); (* dyad *) val t0 = Leaf(x); val t1 = Leaf(a0); val t2 = Leaf(a1); val tn = Nilad(f); val tm = Monad(g,t1); val td = Dyad(h,t1,t2); *)