expr13extra.ml 547 Bytes
Newer Older
POTTIER Francois's avatar
POTTIER Francois committed
1
open Expr12
POTTIER Francois's avatar
POTTIER Francois committed
2
open Expr13
3
open Expr13double
POTTIER Francois's avatar
POTTIER Francois committed
4 5 6 7 8 9 10 11 12 13 14 15 16

let const k = E (EConst k)
let add e1 e2 = E (EAdd (e1, e2))

let rec eval (E e) =
  match e with
  | EConst k -> k
  | EAdd (e1, e2) -> eval e1 + eval e2

let e : expr =
  add (const 1) (const 2)

let () =
17
  (* should print: 3 then 6 *)
POTTIER Francois's avatar
POTTIER Francois committed
18
  Printf.printf "%d\n%d\n%!" (eval e) (eval (double e))
19

POTTIER Francois's avatar
POTTIER Francois committed
20
let omap : 'expr1 'expr2 . ('expr1 -> 'expr2) -> 'expr1 oexpr -> 'expr2 oexpr =
21 22 23 24 25 26
  fun f e ->
    let v = object
      inherit [_] omap
      method visit_'expr _env e = f e
    end in
    v # visit_oexpr () e