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/.
30 module N = CicNotationPt
32 (* functions to be moved ****************************************************)
34 let rec list_split n l =
35 if n = 0 then [],l else
36 let l1, l2 = list_split (pred n) (List.tl l) in
39 let cont sep a = match sep with
41 | Some sep -> sep :: a
43 let list_rev_map_concat map sep a l =
44 let rec aux a = function
47 | x :: y :: l -> aux (sep :: map a x) (y :: l)
51 (****************************************************************************)
54 type what = Cic.annterm
55 type using = Cic.annterm
59 type step = Note of note
60 | Theorem of name * what * note
62 | Intros of count option * name list * note
63 | Elim of what * using option * note
64 | LetIn of name * what * note
65 | Apply of what * note
66 | Exact of what * note
67 | Branch of step list list * note
69 (* annterm constructors *****************************************************)
71 let mk_arel i b = Cic.ARel ("", "", i, b)
73 (* level 2 transformation ***************************************************)
75 let mk_name = function
77 | None -> "UNUSED" (**)
79 let mk_intros_arg = function
80 | `Declaration {C.dec_name = name}
81 | `Hypothesis {C.dec_name = name}
82 | `Definition {C.def_name = name} -> mk_name name
83 | `Joint _ -> assert false
84 | `Proof _ -> assert false
86 let mk_intros_args pc = List.map mk_intros_arg pc
88 let split_inductive n tl =
89 let l1, l2 = list_split (int_of_string n) tl in
90 List.hd (List.rev l2), l1
92 let rec mk_apply_term aref ac ds cargs =
93 let step0 = mk_arg true (ac, [], ds) (List.hd cargs) in
94 let ac, row, ds = List.fold_left (mk_arg false) step0 (List.tl cargs) in
95 ac, ds, Cic.AAppl (aref, List.rev row)
97 and mk_delta ac ds = match ac with
99 let cmethod = p.C.proof_conclude.C.conclude_method in
100 let cargs = p.C.proof_conclude.C.conclude_args in
101 let capply = p.C.proof_apply_context in
102 let ccont = p.C.proof_context in
103 let caref = p.C.proof_conclude.C.conclude_aref in
104 begin match cmethod with
106 | "Apply" when ccont = [] && capply = [] ->
107 let ac, ds, what = mk_apply_term caref ac ds cargs in
108 let name = "PREVIOUS" in
109 ac, mk_arel 1 name, LetIn (name, what, "") :: ds
110 | _ -> ac, mk_arel 1 "COMPOUND", ds
114 and mk_arg first (ac, row, ds) = function
115 | C.Lemma {C.lemma_id = aref; C.lemma_uri = uri} ->
116 let t = match CicUtil.term_of_uri (U.uri_of_string uri) with
117 | Cic.Const (uri, _) -> Cic.AConst (aref, uri, [])
118 | Cic.MutConstruct (uri, tno, cno, _) ->
119 Cic.AMutConstruct (aref, uri, tno, cno, [])
123 | C.Premise {C.premise_n = Some i; C.premise_binder = Some b} ->
124 ac, mk_arel i b :: row, ds
125 | C.Premise {C.premise_n = None; C.premise_binder = None} ->
126 let ac, arg, ds = mk_delta ac ds in
128 | C.Term t when first -> ac, t :: row, ds
129 | C.Term _ -> ac, Cic.AImplicit ("", None) :: row, ds
130 | C.Premise _ -> assert false
131 | C.ArgMethod _ -> assert false
132 | C.ArgProof _ -> assert false
133 | C.Aux _ -> assert false
136 let names = mk_intros_args p.C.proof_context in
137 let count = List.length names in
138 if count > 0 then Intros (Some count, names, "") :: mk_proof_body p
141 and mk_proof_body p =
142 let cmethod = p.C.proof_conclude.C.conclude_method in
143 let cargs = p.C.proof_conclude.C.conclude_args in
144 let capply = p.C.proof_apply_context in
145 let caref = p.C.proof_conclude.C.conclude_aref in
146 match cmethod, cargs with
147 | "Intros+LetTac", [C.ArgProof p] -> mk_proof p
148 | "ByInduction", C.Aux n :: C.Term (Cic.AAppl (_, using :: _)) :: tl ->
149 let whatm, ms = split_inductive n tl in (* actual rx params here *)
150 let _, row, ds = mk_arg true (List.rev capply, [], []) whatm in
151 let what, qs = List.hd row, mk_subproofs ms in
152 List.rev ds @ [Elim (what, Some using, ""); Branch (qs, "")]
154 let ac, ds, what = mk_apply_term caref (List.rev capply) [] cargs in
155 let qs = List.map mk_proof ac in
156 List.rev ds @ [Apply (what, ""); Branch (qs, "")]
159 Printf.sprintf "UNEXPANDED %s %u" cmethod (List.length cargs)
162 and mk_subproofs cargs =
163 let mk_subproof proofs = function
164 | C.ArgProof ({C.proof_name = Some n} as p) ->
165 (Note n :: mk_proof p) :: proofs
166 | C.ArgProof ({C.proof_name = None} as p) ->
167 (Note "" :: mk_proof p) :: proofs
170 List.rev (List.fold_left mk_subproof [] cargs)
172 let mk_obj ids_to_inner_sorts prefix (_, params, xmenv, obj) =
173 if List.length params > 0 || xmenv <> None then assert false;
175 | `Def (C.Const, t, `Proof ({C.proof_name = Some name} as p)) ->
176 Theorem (name, t, "") :: mk_proof p @ [Qed ""]
179 (* grafite ast constructors *************************************************)
181 let floc = H.dummy_floc
183 let mk_note str = G.Comment (floc, G.Note (floc, str))
185 let mk_theorem name t =
186 let obj = N.Theorem (`Theorem, name, t, None) in
187 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
190 G.Executable (floc, G.Command (floc, G.Qed floc))
192 let mk_tactic tactic =
193 G.Executable (floc, G.Tactical (floc, G.Tactic (floc, tactic), None))
195 let mk_intros xi ids =
196 let tactic = G.Intros (floc, xi, ids) in
199 let mk_elim what using =
200 let tactic = G.Elim (floc, what, using, Some 0, []) in
203 let mk_letin name what =
204 let tactic = G.LetIn (floc, what, name) in
208 let tactic = G.Apply (floc, t) in
212 let tactic = G.Exact (floc, t) in
215 let mk_dot = G.Executable (floc, G.Tactical (floc, G.Dot floc, None))
217 let mk_sc = G.Executable (floc, G.Tactical (floc, G.Semicolon floc, None))
219 let mk_ob = G.Executable (floc, G.Tactical (floc, G.Branch floc, None))
221 let mk_cb = G.Executable (floc, G.Tactical (floc, G.Merge floc, None))
223 let mk_vb = G.Executable (floc, G.Tactical (floc, G.Shift floc, None))
225 (* rendering ****************************************************************)
227 let rec render_step sep a = function
228 | Note s -> mk_note s :: a
229 | Theorem (n, t, s) -> mk_note s :: mk_theorem n t :: a
230 | Qed s -> mk_note s :: mk_qed :: a
231 | Intros (c, ns, s) -> mk_note s :: cont sep (mk_intros c ns :: a)
232 | Elim (t, xu, s) -> mk_note s :: cont sep (mk_elim t xu :: a)
233 | LetIn (n, t, s) -> mk_note s :: cont sep (mk_letin n t :: a)
234 | Apply (t, s) -> mk_note s :: cont sep (mk_apply t :: a)
235 | Exact (t, s) -> mk_note s :: cont sep (mk_exact t :: a)
236 | Branch ([], s) -> a
237 | Branch ([ps], s) -> render_steps a ps
239 let a = mk_ob :: a in
240 let body = mk_cb :: list_rev_map_concat render_steps mk_vb a pss in
241 mk_note s :: cont sep body
243 and render_steps a = function
245 | [p] -> render_step None a p
246 | (Note _ | Theorem _ | Qed _ as p) :: ps ->
247 render_steps (render_step None a p) ps
248 | p :: ((Branch ([], _) :: _) as ps) ->
249 render_steps (render_step None a p) ps
250 | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
251 render_steps (render_step (Some mk_sc) a p) ps
253 render_steps (render_step (Some mk_dot) a p) ps
255 (* interface functions ******************************************************)
257 let content2procedural ~ids_to_inner_sorts prefix cobj =
258 prerr_endline "Level 2 transformation";
259 let steps = mk_obj ids_to_inner_sorts prefix cobj in
260 prerr_endline "grafite rendering";
261 List.rev (render_steps [] steps)