Source file rewrite.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
(** Implementation of the rewrite tactic. *)
open Lplib
open Timed
open Common open Pos open Error open Debug
open Core open Term open Print
open Proof
(** Logging function for the rewrite tactic. *)
let log_rewr = Logger.make 'r' "rewr" "the rewrite tactic"
let log_rewr = log_rewr.pp
(** Equality configuration. *)
type eq_config =
{ symb_P : sym (** Encoding of propositions. *)
; symb_T : sym (** Encoding of types. *)
; symb_eq : sym (** Equality proposition. *)
; symb_eqind : sym (** Induction principle on equality. *)
; symb_refl : sym (** Reflexivity of equality. *) }
(** [get_eq_config ss pos] returns the current configuration for
equality, used by tactics such as “rewrite” or “reflexivity”. *)
let get_eq_config : Sig_state.t -> popt -> eq_config = fun ss pos ->
let builtin = Builtin.get ss pos in
{ symb_P = builtin "P"
; symb_T = builtin "T"
; symb_eq = builtin "eq"
; symb_eqind = builtin "eqind"
; symb_refl = builtin "refl" }
let _ =
let check_codomain_is_Type _ss pos sym =
let valid =
match Eval.whnf [] !(sym.sym_type) with
| Prod(_, b) -> Eval.eq_modulo [] (snd (Bindlib.unbind b)) mk_Type
| _ -> false
in
if not valid then
fatal pos "The type of [%s] is not of the form [_ → TYPE]." sym.sym_name
in
Builtin.register "T" check_codomain_is_Type;
Builtin.register "P" check_codomain_is_Type;
let get_domain_of_type s =
match Eval.whnf [] !(s.sym_type) with
| Prod(a,_) -> a
| _ -> assert false
in
let register_builtin =
Builtin.register_expected_type (Eval.eq_modulo []) term
in
let expected_eq_type pos map =
let symb_T = Builtin.get pos map "T" in
let symb_P = Builtin.get pos map "P" in
let term_U = lift (get_domain_of_type symb_T) in
let term_Prop = lift (get_domain_of_type symb_P) in
let a = new_tvar "a" in
let term_T_a = _Appl (_Symb symb_T) (_Vari a) in
let impls = _Impl term_T_a (_Impl term_T_a term_Prop) in
Bindlib.unbox (_Prod term_U (Bindlib.bind_var a impls))
in
register_builtin "eq" expected_eq_type;
let expected_refl_type pos map =
let symb_T = Builtin.get pos map "T" in
let symb_P = Builtin.get pos map "P" in
let symb_eq = Builtin.get pos map "eq" in
let term_U = lift (get_domain_of_type symb_T) in
let a = new_tvar "a" in
let x = new_tvar "x" in
let appl_eq = _Appl (_Symb symb_eq) (_Vari a) in
let appl_eq = _Appl (_Appl appl_eq (_Vari x)) (_Vari x) in
let appl = _Appl (_Symb symb_P) appl_eq in
let term_T_a = _Appl (_Symb symb_T) (_Vari a) in
let prod = _Prod term_T_a (Bindlib.bind_var x appl) in
Bindlib.unbox (_Prod term_U (Bindlib.bind_var a prod))
in
register_builtin "refl" expected_refl_type;
let expected_eqind_type pos map =
let symb_T = Builtin.get pos map "T" in
let term_T = _Symb symb_T in
let symb_P = Builtin.get pos map "P" in
let term_P = _Symb symb_P in
let symb_eq = Builtin.get pos map "eq" in
let term_eq = _Symb symb_eq in
let term_U = lift (get_domain_of_type symb_T) in
let term_Prop = lift (get_domain_of_type symb_P) in
let a = new_tvar "a" in
let x = new_tvar "x" in
let y = new_tvar "y" in
let p = new_tvar "p" in
let term_T_a = _Appl term_T (_Vari a) in
let term_P_p_x = _Appl term_P (_Appl (_Vari p) (_Vari x)) in
let term_P_p_y = _Appl term_P (_Appl (_Vari p) (_Vari y)) in
let impl = _Impl term_P_p_y term_P_p_x in
let prod = _Prod (_Impl term_T_a term_Prop) (Bindlib.bind_var p impl) in
let eq = _Appl (_Appl (_Appl term_eq (_Vari a)) (_Vari x)) (_Vari y) in
let impl = _Impl (_Appl term_P eq) prod in
let prod = _Prod term_T_a (Bindlib.bind_var y impl) in
let prod = _Prod term_T_a (Bindlib.bind_var x prod) in
Bindlib.unbox (_Prod term_U (Bindlib.bind_var a prod))
in
register_builtin "eqind" expected_eqind_type
(** [get_eq_data pos cfg a] returns [((a,l,r),[v1;..;vn])] if [a ≡ Π v1:A1,
.., Π vn:An, P (eq a l r)] and fails otherwise. *)
let get_eq_data :
eq_config -> popt -> term -> (term * term * term) * tvar list = fun cfg ->
let exception Not_eq of term in
let get_eq_args u =
if Logger.log_enabled () then log_rewr "get_eq_args %a" term u;
match get_args u with
| eq, [a;l;r] when is_symb cfg.symb_eq eq -> a, l, r
| _ -> raise (Not_eq u)
in
let exception Not_P of term in
let return vs r = r, List.rev vs in
let rec get_eq vs t notin_whnf =
if Logger.log_enabled () then log_rewr "get_eq %a" term t;
match get_args t with
| Prod(_,t), _ -> let v,t = Bindlib.unbind t in get_eq (v::vs) t true
| p, [u] when is_symb cfg.symb_P p ->
begin
let u = Eval.whnf ~tags:[`NoRw;`NoExpand] [] u in
try return vs (get_eq_args u)
with Not_eq _ ->
(try return vs (get_eq_args (Eval.whnf [] u))
with Not_eq _ when notin_whnf -> get_eq vs (Eval.whnf [] t) false)
end
| _ ->
if notin_whnf then get_eq vs (Eval.whnf [] t) false
else raise (Not_P t)
in
fun pos t ->
if Logger.log_enabled () then log_rewr "get_eq_data %a" term t;
try get_eq [] t true with
| Not_P u ->
fatal pos "Expected %a _ but found %a." sym cfg.symb_P term u
| Not_eq u ->
fatal pos "Expected %a _ _ but found %a." sym cfg.symb_eq term u
(** Type of a term with the free variables that need to be substituted. It is
usually used to store the LHS of a proof of equality, together with the
variables that were quantified over. *)
type to_subst = tvar array * term
(** [matches p t] instantiates the [TRef]'s of [p] so that [p] gets equal
to [t] and returns [true] if all [TRef]'s of [p] could be instantiated, and
[false] otherwise. *)
let matches : term -> term -> bool =
let exception Not_equal in
let rec eq l =
match l with
| [] -> ()
| (p,t)::l ->
if Term.cmp p t = 0 then eq l else begin
let hp, ps, k = get_args_len p and ht, ts, n = get_args_len t in
if Logger.log_enabled() then
log_rewr "matches %a %a ≡ %a %a"
term hp (D.list term) ps term ht (D.list term) ts;
match hp with
| Wild -> assert false
| Patt _ -> assert false
| TEnv _ -> assert false
| Plac _ -> assert false
| Appl _ -> assert false
| Type -> assert false
| Kind -> assert false
| TRef r ->
if k > n then raise Not_equal;
let ts1, ts2 = List.cut ts (n-k) in
let u = add_args ht ts1 in
if Logger.log_enabled() then
log_rewr (Color.red "<TRef> ≔ %a") term u;
r := Some u;
eq (List.fold_left2 (fun l pi ti -> (pi,ti)::l) l ps ts2)
| Meta _
| Prod _
| Abst _
| LLet _
| Symb _
| Vari _ ->
if k <> n then raise Not_equal;
let add_args l =
List.fold_left2 (fun l pi ti -> (pi,ti)::l) l ps ts in
match hp, ht with
| Vari x, Vari y when Bindlib.eq_vars x y -> eq (add_args l)
| Symb f, Symb g when f == g -> eq (add_args l)
| _ ->
if Logger.log_enabled() then log_rewr "distinct heads";
raise Not_equal
end
in
fun p t ->
try
eq [(p,t)];
if Logger.log_enabled() then log_rewr "matches OK";
true
with Not_equal ->
if Logger.log_enabled() then log_rewr "matches KO";
false
(** [matching_subs (xs,p) t] attempts to match the pattern [p] containing the
variables [xs]) with the term [t]. If successful, it returns [Some ts]
where [ts] is an array of terms such that substituting [xs] by the
corresponding elements of [ts] in [p] yields [t]. *)
let matching_subs : to_subst -> term -> term array option = fun (xs,p) t ->
let ts = Array.map (fun _ -> mk_TRef(ref None)) xs in
let p = Bindlib.msubst (binds xs lift_not_canonical p) ts in
if matches p t then Some(Array.map unfold ts) else None
(** [find_subst (xs,p) t] tries to find the first instance of a subterm of [t]
matching [p]. If successful, the function returns the array of terms by
which [xs] must substituted. *)
let find_subst : to_subst -> term -> term array option = fun xsp t ->
let time = Time.save () in
let rec find_subst : term -> term array option = fun t ->
if Logger.log_enabled() then
log_rewr "find_subst %a ≡ %a" term (snd xsp) term t;
match matching_subs xsp t with
| None ->
begin
Time.restore time;
match unfold t with
| Appl(t,u) ->
begin
match find_subst t with
| None -> Time.restore time; find_subst u
| sub -> sub
end
| _ -> None
end
| sub -> sub
in find_subst t
(** [find_subterm_matching p t] tries to find a subterm of [t] that matches
[p] by instantiating the [TRef]'s of [p]. In case of success, the function
returns [true]. *)
let find_subterm_matching : term -> term -> bool = fun p t ->
let time = Time.save () in
let rec find_subterm : term -> bool = fun t ->
if matches p t then true else
begin
Time.restore time;
match unfold t with
| Appl(t,u) ->
begin
match find_subterm t with
| false -> Time.restore time; find_subterm u
| true -> true
end
| _ -> false
end
in find_subterm t
(** [bind_pattern p t] replaces in the term [t] every occurence of the pattern
[p] by a fresh variable, and returns the binder on this variable. *)
let bind_pattern : term -> term -> tbinder = fun p t ->
let z = new_tvar "z" in
let rec replace : term -> tbox = fun t ->
if matches p t then _Vari z else
match unfold t with
| Appl(t,u) -> _Appl (replace t) (replace u)
| Prod(a,b) ->
let x,b = Bindlib.unbind b in
_Prod (replace a) (Bindlib.bind_var x (replace b))
| Abst(a,b) ->
let x,b = Bindlib.unbind b in
_Abst (replace a) (Bindlib.bind_var x (replace b))
| LLet(typ, def, body) ->
let x, body = Bindlib.unbind body in
_LLet (replace typ) (replace def) (Bindlib.bind_var x (replace body))
| Meta(m,ts) -> _Meta m (Array.map replace ts)
| TEnv _ -> assert false
| Wild -> assert false
| TRef _ -> assert false
| Patt _ -> assert false
| Plac _ -> assert false
| _ -> lift t
in
Bindlib.(unbox (bind_var z (replace t)))
(** [swap cfg a r l t] returns a term of type [P (eq a l r)] from a term [t]
of type [P (eq a r l)]. *)
let swap : eq_config -> term -> term -> term -> term -> term =
fun cfg a r l t ->
let pred =
let x = new_tvar "x" in
let pred = add_args (mk_Symb cfg.symb_eq) [a; l; mk_Vari x] in
mk_Abst(mk_Appl(mk_Symb cfg.symb_T, a), bind x lift pred)
in
let refl_a_l = add_args (mk_Symb cfg.symb_refl) [a; l] in
add_args (mk_Symb cfg.symb_eqind) [a; r; l; t; pred; refl_a_l]
(** [replace_wild_by_tref t] substitutes every wildcard of [t] by a fresh
[TRef]. *)
let rec replace_wild_by_tref : term -> term = fun t ->
match unfold t with
| Wild -> mk_TRef(ref None)
| Appl(t,u) ->
mk_Appl_not_canonical(replace_wild_by_tref t, replace_wild_by_tref u)
| _ -> t
(** [rewrite ss p pos gt l2r pat t] generates a term for the refine tactic
representing the application of the rewrite tactic to the goal type
[gt]. Every occurrence of the first instance of the left-hand side is
replaced by the right-hand side of the obtained proof (or the reverse if
l2r is false). [pat] is an optional SSReflect pattern. [t] is the
equational lemma that is appied. It handles the full set of SSReflect
patterns. *)
let rewrite : Sig_state.t -> problem -> popt -> goal_typ -> bool ->
(term, tbinder) Parsing.Syntax.rw_patt option -> term -> term =
fun ss p pos {goal_hyps=g_env; goal_type=g_type; _} l2r pat t ->
let cfg = get_eq_config ss pos in
let g_ctxt = Env.to_ctxt g_env in
let (t, t_type) = Query.infer pos p g_ctxt t in
let (a, l, r), vars = get_eq_data cfg pos t_type in
let vars = Array.of_list vars in
let t = Array.fold_left (fun t x -> mk_Appl(t, mk_Vari x)) t vars in
let (t, l, r) = if l2r then (t, l, r) else (swap cfg a l r t, r, l) in
let bound =
let triple =
Bindlib.box_triple (lift t) (lift_not_canonical l) (lift r) in
Bindlib.unbox (Bindlib.bind_mvar vars triple)
in
let g_term =
match get_args g_type with
| t, [u] when is_symb cfg.symb_P t -> u
| _ -> fatal pos "Goal not of the form (%a _)." sym cfg.symb_P
in
let (pred_bind, new_term, t, l, r) =
match pat with
| None ->
let sigma =
match find_subst (vars, l) g_term with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of [%a] matches [%a]."
term g_term term l
in
let (t, l, r) = Bindlib.msubst bound sigma in
let pred_bind = bind_pattern l g_term in
(pred_bind, Bindlib.subst pred_bind r, t, l, r)
| Some(Rw_Term(p)) ->
let match_p =
let p_refs = replace_wild_by_tref p in
if not (find_subterm_matching p_refs g_term) then
fatal pos "No subterm of [%a] matches [%a]."
term g_term term p;
p_refs
in
let sigma =
match matching_subs (vars,l) match_p with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of [%a] matches [%a]."
term match_p term l
in
let (t, l, r) = Bindlib.msubst bound sigma in
let pred_bind = bind_pattern l g_term in
(pred_bind, Bindlib.subst pred_bind r, t, l, r)
| Some(Rw_InTerm(p)) ->
let match_p =
let p_refs = replace_wild_by_tref p in
if not (find_subterm_matching p_refs g_term) then
fatal pos "No subterm of [%a] matches [%a]."
term g_term term p;
p_refs
in
let sigma =
match find_subst (vars,l) match_p with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of the pattern [%a] matches [%a]."
term match_p term l
in
let (t, l, r) = Bindlib.msubst bound sigma in
let p_x = bind_pattern l match_p in
let p_r = Bindlib.subst p_x r in
let pred_bind = bind_pattern match_p g_term in
let new_term = Bindlib.subst pred_bind p_r in
let (x, p_x) = Bindlib.unbind p_x in
let pred = Bindlib.subst pred_bind p_x in
let pred_bind = bind x lift pred in
(pred_bind, new_term, t, l, r)
| Some(Rw_IdInTerm(p)) ->
let (id,p) = Bindlib.unbind p in
let p_refs = replace_wild_by_tref p in
let id_val =
match find_subst ([|id|],p_refs) g_term with
| Some(id_val) -> id_val.(0)
| None ->
fatal pos "The pattern [%a] does not match [%a]."
term p term l
in
let pat = bind id lift_not_canonical p_refs in
let pat_l = Bindlib.subst pat id_val in
let sigma =
match matching_subs (vars,l) id_val with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of [%a], [%a], in [%a] does not match [%a]."
var id term id_val term p term l
in
let (t,l,r) = Bindlib.msubst bound sigma in
let pat_r = Bindlib.subst pat r in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = Bindlib.subst pred_bind_l pat_r in
let (x, l_x) = Bindlib.unbind pat in
let pred_bind = bind x lift (Bindlib.subst pred_bind_l l_x) in
(pred_bind, new_term, t, l, r)
| Some(Rw_TermInIdInTerm(s,p)) ->
let (id,p) = Bindlib.unbind p in
let p_refs = replace_wild_by_tref p in
let id_val =
match find_subst ([|id|],p_refs) g_term with
| Some(id_val) -> id_val
| None ->
fatal pos "The pattern [%a] does not match [%a]."
term p term l
in
let id_val = id_val.(0) in
let pat = bind id lift_not_canonical p_refs in
let pat_l = Bindlib.subst pat id_val in
let s_refs = replace_wild_by_tref s in
if not (find_subterm_matching s_refs id_val) then
fatal pos "The value of [%a], [%a], in [%a] does not match [%a]."
var id term id_val term p term s;
let s = s_refs in
let sigma =
match matching_subs (vars,l) s with
| Some(sigma) -> sigma
| None ->
fatal pos "The term [%a] does not match the LHS [%a]"
term s term l
in
let (t,l,r) = Bindlib.msubst bound sigma in
let id_bind = bind_pattern l id_val in
let new_id = Bindlib.subst id_bind r in
let (x, id_x) = Bindlib.unbind id_bind in
let pat_r = Bindlib.subst pat new_id in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = Bindlib.subst pred_bind_l pat_r in
let l_x = Bindlib.subst pat id_x in
let pred = Bindlib.subst pred_bind_l l_x in
(bind x lift pred, new_term, t, l, r)
| Some(Rw_TermAsIdInTerm(s,p)) ->
let (id,pat) = Bindlib.unbind p in
let s = replace_wild_by_tref s in
let p_s = Bindlib.subst p s in
let p_refs = replace_wild_by_tref p_s in
if not (find_subterm_matching p_refs g_term) then
fatal pos "No subterm of [%a] matches the pattern [%a]"
term g_term term p_s;
let p = p_refs in
let pat_refs = replace_wild_by_tref pat in
let sub =
match matching_subs ([|id|], pat_refs) p with
| Some(sub) -> sub
| None -> assert false
in
let id_val = sub.(0) in
let sigma =
match matching_subs (vars, l) id_val with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of X, [%a], does not match the LHS, [%a]"
term id_val term l
in
let (t,l,r) = Bindlib.msubst bound sigma in
let p_x = bind_pattern l p in
let p_r = Bindlib.subst p_x r in
let pred_bind = bind_pattern p g_term in
let new_term = Bindlib.subst pred_bind p_r in
let (x, p_x) = Bindlib.unbind p_x in
let pred_bind = bind x lift (Bindlib.subst pred_bind p_x) in
(pred_bind, new_term, t, l, r)
| Some(Rw_InIdInTerm(q)) ->
let (id,q) = Bindlib.unbind q in
let q_refs = replace_wild_by_tref q in
let id_val =
match find_subst ([|id|],q_refs) g_term with
| Some(id_val) -> id_val
| None ->
fatal pos "The pattern [%a] does not match [%a]."
term q term g_term
in
let id_val = id_val.(0) in
let pat = bind id lift_not_canonical q_refs in
let pat_l = Bindlib.subst pat id_val in
let sigma =
match find_subst (vars,l) id_val with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of [%a], [%a], in [%a] does not match [%a]."
var id term id_val term q term l
in
let (t,l,r) = Bindlib.msubst bound sigma in
let id_bind = bind_pattern l id_val in
let id_val = Bindlib.subst id_bind r in
let (x, id_x) = Bindlib.unbind id_bind in
let r_val = Bindlib.subst pat id_val in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = Bindlib.subst pred_bind_l r_val in
let l_x = Bindlib.subst pat id_x in
let pred_bind = bind x lift (Bindlib.subst pred_bind_l l_x) in
(pred_bind, new_term, t, l, r)
in
let pred = mk_Abst(mk_Appl(mk_Symb cfg.symb_T, a), pred_bind) in
let goal_type = mk_Appl(mk_Symb cfg.symb_P, new_term) in
let goal_term = LibMeta.make p g_ctxt goal_type in
let eqind = mk_Symb cfg.symb_eqind in
let result = add_args eqind [a; l; r; t; pred; goal_term] in
if Logger.log_enabled () then
begin
log_rewr "Rewriting with:";
log_rewr " goal = [%a]" term g_type;
log_rewr " equality proof = [%a]" term t;
log_rewr " equality LHS = [%a]" term l;
log_rewr " equality RHS = [%a]" term r;
log_rewr " pred = [%a]" term pred;
log_rewr " new goal = [%a]" term goal_type;
log_rewr " produced term = [%a]" term result;
end;
result