2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* $Id: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
14 type eq_sig_type = Eq | EqInd_l | EqInd_r | Refl
16 let eqsig = ref (fun _ -> assert false);;
17 let set_sig f = eqsig:= f;;
18 let get_sig = fun x -> !eqsig x;;
20 let default_sig = function
22 let uri = NUri.uri_of_string "cic:/matita/ng/Plogic/equality/eq.ind" in
23 let ref = NReference.reference_of_spec uri (NReference.Ind(true,0,2)) in
26 let uri = NUri.uri_of_string "cic:/matita/ng/Plogic/equality/rewrite_l.con" in
27 let ref = NReference.reference_of_spec uri (NReference.Def(1)) in
30 let uri = NUri.uri_of_string "cic:/matita/ng/Plogic/equality/rewrite_r.con" in
31 let ref = NReference.reference_of_spec uri (NReference.Def(3)) in
34 let uri = NUri.uri_of_string "cic:/matita/ng/Plogic/equality/eq.ind" in
35 let ref = NReference.reference_of_spec uri (NReference.Con(0,1,2)) in
38 let set_default_sig () =
39 (*prerr_endline "setting default sig";*)
42 let set_reference_of_oxuri reference_of_oxuri =
43 prerr_endline "setting oxuri in nCicProof";
48 (UriManager.uri_of_string
49 "cic:/matita/logic/equality/eq.ind#xpointer(1/1)"))
53 (UriManager.uri_of_string
54 "cic:/matita/logic/equality/eq_ind.con"))
58 (UriManager.uri_of_string
59 "cic:/matita/logic/equality/eq_elim_r.con"))
63 (UriManager.uri_of_string
64 "cic:/matita/logic/equality/eq.ind#xpointer(1/1/1)"))
68 (* let debug c r = prerr_endline r; c *)
71 let eqP() = debug (!eqsig Eq) "eq" ;;
72 let eq_ind() = debug (!eqsig EqInd_l) "eq_ind" ;;
73 let eq_ind_r() = debug (!eqsig EqInd_r) "eq_ind_r";;
74 let eq_refl() = debug (!eqsig Refl) "refl";;
77 let extract lift vl t =
78 let rec pos i = function
79 | [] -> raise Not_found
80 | j :: tl when j <> i -> 1+ pos i tl
83 let vl_len = List.length vl in
84 let rec extract = function
85 | Terms.Leaf x -> NCicSubstitution.lift (vl_len+lift) x
87 (try NCic.Rel (pos j vl) with Not_found -> NCic.Implicit `Term)
88 | Terms.Node l -> NCic.Appl (List.map extract l)
94 let mk_predicate hole_type amount ft p1 vl =
102 let module NCicBlob = NCicBlob.NCicBlob(
104 let metasenv = [] let subst = [] let context = []
107 let module Pp = Pp.Pp(NCicBlob) in
108 prerr_endline ("term: " ^ Pp.pp_foterm ft);
109 prerr_endline ("path: " ^ String.concat ","
110 (List.map string_of_int p1));
111 prerr_endline ("leading to: " ^ Pp.pp_foterm t);
117 if i = n then aux t tl
118 else extract amount (0::vl) t)
123 NCic.Lambda("x", hole_type, aux ft (List.rev p1))
127 let uri = NUri.uri_of_string "cic:/matita/ng/sets/setoids/prop1.con" in
128 let ref = NReference.reference_of_spec uri (NReference.Fix(0,2,4)) in
134 let uri = NUri.uri_of_string "cic:/matita/ng/sets/setoids/eq.con" in
135 let ref = NReference.reference_of_spec uri (NReference.Fix(0,0,2)) in
141 let u= NUri.uri_of_string "cic:/matita/ng/properties/relations/sym.con" in
142 let u = NReference.reference_of_spec u (NReference.Fix(0,1,3)) in
143 NCic.Appl[NCic.Const u; NCic.Implicit `Type; NCic.Implicit `Term;
144 NCic.Implicit `Term; NCic.Implicit `Term; eq];
147 let eq_morphism1 eq =
148 let u= NUri.uri_of_string "cic:/matita/ng/sets/setoids/eq_is_morphism1.con" in
149 let u = NReference.reference_of_spec u (NReference.Def 4) in
150 NCic.Appl[NCic.Const u; NCic.Implicit `Term; NCic.Implicit `Term;
151 NCic.Implicit `Term; NCic.Implicit `Term; eq];
154 let eq_morphism2 eq =
155 let u= NUri.uri_of_string "cic:/matita/ng/sets/setoids/eq_is_morphism2.con" in
156 let u = NReference.reference_of_spec u (NReference.Def 4) in
157 NCic.Appl[NCic.Const u; NCic.Implicit `Term; NCic.Implicit `Term;
158 NCic.Implicit `Term; eq; NCic.Implicit `Term];
162 let u= NUri.uri_of_string "cic:/matita/ng/properties/relations/trans.con" in
163 let u = NReference.reference_of_spec u (NReference.Fix(0,1,3)) in
164 NCic.Appl[NCic.Const u; NCic.Implicit `Type; NCic.Implicit `Term;
165 NCic.Implicit `Term; NCic.Implicit `Term; NCic.Implicit `Term; eq]
169 let uri = NUri.uri_of_string "cic:/matita/ng/logic/connectives/if.con" in
170 let ref = NReference.reference_of_spec uri (NReference.Fix(0,2,1)) in
171 NCic.Appl[NCic.Const ref; NCic.Implicit `Type; NCic.Implicit `Type;
176 let mk_refl = function
177 | NCic.Appl [_; _; x; _] ->
178 let uri= NUri.uri_of_string "cic:/matita/ng/properties/relations/refl.con" in
179 let ref = NReference.reference_of_spec uri (NReference.Fix(0,1,3)) in
180 NCic.Appl[NCic.Const ref; NCic.Implicit `Type; NCic.Implicit `Term;
181 NCic.Implicit `Term(*x*)]
185 let mk_refl = function
186 | NCic.Appl [_; ty; l; _]
187 -> NCic.Appl [eq_refl();ty;l]
191 let mk_morphism eq amount ft pl vl =
196 prerr_endline (string_of_int n);
199 | Terms.Var _ -> assert false
200 | Terms.Node [] -> assert false
201 | Terms.Node [ Terms.Leaf eqt ; _; l; r ]
202 when (eqP ()) = eqt ->
203 if n=2 then eq_morphism1 (aux l tl)
204 else eq_morphism2 (aux r tl)
205 | Terms.Node (f::l) ->
210 let f = extract amount vl f in
212 let imp = NCic.Implicit `Term in
213 NCic.Appl (dag::imp::imp::imp(* f *)::imp::imp::
216 NCicUntrusted.mk_appl acc [extract amount vl t]
217 ) (1,extract amount vl f) l)
218 in aux ft (List.rev pl)
221 (* we should better split forward and backard rewriting *)
222 let mk_proof ?(demod=false) (bag : NCic.term Terms.bag) mp subst steps =
223 let module NCicBlob =
226 let metasenv = [] let subst = [] let context = []
229 let module Pp = Pp.Pp(NCicBlob)
231 let module Subst = FoSubst in
233 let rec aux = function
235 | (j,_) :: tl when i = j -> 1
236 | _ :: tl -> 1 + aux tl
240 let vars_of i l = fst (List.assoc i l) in
241 let ty_of i l = snd (List.assoc i l) in
242 let close_with_lambdas vl t =
245 NCic.Lambda ("x"^string_of_int i, NCic.Implicit `Type, t))
248 let close_with_forall vl t =
251 NCic.Prod ("x"^string_of_int i, NCic.Implicit `Type, t))
255 let (_, lit, vl, proof),_,_ = Terms.get_from_bag id bag in
256 let lit =match lit with
257 | Terms.Predicate t -> assert false
258 | Terms.Equation (l,r,ty,_) ->
259 Terms.Node [ Terms.Leaf eqP(); ty; l; r]
264 let lit,_,_ = get_literal mp in
265 let lit = Subst.apply_subst subst lit in
267 let rec aux ongoal seen = function
270 let amount = List.length seen in
271 let lit,vl,proof = get_literal id in
272 if not ongoal && id = mp then
273 let lit = Subst.apply_subst subst lit in
274 let eq_ty = extract amount [] lit in
276 if demod then NCic.Implicit `Term
277 else mk_refl eq_ty in
278 (* prerr_endline ("Reached m point, id=" ^ (string_of_int id));
279 (NCic.LetIn ("clause_" ^ string_of_int id, eq_ty, refl,
280 aux true ((id,([],lit))::seen) (id::tl))) *)
281 NCicSubstitution.subst
282 ~avoid_beta_redexes:true ~no_implicit:false refl
283 (aux true ((id,([],lit))::seen) (id::tl))
286 | Terms.Exact _ when tl=[] ->
287 (* prerr_endline ("Exact (tl=[]) for " ^ (string_of_int id));*)
289 | Terms.Step _ when tl=[] -> assert false
292 prerr_endline ("Exact for " ^ (string_of_int id));
293 NCic.LetIn ("clause_" ^ string_of_int id,
294 close_with_forall vl (extract amount vl lit),
295 close_with_lambdas vl (extract amount vl ft),
297 ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
299 NCicSubstitution.subst
300 ~avoid_beta_redexes:true ~no_implicit:false
301 (close_with_lambdas vl (extract amount vl ft))
303 ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl)
304 | Terms.Step (_, id1, id2, dir, pos, subst) ->
305 let id, id1,(lit,vl,proof) =
307 let lit,vl,proof = get_literal id1 in
308 id1,id,(Subst.apply_subst subst lit,
309 Subst.filter subst vl, proof)
310 else id,id1,(lit,vl,proof) in
311 let vl = if ongoal then [] else vl in
313 let vars = List.rev (vars_of id seen) in
314 let args = List.map (Subst.apply_subst subst) vars in
315 let args = List.map (extract amount vl) args in
316 let rel_for_id = NCic.Rel (List.length vl + position id seen) in
317 if args = [] then rel_for_id
318 else NCic.Appl (rel_for_id::args)
320 let p_id1 = proof_of_id id1 in
321 let p_id2 = proof_of_id id2 in
325 if (ongoal=true) = (dir=Terms.Left2Right) then
328 let id1_ty = ty_of id1 seen in
330 match ty_of id2 seen with
331 | Terms.Node [ _; t; l; r ] ->
332 extract amount vl (Subst.apply_subst subst t),
333 extract amount vl (Subst.apply_subst subst l),
334 extract amount vl (Subst.apply_subst subst r)
337 (*prerr_endline "mk_predicate :";
338 if ongoal then prerr_endline "ongoal=true"
339 else prerr_endline "ongoal=false";
340 prerr_endline ("id=" ^ string_of_int id);
341 prerr_endline ("id1=" ^ string_of_int id1);
342 prerr_endline ("id2=" ^ string_of_int id2);
343 prerr_endline ("Positions :" ^
345 (List.map string_of_int pos)));*)
347 p amount (Subst.apply_subst subst id1_ty) pos vl,
350 let rewrite_step = iff1 morphism p_id1
353 let pred, hole_type, l, r =
354 let id1_ty = ty_of id1 seen in
356 match ty_of id2 seen with
357 | Terms.Node [ _; t; l; r ] ->
358 extract amount vl (Subst.apply_subst subst t),
359 extract amount vl (Subst.apply_subst subst l),
360 extract amount vl (Subst.apply_subst subst r)
364 prerr_endline "mk_predicate :";
365 if ongoal then prerr_endline "ongoal=true"
366 else prerr_endline "ongoal=false";
367 prerr_endline ("id=" ^ string_of_int id);
368 prerr_endline ("id1=" ^ string_of_int id1
369 ^": " ^ Pp.pp_foterm id1_ty);
370 prerr_endline ("id2=" ^ string_of_int id2
371 ^ ": " ^ NCicPp.ppterm [][][] id2_ty);
372 prerr_endline ("Positions :" ^
374 (List.map string_of_int pos)));*)
376 id2_ty amount (Subst.apply_subst subst id1_ty) pos vl,
381 if (ongoal=true) = (dir=Terms.Left2Right) then
383 [eq_ind_r(); hole_type; r; pred; p_id1; l; p_id2]
386 [ eq_ind(); hole_type; l; pred; p_id1; r; p_id2]
388 let body = aux ongoal
389 ((id,(List.map (fun x -> Terms.Var x) vl,lit))::seen) tl
391 if NCicUntrusted.count_occurrences [] 0 body <= 1 then
392 NCicSubstitution.subst
393 ~avoid_beta_redexes:true ~no_implicit:false
394 (close_with_lambdas vl rewrite_step) body
396 NCic.LetIn ("clause_" ^ string_of_int id,
397 close_with_forall vl (extract amount vl lit),
398 (* NCic.Implicit `Type, *)
399 close_with_lambdas vl rewrite_step, body)
401 aux false [] steps, proof_type