ocaml_printer.ml 20.1 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

59
  (* FIXME? use different printers for record fields, types, etc. *)
Mário Pereira's avatar
Mário Pereira committed
60
  let iprinter, aprinter =
Mário Pereira's avatar
Mário Pereira committed
61
    let isanitize = sanitizer char_to_alpha char_to_alnumus in
Mário Pereira's avatar
Mário Pereira committed
62
63
64
    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
65

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

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

Mário Pereira's avatar
Mário Pereira committed
76
77
78
79
80
81
82
  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
83
84
85
86
  let print_ident fmt id =
    let s = id_unique iprinter id in
    fprintf fmt "%s" s

87
88
  let is_local_id info id =
    Sid.mem id info.info_current_th.th_local ||
Mário Pereira's avatar
Mário Pereira committed
89
    Opt.fold (fun _ m -> Sid.mem id m.Pmodule.mod_local)
90
91
      false info.info_current_mo

92
  let print_qident ~sanitizer info fmt id =
Mário Pereira's avatar
Mário Pereira committed
93
    try
94
      if info.flat then raise Not_found;
Mário Pereira's avatar
Mário Pereira committed
95
      let lp, t, q =
96
97
98
99
100
        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
101
102
103
      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
104
      else begin
Mário Pereira's avatar
Mário Pereira committed
105
106
        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
107
        fprintf fmt "%s.%s" m s end
108
109
110
111
112
113
    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
114

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

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

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

Mário Pereira's avatar
Mário Pereira committed
123
124
  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
125
126
    | [x1], [x2] ->
      print1 fmt x1; sep_m fmt (); print2 fmt x2
Mário Pereira's avatar
Mário Pereira committed
127
128
129
130
131
    | 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
132
133
  let print_rs info fmt rs =
    fprintf fmt "%a" (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
134

135
136
  (** Types *)

Mario Pereira's avatar
Mario Pereira committed
137
  let rec print_ty ?(paren=false) info fmt = function
138
    | Tvar tv ->
Mário Pereira's avatar
Mário Pereira committed
139
      print_tv fmt tv
Mário Pereira's avatar
Mário Pereira committed
140
    | Ttuple [] ->
141
      fprintf fmt "unit"
Mário Pereira's avatar
Mário Pereira committed
142
    | Ttuple tl ->
143
      fprintf fmt (protect_on paren "@[%a@]")
144
        (print_list star (print_ty ~paren:true info)) tl
Mário Pereira's avatar
Mário Pereira committed
145
146
147
    | 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
148
    | Tapp (ts, tl) ->
Mário Pereira's avatar
Mário Pereira committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      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
163

Mário Pereira's avatar
Mário Pereira committed
164
  let print_vsty_opt info fmt id ty =
165
    fprintf fmt "?%s:(%a:@ %a)" id.id_string (print_lident info) id
Mário Pereira's avatar
Mário Pereira committed
166
      (print_ty ~paren:false info) ty
Mário Pereira's avatar
Mário Pereira committed
167

Mário Pereira's avatar
Mário Pereira committed
168
  let print_vsty_named info fmt id ty =
169
    fprintf fmt "~%s:(%a:@ %a)" id.id_string (print_lident info) id
Mário Pereira's avatar
Mário Pereira committed
170
      (print_ty ~paren:false info) ty
Mário Pereira's avatar
Mário Pereira committed
171

Mário Pereira's avatar
Mário Pereira committed
172
173
174
175
176
177
  let print_vsty info fmt (id, ty, _) =
    let labels = id.id_label in
    if is_optional ~labels then print_vsty_opt info fmt id ty
    else if is_named ~labels then print_vsty_named info fmt id ty
    else fprintf fmt "(%a:@ %a)" (print_lident info) id
        (print_ty ~paren:false info) ty
178

Mário Pereira's avatar
Mário Pereira committed
179
180
  let print_tv_arg = print_tv
  let print_tv_args fmt = function
181
    | []   -> ()
Mário Pereira's avatar
Mário Pereira committed
182
183
    | [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
184

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

Mário Pereira's avatar
Mário Pereira committed
188
189
190
191
192
193
194
195
196
197
198
199
200
  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
201
202
203
204
205
206
207
208
209
  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
210
211
212
    | Pwild ->
       fprintf fmt "_"
    | Pident id ->
Mário Pereira's avatar
Mário Pereira committed
213
       (print_lident info) fmt id
214
    | Pas (p, id) ->
Mário Pereira's avatar
Mário Pereira committed
215
       fprintf fmt "%a as %a" (print_pat info) p (print_lident info) id
216
    | Por (p1, p2) ->
Mário Pereira's avatar
Mário Pereira committed
217
       fprintf fmt "%a | %a" (print_pat info) p1 (print_pat info) p2
218
    | Ptuple pl ->
Mário Pereira's avatar
Mário Pereira committed
219
220
221
222
223
224
      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
225
226
227
228
        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
229
          fprintf fmt "@[<hov 2>{ %a }@]"
Mário Pereira's avatar
Mário Pereira committed
230
            (print_list2 semi equal (print_rs info) (print_pat info)) (pjl, pl)
Mário Pereira's avatar
Mário Pereira committed
231
232
233
234
235
236
237

  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
238

239
240
  (** Expressions *)

241
242
  let pv_name pv = pv.pv_vs.vs_name

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

245
246
247
248
249
250
251
252
  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
253
  let rec print_apply_args info fmt = function
254
255
256
257
258
259
260
261
262
263
    | expr :: exprl, pv :: pvl ->
      if is_optional ~labels:(pv_name pv).id_label then
        fprintf fmt "?%s:%a" (pv_name pv).id_string
          (print_expr ~paren:true info) expr
      else if is_named ~labels:(pv_name pv).id_label then
        fprintf fmt "~%s:%a" (pv_name pv).id_string
          (print_expr ~paren:true info) expr
      else
        fprintf fmt "%a" (print_expr ~paren:true info) expr;
      if exprl <> [] then fprintf fmt "@ ";
Mário Pereira's avatar
Mário Pereira committed
264
      print_apply_args info fmt (exprl, pvl)
265
266
    | [], _ -> ()
    | _, [] -> assert false
Mário Pereira's avatar
Mário Pereira committed
267
268

  and print_apply ?(paren=false) info rs fmt pvl =
269
    let isfield =
270
      match rs.rs_field with
Mário Pereira's avatar
Mário Pereira committed
271
      | None   -> false
272
273
274
275
276
277
278
      | 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
279
      | _ -> false in
Mário Pereira's avatar
Mário Pereira committed
280
281
    match query_syntax info.info_convert rs.rs_name,
          query_syntax info.info_syn rs.rs_name, pvl with
282
    | Some s, _, [{e_node = Econst _}] ->
Mário Pereira's avatar
Mário Pereira committed
283
284
      let print_constant fmt e = match e.e_node with
        | Econst c ->
285
286
287
          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
288
      syntax_arguments s print_constant fmt pvl
Mário Pereira's avatar
Mário Pereira committed
289
    | _, Some s, _ ->
Mário Pereira's avatar
Mário Pereira committed
290
      syntax_arguments s (print_expr ~paren:true info) fmt pvl;
291
    | _, None, tl when is_rs_tuple rs ->
Mário Pereira's avatar
Mário Pereira committed
292
293
      fprintf fmt "@[(%a)@]"
        (print_list comma (print_expr info)) tl
294
    | _, None, [t1] when isfield ->
Mário Pereira's avatar
Mário Pereira committed
295
      fprintf fmt "%a.%a" (print_expr info) t1 (print_lident info) rs.rs_name
296
    | _, None, tl when isconstructor () ->
Mário Pereira's avatar
Mário Pereira committed
297
      let pjl = get_record info rs in
Mário Pereira's avatar
Mário Pereira committed
298
      begin match pjl, tl with
Mário Pereira's avatar
Mário Pereira committed
299
300
      | [], [] ->
        (print_uident info) fmt rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
301
      | [], [t] ->
Mário Pereira's avatar
Mário Pereira committed
302
303
        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
304
      | [], tl ->
Mário Pereira's avatar
Mário Pereira committed
305
306
        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
307
      | pjl, tl ->
308
309
        let equal fmt () = fprintf fmt " = " in
        fprintf fmt "@[<hov 2>{ @[%a@] }@]"
Mário Pereira's avatar
Mário Pereira committed
310
          (print_list2 semi equal (print_rs info) (print_expr info)) (pjl, tl)
Mário Pereira's avatar
Mário Pereira committed
311
      end
312
    | _, None, [] ->
Mário Pereira's avatar
Mário Pereira committed
313
      (print_lident info) fmt rs.rs_name
314
    | _, None, tl ->
Mário Pereira's avatar
Mário Pereira committed
315
316
      fprintf fmt (protect_on paren "@[<hov 2>%a %a@]")
        (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
317
318
        (print_apply_args info) (tl, rs.rs_cty.cty_args)
        (* (print_list space (print_expr ~paren:true info)) tl *)
319

Mário Pereira's avatar
Mário Pereira committed
320
321
322
323
  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
324
325
    | Lsym (rs, res, args, ef) ->
      fprintf fmt "@[<hov 2>let %a@[%a@] : %a@ =@ @[%a@]@]"
Mário Pereira's avatar
Mário Pereira committed
326
        (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
327
328
        (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
329
330
331
      forget_vars args
    | Lrec (rdef) ->
      let print_one fst fmt = function
Mário Pereira's avatar
Mário Pereira committed
332
        | { rec_sym = rs1; rec_args = args; rec_exp = e; rec_res = res } ->
333
          fprintf fmt "@[<hov 2>%s %a @[%a@] :@ %a@ =@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
334
335
336
            (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
337
            (print_ty info) res (print_expr info) e;
338
          forget_vars args
Mário Pereira's avatar
Mário Pereira committed
339
340
341
342
343
      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
344
  and print_enode ?(paren=false) info fmt = function
345
    | Econst c ->
Mário Pereira's avatar
Mário Pereira committed
346
347
      let n = Number.compute_int c in
      fprintf fmt "(Z.of_string \"%s\")" (BigInt.to_string n)
348
    | Evar pvs ->
Mário Pereira's avatar
Mário Pereira committed
349
      (print_lident info) fmt (pv_name pvs)
Mário Pereira's avatar
Mário Pereira committed
350
    | Elet (let_def, e) ->
Mário Pereira's avatar
Mário Pereira committed
351
      fprintf fmt (protect_on paren "@[%a@] in@ @[%a@]")
Mário Pereira's avatar
Mário Pereira committed
352
        (print_let_def info) let_def (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
353
      forget_let_defn let_def
354
    | Eabsurd ->
Mário Pereira's avatar
Mário Pereira committed
355
      fprintf fmt (protect_on paren "assert false (* absurd *)")
Mário Pereira's avatar
Mário Pereira committed
356
    | Ehole -> ()
Mário Pereira's avatar
Mário Pereira committed
357
    | Eapp (rs, []) when rs_equal rs rs_true ->
Mário Pereira's avatar
Mário Pereira committed
358
      fprintf fmt "true"
Mário Pereira's avatar
Mário Pereira committed
359
    | Eapp (rs, []) when rs_equal rs rs_false ->
Mário Pereira's avatar
Mário Pereira committed
360
      fprintf fmt "false"
Mário Pereira's avatar
Mário Pereira committed
361
    | Eapp (rs, [e1; e2]) when rs_equal rs rs_func_app ->
362
      fprintf fmt (protect_on paren "@[<hov 1>%a %a@]")
Mário Pereira's avatar
Mário Pereira committed
363
        (print_expr info) e1 (print_expr ~paren:true info) e2
Mário Pereira's avatar
Mário Pereira committed
364
365
366
367
    | 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
368
369
370
371
372
373
374
375
376
      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
377
378
      fprintf fmt (protect_on paren "%a")
        (print_apply info (Hrs.find_def ht_rs rs rs)) pvl end
379
    | Ematch (e, pl) ->
380
      fprintf fmt (protect_on paren "begin match @[%a@] with@\n@[%a@]@\nend")
381
        (print_expr info) e (print_list newline (print_branch info)) pl
382
383
    | Eassign al ->
      let assign fmt (rho, rs, pv) =
Mário Pereira's avatar
Mário Pereira committed
384
        fprintf fmt "@[<hov 2>%a.%a <-@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
385
386
          (print_lident info) (pv_name rho) (print_lident info) rs.rs_name
          (print_lident info) (pv_name pv) in
387
388
389
      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
390
    | Eif (e1, e2, {e_node = Eblock []}) ->
Mário Pereira's avatar
Mário Pereira committed
391
392
      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
393
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
394
395
    | Eif (e1, e2, e3) when is_false e2 && is_true e3 ->
      fprintf fmt (protect_on paren "not %a") (print_expr info ~paren:true) e1
396
    | Eif (e1, e2, e3) when is_true e2 ->
Mário Pereira's avatar
Mário Pereira committed
397
398
      fprintf fmt (protect_on paren "@[<hv>%a || %a@]")
        (print_expr info) e1 (print_expr info) e3
399
    | Eif (e1, e2, e3) when is_false e3 ->
Mário Pereira's avatar
Mário Pereira committed
400
401
      fprintf fmt (protect_on paren "@[<hv>%a && %a@]")
        (print_expr info) e1 (print_expr info) e2
402
    | Eif (e1, e2, e3) ->
Mário Pereira's avatar
Mário Pereira committed
403
404
      fprintf fmt (protect_on paren
        "@[<hv>@[<hov 2>if@ %a@]@ then@;<1 2>@[%a@]@;<1 0>else@;<1 2>@[%a@]@]")
405
        (print_expr info) e1 (print_expr info) e2 (print_expr info) e3
406
    | Eblock [] ->
407
408
409
410
      fprintf fmt "()"
    | Eblock [e] ->
      print_expr info fmt e
    | Eblock el ->
411
412
413
      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
414
      fprintf fmt (protect_on paren "@[<hov 2>fun %a ->@ %a@]")
Mario Pereira's avatar
Mario Pereira committed
415
        (print_list space (print_vs_arg info)) varl (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
416
    | Ewhile (e1, e2) ->
417
      fprintf fmt "@[<hov 2>while %a do@\n%a@ done@]"
Mário Pereira's avatar
Mário Pereira committed
418
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
419
420
421
    | 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
422
423
424
425
426
427
    | 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
428
        (print_lident info) (pv_name pv1) (print_lident info) (pv_name pv2)
Mário Pereira's avatar
Mário Pereira committed
429
430
        print_for_direction direction (print_lident info) (pv_name pv3)
        (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
431
432
433
434
    | 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
435
    | Eignore e -> fprintf fmt "ignore (%a)" (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
436
437
438
    (* | Enot _ -> (\* TODO *\) assert false *)
    (* | Ebinop _ -> (\* TODO *\) assert false *)
    (* | Ecast _ -> (\* TODO *\) assert false *)
Mário Pereira's avatar
Mário Pereira committed
439

440
  and print_branch info fmt (p, e) =
441
    fprintf fmt "@[<hov 2>| %a ->@ @[%a@]@]"
Mário Pereira's avatar
Mário Pereira committed
442
443
      (print_pat info) p (print_expr info) e;
    forget_pat p
444

Mário Pereira's avatar
Mário Pereira committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  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
459
460
461
  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
462
463
464
465
466
467
468
    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
469
470
471

  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
472

Mário Pereira's avatar
Mário Pereira committed
473
  let print_type_decl info fst fmt its =
474
475
    let print_constr fmt (id, cs_args) =
      match cs_args with
Mário Pereira's avatar
Mário Pereira committed
476
477
478
      | [] -> 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
479
    let print_field fmt (is_mutable, id, ty) =
Mário Pereira's avatar
Mário Pereira committed
480
      fprintf fmt "%s%a: %a;" (if is_mutable then "mutable " else "")
Mário Pereira's avatar
Mário Pereira committed
481
        (print_lident info) id (print_ty ~paren:false info) ty in
482
    let print_def fmt = function
483
484
485
486
487
488
489
      | 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 "")
490
          (print_list newline print_field) fl
491
      | Some (Dalias ty) ->
Mario Pereira's avatar
Mario Pereira committed
492
        fprintf fmt " =@ %a" (print_ty ~paren:false info) ty
493
    in
494
    fprintf fmt "@[<hov 2>%s %a%a%a@]"
Mário Pereira's avatar
Mário Pereira committed
495
      (if fst then "type" else "and") print_tv_args its.its_args
Mário Pereira's avatar
Mário Pereira committed
496
      (print_lident info) its.its_name print_def its.its_def
497

498
  let print_decl info fmt = function
Mário Pereira's avatar
Mário Pereira committed
499
    | Dlet ldef ->
Mário Pereira's avatar
Mário Pereira committed
500
      print_let_def info fmt ldef;
Mário Pereira's avatar
Mário Pereira committed
501
      fprintf fmt "@\n"
502
    | Dtype dl ->
Mário Pereira's avatar
Mário Pereira committed
503
      print_list_next newline (print_type_decl info) fmt dl;
Mário Pereira's avatar
Mário Pereira committed
504
      fprintf fmt "@\n"
505
    | Dexn (xs, None) ->
Mário Pereira's avatar
Mário Pereira committed
506
       fprintf fmt "exception %a@\n" (print_uident info) xs.xs_name
Mário Pereira's avatar
Mário Pereira committed
507
    | Dexn (xs, Some t)->
Mário Pereira's avatar
Mário Pereira committed
508
      fprintf fmt "@[<hov 2>exception %a of %a@]@\n"
Mário Pereira's avatar
Mário Pereira committed
509
        (print_uident info) xs.xs_name (print_ty ~paren:true info) t
510
511
    | Dclone _ ->
      assert false (*TODO*)
Mário Pereira's avatar
Mário Pereira committed
512
513
514
515

  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
516
517
518
      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
519
520
    List.iter decide_print decl_name

Mário Pereira's avatar
Mário Pereira committed
521
522
end

523
let print_decl pargs ?old ?fname ~flat ({mod_theory = th} as m) fmt d =
524
525
526
527
528
529
530
531
  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
532
    info_fname        = Opt.map Compile.clean_name fname;
533
    flat              = flat;
534
  } in
Mário Pereira's avatar
Mário Pereira committed
535
  Print.print_decl info fmt d
536

537
let fg ?fname m =
Mário Pereira's avatar
Mário Pereira committed
538
539
540
  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"
541

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

545
546
(*
 * Local Variables:
547
 * compile-command: "make -C ../.. -j3 bin/why3extract.opt"
548
549
 * End:
 *)