]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgGrafite.ml
jet a change in dependences
[helm.git] / helm / software / helena / src / basic_rg / brgGrafite.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 ok = ref true
28
29 let version = KP.sprintf "This file was generated by %s: do not edit" (G.version_string true)
30
31 let base = "matita"
32
33 let ext = ".ma"
34
35 let width = 70
36
37 let out_preamble och =
38    let ich = open_in !G.preamble in
39    let rec aux () = KP.fprintf och "%s\n" (input_line ich); aux () in
40    try aux () with End_of_file -> close_in ich
41
42 let out_top_comment och msg =
43    let padding = width - String.length msg in
44    let padding = if padding >= 0 then padding else 0 in
45    KP.fprintf och "(* %s %s*)\n\n" msg (String.make padding '*')
46
47 let out_comment och msg =
48    KP.fprintf och "(* %s *)\n" msg 
49
50 let out_include och src =
51    KP.fprintf och "include \"%s.ma\".\n\n" src
52
53 let out_uri och u =
54    let str = U.string_of_uri u in
55    let rec aux i =
56      let c = str.[i] in
57      if c = '.' then () else begin 
58         output_char och (if c = '/' then '_' else c);
59         aux (succ i)
60      end
61    in
62    let rec strip i n = 
63       if n <= 0 then succ i else
64       strip (String.index_from str (succ i) '/') (pred n)
65    in
66    aux (strip 0 3)
67
68 let out_name och a =
69    let f n = function 
70       | true  -> KP.fprintf och "%s" n
71       | false -> KP.fprintf och "_"
72    in
73    let err () = f "" false in
74    E.name err f a
75
76 let rec out_term st p e och = function
77    | B.Sort k                        ->
78       let sort = if k = 0 then "Type[0]" else if k = 1 then "Prop" else assert false in
79       KP.fprintf och "%s" sort
80    | B.LRef (_, i)                   ->
81       let _, _, _, y, b = B.get e i in
82       KP.fprintf och "%a" out_name y
83    | B.GRef (_, s)                   ->
84       KP.fprintf och "%a" out_uri s
85    | B.Cast (u, t)                   ->
86       KP.fprintf och "(%a : %a)" (out_term st false e) t (out_term st false e) u 
87    | B.Appl (_, v, t)                ->
88       let pt = match t with B.Appl _ -> false | _ -> true in
89       let op, cp = if p then "(", ")" else "", "" in
90       KP.fprintf och "%s%a %a%s" op (out_term st pt e) t (out_term st true e) v cp
91    | B.Bind (y, B.Abst (r, n, w), t) ->
92       let op, cp = if p then "(", ")" else "", "" in
93       let y = R.alpha B.mem e y in
94       let ee = B.push e B.empty E.empty_node y (B.abst r n w) in
95       let binder = match N.to_string st n, fst y.E.b_main with
96          | "1", 0 -> "Π"
97          | "1", 1 -> "∀"
98          | "2", _ -> "λ"
99          | _      -> ok := false; "?"
100       in
101       KP.fprintf och "%s%s%a:%a.%a%s"
102          op binder out_name y (out_term st false e) w (out_term st false ee) t cp
103    | B.Bind (y, B.Abbr v, t)         ->
104       let op, cp = if p then "(", ")" else "", "" in
105       let y = R.alpha B.mem e y in
106       let ee = B.push e B.empty E.empty_node y (B.abbr v) in
107       KP.fprintf och "%slet %a ≝ %a in %a%s"
108          op out_name y (out_term st false e) v (out_term st false ee) t cp
109    | B.Bind (a, B.Void, t)           -> C.err ()
110
111 let close_out och () = close_out och
112
113 let output_entity och st (_, na, u, b) =
114    out_comment och (KP.sprintf "constant %u" na.E.n_apix);
115    match b with
116       | E.Abbr v ->
117          KP.fprintf och "definition %a ≝ %a.\n\n%!" out_uri u (out_term st false B.empty) v; !ok
118       | E.Abst w ->
119          KP.fprintf och "axiom %a : %a.\n\n%!" out_uri u (out_term st false B.empty) w; !ok
120       | E.Void   -> C.err ()
121
122 (* Interface functions ******************************************************)
123
124 let open_out fname =
125    let dir = KF.concat !G.manager_dir base in 
126    let path = KF.concat dir fname in
127    let och = open_out (path ^ ext) in
128    out_preamble och;
129    out_top_comment och version;
130    out_include och "basics/pts";
131    output_entity och, close_out och
132
133 END