]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralTypes.ml
tagged 0.5.0-rc1
[helm.git] / 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 name     = string option
58 type hyp      = string
59 type what     = Cic.annterm
60 type how      = bool
61 type using    = Cic.annterm
62 type count    = int
63 type note     = string
64 type where    = (hyp * name) option
65 type inferred = Cic.annterm
66 type pattern  = Cic.annterm
67
68 type step = Note of note 
69           | Theorem of name * what * note
70           | Qed of note
71           | Id of note
72           | Intros of count option * name list * note
73           | Cut of name * what * note
74           | LetIn of name * what * note
75           | Rewrite of how * what * where * pattern * note
76           | Elim of what * using option * pattern * note
77           | Apply of what * note
78           | Change of inferred * what * where * pattern * note 
79           | Clear of hyp list * note
80           | ClearBody of hyp * note
81           | Branch of step list list * note
82
83 (* annterm constructors *****************************************************)
84
85 let mk_arel i b = Cic.ARel ("", "", i, b)
86
87 (* grafite ast constructors *************************************************)
88
89 let floc = HEL.dummy_floc
90
91 let mk_note str = G.Comment (floc, G.Note (floc, str))
92
93 let mk_tacnote str a =
94    if str = "" then mk_note "" :: a else mk_note "" :: mk_note str :: a
95
96 let mk_notenote str a =
97    if str = "" then a else mk_note str :: a
98
99 let mk_thnote str a =
100    if str = "" then a else mk_note "" :: mk_note str :: a
101
102 let mk_theorem name t =
103    let name = match name with Some name -> name | None -> assert false in
104    let obj = N.Theorem (`Theorem, name, t, None) in
105    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
106
107 let mk_qed =
108    G.Executable (floc, G.Command (floc, G.Qed floc))
109
110 let mk_tactic tactic punctation =
111    G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
112
113 let mk_punctation punctation =
114    G.Executable (floc, G.Tactic (floc, None, punctation))
115
116 let mk_id punctation =
117    let tactic = G.IdTac floc in
118    mk_tactic tactic punctation
119
120 let mk_intros xi xids punctation =
121    let tactic = G.Intros (floc, (xi, xids)) in
122    mk_tactic tactic punctation
123
124 let mk_cut name what punctation =
125    let name = match name with Some name -> name | None -> assert false in
126    let tactic = G.Cut (floc, Some name, what) in
127    mk_tactic tactic punctation
128
129 let mk_letin name what punctation =
130    let name = match name with Some name -> name | None -> assert false in
131    let tactic = G.LetIn (floc, what, name) in
132    mk_tactic tactic punctation
133
134 let mk_rewrite direction what where pattern punctation =
135    let direction = if direction then `RightToLeft else `LeftToRight in 
136    let pattern, rename = match where with
137       | None                 -> (None, [], Some pattern), []
138       | Some (premise, name) -> (None, [premise, pattern], None), [name] 
139    in
140    let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
141    mk_tactic tactic punctation
142
143 let mk_elim what using pattern punctation =
144    let pattern = None, [], Some pattern in
145    let tactic = G.Elim (floc, what, using, pattern, (Some 0, [])) in
146    mk_tactic tactic punctation
147
148 let mk_apply t punctation =
149    let tactic = G.Apply (floc, t) in
150    mk_tactic tactic punctation
151
152 let mk_change t where pattern punctation =
153    let pattern = match where with
154       | None              -> None, [], Some pattern
155       | Some (premise, _) -> None, [premise, pattern], None
156    in
157    let tactic = G.Change (floc, pattern, t) in
158    mk_tactic tactic punctation
159
160 let mk_clear ids punctation =
161    let tactic = G.Clear (floc, ids) in
162    mk_tactic tactic punctation
163
164 let mk_clearbody id punctation =
165    let tactic = G.ClearBody (floc, id) in
166    mk_tactic tactic punctation
167
168 let mk_ob = 
169    let punctation = G.Branch floc in
170    mk_punctation punctation
171
172 let mk_dot = G.Dot floc
173
174 let mk_sc = G.Semicolon floc
175
176 let mk_cb = G.Merge floc
177
178 let mk_vb = G.Shift floc
179
180 (* rendering ****************************************************************)
181
182 let rec render_step sep a = function
183    | Note s                  -> mk_notenote s a
184    | Theorem (n, t, s)       -> mk_theorem n t :: mk_thnote s a 
185    | Qed s                   -> mk_qed :: mk_tacnote s a
186    | Id s                    -> mk_id sep :: mk_tacnote s a
187    | Intros (c, ns, s)       -> mk_intros c ns sep :: mk_tacnote s a
188    | Cut (n, t, s)           -> mk_cut n t sep :: mk_tacnote s a
189    | LetIn (n, t, s)         -> mk_letin n t sep :: mk_tacnote s a
190    | Rewrite (b, t, w, e, s) -> mk_rewrite b t w e sep :: mk_tacnote s a
191    | Elim (t, xu, e, s)      -> mk_elim t xu e sep :: mk_tacnote s a
192    | Apply (t, s)            -> mk_apply t sep :: mk_tacnote s a
193    | Change (t, _, w, e, s)  -> mk_change t w e sep :: mk_tacnote s a
194    | Clear (ns, s)           -> mk_clear ns sep :: mk_tacnote s a
195    | ClearBody (n, s)        -> mk_clearbody n sep :: mk_tacnote s a
196    | Branch ([], s)          -> a
197    | Branch ([ps], s)        -> render_steps sep a ps
198    | Branch (ps :: pss, s)   ->      
199       let a = mk_ob :: mk_tacnote s a in
200       let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
201       mk_punctation sep :: render_steps mk_cb a ps
202
203 and render_steps sep a = function
204    | []                                          -> a
205    | [p]                                         -> render_step sep a p
206    | p :: Branch ([], _) :: ps                   ->
207       render_steps sep a (p :: ps)
208    | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
209       render_steps sep (render_step mk_sc a p) ps
210    | p :: ps                                     ->
211       render_steps sep (render_step mk_sc a p) ps
212
213 let render_steps a = render_steps mk_dot a
214
215 (* counting *****************************************************************)
216
217 let rec count_step a = function
218    | Note _
219    | Theorem _  
220    | Qed _           -> a
221    | Branch (pps, _) -> List.fold_left count_steps a pps
222    | _               -> succ a   
223
224 and count_steps a = List.fold_left count_step a
225
226 let rec count_node a = function
227    | Note _
228    | Theorem _   
229    | Qed _
230    | Id _
231    | Intros _
232    | Clear _
233    | ClearBody _             -> a
234    | Cut (_, t, _)
235    | LetIn (_, t, _)
236    | Apply (t, _)            -> I.count_nodes a (H.cic t)
237    | Rewrite (_, t, _, p, _)
238    | Elim (t, _, p, _)
239    | Change (t, _, _, p, _)  -> 
240       let a = I.count_nodes a (H.cic t) in
241       I.count_nodes a (H.cic p) 
242    | Branch (ss, _)          -> List.fold_left count_nodes a ss
243
244 and count_nodes a = List.fold_left count_node a