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/.
29 module DTI = DoubleTypeInference
30 module TC = CicTypeChecker
32 module UM = UriManager
33 module Obj = LibraryObjects
34 module HObj = HelmLibraryObjects
37 module E = CicEnvironment
38 module PEH = ProofEngineHelpers
39 module PER = ProofEngineReduction
42 module P = ProceduralPreprocess
43 module Cl = ProceduralClassify
44 module T = ProceduralTypes
45 module Cn = ProceduralConversion
48 sorts : (C.id, A.sort_kind) Hashtbl.t;
49 types : (C.id, A.anntypes) Hashtbl.t;
51 max_depth: int option;
57 (* helpers ******************************************************************)
59 let cic = D.deannotate_term
61 let split2_last l1 l2 =
63 let n = pred (List.length l1) in
64 let before1, after1 = T.list_split n l1 in
65 let before2, after2 = T.list_split n l2 in
66 before1, before2, List.hd after1, List.hd after2
67 with Invalid_argument _ -> failwith "A2P.split2_last"
69 let string_of_head = function
71 | C.AConst _ -> "const"
72 | C.AMutInd _ -> "mutind"
73 | C.AMutConstruct _ -> "mutconstruct"
77 | C.ALambda _ -> "lambda"
78 | C.ALetIn _ -> "letin"
80 | C.ACoFix _ -> "cofix"
83 | C.AMutCase _ -> "mutcase"
85 | C.AImplicit _ -> "implict"
87 let clear st = {st with intros = []}
89 let next st = {(clear st) with depth = succ st.depth}
91 let add st entry intro =
92 {st with context = entry :: st.context; intros = intro :: st.intros}
96 let msg = Printf.sprintf "Depth %u: " st.depth in
97 match st.max_depth with
99 | Some d -> if st.depth < d then true, msg else false, "DEPTH EXCEDED: "
100 with Invalid_argument _ -> failwith "A2P.test_depth"
102 let is_rewrite_right = function
103 | C.AConst (_, uri, []) ->
104 UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
107 let is_rewrite_left = function
108 | C.AConst (_, uri, []) ->
109 UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
112 let is_fwd_rewrite_right hd tl =
113 if is_rewrite_right hd then match List.nth tl 3 with
118 let is_fwd_rewrite_left hd tl =
119 if is_rewrite_left hd then match List.nth tl 3 with
124 let get_ind_name uri tno xcno =
126 let ts = match E.get_obj Un.empty_ugraph uri with
127 | C.InductiveDefinition (ts, _, _,_), _ -> ts
130 let tname, cs = match List.nth ts tno with
131 | (name, _, _, cs) -> name, cs
135 | Some cno -> fst (List.nth cs (pred cno))
136 with Invalid_argument _ -> failwith "A2P.get_ind_name"
138 let get_inner_types st v =
140 let id = Ut.id_of_annterm v in
141 try match Hashtbl.find st.types id with
142 | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
143 | {A.annsynthesized = st; A.annexpected = None} -> Some (st, st)
144 with Not_found -> None
145 with Invalid_argument _ -> failwith "A2P.get_inner_types"
147 let get_inner_sort st v =
149 let id = Ut.id_of_annterm v in
150 try Hashtbl.find st.sorts id
151 with Not_found -> `Type (CicUniv.fresh())
152 with Invalid_argument _ -> failwith "A2P.get_sort"
154 let get_type msg st bo =
156 let ty, _ = TC.type_of_aux' [] st.context (cic bo) Un.empty_ugraph in
158 with e -> failwith (msg ^ ": " ^ Printexc.to_string e)
160 (* proof construction *******************************************************)
162 let unused_premise = "UNUSED"
164 let convert st ?name v =
165 match get_inner_types st v with
168 let cst, cet = cic st, cic et in
169 if PER.alpha_equivalence cst cet then [] else
170 let e = Cn.mk_pattern 0 (T.mk_arel 1 "") in
172 | None -> [T.Change (st, et, None, e, "")]
173 | Some id -> [T.Change (st, et, Some (id, id), e, ""); T.ClearBody (id, "")]
175 let get_intro = function
176 | C.Anonymous -> unused_premise
179 let mk_intros st script =
181 if st.intros = [] then script else
182 let count = List.length st.intros in
183 T.Intros (Some count, List.rev st.intros, "") :: script
184 with Invalid_argument _ -> failwith "A2P.mk_intros"
186 let rec mk_arg st = function
187 | C.ARel (_, _, _, name) as what -> convert st ~name what, what
190 and mk_fwd_rewrite st dtext name tl direction =
191 assert (List.length tl = 6);
192 let what, where, predicate = List.nth tl 5, List.nth tl 3, List.nth tl 2 in
193 let e = Cn.mk_pattern 1 predicate in
195 | C.ARel (_, _, _, premise) ->
196 let script, what = mk_arg st what in
197 T.Rewrite (direction, what, Some (premise, name), e, dtext) :: script
200 and mk_rewrite st dtext script t what qs tl direction =
201 assert (List.length tl = 5);
202 let predicate = List.nth tl 2 in
203 let e = Cn.mk_pattern 1 predicate in
204 List.rev script @ convert st t @
205 [T.Rewrite (direction, what, None, e, dtext); T.Branch (qs, "")]
207 and mk_fwd_proof st dtext name = function
208 | C.AAppl (_, hd :: tl) as v ->
209 if is_fwd_rewrite_right hd tl then mk_fwd_rewrite st dtext name tl true else
210 if is_fwd_rewrite_left hd tl then mk_fwd_rewrite st dtext name tl false else
211 let ty = get_type "TC1" st hd in
212 begin match get_inner_types st v with
214 let qs = [[T.Id ""]; mk_proof (next st) v] in
215 [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
217 let (classes, rc) as h = Cl.classify st.context ty in
218 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
219 [T.LetIn (name, v, dtext ^ text)]
221 | C.AMutCase _ -> assert false
222 | C.ACast _ -> assert false
224 match get_inner_types st v with
226 let qs = [[T.Id ""]; mk_proof (next st) v] in
227 [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
229 [T.LetIn (name, v, dtext)]
231 and mk_proof st = function
232 | C.ALambda (_, name, v, t) ->
233 let entry = Some (name, C.Decl (cic v)) in
234 let intro = get_intro name in
235 mk_proof (add st entry intro) t
236 | C.ALetIn (_, name, v, t) as what ->
237 let proceed, dtext = test_depth st in
238 let script = if proceed then
239 let entry = Some (name, C.Def (cic v, None)) in
240 let intro = get_intro name in
241 let q = mk_proof (next (add st entry intro)) t in
242 List.rev_append (mk_fwd_proof st dtext intro v) q
244 [T.Apply (what, dtext)]
247 | C.ARel _ as what ->
248 let _, dtext = test_depth st in
249 let text = "assumption" in
250 let script = [T.Apply (what, dtext ^ text)] in
252 | C.AMutConstruct _ as what ->
253 let _, dtext = test_depth st in
254 let script = [T.Apply (what, dtext)] in
256 | C.AAppl (_, hd :: tl) as t ->
257 let proceed, dtext = test_depth st in
258 let script = if proceed then
259 let ty = get_type "TC2" st hd in
260 let (classes, rc) as h = Cl.classify st.context ty in
261 let decurry = List.length classes - List.length tl in
262 if decurry <> 0 then begin
263 let msg = Printf.sprintf "Decurry: %i\nTerm: %s\nContext: %s"
264 decurry (Pp.ppterm (cic t)) (Pp.ppcontext st.context)
266 HLog.warn msg; assert false
268 let synth = I.S.singleton 0 in
269 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
272 let classes, tl, _, what = split2_last classes tl in
273 let script, what = mk_arg st what in
274 let synth = I.S.add 1 synth in
275 let qs = mk_bkd_proofs (next st) synth classes tl in
276 if is_rewrite_right hd then
277 mk_rewrite st dtext script t what qs tl false
278 else if is_rewrite_left hd then
279 mk_rewrite st dtext script t what qs tl true
281 let l = succ (List.length tl) in
282 let predicate = List.nth tl (l - i) in
283 let e = Cn.mk_pattern 0 (T.mk_arel 1 "") (* j predicate *) in
284 let using = Some hd in
285 List.rev script @ convert st t @
286 [T.Elim (what, using, e, dtext ^ text); T.Branch (qs, "")]
288 let qs = mk_bkd_proofs (next st) synth classes tl in
289 let script, hd = mk_arg st hd in
290 List.rev script @ convert st t @
291 [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
296 | C.AMutCase _ -> assert false
297 | C.ACast _ -> assert false
299 let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
300 let script = [T.Note text] in
303 and mk_bkd_proofs st synth classes ts =
305 let _, dtext = test_depth st in
307 if I.overlaps synth inv then None else
308 if I.S.is_empty inv then Some (mk_proof st v) else
309 Some [T.Apply (v, dtext ^ "dependent")]
311 T.list_map2_filter aux classes ts
312 with Invalid_argument _ -> failwith "A2P.mk_bkd_proofs"
314 (* object costruction *******************************************************)
316 let is_theorem pars =
317 List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars ||
318 List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
320 let mk_obj st = function
321 | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
322 let ast = mk_proof st v in
323 let count = T.count_steps 0 ast in
324 let text = Printf.sprintf "tactics: %u" count in
325 T.Theorem (s, t, text) :: ast @ [T.Qed ""]
327 failwith "not a theorem"
329 (* interface functions ******************************************************)
331 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types ?depth prefix aobj =
333 sorts = ids_to_inner_sorts;
334 types = ids_to_inner_types;
341 HLog.debug "Level 2 transformation";
342 let steps = mk_obj st aobj in
343 HLog.debug "grafite rendering";
344 List.rev (T.render_steps [] steps)