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