+(*
+ ||M|| This file is part of HELM, an Hypertextual, Electronic
+ ||A|| Library of Mathematics, developed at the Computer Science
+ ||T|| Department, University of Bologna, Italy.
+ ||I||
+ ||T|| HELM is free software; you can redistribute it and/or
+ ||A|| modify it under the terms of the GNU General Public License
+ \ / version 2 or (at your option) any later version.
+ \ / This software is distributed as is, NO WARRANTY.
+ V_______________________________________________________________ *)
+
+module KF = Filename
+module KP = Printf
+
+module U = NUri
+module C = Cps
+module G = Options
+module N = Layer
+module E = Entity
+module R = Alpha
+module B = Brg
+
+(* Internal functions *******************************************************)
+
+let ok = ref true
+
+let level = ref 0
+
+let base = "elpi"
+
+let ext = ".elpi"
+
+let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
+
+let alpha n =
+ if List.mem n reserved then !G.alpha ^ n else n
+
+let out_preamble och =
+ let ich = open_in !G.preamble in
+ let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
+ try aux () with End_of_file -> close_in ich
+
+let out_top_comment och msg =
+ KP.fprintf och "%% %s\n\n" msg
+
+let out_comment och msg =
+ KP.fprintf och "%% %s\n" msg
+
+let out_clause och msg =
+ KP.fprintf och "%s\n\n" msg
+
+let out_uri och u =
+ let str = U.string_of_uri u in
+ let rec aux i =
+ let c = str.[i] in
+ if c = '.' then () else begin
+ output_char och (if c = '/' then '_' else c);
+ aux (succ i)
+ end
+ in
+ let rec strip i n =
+ if n <= 0 then succ i else
+ strip (String.index_from str (succ i) '/') (pred n)
+ in
+ aux (strip 0 3)
+
+let out_name och a =
+ let f n = function
+ | true -> KP.fprintf och "%s" (alpha n)
+ | false -> KP.fprintf och "_"
+ in
+ let err () = f "" false in
+ E.name err f a
+
+let rec out_term st e och = function
+ | B.Sort (_, h) ->
+ let sort = if h = 0 then "k+0" else if h = 1 then "k+1" else assert false in
+ KP.fprintf och "(sort %s)" sort
+ | B.LRef (_, i) ->
+ let _, _, a, b = B.get e i in
+ KP.fprintf och "%a" out_name a
+ | B.GRef (_, s) ->
+ KP.fprintf och "%a" out_uri s
+ | B.Cast (_, u, t) ->
+ KP.fprintf och "(cast %a %a)" (out_term st e) u (out_term st e) t
+ | B.Appl (_, v, t) ->
+ KP.fprintf och "(appx %a %a)" (out_term st e) v (out_term st e) t
+ | B.Bind (a, B.Abst (n, w), t) ->
+ let a = R.alpha B.mem e a in
+ let ee = B.push e B.empty a (B.abst n w) in
+ let l = match N.to_string st n with
+ | "1" -> "l+1"
+ | "2" -> "l+2"
+ | _ -> ok := false; "?"
+ in
+ KP.fprintf och "(abst %s %a %a\\ %a)"
+ l (out_term st e) w out_name a (out_term st ee) t
+ | B.Bind (a, B.Abbr v, t) ->
+ let a = R.alpha B.mem e a in
+ let ee = B.push e B.empty a (B.abbr v) in
+ KP.fprintf och "(abbr %a %a\\ %a)"
+ (out_term st e) v out_name a (out_term st ee) t
+ | B.Bind (a, B.Void, t) ->
+ let a = R.alpha B.mem e a in
+ let ee = B.push e B.empty a (B.Void) in
+ KP.fprintf och "(void %a\\ %a)"
+ out_name a (out_term st ee) t
+
+let output_entity och st (_, na, s, b) =
+(* if na.E.n_apix <= 4500 then begin *)
+ out_comment och (KP.sprintf "constant %u" na.E.n_apix);
+ match b with
+ | E.Abbr t ->
+ KP.fprintf och "(gdef c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) t out_uri s;
+ incr level; !ok
+ | E.Abst u ->
+ KP.fprintf och "(gdec c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) u out_uri s;
+ incr level; !ok
+ | E.Void -> C.err ()
+(* end else !ok *)
+
+let close_out och () =
+ let rec aux () =
+ if !level <= 0 then KP.fprintf och "%s" "\n\n).\n"
+ else begin KP.fprintf och "%s" ")"; decr level; aux () end
+ in
+ KP.fprintf och "%s" "gtop";
+ aux (); close_out och
+
+(* Interface functions ******************************************************)
+
+let open_out fname =
+ let dir = KF.concat !G.manager_dir base in
+ let path = KF.concat dir fname in
+ let och = open_out (path ^ ext) in
+ out_preamble och;
+ out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
+ out_clause och "k+succ k+0 k+2.";
+ out_clause och "k+succ k+1 k+3.";
+ out_clause och "grundlagen :- gv+ (";
+ output_entity och, close_out och