ocaml_printer.ml 18.2 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_tvs () =
Mário Pereira's avatar
Mário Pereira committed
66
    forget_all iprinter
Mário Pereira's avatar
Mário Pereira committed
67
68

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

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

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

89
  let print_qident ~sanitizer info fmt id =
Mário Pereira's avatar
Mário Pereira committed
90
    try
Mário Pereira's avatar
Mário Pereira committed
91
      let lp, t, q =
92
93
94
95
96
97
98
99
100
101
        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
      let s = if is_ocaml_keyword s then s ^ "_renamed" else s in
      if 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
      then fprintf fmt "%s" s
Mário Pereira's avatar
Mário Pereira committed
102
103
      else begin
        if info.flat then raise Not_found;
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 info fmt tv =
    fprintf fmt "'%a" (print_lident info) 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
124
125
126
127
128
  let rec print_list2 sep sep_m print1 print2 fmt (l1, l2) =
    match l1, l2 with
    | 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
129
130
  let print_rs info fmt rs =
    fprintf fmt "%a" (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
131

132
133
  (** Types *)

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

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

Mário Pereira's avatar
Mário Pereira committed
164
165
  let print_tv_arg info = print_tv info
  let print_tv_args info fmt = function
166
    | []   -> ()
Mário Pereira's avatar
Mário Pereira committed
167
168
    | [tv] -> fprintf fmt "%a@ " (print_tv_arg info) tv
    | tvl  -> fprintf fmt "(%a)@ " (print_list comma (print_tv_arg info)) tvl
Mário Pereira's avatar
Mário Pereira committed
169

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

Mário Pereira's avatar
Mário Pereira committed
173
174
175
176
177
178
179
180
181
182
183
184
185
  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
186
187
188
189
190
191
192
193
194
  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
195
196
197
    | Pwild ->
       fprintf fmt "_"
    | Pident id ->
Mário Pereira's avatar
Mário Pereira committed
198
       print_ident fmt id
199
    | Pas (p, id) ->
Mário Pereira's avatar
Mário Pereira committed
200
       fprintf fmt "%a as %a" (print_pat info) p print_ident id
201
    | Por (p1, p2) ->
Mário Pereira's avatar
Mário Pereira committed
202
       fprintf fmt "%a | %a" (print_pat info) p1 (print_pat info) p2
203
    | Ptuple pl ->
Mário Pereira's avatar
Mário Pereira committed
204
205
206
207
208
209
      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
210
211
212
213
214
        let pjl = let rs = restore_rs ls in get_record info rs in
        match pjl with
        | []  -> print_papp info ls fmt pl
        | pjl ->
          fprintf fmt "@[<hov 2>{ %a}@]"
Mário Pereira's avatar
Mário Pereira committed
215
            (print_list2 semi equal (print_rs info) (print_pat info)) (pjl, pl)
Mário Pereira's avatar
Mário Pereira committed
216
217
218
219
220
221
222

  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
223

224
225
  (** Expressions *)

226
227
  let pv_name pv = pv.pv_vs.vs_name

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

230
231
232
233
234
235
236
237
238
  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

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

Mário Pereira's avatar
Mário Pereira committed
287
288
289
290
  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
291
292
    | Lsym (rs, res, args, ef) ->
      fprintf fmt "@[<hov 2>let %a@[%a@] : %a@ =@ @[%a@]@]"
Mário Pereira's avatar
Mário Pereira committed
293
        (print_lident info) rs.rs_name
Mário Pereira's avatar
Mário Pereira committed
294
295
        (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
296
297
298
      forget_vars args
    | Lrec (rdef) ->
      let print_one fst fmt = function
Mário Pereira's avatar
Mário Pereira committed
299
300
        | { 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
301
302
303
            (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
304
            (print_ty info) res (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
305
          forget_vars args;
Mário Pereira's avatar
Mário Pereira committed
306
          (* forget_tvs () *)
Mário Pereira's avatar
Mário Pereira committed
307
308
309
310
311
      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
312
  and print_enode ?(paren=false) info fmt = function
313
    | Econst c ->
Mário Pereira's avatar
Mário Pereira committed
314
315
      let n = Number.compute_int c in
      fprintf fmt "(Z.of_string \"%s\")" (BigInt.to_string n)
316
    | Evar pvs ->
Mário Pereira's avatar
Mário Pereira committed
317
      (print_lident info) fmt (pv_name pvs)
Mário Pereira's avatar
Mário Pereira committed
318
    | Elet (let_def, e) ->
Mário Pereira's avatar
Mário Pereira committed
319
      fprintf fmt (protect_on paren "@[%a@] in@ %a")
Mário Pereira's avatar
Mário Pereira committed
320
        (print_let_def info) let_def (print_expr info) e;
Mário Pereira's avatar
Mário Pereira committed
321
      forget_let_defn let_def
322
    | Eabsurd ->
Mário Pereira's avatar
Mário Pereira committed
323
      fprintf fmt (protect_on paren "assert false (* absurd *)")
Mário Pereira's avatar
Mário Pereira committed
324
    | Ehole -> ()
Mário Pereira's avatar
Mário Pereira committed
325
    | Eapp (rs, []) when rs_equal rs rs_true ->
Mário Pereira's avatar
Mário Pereira committed
326
      fprintf fmt "true"
Mário Pereira's avatar
Mário Pereira committed
327
    | Eapp (rs, []) when rs_equal rs rs_false ->
Mário Pereira's avatar
Mário Pereira committed
328
      fprintf fmt "false"
Mário Pereira's avatar
Mário Pereira committed
329
    | Eapp (rs, [e1; e2]) when rs_equal rs rs_func_app ->
330
      fprintf fmt (protect_on paren "@[<hov 1>%a %a@]")
Mário Pereira's avatar
Mário Pereira committed
331
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
332
333
334
335
    | 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
336
      eprintf "rs->%s@." rs.rs_name.id_string;
337
      fprintf fmt (protect_on paren "%a")
Mário Pereira's avatar
Mário Pereira committed
338
        (print_apply info (Hrs.find_def ht_rs rs rs)) pvl
339
    | Ematch (e, pl) ->
Mário Pereira's avatar
Mário Pereira committed
340
      fprintf fmt (protect_on paren
Mário Pereira's avatar
Mário Pereira committed
341
      "begin match @[%a@] with@\n@[%a@] end")
342
        (print_expr info) e (print_list newline (print_branch info)) pl
343
344
    | Eassign al ->
      let assign fmt (rho, rs, pv) =
Mário Pereira's avatar
Mário Pereira committed
345
        fprintf fmt "@[<hov 2>%a.%a <-@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
346
347
          print_ident (pv_name rho) print_ident rs.rs_name
          print_ident (pv_name pv) in
348
349
350
      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
351
    | Eif (e1, e2, {e_node = Eblock []}) ->
Mário Pereira's avatar
Mário Pereira committed
352
353
      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
354
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
355
356
    | Eif (e1, e2, e3) when is_false e2 && is_true e3 ->
      fprintf fmt (protect_on paren "not %a") (print_expr info ~paren:true) e1
357
    | Eif (e1, e2, e3) when is_true e2 ->
Mário Pereira's avatar
Mário Pereira committed
358
359
      fprintf fmt (protect_on paren "@[<hv>%a || %a@]")
        (print_expr info) e1 (print_expr info) e3
360
    | Eif (e1, e2, e3) when is_false e3 ->
Mário Pereira's avatar
Mário Pereira committed
361
362
      fprintf fmt (protect_on paren "@[<hv>%a && %a@]")
        (print_expr info) e1 (print_expr info) e2
363
    | Eif (e1, e2, e3) ->
Mário Pereira's avatar
Mário Pereira committed
364
365
      fprintf fmt (protect_on paren
        "@[<hv>@[<hov 2>if@ %a@]@ then@;<1 2>@[%a@]@;<1 0>else@;<1 2>@[%a@]@]")
366
        (print_expr info) e1 (print_expr info) e2 (print_expr info) e3
367
    | Eblock [] ->
368
369
370
371
      fprintf fmt "()"
    | Eblock [e] ->
      print_expr info fmt e
    | Eblock el ->
372
373
374
      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
375
      fprintf fmt (protect_on paren "@[<hov 2>(fun %a ->@ %a)@]")
Mario Pereira's avatar
Mario Pereira committed
376
        (print_list space (print_vs_arg info)) varl (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
377
    | Ewhile (e1, e2) ->
378
      fprintf fmt "@[<hov 2>while %a do@\n%a@ done@]"
Mário Pereira's avatar
Mário Pereira committed
379
        (print_expr info) e1 (print_expr info) e2
Mário Pereira's avatar
Mário Pereira committed
380
381
382
    | 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
383
384
385
386
387
388
    | 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
389
        (print_lident info) (pv_name pv1) (print_lident info) (pv_name pv2)
Mário Pereira's avatar
Mário Pereira committed
390
391
        print_for_direction direction (print_lident info) (pv_name pv3)
        (print_expr info) e
Mário Pereira's avatar
Mário Pereira committed
392
393
394
395
    | 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
396
397
398
    (* | Enot _ -> (\* TODO *\) assert false *)
    (* | Ebinop _ -> (\* TODO *\) assert false *)
    (* | Ecast _ -> (\* TODO *\) assert false *)
Mário Pereira's avatar
Mário Pereira committed
399

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

Mário Pereira's avatar
Mário Pereira committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  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
418
419
420
  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
421
422
423
424
425
426
427
    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
428
429
430

  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
431

Mário Pereira's avatar
Mário Pereira committed
432
  let print_type_decl info fst fmt its =
433
434
    let print_constr fmt (id, cs_args) =
      match cs_args with
Mário Pereira's avatar
Mário Pereira committed
435
436
437
      | [] -> 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
438
    let print_field fmt (is_mutable, id, ty) =
Mário Pereira's avatar
Mário Pereira committed
439
      fprintf fmt "%s%a: %a;" (if is_mutable then "mutable " else "")
Mário Pereira's avatar
Mário Pereira committed
440
        print_ident id (print_ty ~paren:false info) ty in
441
    let print_def fmt = function
442
443
444
445
446
447
448
449
450
      | 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
451
        fprintf fmt " =@ %a" (print_ty ~paren:false info) ty
452
    in
453
    fprintf fmt "@[<hov 2>%s %a%a%a@]"
Mário Pereira's avatar
Mário Pereira committed
454
      (if fst then "type" else "and") (print_tv_args info) its.its_args
Mário Pereira's avatar
Mário Pereira committed
455
      (print_lident info) its.its_name print_def its.its_def
456

457
  let print_decl info fmt = function
Mário Pereira's avatar
Mário Pereira committed
458
    | Dlet ldef ->
Mário Pereira's avatar
Mário Pereira committed
459
460
      print_let_def info fmt ldef;
      forget_tvs ();
Mário Pereira's avatar
Mário Pereira committed
461
      fprintf fmt "@\n"
462
    | Dtype dl ->
Mário Pereira's avatar
Mário Pereira committed
463
      print_list_next newline (print_type_decl info) fmt dl;
Mário Pereira's avatar
Mário Pereira committed
464
      fprintf fmt "@\n"
465
    | Dexn (xs, None) ->
Mário Pereira's avatar
Mário Pereira committed
466
467
       fprintf fmt "exception %a@\n" print_ident xs.xs_name
    | Dexn (xs, Some t)->
Mário Pereira's avatar
Mário Pereira committed
468
      fprintf fmt "@[<hov 2>exception %a of %a@]@\n"
Mário Pereira's avatar
Mário Pereira committed
469
470
471
472
473
        print_ident xs.xs_name (print_ty ~paren:true info) t

  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
474
475
476
      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
477
478
    List.iter decide_print decl_name

Mário Pereira's avatar
Mário Pereira committed
479
480
end

481
let print_decl pargs ?old ?fname ~flat ({mod_theory = th} as m) fmt d =
482
483
484
485
486
487
488
489
  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
490
    info_fname        = Opt.map Compile.clean_name fname;
491
    flat              = flat;
492
  } in
Mário Pereira's avatar
Mário Pereira committed
493
  Print.print_decl info fmt d
494

495
let fg ?fname m =
Mário Pereira's avatar
Mário Pereira committed
496
497
498
  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"
499

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

503
504
(*
 * Local Variables:
505
 * compile-command: "make -C ../.. -j3 bin/why3extract.opt"
506
507
 * End:
 *)