compile.ml 34.9 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Mário Pereira's avatar
Mário Pereira committed
4
(*  Copyright 2010-2017   --   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 48
    | Dtype itdefl -> List.map (fun {its_name = id} -> id) itdefl
    | Dlet (Lrec rdef) -> List.map (fun {rec_sym = rs} -> rs.rs_name) rdef
49 50
    | Dlet (Lvar ({pv_vs={vs_name=id}}, _))
    | Dlet (Lsym ({rs_name=id}, _, _, _))
Mário Pereira's avatar
Mário Pereira committed
51
    | Dlet (Lany ({rs_name=id}, _, _))
52
    | Dexn ({xs_name=id}, _) -> [id]
53
    | Dmodule (_, dl) -> List.concat (List.map get_decl_name dl)
Mário Pereira's avatar
Mário Pereira committed
54

55
  let rec add_known_decl decl k_map id =
Mário Pereira's avatar
Mário Pereira committed
56
    match decl with
57
    | Dmodule (_, dl) ->
58 59 60 61
        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
62
    | _ -> Mid.add id decl k_map
Mário Pereira's avatar
Mário Pereira committed
63

Mário Pereira's avatar
Mário Pereira committed
64 65
  let rec iter_deps_ty f = function
    | Tvar _ -> ()
Mário Pereira's avatar
Mário Pereira committed
66
    | Tapp (id, ty_l) -> f id; List.iter (iter_deps_ty f) ty_l
Mário Pereira's avatar
Mário Pereira committed
67 68 69
    | 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
70
    | Ddata constrl ->
71
        List.iter (fun (_, tyl) -> List.iter (iter_deps_ty f) tyl) constrl
Mário Pereira's avatar
Mário Pereira committed
72
    | Drecord pjl -> List.iter (fun (_, _, ty) -> iter_deps_ty f ty) pjl
Mário Pereira's avatar
Mário Pereira committed
73 74 75 76 77
    | Dalias ty -> iter_deps_ty f ty

  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
78 79 80 81 82 83 84 85 86 87 88 89 90
  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
    | Pwild | Pident _ -> ()
    | Papp (ls, patl) ->
91 92
        f ls.ls_name;
        iter_deps_pat_list f patl
Mário Pereira's avatar
Mário Pereira committed
93 94
    | Ptuple patl -> iter_deps_pat_list f patl
    | Por (p1, p2) ->
95 96
        iter_deps_pat f p1;
        iter_deps_pat f p2
Mário Pereira's avatar
Mário Pereira committed
97 98 99
    | 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
100
    | Econst _ | Evar _ | Eabsurd | Ehole -> ()
Mário Pereira's avatar
Mário Pereira committed
101
    | Eapp (rs, exprl) ->
102
        f rs.rs_name; List.iter (iter_deps_expr f) exprl
Mário Pereira's avatar
Mário Pereira committed
103
    | Efun (args, e) ->
104 105
        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
106
    | Elet (Lvar (_, e1), e2) ->
107 108
        iter_deps_expr f e1;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
109
    | Elet (Lsym (_, ty_result, args, e1), e2) ->
110 111 112 113
        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
114
    | Elet (Lany (_, ty_result, args), e2) ->
115 116 117
        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
118
    | Elet ((Lrec rdef), e) ->
119 120 121 122 123
        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
Mário Pereira's avatar
Mário Pereira committed
124
    | Ematch (e, branchl) ->
125 126
        iter_deps_expr f e;
        List.iter (fun (p, e) -> iter_deps_pat f p; iter_deps_expr f e) branchl
Mário Pereira's avatar
Mário Pereira committed
127
    | Eif (e1, e2, e3) ->
128 129 130
        iter_deps_expr f e1;
        iter_deps_expr f e2;
        iter_deps_expr f e3
Mário Pereira's avatar
Mário Pereira committed
131
    | Eblock exprl ->
132
        List.iter (iter_deps_expr f) exprl
Mário Pereira's avatar
Mário Pereira committed
133
    | Ewhile (e1, e2) ->
134 135
        iter_deps_expr f e1;
        iter_deps_expr f e2
Mário Pereira's avatar
Mário Pereira committed
136
    | Efor (_, _, _, _, e) ->
137
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
138
    | Eraise (xs, None) ->
139
        f xs.xs_name
Mário Pereira's avatar
Mário Pereira committed
140
    | Eraise (xs, Some e) ->
141 142
        f xs.xs_name;
        iter_deps_expr f e
143
    | Eexn (_xs, None, e) -> (* FIXME? How come we never do binding here? *)
144
        iter_deps_expr f e
145
    | Eexn (_xs, Some ty, e) -> (* FIXME? How come we never do binding here? *)
146 147
        iter_deps_ty f ty;
        iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
148
    | Etry (e, xbranchl) ->
149 150
        iter_deps_expr f e;
        List.iter (iter_deps_xbranch f) xbranchl
Mário Pereira's avatar
Mário Pereira committed
151
    | Eassign assingl ->
152
        List.iter (fun (_, rs, _) -> f rs.rs_name) assingl
Mário Pereira's avatar
Mário Pereira committed
153
    | Eignore e -> iter_deps_expr f e
Mário Pereira's avatar
Mário Pereira committed
154

155
  let rec iter_deps f = function
Mário Pereira's avatar
Mário Pereira committed
156
    | Dtype its_dl ->
157
        List.iter (iter_deps_its_defn f) its_dl
Mário Pereira's avatar
Mário Pereira committed
158
    | Dlet (Lsym (_rs, ty_result, args, e)) ->
159 160 161
        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
162
    | Dlet (Lany (_rs, ty_result, args)) ->
163 164
        iter_deps_ty f ty_result;
        iter_deps_args f args
Mário Pereira's avatar
Mário Pereira committed
165
    | Dlet (Lrec rdef) ->
166 167 168 169
        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
170 171 172
    | Dlet (Lvar (_, e)) -> iter_deps_expr f e
    | Dexn (_, None) -> ()
    | Dexn (_, Some ty) -> iter_deps_ty f ty
173
    | Dmodule (_, dl) -> List.iter (iter_deps f) dl
174

Mário Pereira's avatar
Mário Pereira committed
175 176
  let mk_expr e_node e_ity e_effect e_label =
    { e_node = e_node; e_ity = e_ity; e_effect = e_effect; e_label = e_label; }
Mário Pereira's avatar
Mário Pereira committed
177

Mário Pereira's avatar
Mário Pereira committed
178
  let tunit = Ttuple []
179

Mário Pereira's avatar
Mário Pereira committed
180 181 182 183 184
  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
185

186 187
  let enope = Eblock []

Mário Pereira's avatar
Mário Pereira committed
188
  let mk_hole =
Mário Pereira's avatar
Mário Pereira committed
189
    mk_expr Ehole (I Ity.ity_unit) Ity.eff_empty Slab.empty
Mário Pereira's avatar
Mário Pereira committed
190

191 192
  let mk_var id ty ghost = (id, ty, ghost)

193 194
  let mk_var_unit () = id_register (id_fresh "_"), tunit, true

195
  let mk_its_defn id args private_ def =
Mário Pereira's avatar
Mário Pereira committed
196 197
    { its_name    = id      ; its_args = args;
      its_private = private_; its_def  = def; }
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  (* 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
217
    if is_unit e.e_ity then e
Mário Pereira's avatar
Mário Pereira committed
218
    else mk_expr (Eignore e) ity_unit e.e_effect e.e_label
Mário Pereira's avatar
Mário Pereira committed
219

220
  let e_if e1 e2 e3 =
Mário Pereira's avatar
Mário Pereira committed
221
    mk_expr (Mltree.Eif (e1, e2, e3)) e2.e_ity
222 223 224 225

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

226 227 228
  let e_for pv1 pv2 dir pv3 e1 =
    mk_expr (Mltree.Efor (pv1, pv2, dir, pv3, e1)) ity_unit

229 230 231
  let e_match e bl =
    mk_expr (Mltree.Ematch (e, bl))

232 233 234 235 236
  let e_assign al ity eff lbl =
    let rm_ghost (_, rs, _) = not (rs_ghost rs) in
    let al = List.filter rm_ghost al in
    if al = [] then e_unit else mk_expr (Mltree.Eassign al) ity eff lbl

237 238 239 240 241 242 243 244 245 246 247
  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
248

249
end
Mário Pereira's avatar
Mário Pereira committed
250 251 252 253 254

(** Translation from Mlw to ML *)

module Translate = struct

Mário Pereira's avatar
Mário Pereira committed
255 256 257
  open Expr
  open Pmodule
  open Pdecl
Mário Pereira's avatar
Mário Pereira committed
258

Mário Pereira's avatar
Mário Pereira committed
259 260 261
  (* useful predicates and transformations *)
  let pv_not_ghost e = not e.pv_ghost

Mário Pereira's avatar
Mário Pereira committed
262 263
  (* remove ghost components from tuple, using mask *)
  let visible_of_mask m sl = match m with
Mário Pereira's avatar
Mário Pereira committed
264
    | MaskGhost    -> assert false (* FIXME ? *)
Mário Pereira's avatar
Mário Pereira committed
265 266
    | MaskVisible  -> sl
    | MaskTuple ml ->
267 268 269
        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
270

Mário Pereira's avatar
Mário Pereira committed
271 272 273 274
  (* types *)
  let rec type_ ty =
    match ty.ty_node with
    | Tyvar tvs ->
275
        Mltree.Tvar tvs
Mário Pereira's avatar
Mário Pereira committed
276
    | Tyapp (ts, tyl) when is_ts_tuple ts ->
277
        Mltree.Ttuple (List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
278
    | Tyapp (ts, tyl) ->
279
        Mltree.Tapp (ts.ts_name, List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
280 281 282 283

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

Mário Pereira's avatar
Mário Pereira committed
284 285 286
  let rec filter_ghost_params p def = function
    | [] -> []
    | pv :: l ->
287 288
        if p pv then def pv :: (filter_ghost_params p def l)
        else filter_ghost_params p def l
289

290
  let rec filter_out_ghost_rdef = function
Mário Pereira's avatar
Mário Pereira committed
291
    | [] -> []
292
    | { rec_sym = rs; rec_rsym = rrs } :: l when rs_ghost rs || rs_ghost rrs ->
293
        filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
294
    | rdef :: l ->
295
        rdef :: filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
296

Mário Pereira's avatar
Mário Pereira committed
297
  let rec pat m p = match p.pat_node with
298
    | Pwild ->
299
        Mltree.Pwild
300
    | Pvar vs when (restore_pv vs).pv_ghost ->
301
        Mltree.Pwild
302
    | Pvar vs ->
303
        Mltree.Pident vs.vs_name
304
    | Por (p1, p2) ->
305
        Mltree.Por (pat m p1, pat m p2)
306
    | Pas (p, vs) when (restore_pv vs).pv_ghost ->
307
        pat m p
308
    | Pas (p, vs) ->
309
        Mltree.Pas (pat m p, vs.vs_name)
310
    | Papp (ls, pl) when is_fs_tuple ls ->
311 312 313 314 315
        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
316
    | Papp (ls, pl) ->
317 318 319 320 321
        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)
322

Mário Pereira's avatar
Mário Pereira committed
323 324
  (** programs *)

325 326
  let pv_name pv = pv.pv_vs.vs_name

327
  (* individual types *)
Mário Pereira's avatar
Mário Pereira committed
328 329
  let mlty_of_ity mask t =
    let rec loop t = match t.ity_node with
330
      | _ when mask_equal mask MaskGhost -> ML.tunit
331
      | Ityvar tvs ->
332 333
          Mltree.Tvar tvs
      | Ityapp ({its_ts = ts}, itl, _) when is_ts_tuple ts ->
334
          let itl = visible_of_mask mask itl in
335 336 337 338 339 340
          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
341
    loop t
Mário Pereira's avatar
Mário Pereira committed
342

343 344
  let ty_int = mlty_of_ity MaskVisible ity_int

Mário Pereira's avatar
Mário Pereira committed
345
  let pvty pv =
Mário Pereira's avatar
Mário Pereira committed
346 347
    if pv.pv_ghost then ML.mk_var (pv_name pv) ML.tunit true
    else let (vs, vs_ty) = vsty pv.pv_vs in
348
      ML.mk_var vs vs_ty false
Mário Pereira's avatar
Mário Pereira committed
349

350 351 352
  let for_direction = function
    | To -> Mltree.To
    | DownTo -> Mltree.DownTo
353

Mário Pereira's avatar
Mário Pereira committed
354
  let isconstructor info rs =
355
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
356
    | Some {pd_node = PDtype its} ->
357 358 359
        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
360 361
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
362 363 364 365
  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
366
    let pv_equal_field rs = pv_equal (fd_of_rs rs) in
Mário Pereira's avatar
Mário Pereira committed
367 368 369 370
    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
371

372
  let get_record_itd info rs =
373
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
374
    | Some {pd_node = PDtype itdl} ->
375 376 377 378 379
        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
380 381 382 383 384 385 386 387 388 389 390 391 392 393
    | _ -> 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
394

Mário Pereira's avatar
Mário Pereira committed
395
  let mk_eta_expansion rs pvl ({cty_args = ca; cty_effect = ce} as c) =
Mário Pereira's avatar
Mário Pereira committed
396 397
    (* FIXME : effects and types of the expression in this situation *)
    let args_f =
Mário Pereira's avatar
Mário Pereira committed
398 399 400
      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
401
    let args =
Mário Pereira's avatar
Mário Pereira committed
402 403
      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
404
      let args = filter_ghost_params pv_not_ghost def pvl in
Mário Pereira's avatar
Mário Pereira committed
405 406 407
      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
408

409 410 411 412 413 414
  (* 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
415 416 417
  let params = function
    | []   -> []
    | args -> let args = filter_params args in
418
        if args = [] then [ML.mk_var_unit ()] else args
419

Mário Pereira's avatar
Mário Pereira committed
420 421
  let filter_params_cty p def pvl cty_args =
    let rec loop = function
Mário Pereira's avatar
Mário Pereira committed
422
      | [], _ -> []
Mário Pereira's avatar
Mário Pereira committed
423
      | pv :: l1, arg :: l2 ->
Mário Pereira's avatar
Mário Pereira committed
424
          if p pv && p arg then def pv :: loop (l1, l2) else loop (l1, l2)
Mário Pereira's avatar
Mário Pereira committed
425 426 427 428
      | _ -> assert false
    in loop (pvl, cty_args)

  let app pvl cty_args =
Mário Pereira's avatar
Mário Pereira committed
429 430
    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
431 432
    filter_params_cty pv_not_ghost def pvl cty_args

433 434
  (* build the set of type variables from functions arguments *)
  let rec add_tvar acc = function
435 436
    | Mltree.Tvar tv -> Stv.add tv acc
    | Mltree.Tapp (_, tyl) | Mltree.Ttuple tyl ->
437
        List.fold_left add_tvar acc tyl
438

439 440 441 442 443 444 445 446 447 448 449 450
  let exp_of_mask e = function
    | MaskGhost   -> e_void
    | MaskVisible -> e
    (* | MaskTuple l -> match e with *)
    (*   | Capp (rs, pvl) -> assert (is_rs_tuple rs); *)
    (*       let rec add_exp acc pv = begin function *)
    (*         | MaskGhost   -> acc *)
    (*         | MaskVisible -> pv *)
    (*         | MaskTuple l ->  *)
    (*       end *)
      | _ -> assert false

Mário Pereira's avatar
Mário Pereira committed
451
  (* expressions *)
452
  let rec expr info svar ({e_effect = eff; e_label = lbl} as e) =
453
    assert (not (e_ghost e));
Mário Pereira's avatar
Mário Pereira committed
454
    match e.e_node with
455
    | Econst c ->
456 457
        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
458
    | Evar pv -> ML.mk_expr (Mltree.Evar pv) (Mltree.I e.e_ity) eff lbl
459
    | Elet (LDvar (_, e1), e2) when e_ghost e1 -> expr info svar e2
460
    | Elet (LDvar (_, e1), e2) when e_ghost e2 ->
461 462 463
        (* sequences are transformed into [let o = e1 in e2] by A-normal form *)
        (* FIXME? this is only the case when [e1] is effectful ? *)
        assert (ity_equal ity_unit e1.e_ity);
464
        expr info svar e1
Mário Pereira's avatar
Mário Pereira committed
465 466
    | Elet (LDvar (pv, e1), e2)
      when pv.pv_ghost || not (Mpv.mem pv e2.e_effect.eff_reads) ->
467 468 469
        if eff_pure e1.e_effect then expr info svar e2
        else let e1 = ML.e_ignore (expr info svar e1) in
          ML.e_seq e1 (expr info svar e2) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
470
    | Elet (LDvar (pv, e1), e2) ->
471 472 473
        let ld = ML.var_defn pv (expr info svar e1) in
        ML.e_let ld (expr info svar e2) (Mltree.I e.e_ity) eff lbl
    | Elet (LDsym (rs, _), ein) when rs_ghost rs -> expr info svar ein
474
    | Elet (LDsym (rs, {c_node = Cfun ef; c_cty = cty}), ein) ->
475 476
        let args = params cty.cty_args in
        let res = mlty_of_ity cty.cty_mask cty.cty_result in
477 478
        let ld = ML.sym_defn rs res args (expr info svar ef) in
        ML.e_let ld (expr info svar ein) (Mltree.I e.e_ity) eff lbl
479
    | Elet (LDsym (rs, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein)
480
      when isconstructor info rs_app -> (* partial application of constructor *)
481 482 483 484 485
        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
486
        ML.e_let ld (expr info svar ein) (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
487
    | Elet (LDsym (rsf, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein) ->
488 489 490 491 492 493
        (* 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
494
        ML.e_let ld (expr info svar ein) (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
495
    | Elet (LDrec rdefl, ein) ->
496 497
        let rdefl = filter_out_ghost_rdef rdefl in
        let def = function
498 499
          | { rec_sym = rs1; rec_rsym = rs2;
              rec_fun = {c_node = Cfun ef; c_cty = cty} } ->
500 501
              let res = mlty_of_ity rs1.rs_cty.cty_mask rs1.rs_cty.cty_result in
              let args = params cty.cty_args in
502
              let new_svar =
503 504 505
                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
506 507
              let new_svar = Stv.diff svar new_svar in
              let ef = expr info (Stv.union svar new_svar) ef in
508
              { Mltree.rec_sym  = rs1;  Mltree.rec_rsym = rs2;
509 510
                Mltree.rec_args = args; Mltree.rec_exp  = ef;
                Mltree.rec_res  = res;  Mltree.rec_svar = new_svar; }
511 512 513
          | _ -> assert false in
        let rdefl = List.map def rdefl in
        if rdefl <> [] then
514
          let ml_letrec = Mltree.Elet (Mltree.Lrec rdefl, expr info svar ein) in
515
          ML.mk_expr ml_letrec (Mltree.I e.e_ity) e.e_effect lbl
516
        else expr info svar ein
517 518 519
    | Eexec ({c_node = Capp (rs, [])}, _) when is_rs_tuple rs -> ML.e_unit
    | Eexec ({c_node = Capp (rs, _)}, _)
      when is_empty_record info rs || rs_ghost rs -> ML.e_unit
Mário Pereira's avatar
Mário Pereira committed
520
    | Eexec ({c_node = Capp (rs, pvl); c_cty = cty}, _)
Mário Pereira's avatar
Mário Pereira committed
521
      when isconstructor info rs && cty.cty_args <> [] ->
522 523
        (* partial application of constructors *)
        mk_eta_expansion rs pvl cty
Mário Pereira's avatar
Mário Pereira committed
524
    | Eexec ({c_node = Capp (rs, pvl); _}, _) ->
525 526 527 528
        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
529
    | Eexec ({c_node = Cfun e; c_cty = {cty_args = []}}, _) ->
530
        (* abstract block *)
531
        expr info svar e
532
    | Eexec ({c_node = Cfun e; c_cty = cty}, _) ->
533 534
        ML.e_fun (params cty.cty_args) (expr info svar e)
                 (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
535
    | Eexec ({c_node = Cany}, _) -> (* raise ExtractionAny *)
536
        ML.mk_hole
537
    | Eabsurd -> ML.e_absurd (Mltree.I e.e_ity) eff lbl
538
    | Ecase (e1, bl) when e_ghost e1 ->
539 540
        (* if [e1] is ghost but the entire [match-with] expression doesn't,
           it must be the case the first branch is irrefutable *)
541
        (match bl with [] -> assert false | (_, e) :: _ -> expr info svar e)
542
    | Ecase (e1, pl) ->
543 544
        let pl = List.map (ebranch info svar) pl in
        ML.e_match (expr info svar e1) pl (Mltree.I e.e_ity) eff lbl
545
    | Eassert _ -> ML.e_unit
546
    | Eif (e1, e2, e3) when e_ghost e1 ->
547 548
        (* if [e1] is ghost but the entire [if-then-else] expression doesn't,
           it must be the case one of the branches is [Eabsurd] *)
549
        if e2.e_node = Eabsurd then expr info svar e3 else expr info svar e2
Mário Pereira's avatar
Mário Pereira committed
550
    | Eif (e1, e2, e3) when e_ghost e3 ->
551
        ML.e_if (expr info svar e1) (expr info svar e2) ML.e_unit eff lbl
Mário Pereira's avatar
Mário Pereira committed
552
    | Eif (e1, e2, e3) when e_ghost e2 ->
553
        ML.e_if (expr info svar e1) ML.e_unit (expr info svar e3) eff lbl
554
    | Eif (e1, e2, e3) ->
555 556 557 558
        let e1 = expr info svar e1 in
        let e2 = expr info svar e2 in
        let e3 = expr info svar e3 in
        ML.e_if e1 e2 e3 eff lbl
Mário Pereira's avatar
Mário Pereira committed
559
    | Ewhile (e1, _, _, e2) ->
560
        ML.e_while (expr info svar e1) (expr info svar e2) eff lbl
561 562
    | Efor (pv1, (pv2, dir, pv3), _, _, efor) ->
        let dir = for_direction dir in
563
        let efor = expr info svar efor in
564
        ML.e_for pv1 pv2 dir pv3 efor eff lbl
Mário Pereira's avatar
Mário Pereira committed
565
    | Eghost _ -> assert false
566
    | Eassign al ->
567
        ML.e_assign al (Mltree.I e.e_ity) eff lbl
568
    | Epure _ -> assert false
569
    | Etry (etry, case, pvl_e_map) ->
570
        assert (not case); (* TODO *)
571
        let etry = expr info svar etry in
572 573
        let bl   =
          let bl_map = Mxs.bindings pvl_e_map in
574
          List.map (fun (xs, (pvl, e)) -> xs, pvl, expr info svar e) bl_map in
575
        ML.mk_expr (Mltree.Etry (etry, bl)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
576
    | Eraise (xs, ex) ->
577 578
        let ex = exp_of_mask ex xs.xs_mask in
        let ex = match expr info svar ex with
579
          | {Mltree.e_node = Mltree.Eblock []} -> None
580
          | e -> Some e in
581
        ML.mk_expr (Mltree.Eraise (xs, ex)) (Mltree.I e.e_ity) eff lbl
582
    | Eexn (xs, e1) ->
583 584 585 586 587
        if mask_ghost e1.e_mask then ML.mk_expr
            (Mltree.Eexn (xs, None, ML.e_unit)) (Mltree.I e.e_ity) eff lbl
        else let e1 = expr info svar 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
588
        ML.mk_expr (Mltree.Eexn (xs, ty, e1)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
589 590
    | Elet (LDsym (_, {c_node=(Cany|Cpur (_, _)); _ }), _)
    | Eexec ({c_node=Cpur (_, _); _ }, _) -> ML.mk_hole
591

592
  and ebranch info svar ({pp_pat = p; pp_mask = m}, e) =
Mário Pereira's avatar
Mário Pereira committed
593 594 595
    (* 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 *)
596
    if e.e_effect.eff_ghost then (pat m p, ML.e_unit)
597
    else (pat m p, expr info svar e)
598

599
  (* type declarations/definitions *)
Mário Pereira's avatar
Mário Pereira committed
600
  let tdef itd =
601
    let s = itd.itd_its in
602
    let ddata_constructs = (* point-free *)
Mário Pereira's avatar
Mário Pereira committed
603
      List.map (fun ({rs_cty = cty} as rs) ->
604
          rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
605
          let args = List.filter pv_not_ghost cty.cty_args in
606 607
          List.map (fun {pv_vs = vs} -> type_ vs.vs_ty) args)
    in
Mário Pereira's avatar
Mário Pereira committed
608
    let drecord_fields ({rs_cty = cty} as rs) =
609
      (List.exists (pv_equal (fd_of_rs rs)) s.its_mfields),
Mário Pereira's avatar
Mário Pereira committed
610
      rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
611
      mlty_of_ity cty.cty_mask cty.cty_result
612 613 614 615
    in
    let id = s.its_ts.ts_name in
    let is_private = s.its_private in
    let args = s.its_ts.ts_args in
616
    begin match s.its_def, itd.itd_constructors, itd.itd_fields with
617
      | NoDef, [], [] ->
618
          ML.mk_its_defn id args is_private None
619
      | NoDef, cl, [] ->
620 621
          let cl = ddata_constructs cl in
          ML.mk_its_defn id args is_private (Some (Mltree.Ddata cl))
622
      | NoDef, _, pjl ->
623 624 625 626 627 628 629 630 631 632
          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
633
      | Alias t, _, _ ->
Mário Pereira's avatar
Mário Pereira committed
634
          ML.mk_its_defn id args is_private (* FIXME ? is this a good mask ? *)
635
            (Some (Mltree.Dalias (mlty_of_ity MaskVisible t)))
636 637 638 639
      | Range _, [], [] ->
          assert (args = []); (* a range type is not polymorphic *)
          ML.mk_its_defn id [] is_private (Some (Mltree.Dalias ty_int))
      | Range _, _, _ -> assert false (* a range type has no field/constr. *)
640
      | Float _, _, _ -> assert false (* TODO *)
641
    end
Mário Pereira's avatar
Mário Pereira committed
642

643
  (* exception ExtractionVal of rsymbol *)
Mário Pereira's avatar
Mário Pereira committed
644

Mário Pereira's avatar
Mário Pereira committed
645 646 647 648
  let is_val = function
    | Eexec ({c_node = Cany}, _) -> true
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
649 650 651
  let rec fun_expr_of_mask mask e =
    let open Mltree in
    let mk_e e_node = { e with e_node = e_node } in
652
    (* assert (mask <> MaskGhost); *)
Mário Pereira's avatar
Mário Pereira committed
653 654 655 656
    match e.e_node with
    | Econst _ | Evar _   | Efun _ | Eassign _ | Ewhile _
    | Efor   _ | Eraise _ | Eexn _ | Eabsurd   | Ehole    -> e
    | Eapp (rs, el) when is_rs_tuple rs ->
657 658 659 660
        begin match visible_of_mask mask el with
          | [] -> ML.e_unit
          | [e] -> e
          | el -> mk_e (Eapp (rs, el)) end
Mário Pereira's avatar
Mário Pereira committed
661 662
    | Eapp _ -> e
    | Elet (let_def, ein) -> let ein = fun_expr_of_mask mask ein in
663
        mk_e (Elet (let_def, ein))
Mário Pereira's avatar
Mário Pereira committed
664
    | Eif (e1, e2, e3) ->
665 666 667
        let e2 = fun_expr_of_mask mask e2 in
        let e3 = fun_expr_of_mask mask e3 in
        mk_e (Eif (e1, e2, e3))
Mário Pereira's avatar
Mário Pereira committed
668
    | Ematch (e1, pel) ->
669 670
        let mk_pel (p, ee) = (p, fun_expr_of_mask mask ee) in
        mk_e (Ematch (e1, List.map mk_pel pel))
Mário Pereira's avatar
Mário Pereira committed
671 672
    | Eblock [] -> e
    | Eblock el -> let (e_block, e_last) = Lists.chop_last el in
673 674
        let e_last = fun_expr_of_mask mask e_last in
        mk_e (Eblock (e_block @ [e_last]))
Mário Pereira's avatar
Mário Pereira committed
675
    | Etry (e1, xspvel) ->
676 677
        let mk_xspvel (xs, pvl, ee) = (xs, pvl, fun_expr_of_mask mask ee) in
        mk_e (Etry (e1, List.map mk_xspvel xspvel))
Mário Pereira's avatar
Mário Pereira committed
678
    | Eignore ee ->
679 680
        let ee = fun_expr_of_mask mask ee in
        mk_e (Eignore ee)
Mário Pereira's avatar
Mário Pereira committed
681

682 683
  (* pids: identifiers from cloned modules without definitions *)
  let pdecl _pids info pd =
Mário Pereira's avatar
Mário Pereira committed
684
    match pd.pd_node with
685
    | PDlet (LDsym (rs, _)) when rs_ghost rs ->
686
        []
Mário Pereira's avatar
Mário Pereira committed
687
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cany})) ->
688 689 690
        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))]
691 692 693 694
    | 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, []))]
Mário Pereira's avatar
Mário Pereira committed
695
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cfun e})) ->
696 697
        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
698 699 700 701 702
        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
        let e = expr info svar e in
703 704
        let e = fun_expr_of_mask cty.cty_mask e in
        [Mltree.Dlet (Mltree.Lsym (rs, res, args, e))]
705
    | PDlet (LDrec rl) ->
706 707 708 709 710 711 712 713 714
        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
715
          let e = fun_expr_of_mask rs1.rs_cty.cty_mask (expr info svar e) in
716 717 718 719
          { 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))]
Mário Pereira's avatar
Mário Pereira committed
720
    | PDlet (LDsym _) | PDpure | PDlet (LDvar _) ->
721
        []
722
    | PDtype itl ->
723 724
        let itsd = List.map tdef itl in
        [Mltree.Dtype itsd]
725
    | PDexn xs ->
726 727
        if ity_equal xs.xs_ity ity_unit || xs.xs_mask = MaskGhost then
          [Mltree.Dexn (xs, None)]
728
        else [Mltree.Dexn (xs, Some (mlty_of_ity xs.xs_mask xs.xs_ity))]
Mário Pereira's avatar
Mário Pereira committed
729

Mário Pereira's avatar
Mário Pereira committed
730
  let pdecl_m m pd =
731
    let info = { Mltree.from_mod = Some m; Mltree.from_km = m.mod_known; } in
732
    pdecl Sid.empty info pd
Mário Pereira's avatar
Mário Pereira committed
733

734 735 736 737 738 739 740 741 742 743 744
  let abstract_or_alias_type itd =
    itd.itd_fields = [] && itd.itd_constructors = []

  let empty_pdecl pd  = match pd.pd_node with
    | PDlet (LDsym (_, {c_node = Cfun _})) | PDlet (LDrec _) -> false
    | PDexn _ -> false (* FIXME? *)
    | PDtype itl -> List.for_all abstract_or_alias_type itl
    | _ -> true
  let rec empty_munit = function
    | Udecl pd -> empty_pdecl pd
    | Uclone mi -> List.for_all empty_munit mi.mi_mod.mod_units
745
    | Uscope (_, l) -> List.for_all empty_munit l
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
    | Uuse _ | Umeta _ -> true

  let is_empty_clone mi =
    Mts.is_empty mi.mi_ty &&
    Mts.is_empty mi.mi_ts &&
    Mls.is_empty mi.mi_ls &&
    Decl.Mpr.is_empty mi.mi_pr &&
    Decl.Mpr.is_empty mi.mi_pk &&
    Mvs.is_empty mi.mi_pv &&
    Mrs.is_empty mi.mi_rs &&
    Mxs.is_empty mi.mi_xs &&
    List.for_all empty_munit mi.mi_mod.mod_units

  let find_params dl =
    let add params = function
      | Uclone mi when is_empty_clone mi -> mi :: params
      | _ -> params in
    List.fold_left add [] dl

Mário Pereira's avatar
Mário Pereira committed
765 766 767 768
  (* unit module declarations *)
  let rec mdecl pids info = function
    | Udecl pd -> pdecl pids info pd
    | Uscope (_, ([Uuse _] | [Uclone _])) -> []
769
    | Uscope (s, dl) ->
770 771
        let dl = List.concat (List.map (mdecl pids info) dl) in
        [Mltree.Dmodule (s, dl)]
Mário Pereira's avatar
Mário Pereira committed
772 773
    | Uuse _ | Uclone _ | Umeta _ -> []

774 775 776
  let ids_of_params pids mi =
    Mid.fold (fun id _ pids -> Sid.add id pids) mi.mi_mod.mod_known pids

Mário Pereira's avatar
Mário Pereira committed
777
  (* modules *)