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 *******************************************************)
40 let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
43 if List.mem n reserved then !G.alpha ^ n else n
45 let out_preamble och =
46 let ich = open_in !G.preamble in
47 let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
48 try aux () with End_of_file -> close_in ich
50 let out_top_comment och msg =
51 KP.fprintf och "%% %s\n\n" msg
53 let out_comment och msg =
54 KP.fprintf och "%% %s\n" msg
56 let out_clause och msg =
57 KP.fprintf och "%s\n\n" msg
60 let str = U.string_of_uri u in
63 if c = '.' then () else begin
64 output_char och (if c = '/' then '_' else c);
69 if n <= 0 then succ i else
70 strip (String.index_from str (succ i) '/') (pred n)
76 | true -> KP.fprintf och "%s" (alpha n)
77 | false -> KP.fprintf och "_"
79 let err () = f "" false in
82 let rec out_term st e och = function
84 let sort = if k = 0 then "k+set" else if k = 1 then "k+prop" else assert false in
85 KP.fprintf och "(sort %s)" sort
87 let _, _, _, y, b = B.get e i in
88 KP.fprintf och "%a" out_name y
90 KP.fprintf och "%a" out_uri s
92 KP.fprintf och "(cast %a %a)" (out_term st e) u (out_term st e) t
94 let c = if x then "appx" else "appr" in
95 KP.fprintf och "(%s %a %a)" c (out_term st e) v (out_term st e) t
96 | B.Bind (y, B.Abst (r, n, w), t) ->
97 let y = R.alpha B.mem e y in
98 let ee = B.push e B.empty E.empty_node y (B.abst r n w) in
99 let c = if r then "prod" else "abst" in
100 let l = match N.to_string st n with
103 | _ -> ok := false; "?"
105 KP.fprintf och "(%s %s %a %a\\ %a)"
106 c l (out_term st e) w out_name y (out_term st ee) t
107 | B.Bind (y, B.Abbr v, t) ->
108 let y = R.alpha B.mem e y in
109 let ee = B.push e B.empty E.empty_node y (B.abbr v) in
110 KP.fprintf och "(abbr %a %a\\ %a)"
111 (out_term st e) v out_name y (out_term st ee) t
112 | B.Bind (_, B.Void, _) -> C.err ()
114 (* elpi variant 1 ***********************************************************)
116 let output_entity_lp1 och st (_, na, u, b) =
117 if na.E.n_apix <= !G.last then begin
120 KP.fprintf och "(gdef+1 c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) v out_uri u;
121 uris := (true, u) :: !uris; !ok
123 KP.fprintf och "(gdec+1 c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) w out_uri u;
124 uris := (false, u) :: !uris; !ok
128 let close_out_lp1 och () =
129 let aux_sep _ = KP.fprintf och "%s" ")" in
130 KP.fprintf och "%s" "gtop";
131 List.iter aux_sep !uris;
132 out_clause och "\n\n.";
135 (* elpi variant 2 ***********************************************************)
137 let output_entity_lp2 och st (_, na, u, b) =
138 if na.E.n_apix <= !G.last then begin
141 KP.fprintf och "g+line %a c+%u\n %a\n.\n\n"
142 out_uri u na.E.n_apix (out_term st B.empty) v;
143 uris := (true, u) :: !uris; !ok
145 KP.fprintf och "g+line %a c+%u\n %a\n.\n\n"
146 out_uri u na.E.n_apix (out_term st B.empty) w;
147 uris := (false, u) :: !uris; !ok
151 let close_out_lp2 och () =
152 let aux_name (b, s) =
153 let gde = if b then "gdef+2" else "gdec+2" in
154 KP.fprintf och "(%s %a\n" gde out_uri s
156 let aux_sep _ = KP.fprintf och "%s" ")" in
157 if !G.first > 0 then begin
158 let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
160 out_clause och "tv+c C T :- tv+ T."
162 out_clause och "main :- grundlagen.";
163 out_clause och "grundlagen :- gv+";
164 List.iter aux_name (List.rev !uris);
165 KP.fprintf och "%s" "gtop";
166 List.iter aux_sep !uris;
167 out_clause och "\n\n.";
170 (* teyjus variant 2 *************************************************)
172 let output_entity_tj2 och st (_, na, u, b) =
173 if na.E.n_apix <= !G.last then begin
174 out_comment och (KP.sprintf "constant %u" na.E.n_apix);
177 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
178 out_uri u (top_age - na.E.n_apix) (out_term st B.empty) v;
179 uris := (true, u) :: !uris; !ok
181 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
182 out_uri u (top_age - na.E.n_apix) (out_term st B.empty) w;
183 uris := (false, u) :: !uris; !ok
187 let close_out_tj2 och () =
188 let out_name (b, u) =
189 let gde = if b then "gdef+2" else "gdec+2" in
190 KP.fprintf och "(%s %a\n" gde out_uri u
192 let rec out_pars och p =
193 if p > 0 then begin KP.fprintf och "%s" ")"; out_pars och (pred p) end
195 let rec out_list och chunk pars first items uris = match first, items, uris with
197 KP.fprintf och "g+list %u\n" chunk;
198 out_list och (succ chunk) pars false items uris
199 | false, _, [] -> KP.fprintf och "gtop%a.\n\n" out_pars pars
201 KP.fprintf och "(genv %u)%a.\n\n" chunk out_pars pars;
202 out_list och chunk 0 true size uris
203 | false, _, hd :: tl ->
204 out_name hd; out_list och chunk (succ pars) false (pred items) tl
206 out_list och 1 0 true size (List.rev !uris);
207 out_clause och "main :- grundlagen.";
208 out_clause och "grundlagen :- gv+ (genv 1).";
211 (* teyjus variant 3 *************************************************)
213 let output_entity_tj3 och st (_, na, u, b) =
214 if na.E.n_apix <= !G.last then begin
215 out_comment och (KP.sprintf "constant %u" na.E.n_apix);
216 let age = top_age - na.E.n_apix in
219 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
220 out_uri u age (out_term st B.empty) v;
221 KP.fprintf och "tv+ %a.\n\n" out_uri u;
222 KP.fprintf och "r+exp %a M C E M V :- g+line %a C V.\n\n"
224 uris := (true, u) :: !uris; !ok
226 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
227 out_uri u age (out_term st B.empty) w;
228 KP.fprintf och "tv+ %a.\n\n" out_uri u;
229 KP.fprintf och "r+exp %a M1 C E M2 W :- m+pred M1 M2, g+line %a C W.\n\n"
231 uris := (false, u) :: !uris; !ok
235 let close_out_tj3 och () =
236 let out_name (_, u) =
237 KP.fprintf och "gv+3 %a,\n" out_uri u
239 let rec out_list och chunk first items uris = match first, items, uris with
241 KP.fprintf och "chunk %u :-\n" chunk;
242 out_list och (succ chunk) false items uris
243 | false, _, [] -> KP.fprintf och "!.\n\n"; chunk
245 KP.fprintf och "!.\n\n";
246 out_list och chunk true size uris
247 | false, _, hd :: tl ->
248 out_name hd; out_list och chunk false (pred items) tl
250 let rec out_chunks och chunks c =
251 if chunks < c then out_clause och "!." else begin
252 KP.fprintf och "chunk %u,\n" c; out_chunks och chunks (succ c)
255 let chunks = out_list och 1 true size (List.rev !uris) in
256 out_clause och "main :- grundlagen.";
257 KP.fprintf och "grundlagen :-\n";
258 out_chunks och (pred chunks) 1;
261 (* Interface functions ******************************************************)
263 let open_out_lp1 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 ^ "1" ^ ext_lp) in
268 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
269 out_clause och "main :- grundlagen.";
270 out_clause och "grundlagen :- gv+";
271 output_entity_lp1 och, close_out_lp1 och
273 let open_out_lp2 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 ^ "2" ^ ext_lp) in
278 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
279 output_entity_lp2 och, close_out_lp2 och
281 let open_out_tj2 fname =
282 let dir = KF.concat !G.manager_dir base in
283 let path = KF.concat dir fname in
284 let och = open_out (path ^ "2" ^ ext_tj) in
286 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
287 out_clause och "module grundlagen.";
288 out_clause och "accumulate helena.";
289 output_entity_tj2 och, close_out_tj2 och
291 let open_out_tj3 fname =
292 let dir = KF.concat !G.manager_dir base in
293 let path = KF.concat dir fname in
294 let och = open_out (path ^ "3" ^ ext_tj) in
296 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
297 out_clause och "module grundlagen.";
298 out_clause och "accumulate helena.";
299 output_entity_tj3 och, close_out_tj3 och