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