(* ||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 base = "coq" let ext = ".v" let width = 70 let reserved = ["if"] 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 = let padding = width - String.length msg in let padding = if padding >= 0 then padding else 0 in KP.fprintf och "(* %s %s*)\n\n" msg (String.make padding '*') let out_comment och msg = KP.fprintf och "(* %s *)\n" msg let out_include och src = KP.fprintf och "include \"%s.ma\".\n\n" src 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 p e och = function | B.Sort (_, h) -> let sort = if h = 0 then "Type" else if h = 1 then "Prop" else assert false in KP.fprintf och "%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 "(%a : %a)" (out_term st false e) t (out_term st false e) u | B.Appl (_, _, v, t) -> let pt = match t with B.Appl _ -> false | _ -> true in let op, cp = if p then "(", ")" else "", "" in KP.fprintf och "%s%a %a%s" op (out_term st pt e) t (out_term st true e) v cp | B.Bind (a, B.Abst (r, n, w), t) -> let p = true in let op, cp = if p then "(", ")" else "", "" in let a = R.alpha B.mem e a in let ee = B.push e B.empty a (B.abst r n w) in let ob, cb = match N.to_string st n with | "1" -> "forall", "," | "2" -> "fun", " =>" | _ -> ok := false; "?", "?" in KP.fprintf och "%s%s (%a:%a)%s %a%s" op ob out_name a (out_term st false e) w cb (out_term st false ee) t cp | B.Bind (a, B.Abbr v, t) -> let op, cp = if p then "(", ")" else "", "" in let a = R.alpha B.mem e a in let ee = B.push e B.empty a (B.abbr v) in KP.fprintf och "%slet %a := %a in %a%s" op out_name a (out_term st false e) v (out_term st false ee) t cp | B.Bind (a, B.Void, t) -> C.err () let close_out och () = close_out och let output_entity och st (_, na, u, b) = out_comment och (KP.sprintf "constant %u" na.E.n_apix); match b with | E.Abbr v -> KP.fprintf och "Definition %a := %a.\n\n%!" out_uri u (out_term st false B.empty) v; (* KP.fprintf och "Strategy -%u [ %a ].\n\n%!" na.E.n_apix out_uri u; *) !ok | E.Abst w -> KP.fprintf och "Axiom %a : %a.\n\n%!" out_uri u (out_term st false B.empty) w; !ok | E.Void -> C.err () (* 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); output_entity och, close_out och