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