ocaml_printer.ml 18.7 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
(********************************************************************)
(*                                                                  *)
(*  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 Ident
open Pp
18
19
open Ity
open Term
20
21
open Expr
open Ty
22
23
24
open Theory
open Pmodule
open Stdlib
Mário Pereira's avatar
Mário Pereira committed
25
open Pdecl
Mario Pereira's avatar
Mario Pereira committed
26
open Printer
Mário Pereira's avatar
Mário Pereira committed
27

Mário Pereira's avatar
Mário Pereira committed
28
29
30
31
32
33
34
35
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;
36
  flat              : bool;
Mário Pereira's avatar
Mário Pereira committed
37
38
}

Mário Pereira's avatar
Mário Pereira committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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";]

54
55
56
57
58
  let is_ocaml_keyword =
    let h = Hstr.create 16 in
    List.iter (fun s -> Hstr.add h s ()) ocaml_keywords;
    Hstr.mem h

Mário Pereira's avatar
Mário Pereira committed
59
  let iprinter, aprinter =
Mário Pereira's avatar
Mário Pereira committed
60
    let isanitize = sanitizer char_to_alpha char_to_alnumus in
Mário Pereira's avatar
Mário Pereira committed
61
62
63
    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
64

Mário Pereira's avatar
Mário Pereira committed
65
  let forget_id vs = forget_id iprinter vs
Mário Pereira's avatar
Mário Pereira committed
66
  let _forget_ids = List.iter forget_id
Mário Pereira's avatar
Mário Pereira committed
67
68
69
  let forget_var (id, _, _) = forget_id id
  let forget_vars = List.iter forget_var

Mário Pereira's avatar
Mário Pereira committed
70
  let forget_let_defn = function
Mário Pereira's avatar
Mário Pereira committed
71
  | Lvar (v,_) -> forget_id v.pv_vs.vs_name
Mário Pereira's avatar
Mário Pereira committed
72
  | Lsym (s,_,_,_) -> forget_rs s
Mário Pereira's avatar
Mário Pereira committed
73
74
  | Lrec rdl -> List.iter (fun fd -> forget_rs fd.rec_sym) rdl

Mário Pereira's avatar
Mário Pereira committed
75
76
77
78
79
80
81
  let rec forget_pat = function
    | Pwild -> ()
    | Pident id -> forget_id id
    | Papp (_, pl) | Ptuple pl -> List.iter forget_pat pl
    | Por (p1, p2) -> forget_pat p1; forget_pat p2
    | Pas (p, _) -> forget_pat p

Mário Pereira's avatar
Mário Pereira committed
82
83
84
85
  let print_ident fmt id =
    let s = id_unique iprinter id in
    fprintf fmt "%s" s

86
87
88
89
90
  let is_local_id info id =
    Sid.mem id info.info_current_th.th_local ||
      Opt.fold (fun _ m -> Sid.mem id m.Pmodule.mod_local)
      false info.info_current_mo

91
  let print_qident ~sanitizer info fmt id =
Mário Pereira's avatar
Mário Pereira committed
92
    try
93
      if info.flat then raise Not_found;
Mário Pereira's avatar
Mário Pereira committed
94
      let lp, t, q =
95
96
97
98
99
        try Pmodule.restore_path id
        with Not_found -> Theory.restore_path id in
      let s = String.concat "__" q in
      let s = Ident.sanitizer char_to_alpha char_to_alnumus s in
      let s = sanitizer s in
100
101
102
      let s = if is_ocaml_keyword s then s ^ "_renamed" else s in (* FIXME *)
      if is_local_id info id then
        fprintf fmt "%s" s
Mário Pereira's avatar
Mário Pereira committed
103
      else begin
Mário Pereira's avatar
Mário Pereira committed
104
105
        let fname = if lp = [] then info.info_fname else None in
        let m = Strings.capitalize (module_name ?fname lp t) in
Mário Pereira's avatar
Mário Pereira committed
106
        fprintf fmt "%s.%s" m s end
107
108
109
110
111
112
    with Not_found ->
      let s = id_unique ~sanitizer iprinter id in
      fprintf fmt "%s" s

  let print_lident = print_qident ~sanitizer:Strings.uncapitalize
  let print_uident = print_qident ~sanitizer:Strings.capitalize
Mário Pereira's avatar
Mário Pereira committed
113

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

117
118
119
  let protect_on b s =
    if b then "(" ^^ s ^^ ")" else s

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

Mário Pereira's avatar
Mário Pereira committed
122
123
  let rec print_list2 sep sep_m print1 print2 fmt (l1, l2) =
    match l1, l2 with
Mário Pereira's avatar
Mário Pereira committed
124
125
    | [x1], [x2] ->
      print1 fmt x1; sep_m fmt (); print2 fmt x2
Mário Pereira's avatar
Mário Pereira committed
126
127
128
129
130
    | x1 :: r1, x2 :: r2 ->
      print1 fmt x1; sep_m fmt (); print2 fmt x2; sep fmt ();
      print_list2 sep sep_m print1 print2 fmt (r1, r2)
    | _ -> ()

Mário Pereira's avatar
Mário Pereira committed
131
132
  let print_rs info fmt rs =
    fprintf fmt "%a" (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
133

134
135
  (** Types *)

Mario Pereira's avatar
Mario Pereira committed
136
  let rec print_ty ?(paren=false) info fmt = function
137
    | Tvar tv ->
Mário Pereira's avatar
Mário Pereira committed
138
      print_tv fmt tv
Mário Pereira's avatar
Mário Pereira committed
139
    | Ttuple [] ->
140
      fprintf fmt "unit"
Mário Pereira's avatar
Mário Pereira committed
141
    | Ttuple tl ->
142
      fprintf fmt (protect_on paren "@[%a@]")
143
        (print_list star (print_ty ~paren:true info)) tl
Mário Pereira's avatar
Mário Pereira committed
144
145
146
    | Tapp (ts, [t1; t2]) when id_equal ts ts_func.ts_name ->
      fprintf fmt (protect_on paren "@[%a ->@ %a@]")
        (print_ty ~paren:true info) t1 (print_ty info) t2
Mário Pereira's avatar
Mário Pereira committed
147
    | Tapp (ts, tl) ->
Mário Pereira's avatar
Mário Pereira committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      match query_syntax info.info_syn ts with
      | Some s ->
        syntax_arguments s (print_ty ~paren:true info) fmt tl
      | None   ->
        match tl with
        | [] ->
          (print_lident info) fmt ts
        | [ty] ->
          fprintf fmt (protect_on paren "%a@ %a")
            (print_ty ~paren:true info) ty (print_lident info) ts
        | tl ->
          fprintf fmt (protect_on paren "(%a)@ %a")
            (print_list comma (print_ty ~paren:false info)) tl
            (print_lident info) ts
Mario Pereira's avatar
Mario Pereira committed
162
163
164

  let print_vsty info fmt (v, ty, _) =
    fprintf fmt "%a:@ %a" print_ident v (print_ty ~paren:false info) ty
165

Mário Pereira's avatar
Mário Pereira committed
166
167
  let print_tv_arg = print_tv
  let print_tv_args fmt = function
168
    | []   -> ()
Mário Pereira's avatar
Mário Pereira committed
169
170
    | [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
171

Mario Pereira's avatar
Mario Pereira committed
172
173
  let print_vs_arg info fmt vs =
    fprintf fmt "@[(%a)@]" (print_vsty info) vs
Mário Pereira's avatar
Mário Pereira committed
174

Mário Pereira's avatar
Mário Pereira committed
175
176
177
178
179
180
181
182
183
184
185
186
187
  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

Mário Pereira's avatar
Mário Pereira committed
188
189
190
191
192
193
194
195
196
  let get_record info rs =
    match Mid.find_opt rs.rs_name info.info_mo_known_map with
    | Some {pd_node = PDtype itdl} ->
      let itd = List.find (fun {itd_constructors=constr} ->
          List.exists (rs_equal rs) constr) itdl in
      List.filter (fun e -> not (rs_ghost e)) itd.itd_fields
    | _ -> []

  let rec print_pat info fmt = function
197
198
199
    | Pwild ->
       fprintf fmt "_"
    | Pident id ->
Mário Pereira's avatar
Mário Pereira committed
200
       print_ident fmt id
201
    | Pas (p, id) ->
Mário Pereira's avatar
Mário Pereira committed
202
       fprintf fmt "%a as %a" (print_pat info) p print_ident id
203
    | Por (p1, p2) ->
Mário Pereira's avatar
Mário Pereira committed
204
       fprintf fmt "%a | %a" (print_pat info) p1 (print_pat info) p2
205
    | Ptuple pl ->
Mário Pereira's avatar
Mário Pereira committed
206
207
208
209
210
211
      fprintf fmt "(%a)" (print_list comma (print_pat info)) pl
    | Papp (ls, pl) ->
      match query_syntax info.info_syn ls.ls_name, pl with
      | Some s, _ ->
        syntax_arguments s (print_pat info) fmt pl
      | None, pl ->
Mário Pereira's avatar
Mário Pereira committed
212
213
214
215
        let pjl = let rs = restore_rs ls in get_record info rs in
        match pjl with
        | []  -> print_papp info ls fmt pl
        | pjl ->
Mário Pereira's avatar
Mário Pereira committed
216
          fprintf fmt "@[<hov 2>{ %a }@]"
Mário Pereira's avatar
Mário Pereira committed
217
            (print_list2 semi equal (print_rs info) (print_pat info)) (pjl, pl)
Mário Pereira's avatar
Mário Pereira committed
218
219
220
221
222
223
224

  and print_papp info ls fmt = function
    | []  -> fprintf fmt "%a"      (print_uident info) ls.ls_name
    | [p] -> fprintf fmt "%a %a"   (print_uident info) ls.ls_name
                                   (print_pat info) p
    | pl  -> fprintf fmt "%a (%a)" (print_uident info) ls.ls_name
                                   (print_list comma (print_pat info)) pl
225

226
227
  (** Expressions *)

228
229
  let pv_name pv = pv.pv_vs.vs_name

Mario Pereira's avatar
Mario Pereira committed
230
231
  let ht_rs = Hrs.create 7 (* rec_rsym -> rec_sym *)

232
233
234
235
236
237
238
239
  let is_true e = match e.e_node with
    | Eapp (s, []) -> rs_equal s rs_true
    | _ -> false

  let is_false e = match e.e_node with
    | Eapp (s, []) -> rs_equal s rs_false
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
240
  let rec print_apply ?(paren=false) info rs fmt pvl =
241
    let isfield =
242
      match rs.rs_field with
Mário Pereira's avatar
Mário Pereira committed
243
      | None   -> false
244
245
246
247
248
249
250
      | Some _ -> true in
    let isconstructor () =
      match Mid.find_opt rs.rs_name info.info_mo_known_map with
      | Some {pd_node = PDtype its} ->
        let is_constructor its =
          List.exists (rs_equal rs) its.itd_constructors in
        List.exists is_constructor its
Mário Pereira's avatar
Mário Pereira committed
251
      | _ -> false in
Mário Pereira's avatar
Mário Pereira committed
252
253
    match query_syntax info.info_convert rs.rs_name,
          query_syntax info.info_syn rs.rs_name, pvl with
254
    | Some s, _, [{e_node = Econst _}] ->
Mário Pereira's avatar
Mário Pereira committed
255
256
      let print_constant fmt e = match e.e_node with
        | Econst c ->
257
258
259
          let s = BigInt.to_string (Number.compute_int c) in
          fprintf fmt "%s" s
        | _ -> assert false in
Mário Pereira's avatar
Mário Pereira committed
260
      syntax_arguments s print_constant fmt pvl
Mário Pereira's avatar
Mário Pereira committed
261
    | _, Some s, _ ->
Mário Pereira's avatar
Mário Pereira committed
262
      syntax_arguments s (print_expr ~paren:true info) fmt pvl;
Mário Pereira's avatar
Mário Pereira committed
263
264
265
    | _, _, tl when is_rs_tuple rs ->
      fprintf fmt "@[(%a)@]"
        (print_list comma (print_expr info)) tl
Mário Pereira's avatar
Mário Pereira committed
266
    | _,  _, [t1] when isfield ->
Mário Pereira's avatar
Mário Pereira committed
267
      fprintf fmt "%a.%a" (print_expr info) t1 print_ident rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
268
    | _, _, tl when isconstructor () ->
Mário Pereira's avatar
Mário Pereira committed
269
      let pjl = get_record info rs in
Mário Pereira's avatar
Mário Pereira committed
270
      begin match pjl, tl with
Mário Pereira's avatar
Mário Pereira committed
271
272
      | [], [] ->
        (print_uident info) fmt rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
273
      | [], [t] ->
Mário Pereira's avatar
Mário Pereira committed
274
275
        fprintf fmt "@[<hov 2>%a %a@]"
          (print_uident info) rs.rs_name (print_expr info) t
Mário Pereira's avatar
Mário Pereira committed
276
      | [], tl ->
Mário Pereira's avatar
Mário Pereira committed
277
278
        fprintf fmt "@[<hov 2>%a (%a)@]" (print_uident info) rs.rs_name
          (print_list comma (print_expr info)) tl
Mário Pereira's avatar
Mário Pereira committed
279
      | pjl, tl ->
Mário Pereira's avatar
Mário Pereira committed
280
        fprintf fmt "@[<hov 2>{ %a }@]"
Mário Pereira's avatar
Mário Pereira committed
281
          (print_list2 semi equal (print_rs info) (print_expr info)) (pjl, tl)
Mário Pereira's avatar
Mário Pereira committed
282
283
284
      end
    | _, _, [] ->
      (print_lident info) fmt rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
285
    | _, _, tl ->
Mário Pereira's avatar
Mário Pereira committed
286
287
      fprintf fmt (protect_on paren "@[<hov 2>%a %a@]")
        (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
288
        (print_list space (print_expr ~paren:true info)) tl
289

Mário Pereira's avatar
Mário Pereira committed
290
291
292
293
  and print_let_def info fmt = function
    | Lvar (pv, e) ->
      fprintf fmt "@[<hov 2>let %a =@ %a@]"
        (print_lident info) (pv_name pv) (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
294
295
    | Lsym (rs, res, args, ef) ->
      fprintf fmt "@[<hov 2>let %a@[%a@] : %a@ =@ @[%a@]@]"
Mário Pereira's avatar
Mário Pereira committed
296
        (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
297
298
        (print_list_pre space (print_vs_arg info)) args
        (print_ty info) res (print_expr info) ef;
Mário Pereira's avatar
Mário Pereira committed
299
300
301
      forget_vars args
    | Lrec (rdef) ->
      let print_one fst fmt = function
Mário Pereira's avatar
Mário Pereira committed
302
303
        | { rec_sym = rs1; rec_args = args; rec_exp = e; rec_res = res } ->
          fprintf fmt "@[<hov 2>%s %a@ %a :@ %a@ =@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
304
305
306
            (if fst then "let rec" else "and")
            (print_lident info) rs1.rs_name
            (print_list space (print_vs_arg info)) args
Mário Pereira's avatar
Mário Pereira committed
307
            (print_ty info) res (print_expr info) e;
308
          forget_vars args
Mário Pereira's avatar
Mário Pereira committed
309
310
311
312
313
      in
      List.iter (fun fd -> Hrs.replace ht_rs fd.rec_rsym fd.rec_sym) rdef;
      print_list_next newline print_one fmt rdef;
      List.iter (fun fd -> Hrs.remove ht_rs fd.rec_rsym) rdef

Mário Pereira's avatar
Mário Pereira committed
314
  and print_enode ?(paren=false) info fmt = function
315
    | Econst c ->
Mário Pereira's avatar
Mário Pereira committed
316
317
      let n = Number.compute_int c in
      fprintf fmt "(Z.of_string \"%s\")" (BigInt.to_string n)
318
    | Evar pvs ->
Mário Pereira's avatar
Mário Pereira committed
319
      (print_lident info) fmt (pv_name pvs)
Mário Pereira's avatar
Mário Pereira committed
320
    | Elet (let_def, e) ->
Mário Pereira's avatar
Mário Pereira committed
321
      fprintf fmt (protect_on paren "@[%a@] in@ @[%a@]")
Mário Pereira's avatar
Mário Pereira committed
322
        (print_let_def info) let_def (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
323
      forget_let_defn let_def
324
    | Eabsurd ->
Mário Pereira's avatar
Mário Pereira committed
325
      fprintf fmt (protect_on paren "assert false (* absurd *)")
Mário Pereira's avatar
Mário Pereira committed
326
    | Ehole -> ()
Mário Pereira's avatar
Mário Pereira committed
327
    | Eapp (rs, []) when rs_equal rs rs_true ->
Mário Pereira's avatar
Mário Pereira committed
328
      fprintf fmt "true"
Mário Pereira's avatar
Mário Pereira committed
329
    | Eapp (rs, []) when rs_equal rs rs_false ->
Mário Pereira's avatar
Mário Pereira committed
330
      fprintf fmt "false"
Mário Pereira's avatar
Mário Pereira committed
331
    | Eapp (rs, [e1; e2]) when rs_equal rs rs_func_app ->
332
      fprintf fmt (protect_on paren "@[<hov 1>%a %a@]")
Mário Pereira's avatar
Mário Pereira committed
333
        (print_expr info) e1 (print_expr ~paren:true info) e2
Mário Pereira's avatar
Mário Pereira committed
334
335
336
337
    | Eapp (rs, [])  ->
      (* avoids parenthesis around values *)
      fprintf fmt "%a" (print_apply info (Hrs.find_def ht_rs rs rs)) []
    | Eapp (rs, pvl) ->
Mário Pereira's avatar
Mário Pereira committed
338
339
340
341
342
343
344
345
346
      begin match query_syntax info.info_convert rs.rs_name, pvl with
        | Some s, [{e_node = Econst _}] ->
          let print_constant fmt e = begin match e.e_node with
            | Econst c ->
              let s = BigInt.to_string (Number.compute_int c) in
              fprintf fmt "%s" s
            | _ -> assert false end in
          syntax_arguments s print_constant fmt pvl
        | _ ->
Mário Pereira's avatar
Mário Pereira committed
347
348
      fprintf fmt (protect_on paren "%a")
        (print_apply info (Hrs.find_def ht_rs rs rs)) pvl end
349
    | Ematch (e, pl) ->
Mário Pereira's avatar
Mário Pereira committed
350
      fprintf fmt (protect_on paren "begin match @[%a@] with@\n@[%a@] end")
351
        (print_expr info) e (print_list newline (print_branch info)) pl
352
353
    | Eassign al ->
      let assign fmt (rho, rs, pv) =
Mário Pereira's avatar
Mário Pereira committed
354
        fprintf fmt "@[<hov 2>%a.%a <-@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
355
356
          print_ident (pv_name rho) print_ident rs.rs_name
          print_ident (pv_name pv) in
357
358
359
      begin match al with
      | [] -> assert false | [a] -> assign fmt a
      | al -> fprintf fmt "@[begin %a end@]" (print_list semi assign) al end
Mário Pereira's avatar
Mário Pereira committed
360
    | Eif (e1, e2, {e_node = Eblock []}) ->
Mário Pereira's avatar
Mário Pereira committed
361
362
      fprintf fmt (protect_on paren
        "@[<hv>@[<hov 2>if@ %a@]@ then begin@;<1 2>@[%a@] end@]")
Mário Pereira's avatar
Mário Pereira committed
363
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
364
365
    | Eif (e1, e2, e3) when is_false e2 && is_true e3 ->
      fprintf fmt (protect_on paren "not %a") (print_expr info ~paren:true) e1
366
    | Eif (e1, e2, e3) when is_true e2 ->
Mário Pereira's avatar
Mário Pereira committed
367
368
      fprintf fmt (protect_on paren "@[<hv>%a || %a@]")
        (print_expr info) e1 (print_expr info) e3
369
    | Eif (e1, e2, e3) when is_false e3 ->
Mário Pereira's avatar
Mário Pereira committed
370
371
      fprintf fmt (protect_on paren "@[<hv>%a && %a@]")
        (print_expr info) e1 (print_expr info) e2
372
    | Eif (e1, e2, e3) ->
Mário Pereira's avatar
Mário Pereira committed
373
374
      fprintf fmt (protect_on paren
        "@[<hv>@[<hov 2>if@ %a@]@ then@;<1 2>@[%a@]@;<1 0>else@;<1 2>@[%a@]@]")
375
        (print_expr info) e1 (print_expr info) e2 (print_expr info) e3
376
    | Eblock [] ->
377
378
379
380
      fprintf fmt "()"
    | Eblock [e] ->
      print_expr info fmt e
    | Eblock el ->
381
382
383
      fprintf fmt "@[<hv>begin@;<1 2>@[%a@]@ end@]"
        (print_list semi (print_expr info)) el
    | Efun (varl, e) ->
Mário Pereira's avatar
Mário Pereira committed
384
      fprintf fmt (protect_on paren "@[<hov 2>fun %a ->@ %a@]")
Mario Pereira's avatar
Mario Pereira committed
385
        (print_list space (print_vs_arg info)) varl (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
386
    | Ewhile (e1, e2) ->
387
      fprintf fmt "@[<hov 2>while %a do@\n%a@ done@]"
Mário Pereira's avatar
Mário Pereira committed
388
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
389
390
391
    | Eraise (xs, e_opt) ->
      print_raise ~paren info xs fmt e_opt
    (* | Etuple _ -> (\* TODO *\) assert false *)
Mário Pereira's avatar
Mário Pereira committed
392
393
394
395
396
397
    | Efor (pv1, pv2, direction, pv3, e) ->
      let print_for_direction fmt = function
        | To -> fprintf fmt "to"
        | DownTo -> fprintf fmt "downto"
      in
      fprintf fmt "@[<hov 2>for %a = %a %a %a do@ @[%a@]@ done@]"
Mário Pereira's avatar
Mário Pereira committed
398
        (print_lident info) (pv_name pv1) (print_lident info) (pv_name pv2)
Mário Pereira's avatar
Mário Pereira committed
399
400
        print_for_direction direction (print_lident info) (pv_name pv3)
        (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
401
402
403
404
    | Etry (e, bl) ->
      fprintf fmt
        "@[<hv>@[<hov 2>begin@ try@ %a@] with@]@\n@[<hov>%a@]@\nend"
        (print_expr info) e (print_list newline (print_xbranch info)) bl
Mário Pereira's avatar
Mário Pereira committed
405
    | Eignore e -> fprintf fmt "ignore (%a)" (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
406
407
408
    (* | Enot _ -> (\* TODO *\) assert false *)
    (* | Ebinop _ -> (\* TODO *\) assert false *)
    (* | Ecast _ -> (\* TODO *\) assert false *)
Mário Pereira's avatar
Mário Pereira committed
409

410
  and print_branch info fmt (p, e) =
Mário Pereira's avatar
Mário Pereira committed
411
412
    fprintf fmt "@[<hov 4>| %a ->@ @[%a@]@]"
      (print_pat info) p (print_expr info) e
413

Mário Pereira's avatar
Mário Pereira committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  and print_raise ~paren info xs fmt e_opt =
    match query_syntax info.info_syn xs.xs_name, e_opt with
    | Some s, None ->
      fprintf fmt "raise %s" s
    | Some s, Some e ->
      fprintf fmt (protect_on paren "raise (%a)")
        (syntax_arguments s (print_expr info)) [e]
    | None, None ->
      fprintf fmt (protect_on paren "raise %a")
        (print_uident info) xs.xs_name
    | None, Some e ->
      fprintf fmt (protect_on paren "raise (%a %a)")
        (print_uident info) xs.xs_name (print_expr ~paren:true info) e

Mário Pereira's avatar
Mário Pereira committed
428
429
430
  and print_xbranch info fmt (xs, pvl, e) =
    let print_var fmt pv =
      print_lident info fmt (pv_name pv) in
Mário Pereira's avatar
Mário Pereira committed
431
432
433
434
435
436
437
    match query_syntax info.info_syn xs.xs_name with
    | Some s ->
      fprintf fmt "@[<hov 4>| %a ->@ %a@]"
        (syntax_arguments s print_var) pvl (print_expr info ~paren:true) e
    | None   ->
      fprintf fmt "@[<hov 4>| %a %a ->@ %a@]" (print_uident info) (xs.xs_name)
        (print_list nothing print_var) pvl (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
438
439
440

  and print_expr ?(paren=false) info fmt e =
    print_enode ~paren info fmt e.e_node
Mário Pereira's avatar
Mário Pereira committed
441

Mário Pereira's avatar
Mário Pereira committed
442
  let print_type_decl info fst fmt its =
443
444
    let print_constr fmt (id, cs_args) =
      match cs_args with
Mário Pereira's avatar
Mário Pereira committed
445
446
447
      | [] -> fprintf fmt "@[<hov 4>| %a@]" (print_uident info) id
      | l -> fprintf fmt "@[<hov 4>| %a of %a@]" (print_uident info) id
               (print_list star (print_ty ~paren:false info)) l in
448
    let print_field fmt (is_mutable, id, ty) =
Mário Pereira's avatar
Mário Pereira committed
449
      fprintf fmt "%s%a: %a;" (if is_mutable then "mutable " else "")
Mário Pereira's avatar
Mário Pereira committed
450
        print_ident id (print_ty ~paren:false info) ty in
451
    let print_def fmt = function
452
453
454
455
456
457
458
459
460
      | 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) ->
Mario Pereira's avatar
Mario Pereira committed
461
        fprintf fmt " =@ %a" (print_ty ~paren:false info) ty
462
    in
463
    fprintf fmt "@[<hov 2>%s %a%a%a@]"
Mário Pereira's avatar
Mário Pereira committed
464
      (if fst then "type" else "and") print_tv_args its.its_args
Mário Pereira's avatar
Mário Pereira committed
465
      (print_lident info) its.its_name print_def its.its_def
466

467
  let print_decl info fmt = function
Mário Pereira's avatar
Mário Pereira committed
468
    | Dlet ldef ->
Mário Pereira's avatar
Mário Pereira committed
469
      print_let_def info fmt ldef;
Mário Pereira's avatar
Mário Pereira committed
470
      fprintf fmt "@\n"
471
    | Dtype dl ->
Mário Pereira's avatar
Mário Pereira committed
472
      print_list_next newline (print_type_decl info) fmt dl;
Mário Pereira's avatar
Mário Pereira committed
473
      fprintf fmt "@\n"
474
    | Dexn (xs, None) ->
Mário Pereira's avatar
Mário Pereira committed
475
476
       fprintf fmt "exception %a@\n" print_ident xs.xs_name
    | Dexn (xs, Some t)->
Mário Pereira's avatar
Mário Pereira committed
477
      fprintf fmt "@[<hov 2>exception %a of %a@]@\n"
Mário Pereira's avatar
Mário Pereira committed
478
        print_ident xs.xs_name (print_ty ~paren:true info) t
479
480
    | Dclone _ ->
      assert false (*TODO*)
Mário Pereira's avatar
Mário Pereira committed
481
482
483
484

  let print_decl info fmt decl =
    let decl_name = get_decl_name decl in
    let decide_print id =
Mário Pereira's avatar
Mário Pereira committed
485
486
487
      if query_syntax info.info_syn id = None then begin
        print_decl info fmt decl;
        fprintf fmt "@." end in
Mário Pereira's avatar
Mário Pereira committed
488
489
    List.iter decide_print decl_name

Mário Pereira's avatar
Mário Pereira committed
490
491
end

492
let print_decl pargs ?old ?fname ~flat ({mod_theory = th} as m) fmt d =
493
494
495
496
497
498
499
500
  ignore (old);
  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;
Mário Pereira's avatar
Mário Pereira committed
501
    info_fname        = Opt.map Compile.clean_name fname;
502
    flat              = flat;
503
  } in
Mário Pereira's avatar
Mário Pereira committed
504
  Print.print_decl info fmt d
505

506
let fg ?fname m =
Mário Pereira's avatar
Mário Pereira committed
507
508
509
  let mod_name = m.mod_theory.th_name.id_string in
  let path     = m.mod_theory.th_path in
  (module_name ?fname path mod_name) ^ ".ml"
510

511
let () = Pdriver.register_printer "ocaml"
Mário Pereira's avatar
Mário Pereira committed
512
  ~desc:"printer for OCaml code" fg print_decl
513

514
515
(*
 * Local Variables:
516
 * compile-command: "make -C ../.. -j3 bin/why3extract.opt"
517
518
 * End:
 *)