]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/proceduralTypes.ml
cb4c11d2ee6f87b10d671297b936f903a4da36a4
[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
72 type step = Note of note 
73           | Inductive of types * lpsno * note
74           | Statement of flavour * name * what * body * note
75           | Qed of note
76           | Id of note
77           | Intros of count option * name list * note
78           | Cut of name * what * note
79           | LetIn of name * what * note
80           | Rewrite of how * what * where * pattern * note
81           | Elim of what * using option * pattern * note
82           | Cases of what * pattern * note
83           | Apply of what * note
84           | Change of inferred * what * where * pattern * note 
85           | Clear of hyp list * note
86           | ClearBody of hyp * note
87           | Branch of step list list * note
88
89 (* annterm constructors *****************************************************)
90
91 let mk_arel i b = C.ARel ("", "", i, b)
92
93 (* FG: this is really awful !! *)
94 let arel_of_name = function
95    | C.Name s    -> mk_arel 0 s
96    | C.Anonymous -> mk_arel 0 "_"
97
98 (* helper functions on left params for use with inductive types *************)
99
100 let strip_lps lpsno arity =
101    let rec aux no lps = function
102       | C.AProd (_, name, w, t) when no > 0 ->
103          let lp = name, Some w in
104          aux (pred no) (lp :: lps) t
105       | t                                   -> lps, t
106    in
107    aux lpsno [] arity
108
109 let merge_lps lps1 lps2 =
110    let map (n1, w1) (n2, _) =
111       let n = match n1, n2 with
112          | C.Name _, _ -> n1
113          | _           -> n2
114       in
115       n, w1
116    in
117    if lps1 = [] then lps2 else
118    List.map2 map lps1 lps2
119
120 (* grafite ast constructors *************************************************)
121
122 let floc = HEL.dummy_floc
123
124 let mk_note str = G.Comment (floc, G.Note (floc, str))
125
126 let mk_tacnote str a =
127    if str = "" then mk_note "" :: a else mk_note "" :: mk_note str :: a
128
129 let mk_notenote str a =
130    if str = "" then a else mk_note str :: a
131
132 let mk_thnote str a =
133    if str = "" then a else mk_note "" :: mk_note str :: a
134
135 let mk_inductive types lpsno =
136    let map1 (lps1, cons) (name, arity) = 
137       let lps2, arity = strip_lps lpsno arity in
138       merge_lps lps1 lps2, (name, arity) :: cons
139    in
140    let map2 (lps1, types) (_, name, kind, arity, cons) =
141       let lps2, arity = strip_lps lpsno arity in 
142       let lps1, rev_cons = List.fold_left map1 (lps1, []) cons in 
143       merge_lps lps1 lps2, (name, kind, arity, List.rev rev_cons) :: types
144    in
145    let map3 (name, xw) = arel_of_name name, xw in
146    let rev_lps, rev_types = List.fold_left map2 ([], []) types in
147    let lpars, types = List.rev_map map3 rev_lps, List.rev rev_types in
148    let obj = N.Inductive (lpars, types) in
149    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
150
151 let mk_statement flavour name t v =
152    let name = match name with Some name -> name | None -> assert false in
153    let obj = N.Theorem (flavour, name, t, v) in
154    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
155
156 let mk_qed =
157    G.Executable (floc, G.Command (floc, G.Qed floc))
158
159 let mk_tactic tactic punctation =
160    G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
161
162 let mk_punctation punctation =
163    G.Executable (floc, G.Tactic (floc, None, punctation))
164
165 let mk_id punctation =
166    let tactic = G.IdTac floc in
167    mk_tactic tactic punctation
168
169 let mk_intros xi xids punctation =
170    let tactic = G.Intros (floc, (xi, xids)) in
171    mk_tactic tactic punctation
172
173 let mk_cut name what punctation =
174    let name = match name with Some name -> name | None -> assert false in
175    let tactic = G.Cut (floc, Some name, what) in
176    mk_tactic tactic punctation
177
178 let mk_letin name what punctation =
179    let name = match name with Some name -> name | None -> assert false in
180    let tactic = G.LetIn (floc, what, name) in
181    mk_tactic tactic punctation
182
183 let mk_rewrite direction what where pattern punctation =
184    let direction = if direction then `RightToLeft else `LeftToRight in 
185    let pattern, rename = match where with
186       | None                      -> (None, [], Some pattern), []
187       | Some (premise, Some name) -> (None, [premise, pattern], None), [Some name]
188       | Some (premise, None)      -> (None, [premise, pattern], None), [] 
189    in
190    let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
191    mk_tactic tactic punctation
192
193 let mk_elim what using pattern punctation =
194    let pattern = None, [], Some pattern in
195    let tactic = G.Elim (floc, what, using, pattern, (Some 0, [])) in
196    mk_tactic tactic punctation
197
198 let mk_cases what pattern punctation =
199    let pattern = None, [], Some pattern in
200    let tactic = G.Cases (floc, what, pattern, (Some 0, [])) in
201    mk_tactic tactic punctation
202
203 let mk_apply t punctation =
204    let tactic = G.ApplyP (floc, t) in
205    mk_tactic tactic punctation
206
207 let mk_change t where pattern punctation =
208    let pattern = match where with
209       | None              -> None, [], Some pattern
210       | Some (premise, _) -> None, [premise, pattern], None
211    in
212    let tactic = G.Change (floc, pattern, t) in
213    mk_tactic tactic punctation
214
215 let mk_clear ids punctation =
216    let tactic = G.Clear (floc, ids) in
217    mk_tactic tactic punctation
218
219 let mk_clearbody id punctation =
220    let tactic = G.ClearBody (floc, id) in
221    mk_tactic tactic punctation
222
223 let mk_ob = 
224    let punctation = G.Branch floc in
225    mk_punctation punctation
226
227 let mk_dot = G.Dot floc
228
229 let mk_sc = G.Semicolon floc
230
231 let mk_cb = G.Merge floc
232
233 let mk_vb = G.Shift floc
234
235 (* rendering ****************************************************************)
236
237 let rec render_step sep a = function
238    | Note s                    -> mk_notenote s a
239    | Statement (f, n, t, v, s) -> mk_statement f n t v :: mk_thnote s a 
240    | Inductive (lps, ts, s)    -> mk_inductive lps ts :: mk_thnote s a
241    | Qed s                     -> mk_qed :: mk_tacnote s a
242    | Id s                      -> mk_id sep :: mk_tacnote s a
243    | Intros (c, ns, s)         -> mk_intros c ns sep :: mk_tacnote s a
244    | Cut (n, t, s)             -> mk_cut n t sep :: mk_tacnote s a
245    | LetIn (n, t, s)           -> mk_letin n t sep :: mk_tacnote s a
246    | Rewrite (b, t, w, e, s)   -> mk_rewrite b t w e sep :: mk_tacnote s a
247    | Elim (t, xu, e, s)        -> mk_elim t xu e sep :: mk_tacnote s a
248    | Cases (t, e, s)           -> mk_cases t e sep :: mk_tacnote s a
249    | Apply (t, s)              -> mk_apply t sep :: mk_tacnote s a
250    | Change (t, _, w, e, s)    -> mk_change t w e sep :: mk_tacnote s a
251    | Clear (ns, s)             -> mk_clear ns sep :: mk_tacnote s a
252    | ClearBody (n, s)          -> mk_clearbody n sep :: mk_tacnote s a
253    | Branch ([], s)            -> a
254    | Branch ([ps], s)          -> render_steps sep a ps
255    | Branch (ps :: pss, s)     ->      
256       let a = mk_ob :: mk_tacnote s a in
257       let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
258       mk_punctation sep :: render_steps mk_cb a ps
259
260 and render_steps sep a = function
261    | []                                          -> a
262    | [p]                                         -> render_step sep a p
263    | p :: Branch ([], _) :: ps                   ->
264       render_steps sep a (p :: ps)
265    | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
266       render_steps sep (render_step mk_sc a p) ps
267    | p :: ps                                     ->
268       render_steps sep (render_step mk_sc a p) ps
269
270 let render_steps a = render_steps mk_dot a
271
272 (* counting *****************************************************************)
273
274 let rec count_step a = function
275    | Note _
276    | Statement _  
277    | Id _
278    | Qed _           -> a
279    | Branch (pps, _) -> List.fold_left count_steps a pps
280    | _               -> succ a   
281
282 and count_steps a = List.fold_left count_step a
283
284 let count = I.count_nodes ~meta:false
285
286 let rec count_node a = function
287    | Note _
288    | Inductive _
289    | Statement _
290    | Qed _
291    | Id _
292    | Intros _
293    | Clear _
294    | ClearBody _             -> a
295    | Cut (_, t, _)
296    | LetIn (_, t, _)
297    | Apply (t, _)            -> count a (H.cic t)
298    | Rewrite (_, t, _, p, _)
299    | Elim (t, _, p, _)
300    | Cases (t, p, _)
301    | Change (t, _, _, p, _)  -> let a = count a (H.cic t) in count a (H.cic p) 
302    | Branch (ss, _)          -> List.fold_left count_nodes a ss
303
304 and count_nodes a = List.fold_left count_node a