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 module H = ProceduralHelpers
34 (* functions to be moved ****************************************************)
36 let list_rev_map2 map l1 l2 =
37 let rec aux res = function
38 | hd1 :: tl1, hd2 :: tl2 -> aux (map hd1 hd2 :: res) (tl1, tl2)
43 let list_map2_filter map l1 l2 =
44 let rec filter l = function
46 | None :: tl -> filter l tl
47 | Some a :: tl -> filter (a :: l) tl
49 filter [] (list_rev_map2 map l1 l2)
52 let rec aux a j = if j < 0 then a else aux (f j :: a) (pred j) in
55 (****************************************************************************)
57 type flavour = C.object_flavour
58 type name = string option
62 type using = C.annterm
65 type where = (hyp * name) option
66 type inferred = C.annterm
67 type pattern = C.annterm
68 type body = C.annterm option
69 type types = C.anninductiveType list
71 type fields = (string * bool * int) list
73 type step = Note of note
74 | Record of types * lpsno * fields * note
75 | Inductive of types * lpsno * note
76 | Statement of flavour * name * what * body * note
79 | Exact of what * note
80 | Intros of count option * name list * note
81 | Cut of name * what * note
82 | LetIn of name * what * note
83 | Rewrite of how * what * where * pattern * note
84 | Elim of what * using option * pattern * note
85 | Cases of what * pattern * note
86 | Apply of what * note
87 | Change of inferred * what * where * pattern * note
88 | Clear of hyp list * note
89 | ClearBody of hyp * note
90 | Branch of step list list * note
93 (* annterm constructors *****************************************************)
95 let mk_arel i b = C.ARel ("", "", i, b)
97 (* FG: this is really awful !! *)
98 let arel_of_name = function
99 | C.Name s -> mk_arel 0 s
100 | C.Anonymous -> mk_arel 0 "_"
102 (* helper functions on left params for use with inductive types *************)
104 let strip_lps lpsno arity =
105 let rec aux no lps = function
106 | C.AProd (_, name, w, t) when no > 0 ->
107 let lp = name, Some w in
108 aux (pred no) (lp :: lps) t
113 let merge_lps lps1 lps2 =
114 let map (n1, w1) (n2, _) =
115 let n = match n1, n2 with
121 if lps1 = [] then lps2 else
122 List.map2 map lps1 lps2
124 (* grafite ast constructors *************************************************)
126 let floc = HEL.dummy_floc
128 let mk_note str = G.Comment (floc, G.Note (floc, str))
130 let mk_tacnote str a =
131 if str = "" then mk_note "" :: a else mk_note "" :: mk_note str :: a
133 let mk_notenote str a =
134 if str = "" then a else mk_note str :: a
136 let mk_thnote str a =
137 if str = "" then a else mk_note "" :: mk_note str :: a
139 let mk_pre_inductive types lpsno =
140 let map1 (lps1, cons) (name, arity) =
141 let lps2, arity = strip_lps lpsno arity in
142 merge_lps lps1 lps2, (name, arity) :: cons
144 let map2 (lps1, types) (_, name, kind, arity, cons) =
145 let lps2, arity = strip_lps lpsno arity in
146 let lps1, rev_cons = List.fold_left map1 (lps1, []) cons in
147 merge_lps lps1 lps2, (name, kind, arity, List.rev rev_cons) :: types
149 let map3 (name, xw) = arel_of_name name, xw in
150 let rev_lps, rev_types = List.fold_left map2 ([], []) types in
151 List.rev_map map3 rev_lps, List.rev rev_types
153 let mk_inductive types lpsno =
154 let lpars, types = mk_pre_inductive types lpsno in
155 let obj = N.Inductive (lpars, types) in
156 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
158 let mk_record types lpsno fields =
159 match mk_pre_inductive types lpsno with
160 | lpars, [name, _, ty, [_, cty]] ->
161 let map (fields, cty) (name, coercion, arity) =
163 | C.AProd (_, _, w, t) ->
164 (name, w, coercion, arity) :: fields, t
168 let rev_fields, _ = List.fold_left map ([], cty) fields in
169 let fields = List.rev rev_fields in
170 let obj = N.Record (lpars, name, ty, fields) in
171 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
174 let mk_statement flavour name t v =
175 let name = match name with Some name -> name | None -> assert false in
176 let obj = N.Theorem (flavour, name, t, v) in
177 G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
180 G.Executable (floc, G.Command (floc, G.Qed floc))
182 let mk_tactic tactic punctation =
183 G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
185 let mk_punctation punctation =
186 G.Executable (floc, G.Tactic (floc, None, punctation))
188 let mk_id punctation =
189 let tactic = G.IdTac floc in
190 mk_tactic tactic punctation
192 let mk_exact t punctation =
193 let tactic = G.Exact (floc, t) in
194 mk_tactic tactic punctation
196 let mk_intros xi xids punctation =
197 let tactic = G.Intros (floc, (xi, xids)) in
198 mk_tactic tactic punctation
200 let mk_cut name what punctation =
201 let name = match name with Some name -> name | None -> assert false in
202 let tactic = G.Cut (floc, Some name, what) in
203 mk_tactic tactic punctation
205 let mk_letin name what punctation =
206 let name = match name with Some name -> name | None -> assert false in
207 let tactic = G.LetIn (floc, what, name) in
208 mk_tactic tactic punctation
210 let mk_rewrite direction what where pattern punctation =
211 let direction = if direction then `RightToLeft else `LeftToRight in
212 let pattern, rename = match where with
213 | None -> (None, [], Some pattern), []
214 | Some (premise, Some name) -> (None, [premise, pattern], None), [Some name]
215 | Some (premise, None) -> (None, [premise, pattern], None), []
217 let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
218 mk_tactic tactic punctation
220 let mk_elim what using pattern punctation =
221 let pattern = None, [], Some pattern in
222 let tactic = G.Elim (floc, what, using, pattern, (Some 0, [])) in
223 mk_tactic tactic punctation
225 let mk_cases what pattern punctation =
226 let pattern = None, [], Some pattern in
227 let tactic = G.Cases (floc, what, pattern, (Some 0, [])) in
228 mk_tactic tactic punctation
230 let mk_apply t punctation =
231 let tactic = G.Apply (floc, t) in
232 mk_tactic tactic punctation
234 let mk_change t where pattern punctation =
235 let pattern = match where with
236 | None -> None, [], Some pattern
237 | Some (premise, _) -> None, [premise, pattern], None
239 let tactic = G.Change (floc, pattern, t) in
240 mk_tactic tactic punctation
242 let mk_clear ids punctation =
243 let tactic = G.Clear (floc, ids) in
244 mk_tactic tactic punctation
246 let mk_clearbody id punctation =
247 let tactic = G.ClearBody (floc, id) in
248 mk_tactic tactic punctation
250 let mk_reflexivity punctation =
251 let tactic = G.Reflexivity floc in
252 mk_tactic tactic punctation
255 let punctation = G.Branch floc in
256 mk_punctation punctation
258 let mk_dot = G.Dot floc
260 let mk_sc = G.Semicolon floc
262 let mk_cb = G.Merge floc
264 let mk_vb = G.Shift floc
266 (* rendering ****************************************************************)
268 let rec render_step sep a = function
269 | Note s -> mk_notenote s a
270 | Statement (f, n, t, v, s) -> mk_statement f n t v :: mk_thnote s a
271 | Inductive (ts, lps, s) -> mk_inductive ts lps :: mk_thnote s a
272 | Record (ts, lps, fs, s) -> mk_record ts lps fs :: mk_thnote s a
273 | Qed s -> mk_qed :: mk_tacnote s a
274 | Exact (t, s) -> mk_exact t sep :: mk_tacnote s a
275 | Id s -> mk_id sep :: mk_tacnote s a
276 | Intros (c, ns, s) -> mk_intros c ns sep :: mk_tacnote s a
277 | Cut (n, t, s) -> mk_cut n t sep :: mk_tacnote s a
278 | LetIn (n, t, s) -> mk_letin n t sep :: mk_tacnote s a
279 | Rewrite (b, t, w, e, s) -> mk_rewrite b t w e sep :: mk_tacnote s a
280 | Elim (t, xu, e, s) -> mk_elim t xu e sep :: mk_tacnote s a
281 | Cases (t, e, s) -> mk_cases t e sep :: mk_tacnote s a
282 | Apply (t, s) -> mk_apply t sep :: mk_tacnote s a
283 | Change (t, _, w, e, s) -> mk_change t w e sep :: mk_tacnote s a
284 | Clear (ns, s) -> mk_clear ns sep :: mk_tacnote s a
285 | ClearBody (n, s) -> mk_clearbody n sep :: mk_tacnote s a
286 | Branch ([], s) -> a
287 | Branch ([ps], s) -> render_steps sep a ps
288 | Branch (ps :: pss, s) ->
289 let a = mk_ob :: mk_tacnote s a in
290 let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
291 mk_punctation sep :: render_steps mk_cb a ps
292 | Reflexivity s -> mk_reflexivity sep :: mk_tacnote s a
294 and render_steps sep a = function
296 | [p] -> render_step sep a p
297 | p :: Branch ([], _) :: ps ->
298 render_steps sep a (p :: ps)
299 | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
300 render_steps sep (render_step mk_sc a p) ps
302 render_steps sep (render_step mk_sc a p) ps
304 let render_steps a = render_steps mk_dot a
306 (* counting *****************************************************************)
308 let rec count_step a = function
314 | Branch (pps, _) -> List.fold_left count_steps a pps
318 | Intros (Some 0, [], _)
329 and count_steps a = List.fold_left count_step a
331 let count = I.count_nodes ~meta:false
333 let rec count_node a = function
347 | Apply (t, _) -> count a (H.cic t)
348 | Rewrite (_, t, _, p, _)
351 | Change (t, _, _, p, _) -> let a = count a (H.cic t) in count a (H.cic p)
352 | Branch (ss, _) -> List.fold_left count_nodes a ss
354 and count_nodes a = List.fold_left count_node a
356 (* helpers ******************************************************************)
358 let rec note_of_step = function
360 | Statement (_, _, _, _, s)
361 | Inductive (_, _, s)
362 | Record (_, _, _, s)
369 | Rewrite (_, _, _, _, s)
373 | Change (_, _, _, _, s)