compile.ml 35.8 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 25 26
(*                                                                  *)
(*  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.                           *)
(*                                                                  *)
(********************************************************************)

(*
  - extract file f.mlw into OCaml file f.ml, with sub-modules

  - "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
27
open Ty
Mário Pereira's avatar
Mário Pereira committed
28
open Term
29

Mário Pereira's avatar
Mário Pereira committed
30 31 32 33 34 35 36 37
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
38
  let fname = match fname, path with
Mário Pereira's avatar
Mário Pereira committed
39 40 41 42 43
    | None, "why3"::_ -> "why3"
    | None, _   -> String.concat "__" path
    | Some f, _ -> clean_name f in
  fname ^ "__" ^ t

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

48
  let rec get_decl_name = function
Mário Pereira's avatar
Mário Pereira committed
49 50
    | Dtype itdefl -> List.map (fun {its_name = id} -> id) itdefl
    | Dlet (Lrec rdef) -> List.map (fun {rec_sym = rs} -> rs.rs_name) rdef
51 52
    | Dlet (Lvar ({pv_vs={vs_name=id}}, _))
    | Dlet (Lsym ({rs_name=id}, _, _, _))
Mário Pereira's avatar
Mário Pereira committed
53
    | Dlet (Lany ({rs_name=id}, _, _))
54
    | Dexn ({xs_name=id}, _) -> [id]
Mário Pereira's avatar
Mário Pereira committed
55
    | Dmodule (_, _, dl) -> List.concat (List.map get_decl_name dl)
56
    | Dclone _ -> [] (* FIXME? *)
Mário Pereira's avatar
Mário Pereira committed
57

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

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

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

Mário Pereira's avatar
Mário Pereira committed
179 180
  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
181

182
  (* smart constructors *)
Mário Pereira's avatar
Mário Pereira committed
183
  let mk_let_var pv e1 e2 =
Mário Pereira's avatar
Mário Pereira committed
184
    Elet (Lvar (pv, e1), e2)
Mário Pereira's avatar
Mário Pereira committed
185 186

  let tunit = Ttuple []
187

Mário Pereira's avatar
Mário Pereira committed
188 189 190 191 192 193
  let ity_int  = I Ity.ity_int
  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
194

195 196 197
  let enope = Eblock []

  let mk_unit =
Mário Pereira's avatar
Mário Pereira committed
198
    mk_expr enope (I Ity.ity_unit) Ity.eff_empty Slab.empty
199

Mário Pereira's avatar
Mário Pereira committed
200
  let mk_hole =
Mário Pereira's avatar
Mário Pereira committed
201
    mk_expr Ehole (I Ity.ity_unit) Ity.eff_empty Slab.empty
Mário Pereira's avatar
Mário Pereira committed
202

203 204
  let mk_var id ty ghost = (id, ty, ghost)

205 206
  let mk_var_unit () = id_register (id_fresh "_"), tunit, true

207
  let mk_its_defn id args private_ def =
Mário Pereira's avatar
Mário Pereira committed
208 209
    { its_name    = id      ; its_args = args;
      its_private = private_; its_def  = def; }
210

Mário Pereira's avatar
Mário Pereira committed
211 212
  let mk_ignore e =
    if is_unit e.e_ity then e
Mário Pereira's avatar
Mário Pereira committed
213
    else mk_expr (Eignore e) ity_unit e.e_effect e.e_label
Mário Pereira's avatar
Mário Pereira committed
214

215 216
  let eseq e1 e2 =
    match e1.e_node, e2.e_node with
Mário Pereira's avatar
Mário Pereira committed
217
    | (Eblock [] | Ehole), e | e, (Eblock [] | Ehole) -> e
218 219 220 221 222
    | Eblock e1, Eblock e2 -> Eblock (e1 @ e2)
    | _, Eblock e2 -> Eblock (e1 :: e2)
    | Eblock e1, _ -> Eblock (e1 @ [e2])
    | _ -> Eblock [e1; e2]

223
end
Mário Pereira's avatar
Mário Pereira committed
224 225 226 227 228

(** Translation from Mlw to ML *)

module Translate = struct

Mário Pereira's avatar
Mário Pereira committed
229 230 231
  open Expr
  open Pmodule
  open Pdecl
Mário Pereira's avatar
Mário Pereira committed
232

Mário Pereira's avatar
Mário Pereira committed
233 234 235
  (* useful predicates and transformations *)
  let pv_not_ghost e = not e.pv_ghost

Mário Pereira's avatar
Mário Pereira committed
236 237 238 239 240 241 242 243 244
  (* remove ghost components from tuple, using mask *)
  let visible_of_mask m sl = match m with
    | MaskGhost    -> assert false (* FIXME ? *)
    | MaskVisible  -> sl
    | MaskTuple ml ->
      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
245 246 247 248
  (* types *)
  let rec type_ ty =
    match ty.ty_node with
    | Tyvar tvs ->
249
       Mltree.Tvar tvs
Mário Pereira's avatar
Mário Pereira committed
250
    | Tyapp (ts, tyl) when is_ts_tuple ts ->
251
       Mltree.Ttuple (List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
252
    | Tyapp (ts, tyl) ->
253
       Mltree.Tapp (ts.ts_name, List.map type_ tyl)
Mário Pereira's avatar
Mário Pereira committed
254 255 256 257

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

Mário Pereira's avatar
Mário Pereira committed
258 259 260 261 262
  let rec filter_ghost_params p def = function
    | [] -> []
    | pv :: l ->
      if p pv then def pv :: (filter_ghost_params p def l)
      else filter_ghost_params p def l
263

264
  let rec filter_out_ghost_rdef = function
Mário Pereira's avatar
Mário Pereira committed
265
    | [] -> []
266 267
    | { rec_sym = rs; rec_rsym = rrs } :: l when rs_ghost rs || rs_ghost rrs ->
      filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
268 269
    | rdef :: l ->
      rdef :: filter_out_ghost_rdef l
Mário Pereira's avatar
Mário Pereira committed
270

Mário Pereira's avatar
Mário Pereira committed
271
  let rec pat m p = match p.pat_node with
272
    | Pwild ->
273
      Mltree.Pwild
274
    | Pvar vs when (restore_pv vs).pv_ghost ->
275
      Mltree.Pwild
276
    | Pvar vs ->
277
      Mltree.Pident vs.vs_name
278
    | Por (p1, p2) ->
Mário Pereira's avatar
Mário Pereira committed
279
      Mltree.Por (pat m p1, pat m p2)
280
    | Pas (p, vs) when (restore_pv vs).pv_ghost ->
Mário Pereira's avatar
Mário Pereira committed
281
      pat m p
282
    | Pas (p, vs) ->
Mário Pereira's avatar
Mário Pereira committed
283
      Mltree.Pas (pat m p, vs.vs_name)
284
    | Papp (ls, pl) when is_fs_tuple ls ->
Mário Pereira's avatar
Mário Pereira committed
285 286 287 288 289
      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
290
    | Papp (ls, pl) ->
291 292
      let rs = restore_rs ls in
      let args = rs.rs_cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
293
      let mk acc pv pp = if not pv.pv_ghost then pat m pp :: acc else acc in
294
      let pat_pl = List.fold_left2 mk [] args pl in
295
      Mltree.Papp (ls, List.rev pat_pl)
296

Mário Pereira's avatar
Mário Pereira committed
297 298
  (** programs *)

299 300
  let pv_name pv = pv.pv_vs.vs_name

301
  (* individual types *)
Mário Pereira's avatar
Mário Pereira committed
302 303
  let mlty_of_ity mask t =
    let rec loop t = match t.ity_node with
304
    | Ityvar (tvs, _) ->
305
      Mltree.Tvar tvs
306
    | Ityapp ({its_ts = ts}, itl, _) when is_ts_tuple ts ->
Mário Pereira's avatar
Mário Pereira committed
307 308
      let itl = List.rev (visible_of_mask mask itl) in
      Mltree.Ttuple (List.map loop itl)
309
    | Ityapp ({its_ts = ts}, itl, _) ->
Mário Pereira's avatar
Mário Pereira committed
310
      Mltree.Tapp (ts.ts_name, List.map loop itl)
311
    | Ityreg {reg_its = its; reg_args = args} ->
Mário Pereira's avatar
Mário Pereira committed
312 313 314
      let args = List.map loop args in
      Mltree.Tapp (its.its_ts.ts_name, args) in
    loop t
Mário Pereira's avatar
Mário Pereira committed
315

Mário Pereira's avatar
Mário Pereira committed
316
  let pvty pv =
Mário Pereira's avatar
Mário Pereira committed
317 318
    if pv.pv_ghost then ML.mk_var (pv_name pv) ML.tunit true
    else let (vs, vs_ty) = vsty pv.pv_vs in
319
      ML.mk_var vs vs_ty false
Mário Pereira's avatar
Mário Pereira committed
320

321 322 323
  (* let for_direction = function *)
  (*   | To -> Mltree.To *)
  (*   | DownTo -> Mltree.DownTo *)
324

Mário Pereira's avatar
Mário Pereira committed
325
  let isconstructor info rs =
326
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
327 328 329 330 331 332
    | Some {pd_node = PDtype its} ->
      let is_constructor its =
        List.exists (rs_equal rs) its.itd_constructors in
      List.exists is_constructor its
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
333 334 335 336
  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
337
    let pv_equal_field rs = pv_equal (fd_of_rs rs) in
Mário Pereira's avatar
Mário Pereira committed
338 339 340 341
    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
342

343
  let get_record_itd info rs =
344
    match Mid.find_opt rs.rs_name info.Mltree.from_km with
Mário Pereira's avatar
Mário Pereira committed
345 346
    | Some {pd_node = PDtype itdl} ->
      let f pjl_constr = List.exists (rs_equal rs) pjl_constr in
347
      let itd = match rs.rs_field with
Mário Pereira's avatar
Mário Pereira committed
348
        | Some _ -> List.find (fun itd -> f itd.itd_fields) itdl
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        | None -> List.find (fun itd -> f itd.itd_constructors) itdl in
      if itd.itd_fields = [] then None else Some itd
    | _ -> 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
365

Mário Pereira's avatar
Mário Pereira committed
366
  let mk_eta_expansion rsc pvl cty_app =
Mário Pereira's avatar
Mário Pereira committed
367 368
    (* FIXME : effects and types of the expression in this situation *)
    let args_f =
Mário Pereira's avatar
Mário Pereira committed
369 370
      let def pv =
        pv_name pv, mlty_of_ity (mask_of_pv pv) pv.pv_ity, pv.pv_ghost in
Mário Pereira's avatar
Mário Pereira committed
371 372
      filter_ghost_params pv_not_ghost def cty_app.cty_args in
    let args =
Mário Pereira's avatar
Mário Pereira committed
373 374
      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
375 376 377 378 379
      let args = filter_ghost_params pv_not_ghost def pvl in
      let extra_args =
        (* FIXME : ghost status in this extra arguments *)
        List.map def cty_app.cty_args in
      args @ extra_args in
380
    let eapp = ML.mk_expr (Mltree.Eapp (rsc, args)) (Mltree.C cty_app)
Mário Pereira's avatar
Mário Pereira committed
381
        cty_app.cty_effect Slab.empty in
382
    ML.mk_expr (Mltree.Efun (args_f, eapp)) (Mltree.C cty_app)
Mário Pereira's avatar
Mário Pereira committed
383
      cty_app.cty_effect Slab.empty
Mário Pereira's avatar
Mário Pereira committed
384

385 386 387 388 389 390
  (* 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
391 392 393 394
  let params = function
    | []   -> []
    | args -> let args = filter_params args in
      if args = [] then [ML.mk_var_unit ()] else args
395

Mário Pereira's avatar
Mário Pereira committed
396 397
  let filter_params_cty p def pvl cty_args =
    let rec loop = function
Mário Pereira's avatar
Mário Pereira committed
398
      | [], _ -> []
Mário Pereira's avatar
Mário Pereira committed
399 400 401 402 403 404 405
      | pv :: l1, arg :: l2 ->
        if p pv && p arg then def pv :: loop (l1, l2)
        else loop (l1, l2)
      | _ -> assert false
    in loop (pvl, cty_args)

  let app pvl cty_args =
Mário Pereira's avatar
Mário Pereira committed
406 407
    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
408 409
    filter_params_cty pv_not_ghost def pvl cty_args

Mário Pereira's avatar
Mário Pereira committed
410
  let mk_for op_b_rs op_a_rs i_pv from_pv to_pv body_expr eff =
Mário Pereira's avatar
Mário Pereira committed
411 412
    let i_expr, from_expr, to_expr =
      let int_ity = ML.ity_int in let eff_e = eff_empty in
Mário Pereira's avatar
Mário Pereira committed
413 414 415
      ML.mk_expr (Mltree.Evar i_pv)    int_ity eff_e Slab.empty,
      ML.mk_expr (Mltree.Evar from_pv) int_ity eff_e Slab.empty,
      ML.mk_expr (Mltree.Evar to_pv)   int_ity eff_e Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
416
    let for_rs    =
Mário Pereira's avatar
Mário Pereira committed
417
      let for_id  = id_fresh "for_loop_to" in
Mário Pereira's avatar
Mário Pereira committed
418
      let for_cty = create_cty [i_pv] [] [] Mxs.empty Mpv.empty eff ity_unit in
Mário Pereira's avatar
Mário Pereira committed
419 420
      create_rsymbol for_id for_cty in
    let for_expr =
Mário Pereira's avatar
Mário Pereira committed
421 422 423
      let test =
        ML.mk_expr (Mltree.Eapp (op_b_rs, [i_expr; to_expr]))
          (Mltree.I ity_bool) eff_empty Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
424
      let next_expr =
425
        let one_const = Number.int_const_of_int 1 in
Mário Pereira's avatar
Mário Pereira committed
426
        let one_expr  =
Mário Pereira's avatar
Mário Pereira committed
427 428
          ML.mk_expr (Mltree.Econst one_const) ML.ity_int
            eff_empty Slab.empty in
429
        let i_op_one = Mltree.Eapp (op_a_rs, [i_expr; one_expr]) in
Mário Pereira's avatar
Mário Pereira committed
430 431 432 433
        ML.mk_expr i_op_one ML.ity_int eff_empty Slab.empty in
      let rec_call  =
        ML.mk_expr (Mltree.Eapp (for_rs, [next_expr])) ML.ity_unit
          eff Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
434
      let seq_expr =
Mário Pereira's avatar
Mário Pereira committed
435 436 437 438
        ML.mk_expr (ML.eseq body_expr rec_call) ML.ity_unit eff Slab.empty in
      ML.mk_expr (Mltree.Eif (test, seq_expr, ML.mk_unit)) ML.ity_unit
        eff Slab.empty in
    let ty_int = mlty_of_ity MaskVisible ity_int in
Mário Pereira's avatar
Mário Pereira committed
439
    let for_call_expr = let for_call = Mltree.Eapp (for_rs, [from_expr]) in
Mário Pereira's avatar
Mário Pereira committed
440
      ML.mk_expr for_call ML.ity_unit eff Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
441
    let pv_name pv = pv.pv_vs.vs_name in
442
    let args = [ pv_name i_pv, ty_int, false ] in
Mário Pereira's avatar
Mário Pereira committed
443
    let for_rec_def = {
444 445 446
      Mltree.rec_sym  = for_rs;   Mltree.rec_args = args;
      Mltree.rec_rsym = for_rs;   Mltree.rec_exp  = for_expr;
      Mltree.rec_res  = ML.tunit; Mltree.rec_svar = Stv.empty;
Mário Pereira's avatar
Mário Pereira committed
447
    } in
448
    let for_let = Mltree.Elet (Mltree.Lrec [for_rec_def], for_call_expr) in
Mário Pereira's avatar
Mário Pereira committed
449 450 451 452
    ML.mk_expr for_let ML.ity_unit eff

  let mk_for_downto info i_pv from_pv to_pv body eff =
    let ge_rs, minus_rs =
453
      let ns = (Opt.get info.Mltree.from_mod).mod_export in
Mário Pereira's avatar
Mário Pereira committed
454 455 456 457 458
      ns_find_rs ns ["Int"; "infix >="], ns_find_rs ns ["Int"; "infix -"] in
    mk_for ge_rs minus_rs i_pv from_pv to_pv body eff

  let mk_for_to info i_pv from_pv to_pv body eff =
    let le_rs, plus_rs =
459
      let ns = (Opt.get info.Mltree.from_mod).mod_export in
Mário Pereira's avatar
Mário Pereira committed
460 461 462
      ns_find_rs ns ["Int"; "infix <="], ns_find_rs ns ["Int"; "infix +"] in
    mk_for le_rs plus_rs i_pv from_pv to_pv body eff

463
  (* exception ExtractionAny *)
Mário Pereira's avatar
Mário Pereira committed
464

465 466
  (* build the set of type variables from functions arguments *)
  let rec add_tvar acc = function
467 468
    | Mltree.Tvar tv -> Stv.add tv acc
    | Mltree.Tapp (_, tyl) | Mltree.Ttuple tyl ->
469 470
      List.fold_left add_tvar acc tyl

Mário Pereira's avatar
Mário Pereira committed
471
  (* expressions *)
Mário Pereira's avatar
Mário Pereira committed
472
  let rec expr info ({e_effect = eff; e_label = lbl} as e) =
Mário Pereira's avatar
Mário Pereira committed
473
    assert (not eff.eff_ghost);
Mário Pereira's avatar
Mário Pereira committed
474
    match e.e_node with
475
    | Econst c ->
Mário Pereira's avatar
Mário Pereira committed
476 477
      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
Mário Pereira's avatar
Mário Pereira committed
478
    | Evar pv ->
Mário Pereira's avatar
Mário Pereira committed
479
      ML.mk_expr (Mltree.Evar pv) (Mltree.I e.e_ity) eff lbl
480
    | Elet (LDvar (_, e1), e2) when e_ghost e1 ->
Mário Pereira's avatar
Mário Pereira committed
481
      expr info e2
482
    | Elet (LDvar (_, e1), e2) when e_ghost e2 ->
Mário Pereira's avatar
Mário Pereira committed
483
      ML.mk_expr (ML.eseq (expr info e1) ML.mk_unit) ML.ity_unit eff lbl
Mário Pereira's avatar
Mário Pereira committed
484 485 486 487
    | Elet (LDvar (pv, e1), e2)
      when pv.pv_ghost || not (Mpv.mem pv e2.e_effect.eff_reads) ->
      if eff_pure e1.e_effect then expr info e2
      else let e1 = ML.mk_ignore (expr info e1) in
Mário Pereira's avatar
Mário Pereira committed
488
        ML.mk_expr (ML.eseq e1 (expr info e2)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
489 490
    | Elet (LDvar (pv, e1), e2) ->
      let ml_let = ML.mk_let_var pv (expr info e1) (expr info e2) in
Mário Pereira's avatar
Mário Pereira committed
491
      ML.mk_expr ml_let (Mltree.I e.e_ity) eff lbl
492 493
    | Elet (LDsym (rs, _), ein) when rs_ghost rs ->
      expr info ein
494
    | Elet (LDsym (rs, {c_node = Cfun ef; c_cty = cty}), ein) ->
495
      let args = params cty.cty_args in
496 497
      let ef = expr info ef in
      let ein = expr info ein in
Mário Pereira's avatar
Mário Pereira committed
498
      let res = mlty_of_ity cty.cty_mask cty.cty_result in
499
      let ml_letrec = Mltree.Elet (Mltree.Lsym (rs, res, args, ef), ein) in
Mário Pereira's avatar
Mário Pereira committed
500
      ML.mk_expr ml_letrec (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
501 502
    | Elet (LDsym (rsf, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein)
      when isconstructor info rs_app ->
Mário Pereira's avatar
Mário Pereira committed
503 504
      (* partial application of constructor *)
      let eta_app = mk_eta_expansion rs_app pvl cty in
Mário Pereira's avatar
Mário Pereira committed
505
      let ein = expr info ein in
Mário Pereira's avatar
Mário Pereira committed
506 507
      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
Mário Pereira's avatar
Mário Pereira committed
508
      let res = mlty_of_ity cty.cty_mask func in
509
      let ml_letrec = Mltree.Elet (Mltree.Lsym (rsf, res, [], eta_app), ein) in
Mário Pereira's avatar
Mário Pereira committed
510
      ML.mk_expr ml_letrec (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
511
    | Elet (LDsym (rsf, {c_node = Capp (rs_app, pvl); c_cty = cty}), ein) ->
512
      (* partial application *)
Mário Pereira's avatar
Mário Pereira committed
513
      let pvl = app pvl rs_app.rs_cty.cty_args in
514
      let eapp =
Mário Pereira's avatar
Mário Pereira committed
515 516
        ML.mk_expr (Mltree.Eapp (rs_app, pvl)) (Mltree.C cty)
          cty.cty_effect Slab.empty in
Mário Pereira's avatar
Mário Pereira committed
517
      let ein  = expr info ein in
Mário Pereira's avatar
Mário Pereira committed
518
      let res  = mlty_of_ity cty.cty_mask cty.cty_result in
Mário Pereira's avatar
Mário Pereira committed
519
      let args = params cty.cty_args in
520
      let ml_letrec = Mltree.Elet (Mltree.Lsym (rsf, res, args, eapp), ein) in
Mário Pereira's avatar
Mário Pereira committed
521
      ML.mk_expr ml_letrec (Mltree.I e.e_ity) e.e_effect lbl
Mário Pereira's avatar
Mário Pereira committed
522
    | Elet (LDrec rdefl, ein) ->
523 524 525 526
      let rdefl = filter_out_ghost_rdef rdefl in
      let def = function
        | {rec_sym = rs1; rec_rsym = rs2;
           rec_fun = {c_node = Cfun ef; c_cty = cty}} ->
Mário Pereira's avatar
Mário Pereira committed
527
          let res  = mlty_of_ity rs1.rs_cty.cty_mask rs1.rs_cty.cty_result in
528 529 530
          let args = params cty.cty_args in
          let svar =
            let args' = List.map (fun (_, ty, _) -> ty) args in
531
            let svar  = List.fold_left add_tvar Stv.empty args' in
532 533
            add_tvar svar res in
          let ef = expr info ef in
534 535 536
          { Mltree.rec_sym  = rs1;  Mltree.rec_rsym = rs2;
            Mltree.rec_args = args; Mltree.rec_exp  = ef ;
            Mltree.rec_res  = res;  Mltree.rec_svar = svar; }
537 538
        | _ -> assert false in
      let rdefl = List.map def rdefl in
539
      if rdefl <> [] then
540
        let ml_letrec = Mltree.Elet (Mltree.Lrec rdefl, expr info ein) in
Mário Pereira's avatar
Mário Pereira committed
541
        ML.mk_expr ml_letrec (Mltree.I e.e_ity) e.e_effect lbl
542
      else expr info ein
543 544
    | Eexec ({c_node = Capp (rs, [])}, _) when is_rs_tuple rs ->
      ML.mk_unit
545 546
    | Eexec ({c_node = Capp (rs, _)}, _) when is_empty_record info rs ->
      ML.mk_unit
547 548
    | Eexec ({c_node = Capp (rs, _)}, _) when rs_ghost rs ->
      ML.mk_unit
Mário Pereira's avatar
Mário Pereira committed
549
    | Eexec ({c_node = Capp (rs, pvl); c_cty = cty}, _)
Mário Pereira's avatar
Mário Pereira committed
550 551
      when isconstructor info rs && cty.cty_args <> [] ->
      (* partial application of constructors *)
Mário Pereira's avatar
Mário Pereira committed
552
      mk_eta_expansion rs pvl cty
Mário Pereira's avatar
Mário Pereira committed
553
    | Eexec ({c_node = Capp (rs, pvl); _}, _) ->
Mário Pereira's avatar
Mário Pereira committed
554
      let pvl = app pvl rs.rs_cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
555
      begin match pvl with
556
      | [pv_expr] when is_optimizable_record_rs info rs -> pv_expr
Mário Pereira's avatar
Mário Pereira committed
557
      | _ -> ML.mk_expr (Mltree.Eapp (rs, pvl)) (Mltree.I e.e_ity) eff lbl end
Mário Pereira's avatar
Mário Pereira committed
558 559 560
    | Eexec ({c_node = Cfun e; c_cty = {cty_args = []}}, _) ->
      (* abstract block *)
      expr info e
561
    | Eexec ({c_node = Cfun e; c_cty = cty}, _) ->
562
      let args = params cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
563
      ML.mk_expr (Mltree.Efun (args, expr info e)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
564
    | Eexec ({c_node = Cany}, _) -> (* raise ExtractionAny *)
Mário Pereira's avatar
Mário Pereira committed
565
      ML.mk_hole
566
    | Eabsurd ->
Mário Pereira's avatar
Mário Pereira committed
567
      ML.mk_expr Mltree.Eabsurd (Mltree.I e.e_ity) eff lbl
568 569
    | Ecase (e1, _) when e_ghost e1 ->
      ML.mk_unit
570
    | Ecase (e1, pl) ->
Mário Pereira's avatar
Mário Pereira committed
571 572
      let e1 = expr info e1 in
      let pl = List.map (ebranch info) pl in
Mário Pereira's avatar
Mário Pereira committed
573
      ML.mk_expr (Mltree.Ematch (e1, pl)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
574
    | Eassert _ -> ML.mk_unit
Mário Pereira's avatar
Mário Pereira committed
575 576 577
    | Eif (e1, e2, e3) when e_ghost e3 ->
      let e1 = expr info e1 in
      let e2 = expr info e2 in
Mário Pereira's avatar
Mário Pereira committed
578
      ML.mk_expr (Mltree.Eif (e1, e2, ML.mk_unit)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
579 580 581
    | Eif (e1, e2, e3) when e_ghost e2 ->
      let e1 = expr info e1 in
      let e3 = expr info e3 in
Mário Pereira's avatar
Mário Pereira committed
582
      ML.mk_expr (Mltree.Eif (e1, ML.mk_unit, e3)) (Mltree.I e.e_ity) eff lbl
583
    | Eif (e1, e2, e3) ->
Mário Pereira's avatar
Mário Pereira committed
584 585 586
      let e1 = expr info e1 in
      let e2 = expr info e2 in
      let e3 = expr info e3 in
Mário Pereira's avatar
Mário Pereira committed
587
      ML.mk_expr (Mltree.Eif (e1, e2, e3)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
588 589 590
    | Ewhile (e1, _, _, e2) ->
      let e1 = expr info e1 in
      let e2 = expr info e2 in
Mário Pereira's avatar
Mário Pereira committed
591
      ML.mk_expr (Mltree.Ewhile (e1, e2)) (Mltree.I e.e_ity) eff lbl
592
    | Efor (pv1, (pv2, To, pv3), _, _, efor) ->
Mário Pereira's avatar
Mário Pereira committed
593
      let efor = expr info efor in
Mário Pereira's avatar
Mário Pereira committed
594
      mk_for_to info pv1 pv2 pv3 efor eff lbl
595
    | Efor (pv1, (pv2, DownTo, pv3), _, _, efor) ->
Mário Pereira's avatar
Mário Pereira committed
596
      let efor = expr info efor in
Mário Pereira's avatar
Mário Pereira committed
597
      mk_for_downto info pv1 pv2 pv3 efor eff lbl
Mário Pereira's avatar
Mário Pereira committed
598
    | Eghost _ -> assert false
599
    | Eassign al ->
Mário Pereira's avatar
Mário Pereira committed
600
      ML.mk_expr (Mltree.Eassign al) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
601
    | Epure _ -> (* assert false (\*TODO*\) *) ML.mk_hole
602 603
    | Etry (etry, case, pvl_e_map) ->
      assert (not case); (* TODO *)
Mário Pereira's avatar
Mário Pereira committed
604 605 606 607
      let etry = expr info etry in
      let bl   =
        let bl_map = Mxs.bindings pvl_e_map in
        List.map (fun (xs, (pvl, e)) -> xs, pvl, expr info e) bl_map in
Mário Pereira's avatar
Mário Pereira committed
608
      ML.mk_expr (Mltree.Etry (etry, bl)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
609 610 611
    | Eraise (xs, ex) ->
      let ex =
        match expr info ex with
612
        | {Mltree.e_node = Mltree.Eblock []} -> None
Mário Pereira's avatar
Mário Pereira committed
613 614
        | e -> Some e
      in
Mário Pereira's avatar
Mário Pereira committed
615
      ML.mk_expr (Mltree.Eraise (xs, ex)) (Mltree.I e.e_ity) eff lbl
616 617 618
    | Eexn (xs, e1) ->
      let e1 = expr info e1 in
      let ty = if ity_equal xs.xs_ity ity_unit
Mário Pereira's avatar
Mário Pereira committed
619 620
        then None else Some (mlty_of_ity xs.xs_mask xs.xs_ity) in
      ML.mk_expr (Mltree.Eexn (xs, ty, e1)) (Mltree.I e.e_ity) eff lbl
Mário Pereira's avatar
Mário Pereira committed
621 622 623 624
    | Elet (LDsym (_, {c_node=(Cany|Cpur (_, _)); _ }), _)
    (*   assert false (\*TODO*\) *)
    | Eexec ({c_node=Cpur (_, _); _ }, _) -> ML.mk_hole
    (*   assert false (\*TODO*\) *)
625

Mário Pereira's avatar
Mário Pereira committed
626 627
  and ebranch info ({pp_pat = p; pp_mask = m}, e) =
    (pat m p, expr info e)
628

629
  (* type declarations/definitions *)
Mário Pereira's avatar
Mário Pereira committed
630
  let tdef itd =
631
    let s = itd.itd_its in
632
    let ddata_constructs = (* point-free *)
Mário Pereira's avatar
Mário Pereira committed
633
      List.map (fun ({rs_cty = cty} as rs) ->
634
          rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
635
          let args = List.filter pv_not_ghost cty.cty_args in
636 637
          List.map (fun {pv_vs = vs} -> type_ vs.vs_ty) args)
    in
Mário Pereira's avatar
Mário Pereira committed
638
    let drecord_fields ({rs_cty = cty} as rs) =
639
      (List.exists (pv_equal (fd_of_rs rs)) s.its_mfields),
Mário Pereira's avatar
Mário Pereira committed
640
      rs.rs_name,
Mário Pereira's avatar
Mário Pereira committed
641
      mlty_of_ity cty.cty_mask cty.cty_result
642 643 644 645
    in
    let id = s.its_ts.ts_name in
    let is_private = s.its_private in
    let args = s.its_ts.ts_args in
646
    begin match s.its_def, itd.itd_constructors, itd.itd_fields with
647
      | NoDef, [], [] ->
Mário Pereira's avatar
Mário Pereira committed
648
        ML.mk_its_defn id args is_private None
649
      | NoDef, cl, [] ->
Mário Pereira's avatar
Mário Pereira committed
650
        let cl = ddata_constructs cl in
651
        ML.mk_its_defn id args is_private (Some (Mltree.Ddata cl))
652
      | NoDef, _, pjl ->
Mário Pereira's avatar
Mário Pereira committed
653 654
        let p e = not (rs_ghost e) in
        let pjl = filter_ghost_params p drecord_fields pjl in
Mário Pereira's avatar
Mário Pereira committed
655
        begin match pjl with
656 657
          | [] -> ML.mk_its_defn id args is_private
                    (Some (Mltree.Dalias ML.tunit))
658
          | [_, _, ty_pj] when is_optimizable_record_itd itd ->
659 660
            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))
Mário Pereira's avatar
Mário Pereira committed
661
        end
662
      | Alias t, _, _ ->
Mário Pereira's avatar
Mário Pereira committed
663 664
        ML.mk_its_defn id args is_private (* FIXME ? is this a good mask ? *)
          (Some (Mltree.Dalias (mlty_of_ity MaskVisible t)))
665 666
      | Range _, _, _ -> assert false (* TODO *)
      | Float _, _, _ -> assert false (* TODO *)
667
    end
Mário Pereira's avatar
Mário Pereira committed
668

669
  (* exception ExtractionVal of rsymbol *)
Mário Pereira's avatar
Mário Pereira committed
670

Mário Pereira's avatar
Mário Pereira committed
671 672 673 674
  let is_val = function
    | Eexec ({c_node = Cany}, _) -> true
    | _ -> false

Mário Pereira's avatar
Mário Pereira committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  let rec fun_expr_of_mask mask e =
    let open Mltree in
    let mk_e e_node = { e with e_node = e_node } in
    assert (mask <> MaskGhost);
    match e.e_node with
    | Econst _ | Evar _   | Efun _ | Eassign _ | Ewhile _
    | Efor   _ | Eraise _ | Eexn _ | Eabsurd   | Ehole    -> e
    | Eapp (rs, el) when is_rs_tuple rs ->
      begin match visible_of_mask mask el with
        | [] -> ML.mk_unit
        | [e] -> e
        | el -> mk_e (Eapp (rs, el)) end
    | Eapp _ -> e
    | Elet (let_def, ein) -> let ein = fun_expr_of_mask mask ein in
      mk_e (Elet (let_def, ein))
    | Eif (e1, e2, e3) ->
      let e2 = fun_expr_of_mask mask e2 in
      let e3 = fun_expr_of_mask mask e3 in
      mk_e (Eif (e1, e2, e3))
    | Ematch (e1, pel) ->
      let mk_pel (p, ee) = (p, fun_expr_of_mask mask ee) in
      mk_e (Ematch (e1, List.map mk_pel pel))
    | Eblock [] -> e
    | Eblock el -> let (e_block, e_last) = Lists.chop_last el in
      let e_last = fun_expr_of_mask mask e_last in
      mk_e (Eblock (e_block @ [e_last]))
    | Etry (e1, xspvel) ->
      let mk_xspvel (xs, pvl, ee) = (xs, pvl, fun_expr_of_mask mask ee) in
      mk_e (Etry (e1, List.map mk_xspvel xspvel))
    | Eignore ee ->
      let ee = fun_expr_of_mask mask ee in
      mk_e (Eignore ee)

708 709
  (* pids: identifiers from cloned modules without definitions *)
  let pdecl _pids info pd =
Mário Pereira's avatar
Mário Pereira committed
710
    match pd.pd_node with
711
    | PDlet (LDsym (rs, _)) when rs_ghost rs ->
Mário Pereira's avatar
Mário Pereira committed
712
      []
Mário Pereira's avatar
Mário Pereira committed
713 714 715 716
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cany})) ->
      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))]
Mário Pereira's avatar
Mário Pereira committed
717
      (* raise (ExtractionVal _rs) *)
Mário Pereira's avatar
Mário Pereira committed
718 719
    | PDlet (LDsym (_, {c_node = Cfun e})) when is_val e.e_node ->
      []
720
    | PDlet (LDsym ({rs_cty = cty} as rs, {c_node = Cfun e})) ->
721
      let args = params cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
722 723 724 725
      let res = mlty_of_ity cty.cty_mask cty.cty_result in
      let e = expr info e in
      let e = fun_expr_of_mask cty.cty_mask e in
      [Mltree.Dlet (Mltree.Lsym (rs, res, args, e))]
726
    | PDlet (LDrec rl) ->
727
      let rl = filter_out_ghost_rdef rl in
Mário Pereira's avatar
Mário Pereira committed
728
      let def {rec_fun = e; rec_sym = rs1; rec_rsym = rs2} =
Mário Pereira's avatar
Mário Pereira committed
729
        let e = match e.c_node with Cfun e -> e | _ -> assert false in
Mário Pereira's avatar
Mário Pereira committed
730
        let args = params rs1.rs_cty.cty_args in
Mário Pereira's avatar
Mário Pereira committed
731
        let res  = mlty_of_ity rs1.rs_cty.cty_mask rs1.rs_cty.cty_result in
732 733
        let svar =
          let args' = List.map (fun (_, ty, _) -> ty) args in
734
          let svar  = List.fold_left add_tvar Stv.empty args' in
735
          add_tvar svar res in
Mário Pereira's avatar
Mário Pereira committed
736
        let e = fun_expr_of_mask rs1.rs_cty.cty_mask (expr info e) in
737
        { Mltree.rec_sym  = rs1;  Mltree.rec_rsym = rs2;
Mário Pereira's avatar
Mário Pereira committed
738
          Mltree.rec_args = args; Mltree.rec_exp  = e;
739 740
          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
741
    | PDlet (LDsym _) | PDpure | PDlet (LDvar _) ->
742
      []
743
    | PDtype itl ->
Mário Pereira's avatar
Mário Pereira committed
744
      let itsd = List.map tdef itl in
745
      [Mltree.Dtype itsd]
746
    | PDexn xs ->
747
      if ity_equal xs.xs_ity ity_unit then [Mltree.Dexn (xs, None)]
Mário Pereira's avatar
Mário Pereira committed
748
      else [Mltree.Dexn (xs, Some (mlty_of_ity xs.xs_mask xs.xs_ity))]
Mário Pereira's avatar
Mário Pereira committed
749

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

754 755 756 757 758 759 760 761 762 763 764
  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
765
    | Uscope (_, l) -> List.for_all empty_munit l
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    | 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
785 786 787 788 789 790 791 792 793 794 795 796
  (* unit module declarations *)
  let rec mdecl pids info = function
    | Udecl pd -> pdecl pids info pd
    | Uscope (_, ([Uuse _] | [Uclone _])) -> []
    | Uscope (s, dl) -> let dl = List.concat (List.map (mdecl pids info) dl) in
      let filter_func_params (dl, params) = function
        | Mltree.Dmodule (s, _args, mod_dl) -> dl, (s, mod_dl) :: params
        | d -> d :: dl, params in
      let dl, params = List.fold_left filter_func_params ([], []) dl in
      [Mltree.Dmodule (s, List.rev params, List.rev dl)]
    | Uuse _ | Uclone _ | Umeta _ -> []

797 798 799 800 801
  let make_param from mi =
    let id = mi.mi_mod.mod_theory.Theory.th_name in
    Format.printf "param %s@." id.id_string;
    let dl =
      List.concat (List.map (mdecl Sid.empty from) mi.mi_mod.mod_units) in
802
    Mltree.Dclone (id, dl)