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 PER = ProofEngineReduction
40 module P = ProceduralPreprocess
41 module Cl = ProceduralClassify
42 module M = ProceduralMode
43 module T = ProceduralTypes
44 module Cn = ProceduralConversion
47 sorts : (C.id, A.sort_kind) Hashtbl.t;
48 types : (C.id, A.anntypes) Hashtbl.t;
50 max_depth: int option;
56 (* helpers ******************************************************************)
60 let comp f g x = f (g x)
62 let cic = D.deannotate_term
64 let split2_last l1 l2 =
66 let n = pred (List.length l1) in
67 let before1, after1 = T.list_split n l1 in
68 let before2, after2 = T.list_split n l2 in
69 before1, before2, List.hd after1, List.hd after2
70 with Invalid_argument _ -> failwith "A2P.split2_last"
72 let string_of_head = function
74 | C.AConst _ -> "const"
75 | C.AMutInd _ -> "mutind"
76 | C.AMutConstruct _ -> "mutconstruct"
80 | C.ALambda _ -> "lambda"
81 | C.ALetIn _ -> "letin"
83 | C.ACoFix _ -> "cofix"
86 | C.AMutCase _ -> "mutcase"
88 | C.AImplicit _ -> "implict"
90 let clear st = {st with intros = []}
92 let next st = {(clear st) with depth = succ st.depth}
94 let add st entry intro =
95 {st with context = entry :: st.context; intros = intro :: st.intros}
99 let msg = Printf.sprintf "Depth %u: " st.depth in
100 match st.max_depth with
102 | Some d -> if st.depth < d then true, msg else false, "DEPTH EXCEDED: "
103 with Invalid_argument _ -> failwith "A2P.test_depth"
105 let is_rewrite_right = function
106 | C.AConst (_, uri, []) ->
107 UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
110 let is_rewrite_left = function
111 | C.AConst (_, uri, []) ->
112 UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
115 let is_fwd_rewrite_right hd tl =
116 if is_rewrite_right hd then match List.nth tl 3 with
121 let is_fwd_rewrite_left hd tl =
122 if is_rewrite_left hd then match List.nth tl 3 with
127 let get_ind_name uri tno xcno =
129 let ts = match E.get_obj Un.empty_ugraph uri with
130 | C.InductiveDefinition (ts, _, _,_), _ -> ts
133 let tname, cs = match List.nth ts tno with
134 | (name, _, _, cs) -> name, cs
138 | Some cno -> fst (List.nth cs (pred cno))
139 with Invalid_argument _ -> failwith "A2P.get_ind_name"
141 let get_inner_types st v =
143 let id = Ut.id_of_annterm v in
144 try match Hashtbl.find st.types id with
145 | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
146 | {A.annsynthesized = st; A.annexpected = None} -> Some (st, st)
147 with Not_found -> None
148 with Invalid_argument _ -> failwith "A2P.get_inner_types"
150 let get_inner_sort st v =
152 let id = Ut.id_of_annterm v in
153 try Hashtbl.find st.sorts id
154 with Not_found -> `Type (CicUniv.fresh())
155 with Invalid_argument _ -> failwith "A2P.get_sort"
157 (* proof construction *******************************************************)
159 let unused_premise = "UNUSED"
161 let defined_premise = "DEFINED"
163 let expanded_premise = "EXPANDED"
165 let convert st ?name v =
166 match get_inner_types st v with
169 let cst, cet = cic st, cic et in
170 if PER.alpha_equivalence cst cet then [] else
171 let e = Cn.mk_pattern [] (T.mk_arel 1 "") in
173 | None -> [T.Change (st, et, None, e, "")]
174 | Some id -> [T.Change (st, et, Some (id, id), e, ""); T.ClearBody (id, "")]
177 let id = Ut.id_of_annterm t in
178 let ty = C.AImplicit ("", None) in
179 let name i = Printf.sprintf "%s%u" expanded_premise i in
180 let lambda i t = C.ALambda (id, C.Name (name i), ty, t) in
181 let arg i n = T.mk_arel (n - i) (name (n - i - 1)) in
183 if i >= n then f, a else aux (succ i) (comp f (lambda i)) (arg i n :: a)
185 let absts, args = aux 0 identity [] in
186 match Cn.lift 1 n t with
187 | C.AAppl (id, ts) -> absts (C.AAppl (id, ts @ args))
188 | t -> absts (C.AAppl ("", t :: args))
190 let appl_expand n = function
191 | C.AAppl (id, ts) ->
192 let before, after = T.list_split (List.length ts + n) ts in
193 C.AAppl (id, C.AAppl ("", before) :: after)
196 let get_intro name t =
199 | C.Anonymous -> unused_premise
201 if DTI.does_not_occur 1 (cic t) then unused_premise else s
202 with Invalid_argument _ -> failwith "A2P.get_intro"
204 let mk_intros st script =
206 if st.intros = [] then script else
207 let count = List.length st.intros in
208 T.Intros (Some count, List.rev st.intros, "") :: script
209 with Invalid_argument _ -> failwith "A2P.mk_intros"
211 let rec mk_atomic st dtext what =
212 if T.is_atomic what then
214 | C.ARel (_, _, _, name) -> convert st ~name what, what
217 let name = defined_premise in
218 let script = convert st ~name what in
219 script @ mk_fwd_proof st dtext name what, T.mk_arel 0 name
221 and mk_fwd_rewrite st dtext name tl direction =
223 let what, where = List.nth tl 5, List.nth tl 3 in
224 let rps, predicate = [List.nth tl 4], List.nth tl 2 in
225 let e = Cn.mk_pattern rps predicate in
227 | C.ARel (_, _, _, premise) ->
228 let script, what = mk_atomic st dtext what in
229 T.Rewrite (direction, what, Some (premise, name), e, dtext) :: script
231 with e -> failwith ("mk_fwd_rewrite: " ^ Printexc.to_string e)
233 and mk_fwd_proof st dtext name = function
234 | C.ALetIn (_, n, v, t) ->
235 let entry = Some (n, C.Def (cic v, None)) in
236 let intro = get_intro n t in
237 let qt = mk_fwd_proof (add st entry intro) dtext name t in
238 let qv = mk_fwd_proof st "" intro v in
240 | C.AAppl (_, hd :: tl) as v ->
241 if is_fwd_rewrite_right hd tl then mk_fwd_rewrite st dtext name tl true else
242 if is_fwd_rewrite_left hd tl then mk_fwd_rewrite st dtext name tl false else
243 let ty, _ = TC.type_of_aux' [] st.context (cic hd) Un.empty_ugraph in
244 begin match get_inner_types st v with
245 | Some (ity, _) when M.bkd st.context ty ->
246 let qs = [[T.Id ""]; mk_proof (next st) v] in
247 [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
249 let (classes, rc) as h = Cl.classify st.context ty in
250 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
251 [T.LetIn (name, v, dtext ^ text)]
253 (* | C.AMutCase (id, uri, tyno, outty, arg, cases) as v ->
254 begin match Cn.mk_ind st.context id uri tyno outty arg cases with
255 | None -> [T.LetIn (name, v, dtext)]
256 | Some v -> mk_fwd_proof st dtext name v
258 *) | C.ACast (_, v, _) ->
259 mk_fwd_proof st dtext name v
261 match get_inner_types st v with
263 let qs = [[T.Id ""]; mk_proof (next st) v] in
264 [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
266 [T.LetIn (name, v, dtext)]
271 | C.ALambda (_, name, v, t) ->
272 let entry = Some (name, C.Decl (cic v)) in
273 let intro = get_intro name t in
274 mk_proof (add st entry intro) t
275 | C.ALetIn (_, name, v, t) as what ->
276 let proceed, dtext = test_depth st in
277 let script = if proceed then
278 let entry = Some (name, C.Def (cic v, None)) in
279 let intro = get_intro name t in
280 let q = mk_proof (next (add st entry intro)) t in
281 List.rev_append (mk_fwd_proof st dtext intro v) q
283 [T.Apply (what, dtext)]
286 | C.ARel _ as what ->
287 let _, dtext = test_depth st in
288 let text = "assumption" in
289 let script = [T.Apply (what, dtext ^ text)] in
291 | C.AMutConstruct _ as what ->
292 let _, dtext = test_depth st in
293 let script = [T.Apply (what, dtext)] in
295 | C.AAppl (_, hd :: tl) as t ->
296 let proceed, dtext = test_depth st in
297 let script = if proceed then
298 let ty, _ = TC.type_of_aux' [] st.context (cic hd) Un.empty_ugraph in
299 let (classes, rc) as h = Cl.classify st.context ty in
300 let premises, _ = P.split st.context ty in
301 let decurry = List.length classes - List.length tl in
302 if decurry < 0 then mk_proof (clear st) (appl_expand decurry t) else
303 if decurry > 0 then mk_proof (clear st) (eta_expand decurry t) else
304 let synth = I.S.singleton 0 in
305 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
307 | Some (i, j) when i > 1 && i <= List.length classes && M.is_eliminator premises ->
308 let classes, tl, _, what = split2_last classes tl in
309 let script, what = mk_atomic st dtext what in
310 let synth = I.S.add 1 synth in
311 let qs = mk_bkd_proofs (next st) synth classes tl in
312 if is_rewrite_right hd then
313 let rps, predicate = [List.nth tl 4], List.nth tl 2 in
314 let e = Cn.mk_pattern rps predicate in
315 List.rev script @ convert st t @
316 [T.Rewrite (false, what, None, e, dtext); T.Branch (qs, "")]
317 else if is_rewrite_left hd then
318 let rps, predicate = [List.nth tl 4], List.nth tl 2 in
319 let e = Cn.mk_pattern rps predicate in
320 List.rev script @ convert st t @
321 [T.Rewrite (true, what, None, e, dtext); T.Branch (qs, "")]
323 let using = Some hd in
324 List.rev script @ convert st t @
325 [T.Elim (what, using, dtext ^ text); T.Branch (qs, "")]
327 let qs = mk_bkd_proofs (next st) synth classes tl in
328 let script, hd = mk_atomic st dtext hd in
329 List.rev script @ convert st t @
330 [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
335 | C.AMutCase (id, uri, tyno, outty, arg, cases) ->
336 begin match Cn.mk_ind st.context id uri tyno outty arg cases with
338 let text = Printf.sprintf "%s" "UNEXPANDED: mutcase" in
339 let script = [T.Note text] in
341 (* | Some t -> mk_proof st t *)
343 | C.ACast (_, t, _) ->
346 let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
347 let script = [T.Note text] in
349 with e -> failwith ("mk_proof: " ^ Printexc.to_string e)
351 and mk_bkd_proofs st synth classes ts =
353 let _, dtext = test_depth st in
355 if I.overlaps synth inv then None else
356 if I.S.is_empty inv then Some (mk_proof st v) else
357 Some [T.Apply (v, dtext ^ "dependent")]
359 T.list_map2_filter aux classes ts
360 with Invalid_argument _ -> failwith "A2P.mk_bkd_proofs"
362 (* object costruction *******************************************************)
364 let is_theorem pars =
365 List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars ||
366 List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
368 let mk_obj st = function
369 | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
370 let ast = mk_proof st v in
371 let count = T.count_steps 0 ast in
372 let text = Printf.sprintf "tactics: %u" count in
373 T.Theorem (s, t, text) :: ast @ [T.Qed ""]
375 failwith "not a theorem"
377 (* interface functions ******************************************************)
379 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types ?depth prefix aobj =
381 sorts = ids_to_inner_sorts;
382 types = ids_to_inner_types;
389 HLog.debug "Level 2 transformation";
390 let steps = mk_obj st aobj in
391 HLog.debug "grafite rendering";
392 List.rev (T.render_steps [] steps)