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