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