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