Source file shape_reduce.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
open Shape
type result =
| Resolved of Uid.t
| Resolved_alias of Uid.t * result
| Resolved_local_use of Uid.t
| Unresolved of t
| Approximated of Uid.t option
| Missing_uid of t
| Internal_error_missing_uid
let rec print_result fmt result =
match result with
| Resolved uid ->
Format.fprintf fmt "@[Resolved:@ %a@]" Uid.print uid
| Resolved_alias (uid, r) ->
Format.fprintf fmt "@[Alias:@ %a@] ->@ %a"
Uid.print uid print_result r
| Resolved_local_use uid ->
Format.fprintf fmt "@[Local opaque:@ %a@]" Uid.print uid
| Unresolved shape ->
Format.fprintf fmt "@[Unresolved:@ %a@]" print shape
| Approximated (Some uid) ->
Format.fprintf fmt "@[Approximated:@ %a@]" Uid.print uid
| Approximated None ->
Format.fprintf fmt "Approximated: No uid"
| Missing_uid shape ->
Format.fprintf fmt "Missing uid: %a" print shape
| Internal_error_missing_uid ->
Format.fprintf fmt "Missing uid"
let find_shape env id =
let namespace = Shape.Sig_component_kind.Module in
Env.shape_of_path ~namespace env (Pident id)
module Make(Params : sig
val fuel : int
val read_unit_shape : unit_name:string -> t option
end) = struct
type nf = { uid: Uid.t option; desc: nf_desc; approximated: bool }
and nf_desc =
| NVar of var
| NApp of nf * nf
| NAbs of local_env * var * t * delayed_nf
| NStruct of delayed_nf Item.Map.t
| NAlias of delayed_nf
| NProj of nf * Item.t
| NLeaf
| NPack of Ident.t
| NComp_unit of string
| NError of string
and delayed_nf = Thunk of local_env * t
and local_env = delayed_nf option Ident.Map.t
let[@warning "-32"] print_nf fmt nf =
let print_uid_opt =
Format.pp_print_option (fun fmt -> Format.fprintf fmt "<%a>" Uid.print)
in
let rec aux fmt { uid; desc; _ }=
match desc with
| NComp_unit name -> Format.fprintf fmt "CU %s" name
| NLeaf ->
Format.fprintf fmt "<%a>" print_uid_opt uid
| NPack id ->
Format.fprintf fmt "<%a>" Ident.print id
| NVar var ->
Format.fprintf fmt "%a%a" Ident.print var print_uid_opt uid
| NProj (nf, item) ->
Format.fprintf fmt "(%a.%a)%a" aux nf Item.print item print_uid_opt uid
| NApp (nf1, nf2) ->
Format.fprintf fmt "@[%a(@,%a)%a@]" aux nf1 aux nf2
print_uid_opt uid
| NStruct map ->
let print_map fmt =
Item.Map.iter (fun item (Thunk (_, t)) ->
Format.fprintf fmt "@[<hv 2>%a ->@ %a;@]@,"
Item.print item
print t
)
in
if Item.Map.is_empty map then
Format.fprintf fmt "@[<hv>{%a}@]" print_uid_opt uid
else
Format.fprintf fmt "{@[<v>%a@,%a@]}" print_uid_opt uid print_map map
| NAlias (Thunk (_, t)) ->
Format.fprintf fmt "Alias@[(@[<v>%a@,<delayed:%a>@])@]"
print_uid_opt uid print t
| NError s ->
Format.fprintf fmt "Error %s" s
| NAbs _ -> ()
in
Format.fprintf fmt "@[%a@]@;" aux nf
let approx_nf nf = { nf with approximated = true }
let in_memo_table memo_table memo_key f arg =
match Hashtbl.find memo_table memo_key with
| res -> res
| exception Not_found ->
let res = f arg in
Hashtbl.replace memo_table memo_key res;
res
type env = {
fuel: int ref;
global_env: Env.t;
local_env: local_env;
reduce_memo_table: (local_env * t, nf) Hashtbl.t;
read_back_memo_table: (nf, t) Hashtbl.t;
}
let bind env var shape =
{ env with local_env = Ident.Map.add var shape env.local_env }
let rec reduce_ env t =
let local_env = env.local_env in
let memo_key = (local_env, t) in
in_memo_table env.reduce_memo_table memo_key (reduce__ env) t
and force env (Thunk (local_env, t)) =
reduce_ { env with local_env } t
and reduce__
({fuel; global_env; local_env; _} as env) (t : t) =
let reduce env t = reduce_ env t in
let delay_reduce env t = Thunk (env.local_env, t) in
let return desc = { uid = t.uid; desc; approximated = t.approximated } in
let rec force_aliases nf = match nf.desc with
| NAlias delayed_nf ->
let nf = force env delayed_nf in
force_aliases nf
| _ -> nf
in
let reset_uid_if_new_binding t' =
match t.uid with
| None -> t'
| Some _ as uid -> { t' with uid }
in
if !fuel < 0 then approx_nf (return (NError "NoFuelLeft"))
else
match t.desc with
| Comp_unit unit_name ->
begin match Params.read_unit_shape ~unit_name with
| Some t -> reduce env t
| None -> return (NComp_unit unit_name)
end
| App(f, arg) ->
let f = reduce env f |> force_aliases in
begin match f.desc with
| NAbs(clos_env, var, body, _body_nf) ->
let arg = delay_reduce env arg in
let env = bind { env with local_env = clos_env } var (Some arg) in
reduce env body |> reset_uid_if_new_binding
| _ ->
let arg = reduce env arg in
return (NApp(f, arg))
end
| Proj(str, item) ->
let str = reduce env str |> force_aliases in
let nored () = return (NProj(str, item)) in
begin match str.desc with
| NStruct (items) ->
begin match Item.Map.find item items with
| exception Not_found -> nored ()
| nf -> force env nf |> reset_uid_if_new_binding
end
| _ ->
nored ()
end
| Abs(var, body) ->
let body_nf = delay_reduce (bind env var None) body in
return (NAbs(local_env, var, body, body_nf))
| Var id ->
begin match Ident.Map.find id local_env with
| None -> return (NVar id)
| Some def ->
begin match force env def with
| { uid = Some _; _ } as nf -> nf
| { uid = None; _ } as nf -> { nf with uid = t.uid }
end
| exception Not_found ->
match find_shape global_env id with
| exception Not_found -> return (NVar id)
| res when res = t -> return (NVar id)
| res ->
decr fuel;
reduce env res
end
| Leaf -> return NLeaf
| Pack id -> return (NPack id)
| Struct m ->
let mnf = Item.Map.map (delay_reduce env) m in
return (NStruct mnf)
| Alias t -> return (NAlias (delay_reduce env t))
| Error s -> approx_nf (return (NError s))
and read_back env (nf : nf) : t =
in_memo_table env.read_back_memo_table nf (read_back_ env) nf
and read_back_ env (nf : nf) : t =
{ uid = nf.uid ;
desc = read_back_desc env nf.desc;
approximated = nf.approximated }
and read_back_desc env desc =
let read_back nf = read_back env nf in
let read_back_force dnf = read_back (force env dnf) in
match desc with
| NVar v ->
Var v
| NApp (nft, nfu) ->
App(read_back nft, read_back nfu)
| NAbs (_env, x, _t, nf) ->
Abs(x, read_back_force nf)
| NStruct nstr ->
Struct (Item.Map.map read_back_force nstr)
| NAlias nf -> Alias (read_back_force nf)
| NProj (nf, item) ->
Proj (read_back nf, item)
| NLeaf -> Leaf
| NPack path -> Pack path
| NComp_unit s -> Comp_unit s
| NError s -> Error s
let reduce_memo_table = Hashtbl.create 42
let read_back_memo_table = Hashtbl.create 42
let reduce global_env t =
let fuel = ref Params.fuel in
let local_env = Ident.Map.empty in
let env = {
fuel;
global_env;
reduce_memo_table = reduce_memo_table;
read_back_memo_table = read_back_memo_table;
local_env;
} in
reduce_ env t |> read_back env
let rec is_stuck_on_comp_unit (nf : nf) =
match nf.desc with
| NVar _ ->
false
| NApp (nf, _) | NProj (nf, _) -> is_stuck_on_comp_unit nf
| NStruct _ | NAbs _ -> false
| NAlias _ -> false
| NComp_unit _ -> true
| NError _ -> false
| NLeaf | NPack _ -> false
let rec reduce_aliases_for_uid env (nf : nf) =
match nf with
| { uid = Some uid; desc = NAlias dnf; approximated = false; _ } ->
let result = reduce_aliases_for_uid env (force env dnf) in
Resolved_alias (uid, result)
| { uid = Some uid; approximated = false; _ } -> Resolved uid
| { uid; approximated = true } -> Approximated uid
| { uid = None; approximated = false; _ } -> Missing_uid (read_back env nf)
let reduce_for_uid global_env t =
let fuel = ref Params.fuel in
let local_env = Ident.Map.empty in
let env = {
fuel;
global_env;
reduce_memo_table = reduce_memo_table;
read_back_memo_table = read_back_memo_table;
local_env;
} in
let nf = reduce_ env t in
if is_stuck_on_comp_unit nf then
Unresolved (read_back env nf)
else
reduce_aliases_for_uid env nf
end
module Local_reduce =
Make(struct
let fuel = 10
let read_unit_shape ~unit_name:_ = None
end)
let local_reduce = Local_reduce.reduce
module Ident_and_uid = Identifiable.Make (Identifiable.Pair (Ident) (Uid))
let uid_memo : Uid.t Ident_and_uid.Tbl.t ref =
Local_store.s_table Ident_and_uid.Tbl.create 16
let make_definition_uid ~current_unit parent_id decl_uid =
match Ident_and_uid.Tbl.find_opt !uid_memo (parent_id, decl_uid) with
| Some uid -> uid
| None ->
let uid = Uid.mk_local_opaque ~current_unit in
Uid.Deps.record_declaration_dependency
(Definition_to_declaration, uid, decl_uid);
Ident_and_uid.Tbl.add !uid_memo (parent_id, decl_uid) uid;
uid
let rec stuck_on_var_or_pack (t : t) =
match t.desc with
| Var id | Pack id -> Some id
| App (t, _) | Proj (t, _) -> stuck_on_var_or_pack t
| Struct _ | Abs _ -> None
| Alias _ -> None
| Comp_unit _ -> None
| Error _ -> None
| Leaf -> None
let local_reduce_for_uid env ~namespace path shape =
let rec aux = function
| Resolved_alias (uid, result) -> Resolved_alias (uid, aux result)
| Missing_uid t ->
begin match stuck_on_var_or_pack t with
| None -> Internal_error_missing_uid
| Some parent_id ->
begin match Env.find_uid namespace path env with
| Some uid ->
let current_unit = Env.get_current_unit () in
let uid = make_definition_uid ~current_unit parent_id uid in
Resolved_local_use uid
| None -> Internal_error_missing_uid
end
end
| otherwise -> otherwise
in
aux (Local_reduce.reduce_for_uid env shape )