]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/basic_rg/brgLP.ml
advances on exportation to prolog
[helm.git] / helm / software / helena / src / basic_rg / brgLP.ml
diff --git a/helm/software/helena/src/basic_rg/brgLP.ml b/helm/software/helena/src/basic_rg/brgLP.ml
new file mode 100644 (file)
index 0000000..d4ed3c9
--- /dev/null
@@ -0,0 +1,283 @@
+(*
+    ||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 top_age = 7000
+
+let uris = ref []
+
+let base = "lp"
+
+let ext_lp = ".elpi"
+let ext_tj = ".mod"
+
+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+set" else if h = 1 then "k+prop" 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 (_, x, v, t)             ->
+      let c = if x then "appx" else "appr" in
+      KP.fprintf och "(%s %a %a)" c (out_term st e) v (out_term st e) t
+   | B.Bind (a, B.Abst (r, n, w), t) ->
+      let a = R.alpha B.mem e a in
+      let ee = B.push e B.empty a (B.abst r n w) in
+      let c = if r then "prod" else "abst" in
+      let l = match N.to_string st n with
+         | "1" -> "l+1"
+         | "2" -> "l+2"
+         | _   -> ok := false; "?"
+      in
+      KP.fprintf och "(%s %s %a %a\\ %a)"
+         c 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
+
+(* elpi variant 1 ***********************************************************)
+
+let output_entity_lp1 och st (_, na, s, b) =
+   if na.E.n_apix <= !G.last then begin
+   match b with
+      | E.Abbr t ->
+         KP.fprintf och "(gdef+1 c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) t out_uri s;
+         uris := (true, s) :: !uris; !ok
+      | E.Abst u ->
+         KP.fprintf och "(gdec+1 c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) u out_uri s;
+         uris := (false, s) :: !uris; !ok
+      | E.Void   -> C.err ()
+   end else !ok
+
+let close_out_lp1 och () =
+   let aux_sep _ = KP.fprintf och "%s" ")" in
+   KP.fprintf och "%s" "gtop";   
+   List.iter aux_sep !uris;
+   out_clause och "\n\n.";
+   close_out och
+
+(* elpi variant 2 ***********************************************************)
+
+let output_entity_lp2 och st (_, na, s, b) =
+   if na.E.n_apix <= !G.last then begin
+   match b with
+      | E.Abbr t ->
+         KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
+            out_uri s na.E.n_apix (out_term st B.empty) t;
+         uris := (true, s) :: !uris; !ok
+      | E.Abst u ->
+         KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
+            out_uri s na.E.n_apix (out_term st B.empty) u;
+         uris := (false, s) :: !uris; !ok
+      | E.Void   -> C.err ()
+   end else !ok
+
+let close_out_lp2 och () =
+   let aux_name (b, s) =
+      let gde = if b then "gdef+2" else "gdec+2" in 
+      KP.fprintf och "(%s %a\n" gde out_uri s
+   in
+   let aux_sep _ = KP.fprintf och "%s" ")" in
+   if !G.first > 0 then begin
+      let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
+      out_clause och s;
+      out_clause och "tv+c C T :- tv+ T."
+   end;
+   out_clause och "main :- grundlagen.";
+   out_clause och "grundlagen :- gv+ ";
+   List.iter aux_name (List.rev !uris);
+   KP.fprintf och "%s" "gtop";
+   List.iter aux_sep !uris;
+   out_clause och "\n\n.";
+   close_out och
+
+(* teyjus variant 2 *************************************************)
+
+let output_entity_tj2 och st (_, na, s, b) =
+   if na.E.n_apix <= !G.last then begin
+   out_comment och (KP.sprintf "constant %u" na.E.n_apix); 
+   match b with
+      | E.Abbr t ->
+         KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
+            out_uri s (top_age - na.E.n_apix) (out_term st B.empty) t;
+         uris := (true, s) :: !uris; !ok
+      | E.Abst u ->
+         KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
+            out_uri s (top_age - na.E.n_apix) (out_term st B.empty) u;
+         uris := (false, s) :: !uris; !ok
+      | E.Void   -> C.err ()
+   end else !ok
+
+let close_out_tj2 och () =
+   let aux_name (b, s) =
+      let gde = if b then "gdef+2" else "gdec+2" in 
+      KP.fprintf och "(%s %a\n" gde out_uri s
+   in
+   let aux_sep _ = KP.fprintf och "%s" ")" in
+   if !G.first > 0 then begin
+      let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
+      out_clause och s;
+      out_clause och "tv+c C T :- tv+ T."
+   end;
+   out_clause och "main :- grundlagen.";
+   out_clause och "grundlagen :- gv+ ";
+   List.iter aux_name (List.rev !uris);
+   KP.fprintf och "%s" "gtop";
+   List.iter aux_sep !uris;
+   out_clause och "\n\n.";
+   close_out och
+
+(* teyjus variant 3 *************************************************)
+
+let output_entity_tj3 och st (_, na, u, b) =
+   if na.E.n_apix <= !G.last then begin
+      out_comment och (KP.sprintf "constant %u" na.E.n_apix);
+      let age = top_age - na.E.n_apix in
+      match b with
+         | E.Abbr v ->
+            KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
+               out_uri u age (out_term st B.empty) v;
+            KP.fprintf och "tv+ %a.\n\n" out_uri u;
+            KP.fprintf och "r+exp %a M C E M V :- g+line %a C V.\n\n"
+               out_uri u out_uri u;
+            uris := (true, u) :: !uris; !ok
+         | E.Abst w ->
+            KP.fprintf och "g+line %a %u\n       %a\n.\n\n"
+               out_uri u age (out_term st B.empty) w;
+            KP.fprintf och "tv+ %a.\n\n" out_uri u;
+            KP.fprintf och "r+exp %a M1 C E M2 W :- m+pred M1 M2, g+line %a C W.\n\n"
+               out_uri u out_uri u;
+            uris := (false, u) :: !uris; !ok
+         | E.Void   -> C.err ()
+   end else !ok
+
+let close_out_tj3 och () =
+   let aux_name (_, u) =
+      KP.fprintf och "gv+3 %a,\n" out_uri u
+   in
+   if !G.first > 0 then begin
+      let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
+      out_clause och s;
+      out_clause och "tv+c C T :- tv+ T."
+   end;
+   out_clause och "main :- grundlagen.";
+   out_clause och "grundlagen :-";
+   List.iter aux_name (List.rev !uris);
+   out_clause och "!.";
+   close_out och
+
+(* Interface functions ******************************************************)
+
+let open_out_lp1 fname =
+   let dir = KF.concat !G.manager_dir base in 
+   let path = KF.concat dir fname in
+   let och = open_out (path ^ "1" ^ ext_lp) 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 "main :- grundlagen.";
+   out_clause och "grundlagen :- gv+";
+   output_entity_lp1 och, close_out_lp1 och
+
+let open_out_lp2 fname =
+   let dir = KF.concat !G.manager_dir base in 
+   let path = KF.concat dir fname in
+   let och = open_out (path ^ "2" ^ ext_lp) in
+   out_preamble och;
+   out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
+   output_entity_lp2 och, close_out_lp2 och
+
+let open_out_tj2 fname =
+   let dir = KF.concat !G.manager_dir base in 
+   let path = KF.concat dir fname in
+   let och = open_out (path ^ "2" ^ ext_tj) 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 "module grundlagen.";
+   out_clause och "accumulate helena.";
+   output_entity_tj2 och, close_out_tj2 och
+
+let open_out_tj3 fname =
+   let dir = KF.concat !G.manager_dir base in 
+   let path = KF.concat dir fname in
+   let och = open_out (path ^ "3" ^ ext_tj) 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 "module grundlagen.";
+   out_clause och "accumulate helena.";
+   output_entity_tj3 och, close_out_tj3 och