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_rev_map2 map l1 l2 =
34 let rec aux res = function
35 | hd1 :: tl1, hd2 :: tl2 -> aux (map hd1 hd2 :: res) (tl1, tl2)
40 let list_map2_filter map l1 l2 =
41 let rec filter l = function
43 | None :: tl -> filter l tl
44 | Some a :: tl -> filter (a :: l) tl
46 filter [] (list_rev_map2 map l1 l2)
48 (****************************************************************************)
51 type what = Cic.annterm
53 type using = Cic.annterm
56 type where = (name * name) option
57 type inferred = Cic.annterm
58 type pattern = Cic.annterm
60 type step = Note of note
61 | Theorem of name * what * note
64 | Intros of count option * name list * note
65 | Cut of name * what * note
66 | LetIn of name * what * note
67 | Rewrite of how * what * where * pattern * note
68 | Elim of what * using option * pattern * note
69 | Apply of what * note
70 | Change of inferred * what * where * pattern * note
71 | ClearBody of name * note
72 | Branch of step list list * note
74 (* annterm constructors *****************************************************)
76 let mk_arel i b = Cic.ARel ("", "", i, b)
78 (* grafite ast constructors *************************************************)
80 let floc = H.dummy_floc
82 let mk_note str = G.Comment (floc, G.Note (floc, str))
85 if str = "" then mk_note "" :: a else mk_note str :: mk_note "" :: a
87 let mk_theorem name t =
88 let obj = N.Theorem (`Theorem, name, t, None) in
89 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
92 G.Executable (floc, G.Command (floc, G.Qed floc))
94 let mk_tactic tactic punctation =
95 G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
97 let mk_punctation punctation =
98 G.Executable (floc, G.Tactic (floc, None, punctation))
100 let mk_id punctation =
101 let tactic = G.IdTac floc in
102 mk_tactic tactic punctation
104 let mk_intros xi ids punctation =
105 let tactic = G.Intros (floc, xi, ids) in
106 mk_tactic tactic punctation
108 let mk_cut name what punctation =
109 let tactic = G.Cut (floc, Some name, what) in
110 mk_tactic tactic punctation
112 let mk_letin name what punctation =
113 let tactic = G.LetIn (floc, what, name) in
114 mk_tactic tactic punctation
116 let mk_rewrite direction what where pattern punctation =
117 let direction = if direction then `RightToLeft else `LeftToRight in
118 let pattern, rename = match where with
119 | None -> (None, [], Some pattern), []
120 | Some (premise, name) -> (None, [premise, pattern], None), [name]
122 let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
123 mk_tactic tactic punctation
125 let mk_elim what using pattern punctation =
126 let pattern = None, [], Some pattern in
127 let tactic = G.Elim (floc, what, using, pattern, Some 0, []) in
128 mk_tactic tactic punctation
130 let mk_apply t punctation =
131 let tactic = G.Apply (floc, t) in
132 mk_tactic tactic punctation
134 let mk_change t where pattern punctation =
135 let pattern = match where with
136 | None -> None, [], Some pattern
137 | Some (premise, _) -> None, [premise, pattern], None
139 let tactic = G.Change (floc, pattern, t) in
140 mk_tactic tactic punctation
142 let mk_clearbody id punctation =
143 let tactic = G.ClearBody (floc, id) in
144 mk_tactic tactic punctation
147 let punctation = G.Branch floc in
148 mk_punctation punctation
150 let mk_dot = G.Dot floc
152 let mk_sc = G.Semicolon floc
154 let mk_cb = G.Merge floc
156 let mk_vb = G.Shift floc
158 (* rendering ****************************************************************)
160 let rec render_step sep a = function
161 | Note s -> mk_note s :: a
162 | Theorem (n, t, s) -> mk_theorem n t :: mk_note s :: a
163 | Qed s -> mk_qed :: mk_nlnote s a
164 | Id s -> mk_id sep :: mk_nlnote s a
165 | Intros (c, ns, s) -> mk_intros c ns sep :: mk_nlnote s a
166 | Cut (n, t, s) -> mk_cut n t sep :: mk_nlnote s a
167 | LetIn (n, t, s) -> mk_letin n t sep :: mk_nlnote s a
168 | Rewrite (b, t, w, e, s) -> mk_rewrite b t w e sep :: mk_nlnote s a
169 | Elim (t, xu, e, s) -> mk_elim t xu e sep :: mk_nlnote s a
170 | Apply (t, s) -> mk_apply t sep :: mk_nlnote s a
171 | Change (t, _, w, e, s) -> mk_change t w e sep :: mk_nlnote s a
172 | ClearBody (n, s) -> mk_clearbody n sep :: mk_nlnote s a
173 | Branch ([], s) -> a
174 | Branch ([ps], s) -> render_steps sep a ps
175 | Branch (ps :: pss, s) ->
176 let a = mk_ob :: mk_nlnote s a in
177 let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
178 mk_punctation sep :: render_steps mk_cb a ps
180 and render_steps sep a = function
182 | [p] -> render_step sep a p
183 | p :: Branch ([], _) :: ps ->
184 render_steps sep a (p :: ps)
185 | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
186 render_steps sep (render_step mk_sc a p) ps
188 render_steps sep (render_step mk_dot a p) ps
190 let render_steps a = render_steps mk_dot a
192 (* counting *****************************************************************)
194 let rec count_step a = function
198 | Branch (pps, _) -> List.fold_left count_steps a pps
201 and count_steps a = List.fold_left count_step a