]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/basic_rg/brgLYP.ml
update in helena
[helm.git] / helm / software / helena / src / basic_rg / brgLYP.ml
diff --git a/helm/software/helena/src/basic_rg/brgLYP.ml b/helm/software/helena/src/basic_rg/brgLYP.ml
new file mode 100644 (file)
index 0000000..0c4a2ab
--- /dev/null
@@ -0,0 +1,226 @@
+(*
+    ||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
+
+IFDEF MANAGER THEN
+
+(* Internal functions *******************************************************)
+
+let version = KP.sprintf "This file was generated by %s: do not edit" (G.version_string true)
+
+let affiliation = "This file is part of the LYP engine"
+
+let description = "This file contains the Grundlagen for lyp_global: validate by invoking clause (grundlagen)"
+
+let ok = ref true
+
+let trusted = ref false (* "trusted" indicator out? *)
+
+let untrusted = ref false (* "untrusted" indicator out? *)
+
+let uris = ref []
+
+let chunk = ref 0
+
+let sub_och = ref stdout
+
+let top_age = 7000
+
+let size = 250
+
+let base = "lp"
+
+let ext_lp = ".elpi"
+let ext_tj = ".mod"
+let ext_tj_sig = ".sig"
+
+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 appl_fragment (_, ds) (_, dm) = match ds, dm with
+   | 2, 2 -> "ss"
+   | 2, 1 -> "sb"
+   | 1, 2 -> "bs"
+   | 1, 1 -> "bb"
+   | _    -> ok := false; "??"
+
+let abst_fragment (_, ds) (_, dm) = match ds, dm with
+   | 1, 2 -> "ss"
+   | 1, 1 -> "sb"
+   | 0, 2 -> "bs"
+   | 0, 1 -> "bb"
+   | _    -> ok := false; "??"
+
+let prod_fragment (_, ds) (_, dm) = match ds, dm with
+   | 1, 1 -> "ss"
+   | 1, 0 -> "sb"
+   | 0, 1 -> "bs"
+   | 0, 0 -> "bb"
+   | _    -> ok := false; "??"
+
+let entity_level (_, ds) = match ds with
+   | 1 -> "1"
+   | 2 -> "2"
+   | _ -> ok := false; "?"
+
+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 k                        ->
+      KP.fprintf och "sort"
+   | B.LRef (_, i)                   ->
+      let _, _, _, y, b = B.get e i in
+      KP.fprintf och "%a" out_name y
+   | B.GRef (_, s)                   ->
+      KP.fprintf och "%a" out_uri s
+   | B.Cast (u, t)                   ->
+      C.err ()
+   | B.Appl (a, v, t)                ->
+      let f = appl_fragment a.E.a_side a.E.a_main in 
+      KP.fprintf och "(appl f_%s %a %a)" f (out_term st e) v (out_term st e) t
+   | B.Bind (a, B.Abst (r, n, w), t) -> (* r *)
+      let a = R.alpha B.mem e a in
+      let ee = B.push e B.empty E.empty_node a (B.abst r n w) in
+      let lf = match N.to_string st n with
+         | "1" -> "prod", prod_fragment a.E.b_side a.E.b_main 
+         | "2" -> "abst", abst_fragment a.E.b_side a.E.b_main
+         | _   -> ok := false; "????", "??"
+      in
+      let l, f = match lf with
+         | "abst", "bs" -> "rest", "bs"
+         | "prod", "bs" -> "rest", "ho"
+         | "abst", "bb" -> "rest", "ho"
+         | "prod", "bb" -> "rest", "bb"
+         | lf           -> lf 
+      in 
+      KP.fprintf och "(%s f_%s %a %a\\ %a)"
+         l f (out_term st e) w out_name a (out_term st ee) t
+   | B.Bind (_, B.Abbr _, _)         -> C.err ()
+   | B.Bind (_, B.Void, _)           -> C.err ()
+
+(* LYP variant 2 ***********************************************************)
+
+let output_entity_lyp2 och st (_, na, u, b) =
+   begin match !trusted, !untrusted with
+      | false, _ when na.E.n_apix < !G.first ->
+         out_top_comment och "Trusted part";
+         trusted := true
+      | _ , false when !G.first <= na.E.n_apix && na.E.n_apix <= !G.last ->
+         out_top_comment och "Untrusted part";
+         untrusted := true
+      | _ -> ()
+   end;
+   if na.E.n_apix < !G.first then match b with
+      | E.Abbr (a, B.Cast (w, v)) ->
+         KP.fprintf och "type %a @id.\n" out_uri u;
+         KP.fprintf och "dec_l %a d_%s\n      %a\n.\n"
+            out_uri u (entity_level a.E.e_side) (out_term st B.empty) w;
+         KP.fprintf och "def_l %a\n      %a\n.\n\n"
+            out_uri u (out_term st B.empty) v;
+         !ok
+      | E.Abst (a, w)             ->
+         KP.fprintf och "type %a @id.\n" out_uri u;
+         KP.fprintf och "dec_l %a d_%s\n      %a\n.\n\n"
+            out_uri u (entity_level a.E.e_side) (out_term st B.empty) w;
+         !ok
+      | _                         -> C.err ()
+   else if na.E.n_apix <= !G.last then match b with
+      | E.Abbr (a, B.Cast (w, v)) ->
+         KP.fprintf och "type %a @id.\n" out_uri u;
+         KP.fprintf och "type c_%u ann.\n" na.E.n_apix;
+         KP.fprintf och "cast_g %a c_%u d_%s\n       %a\n       %a\n.\n\n"
+            out_uri u na.E.n_apix (entity_level a.E.e_side)
+            (out_term st B.empty) w (out_term st B.empty) v;
+         uris := (true, u) :: !uris; !ok
+      | E.Abst (a, w)             ->
+         KP.fprintf och "type %a @id.\n" out_uri u; 
+         KP.fprintf och "type c_%u ann.\n" na.E.n_apix;
+         KP.fprintf och "type_g %a c_%u d_%s\n       %a\n.\n\n"
+            out_uri u na.E.n_apix (entity_level a.E.e_side)
+            (out_term st B.empty) w;
+         uris := (false, u) :: !uris; !ok
+      | _                         -> C.err ()
+   else !ok
+
+let close_out_lyp2 och () =
+   let aux_name (b, s) =
+      let gde = if b then "g_def" else "g_dec" in 
+      KP.fprintf och "(%s %a\n" gde out_uri s
+   in
+   let aux_sep _ = KP.fprintf och "%s" ")" in
+   out_clause och "type grundlagen prop.";
+   out_clause och "grundlagen :- valid_g";
+   List.iter aux_name (List.rev !uris);
+   KP.fprintf och "%s" "g_top";
+   List.iter aux_sep !uris;
+   out_clause och "\n\n.";
+   close_out och
+
+(* Interface functions ******************************************************)
+
+let open_out_lyp2 fname =
+   let dir = KF.concat !G.manager_dir base in 
+   let path = KF.concat dir fname in
+   let och = open_out (path ^ "b_lyp" ^ ext_lp) in
+   out_preamble och;
+   out_comment och version;
+   out_comment och affiliation;
+   out_top_comment och description;
+   output_entity_lyp2 och, close_out_lyp2 och
+
+END