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
(** Implementation of the rewrite tactic. *)
open Lplib
open Common open Pos open Error open Debug
open Core open Term open Print
open Proof
(** Logging function for the rewrite tactic. *)
let log = Logger.make 'r' "rewr" "the rewrite tactic"
let log = log.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 [] Timed.(!(sym.sym_type)) with
| Prod(_, b) -> Eval.eq_modulo [] (snd (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 [] Timed.(!(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 = get_domain_of_type symb_T in
let term_Prop = get_domain_of_type symb_P in
let a = new_var "a" in
let term_T_a = mk_Appl (mk_Symb symb_T, mk_Vari a) in
let impls = mk_Arro (term_T_a, mk_Arro (term_T_a, term_Prop)) in
mk_Prod (term_U, 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 = get_domain_of_type symb_T in
let a = new_var "a" in
let x = new_var "x" in
let appl_eq = mk_Appl (mk_Symb symb_eq, mk_Vari a) in
let appl_eq = mk_Appl (mk_Appl (appl_eq, mk_Vari x), mk_Vari x) in
let appl = mk_Appl (mk_Symb symb_P, appl_eq) in
let term_T_a = mk_Appl (mk_Symb symb_T, mk_Vari a) in
let prod = mk_Prod (term_T_a, bind_var x appl) in
mk_Prod (term_U, 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 = mk_Symb symb_T in
let symb_P = Builtin.get pos map "P" in
let term_P = mk_Symb symb_P in
let symb_eq = Builtin.get pos map "eq" in
let term_eq = mk_Symb symb_eq in
let term_U = get_domain_of_type symb_T in
let term_Prop = get_domain_of_type symb_P in
let a = new_var "a" in
let x = new_var "x" in
let y = new_var "y" in
let p = new_var "p" in
let term_T_a = mk_Appl (term_T, mk_Vari a) in
let term_P_p_x = mk_Appl (term_P, mk_Appl (mk_Vari p, mk_Vari x)) in
let term_P_p_y = mk_Appl (term_P, mk_Appl (mk_Vari p, mk_Vari y)) in
let impl = mk_Arro (term_P_p_y, term_P_p_x) in
let prod =
mk_Prod (mk_Arro (term_T_a, term_Prop), bind_var p impl) in
let eq = add_args term_eq [mk_Vari a; mk_Vari x; mk_Vari y] in
let impl = mk_Arro (mk_Appl(term_P, eq), prod) in
let prod = mk_Prod (term_T_a, bind_var y impl) in
let prod = mk_Prod (term_T_a, bind_var x prod) in
mk_Prod (term_U, 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) * var list = fun cfg ->
let exception Not_eq of term in
let get_eq_args u =
if Logger.log_enabled () then log "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 "get_eq %a" term t;
match get_args t with
| Prod(_,t), _ -> let v,t = 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 "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 = var 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 add_eqs = List.fold_left2 (fun l pi ti -> (pi,ti)::l) 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, kp = get_args_len p and ht, ts, kt = get_args_len t in
if Logger.log_enabled() then
log "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
| Plac _ -> assert false
| Appl _ -> assert false
| Type -> assert false
| Kind -> assert false
| Bvar _ -> assert false
| TRef r ->
if kp > kt then raise Not_equal;
let ts1, ts2 = List.cut ts (kt-kp) in
let u = add_args ht ts1 in
if Logger.log_enabled() then log (Color.red "<TRef> ≔ %a") term u;
Timed.(r := Some u);
eq (add_eqs l ps ts2)
| Meta _ -> eq l
| Prod _
| Abst _
| LLet _
| Symb _
| Vari _ ->
if kp <> kt then raise Not_equal;
match hp, ht with
| Vari x, Vari y when eq_vars x y -> eq (add_eqs l ps ts)
| Symb f, Symb g when f == g -> eq (add_eqs l ps ts)
| Abst(a,b), Abst(a',b')
| Prod(a,b), Prod(a',b') ->
let _,b,b' = unbind2 b b' in
eq ((a,a')::(b,b')::add_eqs l ps ts)
| LLet(a,c,b), LLet(a',c',b') ->
let _,b,b' = unbind2 b b' in
eq ((a,a')::(c,c')::(b,b')::add_eqs l ps ts)
| _ ->
if Logger.log_enabled() then log "distinct heads";
raise Not_equal
end
in
fun p t ->
let r = try eq [p,t]; true with Not_equal -> false in
if Logger.log_enabled() then log "matches result: %b" r; r
let no_match ?(subterm=false) pos (vars,p) t =
let ts = Array.map (fun v -> mk_Vari (new_var ("$"^base_name v))) vars in
let p = msubst (bind_mvar vars p) ts in
if subterm then fatal pos "No subterm of [%a] matches [%a]." term t term p
else fatal pos "[%a] doesn't match [%a]." term t term p
(** [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].
WARNING: Some elements of [ts] may be uninstantiated TRef's. It will happen if
not all [vars] are in [LibTerm.free_vars p], for instance when some [vars] are
type variables not occurring in [p], for instance when trying to apply an
equation of the form [x = ...] with [x] of polymorphic type. This could be
improved by generating p_terms instead of terms. Indeed, in this case, we
could replace uninstantiated TRef's by underscores. *)
let matching_subs : to_subst -> term -> term array option = fun (xs,p) t ->
let ts = Array.map (fun _ -> mk_TRef(Timed.ref None)) xs in
let p = msubst (bind_mvar xs p) ts in
if matches p t then Some ts else None
(** [check_subs vars ts] check that no element of [ts] is an uninstantiated
TRef. *)
let check_subs pos vars ts =
let f i ti =
match unfold ti with
| TRef _ ->
fatal pos "Don't know how to instantiate the argument \"%a\" \
of the equation." var vars.(i)
| _ -> ()
in
Array.iteri f ts
let matching_subs_check_TRef pos ((vars,_) as xsp) t =
match matching_subs xsp t with
| Some ts -> check_subs pos vars ts; ts
| None -> no_match pos xsp t
(** [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 be substituted. *)
let find_subst : to_subst -> term -> term array option = fun xsp t ->
let time = Timed.Time.save () in
let rec find : term -> term array option = fun t ->
if Logger.log_enabled() then
log "find_subst %a ≡ %a" term (snd xsp) term t;
match matching_subs xsp t with
| None ->
begin
Timed.Time.restore time;
match unfold t with
| Appl(a,b) -> find2 a b
| Abst(a,b) | Prod(a,b) -> let _,b = unbind b in find2 a b
| LLet(a,c,b) -> let _,b = unbind b in find3 a c b
| _ -> None
end
| sub -> sub
and find2 a b =
match find a with
| None -> Timed.Time.restore time; find b
| sub -> sub
and find3 a b c =
match find a with
| None -> Timed.Time.restore time; find2 b c
| sub -> sub
in find t
let find_subst pos (vars,p) t =
match find_subst (vars,p) t with
| None -> no_match ~subterm:true pos (vars,p) t
| Some ts -> check_subs pos vars ts; ts
(** [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 = Timed.Time.save () in
let rec find : term -> bool = fun t ->
matches p t ||
begin
Timed.Time.restore time;
match unfold t with
| Appl(a,b) -> find2 a b
| Abst(a,b) | Prod(a,b) -> let _,b = unbind b in find2 a b
| LLet(a,c,b) -> let _,b = unbind b in find3 a c b
| _ -> false
end
and find2 a b =
match find a with
| false -> Timed.Time.restore time; find b
| true -> true
and find3 a c b =
match find a with
| false -> Timed.Time.restore time; find2 c b
| true -> true
in find t
(** [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(Timed.ref None)
| Appl(t,u) ->
mk_Appl_not_canonical(replace_wild_by_tref t, replace_wild_by_tref u)
| _ -> t
let find_subterm_matching pos p t =
let p_refs = replace_wild_by_tref p in
if not (find_subterm_matching p_refs t) then
no_match ~subterm:true pos ([||],p) t;
p_refs
(** [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 -> binder = fun p t ->
let z = new_var "z" in
let rec replace : term -> term = fun t ->
if matches p t then mk_Vari z else
match unfold t with
| Appl(t,u) -> mk_Appl (replace t, replace u)
| Prod(a,b) ->
let x,b = unbind b in
mk_Prod (replace a, bind_var x (replace b))
| Abst(a,b) ->
let x,b = unbind b in
mk_Abst (replace a, bind_var x (replace b))
| LLet(typ, def, body) ->
let x, body = unbind body in
mk_LLet (replace typ, replace def, bind_var x (replace body))
| Meta(m,ts) -> mk_Meta (m, Array.map replace ts)
| Bvar _ -> assert false
| Wild -> assert false
| TRef _ -> assert false
| Patt _ -> assert false
| Plac _ -> assert false
| _ -> t
in
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_var "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_var x 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]
(** [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, binder) 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_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 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 bind = bind_mvar vars in bind t, bind l, bind r in
let msubst3 (b1, b2, b3) ts = msubst b1 ts, msubst b2 ts, msubst b3 ts in
let (pred_bind, new_term, t, l, r) =
match pat with
| None ->
let sigma = find_subst pos (vars, l) g_term in
let (t, l, r) = msubst3 bound sigma in
let pred_bind = bind_pattern l g_term in
(pred_bind, subst pred_bind r, t, l, r)
| Some(Rw_Term(p)) ->
let match_p = find_subterm_matching pos p g_term in
let sigma = matching_subs_check_TRef pos (vars,l) match_p in
let (t, l, r) = msubst3 bound sigma in
let pred_bind = bind_pattern l g_term in
(pred_bind, subst pred_bind r, t, l, r)
| Some(Rw_InTerm(p)) ->
let match_p = find_subterm_matching pos p g_term in
let sigma = find_subst pos (vars,l) match_p in
let (t, l, r) = msubst3 bound sigma in
let p_x = bind_pattern l match_p in
let p_r = subst p_x r in
let pred_bind = bind_pattern match_p g_term in
let new_term = subst pred_bind p_r in
let (x, p_x) = unbind p_x in
let pred = subst pred_bind p_x in
let pred_bind = bind_var x pred in
(pred_bind, new_term, t, l, r)
| Some(Rw_IdInTerm(p)) ->
let (id,p) = unbind p in
let p_refs = replace_wild_by_tref p in
let sigma = find_subst pos ([|id|],p_refs) g_term in
let id_val = sigma.(0) in
let pat = bind_var id p_refs in
let pat_l = subst pat id_val in
let sigma = matching_subs_check_TRef pos (vars,l) id_val in
let (t,l,r) = msubst3 bound sigma in
let pat_r = subst pat r in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = subst pred_bind_l pat_r in
let (x, l_x) = unbind pat in
let pred_bind = bind_var x (subst pred_bind_l l_x) in
(pred_bind, new_term, t, l, r)
| Some(Rw_TermInIdInTerm(s,p)) ->
let (id,p) = unbind p in
let p_refs = replace_wild_by_tref p in
let sigma = find_subst pos ([|id|],p_refs) g_term in
let id_val = sigma.(0) in
let pat = bind_var id p_refs in
let pat_l = subst pat id_val in
let s = find_subterm_matching pos s id_val in
let sigma = matching_subs_check_TRef pos (vars,l) s in
let (t,l,r) = msubst3 bound sigma in
let id_bind = bind_pattern l id_val in
let new_id = subst id_bind r in
let (x, id_x) = unbind id_bind in
let pat_r = subst pat new_id in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = subst pred_bind_l pat_r in
let l_x = subst pat id_x in
let pred = subst pred_bind_l l_x in
(bind_var x pred, new_term, t, l, r)
| Some(Rw_TermAsIdInTerm(s,p)) ->
let (id,pat) = unbind p in
let s = replace_wild_by_tref s in
let p_s = subst p s in
let p = find_subterm_matching pos p_s g_term in
let pat_refs = replace_wild_by_tref pat in
let sub = matching_subs_check_TRef pos ([|id|],pat_refs) p in
let id_val = sub.(0) in
let sigma = matching_subs_check_TRef pos (vars,l) id_val in
let (t,l,r) = msubst3 bound sigma in
let p_x = bind_pattern l p in
let p_r = subst p_x r in
let pred_bind = bind_pattern p g_term in
let new_term = subst pred_bind p_r in
let (x, p_x) = unbind p_x in
let pred_bind = bind_var x (subst pred_bind p_x) in
(pred_bind, new_term, t, l, r)
| Some(Rw_InIdInTerm(q)) ->
let (id,q) = unbind q in
let q_refs = replace_wild_by_tref q in
let sigma = find_subst pos ([|id|],q_refs) g_term in
let id_val = sigma.(0) in
let pat = bind_var id q_refs in
let pat_l = subst pat id_val in
let sigma = find_subst pos (vars,l) id_val in
let (t,l,r) = msubst3 bound sigma in
let id_bind = bind_pattern l id_val in
let id_val = subst id_bind r in
let (x, id_x) = unbind id_bind in
let r_val = subst pat id_val in
let pred_bind_l = bind_pattern pat_l g_term in
let new_term = subst pred_bind_l r_val in
let l_x = subst pat id_x in
let pred_bind = bind_var x (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 "Rewriting with:";
log " goal = [%a]" term g_type;
log " equality proof = [%a]" term t;
log " equality LHS = [%a]" term l;
log " equality RHS = [%a]" term r;
log " pred = [%a]" term pred;
log " new goal = [%a]" term goal_type;
log " produced term = [%a]" term result;
end;
result