1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 module MI = CicMkImplicit
27 module TC = CicTypeChecker
28 module PET = ProofEngineTypes
29 module PEH = ProofEngineHelpers
31 module S = CicSubstitution
32 module PT = PrimitiveTactics
34 let fail_msg1 = "no applicable simplification"
36 let error msg = raise (PET.Fail msg)
38 (* lapply *******************************************************************)
40 let lapply_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
41 (* ?(substs = []) *) what =
42 let count_dependent_prods context t =
43 let rec aux context p = function
44 | Cic.Prod (name, t1, t2) ->
45 if TC.does_not_occur context 0 1 t2 then p else
46 let e = Some (name, Cic.Decl t1) in
47 aux (e :: context) (succ p) t2
52 let rec pad_context p context add_context =
53 if List.length add_context >= p then add_context @ context
54 else pad_context p context (None :: add_context)
56 let strip_dependent_prods metasenv context p t =
57 let rec aux metasenv add_context q = function
58 | Cic.Prod (name, t1, t2) when q > 0 ->
59 let context_for_meta = pad_context p context add_context in
60 let metasenv, index = MI.mk_implicit metasenv [] context_for_meta in
61 let rs = MI.identity_relocation_list_for_metavariable context_for_meta in
62 let e, s = Some (name, Cic.Decl t1), Cic.Meta (index, rs) in
63 aux metasenv (e :: add_context) (pred q) (S.subst s t2)
64 | t -> metasenv, add_context, t
68 let mk_body bo = function
69 | Some (name, Cic.Decl t1) -> Cic.Lambda (name, t1, bo)
70 | _ -> failwith "mk_body"
72 let lapply_tac (proof, goal) =
73 let xuri, metasenv, u, t = proof in
75 let metano, context, ty = CicUtil.lookup_meta goal metasenv in
76 let lemma, _ = TC.type_of_aux' metasenv context what U.empty_ugraph in
77 let p = count_dependent_prods context lemma in
79 let metasenv, add_context, holed_lemma = strip_dependent_prods metasenv context p lemma in
80 let proof = xuri, metasenv, u, t in
81 let newmeta = MI.new_meta metasenv [] in
82 let context = add_context @ context in
83 let irl = MI.identity_relocation_list_for_metavariable context in
84 let bo = List.fold_left mk_body (Cic.Meta (newmeta, irl)) add_context in
85 let ty = S.lift p ty in
86 let (xuri, metasenv, u, t), _ =
87 PEH.subst_meta_in_proof proof metano bo [newmeta, context, ty]
89 prerr_endline (CicPp.ppterm holed_lemma);
91 let status = (xuri, metasenv, u, t), newmeta in
92 PET.apply_tactic (PT.cut_tac ~mk_fresh_name_callback holed_lemma) status
94 PET.mk_tactic lapply_tac
96 (* fwd **********************************************************************)
98 let fwd_simpl_tac ~what ~dbd =
99 let fwd_simpl_tac status =
100 let (proof, goal) = status in
101 let _, metasenv, _, _ = proof in
102 let _, context, ty = CicUtil.lookup_meta goal metasenv in
103 let major, _ = TC.type_of_aux' metasenv context what U.empty_ugraph in
104 match MetadataQuery.fwd_simpl ~dbd major with
105 | [] -> error fail_msg1
106 | uri :: _ -> prerr_endline (UriManager.string_of_uri uri); (proof, [])
108 PET.mk_tactic fwd_simpl_tac