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