-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.mli
94 lines (76 loc) · 3.11 KB
/
expr.mli
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
(*
CS 51 Final Project
MiniML -- Expressions
*)
(* ......................................................................
Abstract syntax of MiniML expressions
*)
(* Unary operators *)
type unop =
| Negate
;;
(* Binary operators *)
type binop =
| Plus
| Minus
| Times
| Equals
| LessThan
;;
(* Variable identifers *)
type varid = string ;;
(* Expressions *)
type expr =
| Var of varid (* variables *)
| Num of int (* integers *)
| Bool of bool (* booleans *)
| String of string (* strings *)
| Unop of unop * expr (* unary operators *)
| Binop of binop * expr * expr (* binary operators *)
| Conditional of expr * expr * expr (* if then else *)
| Fun of varid * expr (* function definitions *)
| Let of varid * expr * expr (* local naming *)
| Letrec of varid * expr * expr (* recursive local naming *)
| Raise (* exceptions *)
| Unassigned (* (temporarily) unassigned *)
| App of expr * expr (* function applications *)
;;
(*......................................................................
Manipulation of variable names (varids) and sets of them
*)
(* varidset -- Sets of varids *)
type varidset ;;
(* same_vars varids1 varids2 -- Tests to see if two `varid` sets have
the same elements (for testing purposes) *)
val same_vars : varidset -> varidset -> bool ;;
(* vars_of_list varids -- Generates a set of variable names from a
list of `varid`s (for testing purposes) *)
val vars_of_list : varid list -> varidset ;;
(* list_of_vars: creates a list from a set of variable IDs.
Useful for testing. *)
val list_of_vars : varidset -> varid list ;;
(* free_vars exp -- Returns the set of `varid`s corresponding to free
variables in `exp` *)
val free_vars : expr -> varidset ;;
(* new_varname () -- Returns a freshly minted `varid` *)
val new_varname : unit -> varid ;;
(*......................................................................
Substitution
Substitution of expressions for free occurrences of variables is the
cornerstone of the substitution model for functional programming
semantics.
*)
(* subst var_name repl exp -- Return the expression `exp` with `repl`
substituted for free occurrences of `var_name`, avoiding variable
capture *)
(* optional argument new_varname has been added to enable dependency injection in testing *)
val subst : ?new_varname:(unit -> varid) -> varid -> expr -> expr -> expr ;;
(*......................................................................
String representations of expressions
*)
(* exp_to_concrete_string exp -- Returns a string representation of
the concrete syntax of the expression `exp` *)
val exp_to_concrete_string : expr -> string ;;
(* exp_to_abstract_string exp -- Return a string representation of the
abstract syntax of the expression `exp` *)
val exp_to_abstract_string : expr -> string ;;