]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/variousTactics.ml
matita 0.5.1 tagged
[helm.git] / components / tactics / variousTactics.ml
1 (* Copyright (C) 2002, 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 (* $Id$ *)
27
28
29 (* TODO se ce n'e' piu' di una, prende la prima che trova... sarebbe meglio
30 chiedere: find dovrebbe restituire una lista di hyp (?) da passare all'utonto con una
31 funzione di callback che restituisce la (sola) hyp da applicare *)
32
33 let assumption_tac =
34  let module PET = ProofEngineTypes in
35  let assumption_tac status =
36   let (proof, goal) = status in
37   let module C = Cic in
38   let module R = CicReduction in
39   let module S = CicSubstitution in
40   let module PT = PrimitiveTactics in
41   let _,metasenv,_subst,_,_, _ = proof in
42   let _,context,ty = CicUtil.lookup_meta goal metasenv in
43   let rec find n = function 
44       hd::tl -> 
45         (match hd with
46              (Some (_, C.Decl t)) when
47                fst (R.are_convertible context (S.lift n t) ty 
48                        CicUniv.oblivion_ugraph) -> n
49            | (Some (_, C.Def (_,ty'))) when
50                fst (R.are_convertible context (S.lift n ty') ty
51                        CicUniv.oblivion_ugraph) -> n
52            | _ -> find (n+1) tl
53          )
54       | [] -> raise (PET.Fail (lazy "Assumption: No such assumption"))
55      in PET.apply_tactic (PT.apply_tac ~term:(C.Rel (find 1 context))) status
56  in
57   PET.mk_tactic assumption_tac
58 ;;
59
60 (* ANCORA DA DEBUGGARE *)
61
62 exception UnableToDetectTheTermThatMustBeGeneralizedYouMustGiveItExplicitly;;
63 exception TheSelectedTermsMustLiveInTheGoalContext
64 exception AllSelectedTermsMustBeConvertible;;
65 exception GeneralizationInHypothesesNotImplementedYet;;
66
67 let generalize_tac 
68  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
69  pattern
70  =
71   let module PET = ProofEngineTypes in
72   let generalize_tac mk_fresh_name_callback
73        ~pattern:(term,hyps_pat,concl_pat) status
74   =
75    if hyps_pat <> [] then raise GeneralizationInHypothesesNotImplementedYet;
76    let (proof, goal) = status in
77    let module C = Cic in
78    let module P = PrimitiveTactics in
79    let module T = Tacticals in
80     let uri,metasenv,_subst,pbo,pty, attrs = proof in
81     let (_,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
82     let subst,metasenv,u,selected_hyps,terms_with_context =
83      ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.oblivion_ugraph
84       ~conjecture ~pattern in
85     let context = CicMetaSubst.apply_subst_context subst context in
86     let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
87     let pbo = CicMetaSubst.apply_subst subst pbo in
88     let pty = CicMetaSubst.apply_subst subst pty in
89     let term =
90      match term with
91         None -> None
92       | Some term ->
93           Some (fun context metasenv ugraph -> 
94                   let term, metasenv, ugraph = term context metasenv ugraph in
95                    CicMetaSubst.apply_subst subst term,
96                     CicMetaSubst.apply_subst_metasenv subst metasenv,
97                     ugraph)
98     in
99     let u,typ,term, metasenv' =
100      let context_of_t, (t, metasenv, u) =
101       match terms_with_context, term with
102          [], None ->
103           raise
104            UnableToDetectTheTermThatMustBeGeneralizedYouMustGiveItExplicitly
105        | [], Some t -> context, t context metasenv u
106        | (context_of_t, _)::_, Some t -> 
107            context_of_t, t context_of_t metasenv u
108        | (context_of_t, t)::_, None -> context_of_t, (t, metasenv, u)
109      in
110       let t,subst,metasenv' =
111        try
112         CicMetaSubst.delift_rels [] metasenv
113          (List.length context_of_t - List.length context) t
114        with
115         CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
116          raise TheSelectedTermsMustLiveInTheGoalContext
117       in
118        (*CSC: I am not sure about the following two assertions;
119          maybe I need to propagate the new subst and metasenv *)
120        assert (subst = []);
121        assert (metasenv' = metasenv);
122        let typ,u = CicTypeChecker.type_of_aux' ~subst metasenv context t u in
123         u,typ,t,metasenv
124     in
125     (* We need to check:
126         1. whether they live in the context of the goal;
127            if they do they are also well-typed since they are closed subterms
128            of a well-typed term in the well-typed context of the well-typed
129            term
130         2. whether they are convertible
131     *)
132     ignore (
133      List.fold_left
134       (fun u (context_of_t,t) ->
135         (* 1 *)
136         let t,subst,metasenv'' =
137          try
138           CicMetaSubst.delift_rels [] metasenv'
139            (List.length context_of_t - List.length context) t
140          with
141           CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
142            raise TheSelectedTermsMustLiveInTheGoalContext in
143         (*CSC: I am not sure about the following two assertions;
144           maybe I need to propagate the new subst and metasenv *)
145         assert (subst = []);
146         assert (metasenv'' = metasenv');
147         (* 2 *)
148         let b,u1 = CicReduction.are_convertible ~subst context term t u in 
149          if not b then 
150           raise AllSelectedTermsMustBeConvertible
151          else
152           u1
153       ) u terms_with_context) ;
154     let status = (uri,metasenv',_subst,pbo,pty, attrs),goal in
155     let proof,goals =
156      PET.apply_tactic 
157       (T.thens 
158         ~start:
159           (P.cut_tac 
160            (C.Prod(
161              (mk_fresh_name_callback metasenv context C.Anonymous ~typ:typ), 
162              typ,
163              (ProofEngineReduction.replace_lifting_csc 1
164                ~equality:(==) 
165                ~what:(List.map snd terms_with_context)
166                ~with_what:(List.map (function _ -> C.Rel 1) terms_with_context)
167                ~where:ty)
168            )))
169         ~continuations:
170           [(P.apply_tac ~term:(C.Appl [C.Rel 1; CicSubstitution.lift 1 term])) ;
171             T.id_tac])
172         status
173     in
174      let _,metasenv'',_subst,_,_, _ = proof in
175       (* CSC: the following is just a bad approximation since a meta
176          can be closed and then re-opened! *)
177       (proof,
178         goals @
179          (List.filter
180            (fun j -> List.exists (fun (i,_,_) -> i = j) metasenv'')
181            (ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv
182              ~newmetasenv:metasenv')))
183  in
184   PET.mk_tactic (generalize_tac mk_fresh_name_callback ~pattern)
185 ;;