]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/variousTactics.ml
Added a filter for uris in tactic "auto".
[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 (* Da rimuovere, solo per debug*)
28 let print_context ctx =
29     let print_name =
30      function
31         Cic.Name n -> n
32       | Cic.Anonymous -> "_"
33     in
34      List.fold_right
35       (fun i (output,context) ->
36         let (newoutput,context') =
37          match i with
38             Some (n,Cic.Decl t) ->
39               print_name n ^ ":" ^ CicPp.pp t context ^ "\n", (Some n)::context
40           | Some (n,Cic.Def (t,None)) ->
41               print_name n ^ ":=" ^ CicPp.pp t context ^ "\n", (Some n)::context
42           | None ->
43               "_ ?= _\n", None::context
44           | Some (_,Cic.Def (_,Some _)) -> assert false
45         in
46          output^newoutput,context'
47       ) ctx ("",[])
48   ;;
49
50
51
52
53
54 let search_theorems_in_context status =
55   let (proof, goal) = status in
56   let module C = Cic in
57   let module R = CicReduction in
58   let module S = CicSubstitution in
59   prerr_endline "Entro in search_context";
60   let _,metasenv,_,_ = proof in
61   let _,context,ty = CicUtil.lookup_meta goal metasenv in
62   let rec find n = function 
63       [] -> []
64     | hd::tl ->
65         let res =
66           try 
67             Some (PrimitiveTactics.apply_tac status ~term:(C.Rel n)) 
68           with 
69             ProofEngineTypes.Fail _ -> None in
70         (match res with
71           Some res -> res::(find (n+1) tl)
72         | None -> find (n+1) tl)
73   in
74   try 
75     let res = find 1 context in
76     prerr_endline "Ho finito context";
77     res 
78   with Failure s -> 
79     prerr_endline ("SIAM QUI = " ^ s); []
80 ;;     
81
82 exception NotAProposition;;
83 exception NotApplicableTheorem;;
84 exception MaxDepth;;
85
86 let depth = 3;;
87
88 let rec auto_tac_aux mqi_handle level proof goal = 
89 prerr_endline ("Entro in Auto_rec; level = " ^ (string_of_int level));
90 if level = 0 then
91   (* (proof, [goal]) *)
92   (prerr_endline ("MaxDepth");
93    raise MaxDepth)
94 else 
95   (* let us verify that the metavariable is still an open goal --
96      it could have been closed by closing other goals -- and that
97      it is of sort Prop *)
98   let _,metasenv,_,_ = proof in
99   let meta_inf = 
100     (try 
101        let (_, ey ,ty) = CicUtil.lookup_meta goal metasenv in
102          Some (ey, ty)
103      with _ -> None) in
104   match meta_inf with
105       Some (ey, ty) ->
106         prerr_endline ("CURRENT GOAL = " ^ (CicPp.ppterm ty));
107         prerr_endline ("CURRENT HYP = " ^ (fst (print_context ey)));
108         (*
109         let time1 = Unix.gettimeofday() in
110         let _, all_paths = NewConstraints.prefixes 5 ty in
111         let time2 = Unix.gettimeofday() in
112         prerr_endline 
113           (Printf.sprintf "TEMPO DI CALCOLO = %1.3f" (time2 -. time1) );
114         prerr_endline 
115           ("ALL PATHS: n = " ^ string_of_int 
116              (List.length all_paths));
117         prerr_endline (NewConstraints.pp_prefixes all_paths); 
118         *)
119         (* if the goal does not have a sort Prop we return the
120            current proof and a list containing the goal *)
121         let ty_sort = CicTypeChecker.type_of_aux' metasenv ey ty in
122           if CicReduction.are_convertible 
123             ey (Cic.Sort Cic.Prop) ty_sort then
124             (* sort Prop *)
125             (* choices is a list of pairs proof and goallist *)
126             let choices  =
127               (search_theorems_in_context (proof,goal))@ 
128               (TacticChaser.searchTheorems mqi_handle (proof,goal)) 
129             in
130             let rec try_choices = function
131                 [] -> raise NotApplicableTheorem
132               | (proof,goallist)::tl ->
133 prerr_endline ("GOALLIST = " ^ string_of_int (List.length goallist));
134                   (try 
135                      List.fold_left 
136                        (fun proof goal ->
137                             auto_tac_aux mqi_handle (level-1) proof goal)
138                        proof goallist
139                    with 
140                      | MaxDepth
141                      | NotApplicableTheorem 
142                      | NotAProposition ->
143                          try_choices tl) in
144               try_choices choices
145           else
146             (* CUT AND PASTE DI PROVA !! *)
147             let choices  =
148               (search_theorems_in_context (proof,goal))@ 
149               (TacticChaser.searchTheorems mqi_handle (proof,goal)) 
150             in
151             let rec try_choices = function
152                 [] -> raise NotApplicableTheorem
153               | (proof,[])::tl -> proof
154               | _::tl -> try_choices tl in
155             try_choices choices
156             (* raise NotAProposition *)
157     | None -> proof
158 ;;
159
160 let auto_tac mqi_handle (proof,goal) =
161   prerr_endline "Entro in Auto";
162   try 
163     let proof = auto_tac_aux mqi_handle depth proof goal in
164 prerr_endline "AUTO_TAC HA FINITO";
165     (proof,[])
166   with 
167   | MaxDepth -> assert false (* this should happens only if depth is 0 above *)
168   | NotApplicableTheorem -> 
169       prerr_endline("No applicable theorem");
170       raise (ProofEngineTypes.Fail "No Applicable theorem");;
171
172 (* TODO se ce n'e' piu' di una, prende la prima che trova... sarebbe meglio
173 chiedere: find dovrebbe restituire una lista di hyp (?) da passare all'utonto con una
174 funzione di callback che restituisce la (sola) hyp da applicare *)
175
176 let assumption_tac status =
177   let (proof, goal) = status in
178   let module C = Cic in
179   let module R = CicReduction in
180   let module S = CicSubstitution in
181    let _,metasenv,_,_ = proof in
182     let _,context,ty = CicUtil.lookup_meta goal metasenv in
183      let rec find n = function 
184         hd::tl -> 
185          (match hd with
186              (Some (_, C.Decl t)) when
187                (R.are_convertible context (S.lift n t) ty) -> n
188            | (Some (_, C.Def (_,Some ty'))) when
189                (R.are_convertible context ty' ty) -> n
190            | (Some (_, C.Def (t,None))) when
191                (R.are_convertible context
192                 (CicTypeChecker.type_of_aux' metasenv context (S.lift n t)) ty) -> n 
193            | _ -> find (n+1) tl
194          )
195       | [] -> raise (ProofEngineTypes.Fail "Assumption: No such assumption")
196      in PrimitiveTactics.apply_tac status ~term:(C.Rel (find 1 context))
197 ;;
198
199 (* ANCORA DA DEBUGGARE *)
200
201 exception AllSelectedTermsMustBeConvertible;;
202
203 (* serve una funzione che cerchi nel ty dal basso a partire da term, i lambda
204 e li aggiunga nel context, poi si conta la lunghezza di questo nuovo
205 contesto e si lifta di tot... COSA SIGNIFICA TUTTO CIO'?????? *)
206
207 let generalize_tac
208  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
209  terms status
210 =
211   let (proof, goal) = status in
212   let module C = Cic in
213   let module P = PrimitiveTactics in
214   let module T = Tacticals in
215    let _,metasenv,_,_ = proof in
216    let _,context,ty = CicUtil.lookup_meta goal metasenv in
217     let typ =
218      match terms with
219         [] -> assert false
220       | he::tl ->
221          (* We need to check that all the convertibility of all the terms *)
222          List.iter
223           (function t ->
224             if not (CicReduction.are_convertible context he t) then 
225              raise AllSelectedTermsMustBeConvertible
226           ) tl ;
227          (CicTypeChecker.type_of_aux' metasenv context he)
228     in
229      T.thens 
230       ~start:
231         (P.cut_tac 
232          (C.Prod(
233            (mk_fresh_name_callback metasenv context C.Anonymous typ), 
234            typ,
235            (ProofEngineReduction.replace_lifting_csc 1
236              ~equality:(==) 
237              ~what:terms
238              ~with_what:(List.map (function _ -> C.Rel 1) terms)
239              ~where:ty)
240          )))
241       ~continuations: [(P.apply_tac ~term:(C.Rel 1)) ; T.id_tac]
242       status
243 ;;
244
245