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