-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.ml
More file actions
219 lines (197 loc) · 6.64 KB
/
Copy pathtypes.ml
File metadata and controls
219 lines (197 loc) · 6.64 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
open Env
(** Types *)
type lam = (string * int) list * (Ast.node list)
type prim_value =
| String of string
| Integer of int
| Boolean of bool
| Symbol of string
| Tid of tid
| Cons of value * value
| Unlocked
| Locked
| Nil
| Unspecified
| Closure of lam * env
| Primitive of string
| Kont of kont
and value =
| AbsUnique of prim_value
| AbsString
| AbsInteger
| AbsBoolean
| AbsSymbol
| AbsList
and kont =
| OperatorKont of int * Ast.node list * env * addr
| OperandsKont of int * value * Ast.node list * value list * env * addr
| BeginKont of int * Ast.node list * env * addr
| LetRecKont of int * addr * (Ast.var * Ast.node) list *
Ast.node list * env * addr
| IfKont of int * Ast.node * Ast.node * env * addr
| SetKont of int * string * env * addr
| CallccKont of int * env * addr
| HaltKont
and prim = string * (value list -> value option)
and time =
| IntTime of int
| KCallSitesTime of Ast.node list
and addr =
| HaltAddr
| TagAddr of int * time
| VarAddr of string * time
| PrimAddr of string * time
| KontAddr of Ast.node * time
| TidAddr of tid
and tid =
| InitialTid
| TagTid of int * time
| IntTid of int
and env = addr Env.t
type tag = int
(** String conversion *)
let string_of_kont = function
| OperatorKont (t, _, _, _) -> "Operator-" ^ (string_of_int t)
| OperandsKont (t, _, _, _, _, _) -> "Operands-" ^ (string_of_int t)
| BeginKont (t, _, _, _) -> "Begin-" ^ (string_of_int t)
| LetRecKont (t, _, _, _, _, _) -> "LetRec-" ^ (string_of_int t)
| IfKont (t, _, _, _, _) -> "If-" ^ (string_of_int t)
| SetKont (t, _, _, _) -> "Set-" ^ (string_of_int t)
| CallccKont (t, _, _) -> "Callcc-" ^ (string_of_int t)
| HaltKont -> "Halt"
let string_of_time = function
| IntTime n -> string_of_int n
| KCallSitesTime l -> "[" ^ (String.concat ","
(List.map Ast.string_of_node l)) ^ "]"
let string_of_tid = function
| InitialTid -> "1"
| IntTid n -> (string_of_int n)
| TagTid (n, t) -> (string_of_int n) ^ (string_of_time t)
let rec string_of_prim_value = function
| String s -> "\"" ^ s ^ "\""
| Integer n -> string_of_int n
| Boolean true -> "#t"
| Boolean false -> "#f"
| Symbol sym -> "'" ^ sym
| Tid t -> "#<thread " ^ (string_of_tid t) ^ ">"
| Cons (car, cdr) ->
"(" ^ (string_of_value car) ^ " . " ^
(string_of_value cdr) ^")"
| Locked -> "#locked"
| Unlocked -> "#unlocked"
| Nil -> "()"
| Unspecified -> "#<unspecified>"
| Closure ((args, body), _) ->
"#<closure (lambda (" ^ (String.concat " "
(List.map fst args)) ^ ") " ^
(Ast.string_of_nodes " " body) ^ ")>"
| Primitive name -> "#<primitive " ^ name ^ ">"
| Kont k -> "#<continuation " ^ (string_of_kont k) ^ ">"
and string_of_value = function
| AbsUnique v -> string_of_prim_value v
| AbsString -> "Str"
| AbsInteger -> "Int"
| AbsBoolean -> "Bool"
| AbsSymbol -> "Sym"
| AbsList -> "List"
(** Addresses *)
module Addr = struct
type t = addr
let compare = Pervasives.compare
let is_reclaimable = function
| PrimAddr _
| TidAddr _ -> false
| _ -> true
let string_of_address = function
| HaltAddr ->
"HaltAddr"
| TagAddr (n, t) ->
"TagAddr(" ^ (string_of_int n) ^ "," ^ (string_of_time t) ^ ")"
| VarAddr (s, t) ->
"VarAddr(" ^ s ^ "," ^ (string_of_time t) ^ ")"
| PrimAddr (s, t) ->
"PrimAddr(" ^ s ^ "," ^ (string_of_time t) ^ ")"
| KontAddr (n, t) ->
"KontAddr(" ^ (Ast.string_of_node n) ^ "," ^ (string_of_time t) ^ ")"
| TidAddr t ->
"TidAddr(" ^ (string_of_tid t) ^ ")"
end
module AddressSet = Set.Make(struct
type t = addr
let compare = Pervasives.compare
end)
let string_of_address_set set =
"{" ^ (String.concat ", " (List.map Addr.string_of_address
(AddressSet.elements set))) ^ "}"
(** Some operations on primitive values and (abstract) values *)
let value_equals x y = compare x y = 0
let value_subsumes x y = match x, y with
| AbsUnique a, AbsUnique b -> a = b
| AbsString, AbsString
| AbsInteger, AbsInteger
| AbsBoolean, AbsBoolean
| AbsSymbol, AbsSymbol
| AbsList, AbsList
| AbsString, AbsUnique (String _)
| AbsInteger, AbsUnique (Integer _)
| AbsBoolean, AbsUnique (Boolean _)
| AbsSymbol, AbsUnique (Symbol _) -> true
| _ -> false
let merge x y = match x, y with
| AbsUnique (Primitive _), AbsUnique (Primitive _) -> None
| AbsUnique x, AbsUnique y when value_equals x y -> Some (AbsUnique x)
| AbsUnique (Integer _), AbsUnique (Integer _) -> Some AbsInteger
| AbsUnique (String _), AbsUnique (String _) -> Some AbsString
| AbsUnique (Symbol _), AbsUnique (Symbol _) -> Some AbsSymbol
| AbsUnique (Boolean _), AbsUnique (Boolean _) -> Some AbsBoolean
| AbsUnique (Cons _), AbsUnique (Cons _) -> Some AbsList
| AbsUnique Nil, AbsUnique Nil -> Some (AbsUnique Nil)
| AbsUnique (Cons (_, _)), AbsUnique Nil
| AbsUnique Nil, AbsUnique (Cons (_, _)) -> Some AbsList
| AbsString, AbsString
| AbsString, AbsUnique (String _)
| AbsUnique (String _), AbsString -> Some AbsString
| AbsInteger, AbsInteger
| AbsInteger, AbsUnique (Integer _)
| AbsUnique (Integer _), AbsInteger -> Some AbsInteger
| AbsBoolean, AbsBoolean
| AbsBoolean, AbsUnique (Boolean _)
| AbsUnique (Boolean _), AbsBoolean -> Some AbsBoolean
| AbsSymbol, AbsSymbol
| AbsSymbol, AbsUnique (Symbol _)
| AbsUnique (Symbol _), AbsSymbol -> Some AbsSymbol
| AbsList, AbsList
| AbsList, AbsUnique (Cons _)
| AbsList, AbsUnique Nil
| AbsUnique (Cons _), AbsList
| AbsUnique Nil, AbsList -> Some AbsList
| _ -> None
let value_op_int f x y = match x, y with
| AbsInteger, _ | _, AbsInteger -> Some AbsInteger
| AbsUnique (Integer v1), AbsUnique (Integer v2) ->
Some (AbsUnique (Integer (f v1 v2)))
| _ -> None
let value_comp_int f x y = match x, y with
| AbsInteger, _ | _, AbsInteger -> Some AbsBoolean
| AbsUnique (Integer v1), AbsUnique (Integer v2) ->
Some (AbsUnique (Boolean (f v1 v2)))
| _ -> None
let value_add = value_op_int (+)
let value_sub = value_op_int (-)
let value_mul = value_op_int ( * )
let value_div = value_op_int (/)
let value_mod = value_op_int (mod)
let value_neg x = match x with
| AbsUnique (Integer a) -> Some (AbsUnique (Integer (-a)))
| AbsInteger -> Some AbsInteger
| _ -> None
let value_gt = value_comp_int (>)
let value_gte = value_comp_int (>=)
let value_lt = value_comp_int (<)
let value_lte = value_comp_int (<=)
let value_int_eq = value_comp_int (=)
let value_int_neq = value_comp_int (<>)
let value_not x = match x with
| AbsUnique (Boolean b) -> Some (AbsUnique (Boolean (not b)))
| AbsBoolean -> Some AbsBoolean
| _ -> None