aboutsummaryrefslogtreecommitdiff
path: root/discocaml/eval.ml
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-20 00:00:17 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-20 00:00:17 -0600
commit514896e05424d9df97295840b3fa6f2ad46620ab (patch)
treef69abbb4971a3bf9cc4ca3152b8f6cc141069cc8 /discocaml/eval.ml
parent738f437f59bf2408dcdc6514ffa2d7a2ee5a61c5 (diff)
Adds lists, although they don't evaluate yet.
Diffstat (limited to 'discocaml/eval.ml')
-rw-r--r--discocaml/eval.ml18
1 files changed, 7 insertions, 11 deletions
diff --git a/discocaml/eval.ml b/discocaml/eval.ml
index 5e2cae4..3957531 100644
--- a/discocaml/eval.ml
+++ b/discocaml/eval.ml
@@ -6,13 +6,12 @@ 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) | _ -> ())
- | Int _ -> ()
- | 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)
| _ -> ())
| Var _ -> ()
+ | Cons _ | Int _ | Lam _ | Nil -> ()
let find_redex_cbv_in (ast : expr ast) : expr index -> unit =
let rec loop (i : expr index) : unit =
@@ -20,12 +19,11 @@ let find_redex_cbv_in (ast : expr ast) : expr index -> unit =
| App (f, x) ->
loop f;
loop x
- | Int _ -> ()
- | Lam _ -> ()
| Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
loop l;
loop r
- | Var _ -> ());
+ | Var _ -> ()
+ | Cons _ | Int _ | Lam _ | Nil -> ());
check_redex ast i
in
loop
@@ -37,12 +35,11 @@ let find_redex_cbn_in (ast : expr ast) : expr index -> unit =
| App (f, x) ->
loop f;
loop x
- | Int _ -> ()
- | Lam _ -> ()
| Prim (Add, (l, r)) | Prim (Sub, (l, r)) | Prim (Mul, (l, r)) ->
loop l;
loop r
| Var _ -> ()
+ | Cons _ | Int _ | Lam _ | Nil -> ()
in
loop
@@ -71,10 +68,9 @@ let subst (ast : expr ast) (from : string) (to_ : expr index) :
{ 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
+ | 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)))