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