aboutsummaryrefslogtreecommitdiff
path: root/discocaml
diff options
context:
space:
mode:
Diffstat (limited to 'discocaml')
-rw-r--r--discocaml/ast.ml2
-rw-r--r--discocaml/draw_tree.ml4
-rw-r--r--discocaml/eval.ml38
3 files changed, 34 insertions, 10 deletions
diff --git a/discocaml/ast.ml b/discocaml/ast.ml
index 92ac32d..3ba3f63 100644
--- a/discocaml/ast.ml
+++ b/discocaml/ast.ml
@@ -127,7 +127,7 @@ let parsetree_of_subexpr (ast : 'a ast) : expr -> Parsetree.expression =
in
let rec loop : expr -> Parsetree.expression = function
- | App (_, _) as expr ->
+ | App _ as expr ->
let f, xs = list_of_apps expr in
let xs = List.map (fun x -> (Asttypes.Nolabel, loop (subexpr x))) xs in
Wrap.expression (Pexp_apply (loop f, xs))
diff --git a/discocaml/draw_tree.ml b/discocaml/draw_tree.ml
index e04a59c..3d01412 100644
--- a/discocaml/draw_tree.ml
+++ b/discocaml/draw_tree.ml
@@ -8,11 +8,11 @@ let add_node (fmt : Format.formatter) (i : expr index) (expr : expr) : unit =
" expr%d [fontname=\"CMU Typewriter Text Bold\", label=\"apply\"];\n"
i.index
| Int n -> Format.fprintf fmt " expr%d [label=\"%d\"];\n" i.index n
- | Lam (_, _) -> Format.fprintf fmt " expr%d [label=\"λ\"];\n" i.index
+ | Lam _ -> Format.fprintf fmt " expr%d [label=\"λ\"];\n" i.index
| Prim (Add, _) -> Format.fprintf fmt " expr%d [label=\"+\"];\n" i.index
| Prim (Sub, _) -> Format.fprintf fmt " expr%d [label=\"-\"];\n" i.index
| Prim (Mul, _) -> Format.fprintf fmt " expr%d [label=\"*\"];\n" i.index
- | Var n -> Format.fprintf fmt " expr%d [label=%S];\n" i.index n
+ | Var x -> Format.fprintf fmt " expr%d [label=%S];\n" i.index x
let add_expr_edges (ast : 'a ast) (fmt : Format.formatter)
(nodes : IntSet.t ref) : expr index -> unit =
diff --git a/discocaml/eval.ml b/discocaml/eval.ml
index aa90d41..5e2cae4 100644
--- a/discocaml/eval.ml
+++ b/discocaml/eval.ml
@@ -5,11 +5,9 @@ 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)
- | _ -> ())
+ match get_subexpr ast f with Lam _ -> raise (FoundRedex i) | _ -> ())
| Int _ -> ()
- | Lam (_, _) -> ()
+ | Lam _ -> ()
| 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)
@@ -23,7 +21,7 @@ let find_redex_cbv_in (ast : expr ast) : expr index -> unit =
loop f;
loop x
| Int _ -> ()
- | Lam (_, _) -> ()
+ | Lam _ -> ()
| Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
loop l;
loop r
@@ -40,7 +38,7 @@ let find_redex_cbn_in (ast : expr ast) : expr index -> unit =
loop f;
loop x
| Int _ -> ()
- | Lam (_, _) -> ()
+ | Lam _ -> ()
| Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
loop l;
loop r
@@ -62,13 +60,39 @@ let find_redex_cbn (ast : expr ast) : expr index option =
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) ->
+ let f' = loop f and x' = loop x in
+ add (App (f', x'))
+ | Int _ -> 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) -> failwith "TODO"
+ | 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)