compile.ml 34.4 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
(*  Copyright 2010-2018   --   Inria - CNRS - Paris-Sud University  *)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(*                                                                  *)
(*  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.                           *)
(*                                                                  *)
(********************************************************************)

(*
  - "use (im|ex)port" -> "open"
    but OCaml's open is not transitive, so requires some extra work
    to figure out all the opens

  - if a WhyML module M is extracted to something that is a signature,
    then extract "module type B_sig = ..." (as well as "module B = ...")
*)

(** Abstract syntax of ML *)

open Ident
open Ity
Mário Pereira's avatar
Mário Pereira committed
25
open Ty
Mário Pereira's avatar
Mário Pereira committed
26
open Term
27

Mário Pereira's avatar
Mário Pereira committed
28 29 30 31 32 33 34 35
let clean_name fname =
  (* TODO: replace with Filename.remove_extension
   * after migration to OCaml 4.04+ *)
  let remove_extension s =
    try Filename.chop_extension s with Invalid_argument _ -> s in
  let f = Filename.basename fname in (remove_extension f)

let module_name ?fname path t =
Mário Pereira's avatar
Mário Pereira committed
36
  let fname = match fname, path with
Mário Pereira's avatar
Mário Pereira committed
37 38 39 40 41
    | None, "why3"::_ -> "why3"
    | None, _   -> String.concat "__" path
    | Some f, _ -> clean_name f in
  fname ^ "__" ^ t

42
module ML = struct
43
  open Expr
44
  open Mltree
Mário Pereira's avatar
Mário Pereira committed
45

46
  let rec get_decl_name = function
Mário Pereira's avatar
Mário Pereira committed
47
    | Dtype itdefl ->
48
        let add_id = function (* add name of constructors and projections *)
Mário Pereira's avatar
Mário Pereira committed
49 50 51 52 53
          | Some (Ddata l)   -> List.map (fun (idc,    _) -> idc) l
          | Some (Drecord l) -> List.map (fun (_, idp, _) -> idp) l
          | _ -> [] in
        let add_td_ids {its_name = id; its_def = def} = id :: (add_id def) in
        List.flatten (List.map add_td_ids itdefl)
Mário Pereira's avatar
Mário Pereira committed
54
    | Dlet (Lrec rdef) -> List.map (fun {rec_sym = rs} -> rs.rs_name) rdef
55 56
    | Dlet (Lvar ({pv_vs={vs_name=id}}, _))
    | Dlet (Lsym ({rs_name=id}, _, _, _))
Mário Pereira's avatar
Mário Pereira committed
57
    | Dlet (Lany ({rs_name=id}, _, _))
58
    | Dexn ({xs_name=id}, _) -> [id]
59
    | Dmodule (_, dl) -> List.concat (List.map get_decl_name dl)
Mário Pereira's avatar
Mário Pereira committed
60

61
  let rec add_known_decl decl k_map id =
Mário Pereira's avatar
Mário Pereira committed
62
    match decl with
63
    | Dmodule (_, dl) ->
64 65 66 67
        let add_decl k_map d =
          let idl = get_decl_name d in
          List.fold_left (add_known_decl d) k_map idl in
        List.fold_left add_decl k_map dl
68
    | _ -> Mid.add id decl k_map
Mário Pereira's avatar
Mário Pereira committed
69

Mário Pereira's avatar
Mário Pereira committed
70 71
  let rec iter_deps_ty f = function
    | Tvar _ -> ()
Mário Pereira's avatar
Mário Pereira committed
72
    | Tapp (id, ty_l) -> f id; List.iter (iter_deps_ty f) ty_l
Mário Pereira's avatar
Mário Pereira committed
73 74 75
    | Ttuple ty_l -> List.iter (iter_deps_ty f) ty_l

  let iter_deps_typedef f = function
Mário Pereira's avatar
Mário Pereira committed
76
    | Ddata constrl ->
77
        List.iter (fun (_, tyl) -> List.iter (iter_deps_ty f) tyl) constrl
Mário Pereira's avatar
Mário Pereira committed
78
    | Drecord pjl -> List.iter (fun (_, _, ty) -> iter_deps_ty f ty) pjl
Mário Pereira's avatar
Mário Pereira committed
79
    | Dalias ty -> iter_deps_ty f ty
80
    | Drange _ | Dfloat _ -> ()
Mário Pereira's avatar
Mário Pereira committed
81 82 83 84

  let iter_deps_its_defn f its_d =
    Opt.iter (iter_deps_typedef f) its_d.its_def

Mário Pereira's avatar
Mário Pereira committed
85 86 87 88 89 90 91 92 93 94 95
  let iter_deps_args f =
    List.iter (fun (_, ty_arg, _) -> iter_deps_ty f ty_arg)

  let rec iter_deps_xbranch f (xs, _, e) =
    f xs.xs_name;
    iter_deps_expr f e

  and iter_deps_pat_list f patl =
    List.iter (iter_deps_pat f) patl

  and iter_deps_pat f = function
Raphael Rieu-Helft's avatar
Raphael Rieu-Helft committed
96
    | Pwild | Pvar _ -> ()
Mário Pereira's avatar
Mário Pereira committed
97
    | Papp (ls, patl) ->
98 99
        f ls.ls_name;
        iter_deps_pat_list f patl
Mário Pereira's avatar
Mário Pereira committed
100 101
    | Ptuple patl -> iter_deps_pat_list f patl
    | Por (p1, p2) ->
102 103
        iter_deps_pat f p1;
        iter_deps_pat f p2
Mário Pereira's avatar
Mário Pereira committed
104 105 106
    | Pas (p, _) -> iter_deps_pat f p

  and iter_deps_expr f e = match e.e_node with
Mário Pereira's avatar
Mário Pereira committed
107
    | Econst _ | Evar _ | Eabsurd | Ehole -> ()
Mário Pereira's avatar
Mário Pereira committed
108
    | Eapp (rs, exprl) ->
109
        f rs.rs_name; List.iter (iter_deps_expr f) exprl
Mário Pereira's avatar
Mário Pereira committed
110
    | Efun (args, e) ->
111 112
        List.iter (fun (_, ty_arg, _) -> iter_deps_ty f ty_arg) args;
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
113
    | Elet (Lvar (_, e1), e2) ->
114 115
        iter_deps_expr f e1;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
116
    | Elet (Lsym (_, ty_result, args, e1), e2) ->
117 118 119 120
        iter_deps_ty f ty_result;
        List.iter (fun (_, ty_arg, _) -> iter_deps_ty f ty_arg) args;
        iter_deps_expr f e1;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
121
    | Elet (Lany (_, ty_result, args), e2) ->
122 123 124
        iter_deps_ty f ty_result;
        List.iter (fun (_, ty_arg, _) -> iter_deps_ty f ty_arg) args;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
125
    | Elet ((Lrec rdef), e) ->
126 127 128 129 130
        List.iter
          (fun {rec_sym = rs; rec_args = args; rec_exp = e; rec_res = res} ->
             f rs.rs_name; iter_deps_args f args;
             iter_deps_expr f e; iter_deps_ty f res) rdef;
        iter_deps_expr f e
131
    | Ematch (e, branchl, xl) ->
132
        iter_deps_expr f e;
133 134
        List.iter (fun (p, e) -> iter_deps_pat f p; iter_deps_expr f e) branchl;
        List.iter (iter_deps_xbranch f) xl
Mário Pereira's avatar
Mário Pereira committed
135
    | Eif (e1, e2, e3) ->
136 137 138
        iter_deps_expr f e1;
        iter_deps_expr f e2;
        iter_deps_expr f e3
Mário Pereira's avatar
Mário Pereira committed
139
    | Eblock exprl ->
140
        List.iter (iter_deps_expr f) exprl
Mário Pereira's avatar
Mário Pereira committed
141
    | Ewhile (e1, e2) ->
142 143
        iter_deps_expr f e1;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
144
    | Efor (_, _, _, _, e) ->
145
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
146
    | Eraise (xs, None) ->
147
        f xs.xs_name
Mário Pereira's avatar
Mário Pereira committed
148
    | Eraise (xs, Some e) ->
149 150
        f xs.xs_name;
        iter_deps_expr f e
151
    | Eexn (_xs, None, e) -> (* FIXME? How come we never do binding here? *)
152
        iter_deps_expr f e
153
    | Eexn (_xs, Some ty, e) -> (* FIXME? How come we never do binding here? *)
154 155
        iter_deps_ty f ty;
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
156
    | Eassign assingl ->
157
        List.iter (fun (_, rs, _) -> f rs.rs_name) assingl
Mário Pereira's avatar
Mário Pereira committed
158
    | Eignore e -> iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
159

160
  let rec iter_deps f = function
Mário Pereira's avatar
Mário Pereira committed
161
    | Dtype its_dl ->
162
        List.iter (iter_deps_its_defn f) its_dl
Mário Pereira's avatar
Mário Pereira committed
163
    | Dlet (Lsym (_rs, ty_result, args, e)) ->
164 165 166
        iter_deps_ty f ty_result;
        iter_deps_args f args;
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
167
    | Dlet (Lany (_rs, ty_result, args)) ->
168 169
        iter_deps_ty f ty_result;
        iter_deps_args f args
Mário Pereira's avatar
Mário Pereira committed
170
    | Dlet (Lrec rdef) ->
171 172 173 174
        List.iter
          (fun {rec_sym = rs; rec_args = args; rec_exp = e; rec_res = res} ->
             f rs.rs_name; iter_deps_args f args;
             iter_deps_expr f e; iter_deps_ty f res) rdef
Mário Pereira's avatar
Mário Pereira committed
175 176 177
    | Dlet (Lvar (_, e)) -> iter_deps_expr f e
    | Dexn (_, None) -> ()
    | Dexn (_, Some ty) -> iter_deps_ty f ty
178
    | Dmodule (_, dl) -> List.iter (iter_deps f) dl
179

Mário Pereira's avatar
Mário Pereira committed
180
  let mk_expr e_node e_ity e_effect e_label =
Mário Pereira's avatar
Mário Pereira committed
181
    { e_node; e_ity; e_effect; e_label; }
Mário Pereira's avatar
Mário Pereira committed
182

Mário Pereira's avatar
Mário Pereira committed
183
  let tunit = Ttuple []
184

Mário Pereira's avatar
Mário Pereira committed
185 186 187 188 189
  let ity_unit = I Ity.ity_unit

  let is_unit = function
    | I i -> ity_equal i Ity.ity_unit
    | _ -> false
Mário Pereira's avatar
Mário Pereira committed
190

191 192
  let enope = Eblock []

Mário Pereira's avatar
Mário Pereira committed
193
  let mk_hole =
Mário Pereira's avatar
Mário Pereira committed
194
    mk_expr Ehole (I Ity.ity_unit) Ity.eff_empty Slab.empty
Mário Pereira's avatar
Mário Pereira committed
195

196 197
  let mk_var id ty ghost = (id, ty, ghost)

198 199
  let mk_var_unit () = id_register (id_fresh "_"), tunit, true

200
  let mk_its_defn id args private_ def =
Mário Pereira's avatar
Mário Pereira committed
201 202
    { its_name    = id      ; its_args = args;
      its_private = private_; its_def  = def; }
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  (* smart constructors *)
  let e_unit =
    mk_expr enope (I Ity.ity_unit) Ity.eff_empty Slab.empty

  let var_defn pv e =
    Lvar (pv, e)

  let sym_defn f ty_res args e =
    Lsym (f, ty_res, args, e)

  let e_let ld e = mk_expr (Elet (ld, e))

  let e_app rs pvl =
    mk_expr (Mltree.Eapp (rs, pvl))

  let e_fun args e = mk_expr (Efun (args, e))

  let e_ignore e =
Mário Pereira's avatar
Mário Pereira committed
222
    if is_unit e.e_ity then e
Mário Pereira's avatar
Mário Pereira committed
223
    else mk_expr (Eignore e) ity_unit e.e_effect e.e_label
Mário Pereira's avatar
Mário Pereira committed
224

225
  let e_if e1 e2 e3 =
Mário Pereira's avatar
Mário Pereira committed
226
    mk_expr (Mltree.Eif (e1, e2, e3)) e2.e_ity
227 228 229 230

  let e_while e1 e2 =
    mk_expr (Mltree.Ewhile (e1, e2)) ity_unit

231 232 233
  let e_for pv1 pv2 dir pv3 e1 =
    mk_expr (Mltree.Efor (pv1, pv2, dir, pv3, e1)) ity_unit

234 235
  let e_match e bl xl =
    mk_expr (Mltree.Ematch (e, bl, xl))
236

237
(*
238 239 240 241
  let e_match_exn e bl eff_bl lbl_match xl =
    let ity = match bl with (_, d) :: _ -> d.e_ity | [] -> assert false in
    let e = e_match e bl ity eff_bl lbl_match in
    mk_expr (Mltree.Etry (e, true, xl))
242
*)
243

244 245 246
  let e_assign al ity eff lbl =
    if al = [] then e_unit else mk_expr (Mltree.Eassign al) ity eff lbl

247 248 249 250 251 252 253 254 255 256 257
  let e_absurd =
    mk_expr Eabsurd

  let e_seq e1 e2 =
    let e = match e1.e_node, e2.e_node with
      | (Eblock [] | Ehole), e | e, (Eblock [] | Ehole) -> e
      | Eblock e1, Eblock e2 -> Eblock (e1 @ e2)
      | _, Eblock e2 -> Eblock (e1 :: e2)
      | Eblock e1, _ -> Eblock (e1 @ [e2])
      | _ -> Eblock [e1; e2] in
    mk_expr e
258

259 260 261 262 263
  let var_list_of_pv_list pvl =
    let mk_var pv =
      mk_expr (Mltree.Evar pv) (Mltree.I pv.pv_ity) eff_empty Slab.empty in
    List.map mk_var pvl

264
end
Mário Pereira's avatar
Mário Pereira committed
265 266 267 268 269

(** Translation from Mlw to ML *)

module Translate = struct

Mário Pereira's avatar
Mário Pereira committed
270 271 272
  open Expr
  open Pmodule
  open Pdecl
Mário Pereira's avatar
Mário Pereira committed
273

Mário Pereira's avatar
Mário Pereira committed
274 275 276
  (* useful predicates and transformations *)
  let pv_not_ghost e = not e.pv_ghost

Mário Pereira's avatar
Mário Pereira committed
277 278 279 280 281
  (* remove ghost components from tuple, using mask *)
  let visible_of_mask m sl = match m with
    | MaskGhost    -> assert false (* FIXME ? *)
    | MaskVisible  -> sl
    | MaskTuple ml ->
282 283 284
        let add_ity acc m ity = if mask_ghost m then acc else ity :: acc in
        if List.length sl < List.length ml then sl (* FIXME ? much likely... *)
        else List.fold_left2 add_ity [] ml sl
Mário Pereira's avatar
Mário Pereira committed
285

Mário Pereira's avatar
Mário Pereira committed
286 287 288 289
  (* types *)
  let rec type_ ty =
    match ty.ty_node with
    | Tyvar tvs ->
290
        Mltree.Tvar tvs
Mário Pereira's avatar
Mário Pereira committed
291
    | Tyapp (ts, tyl) when is_ts_tuple ts ->
292
        Mltree.Ttuple (List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
293
    | Tyapp (ts, tyl) ->
294
        Mltree.Tapp (ts.ts_name, List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
295 296 297 298

  let vsty vs =
    vs.vs_name, type_ vs.vs_ty

Mário Pereira's avatar
Mário Pereira committed
299 300 301
  let rec filter_ghost_params p def = function
    | [] -> []
    | pv :: l ->
302 303
        if p pv then def pv :: (filter_ghost_params p def l)
        else filter_ghost_params p def l
304

305
  let rec filter_out_ghost_rdef = function
Mário Pereira's avatar
Mário Pereira committed
306
    | [] -> []
307
    | { rec_sym = rs; rec_rsym = rrs } :: l when rs_ghost rs || rs_ghost rrs ->
308
        filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
309
    | rdef :: l ->
310
        rdef :: filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
311

Mário Pereira's avatar
Mário Pereira committed
312
  let rec pat m p = match p.pat_node with
313
    | Pwild ->
314
        Mltree.Pwild
315
    | Pvar vs when (restore_pv vs).pv_ghost ->
316
        Mltree.Pwild
317
    | Pvar vs ->
318
        Mltree.Pvar vs
319
    | Por (p1, p2) ->
320
        Mltree.Por (pat m p1, pat m p2)
321
    | Pas (p, vs) when (restore_pv vs).pv_ghost ->
322
        pat m p
323
    | Pas (p, vs) ->
324
        Mltree.Pas (pat m p, vs)
325
    | Papp (ls, pl) when is_fs_tuple ls ->
326 327 328 329 330
        let pl = visible_of_mask m pl in
        begin match pl with
          | [] -> Mltree.Pwild
          | [p] -> pat m p
          | _ -> Mltree.Ptuple (List.map (pat m) pl) end
331
    | Papp (ls, pl) ->
332 333 334 335 336
        let rs = restore_rs ls in
        let args = rs.rs_cty.cty_args in
        let mk acc pv pp = if not pv.pv_ghost then pat m pp :: acc else acc in
        let pat_pl = List.fold_left2 mk [] args pl in
        Mltree.Papp (ls, List.rev pat_pl)
337

Mário Pereira's avatar
Mário Pereira committed
338 339
  (** programs *)

340 341
  let pv_name pv = pv.pv_vs.vs_name

342
  (* individual types *)
Mário Pereira's avatar
Mário Pereira committed
343 344
  let mlty_of_ity mask t =
    let rec loop t = match t.ity_node with
345
      | _ when mask_equal mask MaskGhost -> ML.tunit
346
      | Ityvar tvs ->
347 348
          Mltree.Tvar tvs
      | Ityapp ({its_ts = ts}, itl, _) when is_ts_tuple ts ->
349
          let itl = visible_of_mask mask itl in
350 351 352 353 354 355
          Mltree.Ttuple (List.map loop itl)
      | Ityapp ({its_ts = ts}, itl, _) ->
          Mltree.Tapp (ts.ts_name, List.map loop itl)
      | Ityreg {reg_its = its; reg_args = args} ->
          let args = List.map loop args in
          Mltree.Tapp (its.its_ts.ts_name, args) in
Mário Pereira's avatar
Mário Pereira committed
356
    loop t
Mário Pereira's avatar
Mário Pereira committed
357

Mário Pereira's avatar
Mário Pereira committed
358
  let pvty pv =
Mário Pereira's avatar
Mário Pereira committed
359
    if pv.pv_ghost then ML.mk_var (pv_name pv) ML.tunit true
Mário Pereira's avatar
Mário Pereira committed
360
    else let (vs, vs_ty) = vsty pv.pv_vs in ML.mk_var vs vs_ty false
Mário Pereira's avatar
Mário Pereira committed
361

362 363 364
  let for_direction = function
    | To -> Mltree.To
    | DownTo -> Mltree.DownTo
365

Mário Pereira's avatar
Mário Pereira committed
366
  let isconstructor info rs =
367
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
368
    | Some {pd_node = PDtype its} ->
369 370 371
        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
372 373
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
374 375 376 377
  let is_singleton_imutable itd =
    let not_g e = not (rs_ghost e) in
    let pjl = itd.itd_fields in
    let mfields = itd.itd_its.its_mfields in
378
    let pv_equal_field rs = pv_equal (fd_of_rs rs) in
Mário Pereira's avatar
Mário Pereira committed
379 380 381 382
    let get_mutable rs = List.exists (pv_equal_field rs) mfields in
    match filter_ghost_params not_g get_mutable pjl with
    | [is_mutable] -> not is_mutable
    | _ -> false
Mário Pereira's avatar
Mário Pereira committed
383

384
  let get_record_itd info rs =
385
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
386
    | Some {pd_node = PDtype itdl} ->
387 388 389 390 391
        let f pjl_constr = List.exists (rs_equal rs) pjl_constr in
        let itd = match rs.rs_field with
          | Some _ -> List.find (fun itd -> f itd.itd_fields) itdl
          | None -> List.find (fun itd -> f itd.itd_constructors) itdl in
        if itd.itd_fields = [] then None else Some itd
392 393 394 395 396 397 398 399 400 401 402 403 404 405
    | _ -> None

  let is_optimizable_record_itd itd =
    not itd.itd_its.its_private && is_singleton_imutable itd

  let is_optimizable_record_rs info rs =
    Opt.fold (fun _ -> is_optimizable_record_itd) false (get_record_itd info rs)

  let is_empty_record_itd itd =
    let is_ghost rs = rs_ghost rs in
    List.for_all is_ghost itd.itd_fields

  let is_empty_record info rs =
    Opt.fold (fun _ -> is_empty_record_itd) false (get_record_itd info rs)
Mário Pereira's avatar
Mário Pereira committed
406

Mário Pereira's avatar
Mário Pereira committed
407
  let mk_eta_expansion rs pvl ({cty_args = ca; cty_effect = ce} as c) =
Mário Pereira's avatar
Mário Pereira committed
408 409
    (* FIXME : effects and types of the expression in this situation *)
    let args_f =
Mário Pereira's avatar
Mário Pereira committed
410 411 412
      let def pv =
        (pv_name pv, mlty_of_ity (mask_of_pv pv) pv.pv_ity, pv.pv_ghost) in
      filter_ghost_params pv_not_ghost def ca in
Mário Pereira's avatar
Mário Pereira committed
413
    let args =
Mário Pereira's avatar
Mário Pereira committed
414 415
      let def pv =
        ML.mk_expr (Mltree.Evar pv) (Mltree.I pv.pv_ity) eff_empty Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
416
      let args = filter_ghost_params pv_not_ghost def pvl in
Mário Pereira's avatar
Mário Pereira committed
417 418 419
      let extra_args = List.map def ca in args @ extra_args in
    let eapp = ML.mk_expr (Mltree.Eapp (rs, args)) (Mltree.C c) ce Slab.empty in
    ML.mk_expr (Mltree.Efun (args_f, eapp)) (Mltree.C c) ce Slab.empty
Mário Pereira's avatar
Mário Pereira committed
420

421 422 423 424 425 426
  (* function arguments *)
  let filter_params args =
    let args = List.map pvty args in
    let p (_, _, is_ghost) = not is_ghost in
    List.filter p args

Mário Pereira's avatar
Mário Pereira committed
427 428 429
  let params = function
    | []   -> []
    | args -> let args = filter_params args in
430
        if args = [] then [ML.mk_var_unit ()] else args
431

Mário Pereira's avatar
Mário Pereira committed
432 433
  let filter_params_cty p def pvl cty_args =
    let rec loop = function
Mário Pereira's avatar
Mário Pereira committed
434
      | [], _ -> []
Mário Pereira's avatar
Mário Pereira committed
435
      | pv :: l1, arg :: l2 ->
Mário Pereira's avatar
Mário Pereira committed
436
          if p pv && p arg then def pv :: loop (l1, l2) else loop (l1, l2)
Mário Pereira's avatar
Mário Pereira committed
437 438 439 440
      | _ -> assert false
    in loop (pvl, cty_args)

  let app pvl cty_args =
Mário Pereira's avatar
Mário Pereira committed
441 442
    let def pv =
      ML.mk_expr (Mltree.Evar pv) (Mltree.I pv.pv_ity) eff_empty Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
443 444
    filter_params_cty pv_not_ghost def pvl cty_args

445 446
  (* build the set of type variables from functions arguments *)
  let rec add_tvar acc = function
447 448
    | Mltree.Tvar tv -> Stv.add tv acc
    | Mltree.Tapp (_, tyl) | Mltree.Ttuple tyl ->
449
        List.fold_left add_tvar acc tyl
450

Mário Pereira's avatar
Mário Pereira committed
451
  (* expressions *)
452
  let rec expr info svar mask ({e_effect = eff; e_label = lbl} as e) =
453
    assert (not (e_ghost e));
454
    assert (not (mask_spill e.e_mask mask));
Mário Pereira's avatar
Mário Pereira committed
455 456 457 458 459 460 461 462
    let pv_list_of_mask pvl mask =
      let mk_pv_of_mask acc pv = function MaskGhost -> acc | _ -> pv :: acc in
      match mask with
      | MaskGhost   -> []
      | MaskVisible -> pvl
      | MaskTuple m -> assert (List.length m = List.length pvl);
          let pvl = List.fold_left2 mk_pv_of_mask [] pvl m in
          List.rev pvl in
Mário Pereira's avatar
Mário Pereira committed
463
    match e.e_node with
464 465 466
    | Econst  _ | Evar _ | Eexec ({c_node = Cfun _}, _) | Eassign _
    | Ewhile  _ | Efor _ | Eraise _ | Eexn _ | Eabsurd when mask = MaskGhost ->
        ML.e_unit
467
    | Econst c ->
468 469
        let c = match c with Number.ConstInt c -> c | _ -> assert false in
        ML.mk_expr (Mltree.Econst c) (Mltree.I e.e_ity) eff lbl
470
    | Evar pv -> ML.mk_expr (Mltree.Evar pv) (Mltree.I e.e_ity) eff lbl
471
    | Elet (LDvar (_, e1), e2) when e_ghost e1 -> expr info svar mask e2
472
    | Elet (LDvar (_, e1), e2) when e_ghost e2 ->
473
        (* sequences are transformed into [let o = e1 in e2] by A-normal form *)
474
        expr info svar e1.e_mask e1
Mário Pereira's avatar
Mário Pereira committed
475 476
    | Elet (LDvar (pv, e1), e2)
      when pv.pv_ghost || not (Mpv.mem pv e2.e_effect.eff_reads) ->
477 478 479
        if eff_pure e1.e_effect then expr info svar mask e2
        else let e1 = ML.e_ignore (expr info svar e1.e_mask e1) in
          ML.e_seq e1 (expr info svar mask e2) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
480
    | Elet (LDvar (pv, e1), e2) ->
481 482 483 484
        let ld = ML.var_defn pv (expr info svar e1.e_mask e1) in
        ML.e_let ld (expr info svar mask e2) (Mltree.I e.e_ity) eff lbl
    | Elet (LDsym (rs, _), ein) when rs_ghost rs ->
        expr info svar mask ein
485
    | Elet (LDsym (rs, {c_node = Cfun ef; c_cty = cty}), ein) ->
486 487
        let args = params cty.cty_args in
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
488 489
        let ld = ML.sym_defn rs res args (expr info svar ef.e_mask ef) in
        ML.e_let ld (expr info svar mask ein) (Mltree.I e.e_ity) eff lbl
490
    | Elet (LDsym (rs, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein)
491
      when isconstructor info rs_app -> (* partial application of constructor *)
492 493 494 495 496
        let eta_app = mk_eta_expansion rs_app pvl cty in
        let mk_func pv f = ity_func pv.pv_ity f in
        let func = List.fold_right mk_func cty.cty_args cty.cty_result in
        let res = mlty_of_ity cty.cty_mask func in
        let ld = ML.sym_defn rs res [] eta_app in
497
        ML.e_let ld (expr info svar mask ein) (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
498
    | Elet (LDsym (rsf, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein) ->
499 500 501 502 503 504
        (* partial application *)
        let pvl = app pvl rs_app.rs_cty.cty_args in
        let eff = cty.cty_effect in
        let eapp = ML.e_app rs_app pvl (Mltree.C cty) eff Slab.empty in
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
        let ld = ML.sym_defn rsf res (params cty.cty_args) eapp in
505
        ML.e_let ld (expr info svar mask ein) (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
506
    | Elet (LDrec rdefl, ein) ->
507 508
        let rdefl = filter_out_ghost_rdef rdefl in
        let def = function
509 510
          | { rec_sym = rs1; rec_rsym = rs2;
              rec_fun = {c_node = Cfun ef; c_cty = cty} } ->
511 512
              let res = mlty_of_ity rs1.rs_cty.cty_mask rs1.rs_cty.cty_result in
              let args = params cty.cty_args in
513
              let new_svar =
514 515 516
                let args' = List.map (fun (_, ty, _) -> ty) args in
                let svar  = List.fold_left add_tvar Stv.empty args' in
                add_tvar svar res in
517
              let new_svar = Stv.diff svar new_svar in
518
              let ef = expr info (Stv.union svar new_svar) ef.e_mask ef in
519
              { Mltree.rec_sym  = rs1;  Mltree.rec_rsym = rs2;
520 521
                Mltree.rec_args = args; Mltree.rec_exp  = ef;
                Mltree.rec_res  = res;  Mltree.rec_svar = new_svar; }
522 523 524
          | _ -> assert false in
        let rdefl = List.map def rdefl in
        if rdefl <> [] then
525 526
          let ein = expr info svar mask ein in
          let ml_letrec = Mltree.Elet (Mltree.Lrec rdefl, ein) in
527
          ML.mk_expr ml_letrec (Mltree.I e.e_ity) e.e_effect lbl
528 529 530 531 532
        else expr info svar mask ein
    | Eexec ({c_node = Capp (rs, [])}, _)  when is_rs_tuple rs ->
        ML.e_unit
    | Eexec ({c_node = Capp (rs, pvl)}, _) when is_rs_tuple rs ->
        let pvl = pv_list_of_mask pvl mask in
Mário Pereira's avatar
Mário Pereira committed
533
        let res_ity = ity_tuple (List.map (fun v -> v.pv_ity) pvl) in
534
        let pvl = ML.var_list_of_pv_list pvl in
Mário Pereira's avatar
Mário Pereira committed
535
        ML.e_app rs pvl (Mltree.I res_ity) eff lbl
536
    | Eexec ({c_node = Capp (rs, _)}, _)
Mário Pereira's avatar
Mário Pereira committed
537 538
      when is_empty_record info rs || rs_ghost rs ->
        ML.e_unit
Mário Pereira's avatar
Mário Pereira committed
539
    | Eexec ({c_node = Capp (rs, pvl); c_cty = cty}, _)
Mário Pereira's avatar
Mário Pereira committed
540
      when isconstructor info rs && cty.cty_args <> [] ->
541 542
        (* partial application of constructors *)
        mk_eta_expansion rs pvl cty
Mário Pereira's avatar
Mário Pereira committed
543
    | Eexec ({c_node = Capp (rs, pvl); _}, _) ->
544 545 546 547
        let pvl = app pvl rs.rs_cty.cty_args in
        begin match pvl with
          | [pv_expr] when is_optimizable_record_rs info rs -> pv_expr
          | _ -> ML.e_app rs pvl (Mltree.I e.e_ity) eff lbl end
Mário Pereira's avatar
Mário Pereira committed
548
    | Eexec ({c_node = Cfun e; c_cty = {cty_args = []}}, _) ->
549
        (* abstract block *)
550
        expr info svar e.e_mask e
551
    | Eexec ({c_node = Cfun e; c_cty = cty}, _) ->
552 553 554
        ML.e_fun (params cty.cty_args) (expr info svar e.e_mask e)
          (Mltree.I e.e_ity) eff lbl
    | Eexec ({c_node = Cany}, _) ->
555
        ML.mk_hole
556 557 558 559
    | Eabsurd ->
        ML.e_absurd (Mltree.I e.e_ity) eff lbl
    | Eassert _ ->
        ML.e_unit
560
    | Eif (e1, e2, e3) when e_ghost e1 ->
561 562
        (* if [e1] is ghost but the entire [if-then-else] expression doesn't,
           it must be the case one of the branches is [Eabsurd] *)
563 564
        if e2.e_node = Eabsurd then expr info svar mask e3
        else expr info svar mask e2
Mário Pereira's avatar
Mário Pereira committed
565
    | Eif (e1, e2, e3) when e_ghost e3 ->
566 567 568
        let e1 = expr info svar e1.e_mask e1 in
        let e2 = expr info svar mask e2 in
        ML.e_if e1 e2  ML.e_unit eff lbl
Mário Pereira's avatar
Mário Pereira committed
569
    | Eif (e1, e2, e3) when e_ghost e2 ->
570 571 572
        let e1 = expr info svar e1.e_mask e1 in
        let e3 = expr info svar mask e3 in
        ML.e_if e1 ML.e_unit e3 eff lbl
573
    | Eif (e1, e2, e3) ->
574 575 576
        let e1 = expr info svar e1.e_mask e1 in
        let e2 = expr info svar mask e2 in
        let e3 = expr info svar mask e3 in
577
        ML.e_if e1 e2 e3 eff lbl
Mário Pereira's avatar
Mário Pereira committed
578
    | Ewhile (e1, _, _, e2) ->
579 580 581 582
        assert (mask = MaskVisible);
        let e1 = expr info svar e1.e_mask e1 in
        let e2 = expr info svar e2.e_mask e2 in
        ML.e_while e1 e2 eff lbl
583
    | Efor (pv1, (pv2, dir, pv3), _, _, efor) ->
584
        assert (mask = MaskVisible);
585
        let dir = for_direction dir in
586
        let efor = expr info svar efor.e_mask efor in
587
        ML.e_for pv1 pv2 dir pv3 efor eff lbl
588 589
    | Eghost _ | Epure _ ->
        assert false
590
    | Eassign al ->
591 592
        let rm_ghost (_, rs, _) = not (rs_ghost rs) in
        let al = List.filter rm_ghost al in
593
        ML.e_assign al (Mltree.I e.e_ity) eff lbl
594 595 596 597 598 599 600 601 602 603 604
    | Ecase (e1, [], xl) when Mxs.is_empty xl ->
        expr info svar e1.e_mask e1
    | Ecase (e1, bl, xl) when e_ghost e1 ->
        assert (Mxs.is_empty xl); (* Expr ensures this for the time being *)
        (* if [e1] is ghost but the entire [match-with] expression isn't,
           it must be the case the first non-absurd branch is irrefutable *)
        (match bl with (* FIXME: skip absurd branches *)
         | [] -> assert false | (_, e) :: _ -> expr info svar e.e_mask e)
(*
    | Ecase (e1, bl) ->
        let e1 = expr info svar e1.e_mask e1 in
605
        let bl = List.map (ebranch info svar mask) bl in
606 607 608 609 610 611
        ML.e_match e1 bl (Mltree.I e.e_ity) eff lbl
*)
    | Ecase (e1, bl, xl) ->
        let e1 = expr info svar e1.e_mask e1 in
        let bl = List.map (ebranch info svar mask) bl in
        (* NOTE: why no pv_list_of_mask here? *)
612
        let mk_xl (xs, (pvl, e)) = xs, pvl, expr info svar mask e in
613 614 615
        let xl = List.map mk_xl (Mxs.bindings xl) in
        ML.e_match e1 bl xl (Mltree.I e.e_ity) eff lbl
(*
616
    | Etry (etry, _, xl) ->
617
        let etry = expr info svar mask etry in
Mário Pereira's avatar
Mário Pereira committed
618 619 620
        let mk_xl (xs, (pvl, e)) =
          let pvl = pv_list_of_mask pvl xs.xs_mask in
          xs, pvl, expr info svar mask e in
621 622 623
        let xl = Mxs.bindings xl in
        let xl = List.map mk_xl xl in
        ML.mk_expr (Mltree.Etry (etry, false, xl)) (Mltree.I e.e_ity) eff lbl
624
*)
625 626 627
    | Eraise (xs, ex) -> let ex = match expr info svar xs.xs_mask ex with
        | {Mltree.e_node = Mltree.Eblock []} -> None
        | e -> Some e in
628
        ML.mk_expr (Mltree.Eraise (xs, ex)) (Mltree.I e.e_ity) eff lbl
629
    | Eexn (xs, e1) ->
630
        if mask_ghost e1.e_mask then ML.mk_expr
631
          (Mltree.Eexn (xs, None, ML.e_unit)) (Mltree.I e.e_ity) eff lbl
632 633 634
        else let e1 = expr info svar xs.xs_mask e1 in
          let ty = if ity_equal xs.xs_ity ity_unit then None
            else Some (mlty_of_ity xs.xs_mask xs.xs_ity) in
635
        ML.mk_expr (Mltree.Eexn (xs, ty, e1)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
636 637
    | Elet (LDsym (_, {c_node=(Cany|Cpur (_, _)); _ }), _)
    | Eexec ({c_node=Cpur (_, _); _ }, _) -> ML.mk_hole
638

639
  and ebranch info svar mask ({pp_pat = p; pp_mask = m}, e) =
Mário Pereira's avatar
Mário Pereira committed
640 641 642
    (* if the [case] expression is not ghost but there is (at least) one ghost
       branch, then it must be the case that all the branches return [unit]
       and at least one of the non-ghost branches is effectful *)
643
    if e.e_effect.eff_ghost then (pat m p, ML.e_unit)
644
    else (pat m p, expr info svar mask e)
645

646
  (* type declarations/definitions *)
Mário Pereira's avatar
Mário Pereira committed
647
  let tdef itd =
648
    let s = itd.itd_its in
649
    let ddata_constructs = (* point-free *)
Mário Pereira's avatar
Mário Pereira committed
650
      List.map (fun ({rs_cty = cty} as rs) ->
651
          rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
652
          let args = List.filter pv_not_ghost cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
653
          List.map (fun {pv_vs = vs} -> type_ vs.vs_ty) args) in
Mário Pereira's avatar
Mário Pereira committed
654
    let drecord_fields ({rs_cty = cty} as rs) =
655
      (List.exists (pv_equal (fd_of_rs rs)) s.its_mfields),
Mário Pereira's avatar
Mário Pereira committed
656
      rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
657
      mlty_of_ity cty.cty_mask cty.cty_result in
658 659 660
    let id = s.its_ts.ts_name in
    let is_private = s.its_private in
    let args = s.its_ts.ts_args in
661
    begin match s.its_def, itd.itd_constructors, itd.itd_fields with
662
      | NoDef, [], [] ->
663
          ML.mk_its_defn id args is_private None
664
      | NoDef, cl, [] ->
665 666
          let cl = ddata_constructs cl in
          ML.mk_its_defn id args is_private (Some (Mltree.Ddata cl))
667
      | NoDef, _, pjl ->
668 669 670 671 672 673 674 675 676 677
          let p e = not (rs_ghost e) in
          let pjl = filter_ghost_params p drecord_fields pjl in
          begin match pjl with
            | [] -> ML.mk_its_defn id args is_private
                      (Some (Mltree.Dalias ML.tunit))
            | [_, _, ty_pj] when is_optimizable_record_itd itd ->
                ML.mk_its_defn id args is_private (Some (Mltree.Dalias ty_pj))
            | pjl ->
                ML.mk_its_defn id args is_private (Some (Mltree.Drecord pjl))
          end
678
      | Alias t, _, _ ->
679 680
          ML.mk_its_defn id args is_private (* FIXME ? is this a good mask ? *)
            (Some (Mltree.Dalias (mlty_of_ity MaskVisible t)))
681 682 683 684
      | Range r, [], [] ->
          assert (args = []); (* a range type is not polymorphic *)
          ML.mk_its_defn id [] is_private (Some (Mltree.Drange r))
      | Float ff, [], [] ->
Mário Pereira's avatar
Mário Pereira committed
685
          assert (args = []); (* a float type is not polymorphic *)
686 687 688
          ML.mk_its_defn id [] is_private (Some (Mltree.Dfloat ff))
      | (Range _ | Float _), _, _ ->
          assert false (* cannot have constructors or fields *)
689
    end
Mário Pereira's avatar
Mário Pereira committed
690

Mário Pereira's avatar
Mário Pereira committed
691 692 693 694
  let is_val = function
    | Eexec ({c_node = Cany}, _) -> true
    | _ -> false

695
  let pdecl info pd =
Mário Pereira's avatar
Mário Pereira committed
696
    match pd.pd_node with
697 698 699 700 701
    | PDlet (LDvar (_, e)) when e_ghost e ->
        []
    | PDlet (LDvar (pv, e)) when pv.pv_ghost ->
        if eff_pure e.e_effect then []
        else let unit_ = create_pvsymbol (id_fresh "()") ity_unit in
702
          [Mltree.Dlet (Mltree.Lvar (unit_, expr info Stv.empty e.e_mask e))]
703
    | PDlet (LDvar (pv, e)) ->
704
        [Mltree.Dlet (Mltree.Lvar (pv, expr info Stv.empty e.e_mask e))]
705
    | PDlet (LDsym (rs, _)) when rs_ghost rs ->
706
        []
Mário Pereira's avatar
Mário Pereira committed
707
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cany})) ->
708 709 710
        let args = params cty.cty_args in
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
        [Mltree.Dlet (Mltree.Lany (rs, res, args))]
711 712 713 714
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cfun e}))
      when is_val e.e_node -> (* zero argument functions *)
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
        [Mltree.Dlet (Mltree.Lany (rs, res, []))]
715
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cfun e})) ->
716 717
        let args = params cty.cty_args in
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
Mário Pereira's avatar
Mário Pereira committed
718 719 720 721
        let svar =
          let args' = List.map (fun (_, ty, _) -> ty) args in
          let svar  = List.fold_left add_tvar Stv.empty args' in
          add_tvar svar res in
722
        let e = expr info svar cty.cty_mask e in
723
        [Mltree.Dlet (Mltree.Lsym (rs, res, args, e))]
724
    | PDlet (LDrec rl) ->
725 726 727 728 729 730 731 732 733
        let rl = filter_out_ghost_rdef rl in
        let def {rec_fun = e; rec_sym = rs1; rec_rsym = rs2} =
          let e = match e.c_node with Cfun e -> e | _ -> assert false in
          let args = params rs1.rs_cty.cty_args in
          let res  = mlty_of_ity rs1.rs_cty.cty_mask rs1.rs_cty.cty_result in
          let svar =
            let args' = List.map (fun (_, ty, _) -> ty) args in
            let svar  = List.fold_left add_tvar Stv.empty args' in
            add_tvar svar res in
734
          let e = expr info svar rs1.rs_cty.cty_mask e in
735 736 737 738
          { Mltree.rec_sym  = rs1;  Mltree.rec_rsym = rs2;
            Mltree.rec_args = args; Mltree.rec_exp  = e;
            Mltree.rec_res  = res;  Mltree.rec_svar = svar; } in
        if rl = [] then [] else [Mltree.Dlet (Mltree.Lrec (List.map def rl))]
739
    | PDlet (LDsym _) | PDpure ->
740
        []
741
    | PDtype itl ->
742 743
        let itsd = List.map tdef itl in
        [Mltree.Dtype itsd]
744
    | PDexn xs ->
745 746
        if ity_equal xs.xs_ity ity_unit || xs.xs_mask = MaskGhost then
          [Mltree.Dexn (xs, None)]
747
        else [Mltree.Dexn (xs, Some (mlty_of_ity xs.xs_mask xs.xs_ity))]
Mário Pereira's avatar
Mário Pereira committed
748

Mário Pereira's avatar
Mário Pereira committed
749
  let pdecl_m m pd =
750
    let info = { Mltree.from_mod = Some m; Mltree.from_km = m.mod_known; } in
751
    pdecl info pd
752

Mário Pereira's avatar
Mário Pereira committed
753
  (* unit module declarations *)
754 755
  let rec mdecl info = function
    | Udecl pd -> pdecl info pd
Mário Pereira's avatar
Mário Pereira committed
756
    | Uscope (_, ([Uuse _] | [Uclone _])) -> []
757
    | Uscope (s, dl) -> let dl = List.concat (List.map (mdecl info) dl) in
758
        [Mltree.Dmodule (s, dl)]
Mário Pereira's avatar
Mário Pereira committed
759
    | Uuse _ | Uclone _ | Umeta _ -> []
760

Mário Pereira's avatar
Mário Pereira committed
761
  (* modules *)
Mário Pereira's avatar
Mário Pereira committed
762
  let module_ m =
763
    let from = { Mltree.from_mod = Some m; Mltree.from_km = m.mod_known; } in
764
    let mod_decl = List.concat (List.map (mdecl from) m.mod_units) in