]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/fwdSimplTactic.ml
lapply and fwd improved
[helm.git] / helm / ocaml / tactics / fwdSimplTactic.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 module PEH = ProofEngineHelpers 
28 *)
29 module U  = CicUniv
30 module TC = CicTypeChecker 
31 module PET = ProofEngineTypes 
32 module S = CicSubstitution
33 module PT = PrimitiveTactics
34 module T = Tacticals
35 module FNG = FreshNamesGenerator
36 module MI = CicMkImplicit
37 module PESR = ProofEngineStructuralRules
38
39 let fail_msg0 = "unexported clearbody: invalid argument"
40 let fail_msg1 = "fwd: argument is not premise in the current goal"
41 let fail_msg2 = "fwd: no applicable simplification"
42
43 let error msg = raise (PET.Fail msg)
44
45 (* unexported tactics *******************************************************)
46
47 let clearbody ~index =
48    let rec find_name index = function
49       | Some (Cic.Name name, _) :: _ when index = 1 -> name
50       | _ :: tail when index > 1 -> find_name (pred index) tail
51       | _ -> error fail_msg0
52    in
53    let clearbody status =
54       let (proof, goal) = status in
55       let _, metasenv, _, _ = proof in
56       let _, context, _ = CicUtil.lookup_meta goal metasenv in
57       PET.apply_tactic (PESR.clearbody ~hyp:(find_name index context)) status
58    in
59    PET.mk_tactic clearbody
60
61 (* lapply *******************************************************************)
62
63 let strip_prods metasenv context ?how_many to_what term =
64    let irl = MI.identity_relocation_list_for_metavariable context in
65    let mk_meta metasenv its_type =  
66       let index = MI.new_meta metasenv [] in
67       let metasenv = [index, context, its_type] @ metasenv in
68       metasenv, Cic.Meta (index, irl), index
69    in
70    let update_counters = function
71       | None, []                 -> None, T.id_tac, []
72       | None, to_what :: tail    -> None, PT.apply_tac ~term:to_what, tail
73       | Some hm, []              -> Some (pred hm), T.id_tac, []
74       | Some hm, to_what :: tail -> Some (pred hm), PT.apply_tac ~term:to_what, tail
75    in 
76    let rec aux metasenv metas conts tw = function
77       | Some hm, _ when hm <= 0               -> metasenv, metas, conts 
78       | xhm, Cic.Prod (Cic.Name _, t1, t2)    ->
79          let metasenv, meta, index = mk_meta metasenv t1 in    
80          aux metasenv (meta :: metas) ((T.id_tac, index) :: conts) tw (xhm, (S.subst meta t2))      
81       | xhm, Cic.Prod (Cic.Anonymous, t1, t2) ->
82          let xhm, tac, tw = update_counters (xhm, tw) in 
83          let metasenv, meta, index = mk_meta metasenv t1 in    
84          aux metasenv (meta :: metas) ((tac, index) :: conts) tw (xhm, (S.subst meta t2))
85       | _, t                                  -> metasenv, metas, conts 
86    in
87    aux metasenv [] [] to_what (how_many, term)
88    
89 let lapply_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
90                (* ?(substs = []) *) ?how_many ?(to_what = []) what =
91    let letin_tac term = PT.letin_tac ~mk_fresh_name_callback term in   
92    let lapply_tac (proof, goal) =
93       let xuri, metasenv, u, t = proof in
94       let _, context, _ = CicUtil.lookup_meta goal metasenv in
95       let lemma, _ = TC.type_of_aux' metasenv context what U.empty_ugraph in
96       let lemma = FNG.clean_dummy_dependent_types lemma in
97       let metasenv, metas, conts = strip_prods metasenv context ?how_many to_what lemma in      
98       let conclusion =  Cic.Appl (what :: List.rev metas) in
99       let tac = T.thens ~start:(letin_tac conclusion)
100                         ~continuations:[clearbody ~index:1]
101       in
102       let proof = (xuri, metasenv, u, t) in
103       let aux (proof, goals) (tac, goal) = 
104          let proof, new_goals = PET.apply_tactic tac (proof, goal) in
105          proof, goals @ new_goals
106       in
107       List.fold_left aux (proof, []) ((tac, goal) :: conts)
108    in
109    PET.mk_tactic lapply_tac
110          
111 (* fwd **********************************************************************)
112
113 let fwd_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
114                   ~hyp ~dbd =
115    let find_type metasenv context =
116       let rec aux p = function
117          | Some (Cic.Name name, Cic.Decl t) :: _ when name = hyp -> p, t
118          | Some (Cic.Name name, Cic.Def (_, Some t)) :: _ when name = hyp -> p, t
119          | Some (Cic.Name name, Cic.Def (u, _)) :: tail when name = hyp ->
120             p, fst (TC.type_of_aux' metasenv tail u U.empty_ugraph)
121          | _ :: tail -> aux (succ p) tail
122          | [] -> error fail_msg1
123       in
124       aux 1 context
125    in
126    let lapply_tac to_what lemma = 
127       lapply_tac ~mk_fresh_name_callback ~how_many:1 ~to_what:[to_what] lemma
128    in
129    let fwd_simpl_tac status =
130       let (proof, goal) = status in
131       let _, metasenv, _, _ = proof in
132       let _, context, ty = CicUtil.lookup_meta goal metasenv in
133       let index, major = find_type metasenv context in 
134       match MetadataQuery.fwd_simpl ~dbd major with
135          | []       -> error fail_msg2
136          | uri :: _ -> 
137             Printf.eprintf "fwd: %s\n" (UriManager.string_of_uri uri); flush stderr;
138             let start = lapply_tac (Cic.Rel index) (Cic.Const (uri, [])) in  
139             let tac = T.thens ~start ~continuations:[PESR.clearbody hyp] in
140             PET.apply_tactic tac status
141    in
142    PET.mk_tactic fwd_simpl_tac