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