]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/xml/xmlLibrary.ml
when sort inclusion is enabled, we can produce conversion constraints in xml
[helm.git] / helm / software / lambda-delta / src / xml / xmlLibrary.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 F = Filename
13 module U = NUri
14 module C = Cps
15 module H = Hierarchy
16 module G = Options
17 module E = Entity
18 module N = Level
19 module Q = Ccs
20
21 (* internal functions *******************************************************)
22
23 let base = "xml"
24
25 let ext = ".xml"
26
27 let obj_root = "ENTITY"
28
29 let ccs_name = "ccs.ldc"
30
31 let ccs_root = "CCS"
32
33 let system = "http://helm.cs.unibo.it/lambda-delta/" ^ base ^ "/ld.dtd"
34
35 let path_of_uri xdir uri =
36    let base = F.concat xdir base in 
37    F.concat base (Str.string_after (U.string_of_uri uri) 3)
38
39 (* interface functions ******************************************************)
40
41 type och = string -> unit
42
43 type attr = string * string
44
45 type pp = och -> int -> unit
46
47 let attribute out (name, contents) =
48    if contents <> "" then begin
49       out " "; out name; out "=\""; out contents; out "\""
50    end
51
52 let xml out version encoding =
53    out "<?xml";
54       attribute out ("version", version);
55       attribute out ("encoding", encoding);
56    out "?>\n\n"
57
58 let doctype out root system =
59    out "<!DOCTYPE "; out root; out " SYSTEM \""; out system; out "\">\n\n"
60
61 let tag tag attrs ?contents out indent =
62    let spc = String.make indent ' ' in
63    out spc; out "<"; out tag; List.iter (attribute out) attrs;
64    match contents with
65       | None      -> out "/>\n"
66       | Some cont -> 
67          out ">\n"; cont out (indent + 3); out spc; 
68          out "</"; out tag; out ">\n"
69
70 let sort = "Sort"
71
72 let lref = "LRef"
73
74 let gref = "GRef"
75
76 let cast = "Cast"
77
78 let appl = "Appl"
79
80 let proj = "Proj"
81
82 let abst = "Abst"
83
84 let abbr = "Abbr"
85
86 let void = "Void"
87
88 let position i =
89    "position", string_of_int i
90
91 let offset j = 
92    let contents = if j > 0 then string_of_int j else "" in
93    "offset", contents
94
95 let uri u =
96    "uri", U.string_of_uri u
97
98 let arity ?n l =
99    let n = match n with 
100       | None   -> List.length l
101       | Some n -> n   
102    in
103    let contents = if n > 1 then string_of_int n else "" in
104    "arity", contents
105
106 let name a =
107    let map f i n r s =
108       let n = if r then n else "-" ^ n in 
109       let spc = if i then "" else " " in
110       f (s ^ n ^ spc)
111    in
112    let f s = "name", s in
113    E.names f map a ""
114
115 let mark a =
116    let err () = "mark", "" in
117    let f i = "mark", string_of_int i in
118    E.mark err f a
119
120 let level n =
121    "level", N.to_string n
122
123 (* TODO: the string s must be quoted *)
124 let meta a =
125    let err () = "meta", "" in
126    let f s = "meta", s in
127    E.meta err f a
128
129 let export_entity pp_term (a, u, b) = 
130    let path = path_of_uri !G.xdir u in
131    let _ = Sys.command (Printf.sprintf "mkdir -p %s" (F.dirname path)) in
132    let och = open_out (path ^ ext) in
133    let out = output_string och in
134    xml out "1.0" "UTF-8"; doctype out obj_root system;
135    let a = E.Name (U.name_of_uri u, true) :: a in
136    let attrs = [uri u; name a; mark a; meta a] in 
137    let contents = match b with
138       | E.Abst (n, w) -> tag "ABST" (level n :: attrs) ~contents:(pp_term w) 
139       | E.Abbr v      -> tag "ABBR" attrs ~contents:(pp_term v)
140       | E.Void        -> assert false
141    in
142    let opts = if !G.si then "si" else "" in
143    let shp = H.string_of_graph () in
144    let attrs = ["hierarchy", shp; "options", opts] in
145    tag obj_root attrs ~contents out 0;
146    close_out och
147
148 let marks = function
149    | [] -> "mark", ""
150    | l  -> "mark", String.concat " " (List.rev_map string_of_int l)
151
152 let export_csys s = 
153    let path = path_of_uri !G.xdir s.Q.uri in
154    let _ = Sys.command (Printf.sprintf "mkdir -p %s" path) in
155    let name = F.concat path (ccs_name ^ ext) in
156    let och = open_out name in
157    let out = output_string och in
158    xml out "1.0" "UTF-8"; doctype out ccs_root system;
159    let attrs = [uri s.Q.uri] in
160    let contents = tag "ToInfinity" [arity s.Q.is; marks s.Q.is] in
161    tag ccs_root attrs ~contents out 0;
162    close_out och