4 let string_of_equality ?env =
8 | _, (ty, left, right), _, _ ->
9 Printf.sprintf "{%s}: %s = %s" (CicPp.ppterm ty)
10 (CicPp.ppterm left) (CicPp.ppterm right)
12 | Some (_, context, _) -> (
13 let names = names_of_context context in
15 | _, (ty, left, right), _, _ ->
16 Printf.sprintf "{%s}: %s = %s" (CicPp.pp ty names)
17 (CicPp.pp left names) (CicPp.pp right names)
22 let rec metas_of_term = function
23 | Cic.Meta (i, c) -> [i]
26 | Cic.MutInd (_, _, ens)
27 | Cic.MutConstruct (_, _, _, ens) ->
28 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
31 | Cic.Lambda (_, s, t)
32 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
33 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
34 | Cic.MutCase (uri, i, s, t, l) ->
35 (metas_of_term s) @ (metas_of_term t) @
36 (List.flatten (List.map metas_of_term l))
39 (List.map (fun (s, i, t1, t2) ->
40 (metas_of_term t1) @ (metas_of_term t2)) il)
41 | Cic.CoFix (i, il) ->
43 (List.map (fun (s, t1, t2) ->
44 (metas_of_term t1) @ (metas_of_term t2)) il)
49 exception NotMetaConvertible;;
51 let meta_convertibility_aux table t1 t2 =
56 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
58 let rec aux ((table_l, table_r) as table) t1 t2 =
59 (* Printf.printf "aux %s, %s\ntable_l: %s, table_r: %s\n" *)
60 (* (CicPp.ppterm t1) (CicPp.ppterm t2) *)
61 (* (print_table table_l) (print_table table_r); *)
63 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
64 let m1_binding, table_l =
65 try List.assoc m1 table_l, table_l
66 with Not_found -> m2, (m1, m2)::table_l
67 and m2_binding, table_r =
68 try List.assoc m2 table_r, table_r
69 with Not_found -> m1, (m2, m1)::table_r
71 (* let m1_binding, m2_binding, table = *)
72 (* let m1b, table = *)
73 (* try List.assoc m1 table, table *)
74 (* with Not_found -> m2, (m1, m2)::table *)
76 (* let m2b, table = *)
77 (* try List.assoc m2 table, table *)
78 (* with Not_found -> m1, (m2, m1)::table *)
82 (* Printf.printf "table_l: %s\ntable_r: %s\n\n" *)
83 (* (print_table table_l) (print_table table_r); *)
84 if (m1_binding <> m2) || (m2_binding <> m1) then
85 raise NotMetaConvertible
91 | None, Some _ | Some _, None -> raise NotMetaConvertible
93 | Some t1, Some t2 -> (aux res t1 t2))
94 (table_l, table_r) tl1 tl2
95 with Invalid_argument _ ->
96 raise NotMetaConvertible
98 | C.Var (u1, ens1), C.Var (u2, ens2)
99 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
100 aux_ens table ens1 ens2
101 | C.Cast (s1, t1), C.Cast (s2, t2)
102 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
103 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
104 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
105 let table = aux table s1 s2 in
107 | C.Appl l1, C.Appl l2 -> (
108 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
109 with Invalid_argument _ -> raise NotMetaConvertible
111 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
112 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
113 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
114 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
115 aux_ens table ens1 ens2
116 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
117 when (UriManager.eq u1 u2) && i1 = i2 ->
118 let table = aux table s1 s2 in
119 let table = aux table t1 t2 in (
120 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
121 with Invalid_argument _ -> raise NotMetaConvertible
123 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
126 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
127 if i1 <> i2 then raise NotMetaConvertible
129 let res = (aux res s1 s2) in aux res t1 t2)
131 with Invalid_argument _ -> raise NotMetaConvertible
133 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
136 (fun res (n1, s1, t1) (n2, s2, t2) ->
137 let res = aux res s1 s2 in aux res t1 t2)
139 with Invalid_argument _ -> raise NotMetaConvertible
141 | t1, t2 when t1 = t2 -> table
142 | _, _ -> raise NotMetaConvertible
144 and aux_ens table ens1 ens2 =
145 let cmp (u1, t1) (u2, t2) =
146 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
148 let ens1 = List.sort cmp ens1
149 and ens2 = List.sort cmp ens2 in
152 (fun res (u1, t1) (u2, t2) ->
153 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
156 with Invalid_argument _ -> raise NotMetaConvertible
162 let meta_convertibility_eq eq1 eq2 =
163 let _, (ty, left, right), _, _ = eq1
164 and _, (ty', left', right'), _, _ = eq2 in
167 else if (left = left') && (right = right') then
169 else if (left = right') && (right = left') then
173 let table = meta_convertibility_aux ([], []) left left' in
174 let _ = meta_convertibility_aux table right right' in
176 with NotMetaConvertible ->
178 let table = meta_convertibility_aux ([], []) left right' in
179 let _ = meta_convertibility_aux table right left' in
181 with NotMetaConvertible ->
186 let meta_convertibility t1 t2 =
190 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
196 let l, r = meta_convertibility_aux ([], []) t1 t2 in
197 (* Printf.printf "meta_convertibility:\n%s\n%s\n\n" (f l) (f r); *)
199 with NotMetaConvertible ->
204 let replace_metas (* context *) term =
205 let module C = Cic in
206 let rec aux = function
209 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
211 (* if c = irl then *)
212 (* C.Implicit (Some (`MetaIndex i)) *)
214 (* Printf.printf "WARNING: c non e` un identity_relocation_list!\n%s\n" *)
215 (* (String.concat "\n" *)
217 (* (function None -> "" | Some t -> CicPp.ppterm t) c)); *)
220 C.Implicit (Some (`MetaInfo (i, c)))
221 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
222 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
223 | C.Cast (s, t) -> C.Cast (aux s, aux t)
224 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
225 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
226 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
227 | C.Appl l -> C.Appl (List.map aux l)
228 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
229 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
230 | C.MutCase (uri, i, s, t, l) ->
231 C.MutCase (uri, i, aux s, aux t, List.map aux l)
234 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
238 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
242 List.map (fun (u, t) -> (u, aux t)) ens
248 let restore_metas (* context *) term =
249 let module C = Cic in
250 let rec aux = function
251 | C.Implicit (Some (`MetaInfo (i, c))) ->
253 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
256 (* let local_context:(C.term option) list = *)
257 (* Marshal.from_string mc 0 *)
259 (* C.Meta (i, local_context) *)
261 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
262 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
263 | C.Cast (s, t) -> C.Cast (aux s, aux t)
264 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
265 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
266 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
267 | C.Appl l -> C.Appl (List.map aux l)
268 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
269 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
270 | C.MutCase (uri, i, s, t, l) ->
271 C.MutCase (uri, i, aux s, aux t, List.map aux l)
274 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
278 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
282 List.map (fun (u, t) -> (u, aux t)) ens
288 let rec restore_subst (* context *) subst =
290 (fun (i, (c, t, ty)) ->
291 i, (c, restore_metas (* context *) t, ty))
296 let beta_expand ?(metas_ok=true) ?(match_only=false)
297 what type_of_what where context metasenv ugraph =
298 let module S = CicSubstitution in
299 let module C = Cic in
301 let print_info = false in
304 (* let names = names_of_context context in *)
305 (* Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
306 (* (CicPp.pp what names) (CicPp.ppterm what) *)
307 (* (CicPp.pp where names) (CicPp.ppterm where); *)
308 (* print_newline (); *)
312 ((list of all possible beta expansions, subst, metasenv, ugraph),
315 let rec aux lift_amount term context metasenv subst ugraph =
316 (* Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
317 let res, lifted_term =
320 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
322 | C.Var (uri, exp_named_subst) ->
323 let ens', lifted_ens =
324 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
328 (fun (e, s, m, ug) ->
329 (C.Var (uri, e), s, m, ug)) ens'
331 expansions, C.Var (uri, lifted_ens)
336 (fun arg (res, lifted_tl) ->
339 let arg_res, lifted_arg =
340 aux lift_amount arg context metasenv subst ugraph in
343 (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
348 (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
350 (Some lifted_arg)::lifted_tl)
353 (fun (r, s, m, ug) -> None::r, s, m, ug)
360 (fun (l, s, m, ug) ->
361 (C.Meta (i, l), s, m, ug)) l'
363 e, C.Meta (i, lifted_l)
366 | C.Implicit _ as t -> [], t
370 aux lift_amount s context metasenv subst ugraph in
372 aux lift_amount t context metasenv subst ugraph
376 (fun (t, s, m, ug) ->
377 C.Cast (t, lifted_t), s, m, ug) l1 in
380 (fun (t, s, m, ug) ->
381 C.Cast (lifted_s, t), s, m, ug) l2 in
382 l1'@l2', C.Cast (lifted_s, lifted_t)
384 | C.Prod (nn, s, t) ->
386 aux lift_amount s context metasenv subst ugraph in
388 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
389 metasenv subst ugraph
393 (fun (t, s, m, ug) ->
394 C.Prod (nn, t, lifted_t), s, m, ug) l1 in
397 (fun (t, s, m, ug) ->
398 C.Prod (nn, lifted_s, t), s, m, ug) l2 in
399 l1'@l2', C.Prod (nn, lifted_s, lifted_t)
401 | C.Lambda (nn, s, t) ->
403 aux lift_amount s context metasenv subst ugraph in
405 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
406 metasenv subst ugraph
410 (fun (t, s, m, ug) ->
411 C.Lambda (nn, t, lifted_t), s, m, ug) l1 in
414 (fun (t, s, m, ug) ->
415 C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
416 l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
418 | C.LetIn (nn, s, t) ->
420 aux lift_amount s context metasenv subst ugraph in
422 aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
423 metasenv subst ugraph
427 (fun (t, s, m, ug) ->
428 C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
431 (fun (t, s, m, ug) ->
432 C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
433 l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
437 aux_list lift_amount l context metasenv subst ugraph
439 (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
442 | C.Const (uri, exp_named_subst) ->
443 let ens', lifted_ens =
444 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
448 (fun (e, s, m, ug) ->
449 (C.Const (uri, e), s, m, ug)) ens'
451 (expansions, C.Const (uri, lifted_ens))
453 | C.MutInd (uri, i ,exp_named_subst) ->
454 let ens', lifted_ens =
455 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
459 (fun (e, s, m, ug) ->
460 (C.MutInd (uri, i, e), s, m, ug)) ens'
462 (expansions, C.MutInd (uri, i, lifted_ens))
464 | C.MutConstruct (uri, i, j, exp_named_subst) ->
465 let ens', lifted_ens =
466 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
470 (fun (e, s, m, ug) ->
471 (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
473 (expansions, C.MutConstruct (uri, i, j, lifted_ens))
475 | C.MutCase (sp, i, outt, t, pl) ->
476 let pl_res, lifted_pl =
477 aux_list lift_amount pl context metasenv subst ugraph
479 let l1, lifted_outt =
480 aux lift_amount outt context metasenv subst ugraph in
482 aux lift_amount t context metasenv subst ugraph in
486 (fun (outt, s, m, ug) ->
487 C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
490 (fun (t, s, m, ug) ->
491 C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
494 (fun (pl, s, m, ug) ->
495 C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
497 (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
500 let len = List.length fl in
503 (fun (nm, idx, ty, bo) (res, lifted_tl) ->
504 let lifted_ty = S.lift lift_amount ty in
505 let bo_res, lifted_bo =
506 aux (lift_amount+len) bo context metasenv subst ugraph in
509 (fun (a, s, m, ug) ->
510 (nm, idx, lifted_ty, a)::lifted_tl, s, m, ug)
515 (fun (r, s, m, ug) ->
516 (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
517 (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
521 (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
522 C.Fix (i, lifted_fl))
525 let len = List.length fl in
528 (fun (nm, ty, bo) (res, lifted_tl) ->
529 let lifted_ty = S.lift lift_amount ty in
530 let bo_res, lifted_bo =
531 aux (lift_amount+len) bo context metasenv subst ugraph in
534 (fun (a, s, m, ug) ->
535 (nm, lifted_ty, a)::lifted_tl, s, m, ug)
540 (fun (r, s, m, ug) ->
541 (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
542 (nm, lifted_ty, lifted_bo)::lifted_tl)
546 (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
547 C.CoFix (i, lifted_fl))
551 | C.Meta _ when (not metas_ok) ->
555 (* if match_only then replace_metas context term *)
559 let subst', metasenv', ugraph' =
560 (* Printf.printf "provo a unificare %s e %s\n" *)
561 (* (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
562 CicUnification.fo_unif metasenv context
563 (S.lift lift_amount what) term ugraph
565 (* Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
566 (* (CicPp.ppterm (S.lift lift_amount what)); *)
567 (* Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
568 (* Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
569 (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
571 let t' = CicMetaSubst.apply_subst subst' term in
572 if not (meta_convertibility term t') then (
573 (* if print_info then ( *)
574 (* let names = names_of_context context in *)
576 (* "\nbeta_expand: term e t' sono diversi!:\n%s\n%s\n\n" *)
577 (* (CicPp.pp term names) (CicPp.pp t' names) *)
581 let metas = metas_of_term term in
582 (* let ok = ref false in *)
583 let fix_subst = function
584 | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas ->
585 (* Printf.printf "fix_subst: scambio ?%d e ?%d\n" i j; *)
587 (j, (c, C.Meta (i, lc), ty))
590 let subst' = List.map fix_subst subst' in
592 (* Printf.printf "aaa:\nterm: %s\nt'%s\n term subst': %s\n" *)
593 (* (CicPp.ppterm term) *)
594 (* (CicPp.ppterm t') *)
595 (* (CicPp.ppterm (CicMetaSubst.apply_subst subst' term)) *)
597 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
600 (* ((C.Rel (1 + lift_amount), restore_subst context subst', *)
601 (* metasenv', ugraph')::res, lifted_term) *)
603 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
607 print_endline ("beta_expand ERROR!: " ^ (Printexc.to_string e));
611 (* Printf.printf "exit aux\n"; *)
614 and aux_list lift_amount l context metasenv subst ugraph =
616 (fun arg (res, lifted_tl) ->
617 let arg_res, lifted_arg =
618 aux lift_amount arg context metasenv subst ugraph in
620 (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
623 (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
624 lifted_arg::lifted_tl)
627 and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
629 (fun (u, arg) (res, lifted_tl) ->
630 let arg_res, lifted_arg =
631 aux lift_amount arg context metasenv subst ugraph in
634 (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
636 (l1 @ (List.map (fun (r, s, m, ug) ->
637 (u, lifted_arg)::r, s, m, ug) res),
638 (u, lifted_arg)::lifted_tl)
639 ) exp_named_subst ([], [])
644 (* if match_only then replace_metas (\* context *\) where *)
648 Printf.printf "searching %s inside %s\n"
649 (CicPp.ppterm what) (CicPp.ppterm where);
651 aux 0 where context metasenv [] ugraph
654 (* if match_only then *)
655 (* (fun (term, subst, metasenv, ugraph) -> *)
657 (* C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
658 (* and subst = restore_subst subst in *)
659 (* (term', subst, metasenv, ugraph)) *)
661 (fun (term, subst, metasenv, ugraph) ->
662 let term' = C.Lambda (C.Anonymous, type_of_what, term) in
663 (term', subst, metasenv, ugraph))
665 List.map mapfun expansions
670 Cic.term * (* proof *)
671 (Cic.term * (* type *)
672 Cic.term * (* left side *)
673 Cic.term) * (* right side *)
674 Cic.metasenv * (* environment for metas *)
675 Cic.term list (* arguments *)
679 let find_equalities ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) context proof =
680 let module C = Cic in
681 let module S = CicSubstitution in
682 let module T = CicTypeChecker in
683 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
684 let rec aux index newmeta = function
686 | (Some (_, C.Decl (term)))::tl ->
687 let do_find context term =
689 | C.Prod (name, s, t) ->
690 (* let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
691 let (head, newmetas, args, _) =
692 PrimitiveTactics.new_metasenv_for_apply newmeta proof
693 context (S.lift index term)
699 | C.Meta (i, _) -> (max maxm i)
704 if List.length args = 0 then
707 C.Appl ((C.Rel index)::args)
710 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
711 Printf.printf "OK: %s\n" (CicPp.ppterm term);
712 Some (p, (ty, t1, t2), newmetas, args), (newmeta+1)
715 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
717 (ty, S.lift index t1, S.lift index t2), [], []), (newmeta+1)
720 match do_find context term with
722 let tl, newmeta' = (aux (index+1) newmeta tl) in
723 p::tl, max newmeta newmeta'
725 aux (index+1) newmeta tl
728 aux (index+1) newmeta tl
730 aux 1 newmeta context
734 let fix_metas newmeta ((proof, (ty, left, right), menv, args) as equality) =
735 let table = Hashtbl.create (List.length args) in
738 (fun t (newargs, index) ->
741 Hashtbl.add table i index;
742 ((Cic.Meta (index, l))::newargs, index+1)
747 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
752 (fun (i, context, term) menv ->
754 let index = Hashtbl.find table i in
755 (index, context, term)::menv
757 (i, context, term)::menv)
762 and right = repl right in
763 let metas = (metas_of_term left) @ (metas_of_term right) in
764 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv'
767 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
769 (newmeta + (List.length newargs) + 1,
770 (repl proof, (ty, left, right), menv', newargs))
774 exception TermIsNotAnEquality;;
776 let equality_of_term ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) proof = function
777 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
778 (proof, (ty, t1, t2), [], [])
780 raise TermIsNotAnEquality
784 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
787 let superposition_left (metasenv, context, ugraph) target source =
788 let module C = Cic in
789 let module S = CicSubstitution in
790 let module M = CicMetaSubst in
791 let module HL = HelmLibraryObjects in
792 let module CR = CicReduction in
793 (* we assume that target is ground (does not contain metavariables): this
794 * should always be the case (I hope, at least) *)
795 let proof, (eq_ty, left, right), _, _ = target in
796 let eqproof, (ty, t1, t2), newmetas, args = source in
798 let compare_terms = !Utils.compare_terms in
804 match compare_terms left right with
808 Printf.printf "????????? %s = %s" (CicPp.ppterm left)
809 (CicPp.ppterm right);
811 assert false (* again, for ground terms this shouldn't happen... *)
814 let metasenv' = newmetas @ metasenv in
815 let result = compare_terms t1 t2 in
818 | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
819 | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
823 (fun (t, s, m, ug) ->
824 compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
825 (beta_expand t1 ty where context metasenv' ugraph)
828 (fun (t, s, m, ug) ->
829 compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
830 (beta_expand t2 ty where context metasenv' ugraph)
834 (* let what, other = *)
835 (* if is_left then left, right *)
836 (* else right, left *)
838 let build_new what other eq_URI (t, s, m, ug) =
839 let newgoal, newgoalproof =
841 | C.Lambda (nn, ty, bo) ->
842 let bo' = S.subst (M.apply_subst s other) bo in
845 [C.MutInd (HL.Logic.eq_URI, 0, []);
847 if is_left then [bo'; S.lift 1 right]
848 else [S.lift 1 left; bo'])
850 let t' = C.Lambda (nn, ty, bo'') in
851 S.subst (M.apply_subst s other) bo,
853 (C.Appl [C.Const (eq_URI, []); ty; what; t';
854 proof; other; eqproof])
858 if is_left then (eq_ty, newgoal, right)
859 else (eq_ty, left, newgoal)
861 (eqproof, equation, [], [])
863 let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
864 and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
869 let superposition_right newmeta (metasenv, context, ugraph) target source =
870 let module C = Cic in
871 let module S = CicSubstitution in
872 let module M = CicMetaSubst in
873 let module HL = HelmLibraryObjects in
874 let module CR = CicReduction in
875 let eqproof, (eq_ty, left, right), newmetas, args = target in
876 let eqp', (ty', t1, t2), newm', args' = source in
877 let maxmeta = ref newmeta in
879 let compare_terms = !Utils.compare_terms in
884 (* let ok term subst other other_eq_side ugraph = *)
885 (* match term with *)
886 (* | C.Lambda (nn, ty, bo) -> *)
887 (* let bo' = S.subst (M.apply_subst subst other) bo in *)
888 (* let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
890 (* | _ -> assert false *)
892 let condition left right what other (t, s, m, ug) =
893 let subst = M.apply_subst s in
894 let cmp1 = compare_terms (subst what) (subst other) in
895 let cmp2 = compare_terms (subst left) (subst right) in
896 (* cmp1 = Gt && cmp2 = Gt *)
897 cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
898 (* && (ok t s other right ug) *)
900 let metasenv' = metasenv @ newmetas @ newm' in
901 let beta_expand = beta_expand ~metas_ok:false in
902 let cmp1 = compare_terms left right
903 and cmp2 = compare_terms t1 t2 in
904 let res1, res2, res3, res4 =
908 (beta_expand s eq_ty l context metasenv' ugraph)
910 match cmp1, cmp2 with
912 (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
914 [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
916 [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
918 [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
920 let res1 = res left right t1 t2
921 and res2 = res left right t2 t1 in
924 let res3 = res right left t1 t2
925 and res4 = res right left t2 t1 in
928 let res1 = res left right t1 t2
929 and res3 = res right left t1 t2 in
932 let res2 = res left right t2 t1
933 and res4 = res right left t2 t1 in
936 let res1 = res left right t1 t2
937 and res2 = res left right t2 t1
938 and res3 = res right left t1 t2
939 and res4 = res right left t2 t1 in
940 res1, res2, res3, res4
942 let newmetas = newmetas @ newm' in
943 let newargs = args @ args' in
944 let build_new what other is_left eq_URI (t, s, m, ug) =
945 (* let what, other = *)
946 (* if is_left then left, right *)
947 (* else right, left *)
949 let newterm, neweqproof =
951 | C.Lambda (nn, ty, bo) ->
952 let bo' = M.apply_subst s (S.subst other bo) in
955 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
956 if is_left then [bo'; S.lift 1 right]
957 else [S.lift 1 left; bo'])
959 let t' = C.Lambda (nn, ty, bo'') in
962 (C.Appl [C.Const (eq_URI, []); ty; what; t';
963 eqproof; other; eqp'])
966 let newmeta, newequality =
968 if is_left then (newterm, M.apply_subst s right)
969 else (M.apply_subst s left, newterm) in
971 (neweqproof, (eq_ty, left, right), newmetas, newargs)
976 let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
977 and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
978 and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
979 and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
981 | _, (_, left, right), _, _ ->
982 not (fst (CR.are_convertible context left right ugraph))
984 !maxmeta, (List.filter ok (new1 @ new2 @ new3 @ new4))
988 let is_identity ((_, context, ugraph) as env) = function
989 | ((_, (ty, left, right), _, _) as equality) ->
992 (fst (CicReduction.are_convertible context left right ugraph)))
995 (* Printf.printf "is_identity: %s" (string_of_equality ~env equality); *)
996 (* print_newline (); *)
1002 let demodulation newmeta (metasenv, context, ugraph) target source =
1003 let module C = Cic in
1004 let module S = CicSubstitution in
1005 let module M = CicMetaSubst in
1006 let module HL = HelmLibraryObjects in
1007 let module CR = CicReduction in
1009 let proof, (eq_ty, left, right), metas, args = target
1010 and proof', (ty, t1, t2), metas', args' = source in
1012 let compare_terms = !Utils.compare_terms in
1017 let first_step, get_params =
1018 match compare_terms t1 t2 with
1019 | Gt -> 1, (function
1020 | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1021 | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1022 | _ -> assert false)
1023 | Lt -> 1, (function
1024 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1025 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1026 | _ -> assert false)
1028 let first_step = 3 in
1029 let get_params step =
1031 | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1032 | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1033 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1034 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1037 first_step, get_params
1039 let rec demodulate newmeta step metasenv target =
1040 let proof, (eq_ty, left, right), metas, args = target in
1041 let is_left, what, other, eq_URI = get_params step in
1043 let env = metasenv, context, ugraph in
1044 let names = names_of_context context in
1046 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1047 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1048 (* (CicPp.pp other names) (string_of_bool is_left); *)
1049 (* Printf.printf "step: %d" step; *)
1050 (* print_newline (); *)
1052 let ok (t, s, m, ug) =
1053 compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1056 let r = (beta_expand ~metas_ok:false ~match_only:true
1057 what ty (if is_left then left else right)
1058 context (metasenv @ metas) ugraph)
1060 (* let m' = metas_of_term what *)
1061 (* and m'' = metas_of_term (if is_left then left else right) in *)
1062 (* if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1064 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1065 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1066 (* (CicPp.pp other names) (string_of_bool is_left); *)
1067 (* Printf.printf "step: %d" step; *)
1068 (* print_newline (); *)
1069 (* print_endline "res:"; *)
1070 (* List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1071 (* print_newline (); *)
1072 (* Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1073 (* print_newline (); *)
1079 if step = 0 then newmeta, target
1080 else demodulate newmeta (step-1) metasenv target
1081 | (t, s, m, ug)::_ ->
1082 let newterm, newproof =
1084 | C.Lambda (nn, ty, bo) ->
1085 (* let bo' = M.apply_subst s (S.subst other bo) in *)
1086 let bo' = S.subst (M.apply_subst s other) bo in
1089 [C.MutInd (HL.Logic.eq_URI, 0, []);
1091 if is_left then [bo'; S.lift 1 right]
1092 else [S.lift 1 left; bo'])
1094 let t' = C.Lambda (nn, ty, bo'') in
1095 (* M.apply_subst s (S.subst other bo), *)
1098 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1099 proof; other; proof'])
1102 let newmeta, newtarget =
1104 (* if is_left then (newterm, M.apply_subst s right) *)
1105 (* else (M.apply_subst s left, newterm) in *)
1106 if is_left then newterm, right
1109 (* let newmetasenv = metasenv @ metas in *)
1110 (* let newargs = args @ args' in *)
1111 (* fix_metas newmeta *)
1112 (* (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1113 let m = (metas_of_term left) @ (metas_of_term right) in
1114 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1117 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1120 newmeta, (newproof, (eq_ty, left, right), newmetasenv, newargs)
1123 (* "demodulate, newtarget: %s\ntarget was: %s\n" *)
1124 (* (string_of_equality ~env newtarget) *)
1125 (* (string_of_equality ~env target); *)
1126 (* (\* let _, _, newm, newa = newtarget in *\) *)
1127 (* (\* Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1128 (* (\* (print_metasenv newm) *\) *)
1129 (* (\* (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1130 (* print_newline (); *)
1131 if is_identity env newtarget then
1134 demodulate newmeta first_step metasenv newtarget
1136 demodulate newmeta first_step (metasenv @ metas') target
1141 let demodulation newmeta env target source =