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