]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralTypes.ml
a1d120f807a161ac7aeb472e17d5518a63f28dcf
[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 H = HExtlib
27 module C = Cic
28 module G = GrafiteAst
29 module N = CicNotationPt
30
31 (* functions to be moved ****************************************************)
32
33 let list_rev_map2 map l1 l2 =
34    let rec aux res = function
35       | hd1 :: tl1, hd2 :: tl2 -> aux (map hd1 hd2 :: res) (tl1, tl2)
36       | _                      -> res
37    in
38    aux [] (l1, l2)
39
40 let list_map2_filter map l1 l2 =
41    let rec filter l = function
42       | []           -> l
43       | None :: tl   -> filter l tl
44       | Some a :: tl -> filter (a :: l) tl 
45   in 
46   filter [] (list_rev_map2 map l1 l2)
47
48 let list_init f i =
49    let rec aux a j = if j < 0 then a else aux (f j :: a) (pred j) in
50    aux [] i
51
52 (****************************************************************************)
53
54 type name     = string
55 type what     = Cic.annterm
56 type how      = bool
57 type using    = Cic.annterm
58 type count    = int
59 type note     = string
60 type where    = (name * name) option
61 type inferred = Cic.annterm
62 type pattern  = Cic.annterm
63
64 type step = Note of note 
65           | Theorem of name * what * note
66           | Qed of note
67           | Id of note
68           | Intros of count option * name list * note
69           | Cut of name * what * note
70           | LetIn of name * what * note
71           | Rewrite of how * what * where * pattern * note
72           | Elim of what * using option * pattern * note
73           | Apply of what * note
74           | Change of inferred * what * where * pattern * note 
75           | ClearBody of name * note
76           | Branch of step list list * note
77
78 (* annterm constructors *****************************************************)
79
80 let mk_arel i b = Cic.ARel ("", "", i, b)
81
82 (* grafite ast constructors *************************************************)
83
84 let floc = H.dummy_floc
85
86 let mk_note str = G.Comment (floc, G.Note (floc, str))
87
88 let mk_tacnote str a =
89    if str = "" then mk_note "" :: a else mk_note "" :: mk_note str :: a
90
91 let mk_notenote str a =
92    if str = "" then a else mk_note str :: a
93
94 let mk_thnote str a =
95    if str = "" then a else mk_note "" :: mk_note str :: a
96
97 let mk_theorem name t = 
98    let obj = N.Theorem (`Theorem, name, t, None) in
99    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
100
101 let mk_qed =
102    G.Executable (floc, G.Command (floc, G.Qed floc))
103
104 let mk_tactic tactic punctation =
105    G.Executable (floc, G.Tactic (floc, Some tactic, punctation))
106
107 let mk_punctation punctation =
108    G.Executable (floc, G.Tactic (floc, None, punctation))
109
110 let mk_id punctation =
111    let tactic = G.IdTac floc in
112    mk_tactic tactic punctation
113
114 let mk_intros xi ids punctation =
115    let tactic = G.Intros (floc, xi, ids) in
116    mk_tactic tactic punctation
117
118 let mk_cut name what punctation =
119    let tactic = G.Cut (floc, Some name, what) in
120    mk_tactic tactic punctation
121
122 let mk_letin name what punctation =
123    let tactic = G.LetIn (floc, what, name) in
124    mk_tactic tactic punctation
125
126 let mk_rewrite direction what where pattern punctation =
127    let direction = if direction then `RightToLeft else `LeftToRight in 
128    let pattern, rename = match where with
129       | None                 -> (None, [], Some pattern), []
130       | Some (premise, name) -> (None, [premise, pattern], None), [name] 
131    in
132    let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
133    mk_tactic tactic punctation
134
135 let mk_elim what using pattern punctation =
136    let pattern = None, [], Some pattern in
137    let tactic = G.Elim (floc, what, using, pattern, Some 0, []) in
138    mk_tactic tactic punctation
139
140 let mk_apply t punctation =
141    let tactic = G.Apply (floc, t) in
142    mk_tactic tactic punctation
143
144 let mk_change t where pattern punctation =
145    let pattern = match where with
146       | None              -> None, [], Some pattern
147       | Some (premise, _) -> None, [premise, pattern], None
148    in
149    let tactic = G.Change (floc, pattern, t) in
150    mk_tactic tactic punctation
151
152 let mk_clearbody id punctation =
153    let tactic = G.ClearBody (floc, id) in
154    mk_tactic tactic punctation
155
156 let mk_ob = 
157    let punctation = G.Branch floc in
158    mk_punctation punctation
159
160 let mk_dot = G.Dot floc
161
162 let mk_sc = G.Semicolon floc
163
164 let mk_cb = G.Merge floc
165
166 let mk_vb = G.Shift floc
167
168 (* rendering ****************************************************************)
169
170 let rec render_step sep a = function
171    | Note s                  -> mk_notenote s a
172    | Theorem (n, t, s)       -> mk_theorem n t :: mk_thnote s a 
173    | Qed s                   -> mk_qed :: mk_tacnote s a
174    | Id s                    -> mk_id sep :: mk_tacnote s a
175    | Intros (c, ns, s)       -> mk_intros c ns sep :: mk_tacnote s a
176    | Cut (n, t, s)           -> mk_cut n t sep :: mk_tacnote s a
177    | LetIn (n, t, s)         -> mk_letin n t sep :: mk_tacnote s a
178    | Rewrite (b, t, w, e, s) -> mk_rewrite b t w e sep :: mk_tacnote s a
179    | Elim (t, xu, e, s)      -> mk_elim t xu e sep :: mk_tacnote s a
180    | Apply (t, s)            -> mk_apply t sep :: mk_tacnote s a
181    | Change (t, _, w, e, s)  -> mk_change t w e sep :: mk_tacnote s a
182    | ClearBody (n, s)        -> mk_clearbody n sep :: mk_tacnote s a
183    | Branch ([], s)          -> a
184    | Branch ([ps], s)        -> render_steps sep a ps
185    | Branch (ps :: pss, s)   ->      
186       let a = mk_ob :: mk_tacnote s a in
187       let a = List.fold_left (render_steps mk_vb) a (List.rev pss) in
188       mk_punctation sep :: render_steps mk_cb a ps
189
190 and render_steps sep a = function
191    | []                                          -> a
192    | [p]                                         -> render_step sep a p
193    | p :: Branch ([], _) :: ps                   ->
194       render_steps sep a (p :: ps)
195    | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
196       render_steps sep (render_step mk_sc a p) ps
197    | p :: ps                                     ->
198       render_steps sep (render_step mk_dot a p) ps
199
200 let render_steps a = render_steps mk_dot a
201
202 (* counting *****************************************************************)
203
204 let rec count_step a = function
205    | Note _
206    | Theorem _  
207    | Qed _           -> a
208    | Branch (pps, _) -> List.fold_left count_steps a pps
209    | _               -> succ a   
210
211 and count_steps a = List.fold_left count_step a