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.
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_______________________________________________________________ *)
25 (* Internal functions *******************************************************)
38 let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
41 if List.mem n reserved then !G.alpha ^ n else n
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
48 let out_top_comment och msg =
49 KP.fprintf och "%% %s\n\n" msg
51 let out_comment och msg =
52 KP.fprintf och "%% %s\n" msg
54 let out_clause och msg =
55 KP.fprintf och "%s\n\n" msg
58 let str = U.string_of_uri u in
61 if c = '.' then () else begin
62 output_char och (if c = '/' then '_' else c);
67 if n <= 0 then succ i else
68 strip (String.index_from str (succ i) '/') (pred n)
74 | true -> KP.fprintf och "%s" (alpha n)
75 | false -> KP.fprintf och "_"
77 let err () = f "" false in
80 let rec out_term st e och = function
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
85 let _, _, _, y, b = B.get e i in
86 KP.fprintf och "%a" out_name y
88 KP.fprintf och "%a" out_uri s
90 KP.fprintf och "(cast %a %a)" (out_term st e) u (out_term st e) 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
101 | _ -> ok := false; "?"
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 ()
112 (* elpi variant 1 ***********************************************************)
114 let output_entity_lp1 och st (_, na, u, b) =
115 if na.E.n_apix <= !G.last then begin
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
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
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.";
133 (* elpi variant 2 ***********************************************************)
135 let output_entity_lp2 och st (_, na, u, b) =
136 if na.E.n_apix <= !G.last then begin
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
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
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
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
158 out_clause och "tv+c C T :- tv+ T."
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.";
168 (* teyjus variant 2 *************************************************)
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);
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
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
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
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
194 out_clause och "tv+c C T :- tv+ T."
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.";
204 (* teyjus variant 3 *************************************************)
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
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"
217 uris := (true, u) :: !uris; !ok
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"
224 uris := (false, u) :: !uris; !ok
228 let close_out_tj3 och () =
229 let aux_name (_, u) =
230 KP.fprintf och "gv+3 %a,\n" out_uri u
232 if !G.first > 0 then begin
233 let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
235 out_clause och "tv+c C T :- tv+ T."
237 out_clause och "main :- grundlagen.";
238 out_clause och "grundlagen :-";
239 List.iter aux_name (List.rev !uris);
243 (* Interface functions ******************************************************)
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
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
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
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
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
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
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
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