]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/variousTactics.ml
Added a sort function to decide the order of theorems to try in the 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 (*
89 let rec auto_tac_aux mqi_handle level proof goal = 
90 prerr_endline ("Entro in Auto_rec; level = " ^ (string_of_int level));
91 if level = 0 then
92   (* (proof, [goal]) *)
93   (prerr_endline ("MaxDepth");
94    raise MaxDepth)
95 else 
96   (* let us verify that the metavariable is still an open goal --
97      it could have been closed by closing other goals -- and that
98      it is of sort Prop *)
99   let _,metasenv,_,_ = proof in
100   let meta_inf = 
101     (try 
102        let (_, ey ,ty) = CicUtil.lookup_meta goal metasenv in
103          Some (ey, ty)
104      with _ -> None) in
105   match meta_inf with
106       Some (ey, ty) ->
107         prerr_endline ("CURRENT GOAL = " ^ (CicPp.ppterm ty));
108         prerr_endline ("CURRENT HYP = " ^ (fst (print_context ey)));
109         (* if the goal does not have a sort Prop we return the
110            current proof and a list containing the goal *)
111         let ty_sort = CicTypeChecker.type_of_aux' metasenv ey ty in
112           if CicReduction.are_convertible 
113             ey (Cic.Sort Cic.Prop) ty_sort then
114             (* sort Prop *)
115             (* choices is a list of pairs proof and goallist *)
116             let choices  =
117               (search_theorems_in_context (proof,goal))@ 
118               (TacticChaser.searchTheorems mqi_handle (proof,goal)) 
119             in
120             let rec try_choices = function
121                 [] -> raise NotApplicableTheorem
122               | (proof,goallist)::tl ->
123 prerr_endline ("GOALLIST = " ^ string_of_int (List.length goallist));
124                   (try 
125                      List.fold_left 
126                        (fun proof goal ->
127                             auto_tac_aux mqi_handle (level-1) proof goal)
128                        proof goallist
129                    with 
130                      | MaxDepth
131                      | NotApplicableTheorem 
132                      | NotAProposition ->
133                          try_choices tl) in
134               try_choices choices
135           else
136             (* CUT AND PASTE DI PROVA !! *)
137             let choices  =
138               (search_theorems_in_context (proof,goal))@ 
139               (TacticChaser.searchTheorems mqi_handle (proof,goal)) 
140             in
141             let rec try_choices = function
142                 [] -> raise NotApplicableTheorem
143               | (proof,[])::tl -> proof
144               | _::tl -> try_choices tl in
145             try_choices choices
146             (* raise NotAProposition *)
147     | None -> proof
148 ;;
149
150 let auto_tac mqi_handle (proof,goal) =
151   prerr_endline "Entro in Auto";
152   try 
153     let proof = auto_tac_aux mqi_handle depth proof goal in
154 prerr_endline "AUTO_TAC HA FINITO";
155     (proof,[])
156   with 
157   | MaxDepth -> assert false (* this should happens only if depth is 0 above *)
158   | NotApplicableTheorem -> 
159       prerr_endline("No applicable theorem");
160       raise (ProofEngineTypes.Fail "No Applicable theorem");;
161 *)
162
163 (**** ESPERIMENTO ************************)
164
165 let new_search_theorems f proof goal depth gtl =
166   let local_choices = f (proof,goal)
167   in 
168   List.map 
169     (function (proof, goallist) ->
170        (proof, (List.map (function g -> (g,depth)) goallist)@gtl))
171     local_choices 
172 ;;
173
174 exception NoOtherChoices;;
175
176 let rec auto_new mqi_handle = function
177     [] -> raise NoOtherChoices
178   | (proof, [])::tl -> (proof, [])::tl
179   | (proof, (goal,0)::gtl)::tl -> auto_new mqi_handle tl
180   | (proof, (goal,depth)::gtl)::tl ->
181       let _,metasenv,_,_ = proof in
182       let meta_inf = 
183         (try 
184            let (_, ey ,ty) = CicUtil.lookup_meta goal metasenv in
185              Some (ey, ty)
186          with _ -> None) in
187         match meta_inf with
188             Some (ey, ty) ->
189                prerr_endline ("CURRENT GOAL = " ^ (CicPp.ppterm ty));
190                prerr_endline ("CURRENT HYP = " ^ (fst (print_context ey)));
191               let local_choices =
192                 new_search_theorems 
193                   search_theorems_in_context proof goal (depth-1) gtl in
194               let global_choices =
195                 new_search_theorems 
196                   (TacticChaser.searchTheorems mqi_handle) 
197                   proof goal (depth-1) gtl in
198               let all_choices =
199                 local_choices@global_choices@tl in
200               let sorting_list (_,g1) (_,g2) =
201                 let l1 = List.length g1 in
202                 let l2 = List.length g2 in
203                 if (l1 = l2 && not(l1 = 0)) then
204                 (snd(List.nth g2 0)) - (snd(List.nth g1 0))
205                 else l1 - l2 in
206               let reorder = 
207                 List.stable_sort sorting_list all_choices
208               in
209                 auto_new mqi_handle reorder
210           | None -> auto_new mqi_handle ((proof,gtl)::tl)
211 ;;
212
213
214 let auto_tac mqi_handle (proof,goal) =
215   prerr_endline "Entro in Auto";
216   try 
217     let (proof,_)::_ = auto_new mqi_handle [(proof, [(goal,depth)])] in
218 prerr_endline "AUTO_TAC HA FINITO";
219     (proof,[])
220   with 
221   | NoOtherChoices ->
222       prerr_endline("Auto failed");
223       raise (ProofEngineTypes.Fail "No Applicable theorem");;
224
225 (* TODO se ce n'e' piu' di una, prende la prima che trova... sarebbe meglio
226 chiedere: find dovrebbe restituire una lista di hyp (?) da passare all'utonto con una
227 funzione di callback che restituisce la (sola) hyp da applicare *)
228
229 let assumption_tac status =
230   let (proof, goal) = status in
231   let module C = Cic in
232   let module R = CicReduction in
233   let module S = CicSubstitution in
234    let _,metasenv,_,_ = proof in
235     let _,context,ty = CicUtil.lookup_meta goal metasenv in
236      let rec find n = function 
237         hd::tl -> 
238          (match hd with
239              (Some (_, C.Decl t)) when
240                (R.are_convertible context (S.lift n t) ty) -> n
241            | (Some (_, C.Def (_,Some ty'))) when
242                (R.are_convertible context ty' ty) -> n
243            | (Some (_, C.Def (t,None))) when
244                (R.are_convertible context
245                 (CicTypeChecker.type_of_aux' metasenv context (S.lift n t)) ty) -> n 
246            | _ -> find (n+1) tl
247          )
248       | [] -> raise (ProofEngineTypes.Fail "Assumption: No such assumption")
249      in PrimitiveTactics.apply_tac status ~term:(C.Rel (find 1 context))
250 ;;
251
252 (* ANCORA DA DEBUGGARE *)
253
254 exception AllSelectedTermsMustBeConvertible;;
255
256 (* serve una funzione che cerchi nel ty dal basso a partire da term, i lambda
257 e li aggiunga nel context, poi si conta la lunghezza di questo nuovo
258 contesto e si lifta di tot... COSA SIGNIFICA TUTTO CIO'?????? *)
259
260 let generalize_tac
261  ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
262  terms status
263 =
264   let (proof, goal) = status in
265   let module C = Cic in
266   let module P = PrimitiveTactics in
267   let module T = Tacticals in
268    let _,metasenv,_,_ = proof in
269    let _,context,ty = CicUtil.lookup_meta goal metasenv in
270     let typ =
271      match terms with
272         [] -> assert false
273       | he::tl ->
274          (* We need to check that all the convertibility of all the terms *)
275          List.iter
276           (function t ->
277             if not (CicReduction.are_convertible context he t) then 
278              raise AllSelectedTermsMustBeConvertible
279           ) tl ;
280          (CicTypeChecker.type_of_aux' metasenv context he)
281     in
282      T.thens 
283       ~start:
284         (P.cut_tac 
285          (C.Prod(
286            (mk_fresh_name_callback metasenv context C.Anonymous typ), 
287            typ,
288            (ProofEngineReduction.replace_lifting_csc 1
289              ~equality:(==) 
290              ~what:terms
291              ~with_what:(List.map (function _ -> C.Rel 1) terms)
292              ~where:ty)
293          )))
294       ~continuations: [(P.apply_tac ~term:(C.Rel 1)) ; T.id_tac]
295       status
296 ;;
297
298