-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_utils.ml
54 lines (43 loc) · 1.54 KB
/
eval_utils.ml
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
open Expr
open Env.Env
(* This file only contains one module at the moment, but it's intended
to host smaller modules with self-contained logic helpful to
the evaluation functions. Can be extended as needed. *)
(* EXPR_CONVERSIONS: functions for converting expressions from one
type to another.*)
module type EXPR_CONVERSIONS = sig
val expr_from_val : value -> expr
val closure_from_val : value -> expr * env
(* val_from_value: extracts the value from a closure if needed. *)
val val_from_value : value -> value
val num_from_expr : expr -> int
val bool_from_expr : expr -> bool
end
module ExprConversions = struct
let expr_from_val (value : value) : expr =
match value with
| Val expr -> expr
| Closure _ ->
err "expr_from_val" "expected Val, received Closure"
(value_to_string value)
let closure_from_val (value : value) : expr * env =
match value with
| Closure (exp, env) -> (exp, env)
| Val _ ->
err "closure_from_val" "expected Closure, received Val"
(value_to_string value)
let val_from_value (v : value) : value =
match v with Val _ -> v | Closure (value, _) -> Val value
let num_from_expr (exp : expr) : int =
match exp with
| Num num -> num
| _ ->
err "num_from_expr" "expected Num, received other"
(Expr.exp_to_abstract_string exp)
let bool_from_expr (exp : expr) : bool =
match exp with
| Bool bl -> bl
| _ ->
err "bool_from_expr" "expected Bool, received other"
(Expr.exp_to_abstract_string exp)
end