]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/proceduralTypes.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / acic_procedural / proceduralTypes.ml
1 (* Copyright (C) 2003-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module HEL = HExtlib
27 module C   = Cic
28 module I   = CicInspect
29 module G   = GrafiteAst
30 module N   = CicNotationPt
31
32 module H   = ProceduralHelpers
33
34 (* functions to be moved ****************************************************)
35
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)
39       | _                      -> res
40    in
41    aux [] (l1, l2)
42
43 let list_map2_filter map l1 l2 =
44    let rec filter l = function
45       | []           -> l
46       | None :: tl   -> filter l tl
47       | Some a :: tl -> filter (a :: l) tl 
48   in 
49   filter [] (list_rev_map2 map l1 l2)
50
51 let list_init f i =
52    let rec aux a j = if j < 0 then a else aux (f j :: a) (pred j) in
53    aux [] i
54
55 (****************************************************************************)
56
57 type flavour  = C.object_flavour
58 type name     = string option
59 type hyp      = string
60 type what     = C.annterm
61 type how      = bool
62 type using    = C.annterm
63 type count    = int
64 type note     = string
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
70 type lpsno    = int
71 type fields   = (string * bool * int) list
72
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
77           | Qed of note
78           | Id of 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           | LApply of name * what * note
84           | Rewrite of how * what * where * pattern * note
85           | Elim of what * using option * pattern * note
86           | Cases of what * pattern * note
87           | Apply of what * note
88           | Change of inferred * what * where * pattern * note 
89           | Clear of hyp list * note
90           | ClearBody of hyp * note
91           | Branch of step list list * note
92           | Reflexivity of note
93
94 (* annterm constructors *****************************************************)
95
96 let mk_arel i b = C.ARel ("", "", i, b)
97
98 (* FG: this is really awful !! *)
99 let arel_of_name = function
100    | C.Name s    -> mk_arel 0 s
101    | C.Anonymous -> mk_arel 0 "_"
102
103 (* helper functions on left params for use with inductive types *************)
104
105 let strip_lps lpsno arity =
106    let rec aux no lps = function
107       | C.AProd (_, name, w, t) when no > 0 ->
108          let lp = name, Some w in
109          aux (pred no) (lp :: lps) t
110       | t                                   -> lps, t
111    in
112    aux lpsno [] arity
113
114 let merge_lps lps1 lps2 =
115    let map (n1, w1) (n2, _) =
116       let n = match n1, n2 with
117          | C.Name _, _ -> n1
118          | _           -> n2
119       in
120       n, w1
121    in
122    if lps1 = [] then lps2 else
123    List.map2 map lps1 lps2
124
125 (* grafite ast constructors *************************************************)
126
127 let floc = HEL.dummy_floc
128
129 let mk_note str = G.Comment (floc, G.Note (floc, str))
130
131 let mk_tacnote str a =
132    if str = "" then mk_note "" :: a else mk_note "" :: mk_note str :: a
133
134 let mk_notenote str a =
135    if str = "" then a else mk_note str :: a
136
137 let mk_thnote str a =
138    if str = "" then a else mk_note "" :: mk_note str :: a
139
140 let mk_pre_inductive types lpsno =
141    let map1 (lps1, cons) (name, arity) = 
142       let lps2, arity = strip_lps lpsno arity in
143       merge_lps lps1 lps2, (name, arity) :: cons
144    in
145    let map2 (lps1, types) (_, name, kind, arity, cons) =
146       let lps2, arity = strip_lps lpsno arity in 
147       let lps1, rev_cons = List.fold_left map1 (lps1, []) cons in 
148       merge_lps lps1 lps2, (name, kind, arity, List.rev rev_cons) :: types
149    in
150    let map3 (name, xw) = arel_of_name name, xw in
151    let rev_lps, rev_types = List.fold_left map2 ([], []) types in
152    List.rev_map map3 rev_lps, List.rev rev_types
153
154 let mk_inductive types lpsno =
155    let lpars, types = mk_pre_inductive types lpsno in
156    let obj = N.Inductive (lpars, types) in
157    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
158
159 let mk_record types lpsno fields =
160    match mk_pre_inductive types lpsno with
161       | lpars, [name, _, ty, [_, cty]] -> 
162          let map (fields, cty) (name, coercion, arity) =
163             match cty with
164                | C.AProd (_, _, w, t) ->
165                   (name, w, coercion, arity) :: fields, t
166                | _                    ->
167                   assert false
168          in
169          let rev_fields, _ = List.fold_left map ([], cty) fields in 
170          let fields = List.rev rev_fields in
171          let obj = N.Record (lpars, name, ty, fields) in
172          G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
173       | _ -> assert false
174
175 let mk_statement flavour name t v =
176    let name = match name with Some name -> name | None -> assert false in
177    let obj = N.Theorem (flavour, name, t, v, `Regular) in
178    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
179
180 let mk_qed =
181    G.Executable (floc, G.Command (floc, G.Qed floc))
182
183 let mk_tactic tactic punctation =
184    G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
185
186 let mk_punctation punctation =
187    G.Executable (floc, G.Tactic (floc, None, punctation))
188
189 let mk_id punctation =
190    let tactic = G.IdTac floc in
191    mk_tactic tactic punctation
192
193 let mk_exact t punctation =
194    let tactic = G.Exact (floc, t) in
195    mk_tactic tactic punctation
196
197 let mk_intros xi xids punctation =
198    let tactic = G.Intros (floc, (xi, xids)) in
199    mk_tactic tactic punctation
200
201 let mk_cut name what punctation =
202    let name = match name with Some name -> name | None -> assert false in
203    let tactic = G.Cut (floc, Some name, what) in
204    mk_tactic tactic punctation
205
206 let mk_letin name what punctation =
207    let name = match name with Some name -> name | None -> assert false in
208    let tactic = G.LetIn (floc, what, name) in
209    mk_tactic tactic punctation
210
211 let mk_lapply name what punctation =
212    let tactic = G.LApply (floc, false, None, [], what, name) in
213    mk_tactic tactic punctation
214
215 let mk_rewrite direction what where pattern punctation =
216    let direction = if direction then `RightToLeft else `LeftToRight in 
217    let pattern, rename = match where with
218       | None                      -> (None, [], Some pattern), []
219       | Some (premise, Some name) -> (None, [premise, pattern], None), [Some name]
220       | Some (premise, None)      -> (None, [premise, pattern], None), [] 
221    in
222    let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
223    mk_tactic tactic punctation
224
225 let mk_elim what using pattern punctation =
226    let pattern = None, [], Some pattern in
227    let tactic = G.Elim (floc, what, using, pattern, (Some 0, [])) in
228    mk_tactic tactic punctation
229
230 let mk_cases what pattern punctation =
231    let pattern = None, [], Some pattern in
232    let tactic = G.Cases (floc, what, pattern, (Some 0, [])) in
233    mk_tactic tactic punctation
234
235 let mk_apply t punctation =
236    let tactic = G.Apply (floc, t) in
237    mk_tactic tactic punctation
238
239 let mk_change t where pattern punctation =
240    let pattern = match where with
241       | None              -> None, [], Some pattern
242       | Some (premise, _) -> None, [premise, pattern], None
243    in
244    let tactic = G.Change (floc, pattern, t) in
245    mk_tactic tactic punctation
246
247 let mk_clear ids punctation =
248    let tactic = G.Clear (floc, ids) in
249    mk_tactic tactic punctation
250
251 let mk_clearbody id punctation =
252    let tactic = G.ClearBody (floc, id) in
253    mk_tactic tactic punctation
254
255 let mk_reflexivity punctation =
256    let tactic = G.Reflexivity floc in
257    mk_tactic tactic punctation
258
259 let mk_ob = 
260    let punctation = G.Branch floc in
261    mk_punctation punctation
262
263 let mk_dot = G.Dot floc
264
265 let mk_sc = G.Semicolon floc
266
267 let mk_cb = G.Merge floc
268
269 let mk_vb = G.Shift floc
270
271 (* rendering ****************************************************************)
272
273 let rec render_step sep a = function
274    | Note s                    -> mk_notenote s a
275    | Statement (f, n, t, v, s) -> mk_statement f n t v :: mk_thnote s a 
276    | Inductive (ts, lps, s)    -> mk_inductive ts lps :: mk_thnote s a
277    | Record (ts, lps, fs, s)   -> mk_record ts lps fs :: mk_thnote s a
278    | Qed s                     -> mk_qed :: mk_tacnote s a
279    | Exact (t, s)              -> mk_exact t sep :: mk_tacnote s a   
280    | Id s                      -> mk_id sep :: mk_tacnote s a
281    | Intros (c, ns, s)         -> mk_intros c ns sep :: mk_tacnote s a
282    | Cut (n, t, s)             -> mk_cut n t sep :: mk_tacnote s a
283    | LetIn (n, t, s)           -> mk_letin n t sep :: mk_tacnote s a
284    | LApply (n, t, s)          -> mk_lapply n t sep :: mk_tacnote s a
285    | Rewrite (b, t, w, e, s)   -> mk_rewrite b t w e sep :: mk_tacnote s a
286    | Elim (t, xu, e, s)        -> mk_elim t xu e sep :: mk_tacnote s a
287    | Cases (t, e, s)           -> mk_cases t e sep :: mk_tacnote s a
288    | Apply (t, s)              -> mk_apply t sep :: mk_tacnote s a
289    | Change (t, _, w, e, s)    -> mk_change t w e sep :: mk_tacnote s a
290    | Clear (ns, s)             -> mk_clear ns sep :: mk_tacnote s a
291    | ClearBody (n, s)          -> mk_clearbody n sep :: mk_tacnote s a
292    | Branch ([], s)            -> a
293    | Branch ([ps], s)          -> render_steps sep a ps
294    | Branch (ps :: pss, s)     ->
295       let a = mk_ob :: mk_tacnote s a in
296       let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
297       mk_punctation sep :: render_steps mk_cb a ps
298    | Reflexivity s             -> mk_reflexivity sep :: mk_tacnote s a
299
300 and render_steps sep a = function
301    | []                                          -> a
302    | [p]                                         -> render_step sep a p
303    | p :: Branch ([], _) :: ps                   ->
304       render_steps sep a (p :: ps)
305    | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
306       render_steps sep (render_step mk_sc a p) ps
307    | p :: ps                                     ->
308       render_steps sep (render_step mk_sc a p) ps
309
310 let render_steps a = render_steps mk_dot a
311
312 (* counting *****************************************************************)
313
314 let rec count_step a = function
315    | Note _
316    | Statement _
317    | Inductive _
318    | Qed _                  -> a
319 (* level A0 *)   
320    | Branch (pps, _)        -> List.fold_left count_steps a pps
321    | Clear _
322    | ClearBody _
323    | Id _
324    | Intros (Some 0, [], _)
325 (* leval A1 *)   
326    | Exact _
327 (* level B1 *)   
328    | Cut _
329    | LetIn _
330 (* level B2 *)   
331    | Change _               -> a
332 (* level C *)   
333    | _                      -> succ a   
334
335 and count_steps a = List.fold_left count_step a
336
337 let count = I.count_nodes ~meta:false
338
339 let rec count_node a = function
340    | Note _
341    | Record _
342    | Inductive _
343    | Statement _
344    | Qed _   
345    | Reflexivity _
346    | Id _
347    | Intros _
348    | Clear _
349    | ClearBody _             -> a
350    | Exact (t, _) 
351    | Cut (_, t, _)
352    | LetIn (_, t, _)
353    | LApply (_, t, _)
354    | Apply (t, _)            -> count a (H.cic t)
355    | Rewrite (_, t, _, p, _)
356    | Elim (t, _, p, _)
357    | Cases (t, p, _)
358    | Change (t, _, _, p, _)  -> let a = count a (H.cic t) in count a (H.cic p) 
359    | Branch (ss, _)          -> List.fold_left count_nodes a ss
360
361 and count_nodes a = List.fold_left count_node a
362
363 (* helpers ******************************************************************)
364
365 let rec note_of_step = function
366    | Note s
367    | Statement (_, _, _, _, s)
368    | Inductive (_, _, s)
369    | Record (_, _, _, s)
370    | Qed s
371    | Exact (_, s)
372    | Id s
373    | Intros (_, _, s)
374    | Cut (_, _, s)
375    | LetIn (_, _, s)
376    | LApply (_, _, s)
377    | Rewrite (_, _, _, _, s)
378    | Elim (_, _, _, s)
379    | Cases (_, _, s)
380    | Apply (_, s)
381    | Change (_, _, _, _, s)
382    | Clear (_, s)
383    | ClearBody (_, s)
384    | Reflexivity s
385    | Branch (_, s)             -> s