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 h = 0 then "k+set" else if h = 1 then "k+prop" else assert false in
83 KP.fprintf och "(sort %s)" sort
85 let _, _, a, b = B.get e i in
86 KP.fprintf och "%a" out_name a
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
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 (a, B.Abst (r, n, w), t) ->
95 let a = R.alpha B.mem e a in
96 let ee = B.push e B.empty a (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 a (out_term st ee) t
105 | B.Bind (a, B.Abbr v, t) ->
106 let a = R.alpha B.mem e a in
107 let ee = B.push e B.empty a (B.abbr v) in
108 KP.fprintf och "(abbr %a %a\\ %a)"
109 (out_term st e) v out_name a (out_term st ee) t
110 | B.Bind (a, B.Void, t) ->
111 let a = R.alpha B.mem e a in
112 let ee = B.push e B.empty a (B.Void) in
113 KP.fprintf och "(void %a\\ %a)"
114 out_name a (out_term st ee) t
116 (* elpi variant 1 ***********************************************************)
118 let output_entity_lp1 och st (_, na, u, b) =
119 if na.E.n_apix <= !G.last then begin
122 KP.fprintf och "(gdef+1 c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) v out_uri u;
123 uris := (true, u) :: !uris; !ok
125 KP.fprintf och "(gdec+1 c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) w out_uri u;
126 uris := (false, u) :: !uris; !ok
130 let close_out_lp1 och () =
131 let aux_sep _ = KP.fprintf och "%s" ")" in
132 KP.fprintf och "%s" "gtop";
133 List.iter aux_sep !uris;
134 out_clause och "\n\n.";
137 (* elpi variant 2 ***********************************************************)
139 let output_entity_lp2 och st (_, na, u, b) =
140 if na.E.n_apix <= !G.last then begin
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) v;
145 uris := (true, u) :: !uris; !ok
147 KP.fprintf och "g+line %a c+%u\n %a\n.\n\n"
148 out_uri u na.E.n_apix (out_term st B.empty) w;
149 uris := (false, u) :: !uris; !ok
153 let close_out_lp2 och () =
154 let aux_name (b, s) =
155 let gde = if b then "gdef+2" else "gdec+2" in
156 KP.fprintf och "(%s %a\n" gde out_uri s
158 let aux_sep _ = KP.fprintf och "%s" ")" in
159 if !G.first > 0 then begin
160 let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
162 out_clause och "tv+c C T :- tv+ T."
164 out_clause och "main :- grundlagen.";
165 out_clause och "grundlagen :- gv+";
166 List.iter aux_name (List.rev !uris);
167 KP.fprintf och "%s" "gtop";
168 List.iter aux_sep !uris;
169 out_clause och "\n\n.";
172 (* teyjus variant 2 *************************************************)
174 let output_entity_tj2 och st (_, na, u, b) =
175 if na.E.n_apix <= !G.last then begin
176 out_comment och (KP.sprintf "constant %u" na.E.n_apix);
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) v;
181 uris := (true, u) :: !uris; !ok
183 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
184 out_uri u (top_age - na.E.n_apix) (out_term st B.empty) w;
185 uris := (false, u) :: !uris; !ok
189 let close_out_tj2 och () =
190 let aux_name (b, u) =
191 let gde = if b then "gdef+2" else "gdec+2" in
192 KP.fprintf och "(%s %a\n" gde out_uri u
194 let aux_sep _ = KP.fprintf och "%s" ")" in
195 if !G.first > 0 then begin
196 let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
198 out_clause och "tv+c C T :- tv+ T."
200 out_clause och "main :- grundlagen.";
201 out_clause och "grundlagen :- gv+";
202 List.iter aux_name (List.rev !uris);
203 KP.fprintf och "%s" "gtop";
204 List.iter aux_sep !uris;
205 out_clause och "\n\n.";
208 (* teyjus variant 3 *************************************************)
210 let output_entity_tj3 och st (_, na, u, b) =
211 if na.E.n_apix <= !G.last then begin
212 out_comment och (KP.sprintf "constant %u" na.E.n_apix);
213 let age = top_age - na.E.n_apix in
216 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
217 out_uri u age (out_term st B.empty) v;
218 KP.fprintf och "tv+ %a.\n\n" out_uri u;
219 KP.fprintf och "r+exp %a M C E M V :- g+line %a C V.\n\n"
221 uris := (true, u) :: !uris; !ok
223 KP.fprintf och "g+line %a %u\n %a\n.\n\n"
224 out_uri u age (out_term st B.empty) w;
225 KP.fprintf och "tv+ %a.\n\n" out_uri u;
226 KP.fprintf och "r+exp %a M1 C E M2 W :- m+pred M1 M2, g+line %a C W.\n\n"
228 uris := (false, u) :: !uris; !ok
232 let close_out_tj3 och () =
233 let aux_name (_, u) =
234 KP.fprintf och "gv+3 %a,\n" out_uri u
236 if !G.first > 0 then begin
237 let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
239 out_clause och "tv+c C T :- tv+ T."
241 out_clause och "main :- grundlagen.";
242 out_clause och "grundlagen :-";
243 List.iter aux_name (List.rev !uris);
247 (* Interface functions ******************************************************)
249 let open_out_lp1 fname =
250 let dir = KF.concat !G.manager_dir base in
251 let path = KF.concat dir fname in
252 let och = open_out (path ^ "1" ^ ext_lp) in
254 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
255 out_clause och "main :- grundlagen.";
256 out_clause och "grundlagen :- gv+";
257 output_entity_lp1 och, close_out_lp1 och
259 let open_out_lp2 fname =
260 let dir = KF.concat !G.manager_dir base in
261 let path = KF.concat dir fname in
262 let och = open_out (path ^ "2" ^ ext_lp) in
264 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
265 output_entity_lp2 och, close_out_lp2 och
267 let open_out_tj2 fname =
268 let dir = KF.concat !G.manager_dir base in
269 let path = KF.concat dir fname in
270 let och = open_out (path ^ "2" ^ ext_tj) in
272 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
273 out_clause och "module grundlagen.";
274 out_clause och "accumulate helena.";
275 output_entity_tj2 och, close_out_tj2 och
277 let open_out_tj3 fname =
278 let dir = KF.concat !G.manager_dir base in
279 let path = KF.concat dir fname in
280 let och = open_out (path ^ "3" ^ ext_tj) in
282 out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
283 out_clause och "module grundlagen.";
284 out_clause och "accumulate helena.";
285 output_entity_tj3 och, close_out_tj3 och