4 let string_of_equality ?env =
8 | _, (ty, left, right, o), _, _ ->
9 Printf.sprintf "{%s}: %s =(%s) %s" (CicPp.ppterm ty)
10 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
12 | Some (_, context, _) -> (
13 let names = names_of_context context in
15 | _, (ty, left, right, o), _, _ ->
16 Printf.sprintf "{%s}: %s =(%s) %s" (CicPp.pp ty names)
17 (CicPp.pp left names) (string_of_comparison o)
18 (CicPp.pp right names)
23 let rec metas_of_term = function
24 | Cic.Meta (i, c) -> [i]
27 | Cic.MutInd (_, _, ens)
28 | Cic.MutConstruct (_, _, _, ens) ->
29 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
32 | Cic.Lambda (_, s, t)
33 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
34 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
35 | Cic.MutCase (uri, i, s, t, l) ->
36 (metas_of_term s) @ (metas_of_term t) @
37 (List.flatten (List.map metas_of_term l))
40 (List.map (fun (s, i, t1, t2) ->
41 (metas_of_term t1) @ (metas_of_term t2)) il)
42 | Cic.CoFix (i, il) ->
44 (List.map (fun (s, t1, t2) ->
45 (metas_of_term t1) @ (metas_of_term t2)) il)
50 exception NotMetaConvertible;;
52 let meta_convertibility_aux table t1 t2 =
57 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
59 let rec aux ((table_l, table_r) as table) t1 t2 =
60 (* Printf.printf "aux %s, %s\ntable_l: %s, table_r: %s\n" *)
61 (* (CicPp.ppterm t1) (CicPp.ppterm t2) *)
62 (* (print_table table_l) (print_table table_r); *)
64 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
65 let m1_binding, table_l =
66 try List.assoc m1 table_l, table_l
67 with Not_found -> m2, (m1, m2)::table_l
68 and m2_binding, table_r =
69 try List.assoc m2 table_r, table_r
70 with Not_found -> m1, (m2, m1)::table_r
72 (* let m1_binding, m2_binding, table = *)
73 (* let m1b, table = *)
74 (* try List.assoc m1 table, table *)
75 (* with Not_found -> m2, (m1, m2)::table *)
77 (* let m2b, table = *)
78 (* try List.assoc m2 table, table *)
79 (* with Not_found -> m1, (m2, m1)::table *)
83 (* Printf.printf "table_l: %s\ntable_r: %s\n\n" *)
84 (* (print_table table_l) (print_table table_r); *)
85 if (m1_binding <> m2) || (m2_binding <> m1) then
86 raise NotMetaConvertible
92 | None, Some _ | Some _, None -> raise NotMetaConvertible
94 | Some t1, Some t2 -> (aux res t1 t2))
95 (table_l, table_r) tl1 tl2
96 with Invalid_argument _ ->
97 raise NotMetaConvertible
99 | C.Var (u1, ens1), C.Var (u2, ens2)
100 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
101 aux_ens table ens1 ens2
102 | C.Cast (s1, t1), C.Cast (s2, t2)
103 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
104 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
105 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
106 let table = aux table s1 s2 in
108 | C.Appl l1, C.Appl l2 -> (
109 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
110 with Invalid_argument _ -> raise NotMetaConvertible
112 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
113 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
114 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
115 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
116 aux_ens table ens1 ens2
117 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
118 when (UriManager.eq u1 u2) && i1 = i2 ->
119 let table = aux table s1 s2 in
120 let table = aux table t1 t2 in (
121 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
122 with Invalid_argument _ -> raise NotMetaConvertible
124 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
127 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
128 if i1 <> i2 then raise NotMetaConvertible
130 let res = (aux res s1 s2) in aux res t1 t2)
132 with Invalid_argument _ -> raise NotMetaConvertible
134 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
137 (fun res (n1, s1, t1) (n2, s2, t2) ->
138 let res = aux res s1 s2 in aux res t1 t2)
140 with Invalid_argument _ -> raise NotMetaConvertible
142 | t1, t2 when t1 = t2 -> table
143 | _, _ -> raise NotMetaConvertible
145 and aux_ens table ens1 ens2 =
146 let cmp (u1, t1) (u2, t2) =
147 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
149 let ens1 = List.sort cmp ens1
150 and ens2 = List.sort cmp ens2 in
153 (fun res (u1, t1) (u2, t2) ->
154 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
157 with Invalid_argument _ -> raise NotMetaConvertible
163 let meta_convertibility_eq eq1 eq2 =
164 let _, (ty, left, right, _), _, _ = eq1
165 and _, (ty', left', right', _), _, _ = eq2 in
168 else if (left = left') && (right = right') then
170 else if (left = right') && (right = left') then
174 let table = meta_convertibility_aux ([], []) left left' in
175 let _ = meta_convertibility_aux table right right' in
177 with NotMetaConvertible ->
179 let table = meta_convertibility_aux ([], []) left right' in
180 let _ = meta_convertibility_aux table right left' in
182 with NotMetaConvertible ->
187 let meta_convertibility t1 t2 =
191 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
197 let l, r = meta_convertibility_aux ([], []) t1 t2 in
198 (* Printf.printf "meta_convertibility:\n%s\n%s\n\n" (f l) (f r); *)
200 with NotMetaConvertible ->
205 let replace_metas (* context *) term =
206 let module C = Cic in
207 let rec aux = function
210 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
212 (* if c = irl then *)
213 (* C.Implicit (Some (`MetaIndex i)) *)
215 (* Printf.printf "WARNING: c non e` un identity_relocation_list!\n%s\n" *)
216 (* (String.concat "\n" *)
218 (* (function None -> "" | Some t -> CicPp.ppterm t) c)); *)
221 C.Implicit (Some (`MetaInfo (i, c)))
222 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
223 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
224 | C.Cast (s, t) -> C.Cast (aux s, aux t)
225 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
226 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
227 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
228 | C.Appl l -> C.Appl (List.map aux l)
229 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
230 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
231 | C.MutCase (uri, i, s, t, l) ->
232 C.MutCase (uri, i, aux s, aux t, List.map aux l)
235 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
239 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
243 List.map (fun (u, t) -> (u, aux t)) ens
249 let restore_metas (* context *) term =
250 let module C = Cic in
251 let rec aux = function
252 | C.Implicit (Some (`MetaInfo (i, c))) ->
254 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
257 (* let local_context:(C.term option) list = *)
258 (* Marshal.from_string mc 0 *)
260 (* C.Meta (i, local_context) *)
262 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
263 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
264 | C.Cast (s, t) -> C.Cast (aux s, aux t)
265 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
266 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
267 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
268 | C.Appl l -> C.Appl (List.map aux l)
269 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
270 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
271 | C.MutCase (uri, i, s, t, l) ->
272 C.MutCase (uri, i, aux s, aux t, List.map aux l)
275 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
279 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
283 List.map (fun (u, t) -> (u, aux t)) ens
289 let rec restore_subst (* context *) subst =
291 (fun (i, (c, t, ty)) ->
292 i, (c, restore_metas (* context *) t, ty))
297 exception MatchingFailure;;
299 let matching metasenv context t1 t2 ugraph =
301 let subst, metasenv, ugraph =
302 CicUnification.fo_unif metasenv context t1 t2 ugraph
304 let t' = CicMetaSubst.apply_subst subst t1 in
305 if not (meta_convertibility t1 t') then
306 raise MatchingFailure
308 let metas = metas_of_term t1 in
309 let fix_subst = function
310 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
311 (j, (c, Cic.Meta (i, lc), ty))
314 let subst = List.map fix_subst subst in
315 subst, metasenv, ugraph
317 raise MatchingFailure
321 let beta_expand ?(metas_ok=true) ?(match_only=false)
322 what type_of_what where context metasenv ugraph =
323 let module S = CicSubstitution in
324 let module C = Cic in
326 let print_info = false in
329 (* let names = names_of_context context in *)
330 (* Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
331 (* (CicPp.pp what names) (CicPp.ppterm what) *)
332 (* (CicPp.pp where names) (CicPp.ppterm where); *)
333 (* print_newline (); *)
337 ((list of all possible beta expansions, subst, metasenv, ugraph),
340 let rec aux lift_amount term context metasenv subst ugraph =
341 (* Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
342 let res, lifted_term =
345 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
347 | C.Var (uri, exp_named_subst) ->
348 let ens', lifted_ens =
349 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
353 (fun (e, s, m, ug) ->
354 (C.Var (uri, e), s, m, ug)) ens'
356 expansions, C.Var (uri, lifted_ens)
361 (fun arg (res, lifted_tl) ->
364 let arg_res, lifted_arg =
365 aux lift_amount arg context metasenv subst ugraph in
368 (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
373 (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
375 (Some lifted_arg)::lifted_tl)
378 (fun (r, s, m, ug) -> None::r, s, m, ug)
385 (fun (l, s, m, ug) ->
386 (C.Meta (i, l), s, m, ug)) l'
388 e, C.Meta (i, lifted_l)
391 | C.Implicit _ as t -> [], t
395 aux lift_amount s context metasenv subst ugraph in
397 aux lift_amount t context metasenv subst ugraph
401 (fun (t, s, m, ug) ->
402 C.Cast (t, lifted_t), s, m, ug) l1 in
405 (fun (t, s, m, ug) ->
406 C.Cast (lifted_s, t), s, m, ug) l2 in
407 l1'@l2', C.Cast (lifted_s, lifted_t)
409 | C.Prod (nn, s, t) ->
411 aux lift_amount s context metasenv subst ugraph in
413 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
414 metasenv subst ugraph
418 (fun (t, s, m, ug) ->
419 C.Prod (nn, t, lifted_t), s, m, ug) l1 in
422 (fun (t, s, m, ug) ->
423 C.Prod (nn, lifted_s, t), s, m, ug) l2 in
424 l1'@l2', C.Prod (nn, lifted_s, lifted_t)
426 | C.Lambda (nn, s, t) ->
428 aux lift_amount s context metasenv subst ugraph in
430 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
431 metasenv subst ugraph
435 (fun (t, s, m, ug) ->
436 C.Lambda (nn, t, lifted_t), s, m, ug) l1 in
439 (fun (t, s, m, ug) ->
440 C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
441 l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
443 | C.LetIn (nn, s, t) ->
445 aux lift_amount s context metasenv subst ugraph in
447 aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
448 metasenv subst ugraph
452 (fun (t, s, m, ug) ->
453 C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
456 (fun (t, s, m, ug) ->
457 C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
458 l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
462 aux_list lift_amount l context metasenv subst ugraph
464 (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
467 | C.Const (uri, exp_named_subst) ->
468 let ens', lifted_ens =
469 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
473 (fun (e, s, m, ug) ->
474 (C.Const (uri, e), s, m, ug)) ens'
476 (expansions, C.Const (uri, lifted_ens))
478 | C.MutInd (uri, i ,exp_named_subst) ->
479 let ens', lifted_ens =
480 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
484 (fun (e, s, m, ug) ->
485 (C.MutInd (uri, i, e), s, m, ug)) ens'
487 (expansions, C.MutInd (uri, i, lifted_ens))
489 | C.MutConstruct (uri, i, j, exp_named_subst) ->
490 let ens', lifted_ens =
491 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
495 (fun (e, s, m, ug) ->
496 (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
498 (expansions, C.MutConstruct (uri, i, j, lifted_ens))
500 | C.MutCase (sp, i, outt, t, pl) ->
501 let pl_res, lifted_pl =
502 aux_list lift_amount pl context metasenv subst ugraph
504 let l1, lifted_outt =
505 aux lift_amount outt context metasenv subst ugraph in
507 aux lift_amount t context metasenv subst ugraph in
511 (fun (outt, s, m, ug) ->
512 C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
515 (fun (t, s, m, ug) ->
516 C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
519 (fun (pl, s, m, ug) ->
520 C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
522 (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
525 let len = List.length fl in
528 (fun (nm, idx, 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, idx, lifted_ty, a)::lifted_tl, s, m, ug)
540 (fun (r, s, m, ug) ->
541 (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
542 (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
546 (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
547 C.Fix (i, lifted_fl))
550 let len = List.length fl in
553 (fun (nm, ty, bo) (res, lifted_tl) ->
554 let lifted_ty = S.lift lift_amount ty in
555 let bo_res, lifted_bo =
556 aux (lift_amount+len) bo context metasenv subst ugraph in
559 (fun (a, s, m, ug) ->
560 (nm, lifted_ty, a)::lifted_tl, s, m, ug)
565 (fun (r, s, m, ug) ->
566 (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
567 (nm, lifted_ty, lifted_bo)::lifted_tl)
571 (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
572 C.CoFix (i, lifted_fl))
576 | C.Meta _ when (not metas_ok) ->
580 (* if match_only then replace_metas context term *)
584 let subst', metasenv', ugraph' =
585 (* Printf.printf "provo a unificare %s e %s\n" *)
586 (* (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
588 matching metasenv context term (S.lift lift_amount what) ugraph
590 CicUnification.fo_unif metasenv context
591 (S.lift lift_amount what) term ugraph
593 (* Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
594 (* (CicPp.ppterm (S.lift lift_amount what)); *)
595 (* Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
596 (* Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
597 (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
598 (* if match_only then *)
599 (* let t' = CicMetaSubst.apply_subst subst' term in *)
600 (* if not (meta_convertibility term t') then ( *)
601 (* res, lifted_term *)
603 (* let metas = metas_of_term term in *)
604 (* let fix_subst = function *)
605 (* | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas -> *)
606 (* (j, (c, C.Meta (i, lc), ty)) *)
609 (* let subst' = List.map fix_subst subst' in *)
610 (* ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res, *)
614 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
618 print_endline ("beta_expand ERROR!: " ^ (Printexc.to_string e));
622 (* Printf.printf "exit aux\n"; *)
625 and aux_list lift_amount l context metasenv subst ugraph =
627 (fun arg (res, lifted_tl) ->
628 let arg_res, lifted_arg =
629 aux lift_amount arg context metasenv subst ugraph in
631 (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
634 (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
635 lifted_arg::lifted_tl)
638 and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
640 (fun (u, arg) (res, lifted_tl) ->
641 let arg_res, lifted_arg =
642 aux lift_amount arg context metasenv subst ugraph in
645 (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
647 (l1 @ (List.map (fun (r, s, m, ug) ->
648 (u, lifted_arg)::r, s, m, ug) res),
649 (u, lifted_arg)::lifted_tl)
650 ) exp_named_subst ([], [])
655 (* if match_only then replace_metas (\* context *\) where *)
659 Printf.printf "searching %s inside %s\n"
660 (CicPp.ppterm what) (CicPp.ppterm where);
662 aux 0 where context metasenv [] ugraph
665 (* if match_only then *)
666 (* (fun (term, subst, metasenv, ugraph) -> *)
668 (* C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
669 (* and subst = restore_subst subst in *)
670 (* (term', subst, metasenv, ugraph)) *)
672 (fun (term, subst, metasenv, ugraph) ->
673 let term' = C.Lambda (C.Anonymous, type_of_what, term) in
674 (term', subst, metasenv, ugraph))
676 List.map mapfun expansions
681 Cic.term * (* proof *)
682 (Cic.term * (* type *)
683 Cic.term * (* left side *)
684 Cic.term * (* right side *)
685 Utils.comparison) * (* ordering *)
686 Cic.metasenv * (* environment for metas *)
687 Cic.term list (* arguments *)
691 let find_equalities ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) context proof =
692 let module C = Cic in
693 let module S = CicSubstitution in
694 let module T = CicTypeChecker in
695 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
696 let rec aux index newmeta = function
698 | (Some (_, C.Decl (term)))::tl ->
699 let do_find context term =
701 | C.Prod (name, s, t) ->
702 (* let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
703 let (head, newmetas, args, _) =
704 PrimitiveTactics.new_metasenv_for_apply newmeta proof
705 context (S.lift index term)
711 | C.Meta (i, _) -> (max maxm i)
716 if List.length args = 0 then
719 C.Appl ((C.Rel index)::args)
722 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
723 Printf.printf "OK: %s\n" (CicPp.ppterm term);
724 let o = !Utils.compare_terms t1 t2 in
725 Some (p, (ty, t1, t2, o), newmetas, args), (newmeta+1)
728 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
729 let t1 = S.lift index t1
730 and t2 = S.lift index t2 in
731 let o = !Utils.compare_terms t1 t2 in
732 Some (C.Rel index, (ty, t1, t2, o), [], []), (newmeta+1)
735 match do_find context term with
737 let tl, newmeta' = (aux (index+1) newmeta tl) in
738 p::tl, max newmeta newmeta'
740 aux (index+1) newmeta tl
743 aux (index+1) newmeta tl
745 aux 1 newmeta context
749 let fix_metas newmeta ((proof, (ty, left, right, o), menv, args) as equality) =
750 let table = Hashtbl.create (List.length args) in
753 (fun t (newargs, index) ->
756 Hashtbl.add table i index;
757 ((Cic.Meta (index, l))::newargs, index+1)
762 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
767 (fun (i, context, term) menv ->
769 let index = Hashtbl.find table i in
770 (index, context, term)::menv
772 (i, context, term)::menv)
777 and right = repl right in
778 let metas = (metas_of_term left) @ (metas_of_term right) in
779 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv'
782 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
784 (newmeta + (List.length newargs) + 1,
785 (repl proof, (ty, left, right, o), menv', newargs))
789 exception TermIsNotAnEquality;;
791 let equality_of_term ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) proof = function
792 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when uri = eq_uri ->
793 let o = !Utils.compare_terms t1 t2 in
794 (proof, (ty, t1, t2, o), [], [])
796 raise TermIsNotAnEquality
800 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
803 let superposition_left (metasenv, context, ugraph) target source =
804 let module C = Cic in
805 let module S = CicSubstitution in
806 let module M = CicMetaSubst in
807 let module HL = HelmLibraryObjects in
808 let module CR = CicReduction in
809 (* we assume that target is ground (does not contain metavariables): this
810 * should always be the case (I hope, at least) *)
811 let proof, (eq_ty, left, right, t_order), _, _ = target in
812 let eqproof, (ty, t1, t2, s_order), newmetas, args = source in
814 let compare_terms = !Utils.compare_terms in
820 match t_order (* compare_terms left right *) with
824 Printf.printf "????????? %s = %s" (CicPp.ppterm left)
825 (CicPp.ppterm right);
827 assert false (* again, for ground terms this shouldn't happen... *)
830 let metasenv' = newmetas @ metasenv in
831 let result = s_order (* compare_terms t1 t2 *) in
834 | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
835 | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
839 (fun (t, s, m, ug) ->
840 compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
841 (beta_expand t1 ty where context metasenv' ugraph)
844 (fun (t, s, m, ug) ->
845 compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
846 (beta_expand t2 ty where context metasenv' ugraph)
850 (* let what, other = *)
851 (* if is_left then left, right *)
852 (* else right, left *)
854 let build_new what other eq_URI (t, s, m, ug) =
855 let newgoal, newgoalproof =
857 | C.Lambda (nn, ty, bo) ->
858 let bo' = S.subst (M.apply_subst s other) bo in
861 [C.MutInd (HL.Logic.eq_URI, 0, []);
863 if is_left then [bo'; S.lift 1 right]
864 else [S.lift 1 left; bo'])
866 let t' = C.Lambda (nn, ty, bo'') in
867 S.subst (M.apply_subst s other) bo,
869 (C.Appl [C.Const (eq_URI, []); ty; what; t';
870 proof; other; eqproof])
874 if is_left then (eq_ty, newgoal, right, compare_terms newgoal right)
875 else (eq_ty, left, newgoal, compare_terms left newgoal)
877 (newgoalproof (* eqproof *), equation, [], [])
879 let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
880 and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
885 let superposition_right newmeta (metasenv, context, ugraph) target source =
886 let module C = Cic in
887 let module S = CicSubstitution in
888 let module M = CicMetaSubst in
889 let module HL = HelmLibraryObjects in
890 let module CR = CicReduction in
891 let eqproof, (eq_ty, left, right, t_order), newmetas, args = target in
892 let eqp', (ty', t1, t2, s_order), newm', args' = source in
893 let maxmeta = ref newmeta in
895 let compare_terms = !Utils.compare_terms in
900 (* let ok term subst other other_eq_side ugraph = *)
901 (* match term with *)
902 (* | C.Lambda (nn, ty, bo) -> *)
903 (* let bo' = S.subst (M.apply_subst subst other) bo in *)
904 (* let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
906 (* | _ -> assert false *)
908 let condition left right what other (t, s, m, ug) =
909 let subst = M.apply_subst s in
910 let cmp1 = compare_terms (subst what) (subst other) in
911 let cmp2 = compare_terms (subst left) (subst right) in
912 (* cmp1 = Gt && cmp2 = Gt *)
913 cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
914 (* && (ok t s other right ug) *)
916 let metasenv' = metasenv @ newmetas @ newm' in
917 let beta_expand = beta_expand ~metas_ok:false in
918 let cmp1 = t_order (* compare_terms left right *)
919 and cmp2 = s_order (* compare_terms t1 t2 *) in
920 let res1, res2, res3, res4 =
924 (beta_expand s eq_ty l context metasenv' ugraph)
926 match cmp1, cmp2 with
928 (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
930 [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
932 [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
934 [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
936 let res1 = res left right t1 t2
937 and res2 = res left right t2 t1 in
940 let res3 = res right left t1 t2
941 and res4 = res right left t2 t1 in
944 let res1 = res left right t1 t2
945 and res3 = res right left t1 t2 in
948 let res2 = res left right t2 t1
949 and res4 = res right left t2 t1 in
952 let res1 = res left right t1 t2
953 and res2 = res left right t2 t1
954 and res3 = res right left t1 t2
955 and res4 = res right left t2 t1 in
956 res1, res2, res3, res4
958 let newmetas = newmetas @ newm' in
959 let newargs = args @ args' in
960 let build_new what other is_left eq_URI (t, s, m, ug) =
961 (* let what, other = *)
962 (* if is_left then left, right *)
963 (* else right, left *)
965 let newterm, neweqproof =
967 | C.Lambda (nn, ty, bo) ->
968 let bo' = M.apply_subst s (S.subst other bo) in
971 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
972 if is_left then [bo'; S.lift 1 right]
973 else [S.lift 1 left; bo'])
975 let t' = C.Lambda (nn, ty, bo'') in
978 (C.Appl [C.Const (eq_URI, []); ty; what; t';
979 eqproof; other; eqp'])
982 let newmeta, newequality =
984 if is_left then (newterm, M.apply_subst s right)
985 else (M.apply_subst s left, newterm) in
986 let neworder = compare_terms left right in
988 (neweqproof, (eq_ty, left, right, neworder), newmetas, newargs)
993 let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
994 and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
995 and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
996 and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
998 | _, (_, left, right, _), _, _ ->
999 not (fst (CR.are_convertible context left right ugraph))
1002 (List.filter ok (new1 @ new2 @ new3 @ new4)))
1006 let is_identity ((_, context, ugraph) as env) = function
1007 | ((_, (ty, left, right, _), _, _) as equality) ->
1010 (fst (CicReduction.are_convertible context left right ugraph)))
1013 (* Printf.printf "is_identity: %s" (string_of_equality ~env equality); *)
1014 (* print_newline (); *)
1020 let demodulation newmeta (metasenv, context, ugraph) target source =
1021 let module C = Cic in
1022 let module S = CicSubstitution in
1023 let module M = CicMetaSubst in
1024 let module HL = HelmLibraryObjects in
1025 let module CR = CicReduction in
1027 let proof, (eq_ty, left, right, t_order), metas, args = target
1028 and proof', (ty, t1, t2, s_order), metas', args' = source in
1030 let compare_terms = !Utils.compare_terms in
1035 let first_step, get_params =
1036 match s_order (* compare_terms t1 t2 *) with
1037 | Gt -> 1, (function
1038 | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1039 | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1040 | _ -> assert false)
1041 | Lt -> 1, (function
1042 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1043 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1044 | _ -> assert false)
1046 let first_step = 3 in
1047 let get_params step =
1049 | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1050 | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1051 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1052 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1055 first_step, get_params
1057 let rec demodulate newmeta step metasenv target =
1058 let proof, (eq_ty, left, right, t_order), metas, args = target in
1059 let is_left, what, other, eq_URI = get_params step in
1061 let env = metasenv, context, ugraph in
1062 let names = names_of_context context in
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 (); *)
1070 let ok (t, s, m, ug) =
1071 compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1074 let r = (beta_expand ~metas_ok:false ~match_only:true
1075 what ty (if is_left then left else right)
1076 context (metasenv @ metas) ugraph)
1078 (* let m' = metas_of_term what *)
1079 (* and m'' = metas_of_term (if is_left then left else right) in *)
1080 (* if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1082 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1083 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1084 (* (CicPp.pp other names) (string_of_bool is_left); *)
1085 (* Printf.printf "step: %d" step; *)
1086 (* print_newline (); *)
1087 (* print_endline "res:"; *)
1088 (* List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1089 (* print_newline (); *)
1090 (* Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1091 (* print_newline (); *)
1097 if step = 0 then newmeta, target
1098 else demodulate newmeta (step-1) metasenv target
1099 | (t, s, m, ug)::_ ->
1100 let newterm, newproof =
1102 | C.Lambda (nn, ty, bo) ->
1103 (* let bo' = M.apply_subst s (S.subst other bo) in *)
1104 let bo' = S.subst (M.apply_subst s other) bo in
1107 [C.MutInd (HL.Logic.eq_URI, 0, []);
1109 if is_left then [bo'; S.lift 1 right]
1110 else [S.lift 1 left; bo'])
1112 let t' = C.Lambda (nn, ty, bo'') in
1113 (* M.apply_subst s (S.subst other bo), *)
1116 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1117 proof; other; proof'])
1120 let newmeta, newtarget =
1122 (* if is_left then (newterm, M.apply_subst s right) *)
1123 (* else (M.apply_subst s left, newterm) in *)
1124 if is_left then newterm, right
1127 let neworder = compare_terms left right in
1128 (* let newmetasenv = metasenv @ metas in *)
1129 (* let newargs = args @ args' in *)
1130 (* fix_metas newmeta *)
1131 (* (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1132 let m = (metas_of_term left) @ (metas_of_term right) in
1133 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1136 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1140 (newproof, (eq_ty, left, right, neworder), newmetasenv, newargs)
1143 (* "demodulate, newtarget: %s\ntarget was: %s\n" *)
1144 (* (string_of_equality ~env newtarget) *)
1145 (* (string_of_equality ~env target); *)
1146 (* (\* let _, _, newm, newa = newtarget in *\) *)
1147 (* (\* Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1148 (* (\* (print_metasenv newm) *\) *)
1149 (* (\* (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1150 (* print_newline (); *)
1151 if is_identity env newtarget then
1154 demodulate newmeta first_step metasenv newtarget
1156 demodulate newmeta first_step (metasenv @ metas') target
1161 let demodulation newmeta env target source =
1167 let subsumption env target source =
1168 let _, (ty, tl, tr, _), tmetas, _ = target
1169 and _, (ty', sl, sr, _), smetas, _ = source in
1173 let metasenv, context, ugraph = env in
1174 let metasenv = metasenv @ tmetas @ smetas in
1175 let names = names_of_context context in
1176 let samesubst subst subst' =
1177 (* Printf.printf "samesubst:\nsubst: %s\nsubst': %s\n" *)
1178 (* (print_subst subst) (print_subst subst'); *)
1179 (* print_newline (); *)
1180 let tbl = Hashtbl.create (List.length subst) in
1181 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
1183 (fun (m, (c, t1, t2)) ->
1185 let c', t1', t2' = Hashtbl.find tbl m in
1186 if (c = c') && (t1 = t1') && (t2 = t2') then true
1192 let subsaux left right left' right' =
1194 let subst, menv, ug = matching metasenv context left left' ugraph
1195 and subst', menv', ug' = matching metasenv context right right' ugraph
1197 (* Printf.printf "left = right: %s = %s\n" *)
1198 (* (CicPp.pp left names) (CicPp.pp right names); *)
1199 (* Printf.printf "left' = right': %s = %s\n" *)
1200 (* (CicPp.pp left' names) (CicPp.pp right' names); *)
1201 samesubst subst subst'
1203 (* print_endline (Printexc.to_string e); *)
1207 if subsaux tl tr sl sr then true
1208 else subsaux tl tr sr sl
1211 Printf.printf "subsumption!:\ntarget: %s\nsource: %s\n"
1212 (string_of_equality ~env target) (string_of_equality ~env source);