Source file eval.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
(** Evaluation and conversion. *)
open! Lplib
open Lplib.Extra
open Timed
open Common
open Error
open Debug
open Term
open Print
(** The head-structure of a term t is:
- λx:_,h if t=λx:a,u and h is the head-structure of u
- Π if t=Πx:a,u
- h _ if t=uv and h is the head-structure of u
- ? if t=?M[t1,..,tn] (and ?M is not instantiated)
- t itself otherwise (TYPE, KIND, x, f)
A term t is in head-normal form (hnf) if its head-structure is invariant by
reduction.
A term t is in weak head-normal form (whnf) if it is an abstration or if it
is in hnf. In particular, a term in head-normal form is in weak head-normal
form.
A term t is in strong normal form (snf) if it cannot be reduced further.
*)
(** Logging function for evaluation. *)
let log_eval = Logger.make 'e' "eval" "evaluation"
let log_eval = log_eval.pp
(** Logging function for equality modulo rewriting. *)
let log_conv = Logger.make 'c' "conv" "conversion"
let log_conv = log_conv.pp
(** Convert modulo eta. *)
let eta_equality : bool ref = Console.register_flag "eta_equality" false
(** Counter used to preserve physical equality in {!val:whnf}. *)
let steps : int Stdlib.ref = Stdlib.ref 0
(** [hnf whnf t] computes a hnf of [t] using [whnf]. *)
let hnf : (term -> term) -> (term -> term) = fun whnf ->
let rec hnf t =
match whnf t with
| Abst(a,t) ->
let x, t = Bindlib.unbind t in
mk_Abst(a, Bindlib.unbox (Bindlib.bind_var x (lift (hnf t))))
| t -> t
in hnf
(** [snf whnf t] computes a snf of [t] using [whnf]. *)
let snf : (term -> term) -> (term -> term) = fun whnf ->
let rec snf t =
if Logger.log_enabled () then log_eval "snf %a" pp_term t;
let h = whnf t in
if Logger.log_enabled () then log_eval "whnf %a = %a" pp_term t pp_term h;
match h with
| Vari _
| Type
| Kind
| Symb _ -> h
| LLet(_,t,b) -> snf (Bindlib.subst b t)
| Prod(a,b) ->
let x, b = Bindlib.unbind b in
mk_Prod(snf a, Bindlib.unbox (Bindlib.bind_var x (lift (snf b))))
| Abst(a,b) ->
let x, b = Bindlib.unbind b in
mk_Abst(snf a, Bindlib.unbox (Bindlib.bind_var x (lift (snf b))))
| Appl(t,u) -> mk_Appl(snf t, snf u)
| Meta(m,ts) -> mk_Meta(m, Array.map snf ts)
| Plac _ -> assert false
| Patt(_,_,_) -> assert false
| TEnv(_,_) -> assert false
| Wild -> assert false
| TRef(_) -> assert false
in snf
(** Configuration of the reduction engine. *)
type config =
{ context : ctxt (** Context of the reduction used for generating metas. *)
; defmap : term VarMap.t (** Variable definitions. *)
; rewrite : bool (** Use user-defined rewrite rules. *)
; problem : problem (** Generated metavariables. *) }
let cfg_of_ctx : ctxt -> bool -> config = fun context rewrite ->
{context; defmap = Ctxt.to_map context; rewrite; problem = new_problem()}
let unfold_cfg : config -> term -> term = fun c a ->
let a = unfold a in
match a with
| Vari x ->
begin match VarMap.find_opt x c.defmap with
| None -> a
| Some v -> unfold v
end
| _ -> a
(** [eq_modulo whnf a b] tests the convertibility of [a] and [b] using
[whnf]. *)
let eq_modulo : (config -> term -> term) -> (config -> term -> term -> bool) =
fun whnf ->
let rec eq : config -> (term * term) list -> unit = fun c l ->
match l with
| [] -> ()
| (a,b)::l ->
let a = unfold_cfg c a and b = unfold_cfg c b in
if a == b then eq c l else
match a, b with
| LLet(_,t,u), _ ->
let x,u = Bindlib.unbind u in
eq {c with defmap = VarMap.add x t c.defmap} ((u,b)::l)
| _, LLet(_,t,u) ->
let x,u = Bindlib.unbind u in
eq {c with defmap = VarMap.add x t c.defmap} ((a,u)::l)
| Patt _, _ | _, Patt _
| TEnv _, _| _, TEnv _ -> assert false
| Kind, Kind
| Type, Type -> eq c l
| Vari x, Vari y -> if Bindlib.eq_vars x y then eq c l else raise Exit
| Symb f, Symb g when f == g -> eq c l
| Prod(a1,b1), Prod(a2,b2)
| Abst(a1,b1), Abst(a2,b2) ->
let _,b1,b2 = Bindlib.unbind2 b1 b2 in eq c ((a1,a2)::(b1,b2)::l)
| Abst _, (Type|Kind|Prod _)
| (Type|Kind|Prod _), Abst _ -> raise Exit
| (Abst(_ ,b), t | t, Abst(_ ,b)) when !eta_equality ->
let x,b = Bindlib.unbind b in eq c ((b, mk_Appl(t, mk_Vari x))::l)
| Meta(m1,a1), Meta(m2,a2) when m1 == m2 ->
eq c (if a1 == a2 then l else List.add_array2 a1 a2 l)
| Kind, _ | _, Kind
| Type, _ | _, Type
-> raise Exit
| ((Symb f, (Vari _|Meta _|Prod _|Abst _))
| ((Vari _|Meta _|Prod _|Abst _), Symb f)) when is_constant f ->
raise Exit
| _ ->
let a = whnf c a and b = whnf c b in
match a, b with
| Patt _, _ | _, Patt _
| TEnv _, _| _, TEnv _ -> assert false
| Kind, Kind
| Type, Type -> eq c l
| Vari x, Vari y when Bindlib.eq_vars x y -> eq c l
| Symb f, Symb g when f == g -> eq c l
| Prod(a1,b1), Prod(a2,b2)
| Abst(a1,b1), Abst(a2,b2) ->
let _,b1,b2 = Bindlib.unbind2 b1 b2 in eq c ((a1,a2)::(b1,b2)::l)
| (Abst(_ ,b), t | t, Abst(_ ,b)) when !eta_equality ->
let x,b = Bindlib.unbind b in eq c ((b, mk_Appl(t, mk_Vari x))::l)
| Meta(m1,a1), Meta(m2,a2) when m1 == m2 ->
eq c (if a1 == a2 then l else List.add_array2 a1 a2 l)
| Appl(t1,u1), Appl(t2,u2) -> eq c ((u1,u2)::(t1,t2)::l)
| _ -> raise Exit
in
fun c a b ->
if Logger.log_enabled () then log_conv "%a ≡ %a" pp_term a pp_term b;
try eq c [(a,b)]; true
with Exit -> if Logger.log_enabled () then log_conv "failed"; false
(** Abstract machine stack. *)
type stack = term list
(** [to_tref t] transforms {!constructor:Appl} into
{!constructor:TRef}. *)
let to_tref : term -> term = fun t ->
match t with
| Appl _ -> mk_TRef(ref (Some t))
| Symb s when s.sym_prop <> Const -> mk_TRef(ref (Some t))
| t -> t
(** [whnf c t] computes a whnf of the term [t] wrt configuration [c]. *)
let rec whnf : config -> term -> term = fun c t ->
let n = Stdlib.(!steps) in
let u, stk = whnf_stk c t [] in
let r = if Stdlib.(!steps) <> n then add_args u stk else unfold t in
r
(** [whnf_stk c t stk] computes a whnf of [add_args t stk] wrt
configuration [c]. *)
and whnf_stk : config -> term -> stack -> term * stack = fun c t stk ->
let t = unfold t in
match t, stk with
| Appl(f,u), stk -> whnf_stk c f (to_tref u::stk)
| Abst(_,f), u::stk ->
Stdlib.incr steps; whnf_stk c (Bindlib.subst f u) stk
| LLet(_,t,u), stk ->
Stdlib.incr steps; whnf_stk c (Bindlib.subst u t) stk
| (Symb s as h, stk) as r when c.rewrite ->
begin match !(s.sym_def) with
| Some t ->
if s.sym_opaq then r else (Stdlib.incr steps; whnf_stk c t stk)
| None ->
let stk =
if is_modulo s then
let n = Stdlib.(!steps) in
let stk' = List.map (whnf c) stk in
if Stdlib.(!steps) = n then
stk
else
snd (get_args (add_args h stk'))
else stk
in
match tree_walk c !(s.sym_dtree) stk with
| None -> h, stk
| Some (t', stk') ->
if Logger.log_enabled () then
log_eval "tree_walk %a%a %a = %a %a" pp_ctxt c.context
pp_term t (D.list pp_term) stk pp_term t' (D.list pp_term) stk';
Stdlib.incr steps; whnf_stk c t' stk'
end
| (Vari x, stk) as r ->
begin match VarMap.find_opt x c.defmap with
| Some v -> Stdlib.incr steps; whnf_stk c v stk
| None -> r
end
| r -> r
(** {b NOTE} that in {!val:tree_walk} matching with trees involves two
collections of terms.
1. The argument stack [stk] of type {!type:stack} which contains the terms
that are matched against the decision tree.
2. An array [vars] containing subterms of the argument stack [stk] that
are filtered by a pattern variable. These terms may be used for
non-linearity or free-variable checks, or may be bound in the RHS.
The [bound] array is similar to the [vars] array except that it is used to
save terms with free variables. *)
(** {b NOTE} in the {!val:tree_walk} function, bound variables involve three
elements:
1. a {!constructor:Term.term.Abst} which introduces the bound variable in
the term;
2. a {!constructor:Term.term.Vari} which is the bound variable previously
introduced;
3. a {!constructor:Tree_type.TC.t.Vari} which is a simplified
representation of a variable for trees. *)
(** [tree_walk dt m stk] tries to apply a rewrite rule by matching the stack
[stk] against the decision tree [dt] using variable definitions in [m]. The
resulting state of the abstract machine is returned in case of
success. Even if matching fails, the stack [stk] may be imperatively
updated since a reduction step taken in elements of the stack is preserved
(this is done using {!constructor:Term.term.TRef}). Fresh metavariables
generated by unification rules with extra pattern variables are added to
[!the_problem]. *)
and tree_walk : config -> dtree -> stack -> (term * stack) option =
fun c tree stk ->
let (lazy capacity, lazy tree) = tree in
let vars = Array.make capacity mk_Kind in
let bound = Array.make capacity TE_None in
let rec walk tree stk cursor vars_id id_vars =
let open Tree_type in
match tree with
| Fail -> None
| Leaf(rhs_subst, (act, xvars)) ->
let env_len = Bindlib.mbinder_arity act in
assert (List.length rhs_subst = env_len - xvars);
let env = Array.make env_len TE_None in
let f (pos, (slot, xs)) =
match bound.(pos) with
| TE_Vari(_) -> assert false
| TE_Some(_) -> env.(slot) <- bound.(pos)
| TE_None ->
if Array.length xs = 0 then
let t = unfold vars.(pos) in
let b = Bindlib.raw_mbinder [||] [||] 0 of_tvar (fun _ -> t)
in env.(slot) <- TE_Some(b)
else
let b = lift vars.(pos) in
let xs = Array.map (fun e -> IntMap.find e id_vars) xs in
env.(slot) <- TE_Some(Bindlib.unbox (Bindlib.bind_mvar xs b))
in
List.iter f rhs_subst;
for i = env_len - xvars to env_len - 1 do
let mt = LibMeta.make c.problem c.context mk_Type in
let t = LibMeta.make c.problem c.context mt in
let b = Bindlib.raw_mbinder [||] [||] 0 of_tvar (fun _ -> t) in
env.(i) <- TE_Some(b)
done;
Some (Bindlib.msubst act env, stk)
| Cond({ok; cond; fail}) ->
let next =
match cond with
| CondNL(i, j) ->
if eq_modulo whnf c vars.(i) vars.(j) then ok else fail
| CondFV(i,xs) ->
let allowed =
let fn id =
try IntMap.find id id_vars with Not_found -> assert false
in
Array.map fn xs
in
let forbidden =
IntMap.filter (fun id _ -> not (Array.mem id xs)) id_vars
in
let no_forbidden b =
not (IntMap.exists (fun _ x -> Bindlib.occur x b) forbidden)
in
let b = Bindlib.bind_mvar allowed (lift vars.(i)) in
if no_forbidden b
then (bound.(i) <- TE_Some(Bindlib.unbox b); ok) else
let b = Bindlib.bind_mvar allowed
(lift (snf (whnf c) vars.(i))) in
if no_forbidden b
then (bound.(i) <- TE_Some(Bindlib.unbox b); ok)
else fail
in
walk next stk cursor vars_id id_vars
| Eos(l, r) ->
let next = if stk = [] then l else r in
walk next stk cursor vars_id id_vars
| Node({swap; children; store; abstraction; default; product}) ->
match List.destruct stk swap with
| exception Not_found -> None
| (left, examined, right) ->
if TCMap.is_empty children && abstraction = None && product = None
then
let fn t =
let cursor =
if store then (vars.(cursor) <- examined; cursor + 1)
else cursor
in
let stk = List.reconstruct left [] right in
walk t stk cursor vars_id id_vars
in
Option.bind fn default
else
let s = Stdlib.(!steps) in
let (t, args) = whnf_stk c examined [] in
let args = if store then List.map to_tref args else args in
if Stdlib.(!steps) <> s then
begin
match examined with
| TRef(v) -> v := Some(add_args t args)
| _ -> ()
end;
let cursor =
if store then (vars.(cursor) <- add_args t args; cursor + 1)
else cursor
in
let default () =
let fn d =
let stk = List.reconstruct left [] right in
walk d stk cursor vars_id id_vars
in
Option.bind fn default
in
let walk_binder a b id tr =
let (bound, body) = Bindlib.unbind b in
let vars_id = VarMap.add bound id vars_id in
let id_vars = IntMap.add id bound id_vars in
let stk = List.reconstruct left (a::body::args) right in
walk tr stk cursor vars_id id_vars
in
match t with
| Symb(s) ->
let cons = TC.Symb(s.sym_path, s.sym_name, List.length args) in
begin
try
let matched = TCMap.find cons children in
let stk = List.reconstruct left args right in
walk matched stk cursor vars_id id_vars
with Not_found -> default ()
end
| Vari(x) ->
begin
try
let id = VarMap.find x vars_id in
let matched = TCMap.find (TC.Vari(id)) children in
let stk = List.reconstruct left args right in
walk matched stk cursor vars_id id_vars
with Not_found -> default ()
end
| Abst(a, b) ->
begin
match abstraction with
| None -> default ()
| Some(id,tr) -> walk_binder a b id tr
end
| Prod(a, b) ->
begin
match product with
| None -> default ()
| Some(id,tr) -> walk_binder a b id tr
end
| Kind
| Type
| Meta(_, _) -> default ()
| Plac _ -> assert false
| TRef(_) -> assert false
| Appl(_) -> assert false
| LLet(_) -> assert false
| Patt(_) -> assert false
| TEnv(_) -> assert false
| Wild -> assert false
in
walk tree stk 0 VarMap.empty IntMap.empty
(** [snf c t] computes a snf of [t], unfolding the variables defined in the
context [c]. *)
let snf : ctxt -> term -> term = fun c t ->
Stdlib.(steps := 0);
let u = snf (whnf (cfg_of_ctx c true)) t in
let r = if Stdlib.(!steps = 0) then unfold t else u in
r
let snf =
let open Stdlib in let r = ref mk_Kind in fun c t ->
Debug.(record_time Rewriting (fun () -> r := snf c t)); !r
(** [hnf c t] computes a hnf of [t], unfolding the variables defined in the
context [c], and using user-defined rewrite rules. *)
let hnf : ctxt -> term -> term = fun c t ->
Stdlib.(steps := 0);
let u = hnf (whnf (cfg_of_ctx c true)) t in
let r = if Stdlib.(!steps = 0) then unfold t else u in
r
let hnf =
let open Stdlib in let r = ref mk_Kind in fun c t ->
Debug.(record_time Rewriting (fun () -> r := hnf c t)); !r
(** [eq_modulo c a b] tests the convertibility of [a] and [b] in context
[c]. WARNING: may have side effects in TRef's introduced by whnf. *)
let eq_modulo : ctxt -> term -> term -> bool = fun c ->
eq_modulo whnf (cfg_of_ctx c true)
let eq_modulo =
let open Stdlib in let r = ref false in fun c t u ->
Debug.(record_time Rewriting (fun () -> r := eq_modulo c t u)); !r
(** [pure_eq_modulo c a b] tests the convertibility of [a] and [b] in context
[c] with no side effects. *)
let pure_eq_modulo : ctxt -> term -> term -> bool = fun c a b ->
Timed.pure_test (fun (c,a,b) -> eq_modulo c a b) (c,a,b)
(** [whnf c t] computes a whnf of [t], unfolding the variables defined in the
context [c], and using user-defined rewrite rules if [~rewrite]. *)
let whnf : ?rewrite:bool -> ctxt -> term -> term = fun ?(rewrite=true) c t ->
Stdlib.(steps := 0);
let u = whnf (cfg_of_ctx c rewrite) t in
let r = if Stdlib.(!steps = 0) then unfold t else u in
r
let whnf =
let open Stdlib in let r = ref mk_Kind in fun ?rewrite c t ->
Debug.(record_time Rewriting (fun () -> r := whnf ?rewrite c t)); !r
(** [simplify t] computes a beta whnf of [t] belonging to the set S such that:
- terms of S are in beta whnf normal format
- if [t] is a product, then both its domain and codomain are in S. *)
let rec simplify : term -> term = fun t ->
match get_args (whnf ~rewrite:false [] t) with
| Prod(a,b), _ ->
let x, b = Bindlib.unbind b in
let b = Bindlib.bind_var x (lift (simplify b)) in
mk_Prod (simplify a, Bindlib.unbox b)
| h, ts -> add_args_map h (whnf ~rewrite:false []) ts
let simplify =
let open Stdlib in let r = ref mk_Kind in fun t ->
Debug.(record_time Rewriting (fun () -> r := simplify t)); !r
(** If [s] is a non-opaque symbol having a definition, [unfold_sym s t]
replaces in [t] all the occurrences of [s] by its definition. *)
let unfold_sym : sym -> term -> term =
let unfold_sym : sym -> (term list -> term) -> term -> term =
fun s unfold_sym_app ->
let rec unfold_sym t =
let h, args = get_args t in
let args = List.map unfold_sym args in
match h with
| Symb s' when s' == s -> unfold_sym_app args
| _ ->
let h =
match h with
| Abst(a,b) -> mk_Abst(unfold_sym a, unfold_sym_binder b)
| Prod(a,b) -> mk_Prod(unfold_sym a, unfold_sym_binder b)
| Meta(m,ts) -> mk_Meta(m, Array.map unfold_sym ts)
| LLet(a,t,u) ->
mk_LLet(unfold_sym a, unfold_sym t, unfold_sym_binder u)
| _ -> h
in add_args h args
and unfold_sym_binder b =
let x, b = Bindlib.unbind b in
Bindlib.unbox (Bindlib.bind_var x (lift (unfold_sym b)))
in unfold_sym
in
fun s ->
if s.sym_opaq then fun t -> t else
match !(s.sym_def) with
| Some d -> unfold_sym s (add_args d)
| None ->
match !(s.sym_rules) with
| [] -> fun t -> t
| _ ->
let c = cfg_of_ctx [] true and dt = !(s.sym_dtree) in
let unfold_sym_app args =
match tree_walk c dt args with
| Some(r,ts) -> add_args r ts
| None -> add_args (mk_Symb s) args
in unfold_sym s unfold_sym_app
(** [tree_walk p tr c stk] tries to apply a rewrite rule by matching the
stack [stk] against the decision tree [tr] in context [c]. The resulting
state of the abstract machine is returned in case of success. Even if
matching fails, the stack [stk] may be imperatively updated since a
reduction step taken in elements of the stack is preserved (this is done
using {!constructor:Term.term.TRef}). Fresh metavariables generated by
unification rules with extra pattern variables are added in [p]. *)
let tree_walk : problem -> ctxt -> dtree -> stack -> (term * stack) option =
fun p c dt ts ->
let c = {context=c; defmap=Ctxt.to_map c; problem=p; rewrite=true} in
tree_walk c dt ts
let tree_walk =
let open Stdlib in let r = ref None in fun p c t s ->
Debug.(record_time Rewriting (fun () -> r := tree_walk p c t s)); !r
(** Dedukti evaluation strategies. *)
type strategy =
| WHNF (** Reduce to weak head-normal form. *)
| HNF (** Reduce to head-normal form. *)
| SNF (** Reduce to strong normal form. *)
| NONE (** Do nothing. *)
type strat =
{ strategy : strategy (** Evaluation strategy. *)
; steps : int option (** Max number of steps if given. *) }
(** [eval cfg c t] evaluates the term [t] in the context [c] according to
evaluation configuration [cfg]. *)
let eval : strat -> ctxt -> term -> term = fun s c t ->
match s.strategy, s.steps with
| _, Some 0
| NONE, _ -> t
| WHNF, None -> whnf c t
| SNF, None -> snf c t
| HNF, None -> hnf c t
| _, Some _ -> wrn None "Number of steps not supported."; t