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
(** Implementation of the rewrite tactic. *)
open! Lplib
open Timed
open Common
open Pos
open Core
open Term
open Error
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
(** [eq t u] tests the equality of [t] and [u] (up to α-equivalence).
It fails if [t] or [u] contain terms of the form [Patt(i,s,e)] or
[TEnv(te,env)]. In the process, subterms of the form [TRef(r)] in [t] and
[u] may be set with the corresponding value to enforce equality, and
variables appearing in [ctx] can be unfolded. In other words, [eq t u] can
be used to implement non-linear matching. When the
matching feature is used, one should make sure that [TRef] constructors do
not appear both in [t] and in [u] at the same time. Indeed, the references
are set naively, without occurrence checking. *)
let eq : term -> term -> bool = fun a b -> a == b ||
let exception Not_equal in
let rec eq l =
match l with
| [] -> ()
| (a,b)::l ->
begin
if Logger.log_enabled () then log_rewr "eq [%a] [%a]" pp_term a pp_term b;
match (unfold a, unfold b) with
| (a , b ) when a == b -> eq l
| (Vari(x1) , Vari(x2) ) when Bindlib.eq_vars x1 x2 -> eq l
| (Type , Type )
| (Kind , Kind ) -> eq l
| (Symb(s1) , Symb(s2) ) when s1 == s2 -> eq l
| (Prod(a1,b1), Prod(a2,b2))
| (Abst(a1,b1), Abst(a2,b2)) -> let (_, b1, b2) = Bindlib.unbind2 b1 b2 in
eq ((a1,a2)::(b1,b2)::l)
| (LLet(a1,t1,u1), LLet(a2,t2,u2)) ->
let (_, u1, u2) = Bindlib.unbind2 u1 u2 in
eq ((a1,a2)::(t1,t2)::(u1,u2)::l)
| (Appl(t1,u1), Appl(t2,u2)) -> eq ((t1,t2)::(u1,u2)::l)
| (Meta(m1,e1), Meta(m2,e2)) when m1 == m2 ->
eq (if e1 == e2 then l else List.add_array2 e1 e2 l)
| (Wild , _ )
| (_ , Wild ) -> eq l
| (TRef(r) , b ) -> r := Some(b); eq l
| (a , TRef(r) ) -> r := Some(a); eq l
| (Patt(_,_,_), _ )
| (_ , Patt(_,_,_))
| (TEnv(_,_) , _ )
| (_ , TEnv(_,_) ) -> assert false
| (_ , _ ) -> raise Not_equal
end
in
try eq [(a,b)]; true with Not_equal -> false
(** 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_t_or_p _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_t_or_p;
Builtin.register "P" check_t_or_p;
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 []) pp_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),v)] 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 array = 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" pp_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, Array.of_list (List.rev vs) in
let rec get_eq vs t notin_whnf =
if Logger.log_enabled () then log_rewr "get_eq %a" pp_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 ~rewrite:false [] 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" pp_term t;
try get_eq [] t true with
| Not_P u ->
fatal pos "Expected %a _ but found %a." pp_sym cfg.symb_P pp_term u
| Not_eq u ->
fatal pos "Expected %a _ _ but found %a." pp_sym cfg.symb_eq pp_term u
(** Type of a term with the free variables that need to be substituted (during
some unification process). 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
(** [add_refs t] substitutes each wildcard of [t] using a fresh reference cell
([TRef] constructor). This is used for unification, by performing all the
substitutions in-place. *)
let rec add_refs : term -> term = fun t ->
match unfold t with
| Wild -> mk_TRef(ref None)
| Appl(t1,t2) -> mk_Appl(add_refs t1, add_refs t2)
| _ -> t
(** [match_pattern (xs,p) t] attempts to match the pattern [p] (containing the
“pattern variables” of [xs]) with the term [t]. If successful, it returns
[Some(ts)] where [ts] is an array of terms such that substituting elements
of [xs] by the corresponding elements of [ts] in [p] yields a term that is
equal to [t] (in terms of [eq]). *)
let match_pattern : 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 (Bindlib.unbox (Bindlib.bind_mvar xs (lift p))) ts in
if eq p t then Some(Array.map unfold ts) else None
(** [find_subst t (xs,p)] is given a term [t] and a pattern [p] (with “pattern
variables” of [xs]), and it finds the first instance of (a term matching)
[p] in [t] (if there is any). If successful, the function returns an array
of terms corresponding to the substitution (see [match_pattern]). *)
let find_subst : term -> to_subst -> term array option = fun t (xs,p) ->
let time = Time.save () in
let rec find_sub_aux : term -> term array option = fun t ->
match match_pattern (xs,p) t with
| None ->
begin
Time.restore time;
match unfold t with
| Appl(t,u) ->
begin
match find_sub_aux t with
| None -> Time.restore time; find_sub_aux u
| sub -> sub
end
| _ -> None
end
| sub -> sub
in find_sub_aux t
(** [make_pat t p] is given a term [t], and a pattern [p] containing reference
cells (that are not instantiated) and wildcards. It then tries to find a
subterm of [t] that matches [p], using (instantiating) syntactic equality.
In case of success, the function returns [true], and the matching term is
[p] itself (through instantiation). *)
let make_pat : term -> term -> bool = fun t p ->
let time = Time.save () in
let rec make_pat_aux : term -> bool = fun t ->
if eq t p then true else
begin
Time.restore time;
match unfold t with
| Appl(t,u) ->
begin
match make_pat_aux t with
| false -> Time.restore time; make_pat_aux u
| true -> true
end
| _ -> false
end
in make_pat_aux 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 eq 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
| _ -> 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
let pred = Bindlib.unbox (Bindlib.bind_var x (lift pred)) in
mk_Abst(mk_Appl(mk_Symb cfg.symb_T, a), 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, 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 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 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 _)." pp_sym cfg.symb_P
in
let (pred_bind, new_term, t, l, r) =
match pat with
| None ->
let sigma =
match find_subst g_term (vars, l) with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of [%a] matches [%a]."
pp_term g_term pp_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 = add_refs p in
if not (make_pat g_term p_refs) then
fatal pos "No subterm of [%a] matches [%a]."
pp_term g_term pp_term p;
p_refs
in
let sigma =
match match_pattern (vars,l) match_p with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of [%a] matches [%a]."
pp_term match_p pp_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 = add_refs p in
if not (make_pat g_term p_refs) then
fatal pos "No subterm of [%a] matches [%a]."
pp_term g_term pp_term p;
p_refs
in
let sigma =
match find_subst match_p (vars,l) with
| Some(sigma) -> sigma
| None ->
fatal pos "No subterm of the pattern [%a] matches [%a]."
pp_term match_p pp_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_box = lift (Bindlib.subst pred_bind p_x) in
let pred_bind = Bindlib.unbox (Bindlib.bind_var x pred_box) in
(pred_bind, new_term, t, l, r)
| Some(Rw_IdInTerm(p)) ->
let (id,p) = Bindlib.unbind p in
let p_refs = add_refs p in
let id_val =
match find_subst g_term ([|id|],p_refs) with
| Some(id_val) -> id_val.(0)
| None ->
fatal pos "The pattern [%a] does not match [%a]."
pp_term p pp_term l
in
let pat = Bindlib.unbox (Bindlib.bind_var id (lift p_refs)) in
let pat_l = Bindlib.subst pat id_val in
let sigma =
match match_pattern (vars,l) id_val with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of [%a], [%a], in [%a] does not match [%a]."
pp_var id pp_term id_val pp_term p pp_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_box = lift (Bindlib.subst pred_bind_l l_x) in
let pred_bind = Bindlib.unbox (Bindlib.bind_var x pred_box) in
(pred_bind, new_term, t, l, r)
| Some(Rw_TermInIdInTerm(s,p)) ->
let (id,p) = Bindlib.unbind p in
let p_refs = add_refs p in
let id_val =
match find_subst g_term ([|id|],p_refs) with
| Some(id_val) -> id_val
| None ->
fatal pos "The pattern [%a] does not match [%a]."
pp_term p pp_term l
in
let id_val = id_val.(0) in
let pat = Bindlib.unbox (Bindlib.bind_var id (lift p_refs)) in
let pat_l = Bindlib.subst pat id_val in
let s_refs = add_refs s in
if not (make_pat id_val s_refs) then
fatal pos "The value of [%a], [%a], in [%a] does not match [%a]."
pp_var id pp_term id_val pp_term p pp_term s;
let s = s_refs in
let sigma =
match match_pattern (vars,l) s with
| Some(sigma) -> sigma
| None ->
fatal pos "The term [%a] does not match the LHS [%a]"
pp_term s pp_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
let pred_bind = Bindlib.bind_var x (lift pred) in
(Bindlib.unbox pred_bind, new_term, t, l, r)
| Some(Rw_TermAsIdInTerm(s,p)) ->
let (id,pat) = Bindlib.unbind p in
let s = add_refs s in
let p_s = Bindlib.subst p s in
let p_refs = add_refs p_s in
if not (make_pat g_term p_refs) then
fatal pos "No subterm of [%a] matches the pattern [%a]"
pp_term g_term pp_term p_s;
let p = p_refs in
let pat_refs = add_refs pat in
let sub =
match match_pattern ([|id|], pat_refs) p with
| Some(sub) -> sub
| None -> assert false
in
let id_val = sub.(0) in
let sigma =
match match_pattern (vars, l) id_val with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of X, [%a], does not match the LHS, [%a]"
pp_term id_val pp_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_box = lift (Bindlib.subst pred_bind p_x) in
let pred_bind = Bindlib.(unbox (bind_var x pred_box)) in
(pred_bind, new_term, t, l, r)
| Some(Rw_InIdInTerm(q)) ->
let (id,q) = Bindlib.unbind q in
let q_refs = add_refs q in
let id_val =
match find_subst g_term ([|id|],q_refs) with
| Some(id_val) -> id_val
| None ->
fatal pos "The pattern [%a] does not match [%a]."
pp_term q pp_term g_term
in
let id_val = id_val.(0) in
let pat = Bindlib.unbox (Bindlib.bind_var id (lift q_refs)) in
let pat_l = Bindlib.subst pat id_val in
let sigma =
match find_subst id_val (vars,l) with
| Some(sigma) -> sigma
| None ->
fatal pos
"The value of [%a], [%a], in [%a] does not match [%a]."
pp_var id pp_term id_val pp_term q pp_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_box = lift (Bindlib.subst pred_bind_l l_x) in
let pred_bind = Bindlib.unbox (Bindlib.bind_var x pred_box) 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 term = 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]" pp_term g_type;
log_rewr " equality proof = [%a]" pp_term t;
log_rewr " equality LHS = [%a]" pp_term l;
log_rewr " equality RHS = [%a]" pp_term r;
log_rewr " pred = [%a]" pp_term pred;
log_rewr " new goal = [%a]" pp_term goal_type;
log_rewr " produced term = [%a]" pp_term term;
end;
term