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 N = CicNotationPt
31 (* functions to be moved ****************************************************)
33 let list_map2_filter map l1 l2 =
34 let rec filter l = function
36 | None :: tl -> filter l tl
37 | Some a :: tl -> filter (a :: l) tl
39 filter [] (List.rev_map2 map l1 l2)
41 let rec list_split n l =
42 if n = 0 then [], l else
43 let l1, l2 = list_split (pred n) (List.tl l) in
46 let cont sep a = match sep with
48 | Some sep -> sep :: a
50 let list_rev_map_concat map sep a l =
51 let rec aux a = function
54 | x :: y :: l -> aux (sep :: map a x) (y :: l)
58 let is_atomic = function
66 | C.AImplicit _ -> true
69 (****************************************************************************)
72 type what = Cic.annterm
74 type using = Cic.annterm
77 type where = (name * name) option
78 type inferred = Cic.annterm
79 type pattern = Cic.annterm
81 type step = Note of note
82 | Theorem of name * what * note
85 | Intros of count option * name list * note
86 | Cut of name * what * note
87 | LetIn of name * what * note
88 | Rewrite of how * what * where * pattern * note
89 | Elim of what * using option * pattern * note
90 | Apply of what * note
91 | Change of inferred * what * where * pattern * note
92 | ClearBody of name * note
93 | Branch of step list list * note
95 (* annterm constructors *****************************************************)
97 let mk_arel i b = Cic.ARel ("", "", i, b)
99 (* grafite ast constructors *************************************************)
102 let floc = H.dummy_floc
104 let mk_note str = G.Comment (floc, G.Note (floc, str))
106 let mk_theorem name t =
107 let obj = N.Theorem (`Theorem, name, t, None) in
108 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
111 G.Executable (floc, G.Command (floc, G.Qed floc))
113 let mk_tactic tactic =
114 G.Executable (floc, G.Tactical (floc, G.Tactic (floc, tactic), None))
117 let tactic = G.IdTac floc in
120 let mk_intros xi ids =
121 let tactic = G.Intros (floc, xi, ids) in
124 let mk_cut name what =
125 let tactic = G.Cut (floc, Some name, what) in
128 let mk_letin name what =
129 let tactic = G.LetIn (floc, what, name) in
132 let mk_rewrite direction what where pattern =
133 let direction = if direction then `RightToLeft else `LeftToRight in
134 let pattern, rename = match where with
135 | None -> (None, [], Some pattern), []
136 | Some (premise, name) -> (None, [premise, pattern], None), [name]
138 let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
141 let mk_elim what using pattern =
142 let pattern = None, [], Some pattern in
143 let tactic = G.Elim (floc, what, using, pattern, Some 0, []) in
147 let tactic = G.Apply (floc, t) in
150 let mk_change t where pattern =
151 let pattern = match where with
152 | None -> None, [], Some pattern
153 | Some (premise, _) -> None, [premise, pattern], None
155 let tactic = G.Change (floc, pattern, t) in
158 let mk_clearbody id =
159 let tactic = G.ClearBody (floc, id) in
162 let mk_dot = G.Executable (floc, G.Tactical (floc, G.Dot floc, None))
164 let mk_sc = G.Executable (floc, G.Tactical (floc, G.Semicolon floc, None))
166 let mk_ob = G.Executable (floc, G.Tactical (floc, G.Branch floc, None))
168 let mk_cb = G.Executable (floc, G.Tactical (floc, G.Merge floc, None))
170 let mk_vb = G.Executable (floc, G.Tactical (floc, G.Shift floc, None))
172 (* rendering ****************************************************************)
174 let rec render_step sep a = function
175 | Note s -> mk_note s :: a
176 | Theorem (n, t, s) -> mk_note s :: mk_theorem n t :: a
177 | Qed s -> (* mk_note s :: *) mk_qed :: a
178 | Id s -> mk_note s :: cont sep (mk_id :: a)
179 | Intros (c, ns, s) -> mk_note s :: cont sep (mk_intros c ns :: a)
180 | Cut (n, t, s) -> mk_note s :: cont sep (mk_cut n t :: a)
181 | LetIn (n, t, s) -> mk_note s :: cont sep (mk_letin n t :: a)
182 | Rewrite (b, t, w, e, s) -> mk_note s :: cont sep (mk_rewrite b t w e :: a)
183 | Elim (t, xu, e, s) -> mk_note s :: cont sep (mk_elim t xu e :: a)
184 | Apply (t, s) -> mk_note s :: cont sep (mk_apply t :: a)
185 | Change (t, _, w, e, s) -> mk_note s :: cont sep (mk_change t w e :: a)
186 | ClearBody (n, s) -> mk_note s :: cont sep (mk_clearbody n :: a)
187 | Branch ([], s) -> a
188 | Branch ([ps], s) -> render_steps sep a ps
190 let a = mk_ob :: a in
191 let body = mk_cb :: list_rev_map_concat (render_steps None) mk_vb a pss in
192 mk_note s :: cont sep body
194 and render_steps sep a = function
196 | [p] -> render_step sep a p
197 | p :: Branch ([], _) :: ps ->
198 render_steps sep a (p :: ps)
199 | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
200 render_steps sep (render_step (Some mk_sc) a p) ps
202 render_steps sep (render_step (Some mk_dot) a p) ps
204 let render_steps a = render_steps None a
206 let render_steps sep a = assert false
208 (* counting *****************************************************************)
210 let rec count_step a = function
214 | Branch (pps, _) -> List.fold_left count_steps a pps
217 and count_steps a = List.fold_left count_step a