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