aboutsummaryrefslogtreecommitdiff
path: root/discocaml/eval.ml
blob: 39575310fb9235613cfc4b4c79a86c3efdce0fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
open Ast

exception FoundRedex of expr index

let check_redex (ast : expr ast) (i : expr index) : unit =
  match get_subexpr ast i with
  | App (f, _) -> (
      match get_subexpr ast f with Lam _ -> raise (FoundRedex i) | _ -> ())
  | Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) -> (
      match (get_subexpr ast l, get_subexpr ast r) with
      | Int _, Int _ -> raise (FoundRedex i)
      | _ -> ())
  | Var _ -> ()
  | Cons _ | Int _ | Lam _ | Nil -> ()

let find_redex_cbv_in (ast : expr ast) : expr index -> unit =
  let rec loop (i : expr index) : unit =
    (match get_subexpr ast i with
    | App (f, x) ->
        loop f;
        loop x
    | Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
        loop l;
        loop r
    | Var _ -> ()
    | Cons _ | Int _ | Lam _ | Nil -> ());
    check_redex ast i
  in
  loop

let find_redex_cbn_in (ast : expr ast) : expr index -> unit =
  let rec loop (i : expr index) : unit =
    check_redex ast i;
    match get_subexpr ast i with
    | App (f, x) ->
        loop f;
        loop x
    | Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
        loop l;
        loop r
    | Var _ -> ()
    | Cons _ | Int _ | Lam _ | Nil -> ()
  in
  loop

let find_redex_cbv (ast : expr ast) : expr index option =
  try
    find_redex_cbv_in ast ast.root;
    None
  with FoundRedex i -> Some i

let find_redex_cbn (ast : expr ast) : expr index option =
  try
    find_redex_cbn_in ast ast.root;
    None
  with FoundRedex i -> Some i

exception NotARedex of expr ast

let subst (ast : expr ast) (from : string) (to_ : expr index) :
    expr index -> expr index =
  let rec loop (i : expr index) : expr index =
    let add (expr : expr) : expr index =
      if get_subexpr ast i = expr then i
      else
        let index = Arraylist.length ast.subexprs in
        Arraylist.push ast.subexprs expr;
        { index }
    in
    match get_subexpr ast i with
    | App (f, x) -> add (App (loop f, loop x))
    | Cons (hd, tl) -> add (Cons (loop hd, loop tl))
    | Int _ | Nil -> i
    | Lam (x, b) -> if String.equal from x then i else add (Lam (x, loop b))
    | Prim (Add, (l, r)) -> add (Prim (Add, (loop l, loop r)))
    | Prim (Sub, (l, r)) -> add (Prim (Sub, (loop l, loop r)))
    | Prim (Mul, (l, r)) -> add (Prim (Mul, (loop l, loop r)))
    | Var x -> if String.equal from x then to_ else i
  in
  loop

let reduce (ast : expr ast) (i : expr index) : expr ast =
  let fail () = raise (NotARedex { ast with root = i }) in
  let ast = copy ast in
  let must_int j = match get_subexpr ast j with Int n -> n | _ -> fail () in
  Arraylist.set ast.subexprs i.index
    (match get_subexpr ast i with
    | App (f, x) -> (
        match get_subexpr ast f with
        | Lam (x', b) -> get_subexpr ast (subst ast x' x b)
        | _ -> fail ())
    | Prim (Add, (l, r)) -> Int (must_int l + must_int r)
    | Prim (Sub, (l, r)) -> Int (must_int l - must_int r)
    | Prim (Mul, (l, r)) -> Int (must_int l * must_int r)
    | _ -> fail ());
  ast