1 (* Copyright (C) 2002, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
31 open HelmLibraryObjects
35 (** perform debugging output? *)
38 (** debugging print *)
41 prerr_endline ("RING WARNING: " ^ s)
46 Note: For constructors URIs aren't really URIs but rather triples of
47 the form (uri, typeno, consno). This discrepancy is to preserver an
48 uniformity of invocation of "mkXXX" functions.
51 let equality_is_a_congruence_A =
52 uri_of_string "cic:/Coq/Init/Logic/Logic_lemmas/equality/A.var"
53 let equality_is_a_congruence_x =
54 uri_of_string "cic:/Coq/Init/Logic/Logic_lemmas/equality/x.var"
55 let equality_is_a_congruence_y =
56 uri_of_string "cic:/Coq/Init/Logic/Logic_lemmas/equality/y.var"
59 uri_of_string "cic:/Coq/ring/Ring_abstract/apolynomial.ind"
60 let apvar_uri = (apolynomial_uri, 0, 1)
61 let ap0_uri = (apolynomial_uri, 0, 2)
62 let ap1_uri = (apolynomial_uri, 0, 3)
63 let applus_uri = (apolynomial_uri, 0, 4)
64 let apmult_uri = (apolynomial_uri, 0, 5)
65 let apopp_uri = (apolynomial_uri, 0, 6)
67 let quote_varmap_A_uri = uri_of_string "cic:/Coq/ring/Quote/variables_map/A.var"
68 let varmap_uri = uri_of_string "cic:/Coq/ring/Quote/varmap.ind"
69 let empty_vm_uri = (varmap_uri, 0, 1)
70 let node_vm_uri = (varmap_uri, 0, 2)
71 let varmap_find_uri = uri_of_string "cic:/Coq/ring/Quote/varmap_find.con"
72 let index_uri = uri_of_string "cic:/Coq/ring/Quote/index.ind"
73 let left_idx_uri = (index_uri, 0, 1)
74 let right_idx_uri = (index_uri, 0, 2)
75 let end_idx_uri = (index_uri, 0, 3)
77 let abstract_rings_A_uri =
78 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/A.var"
79 let abstract_rings_Aplus_uri =
80 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Aplus.var"
81 let abstract_rings_Amult_uri =
82 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Amult.var"
83 let abstract_rings_Aone_uri =
84 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Aone.var"
85 let abstract_rings_Azero_uri =
86 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Azero.var"
87 let abstract_rings_Aopp_uri =
88 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Aopp.var"
89 let abstract_rings_Aeq_uri =
90 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/Aeq.var"
91 let abstract_rings_vm_uri =
92 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/vm.var"
93 let abstract_rings_T_uri =
94 uri_of_string "cic:/Coq/ring/Ring_abstract/abstract_rings/T.var"
95 let interp_ap_uri = uri_of_string "cic:/Coq/ring/Ring_abstract/interp_ap.con"
97 uri_of_string "cic:/Coq/ring/Ring_abstract/interp_sacs.con"
98 let apolynomial_normalize_uri =
99 uri_of_string "cic:/Coq/ring/Ring_abstract/apolynomial_normalize.con"
100 let apolynomial_normalize_ok_uri =
101 uri_of_string "cic:/Coq/ring/Ring_abstract/apolynomial_normalize_ok.con"
103 (** CIC PREDICATES *)
106 check whether a term is a constant or not, if argument "uri" is given and is
107 not "None" also check if the constant correspond to the given one or not
109 let cic_is_const ?(uri: uri option = None) term =
113 | Cic.Const _ -> true
117 | Cic.Const (u, _) when (eq u realuri) -> true
120 (** PROOF AND GOAL ACCESSORS *)
124 @return the uri of a given proof
126 let uri_of_proof ~proof:(uri, _, _, _) = uri
129 @param status current proof engine status
130 @raise Failure if proof is None
131 @return current goal's metasenv
133 let metasenv_of_status ~status:((_,m,_,_), _) = m
136 @param status a proof engine status
137 @raise Failure when proof or goal are None
138 @return context corresponding to current goal
140 let context_of_status ~status:(proof, goal as status) =
141 let metasenv = metasenv_of_status ~status:status in
142 let _, context, _ = CicUtil.lookup_meta goal metasenv in
145 (** CIC TERM CONSTRUCTORS *)
148 Create a Cic term consisting of a constant
149 @param uri URI of the constant
151 @exp_named_subst explicit named substitution
153 let mkConst ~uri ~exp_named_subst =
154 Cic.Const (uri, exp_named_subst)
157 Create a Cic term consisting of a constructor
158 @param uri triple <uri, typeno, consno> where uri is the uri of an inductive
159 type, typeno is the type number in a mutind structure (0 based), consno is
160 the constructor number (1 based)
161 @exp_named_subst explicit named substitution
163 let mkCtor ~uri:(uri, typeno, consno) ~exp_named_subst =
164 Cic.MutConstruct (uri, typeno, consno, exp_named_subst)
167 Create a Cic term consisting of a type member of a mutual induction
168 @param uri pair <uri, typeno> where uri is the uri of a mutual inductive
169 type and typeno is the type number (0 based) in the mutual induction
170 @exp_named_subst explicit named substitution
172 let mkMutInd ~uri:(uri, typeno) ~exp_named_subst =
173 Cic.MutInd (uri, typeno, exp_named_subst)
178 raised when the current goal is not ringable; a goal is ringable when is an
179 equality on reals (@see r_uri)
181 exception GoalUnringable
183 (** RING's FUNCTIONS LIBRARY *)
186 Check whether the ring tactic can be applied on a given term (i.e. that is
187 an equality on reals)
188 @param term to be tested
189 @return true if the term is ringable, false otherwise
192 let is_equality = function
193 | Cic.MutInd (uri, 0, []) when (eq uri Logic.eq_URI) -> true
196 let is_real = function
197 | Cic.Const (uri, _) when (eq uri Reals.r_URI) -> true
201 | Cic.Appl (app::set::_::_::[]) when (is_equality app && is_real set) ->
202 warn "Goal Ringable!";
205 warn "Goal Not Ringable :-((";
209 split an equality goal of the form "t1 = t2" in its two subterms t1 and t2
210 after checking that the goal is ringable
211 @param goal the current goal
212 @return a pair (t1,t2) that are two sides of the equality goal
213 @raise GoalUnringable if the goal isn't ringable
215 let split_eq = function
216 | (Cic.Appl (_::_::t1::t2::[])) as term when ringable term ->
217 warn ("<term1>" ^ (CicPp.ppterm t1) ^ "</term1>");
218 warn ("<term2>" ^ (CicPp.ppterm t2) ^ "</term2>");
220 | _ -> raise GoalUnringable
223 @param i an integer index representing a 1 based number of node in a binary
224 search tree counted in a fbs manner (i.e.: 1 is the root, 2 is the left
225 child of the root (if any), 3 is the right child of the root (if any), 4 is
226 the left child of the left child of the root (if any), ....)
227 @param proof the current proof
228 @return an index representing the same node in a varmap (@see varmap_uri),
229 the returned index is as defined in index (@see index_uri)
232 let rec digits_of_int n =
233 if n=1 then [] else (n mod 2 = 1)::(digits_of_int (n lsr 1))
238 mkCtor (if (digit = true) then right_idx_uri else left_idx_uri) [];
240 (List.rev (digits_of_int n)) (* remove leading true (i.e. digit 1) *)
241 (mkCtor end_idx_uri [])
244 Build a variable map (@see varmap_uri) from a variables array.
245 A variable map is almost a binary tree so this function receiving a var list
246 like [v;w;x;y;z] will build a varmap of shape: v
251 @param vars variables array
252 @return a cic term representing the variable map containing vars variables
254 let btree_of_array ~vars =
256 let empty_vm_r = mkCtor empty_vm_uri [quote_varmap_A_uri,r] in
257 let node_vm_r = mkCtor node_vm_uri [quote_varmap_A_uri,r] in
258 let size = Array.length vars in
259 let halfsize = size lsr 1 in
260 let rec aux n = (* build the btree starting from position n *)
262 n is the position in the vars array _1_based_ in order to access
263 left and right child using (n*2, n*2+1) trick
267 else if n > halfsize then (* no more children *)
268 Cic.Appl [node_vm_r; vars.(n-1); empty_vm_r; empty_vm_r]
269 else (* still children *)
270 Cic.Appl [node_vm_r; vars.(n-1); aux (n*2); aux (n*2+1)]
275 abstraction function:
276 concrete polynoms -----> (abstract polynoms, varmap)
277 @param terms list of conrete polynoms
278 @return a pair <aterms, varmap> where aterms is a list of abstract polynoms
279 and varmap is the variable map needed to interpret them
281 let abstract_poly ~terms =
282 let varhash = Hashtbl.create 19 in (* vars hash, to speed up lookup *)
283 let varlist = ref [] in (* vars list in reverse order *)
284 let counter = ref 1 in (* index of next new variable *)
285 let rec aux = function (* TODO not tail recursive *)
286 (* "bop" -> binary operator | "uop" -> unary operator *)
287 | Cic.Appl (bop::t1::t2::[])
288 when (cic_is_const ~uri:(Some Reals.rplus_URI) bop) -> (* +. *)
289 Cic.Appl [mkCtor applus_uri []; aux t1; aux t2]
290 | Cic.Appl (bop::t1::t2::[])
291 when (cic_is_const ~uri:(Some Reals.rmult_URI) bop) -> (* *. *)
292 Cic.Appl [mkCtor apmult_uri []; aux t1; aux t2]
293 | Cic.Appl (uop::t::[])
294 when (cic_is_const ~uri:(Some Reals.ropp_URI) uop) -> (* ~-. *)
295 Cic.Appl [mkCtor apopp_uri []; aux t]
296 | t when (cic_is_const ~uri:(Some Reals.r0_URI) t) -> (* 0. *)
298 | t when (cic_is_const ~uri:(Some Reals.r1_URI) t) -> (* 1. *)
300 | t -> (* variable *)
302 Hashtbl.find varhash t (* use an old var *)
303 with Not_found -> begin (* create a new var *)
305 Cic.Appl [mkCtor apvar_uri []; path_of_int !counter]
308 varlist := t :: !varlist;
309 Hashtbl.add varhash t newvar;
313 let aterms = List.map aux terms in (* abstract vars *)
314 let varmap = (* build varmap *)
315 btree_of_array ~vars:(Array.of_list (List.rev !varlist))
320 given a list of abstract terms (i.e. apolynomials) build the ring "segments"
321 that is triples like (t', t'', t''') where
322 t' = interp_ap(varmap, at)
323 t'' = interp_sacs(varmap, (apolynomial_normalize at))
324 t''' = apolynomial_normalize_ok(varmap, at)
325 at is the abstract term built from t, t is a single member of aterms
327 let build_segments ~terms =
328 let theory_args_subst varmap =
329 [abstract_rings_A_uri, Reals.r ;
330 abstract_rings_Aplus_uri, Reals.rplus ;
331 abstract_rings_Amult_uri, Reals.rmult ;
332 abstract_rings_Aone_uri, Reals.r1 ;
333 abstract_rings_Azero_uri, Reals.r0 ;
334 abstract_rings_Aopp_uri, Reals.ropp ;
335 abstract_rings_vm_uri, varmap] in
336 let theory_args_subst' eq varmap t =
337 [abstract_rings_A_uri, Reals.r ;
338 abstract_rings_Aplus_uri, Reals.rplus ;
339 abstract_rings_Amult_uri, Reals.rmult ;
340 abstract_rings_Aone_uri, Reals.r1 ;
341 abstract_rings_Azero_uri, Reals.r0 ;
342 abstract_rings_Aopp_uri, Reals.ropp ;
343 abstract_rings_Aeq_uri, eq ;
344 abstract_rings_vm_uri, varmap ;
345 abstract_rings_T_uri, t] in
346 let interp_ap varmap =
347 mkConst interp_ap_uri (theory_args_subst varmap) in
348 let interp_sacs varmap =
349 mkConst interp_sacs_uri (theory_args_subst varmap) in
350 let apolynomial_normalize = mkConst apolynomial_normalize_uri [] in
351 let apolynomial_normalize_ok eq varmap t =
352 mkConst apolynomial_normalize_ok_uri (theory_args_subst' eq varmap t) in
353 let lxy_false = (** Cic funcion "fun (x,y):R -> false" *)
354 Cic.Lambda (Cic.Anonymous, Reals.r,
355 Cic.Lambda (Cic.Anonymous, Reals.r, Datatypes.falseb))
357 let (aterms, varmap) = abstract_poly ~terms in (* abstract polys *)
358 List.map (* build ring segments *)
360 Cic.Appl [interp_ap varmap ; t],
362 [interp_sacs varmap ; Cic.Appl [apolynomial_normalize; t]]),
363 Cic.Appl [apolynomial_normalize_ok lxy_false varmap Reals.rtheory ; t]
367 let status_of_single_goal_tactic_result =
369 proof,[goal] -> proof,goal
371 raise (Fail "status_of_single_goal_tactic_result: the tactic did not produce exactly a new goal")
373 (* Galla: spostata in variousTactics.ml
375 auxiliary tactic "elim_type"
376 @param status current proof engine status
377 @param term term to cut
379 let elim_type_tac ~term ~status =
380 warn "in Ring.elim_type_tac";
381 Tacticals.thens ~start:(cut_tac ~term)
382 ~continuations:[elim_simpl_intros_tac ~term:(Cic.Rel 1) ; Tacticals.id_tac] ~status
386 auxiliary tactic, use elim_type and try to close 2nd subgoal using proof
387 @param status current proof engine status
388 @param term term to cut
389 @param proof term used to prove second subgoal generated by elim_type
391 let elim_type2_tac ~term ~proof ~status =
392 let module E = EliminationTactics in
393 warn "in Ring.elim_type2";
394 Tacticals.thens ~start:(E.elim_type_tac ~term)
395 ~continuations:[Tacticals.id_tac ; exact_tac ~term:proof] ~status
397 (* Galla: spostata in variousTactics.ml
399 Reflexivity tactic, try to solve current goal using "refl_eqT"
400 Warning: this isn't equale to the coq's Reflexivity because this one tries
401 only refl_eqT, coq's one also try "refl_equal"
402 @param status current proof engine status
404 let reflexivity_tac ~status:(proof, goal) =
405 warn "in Ring.reflexivity_tac";
406 let refl_eqt = mkCtor ~uri:refl_eqt_uri ~exp_named_subst:[] in
408 apply_tac ~status:(proof, goal) ~term:refl_eqt
409 with (Fail _) as e ->
410 let e_str = Printexc.to_string e in
411 raise (Fail ("Reflexivity failed with exception: " ^ e_str))
414 (** lift an 8-uple of debrujins indexes of n *)
415 let lift ~n (a,b,c,d,e,f,g,h) =
416 match (List.map (CicSubstitution.lift n) [a;b;c;d;e;f;g;h]) with
417 | [a;b;c;d;e;f;g;h] -> (a,b,c,d,e,f,g,h)
421 remove hypothesis from a given status starting from the last one
422 @param count number of hypotheses to remove
423 @param status current proof engine status
425 let purge_hyps_tac ~count ~status:(proof, goal as status) =
426 let module S = ProofEngineStructuralRules in
427 let rec aux n context status =
429 match (n, context) with
433 (status_of_single_goal_tactic_result (S.clear ~hyp:hd ~status))
434 | (_, []) -> failwith "Ring.purge_hyps_tac: no hypotheses left"
436 let (_, metasenv, _, _) = proof in
437 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
438 let proof',goal' = aux count context status in
439 assert (goal = goal') ;
445 Ring tactic, does associative and commutative rewritings in Reals ring
446 @param status current proof engine status
448 let ring_tac ~status:((proof, goal) as status) =
449 warn "in Ring tactic";
450 let eqt = mkMutInd (Logic.eq_URI, 0) [] in
452 let metasenv = metasenv_of_status ~status in
453 let (metano, context, ty) = CicUtil.lookup_meta goal metasenv in
454 let (t1, t2) = split_eq ty in (* goal like t1 = t2 *)
455 match (build_segments ~terms:[t1; t2]) with
456 | (t1', t1'', t1'_eq_t1'')::(t2', t2'', t2'_eq_t2'')::[] -> begin
457 List.iter (* debugging, feel free to remove *)
458 (fun (descr, term) ->
459 warn (descr ^ " " ^ (CicPp.ppterm term)))
461 ["t1"; "t1'"; "t1''"; "t1'_eq_t1''";
462 "t2"; "t2'"; "t2''"; "t2'_eq_t2''"]
463 [t1; t1'; t1''; t1'_eq_t1'';
464 t2; t2'; t2''; t2'_eq_t2'']);
466 let new_hyps = ref 0 in (* number of new hypotheses created *)
467 Tacticals.try_tactics
470 "reflexivity", EqualityTactics.reflexivity_tac ;
471 "exact t1'_eq_t1''", exact_tac ~term:t1'_eq_t1'' ;
472 "exact t2'_eq_t2''", exact_tac ~term:t2'_eq_t2'' ;
473 "exact sym_eqt su t1 ...", exact_tac
476 [mkConst Logic.sym_eq_URI
477 [equality_is_a_congruence_A, Reals.r;
478 equality_is_a_congruence_x, t1'' ;
479 equality_is_a_congruence_y, t1
483 "elim_type eqt su t1 ...", (fun ~status ->
484 let status' = (* status after 1st elim_type use *)
485 let context = context_of_status ~status in
486 if not (are_convertible context t1'' t1) then begin
487 warn "t1'' and t1 are NOT CONVERTIBLE";
489 elim_type2_tac (* 1st elim_type use *)
490 ~status ~proof:t1'_eq_t1''
491 ~term:(Cic.Appl [eqt; r; t1''; t1])
493 incr new_hyps; (* elim_type add an hyp *)
495 (proof,[goal]) -> proof,goal
498 warn "t1'' and t1 are CONVERTIBLE";
502 let (t1,t1',t1'',t1'_eq_t1'',t2,t2',t2'',t2'_eq_t2'') =
503 lift 1 (t1,t1',t1'',t1'_eq_t1'', t2,t2',t2'',t2'_eq_t2'')
506 Tacticals.try_tactics (* try to solve 1st subgoal *)
509 "exact t2'_eq_t2''", exact_tac ~term:t2'_eq_t2'';
510 "exact sym_eqt su t2 ...",
514 [mkConst Logic.sym_eq_URI
515 [equality_is_a_congruence_A, Reals.r;
516 equality_is_a_congruence_x, t2'' ;
517 equality_is_a_congruence_y, t2
521 "elim_type eqt su t2 ...", (fun ~status ->
523 let context = context_of_status ~status in
524 if not (are_convertible context t2'' t2) then begin
525 warn "t2'' and t2 are NOT CONVERTIBLE";
527 elim_type2_tac (* 2nd elim_type use *)
528 ~status ~proof:t2'_eq_t2''
529 ~term:(Cic.Appl [eqt; r; t2''; t2])
531 incr new_hyps; (* elim_type add an hyp *)
533 (proof,[goal]) -> proof,goal
536 warn "t2'' and t2 are CONVERTIBLE";
540 try (* try to solve main goal *)
541 warn "trying reflexivity ....";
542 EqualityTactics.reflexivity_tac ~status:status'
543 with (Fail _) -> (* leave conclusion to the user *)
544 warn "reflexivity failed, solution's left as an ex :-)";
545 purge_hyps_tac ~count:!new_hyps ~status:status')]
549 raise (Fail ("Ring failure: " ^ s))
551 | _ -> (* impossible: we are applying ring exacty to 2 terms *)
554 (* wrap ring_tac catching GoalUnringable and raising Fail *)
555 let ring_tac ~status =
558 with GoalUnringable ->
559 raise (Fail "goal unringable")