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