]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgLP.ml
- simpler attribute system
[helm.git] / helm / software / helena / src / basic_rg / brgLP.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module KF = Filename
13 module KP = Printf
14
15 module U  = NUri
16 module C  = Cps
17 module G  = Options
18 module N  = Layer
19 module E  = Entity
20 module R  = Alpha
21 module B  = Brg
22
23 IFDEF MANAGER THEN
24
25 (* Internal functions *******************************************************)
26
27 let ok = ref true
28
29 let top_age = 7000
30
31 let uris = ref []
32
33 let base = "lp"
34
35 let ext_lp = ".elpi"
36 let ext_tj = ".mod"
37
38 let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
39
40 let alpha n =
41    if List.mem n reserved then !G.alpha ^ n else n
42
43 let out_preamble och =
44    let ich = open_in !G.preamble in
45    let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
46    try aux () with End_of_file -> close_in ich
47
48 let out_top_comment och msg =
49    KP.fprintf och "%% %s\n\n" msg
50
51 let out_comment och msg =
52    KP.fprintf och "%% %s\n" msg 
53
54 let out_clause och msg =
55    KP.fprintf och "%s\n\n" msg 
56
57 let out_uri och u =
58    let str = U.string_of_uri u in
59    let rec aux i =
60      let c = str.[i] in
61      if c = '.' then () else begin 
62         output_char och (if c = '/' then '_' else c);
63         aux (succ i)
64      end
65    in
66    let rec strip i n = 
67       if n <= 0 then succ i else
68       strip (String.index_from str (succ i) '/') (pred n)
69    in
70    aux (strip 0 3)
71
72 let out_name och a =
73    let f n = function 
74       | true  -> KP.fprintf och "%s" (alpha n)
75       | false -> KP.fprintf och "_"
76    in
77    let err () = f "" false in
78    E.name err f a
79
80 let rec out_term st e och = function
81    | B.Sort k                        ->
82       let sort = if k = 0 then "k+set" else if k = 1 then "k+prop" else assert false in
83       KP.fprintf och "(sort %s)" sort
84    | B.LRef (_, i)                   ->
85       let _, _, _, y, b = B.get e i in
86       KP.fprintf och "%a" out_name y
87    | B.GRef (_, s)                   ->
88       KP.fprintf och "%a" out_uri s
89    | B.Cast (u, t)                   ->
90       KP.fprintf och "(cast %a %a)" (out_term st e) u (out_term st e) t 
91    | B.Appl (x, v, t)                ->
92       let c = if x then "appx" else "appr" in
93       KP.fprintf och "(%s %a %a)" c (out_term st e) v (out_term st e) t
94    | B.Bind (y, B.Abst (r, n, w), t) ->
95       let y = R.alpha B.mem e y in
96       let ee = B.push e B.empty E.empty_node y (B.abst r n w) in
97       let c = if r then "prod" else "abst" in
98       let l = match N.to_string st n with
99          | "1" -> "l+1"
100          | "2" -> "l+2"
101          | _   -> ok := false; "?"
102       in
103       KP.fprintf och "(%s %s %a %a\\ %a)"
104          c l (out_term st e) w out_name y (out_term st ee) t
105    | B.Bind (y, B.Abbr v, t)         ->
106       let y = R.alpha B.mem e y in
107       let ee = B.push e B.empty E.empty_node y (B.abbr v) in
108       KP.fprintf och "(abbr %a %a\\ %a)"
109           (out_term st e) v out_name y (out_term st ee) t
110    | B.Bind (_, B.Void, _)           -> C.err ()
111
112 (* elpi variant 1 ***********************************************************)
113
114 let output_entity_lp1 och st (_, na, u, b) =
115    if na.E.n_apix <= !G.last then begin
116    match b with
117       | E.Abbr v ->
118          KP.fprintf och "(gdef+1 c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) v out_uri u;
119          uris := (true, u) :: !uris; !ok
120       | E.Abst w ->
121          KP.fprintf och "(gdec+1 c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) w out_uri u;
122          uris := (false, u) :: !uris; !ok
123       | E.Void   -> C.err ()
124    end else !ok
125
126 let close_out_lp1 och () =
127    let aux_sep _ = KP.fprintf och "%s" ")" in
128    KP.fprintf och "%s" "gtop";   
129    List.iter aux_sep !uris;
130    out_clause och "\n\n.";
131    close_out och
132
133 (* elpi variant 2 ***********************************************************)
134
135 let output_entity_lp2 och st (_, na, u, b) =
136    if na.E.n_apix <= !G.last then begin
137    match b with
138       | E.Abbr v ->
139          KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
140             out_uri u na.E.n_apix (out_term st B.empty) v;
141          uris := (true, u) :: !uris; !ok
142       | E.Abst w ->
143          KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
144             out_uri u na.E.n_apix (out_term st B.empty) w;
145          uris := (false, u) :: !uris; !ok
146       | E.Void   -> C.err ()
147    end else !ok
148
149 let close_out_lp2 och () =
150    let aux_name (b, s) =
151       let gde = if b then "gdef+2" else "gdec+2" in 
152       KP.fprintf och "(%s %a\n" gde out_uri s
153    in
154    let aux_sep _ = KP.fprintf och "%s" ")" in
155    if !G.first > 0 then begin
156       let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
157       out_clause och s;
158       out_clause och "tv+c C T :- tv+ T."
159    end;
160    out_clause och "main :- grundlagen.";
161    out_clause och "grundlagen :- gv+";
162    List.iter aux_name (List.rev !uris);
163    KP.fprintf och "%s" "gtop";
164    List.iter aux_sep !uris;
165    out_clause och "\n\n.";
166    close_out och
167
168 (* teyjus variant 2 *************************************************)
169
170 let output_entity_tj2 och st (_, na, u, b) =
171    if na.E.n_apix <= !G.last then begin
172    out_comment och (KP.sprintf "constant %u" na.E.n_apix); 
173    match b with
174       | E.Abbr v ->
175          KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
176             out_uri u (top_age - na.E.n_apix) (out_term st B.empty) v;
177          uris := (true, u) :: !uris; !ok
178       | E.Abst w ->
179          KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
180             out_uri u (top_age - na.E.n_apix) (out_term st B.empty) w;
181          uris := (false, u) :: !uris; !ok
182       | E.Void   -> C.err ()
183    end else !ok
184
185 let close_out_tj2 och () =
186    let aux_name (b, u) =
187       let gde = if b then "gdef+2" else "gdec+2" in 
188       KP.fprintf och "(%s %a\n" gde out_uri u
189    in
190    let aux_sep _ = KP.fprintf och "%s" ")" in
191    if !G.first > 0 then begin
192       let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
193       out_clause och s;
194       out_clause och "tv+c C T :- tv+ T."
195    end;
196    out_clause och "main :- grundlagen.";
197    out_clause och "grundlagen :- gv+";
198    List.iter aux_name (List.rev !uris);
199    KP.fprintf och "%s" "gtop";
200    List.iter aux_sep !uris;
201    out_clause och "\n\n.";
202    close_out och
203
204 (* teyjus variant 3 *************************************************)
205
206 let output_entity_tj3 och st (_, na, u, b) =
207    if na.E.n_apix <= !G.last then begin
208       out_comment och (KP.sprintf "constant %u" na.E.n_apix);
209       let age = top_age - na.E.n_apix in
210       match b with
211          | E.Abbr v ->
212             KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
213                out_uri u age (out_term st B.empty) v;
214             KP.fprintf och "tv+ %a.\n\n" out_uri u;
215             KP.fprintf och "r+exp %a M C E M V :- g+line %a C V.\n\n"
216                out_uri u out_uri u;
217             uris := (true, u) :: !uris; !ok
218          | E.Abst w ->
219             KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
220                out_uri u age (out_term st B.empty) w;
221             KP.fprintf och "tv+ %a.\n\n" out_uri u;
222             KP.fprintf och "r+exp %a M1 C E M2 W :- m+pred M1 M2, g+line %a C W.\n\n"
223                out_uri u out_uri u;
224             uris := (false, u) :: !uris; !ok
225          | E.Void   -> C.err ()
226    end else !ok
227
228 let close_out_tj3 och () =
229    let aux_name (_, u) =
230       KP.fprintf och "gv+3 %a,\n" out_uri u
231    in
232    if !G.first > 0 then begin
233       let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
234       out_clause och s;
235       out_clause och "tv+c C T :- tv+ T."
236    end;
237    out_clause och "main :- grundlagen.";
238    out_clause och "grundlagen :-";
239    List.iter aux_name (List.rev !uris);
240    out_clause och "!.";
241    close_out och
242
243 (* Interface functions ******************************************************)
244
245 let open_out_lp1 fname =
246    let dir = KF.concat !G.manager_dir base in 
247    let path = KF.concat dir fname in
248    let och = open_out (path ^ "1" ^ ext_lp) in
249    out_preamble och;
250    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
251    out_clause och "main :- grundlagen.";
252    out_clause och "grundlagen :- gv+";
253    output_entity_lp1 och, close_out_lp1 och
254
255 let open_out_lp2 fname =
256    let dir = KF.concat !G.manager_dir base in 
257    let path = KF.concat dir fname in
258    let och = open_out (path ^ "2" ^ ext_lp) in
259    out_preamble och;
260    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
261    output_entity_lp2 och, close_out_lp2 och
262
263 let open_out_tj2 fname =
264    let dir = KF.concat !G.manager_dir base in 
265    let path = KF.concat dir fname in
266    let och = open_out (path ^ "2" ^ ext_tj) in
267    out_preamble och;
268    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
269    out_clause och "module grundlagen.";
270    out_clause och "accumulate helena.";
271    output_entity_tj2 och, close_out_tj2 och
272
273 let open_out_tj3 fname =
274    let dir = KF.concat !G.manager_dir base in 
275    let path = KF.concat dir fname in
276    let och = open_out (path ^ "3" ^ ext_tj) in
277    out_preamble och;
278    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
279    out_clause och "module grundlagen.";
280    out_clause och "accumulate helena.";
281    output_entity_tj3 och, close_out_tj3 och
282
283 END