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 Cl = ProceduralClassify
41 module M = ProceduralMode
42 module T = ProceduralTypes
43 module Cn = ProceduralConversion
46 sorts : (C.id, A.sort_kind) Hashtbl.t;
47 types : (C.id, A.anntypes) Hashtbl.t;
49 max_depth: int option;
55 (* helpers ******************************************************************)
59 let comp f g x = f (g x)
61 let cic = D.deannotate_term
63 let split2_last l1 l2 =
65 let n = pred (List.length l1) in
66 let before1, after1 = T.list_split n l1 in
67 let before2, after2 = T.list_split n l2 in
68 before1, before2, List.hd after1, List.hd after2
69 with Invalid_argument _ -> failwith "A2P.split2_last"
71 let string_of_head = function
73 | C.AConst _ -> "const"
74 | C.AMutInd _ -> "mutind"
75 | C.AMutConstruct _ -> "mutconstruct"
79 | C.ALambda _ -> "lambda"
80 | C.ALetIn _ -> "letin"
82 | C.ACoFix _ -> "cofix"
85 | C.AMutCase _ -> "mutcase"
87 | C.AImplicit _ -> "implict"
89 let clear st = {st with intros = []}
91 let next st = {(clear st) with depth = succ st.depth}
93 let add st entry intro =
94 {st with context = entry :: st.context; intros = intro :: st.intros}
98 let msg = Printf.sprintf "Depth %u: " st.depth in
99 match st.max_depth with
101 | Some d -> if st.depth < d then true, msg else false, "DEPTH EXCEDED: "
102 with Invalid_argument _ -> failwith "A2P.test_depth"
104 let is_rewrite_right = function
105 | C.AConst (_, uri, []) ->
106 UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
109 let is_rewrite_left = function
110 | C.AConst (_, uri, []) ->
111 UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
114 let is_fwd_rewrite_right hd tl =
115 if is_rewrite_right hd then match List.nth tl 3 with
120 let is_fwd_rewrite_left hd tl =
121 if is_rewrite_left hd then match List.nth tl 3 with
126 let get_ind_name uri tno xcno =
128 let ts = match E.get_obj Un.empty_ugraph uri with
129 | C.InductiveDefinition (ts, _, _,_), _ -> ts
132 let tname, cs = match List.nth ts tno with
133 | (name, _, _, cs) -> name, cs
137 | Some cno -> fst (List.nth cs (pred cno))
138 with Invalid_argument _ -> failwith "A2P.get_ind_name"
140 let get_inner_types st v =
142 let id = Ut.id_of_annterm v in
143 try match Hashtbl.find st.types id with
144 | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
145 | {A.annsynthesized = st; A.annexpected = None} -> Some (st, st)
146 with Not_found -> None
147 with Invalid_argument _ -> failwith "A2P.get_inner_types"
149 let get_inner_sort st v =
151 let id = Ut.id_of_annterm v in
152 try Hashtbl.find st.sorts id
153 with Not_found -> `Type (CicUniv.fresh())
154 with Invalid_argument _ -> failwith "A2P.get_sort"
156 (* proof construction *******************************************************)
158 let unused_premise = "UNUSED"
160 let defined_premise = "DEFINED"
162 let expanded_premise = "EXPANDED"
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
171 | None -> [T.Change (st, et, None, "")]
172 | Some id -> [T.Change (st, et, Some (id, id), ""); T.ClearBody (id, "")]
175 let id = Ut.id_of_annterm t in
176 let ty = C.AImplicit ("", None) in
177 let name i = Printf.sprintf "%s%u" expanded_premise i in
178 let lambda i t = C.ALambda (id, C.Name (name i), ty, t) in
179 let arg i n = T.mk_arel (n - i) (name (n - i - 1)) in
181 if i >= n then f, a else aux (succ i) (comp f (lambda i)) (arg i n :: a)
183 let absts, args = aux 0 identity [] in
184 match Cn.lift 1 n t with
185 | C.AAppl (id, ts) -> absts (C.AAppl (id, ts @ args))
186 | t -> absts (C.AAppl ("", t :: args))
188 let appl_expand n = function
189 | C.AAppl (id, ts) ->
190 let before, after = T.list_split (List.length ts + n) ts in
191 C.AAppl ("", C.AAppl (id, before) :: after)
194 let get_intro name t =
197 | C.Anonymous -> unused_premise
199 if DTI.does_not_occur 1 (cic t) then unused_premise else s
200 with Invalid_argument _ -> failwith "A2P.get_intro"
202 let mk_intros st script =
204 if st.intros = [] then script else
205 let count = List.length st.intros in
206 T.Intros (Some count, List.rev st.intros, "") :: script
207 with Invalid_argument _ -> failwith "A2P.mk_intros"
209 let rec mk_atomic st dtext what =
210 if T.is_atomic what then
212 | C.ARel (_, _, _, name) -> convert st ~name what, what
215 let name = defined_premise in
216 let script = convert st ~name what in
217 script @ mk_fwd_proof st dtext name what, T.mk_arel 0 name
219 and mk_fwd_rewrite st dtext name tl direction =
220 let what, where = List.nth tl 5, List.nth tl 3 in
222 | C.ARel (_, _, _, premise) ->
223 let script, what = mk_atomic st dtext what in
224 T.Rewrite (direction, what, Some (premise, name), dtext) :: script
227 and mk_fwd_proof st dtext name = function
228 | C.AAppl (_, hd :: tl) as v ->
229 if is_fwd_rewrite_right hd tl then mk_fwd_rewrite st dtext name tl true else
230 if is_fwd_rewrite_left hd tl then mk_fwd_rewrite st dtext name tl false else
231 let ty, _ = TC.type_of_aux' [] st.context (cic hd) Un.empty_ugraph in
232 begin match get_inner_types st v with
233 | Some (ity, _) when M.bkd st.context ty ->
234 let qs = [[T.Id ""]; mk_proof (next st) v] in
235 [T.Branch (qs, ""); T.Cut (name, ity, dtext)]
237 let (classes, rc) as h = Cl.classify st.context ty in
238 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
239 [T.LetIn (name, v, dtext ^ text)]
241 | C.AMutCase (id, uri, tyno, outty, arg, cases) as v ->
242 begin match Cn.mk_ind st.context id uri tyno outty arg cases with
243 | None -> [T.LetIn (name, v, dtext)]
244 | Some v -> mk_fwd_proof st dtext name v
247 [T.LetIn (name, v, dtext)]
249 and mk_proof st = function
250 | C.ALambda (_, name, v, t) ->
251 let entry = Some (name, C.Decl (cic v)) in
252 let intro = get_intro name t in
253 mk_proof (add st entry intro) t
254 | C.ALetIn (_, name, v, t) as what ->
255 let proceed, dtext = test_depth st in
256 let script = if proceed then
257 let entry = Some (name, C.Def (cic v, None)) in
258 let intro = get_intro name t in
259 let q = mk_proof (next (add st entry intro)) t in
260 List.rev_append (mk_fwd_proof st dtext intro v) q
262 [T.Apply (what, dtext)]
265 | C.ARel _ as what ->
266 let _, dtext = test_depth st in
267 let text = "assumption" in
268 let script = [T.Apply (what, dtext ^ text)] in
270 | C.AMutConstruct _ as what ->
271 let _, dtext = test_depth st in
272 let script = [T.Apply (what, dtext)] in
274 | C.AAppl (_, hd :: tl) as t ->
275 let proceed, dtext = test_depth st in
276 let script = if proceed then
277 let ty, _ = TC.type_of_aux' [] st.context (cic hd) Un.empty_ugraph in
278 let (classes, rc) as h = Cl.classify st.context ty in
279 let premises, _ = Cl.split st.context ty in
280 let decurry = List.length classes - List.length tl in
281 if decurry < 0 then mk_proof (clear st) (appl_expand decurry t) else
282 if decurry > 0 then mk_proof (clear st) (eta_expand decurry t) else
283 let synth = I.S.singleton 0 in
284 let text = Printf.sprintf "%u %s" (List.length classes) (Cl.to_string h) in
286 | Some (i, j) when i > 1 && i <= List.length classes && M.is_eliminator premises ->
287 let classes, tl, _, what = split2_last classes tl in
288 let script, what = mk_atomic st dtext what in
289 let synth = I.S.add 1 synth in
290 let qs = mk_bkd_proofs (next st) synth classes tl in
291 if is_rewrite_right hd then
292 List.rev script @ convert st t @
293 [T.Rewrite (false, what, None, dtext); T.Branch (qs, "")]
294 else if is_rewrite_left hd then
295 List.rev script @ convert st t @
296 [T.Rewrite (true, what, None, dtext); T.Branch (qs, "")]
298 let using = Some hd in
299 List.rev script @ convert st t @
300 [T.Elim (what, using, dtext ^ text); T.Branch (qs, "")]
302 let qs = mk_bkd_proofs (next st) synth classes tl in
303 let script, hd = mk_atomic st dtext hd in
304 List.rev script @ convert st t @
305 [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
310 | C.AMutCase (id, uri, tyno, outty, arg, cases) ->
311 begin match Cn.mk_ind st.context id uri tyno outty arg cases with
313 let text = Printf.sprintf "%s" "UNEXPANDED: mutcase" in
314 let script = [T.Note text] in
316 (* | Some t -> mk_proof st t *)
319 let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head t) in
320 let script = [T.Note text] in
323 and mk_bkd_proofs st synth classes ts =
325 let _, dtext = test_depth st in
327 if I.overlaps synth inv then None else
328 if I.S.is_empty inv then Some (mk_proof st v) else
329 Some [T.Apply (v, dtext ^ "dependent")]
331 T.list_map2_filter aux classes ts
332 with Invalid_argument _ -> failwith "A2P.mk_bkd_proofs"
334 (* object costruction *******************************************************)
336 let is_theorem pars =
337 List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars ||
338 List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
340 let mk_obj st = function
341 | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
342 let ast = mk_proof st v in
343 let count = T.count_steps 0 ast in
344 let text = Printf.sprintf "tactics: %u" count in
345 T.Theorem (s, t, text) :: ast @ [T.Qed ""]
347 failwith "not a theorem"
349 (* interface functions ******************************************************)
351 let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types ?depth prefix aobj =
353 sorts = ids_to_inner_sorts;
354 types = ids_to_inner_types;
361 HLog.debug "Level 2 transformation";
362 let steps = mk_obj st aobj in
363 HLog.debug "grafite rendering";
364 List.rev (T.render_steps [] steps)