ocaml_printer.ml 9.37 KB
Newer Older
Mário Pereira's avatar
Mário Pereira committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2016   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)

(** Printer for extracted OCaml code *)

open Compile
open Format
open Pmodule
open Theory
open Ident
open Pp
20
21
22
open Ity
open Term
open Printer
23
24
open Expr
open Ty
25
26
27
28
29
30
31
32
33
34

type info = {
  info_syn          : syntax_map;
  info_convert      : syntax_map;
  info_current_th   : Theory.theory;
  info_current_mo   : Pmodule.pmodule option;
  info_th_known_map : Decl.known_map;
  info_mo_known_map : Pdecl.known_map;
  info_fname        : string option;
}
Mário Pereira's avatar
Mário Pereira committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

module Print = struct

  open ML

  let ocaml_keywords =
    ["and"; "as"; "assert"; "asr"; "begin";
     "class"; "constraint"; "do"; "done"; "downto"; "else"; "end";
     "exception"; "external"; "false"; "for"; "fun"; "function";
     "functor"; "if"; "in"; "include"; "inherit"; "initializer";
     "land"; "lazy"; "let"; "lor"; "lsl"; "lsr"; "lxor"; "match";
     "method"; "mod"; "module"; "mutable"; "new"; "object"; "of";
     "open"; "or"; "private"; "rec"; "sig"; "struct"; "then"; "to";
     "true"; "try"; "type"; "val"; "virtual"; "when"; "while"; "with";
     "raise";]

Mário Pereira's avatar
Mário Pereira committed
51
  let iprinter, aprinter =
Mário Pereira's avatar
Mário Pereira committed
52
    let isanitize = sanitizer char_to_alpha char_to_alnumus in
Mário Pereira's avatar
Mário Pereira committed
53
54
55
    let lsanitize = sanitizer char_to_lalpha char_to_alnumus in
    create_ident_printer ocaml_keywords ~sanitizer:isanitize,
    create_ident_printer ocaml_keywords ~sanitizer:lsanitize
Mário Pereira's avatar
Mário Pereira committed
56
57
58
59
60

  let print_ident fmt id =
    let s = id_unique iprinter id in
    fprintf fmt "%s" s

Mário Pereira's avatar
Mário Pereira committed
61
62
63
64
  (* let print_lident = print_qident ~sanitizer:Strings.uncapitalize *)
  (* let print_uident = print_qident ~sanitizer:Strings.capitalize *)

  let print_tv fmt tv =
65
    fprintf fmt "'%s" (id_unique aprinter tv.tv_name)
Mário Pereira's avatar
Mário Pereira committed
66

67
68
69
  let protect_on b s =
    if b then "(" ^^ s ^^ ")" else s

Mário Pereira's avatar
Mário Pereira committed
70
71
  let star fmt () = fprintf fmt " *@ "

72
73
  (** Types *)

74
  let rec print_ty ?(paren=false) fmt = function
75
76
    | Tvar tv ->
       print_tv fmt tv
Mário Pereira's avatar
Mário Pereira committed
77
78
79
    | Ttuple [] ->
       fprintf fmt "unit"
    | Ttuple tl ->
80
81
       fprintf fmt (protect_on paren "@[%a@]")
               (print_list star (print_ty ~paren:false)) tl
Mário Pereira's avatar
Mário Pereira committed
82
83
84
    | Tapp (ts, []) ->
       print_ident fmt ts
    | Tapp (ts, [ty]) ->
85
86
       fprintf fmt (protect_on paren "%a@ %a")
               (print_ty ~paren:true) ty print_ident ts
Mário Pereira's avatar
Mário Pereira committed
87
    | Tapp (ts, tl) ->
88
89
       fprintf fmt (protect_on paren "(%a)@ %a")
               (print_list comma (print_ty ~paren:false)) tl
Mário Pereira's avatar
Mário Pereira committed
90
91
               print_ident ts

92
  let print_vsty fmt (v, ty, _) =
93
94
95
96
97
98
99
    fprintf fmt "%a:@ %a" print_ident v (print_ty ~paren:false) ty

  let print_tv_arg = print_tv
  let print_tv_args fmt = function
    | []   -> ()
    | [tv] -> fprintf fmt "%a@ " print_tv_arg tv
    | tvl  -> fprintf fmt "(%a)@ " (print_list comma print_tv_arg) tvl
Mário Pereira's avatar
Mário Pereira committed
100
101
102
103

  let print_vs_arg fmt vs =
    fprintf fmt "@[(%a)@]" print_vsty vs

Mário Pereira's avatar
Mário Pereira committed
104
105
106
107
108
109
110
111
112
113
114
115
116
  let print_path =
    print_list dot pp_print_string (* point-free *)

  let print_path_id fmt = function
    | [], id -> print_ident fmt id
    | p, id  -> fprintf fmt "%a.%a" print_path p print_ident id

  let print_theory_name fmt th =
    print_path_id fmt (th.th_path, th.th_name)

  let print_module_name fmt m =
    print_theory_name fmt m.mod_theory

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  let rec print_pat fmt = function
    | Pwild ->
       fprintf fmt "_"
    | Pident id ->
       print_ident fmt id
    | Pas (p, id) ->
       fprintf fmt "%a as %a" print_pat p print_ident id
    | Por (p1, p2) ->
       fprintf fmt "%a | %a" print_pat p1 print_pat p2
    | Ptuple pl ->
       fprintf fmt "(%a)" (print_list comma print_pat) pl
    | Papp (id, []) ->
       print_ident fmt id
    | Papp (id, [p]) ->
       fprintf fmt "%a %a" print_ident id print_pat p
    | Papp (id, pl) ->
       fprintf fmt "%a (%a)" print_ident id (print_list comma print_pat) pl
    | Precord fl ->
       let print_field fmt (id, p) =
         fprintf fmt "%a = %a" print_ident id print_pat p
       in
       fprintf fmt "{ %a }" (print_list semi print_field) fl

140
141
142
143
144
145
146
147
148
149
150
151
  let print_const fmt c =
    let n = Number.compute_int c in
    let m = BigInt.to_int n in
    fprintf fmt "%d" m

  (** Expressions *)

  let extract_op {id_string = s} =
    try Some (Strings.remove_prefix "infix " s) with Not_found ->
    try Some (Strings.remove_prefix "prefix " s) with Not_found ->
    None

152
153
  let pv_name pv = pv.pv_vs.vs_name

154
  let print_apply info fmt rs pvl =
155
    let isfield =
156
157
158
      match rs.rs_field with
      | None -> false
      | Some _ -> true
159
    in
160
    match extract_op rs.rs_name, pvl with
161
162
    | Some o, [t1; t2] ->
       fprintf fmt "@[<hov 1>%a %s %a@]"
163
         print_pv t1 o print_pv t2
164
    | _, [] ->
165
       print_ident fmt rs.rs_name
166
    | _, [t1] when isfield ->
167
       fprintf fmt "%a.%a" print_pv t1 print_ident rs.rs_name
168
169
    | _, tl ->
       fprintf fmt "@[<hov 2>%a %a@]"
170
         print_ident rs.rs_name (print_list space print_pv) tl
171

172
  let rec print_enode info fmt = function
173
174
    | Econst c ->
       fprintf fmt "%a" print_const c
175
176
177
    | Evar pvs ->
       print_ident fmt (pv_name pvs)
    | Elet (pv, e1, e2) ->
Mário Pereira's avatar
Mário Pereira committed
178
       fprintf fmt "@[<hov 2>let @[%a@] =@ @[%a@]@] in@ %a"
179
         print_ident (pv_name pv) (print_expr info) e1 (print_expr info) e2
180
181
    | Eabsurd ->
       fprintf fmt "assert false (* absurd *)"
182
183
184
185
186
187
    | Eapp (s, []) when rs_equal s rs_true ->
       fprintf fmt "true"
    | Eapp (s, []) when rs_equal s rs_false ->
       fprintf fmt "false"
    | Eapp (s, pvl) ->
       print_apply info fmt s pvl
188
189
    | Ematch (e, pl) ->
       fprintf fmt "@[begin match @[%a@] with@\n@[<hov>%a@] end@]"
190
         (print_expr info) e (print_list newline (print_branch info)) pl
191
192
    | Eblock [] ->
       fprintf fmt "()"
193
    | _ -> (* TODO *) assert false
Mário Pereira's avatar
Mário Pereira committed
194

195
196
  and print_branch info fmt (p, e) =
    fprintf fmt "@[<hov 4>| %a ->@ %a@]" print_pat p (print_expr info) e
197

198
199
  and print_expr info fmt e =
    print_enode info fmt e.e_node
Mário Pereira's avatar
Mário Pereira committed
200

201
  let print_type_decl fmt its =
202
203
    let print_constr fmt (id, cs_args) =
      match cs_args with
204
205
206
207
208
209
210
      | [] ->
         fprintf fmt "@[<hov 4>| %a@]"
                 print_ident id (* FIXME: first letter must be uppercase
                                       -> print_uident *)
      | l ->
         fprintf fmt "@[<hov 4>| %a of %a@]"
                 print_ident id (* FIXME: print_uident *)
211
                 (print_list star (print_ty ~paren:false)) l in
212
213
214
215
    let print_field fmt (is_mutable, id, ty) =
      fprintf fmt "%s%a: %a;"
              (if is_mutable then "mutable " else "")
              print_ident id
216
              (print_ty ~paren:false) ty in
217
    let print_def fmt = function
218
219
220
221
222
223
224
225
226
227
228
      | None ->
        ()
      | Some (Ddata csl) ->
        fprintf fmt " =@\n%a" (print_list newline print_constr) csl
      | Some (Drecord fl) ->
        fprintf fmt " = %s{@\n%a@\n}"
          (if its.its_private then "private " else "")
          (print_list newline print_field) fl
      | Some (Dalias ty) ->
        fprintf fmt " =@ %a" (print_ty ~paren:false) ty
    in
229
    fprintf fmt "@[<hov 2>%s %a%a%a@]"
230
231
232
      (if true then "type" else "and") (* FIXME: mutual recursive types *)
      print_tv_args its.its_args
      print_ident its.its_name  (* FIXME: first letter must be lowercase
233
                                   -> print_lident *)
234
      print_def its.its_def
235

236
  let print_decl info fmt = function
237
    | Dlet (isrec, [rs, pvl, e]) ->
Mário Pereira's avatar
Mário Pereira committed
238
       fprintf fmt "@[<hov 2>%s %a@ %a =@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
239
               (if isrec then "let rec" else "let")
240
241
               print_ident rs.rs_name
               (print_list space print_vs_arg) pvl
242
               (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
243
       fprintf fmt "@\n@\n"
244
245
246
    | Dtype dl ->
       print_list newline print_type_decl fmt dl;
       fprintf fmt "@\n@\n"
247
248
249
    | Dexn (xs, None) ->
       fprintf fmt "exception %a@\n@\n" print_ident xs.xs_name
    | Dexn (xs, Some t) ->
250
       fprintf fmt "@[<hov 2>exception %a of %a@]@\n@\n"
251
               print_ident xs.xs_name (print_ty ~paren:true) t
252
    | _ -> (* TODO *) assert false
Mário Pereira's avatar
Mário Pereira committed
253
254
255

end

256
257
258
259
260
261
262
263
264
265
266
267
268
let extract_module pargs ?old fmt ({mod_theory = th} as m) =
  ignore (pargs);
  ignore (old);
  ignore (m);
  let info = {
    info_syn          = pargs.Pdriver.syntax;
    info_convert      = pargs.Pdriver.converter;
    info_current_th   = th;
    info_current_mo   = Some m;
    info_th_known_map = th.th_known;
    info_mo_known_map = m.mod_known;
    info_fname        = None; (* TODO *)
  } in
269
  fprintf fmt "(*@\n%a@\n*)@\n@\n" print_module m;
Mário Pereira's avatar
Mário Pereira committed
270
271
272
  fprintf fmt
    "(* This file has been generated from Why3 module %a *)@\n@\n"
    Print.print_module_name m;
273
  let mdecls = Translate.module_ info m in
274
  print_list nothing (Print.print_decl info) fmt mdecls;
Mário Pereira's avatar
Mário Pereira committed
275
  fprintf fmt "@."
276

277
278
279
280
281
282
283
284
let fg ?fname m =
  let mod_name = m.Pmodule.mod_theory.Theory.th_name.id_string in
  match fname with
  | None   -> mod_name ^ ".ml"
  | Some f -> (Filename.remove_extension f) ^ "__" ^ mod_name ^ ".ml"

let () = Pdriver.register_printer "ocaml" ~desc:"printer for OCaml code" fg extract_module

285
286
(*
 * Local Variables:
287
 * compile-command: "make -C ../.. -j3 bin/why3extract.opt"
288
289
 * End:
 *)