1 (** settable by the command line (-i switch) *)
2 let use_index = ref true;;
5 type pos = Left | Right ;;
7 let head_of_term = function
8 | Cic.Appl (hd::tl) -> hd
14 let _, (_, l, r, ordering), _, _ = eq in
15 let hl = head_of_term l in
16 let hr = head_of_term r in
18 let x_entry = try Hashtbl.find table x with Not_found -> [] in
19 Hashtbl.replace table x ((pos, eq)::x_entry)
27 | _ -> index hl Left; index hr Right
35 let remove_index table eq =
36 let _, (_, l, r, ordering), _, _ = eq in
37 let hl = head_of_term l
38 and hr = head_of_term r in
39 let remove_index x pos =
40 let x_entry = try Hashtbl.find table x with Not_found -> [] in
41 let newentry = List.filter (fun e -> e <> (pos, eq)) x_entry in
42 Hashtbl.replace table x newentry
45 remove_index hr Right;
50 let rec find_matches metasenv context ugraph lift_amount term =
52 let module U = Utils in
53 let module S = CicSubstitution in
54 let module M = CicMetaSubst in
55 let module HL = HelmLibraryObjects in
56 let cmp = !Utils.compare_terms in
57 let names = Utils.names_of_context context in
58 (* Printf.printf "CHIAMO find_matches (%s) su: %s\n" *)
59 (* (if unif_fun == Inference.matching then "MATCHING" *)
60 (* else if unif_fun == CicUnification.fo_unif then "UNIFICATION" *)
61 (* else "??????????") *)
62 (* (CicPp.pp term names); *)
66 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
67 let do_match c other eq_URI =
68 (* Printf.printf "provo con %s: %s, %s\n\n" *)
69 (* (if unif_fun == Inference.matching then "MATCHING" *)
70 (* else if unif_fun == CicUnification.fo_unif then "UNIFICATION" *)
71 (* else "??????????") *)
72 (* (CicPp.pp term names) *)
73 (* (CicPp.pp (S.lift lift_amount c) names); *)
74 let subst', metasenv', ugraph' =
75 (* Inference.matching (metasenv @ metas) context term *)
76 (* (S.lift lift_amount c) ugraph *)
77 Inference.matching (metasenv @ metas) context
78 term (S.lift lift_amount c) ugraph
80 (* let names = U.names_of_context context in *)
81 (* Printf.printf "MATCH FOUND: %s, %s\n" *)
82 (* (CicPp.pp term names) (CicPp.pp (S.lift lift_amount c) names); *)
83 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
85 (* (proof, ty, c, other, eq_URI)) *)
87 let c, other, eq_URI =
88 if pos = Left then left, right, HL.Logic.eq_ind_URI
89 else right, left, HL.Logic.eq_ind_r_URI
91 if o <> U.Incomparable then
93 (* print_endline "SONO QUI!"; *)
94 let res = do_match c other eq_URI in
95 (* print_endline "RITORNO RES"; *)
98 (* Printf.printf "ERRORE!: %s\n" (Printexc.to_string e); *)
99 find_matches metasenv context ugraph lift_amount term tl
101 let res = try do_match c other eq_URI with e -> None in
103 | Some (_, s, _, _, _) ->
104 let c' = M.apply_subst s c
105 and other' = M.apply_subst s other in
106 let order = cmp c' other' in
107 let names = U.names_of_context context in
108 (* Printf.printf "c': %s\nother': %s\norder: %s\n\n" *)
109 (* (CicPp.pp c' names) (CicPp.pp other' names) *)
110 (* (U.string_of_comparison order); *)
111 (* if cmp (M.apply_subst s c) (M.apply_subst s other) = U.Gt then *)
115 find_matches metasenv context ugraph lift_amount term tl
117 find_matches metasenv context ugraph lift_amount term tl
121 let get_candidates table term =
123 try Hashtbl.find table (head_of_term term) with Not_found -> []
125 Hashtbl.fold (fun k v l -> v @ l) table []
129 let rec demodulate_term metasenv context ugraph table lift_amount term =
130 let module C = Cic in
131 let module S = CicSubstitution in
132 let module M = CicMetaSubst in
133 let module HL = HelmLibraryObjects in
134 let candidates = get_candidates table term in
139 find_matches metasenv context ugraph lift_amount term candidates
150 (res, tl @ [S.lift 1 t])
153 demodulate_term metasenv context ugraph table
157 | None -> (None, tl @ [S.lift 1 t])
158 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
163 | Some (_, subst, menv, ug, eq_found) ->
164 Some (C.Appl ll, subst, menv, ug, eq_found)
166 | C.Prod (nn, s, t) ->
168 demodulate_term metasenv context ugraph table lift_amount s in (
172 demodulate_term metasenv
173 ((Some (nn, C.Decl s))::context) ugraph
174 table (lift_amount+1) t
178 | Some (t', subst, menv, ug, eq_found) ->
179 Some (C.Prod (nn, (S.lift 1 s), t'),
180 subst, menv, ug, eq_found)
182 | Some (s', subst, menv, ug, eq_found) ->
183 Some (C.Prod (nn, s', (S.lift 1 t)),
184 subst, menv, ug, eq_found)
187 (* Printf.printf "Ne` Appl ne` Prod: %s\n" *)
188 (* (CicPp.pp t (Utils.names_of_context context)); *)
193 let rec demodulation newmeta env table target =
194 let module C = Cic in
195 let module S = CicSubstitution in
196 let module M = CicMetaSubst in
197 let module HL = HelmLibraryObjects in
198 let metasenv, context, ugraph = env in
199 let proof, (eq_ty, left, right, order), metas, args = target in
201 (* let names = Utils.names_of_context context in *)
202 (* Printf.printf "demodulation %s = %s\n" *)
203 (* (CicPp.pp left names) (CicPp.pp right names) *)
205 let metasenv' = metasenv @ metas in
206 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
207 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
208 let what, other = if pos = Left then what, other else other, what in
209 let newterm, newproof =
210 let bo = M.apply_subst subst (S.subst other t) in
212 C.Appl ([C.MutInd (HL.Logic.eq_URI, 0, []);
214 if is_left then [bo; S.lift 1 right] else [S.lift 1 left; bo])
216 let t' = C.Lambda (C.Anonymous, ty, bo'') in
218 M.apply_subst subst (C.Appl [C.Const (eq_URI, []); ty; what; t';
219 proof; other; proof'])
221 let left, right = if is_left then newterm, right else left, newterm in
223 (Inference.metas_of_term left) @ (Inference.metas_of_term right)
225 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
228 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
231 let ordering = !Utils.compare_terms left right in
232 newmeta, (newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
234 let res = demodulate_term metasenv' context ugraph table 0 left in
237 let newmeta, newtarget = build_newtarget true t in
238 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
239 (Inference.meta_convertibility_eq target newtarget) then
242 demodulation newmeta env table newtarget
244 let res = demodulate_term metasenv' context ugraph table 0 right in
247 let newmeta, newtarget = build_newtarget false t in
248 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
249 (Inference.meta_convertibility_eq target newtarget) then
252 demodulation newmeta env table newtarget
258 let rec find_all_matches metasenv context ugraph lift_amount term =
259 let module C = Cic in
260 let module U = Utils in
261 let module S = CicSubstitution in
262 let module M = CicMetaSubst in
263 let module HL = HelmLibraryObjects in
264 let cmp = !Utils.compare_terms in
265 let names = Utils.names_of_context context in
269 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
270 let do_match c other eq_URI =
271 let subst', metasenv', ugraph' =
272 CicUnification.fo_unif (metasenv @ metas) context
273 term (S.lift lift_amount c) ugraph
275 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
278 let c, other, eq_URI =
279 if pos = Left then left, right, HL.Logic.eq_ind_URI
280 else right, left, HL.Logic.eq_ind_r_URI
282 if o <> U.Incomparable then
284 let res = do_match c other eq_URI in
285 res::(find_all_matches metasenv context ugraph lift_amount term tl)
287 find_all_matches metasenv context ugraph lift_amount term tl
290 let res = do_match c other eq_URI in
293 let c' = M.apply_subst s c
294 and other' = M.apply_subst s other in
295 let order = cmp c' other' in
296 let names = U.names_of_context context in
297 if order <> U.Lt && order <> U.Le then
298 res::(find_all_matches metasenv context ugraph
301 find_all_matches metasenv context ugraph lift_amount term tl
303 find_all_matches metasenv context ugraph lift_amount term tl
307 let rec betaexpand_term metasenv context ugraph table lift_amount term =
308 let module C = Cic in
309 let module S = CicSubstitution in
310 let module M = CicMetaSubst in
311 let module HL = HelmLibraryObjects in
312 let candidates = get_candidates table term in
313 let res, lifted_term =
318 (fun arg (res, lifted_tl) ->
321 let arg_res, lifted_arg =
322 betaexpand_term metasenv context ugraph table
326 (fun (t, s, m, ug, eq_found) ->
327 (Some t)::lifted_tl, s, m, ug, eq_found)
332 (fun (l, s, m, ug, eq_found) ->
333 (Some lifted_arg)::l, s, m, ug, eq_found)
335 (Some lifted_arg)::lifted_tl)
338 (fun (r, s, m, ug, eq_found) ->
339 None::r, s, m, ug, eq_found) res,
345 (fun (l, s, m, ug, eq_found) ->
346 (C.Meta (i, l), s, m, ug, eq_found)) l'
348 e, C.Meta (i, lifted_l)
351 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
353 | C.Prod (nn, s, t) ->
355 betaexpand_term metasenv context ugraph table lift_amount s in
357 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
358 table (lift_amount+1) t in
361 (fun (t, s, m, ug, eq_found) ->
362 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
365 (fun (t, s, m, ug, eq_found) ->
366 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
367 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
372 (fun arg (res, lifted_tl) ->
373 let arg_res, lifted_arg =
374 betaexpand_term metasenv context ugraph table lift_amount arg
378 (fun (a, s, m, ug, eq_found) ->
379 a::lifted_tl, s, m, ug, eq_found)
384 (fun (r, s, m, ug, eq_found) ->
385 lifted_arg::r, s, m, ug, eq_found)
387 lifted_arg::lifted_tl)
391 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
394 | t -> [], (S.lift lift_amount t)
397 | C.Meta _ -> res, lifted_term
399 (* let names = Utils.names_of_context context in *)
400 (* Printf.printf "CHIAMO find_all_matches su: %s\n" (CicPp.pp term names); *)
402 find_all_matches metasenv context ugraph lift_amount term candidates
406 (* find_all_matches metasenv context ugraph lift_amount term candidates *)
408 (* | None -> res, lifted_term *)
410 (* r::res, lifted_term *)
414 let superposition_left (metasenv, context, ugraph) table target =
415 let module C = Cic in
416 let module S = CicSubstitution in
417 let module M = CicMetaSubst in
418 let module HL = HelmLibraryObjects in
419 let module CR = CicReduction in
420 let module U = Utils in
421 (* print_endline "superposition_left"; *)
422 let proof, (eq_ty, left, right, ordering), _, _ = target in
424 let term = if ordering = U.Gt then left else right in
425 betaexpand_term metasenv context ugraph table 0 term
427 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
428 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
429 let what, other = if pos = Left then what, other else other, what in
430 let newgoal, newproof =
431 let bo' = M.apply_subst s (S.subst other bo) in
434 [C.MutInd (HL.Logic.eq_URI, 0, []);
436 if ordering = U.Gt then [bo'; S.lift 1 right]
437 else [S.lift 1 left; bo'])
439 let t' = C.Lambda (C.Anonymous, ty, bo'') in
442 (C.Appl [C.Const (eq_URI, []); ty; what; t';
443 proof; other; proof'])
446 if ordering = U.Gt then newgoal, right else left, newgoal in
447 let neworder = !Utils.compare_terms left right in
448 (newproof, (eq_ty, left, right, neworder), [], [])
450 List.map build_new expansions
454 let superposition_right newmeta (metasenv, context, ugraph) table target =
455 let module C = Cic in
456 let module S = CicSubstitution in
457 let module M = CicMetaSubst in
458 let module HL = HelmLibraryObjects in
459 let module CR = CicReduction in
460 let module U = Utils in
461 (* print_endline "superposition_right"; *)
462 let eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
463 let metasenv' = metasenv @ newmetas in
464 let maxmeta = ref newmeta in
467 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
468 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
472 (fun (_, subst, _, _, _) ->
473 let subst = M.apply_subst subst in
474 let o = !Utils.compare_terms (subst l) (subst r) in
475 o <> U.Lt && o <> U.Le)
476 (fst (betaexpand_term metasenv' context ugraph table 0 l))
478 (res left right), (res right left)
480 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
481 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
482 let what, other = if pos = Left then what, other else other, what in
483 let newgoal, newproof =
484 let bo' = M.apply_subst s (S.subst other bo) in
487 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
488 if ordering = U.Gt then [bo'; S.lift 1 right]
489 else [S.lift 1 left; bo'])
491 let t' = C.Lambda (C.Anonymous, ty, bo'') in
494 (C.Appl [C.Const (eq_URI, []); ty; what; t';
495 eqproof; other; proof'])
497 let newmeta, newequality =
499 if ordering = U.Gt then newgoal, M.apply_subst s right
500 else M.apply_subst s left, newgoal in
501 let neworder = !Utils.compare_terms left right
502 and newmenv = newmetas @ menv'
503 and newargs = args @ args' in
504 let eq' = (newproof, (eq_ty, left, right, neworder), newmenv, newargs)
505 and env = (metasenv, context, ugraph) in
506 (* Printf.printf "eq' prima di fix_metas: %s\n" *)
507 (* (Inference.string_of_equality eq' ~env); *)
508 let newm, eq' = Inference.fix_metas !maxmeta eq' in
509 (* Printf.printf "eq' dopo fix_metas: %s\n" *)
510 (* (Inference.string_of_equality eq' ~env); *)
516 let new1 = List.map (build_new U.Gt) res1
517 and new2 = List.map (build_new U.Lt) res2 in
519 | _, (_, left, right, _), _, _ ->
520 not (fst (CR.are_convertible context left right ugraph))
523 (List.filter ok (new1 @ new2)))