1 (* Copyright (C) 2000, 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/.
26 open ProofEngineHelpers
29 (* proof assistant status *)
31 let proof = ref (None : proof option)
32 let goal = ref (None : goal option)
34 let apply_or_can_apply_tactic ~try_only ~tactic =
35 match !proof,!goal with
37 | _,None -> assert false
38 | Some proof', Some goal' ->
39 let (newproof, newgoals) = tactic ~status:(proof', goal') in
42 proof := Some newproof;
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... *)
54 let apply_tactic = apply_or_can_apply_tactic ~try_only:false;;
56 let can_apply_tactic ~tactic =
58 apply_or_can_apply_tactic ~try_only:true ~tactic ;
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 =
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)
89 List.fold_left (fun i (_,_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
91 List.fold_left (fun i (_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
93 let metas = aux term in
94 let rec elim_duplicates =
98 he::(elim_duplicates (List.filter (function el -> he <> el) tl))
100 elim_duplicates metas
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
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!!!*)
119 ProofEngineReduction.replace (==) term (C.Meta (newmeta,irl)) bo
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 *)
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
129 List.filter (function (n,_,_) -> List.mem n newmetas) metasenv'
131 proof := Some (uri,metasenv'',bo',gty) ;
135 (************************************************************)
136 (* Some easy tactics. *)
137 (************************************************************)
139 (*CSC: generatore di nomi? Chiedere il nome? *)
141 let next_fresh_index = ref 0
144 incr next_fresh_index ;
145 "fresh_name" ^ string_of_int !next_fresh_index
147 let reduction_tactic reduction_function term =
148 let curi,metasenv,pbo,pty =
151 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
153 let metano,context,ty =
156 | Some metano -> List.find (function (m,_,_) -> m=metano) metasenv
158 (* We don't know if [term] is a subterm of [ty] or a subterm of *)
159 (* the type of one metavariable. So we replace it everywhere. *)
160 (*CSC: Il vero problema e' che non sapendo dove sia il term non *)
161 (*CSC: sappiamo neppure quale sia il suo contesto!!!! Insomma, *)
162 (*CSC: e' meglio prima cercare il termine e scoprirne il *)
163 (*CSC: contesto, poi ridurre e infine rimpiazzare. *)
164 let replace context where=
165 (*CSC: Per il momento se la riduzione fallisce significa solamente che *)
166 (*CSC: siamo nel contesto errato. Metto il try, ma che schifo!!!! *)
167 (*CSC: Anche perche' cosi' catturo anche quelle del replace che non dovrei *)
169 let term' = reduction_function context term in
170 ProofEngineReduction.replace ~equality:(==) ~what:term ~with_what:term'
175 let ty' = replace context ty in
178 (fun entry context ->
180 Some (name,Cic.Def t) ->
181 (Some (name,Cic.Def (replace context t)))::context
182 | Some (name,Cic.Decl t) ->
183 (Some (name,Cic.Decl (replace context t)))::context
184 | None -> None::context
190 (n,_,_) when n = metano -> (metano,context',ty')
194 proof := Some (curi,metasenv',pbo,pty) ;
197 (* Reduces [term] using [reduction_function] in the current scratch goal [ty] *)
198 let reduction_tactic_in_scratch reduction_function term ty =
202 | Some (_,metasenv,_,_) -> metasenv
204 let metano,context,_ =
207 | Some metano -> List.find (function (m,_,_) -> m=metano) metasenv
209 let term' = reduction_function context term in
210 ProofEngineReduction.replace
211 ~equality:(==) ~what:term ~with_what:term' ~where:ty
213 let whd = reduction_tactic CicReduction.whd
214 let reduce = reduction_tactic ProofEngineReduction.reduce
215 let simpl = reduction_tactic ProofEngineReduction.simpl
217 let whd_in_scratch = reduction_tactic_in_scratch CicReduction.whd
218 let reduce_in_scratch =
219 reduction_tactic_in_scratch ProofEngineReduction.reduce
220 let simpl_in_scratch =
221 reduction_tactic_in_scratch ProofEngineReduction.simpl
223 (* It is just the opposite of whd. The code should probably be merged. *)
225 let curi,metasenv,pbo,pty =
228 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
230 let metano,context,ty =
233 | Some metano -> List.find (function (m,_,_) -> m=metano) metasenv
235 let term' = CicReduction.whd context term in
236 (* We don't know if [term] is a subterm of [ty] or a subterm of *)
237 (* the type of one metavariable. So we replace it everywhere. *)
238 (*CSC: ma si potrebbe ovviare al problema. Ma non credo *)
239 (*CSC: che si guadagni nulla in fatto di efficienza. *)
241 ProofEngineReduction.replace ~equality:(=) ~what:term' ~with_what:term
243 let ty' = replace ty in
247 Some (n,Cic.Decl t) -> Some (n,Cic.Decl (replace t))
248 | Some (n,Cic.Def t) -> Some (n,Cic.Def (replace t))
255 (n,_,_) when n = metano -> (metano,context',ty')
259 proof := Some (curi,metasenv',pbo,pty) ;
262 (************************************************************)
263 (* Tactics defined elsewhere *)
264 (************************************************************)
266 (* primitive tactics *)
268 let can_apply term = can_apply_tactic (PrimitiveTactics.apply_tac ~term)
269 let apply term = apply_tactic (PrimitiveTactics.apply_tac ~term)
271 apply_tactic (PrimitiveTactics.intros_tac ~name:(fresh_name ()))
272 let cut term = apply_tactic (PrimitiveTactics.cut_tac ~term)
273 let letin term = apply_tactic (PrimitiveTactics.letin_tac ~term)
274 let exact term = apply_tactic (PrimitiveTactics.exact_tac ~term)
275 let elim_simpl_intros term =
276 apply_tactic (PrimitiveTactics.elim_simpl_intros_tac ~term)
277 let change ~goal_input:what ~input:with_what =
278 apply_tactic (PrimitiveTactics.change_tac ~what ~with_what)
280 (* structural tactics *)
282 let clearbody hyp = apply_tactic (ProofEngineStructuralRules.clearbody ~hyp)
283 let clear hyp = apply_tactic (ProofEngineStructuralRules.clear ~hyp)
287 let elim_type term = apply_tactic (Ring.elim_type_tac ~term)
288 let ring () = apply_tactic Ring.ring_tac
289 let fourier () = apply_tactic FourierR.fourier_tac
290 let rewrite_simpl term = apply_tactic (FourierR.rewrite_simpl_tac ~term)
292 let reflexivity () = apply_tactic VariousTactics.reflexivity_tac
293 let symmetry () = apply_tactic VariousTactics.symmetry_tac
294 let transitivity term = apply_tactic (VariousTactics.transitivity_tac ~term)
296 let left () = apply_tactic VariousTactics.left_tac
297 let right () = apply_tactic VariousTactics.right_tac
299 let assumption () = apply_tactic VariousTactics.assumption_tac
301 let prova_tatticali () = apply_tactic Tacticals.prova_tac