]> matita.cs.unibo.it Git - helm.git/blob - components/content_pres/proceduralTypes.ml
send internally generated headers in lowercase form for consistency
[helm.git] / components / content_pres / 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 (****************************************************************************)
59
60 type name  = string
61 type what  = Cic.annterm
62 type how   = bool
63 type using = Cic.annterm
64 type count = int
65 type note  = string
66 type where = (name * name) option
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 * note
76           | Elim of what * using option * note
77           | Apply of what * note
78           | Whd of count * note
79           | Branch of step list list * note
80
81 (* annterm constructors *****************************************************)
82
83 let mk_arel i b = Cic.ARel ("", "", i, b)
84
85 (* grafite ast constructors *************************************************)
86
87 let floc = H.dummy_floc
88
89 let hole = C.AImplicit ("", Some `Hole)
90
91 let mk_note str = G.Comment (floc, G.Note (floc, str))
92
93 let mk_theorem name t = 
94    let obj = N.Theorem (`Theorem, name, t, None) in
95    G.Executable (floc, G.Command (floc, G.Obj (floc, obj)))
96
97 let mk_qed =
98    G.Executable (floc, G.Command (floc, G.Qed floc))
99
100 let mk_tactic tactic =
101    G.Executable (floc, G.Tactical (floc, G.Tactic (floc, tactic), None))
102
103 let mk_id =
104    let tactic = G.IdTac floc in
105    mk_tactic tactic
106
107 let mk_intros xi ids =
108    let tactic = G.Intros (floc, xi, ids) in
109    mk_tactic tactic
110
111 let mk_cut name what =
112    let tactic = G.Cut (floc, Some name, what) in
113    mk_tactic tactic
114
115 let mk_letin name what =
116    let tactic = G.LetIn (floc, what, name) in
117    mk_tactic tactic
118
119 let mk_rewrite direction what where =
120    let direction = if direction then `RightToLeft else `LeftToRight in 
121    let pattern, rename = match where with
122       | None                 -> (None, [], Some hole), []
123       | Some (premise, name) -> (None, [premise, hole], None), [name] 
124    in
125    let tactic = G.Rewrite (floc, direction, what, pattern, rename) in
126    mk_tactic tactic
127
128 let mk_elim what using =
129    let tactic = G.Elim (floc, what, using, Some 0, []) in
130    mk_tactic tactic
131
132 let mk_apply t =
133    let tactic = G.Apply (floc, t) in
134    mk_tactic tactic
135
136 let mk_whd i =
137    let pattern = None, [], Some hole in
138    let tactic = G.Reduce (floc, `Whd, pattern) in
139    mk_tactic tactic
140
141 let mk_dot = G.Executable (floc, G.Tactical (floc, G.Dot floc, None))
142
143 let mk_sc = G.Executable (floc, G.Tactical (floc, G.Semicolon floc, None))
144
145 let mk_ob = G.Executable (floc, G.Tactical (floc, G.Branch floc, None))
146
147 let mk_cb = G.Executable (floc, G.Tactical (floc, G.Merge floc, None))
148
149 let mk_vb = G.Executable (floc, G.Tactical (floc, G.Shift floc, None))
150
151 (* rendering ****************************************************************)
152
153 let rec render_step sep a = function
154    | Note s               -> mk_note s :: a
155    | Theorem (n, t, s)    -> mk_note s :: mk_theorem n t :: a 
156    | Qed s                -> mk_note s :: mk_qed :: a
157    | Id s                 -> mk_note s :: cont sep (mk_id :: a)
158    | Intros (c, ns, s)    -> mk_note s :: cont sep (mk_intros c ns :: a)
159    | Cut (n, t, s)        -> mk_note s :: cont sep (mk_cut n t :: a)
160    | LetIn (n, t, s)      -> mk_note s :: cont sep (mk_letin n t :: a)
161    | Rewrite (b, t, w, s) -> mk_note s :: cont sep (mk_rewrite b t w :: a)
162    | Elim (t, xu, s)      -> mk_note s :: cont sep (mk_elim t xu :: a)
163    | Apply (t, s)         -> mk_note s :: cont sep (mk_apply t :: a)
164    | Whd (c, s)           -> mk_note s :: cont sep (mk_whd c :: a)
165    | Branch ([], s)       -> a
166    | Branch ([ps], s)     -> render_steps sep a ps
167    | Branch (pss, s)      ->
168       let a =  mk_ob :: a in
169       let body = mk_cb :: list_rev_map_concat (render_steps None) mk_vb a pss in
170       mk_note s :: cont sep body
171
172 and render_steps sep a = function
173    | []                                          -> a
174    | [p]                                         -> render_step sep a p
175    | p :: Branch ([], _) :: ps                   ->
176       render_steps sep a (p :: ps)
177    | p :: ((Branch (_ :: _ :: _, _) :: _) as ps) ->
178       render_steps sep (render_step (Some mk_sc) a p) ps
179    | p :: ps                                     ->
180       render_steps sep (render_step (Some mk_dot) a p) ps
181
182 let render_steps a = render_steps None a
183
184 (* counting *****************************************************************)
185
186 let rec count_step a = function
187    | Note _
188    | Theorem _  
189    | Qed _           -> a
190    | Branch (pps, _) -> List.fold_left count_steps a pps
191    | _               -> succ a   
192
193 and count_steps a = List.fold_left count_step a