]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgPTS.ml
helena: updated prolog exportation to ld3 and ALT-0/PTS
[helm.git] / helm / software / helena / src / basic_rg / brgPTS.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 IFDEF MANAGER THEN
24
25 (* Internal functions *******************************************************)
26
27 let version = KP.sprintf "This file was generated by %s: do not edit" (G.version_string true)
28
29 let ok = ref true
30
31 let uris = ref []
32
33 let chunk = ref 0
34
35 let sub_och = ref stdout
36
37 let top_age = 7000
38
39 let size = 250
40
41 let base = "lp"
42
43 let ext_lp = ".elpi"
44
45 let reserved = ["pi"; "sigma"; "nil"; "delay"; "in"; "with"; "resume"; "context"]
46
47 let alpha n =
48    if List.mem n reserved then !G.alpha ^ n else n
49
50 let out_preamble och =
51    let ich = open_in !G.preamble in
52    let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
53    try aux () with End_of_file -> close_in ich
54
55 let out_top_comment och msg =
56    KP.fprintf och "%% %s\n\n" msg
57
58 let out_comment och msg =
59    KP.fprintf och "%% %s\n" msg 
60
61 let out_clause och msg =
62    KP.fprintf och "%s\n\n" msg 
63
64 let out_uri och u =
65    let str = U.string_of_uri u in
66    let rec aux i =
67      let c = str.[i] in
68      if c = '.' then () else begin 
69         output_char och (if c = '/' then '_' else c);
70         aux (succ i)
71      end
72    in
73    let rec strip i n = 
74       if n <= 0 then succ i else
75       strip (String.index_from str (succ i) '/') (pred n)
76    in
77    aux (strip 0 3)
78
79 let out_name och a =
80    let f n = function 
81       | true  -> KP.fprintf och "%s" (alpha n)
82       | false -> KP.fprintf och "_"
83    in
84    let err () = f "" false in
85    E.name err f a
86
87 let rec out_term st e och = function
88    | B.Sort k                        ->
89       let sort = if k = 0 then "(s+type 0)" else if k = 1 then "(s+prop 0)" else assert false in
90       KP.fprintf och "(sort %s)" sort
91    | B.LRef (_, i)                   ->
92       let _, _, _, y, b = B.get e i in
93       KP.fprintf och "%a" out_name y
94    | B.GRef (_, s)                   ->
95       KP.fprintf och "%a" out_uri s
96    | B.Cast (u, t)                   ->
97       KP.fprintf och "(abbr %a %a x\\ x)" (out_term st e) t (out_term st e) u 
98    | B.Appl (_, v, t)                ->
99       KP.fprintf och "(appl %a %a)" (out_term st e) t (out_term st e) v
100    | B.Bind (y, B.Abst (r, n, w), t) ->
101       let y = R.alpha B.mem e y in
102       let ee = B.push e B.empty E.empty_node y (B.abst r n w) in
103       let c = match r, N.to_string st n with
104          | false, "1" -> "prod"
105          | false, "2" -> "abst"
106          | _          -> ok := false; "?"
107       in
108       KP.fprintf och "(%s %a %a\\ %a)"
109          c (out_term st e) w out_name y (out_term st ee) t
110    | B.Bind (_, B.Void, _)           -> C.err ()
111    | B.Bind (_, B.Abbr _, _)         -> C.err ()
112
113 (* PTS variant 2 ************************************************************)
114
115 let output_entity_pts2 och st (_, na, u, b) =
116    if na.E.n_apix < !G.first then begin
117    match b with
118       | E.Abbr (_, B.Cast (w, v)) ->
119          KP.fprintf och "get_expansion %a %u %a.\n"
120             out_uri u na.E.n_apix (out_term st B.empty) v;
121          KP.fprintf och "get_type %a %a.\n\n"
122             out_uri u (out_term st B.empty) w; !ok
123       | E.Abst (_, w)             ->
124          KP.fprintf och "get_type %a %a.\n\n"
125             out_uri u (out_term st B.empty) w; !ok
126       | _                         -> C.err ()
127    end else if na.E.n_apix <= !G.last then begin
128    match b with
129       | E.Abbr (_, B.Cast (w, v)) ->
130          KP.fprintf och "g+line+2 %a %u\n         %a\n         %a\n.\n\n"
131             out_uri u na.E.n_apix (out_term st B.empty) v (out_term st B.empty) w;
132          uris := (true, u) :: !uris; !ok
133       | E.Abst (_, w)             ->
134          KP.fprintf och "g+line+1 %a %u\n         %a\n.\n\n"
135             out_uri u na.E.n_apix (out_term st B.empty) w;
136          uris := (false, u) :: !uris; !ok
137       | _                         -> C.err ()
138    end else !ok
139
140 let close_out_pts2 och () =
141    let aux_name (b, s) =
142       let gde = if b then "gdef" else "gdec" in 
143       KP.fprintf och "(%s %a\n" gde out_uri s
144    in
145    let aux_sep _ = KP.fprintf och "%s" ")" in
146    out_clause och "grundlagen :- is+valid";
147    List.iter aux_name (List.rev !uris);
148    KP.fprintf och "%s" "gtop";
149    List.iter aux_sep !uris;
150    out_clause och "\n\n.";
151    close_out och
152
153 (* Interface functions ******************************************************)
154
155 let open_out_pts2 fname =
156    let dir = KF.concat !G.manager_dir base in 
157    let path = KF.concat dir fname in
158    let och = open_out (path ^ "b_pts" ^ ext_lp) in
159    out_preamble och;
160    out_top_comment och version;
161    output_entity_pts2 och, close_out_pts2 och
162
163 END