mlw_typing.ml 28.4 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
MARCHE Claude's avatar
MARCHE Claude committed
3
(*  Copyright (C) 2010-2012                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
MARCHE Claude's avatar
MARCHE Claude committed
7
(*    Guillaume Melquiond                                                 *)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

open Why3
open Util
23
open Ident
24 25 26
open Ty
open Term
open Decl
27 28 29
open Theory
open Env
open Ptree
30
open Mlw_dtree
31
open Mlw_ty
32
open Mlw_ty.T
33 34
open Mlw_expr
open Mlw_decl
35
open Mlw_module
36
open Mlw_dty
37

38 39
(** errors *)

40
exception DuplicateProgVar of string
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
exception DuplicateTypeVar of string
(*
exception PredicateExpected
exception TermExpected
exception FSymExpected of lsymbol
exception PSymExpected of lsymbol
exception ClashTheory of string
exception UnboundTheory of qualid
exception UnboundType of string list
*)
exception UnboundTypeVar of string
exception UnboundSymbol of string list

let error = Loc.error
let errorm = Loc.errorm

let rec print_qualid fmt = function
  | Qident s -> Format.fprintf fmt "%s" s.id
  | Qdot (m, s) -> Format.fprintf fmt "%a.%s" print_qualid m s.id

let () = Exn_printer.register (fun fmt e -> match e with
  | DuplicateTypeVar s ->
63 64 65
      Format.fprintf fmt "Type parameter %s is used twice" s
  | DuplicateProgVar s ->
      Format.fprintf fmt "Parameter %s is used twice" s
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
(*
  | PredicateExpected ->
      Format.fprintf fmt "syntax error: predicate expected"
  | TermExpected ->
      Format.fprintf fmt "syntax error: term expected"
  | FSymExpected ls ->
      Format.fprintf fmt "%a is not a function symbol" Pretty.print_ls ls
  | PSymExpected ls ->
      Format.fprintf fmt "%a is not a predicate symbol" Pretty.print_ls ls
  | ClashTheory s ->
      Format.fprintf fmt "Clash with previous theory %s" s
  | UnboundTheory q ->
      Format.fprintf fmt "unbound theory %a" print_qualid q
  | UnboundType sl ->
      Format.fprintf fmt "Unbound type '%a'"
        (Pp.print_list Pp.dot Pp.pp_print_string) sl
*)
  | UnboundTypeVar s ->
      Format.fprintf fmt "unbound type variable '%s" s
  | UnboundSymbol sl ->
      Format.fprintf fmt "Unbound symbol '%a'"
        (Pp.print_list Pp.dot Format.pp_print_string) sl
  | _ -> raise e)

(* TODO: let type_only = Debug.test_flag Typing.debug_type_only in *)

92 93 94 95 96 97
type denv = {
  uc     : module_uc;
  locals : (tvars * dity) Mstr.t;
  tvars  : tvars;
  denv   : Typing.denv; (* for user type variables only *)
}
98

99 100 101 102 103
let create_denv uc =
  { uc = uc;
    locals = Mstr.empty;
    tvars = empty_tvars;
    denv = Typing.create_denv (); }
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
(** Typing type expressions *)

let rec dity_of_pty ~user denv = function
  | Ptree.PPTtyvar id ->
      create_user_type_variable id
  | Ptree.PPTtyapp (pl, p) ->
      let dl = List.map (dity_of_pty ~user denv) pl in
      let x = Typing.string_list_of_qualid [] p in
      begin
        try
          let its = ns_find_it (get_namespace denv.uc) x in
          its_app ~user its dl
        with Not_found -> try
          let ts = ns_find_ts (Theory.get_namespace (get_theory denv.uc)) x in
          ts_app ts dl
        with Not_found ->
          let loc = Typing.qloc p in
          errorm ~loc "unbound symbol %a" Typing.print_qualid p
      end
  | Ptree.PPTtuple pl ->
      ts_app (ts_tuple (List.length pl)) (List.map (dity_of_pty ~user denv) pl)
126 127 128 129 130 131 132 133 134 135 136

(** Typing program expressions *)

let rec extract_labels labs loc e = match e.Ptree.expr_desc with
  | Ptree.Enamed (Ptree.Lstr s, e) -> extract_labels (s :: labs) loc e
  | Ptree.Enamed (Ptree.Lpos p, e) -> extract_labels labs (Some p) e
  | Ptree.Ecast  (e, ty) ->
      let labs, loc, d = extract_labels labs loc e in
      labs, loc, Ptree.Ecast ({ e with Ptree.expr_desc = d }, ty)
  | e -> List.rev labs, loc, e

137
(*
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
let unify_arg dity { dexpr_loc = loc; dexpr_type = (args, res) } =
  if args <> [] then errorm ~loc "value expected";
  unify dity res

let unify_args ls args el =
  try
    List.iter2 unify_arg args el
  with Invalid_argument _ ->
    raise (Term.BadArity (ls, List.length args, List.length el))

let unify_args_prg ~loc prg args el = match prg with
  | PV { pv_vs = vs } ->
      errorm ~loc "%s: not a function" vs.vs_name.id_string
  | PL pl ->
      unify_args pl.pl_ls args el; []
  | PA { pa_name = id } | PS { ps_name = id } ->
      let rec unify_list = function
        | a :: args, e :: el -> unify_arg a e; unify_list (args, el)
        | args, [] -> args
        | [], _ :: _ -> errorm ~loc "too many arguments for %s" id.id_string
      in
      unify_list (args, el)

let rec decompose_app args e = match e.Ptree.expr_desc with
  | Eapply (e1, e2) -> decompose_app (e2 :: args) e1
  | _ -> e, args
164 165 166 167 168 169 170
*)

(* value restriction *)
let rec is_fun e = match e.dexpr_desc with
  | DEfun _ -> true
  | DEmark (_, e) -> is_fun e
  | _ -> false
171

172 173 174 175 176 177 178 179 180 181
let rec dexpr ~userloc denv e =
  let loc = e.Ptree.expr_loc in
  let labs, userloc, d = extract_labels [] userloc e in
  let d, ty = dexpr_desc ~userloc denv loc d in
  let loc = def_option loc userloc in
  let e = {
    dexpr_desc = d; dexpr_loc = loc; dexpr_lab = labs; dexpr_type = ty; }
  in
  e

182
and dexpr_desc ~userloc denv _loc = function
183 184
  | Ptree.Eident (Qident {id=x}) when Mstr.mem x denv.locals ->
      (* local variable *)
185 186 187 188
      let tvs, dity = Mstr.find x denv.locals in
      let dity = specialize_scheme tvs dity in
      DElocal x, dity
(***
189 190 191 192 193
  | Ptree.Eident p ->
      let x = Typing.string_list_of_qualid [] p in
      begin
        try
          let prg = ns_find_ps (get_namespace denv.uc) x in
194 195 196 197 198
          begin match prg with
            | PV pv -> DEglobal_pv pv, specialize_pvsymbol pv
            | PS ps -> DEglobal_ps ps, specialize_psymbol  ps
            | PL pl -> DEglobal_pl (pl, []), specialize_plsymbol pl
          end
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        with Not_found -> try
          let ls = ns_find_ls (Theory.get_namespace (get_theory denv.uc)) x in
          DElogic (ls, []), specialize_lsymbol ls
        with Not_found ->
          errorm ~loc "unbound symbol %a" Typing.print_qualid p
      end
  | Ptree.Eapply (e1, e2) ->
      let e, el = decompose_app [e2] e1 in
      let e = dexpr ~userloc denv e in
      let el = List.map (dexpr ~userloc denv) el in
      begin match e.dexpr_desc with
        | DElogic (ls, _) ->
            let args, res = e.dexpr_type in
            unify_args ls args el;
            DElogic (ls, el), ([], res)
        | DEglobal (prg, _) ->
            let args, res = e.dexpr_type in
            let args = unify_args_prg ~loc prg args el in
            DEglobal (prg, el), (args, res)
        | _ ->
          assert false (*TODO*)
      end
221
***)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  | Ptree.Elet (id, e1, e2) ->
      let e1 = dexpr ~userloc denv e1 in
      let tvars =
        if is_fun e1 then denv.tvars else add_tvars denv.tvars e1.dexpr_type in
      let s = tvars, e1.dexpr_type in
      let denv =
        { denv with locals = Mstr.add id.id s denv.locals; tvars = tvars } in
      let e2 = dexpr ~userloc denv e2 in
      DElet (id, e1, e2), e2.dexpr_type
  | Ptree.Ecast (e1, pty) ->
      let e1 = dexpr ~userloc denv e1 in
      unify e1.dexpr_type (dity_of_pty ~user:false denv pty);
      e1.dexpr_desc, e1.dexpr_type
  | Ptree.Enamed _ ->
      assert false
237 238 239
  | _ ->
      assert false (*TODO*)

240
let id_user x = id_user x.id x.id_loc
241

242
let rec expr locals de = match de.dexpr_desc with
243 244 245
  | DElocal x ->
      assert (Mstr.mem x locals);
      begin match Mstr.find x locals with
246 247
      | LetV pv -> e_value pv
      | LetA ps -> e_cast ps (vty_of_dity de.dexpr_type)
248
      end
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  | DElet (x, { dexpr_desc = DEfun (bl, tr) }, de2) ->
      let def1 = expr_fun locals x bl tr in
      let locals = Mstr.add x.id (LetA def1.rec_ps) locals in
      let e2 = expr locals de2 in
      e_rec [def1] e2
  | DEfun (bl, tr) ->
      let x = { id = "fun"; id_loc = de.dexpr_loc; id_lab = [] } in
      let def = expr_fun locals x bl tr in
      let e2 = e_cast def.rec_ps (VTarrow def.rec_ps.ps_vta) in
      e_rec [def] e2
  | DElet (x, de1, de2) ->
      let e1 = expr locals de1 in
      let def1 = create_let_defn (id_user x) e1 in
      let locals = Mstr.add x.id def1.let_var locals in
      let e2 = expr locals de2 in
      e_let def1 e2
  | DEapply (de1, de2) ->
      let e1 = expr locals de1 in
      let e2 = expr locals de2 in
      e_app e1 [e2]
  | DEglobal_pv pv ->
      e_value pv
  | DEglobal_ps ps ->
      e_cast ps (vty_of_dity de.dexpr_type)
  | DEglobal_pl (pls, del) ->
      let ity = ity_of_dity de.dexpr_type in
      e_plapp pls (List.map (expr locals) del) ity
  | DElogic (ls, del) ->
      let ity = ity_of_dity de.dexpr_type in
      e_lapp ls (List.map (expr locals) del) ity
279 280 281
  | _ ->
      assert false (*TODO*)

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
and expr_fun locals x bl (_, e1, _) =
  let binder (id, ghost, dity) =
    let vtv = vty_value ~ghost (ity_of_dity dity) in
    create_pvsymbol (id_user id) vtv in
  let pvl = List.map binder bl in
  let add_binder pv = Mstr.add pv.pv_vs.vs_name.id_string (LetV pv) in
  let locals' = List.fold_right add_binder pvl locals in
  let e1 = expr locals' e1 in
  let ty1 = match e1.e_vty with
    | VTarrow _ -> ty_tuple []
    | VTvalue vtv -> ty_of_ity vtv.vtv_ity in
  let res1 = create_vsymbol (id_fresh "result") ty1 in
  let lam = {
    l_args = pvl;
    l_variant = [];
    l_pre = t_true;                   (* TODO *)
    l_expr = e1;
    l_post = create_post res1 t_true; (* TODO *)
    l_xpost = Mexn.empty;             (* TODO *)
      } in
  create_fun_defn (id_user x) lam

304 305
(** Type declaration *)

306
type tys = ProgTS of itysymbol | PureTS of tysymbol
307 308 309 310 311 312 313 314 315

let find_tysymbol q uc =
  let loc = Typing.qloc q in
  let sl = Typing.string_list_of_qualid [] q in
  try ProgTS (ns_find_it (get_namespace uc) sl)
  with Not_found ->
  try PureTS (ns_find_ts (Theory.get_namespace (get_theory uc)) sl)
  with Not_found -> error ~loc (UnboundSymbol sl)

316 317 318
let look_for_loc tdl s =
  let look_id loc id = if id.id = s then Some id.id_loc else loc in
  let look_pj loc (id,_) = option_fold look_id loc id in
319 320 321
  let look_cs loc (csloc,id,pjl) =
    let loc = if id.id = s then Some csloc else loc in
    List.fold_left look_pj loc pjl in
322 323 324 325 326 327 328 329 330 331
  let look_fl loc f = look_id loc f.f_ident in
  let look loc d =
    let loc = look_id loc d.td_ident in
    match d.td_def with
      | TDabstract | TDalias _ -> loc
      | TDalgebraic csl -> List.fold_left look_cs loc csl
      | TDrecord fl -> List.fold_left look_fl loc fl
  in
  List.fold_left look None tdl

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
let add_types uc tdl =
  let add m d =
    let id = d.td_ident.id in
    Mstr.add_new (Loc.Located (d.td_loc, ClashSymbol id)) id d m in
  let def = List.fold_left add Mstr.empty tdl in

  (* detect cycles *)

  let rec cyc_visit x d seen = match Mstr.find_opt x seen with
    | Some true -> seen
    | Some false -> errorm ~loc:d.td_loc "Cyclic type definition"
    | None ->
        let ts_seen seen = function
          | Qident { id = x } ->
              begin try cyc_visit x (Mstr.find x def) seen
              with Not_found -> seen end
          | _ -> seen in
        let rec check seen = function
          | PPTtyvar _ -> seen
          | PPTtyapp (tyl,q) -> List.fold_left check (ts_seen seen q) tyl
          | PPTtuple tyl -> List.fold_left check seen tyl in
        let seen = match d.td_def with
          | TDabstract | TDalgebraic _ | TDrecord _ -> seen
          | TDalias ty -> check (Mstr.add x false seen) ty in
        Mstr.add x true seen in
  ignore (Mstr.fold cyc_visit def Mstr.empty);

  (* detect mutable types *)

  let mutables = Hashtbl.create 5 in
  let rec mut_visit x =
    try Hashtbl.find mutables x
    with Not_found ->
      let ts_mut = function
        | Qident { id = x } when Mstr.mem x def -> mut_visit x
        | q ->
            begin match find_tysymbol q uc with
              | ProgTS s -> s.its_regs <> []
              | PureTS _ -> false end in
      let rec check = function
        | PPTtyvar _ -> false
        | PPTtyapp (tyl,q) -> ts_mut q || List.exists check tyl
        | PPTtuple tyl -> List.exists check tyl in
      Hashtbl.replace mutables x false;
      let mut = match (Mstr.find x def).td_def with
        | TDabstract -> false
        | TDalias ty -> check ty
        | TDalgebraic csl ->
            let proj (_,pty) = check pty in
            List.exists (fun (_,_,l) -> List.exists proj l) csl
        | TDrecord fl ->
            let field f = f.f_mutable || check f.f_pty in
            List.exists field fl in
      Hashtbl.replace mutables x mut;
      mut
  in
  Mstr.iter (fun x _ -> ignore (mut_visit x)) def;

  (* create type symbols and predefinitions for mutable types *)

  let tysymbols = Hashtbl.create 5 in
  let predefs = Hashtbl.create 5 in
  let rec its_visit x =
    try match Hashtbl.find tysymbols x with
      | Some ts -> ts
      | None ->
          let loc = (Mstr.find x def).td_loc in
          errorm ~loc "Mutable type in a recursive type definition"
    with Not_found ->
      let d = Mstr.find x def in
      let add_tv acc id =
        let e = Loc.Located (id.id_loc, DuplicateTypeVar id.id) in
404
        let tv = create_tvsymbol (Denv.create_user_id id) in
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        Mstr.add_new e id.id tv acc in
      let vars = List.fold_left add_tv Mstr.empty d.td_params in
      let vl = List.map (fun id -> Mstr.find id.id vars) d.td_params in
      let id = Denv.create_user_id d.td_ident in
      let abst = d.td_vis = Abstract in
      let priv = d.td_vis = Private in
      Hashtbl.add tysymbols x None;
      let get_ts = function
        | Qident { id = x } when Mstr.mem x def -> ProgTS (its_visit x)
        | q -> find_tysymbol q uc
      in
      let rec parse = function
        | PPTtyvar { id = v ; id_loc = loc } ->
            let e = Loc.Located (loc, UnboundTypeVar v) in
            ity_var (Mstr.find_exn e v vars)
        | PPTtyapp (tyl,q) ->
            let tyl = List.map parse tyl in
            begin match get_ts q with
              | PureTS ts -> Loc.try2 (Typing.qloc q) ity_pur ts tyl
              | ProgTS ts -> Loc.try2 (Typing.qloc q) ity_app_fresh ts tyl
            end
        | PPTtuple tyl ->
427
            let ts = ts_tuple (List.length tyl) in
428 429 430 431 432
            ity_pur ts (List.map parse tyl)
      in
      let ts = match d.td_def with
        | TDalias ty ->
            let def = parse ty in
433 434
            let rl = Sreg.elements def.ity_vars.vars_reg in
            create_itysymbol id ~abst ~priv vl rl (Some def)
435
        | TDalgebraic csl when Hashtbl.find mutables x ->
436 437 438 439 440 441
            let projs = Hashtbl.create 5 in
            (* to check projections' types we must fix the tyvars *)
            let add s v = let t = ity_var v in ity_match s t t in
            let sbs = List.fold_left add ity_subst_empty vl in
            let mk_proj s (id,pty) =
              let ity = parse pty in
442
              let vtv = vty_value ity in
443 444
              match id with
                | None ->
445
                    let pv = create_pvsymbol (id_fresh "pj") vtv in
446
                    Sreg.union s ity.ity_vars.vars_reg, (pv, false)
447 448 449
                | Some id ->
                    try
                      let pv = Hashtbl.find projs id.id in
450
                      let ty = pv.pv_vtv.vtv_ity in
451 452
                      (* once we have ghost/mutable fields in algebraics,
                         don't forget to check here that they coincide, too *)
453
                      ignore (Loc.try3 id.id_loc ity_match sbs ty ity);
454 455
                      s, (pv, true)
                    with Not_found ->
456
                      let pv = create_pvsymbol (Denv.create_user_id id) vtv in
457
                      Hashtbl.replace projs id.id pv;
458
                      Sreg.union s ity.ity_vars.vars_reg, (pv, true)
459 460 461 462 463 464
            in
            let mk_constr s (_loc,cid,pjl) =
              let s,pjl = Util.map_fold_left mk_proj s pjl in
              s, (Denv.create_user_id cid, pjl)
            in
            let s,def = Util.map_fold_left mk_constr Sreg.empty csl in
465
            Hashtbl.replace predefs x def;
466
            create_itysymbol id ~abst ~priv vl (Sreg.elements s) None
467
        | TDrecord fl when Hashtbl.find mutables x ->
468 469 470 471 472
            let mk_field s f =
              let ghost = f.f_ghost in
              let ity = parse f.f_pty in
              let fid = Denv.create_user_id f.f_ident in
              let s,mut = if f.f_mutable then
473
                let r = create_region fid ity in
474 475
                Sreg.add r s, Some r
              else
476
                Sreg.union s ity.ity_vars.vars_reg, None
477
              in
478 479
              let vtv = vty_value ?mut ~ghost ity in
              s, (create_pvsymbol fid vtv, true)
480 481 482
            in
            let s,pjl = Util.map_fold_left mk_field Sreg.empty fl in
            let cid = { d.td_ident with id = "mk " ^ d.td_ident.id } in
483
            Hashtbl.replace predefs x [Denv.create_user_id cid, pjl];
484 485 486 487 488 489 490 491 492 493 494
            create_itysymbol id ~abst ~priv vl (Sreg.elements s) None
        | TDalgebraic _ | TDrecord _ | TDabstract ->
            create_itysymbol id ~abst ~priv vl [] None
      in
      Hashtbl.add tysymbols x (Some ts);
      ts
  in
  Mstr.iter (fun x _ -> ignore (its_visit x)) def;

  (* create predefinitions for immutable types *)

495
  let def_visit d (abstr,algeb,alias) =
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    let x = d.td_ident.id in
    let ts = Util.of_option (Hashtbl.find tysymbols x) in
    let add_tv s x v = Mstr.add x.id v s in
    let vars = List.fold_left2 add_tv Mstr.empty d.td_params ts.its_args in
    let get_ts = function
      | Qident { id = x } when Mstr.mem x def ->
          ProgTS (Util.of_option (Hashtbl.find tysymbols x))
      | q -> find_tysymbol q uc
    in
    let rec parse = function
      | PPTtyvar { id = v ; id_loc = loc } ->
          let e = Loc.Located (loc, UnboundTypeVar v) in
          ity_var (Mstr.find_exn e v vars)
      | PPTtyapp (tyl,q) ->
          let tyl = List.map parse tyl in
          begin match get_ts q with
            | PureTS ts -> Loc.try2 (Typing.qloc q) ity_pur ts tyl
            | ProgTS ts -> Loc.try3 (Typing.qloc q) ity_app ts tyl []
          end
      | PPTtuple tyl ->
516
          let ts = ts_tuple (List.length tyl) in
517 518 519
          ity_pur ts (List.map parse tyl)
    in
    match d.td_def with
520 521 522 523
      | TDabstract ->
          ts :: abstr, algeb, alias
      | TDalias _ ->
          abstr, algeb, ts :: alias
524
      | (TDalgebraic _ | TDrecord _) when Hashtbl.find mutables x ->
525
          abstr, (ts, Hashtbl.find predefs x) :: algeb, alias
526 527 528 529
      | TDalgebraic csl ->
          let projs = Hashtbl.create 5 in
          let mk_proj (id,pty) =
            let ity = parse pty in
530
            let vtv = vty_value ity in
531 532
            match id with
              | None ->
533
                  create_pvsymbol (id_fresh "pj") vtv, false
534 535 536
              | Some id ->
                  try
                    let pv = Hashtbl.find projs id.id in
537
                    let ty = pv.pv_vtv.vtv_ity in
538 539
                    (* once we have ghost/mutable fields in algebraics,
                       don't forget to check here that they coincide, too *)
540
                    Loc.try2 id.id_loc ity_equal_check ty ity;
541 542
                    pv, true
                  with Not_found ->
543
                    let pv = create_pvsymbol (Denv.create_user_id id) vtv in
544 545 546 547 548
                    Hashtbl.replace projs id.id pv;
                    pv, true
          in
          let mk_constr (_loc,cid,pjl) =
            Denv.create_user_id cid, List.map mk_proj pjl in
549
          abstr, (ts, List.map mk_constr csl) :: algeb, alias
550 551 552
      | TDrecord fl ->
          let mk_field f =
            let fid = Denv.create_user_id f.f_ident in
553 554
            let vtv = vty_value ~ghost:f.f_ghost (parse f.f_pty) in
            create_pvsymbol fid vtv, true in
555
          let cid = { d.td_ident with id = "mk " ^ d.td_ident.id } in
556 557
          let csl = [Denv.create_user_id cid, List.map mk_field fl] in
          abstr, (ts, csl) :: algeb, alias
558
  in
559
  let abstr,algeb,alias = List.fold_right def_visit tdl ([],[],[]) in
560 561

  (* detect pure type declarations *)
562

563 564 565
  let kn = get_known uc in
  let check its = Mid.mem its.its_pure.ts_name kn in
  let check ity = ity_s_any check Util.ffalse ity in
566
  let is_impure_type ts =
567
    ts.its_abst || ts.its_priv || ts.its_regs <> [] ||
568
    option_apply false check ts.its_def
569
  in
570
  let check (pv,_) =
571
    let vtv = pv.pv_vtv in
572
    vtv.vtv_ghost || vtv.vtv_mut <> None || check vtv.vtv_ity in
573 574 575
  let is_impure_data (ts,csl) =
    is_impure_type ts ||
    List.exists (fun (_,l) -> List.exists check l) csl
576
  in
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  let mk_pure_decl (ts,csl) =
    let pjt = Hvs.create 3 in
    let ty = ty_app ts.its_pure (List.map ty_var ts.its_args) in
    let mk_proj (pv,f) =
      let vs = pv.pv_vs in
      if f then try vs.vs_ty, Some (Hvs.find pjt vs) with Not_found ->
        let pj = create_fsymbol (id_clone vs.vs_name) [ty] vs.vs_ty in
        Hvs.replace pjt vs pj;
        vs.vs_ty, Some pj
      else
        vs.vs_ty, None
    in
    let mk_constr (id,pjl) =
      let pjl = List.map mk_proj pjl in
      let cs = create_fsymbol id (List.map fst pjl) ty in
      cs, List.map snd pjl
    in
    ts.its_pure, List.map mk_constr csl
  in
  let add_type_decl uc ts =
    if is_impure_type ts then
      add_pdecl_with_tuples uc (create_ty_decl ts)
599
    else
600 601 602 603 604 605 606 607 608 609 610 611 612
      add_decl_with_tuples uc (Decl.create_ty_decl ts.its_pure)
  in
  try
    let uc = List.fold_left add_type_decl uc abstr in
    let uc = if algeb = [] then uc else
      if List.exists is_impure_data algeb then
        add_pdecl_with_tuples uc (create_data_decl algeb)
      else
        let d = List.map mk_pure_decl algeb in
        add_decl_with_tuples uc (Decl.create_data_decl d)
    in
    let uc = List.fold_left add_type_decl uc alias in
    uc
613
  with
614 615 616 617 618 619 620 621 622
    | ClashSymbol s ->
        error ?loc:(look_for_loc tdl s) (ClashSymbol s)
    | RecordFieldMissing ({ ls_name = { id_string = s }} as cs,ls) ->
        error ?loc:(look_for_loc tdl s) (RecordFieldMissing (cs,ls))
    | DuplicateRecordField ({ ls_name = { id_string = s }} as cs,ls) ->
        error ?loc:(look_for_loc tdl s) (DuplicateRecordField (cs,ls))
    | DuplicateVar { vs_name = { id_string = s }} ->
        errorm ?loc:(look_for_loc tdl s)
          "Field %s is used twice in the same constructor" s
623 624 625

(** Use/Clone of theories and modules *)

626 627 628 629
type mlw_contents = modul Mstr.t
type mlw_library = mlw_contents library
type mlw_file = mlw_contents * Theory.theory Mstr.t

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
let find_theory loc lib path s =
  (* search first in .mlw files (using lib) *)
  let thm =
    try Some (Env.read_lib_theory lib path s)
    with LibFileNotFound _ | TheoryNotFound _ -> None
  in
  (* search also in .why files *)
  let th =
    try Some (Env.find_theory (Env.env_of_library lib) path s)
    with LibFileNotFound _ | TheoryNotFound _ -> None
  in
  match thm, th with
    | Some _, Some _ ->
        Loc.errorm ~loc
          "a module/theory %s is defined both in Why and WhyML libraries" s
    | None, None -> Loc.error ~loc (Env.TheoryNotFound (path, s))
    | None, Some t | Some t, None -> t

let find_theory loc lib mt path s = match path with
  | [] -> (* local theory *)
      begin try Mstr.find s mt with Not_found -> find_theory loc lib [] s end
  | _ :: _ -> (* theory in file path *)
      find_theory loc lib path s

type theory_or_module = Theory of Theory.theory | Module of modul

656 657 658
let print_path fmt sl =
  Pp.print_list (Pp.constant_string ".") Format.pp_print_string fmt sl

659 660 661 662 663
let find_module loc lib path s =
  (* search first in .mlw files *)
  let m, thm =
    try
      let mm, mt = Env.read_lib_file lib path in
664
      Mstr.find_opt s mm, Mstr.find_opt s mt
665 666 667 668 669 670 671 672 673 674 675 676 677
    with
      | LibFileNotFound _ -> None, None
  in
  (* search also in .why files *)
  let th =
    try Some (Env.find_theory (Env.env_of_library lib) path s)
    with LibFileNotFound _ | TheoryNotFound _ -> None
  in
  match m, thm, th with
    | Some _, None, _ -> assert false
    | _, Some _, Some _ ->
        Loc.errorm ~loc
          "a module/theory %s is defined both in Why and WhyML libraries" s
678 679
    | None, None, None ->
        Loc.errorm ~loc "Theory/module not found: %a" print_path (path @ [s])
680 681 682
    | Some m, Some _, None -> Module m
    | None, Some t, None | None, None, Some t -> Theory t

683
let find_module loc lib mm mt path s = match path with
684 685 686 687 688 689 690
  | [] -> (* local module/theory *)
      begin try Module (Mstr.find s mm)
        with Not_found -> begin try Theory (Mstr.find s mt)
          with Not_found -> find_module loc lib [] s end end
  | _ :: _ -> (* module/theory in file path *)
      find_module loc lib path s

691 692
(** Main loop *)

693
let add_theory lib path mt m =
694 695
  let { id = id; id_loc = loc } = m.pth_name in
  if Mstr.mem id mt then Loc.errorm ~loc "clash with previous theory %s" id;
696
  let uc = create_theory ~path (Denv.create_user_id m.pth_name) in
697
  let rec add_decl uc = function
698 699
    | Dlogic d ->
        Typing.add_decl uc d
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    | Duseclone (loc, use, inst) ->
        let path, s = Typing.split_qualid use.use_theory in
        let th = find_theory loc lib mt path s in
        (* open namespace, if any *)
        let uc =
          if use.use_imp_exp <> None then Theory.open_namespace uc else uc in
        (* use or clone *)
        let uc = match inst with
          | None -> Theory.use_export uc th
          | Some inst ->
              let inst = Typing.type_inst uc th inst in
              Theory.clone_export uc th inst
        in
        (* close namespace, if any *)
        begin match use.use_imp_exp with
          | None -> uc
          | Some imp -> Theory.close_namespace uc imp use.use_as
        end
718 719 720
    | Dnamespace (loc, name, import, dl) ->
        let uc = Theory.open_namespace uc in
        let uc = List.fold_left add_decl uc dl in
721
        Loc.try3 loc Theory.close_namespace uc import name
722 723 724 725
    | Dlet _ | Dletrec _ | Dparam _ | Dexn _ | Duse _ ->
        assert false
  in
  let uc = List.fold_left add_decl uc m.pth_decl in
726
  let th = close_theory uc in
727 728 729 730 731 732 733 734
  Mstr.add id th mt

let add_module lib path mm mt m =
  let { id = id; id_loc = loc } = m.mod_name in
  if Mstr.mem id mm then Loc.errorm ~loc "clash with previous module %s" id;
  if Mstr.mem id mt then Loc.errorm ~loc "clash with previous theory %s" id;
  let uc = create_module ~path (Denv.create_user_id m.mod_name) in
  let rec add_decl uc = function
735 736 737 738
    | Dlogic (TypeDecl tdl) ->
        add_types uc tdl
    | Dlogic d ->
        add_to_theory Typing.add_decl uc d
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    | Duseclone (loc, use, inst) ->
        let path, s = Typing.split_qualid use.use_theory in
        let mth = find_module loc lib mm mt path s in
        (* open namespace, if any *)
        let uc = if use.use_imp_exp <> None then open_namespace uc else uc in
        (* use or clone *)
        let uc = match mth, inst with
          | Theory th, None -> use_export_theory uc th
          | Theory th, Some inst ->
              let inst = Typing.type_inst (get_theory uc) th inst in
              clone_export_theory uc th inst
          | Module m, None -> use_export uc m
          | Module m, Some inst ->
              let inst = Typing.type_inst (get_theory uc) m.mod_theory inst in
              clone_export uc m inst
        in
        (* close namespace, if any *)
        begin match use.use_imp_exp with
          | None -> uc
          | Some imp -> close_namespace uc imp use.use_as
        end
    | Dnamespace (loc, name, import, dl) ->
        let uc = open_namespace uc in
        let uc = List.fold_left add_decl uc dl in
        Loc.try3 loc close_namespace uc import name
764 765 766 767 768 769 770 771
    | Dlet (_id, e) ->
        let e = dexpr ~userloc:None (create_denv uc) e in
        ignore (expr Mstr.empty e);
        uc
    | Dletrec _ | Dparam _ | Dexn _ ->
        assert false (* TODO *)
    | Duse _ ->
        assert false (*TO BE REMOVED EVENTUALLY *)
772 773 774 775
  in
  let uc = List.fold_left add_decl uc m.mod_decl in
  let m = close_module uc in
  Mstr.add id m mm, Mstr.add id m.mod_theory mt
776 777 778

let add_theory_module lib path (mm, mt) = function
  | Ptheory th -> mm, add_theory lib path mt th
779
  | Pmodule m -> add_module lib path mm mt m
780