]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgELPI.ml
4e78eac13a0986eef5e98510b568a3333442c5fd
[helm.git] / helm / software / helena / src / basic_rg / brgELPI.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 module KF = Filename
13 module KP = Printf
14
15 module U  = NUri
16 module C  = Cps
17 module G  = Options
18 module N  = Layer
19 module E  = Entity
20 module R  = Alpha
21 module B  = Brg
22
23 (* Internal functions *******************************************************)
24
25 let ok = ref true
26
27 let uris = ref []
28
29 let base = "elpi"
30
31 let ext = ".elpi"
32
33 let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
34
35 let alpha n =
36    if List.mem n reserved then !G.alpha ^ n else n
37
38 let out_preamble och =
39    let ich = open_in !G.preamble in
40    let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
41    try aux () with End_of_file -> close_in ich
42
43 let out_top_comment och msg =
44    KP.fprintf och "%% %s\n\n" msg
45
46 let out_comment och msg =
47    KP.fprintf och "%% %s\n" msg 
48
49 let out_clause och msg =
50    KP.fprintf och "%s\n\n" msg 
51
52 let out_uri och u =
53    let str = U.string_of_uri u in
54    let rec aux i =
55      let c = str.[i] in
56      if c = '.' then () else begin 
57         output_char och (if c = '/' then '_' else c);
58         aux (succ i)
59      end
60    in
61    let rec strip i n = 
62       if n <= 0 then succ i else
63       strip (String.index_from str (succ i) '/') (pred n)
64    in
65    aux (strip 0 3)
66
67 let out_name och a =
68    let f n = function 
69       | true  -> KP.fprintf och "%s" (alpha n)
70       | false -> KP.fprintf och "_"
71    in
72    let err () = f "" false in
73    E.name err f a
74
75 let rec out_term st e och = function
76    | B.Sort (_, h)                   ->
77       let sort = if h = 0 then "k+set" else if h = 1 then "k+prop" else assert false in
78       KP.fprintf och "(sort %s)" sort
79    | B.LRef (_, i)                   ->
80       let _, _, a, b = B.get e i in
81       KP.fprintf och "%a" out_name a
82    | B.GRef (_, s)                   ->
83       KP.fprintf och "%a" out_uri s
84    | B.Cast (_, u, t)                ->
85       KP.fprintf och "(cast %a %a)" (out_term st e) u (out_term st e) t 
86    | B.Appl (_, x, v, t)             ->
87       let c = if x then "appx" else "appr" in
88       KP.fprintf och "(%s %a %a)" c (out_term st e) v (out_term st e) t
89    | B.Bind (a, B.Abst (r, n, w), t) ->
90       let a = R.alpha B.mem e a in
91       let ee = B.push e B.empty a (B.abst r n w) in
92       let c = if r then "prod" else "abst" in
93       let l = match N.to_string st n with
94          | "1" -> "l+1"
95          | "2" -> "l+2"
96          | _   -> ok := false; "?"
97       in
98       KP.fprintf och "(%s %s %a %a\\ %a)"
99          c l (out_term st e) w out_name a (out_term st ee) t
100    | B.Bind (a, B.Abbr v, t)         ->
101       let a = R.alpha B.mem e a in
102       let ee = B.push e B.empty a (B.abbr v) in
103       KP.fprintf och "(abbr %a %a\\ %a)"
104           (out_term st e) v out_name a (out_term st ee) t
105    | B.Bind (a, B.Void, t)   ->
106       let a = R.alpha B.mem e a in
107       let ee = B.push e B.empty a (B.Void) in
108       KP.fprintf och "(void %a\\ %a)"
109           out_name a (out_term st ee) t
110
111 (* variant 1 *************************************************)
112
113 let output_entity_1 och st (_, na, s, b) =
114    if na.E.n_apix <= !G.last then begin
115    match b with
116       | E.Abbr t ->
117          KP.fprintf och "(gdef+1 c+%u %a\n %a\\\n" na.E.n_apix (out_term st B.empty) t out_uri s;
118          uris := (true, s) :: !uris; !ok
119       | E.Abst u ->
120          KP.fprintf och "(gdec+1 c+%u %a\n%a\\\n" na.E.n_apix (out_term st B.empty) u out_uri s;
121          uris := (false, s) :: !uris; !ok
122       | E.Void   -> C.err ()
123    end else !ok
124
125 let close_out_1 och () =
126    let aux_sep _ = KP.fprintf och "%s" ")" in
127    KP.fprintf och "%s" "gtop";   
128    List.iter aux_sep !uris;
129    out_clause och "\n\n).";
130    close_out och
131
132 (* Variant 2 *************************************************)
133
134 let output_entity_2 och st (_, na, s, b) =
135    if na.E.n_apix <= !G.last then begin
136    match b with
137       | E.Abbr t ->
138          KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
139             out_uri s na.E.n_apix (out_term st B.empty) t;
140          uris := (true, s) :: !uris; !ok
141       | E.Abst u ->
142          KP.fprintf och "g+line %a c+%u\n       %a\n.\n\n"
143             out_uri s na.E.n_apix (out_term st B.empty) u;
144          uris := (false, s) :: !uris; !ok
145       | E.Void   -> C.err ()
146    end else !ok
147
148 let close_out_2 och () =
149    let aux_name (b, s) =
150       let gde = if b then "gdef+2" else "gdec+2" in 
151       KP.fprintf och "(%s %a\n" gde out_uri s
152    in
153    let aux_sep _ = KP.fprintf och "%s" ")" in
154    if !G.first > 0 then begin
155       let s = KP.sprintf "tv+c C T :- $lt C c+%u, !." !G.first in
156       out_clause och s;
157       out_clause och "tv+c C T :- tv+ T."
158    end;
159    out_clause och "main :- grundlagen.";
160    out_clause och "grundlagen :- (gv+ ";
161    List.iter aux_name (List.rev !uris);
162    KP.fprintf och "%s" "gtop";
163    List.iter aux_sep !uris;
164    out_clause och "\n\n).";
165    close_out och
166
167 (* Interface functions ******************************************************)
168
169 let open_out_1 fname =
170    let dir = KF.concat !G.manager_dir base in 
171    let path = KF.concat dir fname in
172    let och = open_out (path ^ "1" ^ ext) in
173    out_preamble och;
174    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
175    out_clause och "main :- grundlagen.";
176    out_clause och "grundlagen :- gv+ (";
177    output_entity_1 och, close_out_1 och
178
179 let open_out_2 fname =
180    let dir = KF.concat !G.manager_dir base in 
181    let path = KF.concat dir fname in
182    let och = open_out (path ^ "2" ^ ext) in
183    out_preamble och;
184    out_top_comment och (KP.sprintf "This file was generated by %s: do not edit" G.version_string);
185    output_entity_2 och, close_out_2 och