1 (* Copyright (C) 2003-2005, 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/.
27 module L = CicClassify
28 module P = ProceduralTypes
30 module DTI = DoubleTypeInference
31 module TC = CicTypeChecker
33 module UM = UriManager
34 module Obj = LibraryObjects
35 module HObj = HelmLibraryObjects
40 sorts : (Cic.id, Cic2acic.sort_kind) Hashtbl.t;
41 types : (C.id, A.anntypes) Hashtbl.t;
43 max_depth: int option;
49 (* helpers ******************************************************************)
51 let cic = D.deannotate_term
53 let split2_last l1 l2 =
54 let n = pred (List.length l1) in
55 let before1, after1 = P.list_split n l1 in
56 let before2, after2 = P.list_split n l2 in
57 before1, before2, List.hd after1, List.hd after2
59 let string_of_head = function
61 | C.AConst _ -> "const"
62 | C.AMutInd _ -> "mutind"
63 | C.AMutConstruct _ -> "mutconstruct"
67 | C.ALambda _ -> "lambda"
68 | C.ALetIn _ -> "letin"
70 | C.ACoFix _ -> "cofix"
73 | C.AMutCase _ -> "mutcase"
75 | C.AImplicit _ -> "implict"
77 let next st = {st with depth = succ st.depth; intros = []}
79 let add st entry intro =
80 {st with entries = entry :: st.entries; intros = intro :: st.intros}
83 let msg = Printf.sprintf "Depth %u: " st.depth in
84 match st.max_depth with
87 if st.depth < d then true, msg else false, "DEPTH EXCEDED"
90 let id = T.id_of_annterm v in
91 try Some ((Hashtbl.find st.types id).A.annsynthesized)
92 with Not_found -> None
94 (* proof construction *******************************************************)
96 let unused_premise = "UNUSED"
98 let get_intro name t = match name with
99 | C.Anonymous -> unused_premise
101 if DTI.does_not_occur 1 (cic t) then unused_premise else s
103 let mk_intros st script =
104 if st.intros = [] then script else
105 let count = List.length st.intros in
106 P.Intros (Some count, List.rev st.intros, "") :: script
108 let is_rewrite_right = function
109 | C.AConst (_, uri, []) ->
110 UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
113 let is_rewrite_left = function
114 | C.AConst (_, uri, []) ->
115 UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
118 let mk_premise = function
119 | C.ARel (_, _, _, binder) -> binder
122 let rec mk_fwd_proof st dtext name = function
123 | C.AAppl (_, hd :: tl) as v ->
124 if is_rewrite_right hd then
125 let what, where = List.nth tl 5, List.nth tl 3 in
126 let premise = mk_premise where in
127 [P.Rewrite (true, what, Some (premise, name), dtext)]
128 else if is_rewrite_left hd then
129 let what, where = List.nth tl 5, List.nth tl 3 in
130 let premise = mk_premise where in
131 [P.Rewrite (false, what, Some (premise, name), dtext)]
132 else begin match get_itype st v with
134 let qs = [[P.Id ""]; mk_proof (next st) v] in
135 [P.Branch (qs, ""); P.Cut (name, ty, dtext)]
137 let ty, _ = TC.type_of_aux' [] st.entries (cic hd) U.empty_ugraph in
138 let (classes, rc) as h = L.classify ty in
139 let text = Printf.sprintf "%u %s" (List.length classes) (L.to_string h) in
140 [P.LetIn (name, v, dtext ^ text)]
143 [P.LetIn (name, v, dtext)]
145 and mk_proof st = function
146 | C.ALambda (_, name, v, t) ->
147 let entry = Some (name, C.Decl (cic v)) in
148 let intro = get_intro name t in
149 mk_proof (add st entry intro) t
150 | C.ALetIn (_, name, v, t) as what ->
151 let proceed, dtext = test_depth st in
152 let script = if proceed then
153 let intro = get_intro name t in
154 let q = mk_proof (next st) t in
155 List.rev_append (mk_fwd_proof st dtext intro v) q
157 [P.Apply (what, dtext)]
160 | C.ARel _ as what ->
161 let _, dtext = test_depth st in
162 let script = [P.Apply (what, dtext)] in
164 | C.AAppl (_, hd :: tl) as t ->
165 let proceed, dtext = test_depth st in
166 let script = if proceed then
167 let ty, _ = TC.type_of_aux' [] st.entries (cic hd) U.empty_ugraph in
168 let (classes, rc) as h = L.classify ty in
169 let synth = L.S.singleton 0 in
170 let text = Printf.sprintf "%u %s" (List.length classes) (L.to_string h) in
172 | Some (i, j) when i > 1 ->
173 let classes, tl, _, what = split2_last classes tl in
174 let synth = L.S.add 1 synth in
175 let qs = mk_bkd_proofs (next st) synth classes tl in
176 if is_rewrite_right hd then
177 [P.Rewrite (false, what, None, dtext); P.Branch (qs, "")]
178 else if is_rewrite_left hd then
179 [P.Rewrite (true, what, None, dtext); P.Branch (qs, "")]
181 let using = Some hd in
182 [P.Elim (what, using, dtext ^ text); P.Branch (qs, "")]
184 let qs = mk_bkd_proofs (next st) synth classes tl in
185 [P.Apply (hd, dtext ^ text); P.Branch (qs, "")]
191 let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
192 let script = [P.Note text] in
195 and mk_bkd_proofs st synth classes ts =
196 let _, dtext = test_depth st in
198 if L.overlaps synth inv then None else
199 if L.S.is_empty inv then Some (mk_proof st v) else
200 Some [P.Apply (v, dtext ^ "dependent")]
202 P.list_map2_filter aux classes ts
204 (* object costruction *******************************************************)
206 let mk_obj st = function
207 | C.AConstant (_, _, s, Some v, t, [], _) ->
208 let ast = mk_proof st v in
209 let count = P.count_steps 0 ast in
210 let text = Printf.sprintf "tactics: %u" count in
211 P.Theorem (s, t, text) :: ast @ [P.Qed ""]
214 (* interface functions ******************************************************)
216 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types prefix aobj =
218 sorts = ids_to_inner_sorts;
219 types = ids_to_inner_types;
226 prerr_endline "Level 2 transformation";
227 let steps = mk_obj st aobj in
228 prerr_endline "grafite rendering";
229 List.rev (P.render_steps [] steps)