]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/proofEngine.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / gTopLevel / proofEngine.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open ProofEngineHelpers
27 open ProofEngineTypes
28
29   (* proof assistant status *)
30
31 let proof = ref (None : proof option)
32 let goal = ref (None : goal option)
33
34 let get_current_status_as_xml () =
35   match !proof with
36      None -> assert false
37    | Some (uri, metasenv, bo, ty) ->
38       let currentproof =
39        (*CSC: Wrong: [] is just plainly wrong *)
40        Cic.CurrentProof (UriManager.name_of_uri uri,metasenv,bo,ty,[])
41       in
42        let (acurrentproof,_,_,ids_to_inner_sorts,_,_,_) =
43         Cic2acic.acic_object_of_cic_object currentproof
44        in
45         let xml, bodyxml =
46          match
47           Cic2Xml.print_object uri ~ids_to_inner_sorts
48            ~ask_dtd_to_the_getter:true acurrentproof
49          with
50             xml,Some bodyxml -> xml,bodyxml
51           | _,None -> assert false
52         in
53          (xml, bodyxml)
54 ;;
55
56 let apply_tactic ~tactic =
57  match !proof,!goal with
58   | None,_
59   | _,None -> assert false
60   | Some proof', Some goal' ->
61      let (newproof, newgoals) = tactic ~status:(proof', goal') in
62       proof := Some newproof;
63       goal :=
64        (match newgoals, newproof with
65            goal::_, _ -> Some goal
66          | [], (_,(goal,_,_)::_,_,_) ->
67          (* the tactic left no open goal ; let's choose the first open goal *)
68          (*CSC: here we could implement and use a proof-tree like notion... *)
69             Some goal
70          | _, _ -> None)
71 ;;
72
73 (* metas_in_term term                                                *)
74 (* Returns the ordered list of the metas that occur in [term].       *)
75 (* Duplicates are removed. The implementation is not very efficient. *)
76 let metas_in_term term =
77  let module C = Cic in
78   let rec aux =
79    function
80       C.Rel _ -> []
81     | C.Meta (n,_) -> [n]
82     | C.Sort _
83     | C.Implicit -> []
84     | C.Cast (te,ty) -> (aux te) @ (aux ty)
85     | C.Prod (_,s,t) -> (aux s) @ (aux t)
86     | C.Lambda (_,s,t) -> (aux s) @ (aux t)
87     | C.LetIn (_,s,t) -> (aux s) @ (aux t)
88     | C.Appl l -> List.fold_left (fun i t -> i @ (aux t)) [] l
89     | C.Var (_,exp_named_subst)
90     | C.Const (_,exp_named_subst)
91     | C.MutInd (_,_,exp_named_subst)
92     | C.MutConstruct (_,_,_,exp_named_subst) ->
93        List.fold_left (fun i (_,t) -> i @ (aux t)) [] exp_named_subst
94     | C.MutCase (_,_,outt,t,pl) ->
95        (aux outt) @ (aux t) @
96         (List.fold_left (fun i t -> i @ (aux t)) [] pl)
97     | C.Fix (_,fl) ->
98         List.fold_left (fun i (_,_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
99     | C.CoFix (_,fl) ->
100         List.fold_left (fun i (_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
101   in
102    let metas = aux term in
103     let rec elim_duplicates =
104      function
105         [] -> []
106       | he::tl ->
107          he::(elim_duplicates (List.filter (function el -> he <> el) tl))
108     in
109      elim_duplicates metas
110
111 (* perforate context term ty                                                 *)
112 (* replaces the term [term] in the proof with a new metavariable whose type  *)
113 (* is [ty]. [context] must be the context of [term] in the whole proof. This *)
114 (* could be easily computed; so the only reasons to have it as an argument   *)
115 (* are efficiency reasons.                                                   *)
116 let perforate context term ty =
117  let module C = Cic in
118   match !proof with
119      None -> assert false
120    | Some (uri,metasenv,bo,gty as proof') ->
121       let newmeta = new_meta proof' in
122        (* We push the new meta at the end of the list for pretty-printing *)
123        (* purposes: in this way metas are ordered.                        *)
124        let metasenv' = metasenv@[newmeta,context,ty] in
125         let irl = identity_relocation_list_for_metavariable context in
126 (*CSC: Bug: se ci sono due term uguali nella prova dovrei bucarne uno solo!!!*)
127         let bo' =
128          ProofEngineReduction.replace (==) [term] [C.Meta (newmeta,irl)] bo
129         in
130         (* It may be possible that some metavariables occurred only in *)
131         (* the term we are perforating and they now occurs no more. We *)
132         (* get rid of them, collecting the really useful metavariables *)
133         (* in metasenv''.                                              *)
134 (*CSC: Bug: una meta potrebbe non comparire in bo', ma comparire nel tipo *)
135 (*CSC: di una metavariabile che compare in bo'!!!!!!!                     *)
136          let newmetas = metas_in_term bo' in
137           let metasenv'' =
138            List.filter (function (n,_,_) -> List.mem n newmetas) metasenv'
139           in
140            proof := Some (uri,metasenv'',bo',gty) ;
141            goal := Some newmeta
142
143
144 (************************************************************)
145 (*                  Some easy tactics.                      *)
146 (************************************************************)
147
148 (* Reduces [term] using [reduction_function] in the current scratch goal [ty] *)
149 let reduction_tactic_in_scratch reduction_function terms ty =
150  let metasenv =
151   match !proof with
152      None -> []
153    | Some (_,metasenv,_,_) -> metasenv
154  in
155  let metano,context,_ =
156   match !goal with
157      None -> assert false
158    | Some metano -> List.find (function (m,_,_) -> m=metano) metasenv
159  in
160   let terms' = List.map (reduction_function context) terms in
161    ProofEngineReduction.replace
162     ~equality:(==) ~what:terms ~with_what:terms' ~where:ty
163 ;;
164
165 let whd_in_scratch    = reduction_tactic_in_scratch CicReduction.whd
166 let reduce_in_scratch = reduction_tactic_in_scratch ProofEngineReduction.reduce
167 let simpl_in_scratch  = reduction_tactic_in_scratch ProofEngineReduction.simpl
168
169 (************************************************************)
170 (*              Tactics defined elsewhere                   *)
171 (************************************************************)
172
173   (* primitive tactics *)
174
175 let apply term = apply_tactic (PrimitiveTactics.apply_tac ~term)
176 let intros ?mk_fresh_name_callback () =
177  apply_tactic (PrimitiveTactics.intros_tac ?mk_fresh_name_callback ())
178 let cut ?mk_fresh_name_callback term =
179  apply_tactic (PrimitiveTactics.cut_tac ?mk_fresh_name_callback term)
180 let letin ?mk_fresh_name_callback term =
181  apply_tactic (PrimitiveTactics.letin_tac ?mk_fresh_name_callback term)
182 let exact term = apply_tactic (PrimitiveTactics.exact_tac ~term)
183 let elim_intros_simpl term =
184   apply_tactic (PrimitiveTactics.elim_intros_simpl_tac ~term)
185 let change ~goal_input:what ~input:with_what =
186   apply_tactic (PrimitiveTactics.change_tac ~what ~with_what)
187
188   (* structural tactics *)
189
190 let clearbody hyp = apply_tactic (ProofEngineStructuralRules.clearbody ~hyp)
191 let clear hyp = apply_tactic (ProofEngineStructuralRules.clear ~hyp)
192
193   (* reduction tactics *)
194
195 let whd terms =
196  apply_tactic
197   (ReductionTactics.whd_tac ~also_in_hypotheses:true ~terms:(Some terms))
198 let reduce terms =
199  apply_tactic
200   (ReductionTactics.reduce_tac ~also_in_hypotheses:true ~terms:(Some terms))
201 let simpl terms =
202  apply_tactic
203   (ReductionTactics.simpl_tac ~also_in_hypotheses:true ~terms:(Some terms))
204
205 let fold_whd term =
206  apply_tactic
207   (ReductionTactics.fold_tac ~reduction:CicReduction.whd
208     ~also_in_hypotheses:true ~term)
209 let fold_reduce term =
210  apply_tactic
211   (ReductionTactics.fold_tac ~reduction:ProofEngineReduction.reduce
212     ~also_in_hypotheses:true ~term)
213 let fold_simpl term =
214  apply_tactic
215   (ReductionTactics.fold_tac ~reduction:ProofEngineReduction.simpl
216     ~also_in_hypotheses:true ~term)
217
218   (* other tactics *)
219
220 let elim_type term = apply_tactic (EliminationTactics.elim_type_tac ~term)
221 let ring () = apply_tactic Ring.ring_tac
222 let fourier () = apply_tactic FourierR.fourier_tac
223
224 let rewrite_simpl term = apply_tactic (EqualityTactics.rewrite_simpl_tac ~term)
225 let rewrite_back_simpl term = apply_tactic (EqualityTactics.rewrite_back_simpl_tac ~term)
226 let replace ~goal_input:what ~input:with_what = 
227   apply_tactic (EqualityTactics.replace_tac ~what ~with_what)
228
229 let reflexivity () = apply_tactic EqualityTactics.reflexivity_tac
230 let symmetry () = apply_tactic EqualityTactics.symmetry_tac
231 let transitivity term = apply_tactic (EqualityTactics.transitivity_tac ~term)
232
233 let exists () = apply_tactic IntroductionTactics.exists_tac
234 let split () = apply_tactic IntroductionTactics.split_tac 
235 let left () = apply_tactic IntroductionTactics.left_tac
236 let right () = apply_tactic IntroductionTactics.right_tac
237
238 let assumption () = apply_tactic VariousTactics.assumption_tac
239
240 let generalize ?mk_fresh_name_callback terms =
241  apply_tactic (VariousTactics.generalize_tac ?mk_fresh_name_callback terms)
242
243 let absurd term = apply_tactic (NegationTactics.absurd_tac ~term)
244 let contradiction () = apply_tactic NegationTactics.contradiction_tac
245
246 let decompose ~uris_choice_callback term =
247  apply_tactic (EliminationTactics.decompose_tac ~uris_choice_callback term)
248
249 let injection term = apply_tactic (DiscriminationTactics.injection_tac ~term)
250 let discriminate term = apply_tactic (DiscriminationTactics.discriminate_tac ~term)
251 let decide_equality () = apply_tactic DiscriminationTactics.decide_equality_tac
252 let compare term = apply_tactic (DiscriminationTactics.compare_tac ~term)
253
254 (*
255 let prova_tatticali () = apply_tactic Tacticals.prova_tac
256 *)
257