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