mlw_typing.ml 44.9 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_pretty
36
open Mlw_module
Andrei Paskevich's avatar
Andrei Paskevich committed
37
open Mlw_wp
38
open Mlw_dty
39

40 41
(** errors *)

42
exception DuplicateProgVar of string
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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

59 60
let qloc = Typing.qloc
let print_qualid = Typing.print_qualid
61 62 63

let () = Exn_printer.register (fun fmt e -> match e with
  | DuplicateTypeVar s ->
64 65 66
      Format.fprintf fmt "Type parameter %s is used twice" s
  | DuplicateProgVar s ->
      Format.fprintf fmt "Parameter %s is used twice" s
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 92
(*
  | 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 *)

93 94 95 96
type denv = {
  uc     : module_uc;
  locals : (tvars * dity) Mstr.t;
  tvars  : tvars;
Andrei Paskevich's avatar
Andrei Paskevich committed
97
  uloc   : Ptree.loc option;
98
}
99

100 101 102 103
let create_denv uc = {
  uc     = uc;
  locals = Mstr.empty;
  tvars  = empty_tvars;
Andrei Paskevich's avatar
Andrei Paskevich committed
104
  uloc   = None;
105
}
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
(** 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 ->
123
          errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
124 125 126
      end
  | Ptree.PPTtuple pl ->
      ts_app (ts_tuple (List.length pl)) (List.map (dity_of_pty ~user denv) pl)
127 128 129

(** Typing program expressions *)

Andrei Paskevich's avatar
Andrei Paskevich committed
130
let dity_int  = ts_app ts_int  []
131
let dity_real = ts_app ts_real []
132
let dity_bool = ts_app ts_bool []
Andrei Paskevich's avatar
Andrei Paskevich committed
133 134
let dity_unit = ts_app ts_unit []
let dity_mark = ts_app ts_mark []
135

136
let expected_type ?(weak=false) e dity =
137
  unify ~weak e.de_type dity
138

139 140 141 142 143 144 145 146
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

147 148 149
let rec decompose_app args e = match e.Ptree.expr_desc with
  | Eapply (e1, e2) -> decompose_app (e2 :: args) e1
  | _ -> e, args
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
(* record parsing *)

let parse_record uc fll =
  let pl = match fll with
    | [] -> raise EmptyRecord
    | (pl,_)::_ -> pl in
  let its = match pl.pl_args with
    | [{ vtv_ity = { ity_node = Ityapp (its,_,_) }}] -> its
    | _ -> raise (BadRecordField pl.pl_ls) in
  let cs, pjl = match find_constructors (get_known uc) its with
    | [cs,pjl] -> cs, List.map (exn_option (BadRecordField pl.pl_ls)) pjl
    | _ -> raise (BadRecordField pl.pl_ls) in
  let pjs = List.fold_left (fun s pj -> Sls.add pj.pl_ls s) Sls.empty pjl in
  let flm = List.fold_left (fun m (pj,v) -> let pj = pj.pl_ls in
    if not (Sls.mem pj pjs) then raise (BadRecordField pj) else
      Mls.add_new (DuplicateRecordField (cs.pl_ls,pj)) pj v m)
    Mls.empty fll
  in
  cs,pjl,flm

171
let find_field uc (p,e) =
172 173 174
  let x = Typing.string_list_of_qualid [] p in
  try match ns_find_ps (get_namespace uc) x with
    | PL pl -> (pl,e)
175 176
    | _ -> errorm ~loc:(qloc p) "bad record field %a" print_qualid p
  with Not_found -> errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
177

178
let find_pure_field uc (p,e) =
179 180
  let x = Typing.string_list_of_qualid [] p in
  try ns_find_ls (Theory.get_namespace (get_theory uc)) x, e
181
  with Not_found -> errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
182

183 184
let pure_record uc = function
  | [] -> raise Decl.EmptyRecord
185 186 187 188 189 190
  | (p,_)::_ ->
      let x = Typing.string_list_of_qualid [] p in
      begin try ignore (ns_find_ps (get_namespace uc) x); false
      with Not_found -> true end

let hidden_pl ~loc pl =
191 192 193
  { de_desc = DEglobal_pl pl;
    de_type = specialize_plsymbol pl;
    de_loc  = loc; de_lab = [] }
194 195

let hidden_ls ~loc ls =
196 197 198
  { de_desc = DEglobal_ls ls;
    de_type = specialize_lsymbol ls;
    de_loc  = loc; de_lab = [] }
199 200

(* helper functions for let-expansion *)
201
let test_var e = match e.de_desc with
202 203 204 205 206
  | DElocal _ | DEglobal_pv _ -> true
  | _ -> false

let mk_var e =
  if test_var e then e else
207 208 209 210
  { de_desc = DElocal "q";
    de_type = e.de_type;
    de_loc  = e.de_loc;
    de_lab  = [] }
211

212 213 214 215
let mk_id s loc =
  { id = s; id_loc = loc; id_lab = [] }

let mk_dexpr desc dity loc labs =
216
  { de_desc = desc; de_type = dity; de_loc = loc; de_lab = labs }
217

Andrei Paskevich's avatar
Andrei Paskevich committed
218
let mk_let ~loc ~uloc e (desc,dity) =
219
  if test_var e then desc, dity else
Andrei Paskevich's avatar
Andrei Paskevich committed
220
  let loc = def_option loc uloc in
221 222
  let e1 = mk_dexpr desc dity loc [] in
  DElet (mk_id "q" loc, e, e1), dity
223

224 225 226 227 228 229 230
(* patterns *)

let add_var id dity denv =
  let tvars = add_tvars denv.tvars dity in
  let locals = Mstr.add id.id (tvars,dity) denv.locals in
  { denv with locals = locals; tvars = tvars }

231
let specialize_qualid uc p =
232 233 234 235 236
  let x = Typing.string_list_of_qualid [] p in
  try match ns_find_ps (get_namespace uc) x 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
237 238
    | PX xs ->
        errorm ~loc:(qloc p) "unexpected exception symbol %a" print_xs xs
239 240 241 242
  with Not_found -> try
    let ls = ns_find_ls (Theory.get_namespace (get_theory uc)) x in
    DEglobal_ls ls, specialize_lsymbol ls
  with Not_found ->
243
    errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
244

245
let find_xsymbol uc p =
Andrei Paskevich's avatar
Andrei Paskevich committed
246 247
  let x = Typing.string_list_of_qualid [] p in
  try match ns_find_ps (get_namespace uc) x with
248
    | PX xs -> xs
249
    | _ -> errorm ~loc:(qloc p) "exception symbol expected"
Andrei Paskevich's avatar
Andrei Paskevich committed
250
  with Not_found ->
251
    errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
Andrei Paskevich's avatar
Andrei Paskevich committed
252

253
let find_variant_ls uc p =
254 255
  let x = Typing.string_list_of_qualid [] p in
  try match ns_find_ps (get_namespace uc) x with
256
    | _ -> errorm ~loc:(qloc p) "%a is not a binary relation" print_qualid p
257 258 259
  with Not_found -> try
    match ns_find_ls (Theory.get_namespace (get_theory uc)) x with
      | { ls_args = [u;v]; ls_value = None } as ls when ty_equal u v -> ls
260 261
      | ls ->
          errorm ~loc:(qloc p) "%a is not a binary relation" Pretty.print_ls ls
262
  with Not_found ->
263
    errorm ~loc:(qloc p) "unbound symbol %a" print_qualid p
264

265 266 267 268 269
let rec dpattern denv ({ pat_loc = loc } as pp) = match pp.pat_desc with
  | Ptree.PPpwild ->
      PPwild, create_type_variable (), denv
  | Ptree.PPpvar id ->
      let dity = create_type_variable () in
270
      PPvar (Denv.create_user_id id), dity, add_var id dity denv
271
  | Ptree.PPpapp (q,ppl) ->
272
      let sym, dity = specialize_qualid denv.uc q in
273
      dpat_app denv (mk_dexpr sym dity loc []) ppl
274
  | Ptree.PPprec fl when pure_record denv.uc fl ->
275
      let kn = Theory.get_known (get_theory denv.uc) in
276
      let fl = List.map (find_pure_field denv.uc) fl in
277 278 279 280 281
      let cs,pjl,flm = Loc.try2 loc Decl.parse_record kn fl in
      let wild = { pat_desc = Ptree.PPpwild; pat_loc = loc } in
      let get_val pj = Mls.find_def wild pj flm in
      dpat_app denv (hidden_ls ~loc cs) (List.map get_val pjl)
  | Ptree.PPprec fl ->
282
      let fl = List.map (find_field denv.uc) fl in
283 284 285 286 287 288 289 290 291 292 293 294 295 296
      let cs,pjl,flm = Loc.try2 loc parse_record denv.uc fl in
      let wild = { pat_desc = Ptree.PPpwild; pat_loc = loc } in
      let get_val pj = Mls.find_def wild pj.pl_ls flm in
      dpat_app denv (hidden_pl ~loc cs) (List.map get_val pjl)
  | Ptree.PPptuple ppl ->
      let cs = fs_tuple (List.length ppl) in
      dpat_app denv (hidden_ls ~loc cs) ppl
  | Ptree.PPpor (pp1, pp2) ->
      let pp1, dity1, denv = dpattern denv pp1 in
      let pp2, dity2, denv = dpattern denv pp2 in
      Loc.try2 loc unify dity1 dity2;
      PPor (pp1, pp2), dity1, denv
  | Ptree.PPpas (pp, id) ->
      let pp, dity, denv = dpattern denv pp in
297
      PPas (pp, Denv.create_user_id id), dity, add_var id dity denv
298

299
and dpat_app denv ({ de_loc = loc } as de) ppl =
300 301 302
  let add_pp pp (ppl, tyl, denv) =
    let pp,ty,denv = dpattern denv pp in pp::ppl,ty::tyl,denv in
  let ppl, tyl, denv = List.fold_right add_pp ppl ([],[],denv) in
303
  let pp = match de.de_desc with
304 305
    | DEglobal_pl pl -> Mlw_expr.PPpapp (pl, ppl)
    | DEglobal_ls ls -> PPlapp (ls, ppl)
306 307
    | DEglobal_pv pv -> errorm ~loc "%a is not a constructor" print_pv pv
    | DEglobal_ps ps -> errorm ~loc "%a is not a constructor" print_ps ps
308 309 310
    | _ -> assert false
  in
  let res = create_type_variable () in
311
  Loc.try2 loc unify de.de_type (make_arrow_type tyl res);
312 313
  pp, res, denv

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
(* specifications *)

let dbinder (id,pty) (denv,bl,tyl) =
  let dity = match pty with
    | Some pty -> dity_of_pty ~user:true denv pty
    | None -> create_type_variable () in
  add_var id dity denv, (id,false,dity)::bl, dity::tyl

let dbinders denv bl = List.fold_right dbinder bl (denv,[],[])

let deff_of_peff uc pe =
  { deff_reads  = List.map (fun le -> false, le) pe.pe_reads;
    deff_writes = List.map (fun le -> false, le) pe.pe_writes;
    deff_raises = List.map (fun q -> false, find_xsymbol uc q) pe.pe_raises; }

let dxpost uc ql = List.map (fun (q,f) -> find_xsymbol uc q, f) ql

let rec dtype_c denv tyc =
  let tyv, dity = dtype_v denv tyc.pc_result_type in
  { dc_result = tyv;
    dc_effect = deff_of_peff denv.uc tyc.pc_effect;
    dc_pre    = tyc.pc_pre;
    dc_post   = fst tyc.pc_post;
    dc_xpost  = dxpost denv.uc (snd tyc.pc_post); },
  dity

and dtype_v denv = function
  | Tpure pty ->
      let dity = dity_of_pty ~user:true denv pty in
      DSpecV dity, dity
  | Tarrow (bl,tyc) ->
      let denv,bl,tyl = dbinders denv bl in
      let tyc,dity = dtype_c denv tyc in
      DSpecA (bl,tyc), make_arrow_type tyl dity
348

349
(* expressions *)
350

351 352 353
let de_unit ~loc = hidden_ls ~loc (fs_tuple 0)

let de_app e el =
354
  let res = create_type_variable () in
355
  let tyl = List.map (fun a -> a.de_type) el in
356 357 358
  expected_type e (make_arrow_type tyl res);
  DEapply (e, el), res

Andrei Paskevich's avatar
Andrei Paskevich committed
359
let rec dexpr denv e =
360
  let loc = e.Ptree.expr_loc in
Andrei Paskevich's avatar
Andrei Paskevich committed
361 362
  let labs, uloc, d = extract_labels [] denv.uloc e in
  let denv = { denv with uloc = uloc } in
363
  let d, ty = de_desc denv loc d in
Andrei Paskevich's avatar
Andrei Paskevich committed
364
  let loc = def_option loc uloc in
365
  mk_dexpr d ty loc labs
366

367
and de_desc denv loc = function
368 369
  | Ptree.Eident (Qident {id=x}) when Mstr.mem x denv.locals ->
      (* local variable *)
370 371 372
      let tvs, dity = Mstr.find x denv.locals in
      let dity = specialize_scheme tvs dity in
      DElocal x, dity
373
  | Ptree.Eident p ->
374
      specialize_qualid denv.uc p
375 376
  | Ptree.Eapply (e1, e2) ->
      let e, el = decompose_app [e2] e1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
377
      let el = List.map (dexpr denv) el in
378
      de_app (dexpr denv e) el
379
  | Ptree.Elet (id, e1, e2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
380
      let e1 = dexpr denv e1 in
381 382
      let dity = e1.de_type in
      let tvars = match e1.de_desc with
Andrei Paskevich's avatar
Andrei Paskevich committed
383 384
        | DEfun _ -> denv.tvars
        | _ -> add_tvars denv.tvars dity in
385 386
      let locals = Mstr.add id.id (tvars, dity) denv.locals in
      let denv = { denv with locals = locals; tvars = tvars } in
Andrei Paskevich's avatar
Andrei Paskevich committed
387
      let e2 = dexpr denv e2 in
388
      DElet (id, e1, e2), e2.de_type
Andrei Paskevich's avatar
Andrei Paskevich committed
389
  | Ptree.Eletrec (rdl, e1) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
390
      let rdl = dletrec denv rdl in
391
      let add_one denv ({ id = id }, dity, _) =
392 393
        { denv with locals = Mstr.add id (denv.tvars, dity) denv.locals } in
      let denv = List.fold_left add_one denv rdl in
Andrei Paskevich's avatar
Andrei Paskevich committed
394
      let e1 = dexpr denv e1 in
395
      DEletrec (rdl, e1), e1.de_type
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
396
  | Ptree.Efun (bl, tr) ->
397
      let lam, dity = dlambda denv bl None tr in
398
      DEfun lam, dity
399
  | Ptree.Ecast (e1, pty) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
400
      let e1 = dexpr denv e1 in
401
      expected_type e1 (dity_of_pty ~user:false denv pty);
402
      e1.de_desc, e1.de_type
403 404
  | Ptree.Enamed _ ->
      assert false
405
  | Ptree.Esequence (e1, e2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
406
      let e1 = dexpr denv e1 in
407
      expected_type e1 dity_unit;
Andrei Paskevich's avatar
Andrei Paskevich committed
408
      let e2 = dexpr denv e2 in
409
      DElet (mk_id "_" loc, e1, e2), e2.de_type
410
  | Ptree.Eif (e1, e2, e3) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
411
      let e1 = dexpr denv e1 in
412
      expected_type e1 dity_bool;
Andrei Paskevich's avatar
Andrei Paskevich committed
413 414
      let e2 = dexpr denv e2 in
      let e3 = dexpr denv e3 in
415 416
      expected_type e3 e2.de_type;
      DEif (e1, e2, e3), e2.de_type
417 418
  | Ptree.Etuple el ->
      let ls = fs_tuple (List.length el) in
Andrei Paskevich's avatar
Andrei Paskevich committed
419
      let el = List.map (dexpr denv) el in
420 421
      de_app (hidden_ls ~loc ls) el
  | Ptree.Erecord fl when pure_record denv.uc fl ->
422
      let kn = Theory.get_known (get_theory denv.uc) in
423
      let fl = List.map (find_pure_field denv.uc) fl in
424 425
      let cs,pjl,flm = Loc.try2 loc Decl.parse_record kn fl in
      let get_val pj = match Mls.find_opt pj flm with
Andrei Paskevich's avatar
Andrei Paskevich committed
426
        | Some e -> dexpr denv e
427
        | None -> error ~loc (Decl.RecordFieldMissing (cs,pj)) in
428
      de_app (hidden_ls ~loc cs) (List.map get_val pjl)
429
  | Ptree.Erecord fl ->
430
      let fl = List.map (find_field denv.uc) fl in
431 432
      let cs,pjl,flm = Loc.try2 loc parse_record denv.uc fl in
      let get_val pj = match Mls.find_opt pj.pl_ls flm with
Andrei Paskevich's avatar
Andrei Paskevich committed
433
        | Some e -> dexpr denv e
434
        | None -> error ~loc (Decl.RecordFieldMissing (cs.pl_ls,pj.pl_ls)) in
435 436
      de_app (hidden_pl ~loc cs) (List.map get_val pjl)
  | Ptree.Eupdate (e1, fl) when pure_record denv.uc fl ->
Andrei Paskevich's avatar
Andrei Paskevich committed
437
      let e1 = dexpr denv e1 in
438 439
      let e0 = mk_var e1 in
      let kn = Theory.get_known (get_theory denv.uc) in
440
      let fl = List.map (find_pure_field denv.uc) fl in
441 442
      let cs,pjl,flm = Loc.try2 loc Decl.parse_record kn fl in
      let get_val pj = match Mls.find_opt pj flm with
Andrei Paskevich's avatar
Andrei Paskevich committed
443
        | Some e -> dexpr denv e
444
        | None ->
445
            let d, dity = de_app (hidden_ls ~loc pj) [e0] in
446
            mk_dexpr d dity loc [] in
447
      let res = de_app (hidden_ls ~loc cs) (List.map get_val pjl) in
Andrei Paskevich's avatar
Andrei Paskevich committed
448
      mk_let ~loc ~uloc:denv.uloc e1 res
449
  | Ptree.Eupdate (e1, fl) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
450
      let e1 = dexpr denv e1 in
451
      let e0 = mk_var e1 in
452
      let fl = List.map (find_field denv.uc) fl in
453 454
      let cs,pjl,flm = Loc.try2 loc parse_record denv.uc fl in
      let get_val pj = match Mls.find_opt pj.pl_ls flm with
Andrei Paskevich's avatar
Andrei Paskevich committed
455
        | Some e -> dexpr denv e
456
        | None ->
457
            let d, dity = de_app (hidden_pl ~loc pj) [e0] in
458
            mk_dexpr d dity loc [] in
459
      let res = de_app (hidden_pl ~loc cs) (List.map get_val pjl) in
Andrei Paskevich's avatar
Andrei Paskevich committed
460
      mk_let ~loc ~uloc:denv.uloc e1 res
Andrei Paskevich's avatar
Andrei Paskevich committed
461
  | Ptree.Eassign (e1, q, e2) ->
462 463
      let fl = { expr_desc = Eident q; expr_loc = loc } in
      let e1 = { expr_desc = Eapply (fl,e1); expr_loc = loc } in
Andrei Paskevich's avatar
Andrei Paskevich committed
464 465
      let e1 = dexpr denv e1 in
      let e2 = dexpr denv e2 in
466
      expected_type ~weak:true e2 e1.de_type;
Andrei Paskevich's avatar
Andrei Paskevich committed
467
      DEassign (e1, e2), dity_unit
468 469 470 471 472
  | Ptree.Econstant (ConstInt _ as c) ->
      DEconstant c, dity_int
  | Ptree.Econstant (ConstReal _ as c) ->
      DEconstant c, dity_real
  | Ptree.Enot e1 ->
Andrei Paskevich's avatar
Andrei Paskevich committed
473
      let e1 = dexpr denv e1 in
474 475 476
      expected_type e1 dity_bool;
      DEnot e1, dity_bool
  | Ptree.Elazy (op, e1, e2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
477 478
      let e1 = dexpr denv e1 in
      let e2 = dexpr denv e2 in
479 480 481
      expected_type e1 dity_bool;
      expected_type e2 dity_bool;
      DElazy (op, e1, e2), dity_bool
482
  | Ptree.Ematch (e1, bl) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
483
      let e1 = dexpr denv e1 in
484 485 486
      let res = create_type_variable () in
      let branch (pp,e) =
        let ppat, dity, denv = dpattern denv pp in
Andrei Paskevich's avatar
Andrei Paskevich committed
487
        let e = dexpr denv e in
488
        Loc.try2 pp.pat_loc unify dity e1.de_type;
489 490 491
        expected_type e res;
        ppat, e in
      DEmatch (e1, List.map branch bl), res
Andrei Paskevich's avatar
Andrei Paskevich committed
492 493
  | Ptree.Eraise (q, e1) ->
      let res = create_type_variable () in
494
      let xs = find_xsymbol denv.uc q in
495
      let dity = specialize_xsymbol xs in
Andrei Paskevich's avatar
Andrei Paskevich committed
496
      let e1 = match e1 with
Andrei Paskevich's avatar
Andrei Paskevich committed
497
        | Some e1 -> dexpr denv e1
498
        | None when ity_equal xs.xs_ity ity_unit -> de_unit ~loc
Andrei Paskevich's avatar
Andrei Paskevich committed
499 500 501 502
        | _ -> errorm ~loc "exception argument expected" in
      expected_type e1 dity;
      DEraise (xs, e1), res
  | Ptree.Etry (e1, cl) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
503
      let e1 = dexpr denv e1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
504
      let branch (q, id, e) =
505
        let xs = find_xsymbol denv.uc q in
506
        let dity = specialize_xsymbol xs in
Andrei Paskevich's avatar
Andrei Paskevich committed
507 508
        let id, denv = match id with
          | Some id -> id, add_var id dity denv
509
          | None -> mk_id "void" loc, denv in
Andrei Paskevich's avatar
Andrei Paskevich committed
510
        xs, id, dexpr denv e
Andrei Paskevich's avatar
Andrei Paskevich committed
511 512
      in
      let cl = List.map branch cl in
513
      DEtry (e1, cl), e1.de_type
514
  | Ptree.Eabsurd ->
515
      DEabsurd, create_type_variable ()
516 517
  | Ptree.Eassert (ak, lexpr) ->
      DEassert (ak, lexpr), dity_unit
Andrei Paskevich's avatar
Andrei Paskevich committed
518 519
  | Ptree.Emark (id, e1) ->
      let e1 = dexpr denv e1 in
520
      DEmark (id, e1), e1.de_type
521
  | Ptree.Eany tyc ->
522 523
      let tyc, dity = dtype_c denv tyc in
      DEany tyc, dity
524
  | Ptree.Eloop (_ann, _e1) ->
525 526 527 528
      assert false (*TODO*)
  | Ptree.Efor (_id, _e1, _dir, _e2, _lexpr_opt, _e3) ->
      assert false (*TODO*)
  | Ptree.Eabstract (_e1, _post) ->
529 530
      assert false (*TODO*)

Andrei Paskevich's avatar
Andrei Paskevich committed
531
and dletrec denv rdl =
Andrei Paskevich's avatar
Andrei Paskevich committed
532 533 534
  (* add all functions into environment *)
  let add_one denv (id, bl, var, tr) =
    let res = create_type_variable () in
Andrei Paskevich's avatar
Andrei Paskevich committed
535
    add_var id res denv, (id, res, bl, var, tr) in
Andrei Paskevich's avatar
Andrei Paskevich committed
536 537 538
  let denv, rdl = Util.map_fold_left add_one denv rdl in
  (* then type-check all of them and unify *)
  let type_one (id, res, bl, var, tr) =
539
    let lam, dity = dlambda denv bl var tr in
Andrei Paskevich's avatar
Andrei Paskevich committed
540
    Loc.try2 id.id_loc unify dity res;
541
    id, dity, lam in
542
  List.map type_one rdl
Andrei Paskevich's avatar
Andrei Paskevich committed
543

544 545
and dlambda denv bl var (p, e, (q, xq)) =
  let denv,bl,tyl = dbinders denv bl in
Andrei Paskevich's avatar
Andrei Paskevich committed
546
  let e = dexpr denv e in
547
  let var = match var with
548
    | Some (le, Some q) -> [le, Some (find_variant_ls denv.uc q)]
549 550 551 552
    | Some (le, None) -> [le, None]
    | None -> [] in
(* TODO: accept a list of variants in the surface language
  let var = List.map (fun (le,q) ->
553
    le, Util.option_map (find_variant_ls denv.uc) q) var in
554
*)
555
  (bl, var, p, e, q, dxpost denv.uc xq), make_arrow_type tyl e.de_type
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
556

557
(* second stage *)
558

Andrei Paskevich's avatar
Andrei Paskevich committed
559 560 561 562 563
type lenv = {
  mod_uc   : module_uc;
  let_vars : let_var Mstr.t;
  log_vars : vsymbol Mstr.t;
  log_denv : Typing.denv;
564 565
}

Andrei Paskevich's avatar
Andrei Paskevich committed
566 567
let create_lenv uc = {
  mod_uc   = uc;
568 569 570 571 572 573 574 575 576 577 578
  let_vars = Mstr.empty;
  log_vars = Mstr.empty;
  log_denv = Typing.denv_empty;
}

let rec dty_of_ty ty = match ty.ty_node with
  | Ty.Tyvar v ->
      Typing.create_user_type_var v.tv_name.id_string
  | Ty.Tyapp (ts, tyl) ->
      Denv.tyapp ts (List.map dty_of_ty tyl)

579 580 581 582
let create_post lenv x ty f =
  let res = create_vsymbol (id_fresh x) ty in
  let log_vars = Mstr.add x res lenv.log_vars in
  let log_denv = Typing.add_var x (dty_of_ty ty) lenv.log_denv in
Andrei Paskevich's avatar
Andrei Paskevich committed
583 584 585 586
  (* FIXME? We could add th_mark in create_lenv, but then at/old
     would be available in preconditions, variants, etc... *)
  let th = Theory.use_export (get_theory lenv.mod_uc) Mlw_wp.th_mark in
  create_post res (Typing.type_fmla th log_denv log_vars f)
587 588 589 590

let create_pre lenv f =
  Typing.type_fmla (get_theory lenv.mod_uc) lenv.log_denv lenv.log_vars f

Andrei Paskevich's avatar
Andrei Paskevich committed
591
let add_local x lv lenv = match lv with
592
  | LetA _ ->
Andrei Paskevich's avatar
Andrei Paskevich committed
593
      { lenv with let_vars = Mstr.add x lv lenv.let_vars }
594 595
  | LetV pv ->
      let dty = dty_of_ty pv.pv_vs.vs_ty in
Andrei Paskevich's avatar
Andrei Paskevich committed
596 597 598 599
      { mod_uc   = lenv.mod_uc;
        let_vars = Mstr.add x lv lenv.let_vars;
        log_vars = Mstr.add x pv.pv_vs lenv.log_vars;
        log_denv = Typing.add_var x dty lenv.log_denv }
600

601 602
exception DuplicateException of xsymbol

603
let rec expr lenv de = match de.de_desc with
604
  | DElocal x ->
Andrei Paskevich's avatar
Andrei Paskevich committed
605 606
      assert (Mstr.mem x lenv.let_vars);
      begin match Mstr.find x lenv.let_vars with
607
      | LetV pv -> e_value pv
608
      | LetA ps -> e_cast ps (vty_of_dity de.de_type)
609
      end
610
  | DElet (x, { de_desc = DEfun lam }, de2) ->
611 612
      let def = expr_fun lenv x lam in
      let lenv = add_local x.id (LetA def.rec_ps) lenv in
Andrei Paskevich's avatar
Andrei Paskevich committed
613
      let e2 = expr lenv de2 in
614 615
      e_rec [def] e2
  | DEfun lam ->
616
      let x = mk_id "fn" de.de_loc in
617
      let def = expr_fun lenv x lam in
618 619 620
      let e2 = e_cast def.rec_ps (VTarrow def.rec_ps.ps_vta) in
      e_rec [def] e2
  | DElet (x, de1, de2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
621
      let e1 = expr lenv de1 in
622
      let def1 = create_let_defn (Denv.create_user_id x) e1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
623 624
      let lenv = add_local x.id def1.let_var lenv in
      let e2 = expr lenv de2 in
625
      e_let def1 e2
Andrei Paskevich's avatar
Andrei Paskevich committed
626
  | DEletrec (rdl, de1) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
627
      let rdl = expr_rec lenv rdl in
628
      let add_rd { rec_ps = ps } = add_local ps.ps_name.id_string (LetA ps) in
Andrei Paskevich's avatar
Andrei Paskevich committed
629
      let e1 = expr (List.fold_right add_rd rdl lenv) de1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
630
      e_rec rdl e1
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
631
  | DEapply (de1, del) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
632
      let el = List.map (expr lenv) del in
633 634 635
      begin match de1.de_desc with
        | DEglobal_pl pls -> e_plapp pls el (ity_of_dity de.de_type)
        | DEglobal_ls ls  -> e_lapp  ls  el (ity_of_dity de.de_type)
Andrei Paskevich's avatar
Andrei Paskevich committed
636
        | _               -> e_app (expr lenv de1) el
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
637
      end
638 639 640
  | DEglobal_pv pv ->
      e_value pv
  | DEglobal_ps ps ->
641
      e_cast ps (vty_of_dity de.de_type)
Andrei Paskevich's avatar
Andrei Paskevich committed
642 643
  | DEglobal_pl pl ->
      assert (pl.pl_ls.ls_args = []);
644
      e_plapp pl [] (ity_of_dity de.de_type)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
645
  | DEglobal_ls ls ->
Andrei Paskevich's avatar
Andrei Paskevich committed
646
      assert (ls.ls_args = []);
647
      e_lapp ls [] (ity_of_dity de.de_type)
648
  | DEif (de1, de2, de3) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
649
      e_if (expr lenv de1) (expr lenv de2) (expr lenv de3)
Andrei Paskevich's avatar
Andrei Paskevich committed
650
  | DEassign (de1, de2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
651
      e_assign (expr lenv de1) (expr lenv de2)
652 653 654
  | DEconstant c ->
      e_const c
  | DElazy (LazyAnd, de1, de2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
655
      e_lazy_and (expr lenv de1) (expr lenv de2)
656
  | DElazy (LazyOr, de1, de2) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
657
      e_lazy_or (expr lenv de1) (expr lenv de2)
658
  | DEnot de1 ->
Andrei Paskevich's avatar
Andrei Paskevich committed
659
      e_not (expr lenv de1)
660
  | DEmatch (de1, bl) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
661
      let e1 = expr lenv de1 in
662 663 664
      let vtv = vtv_of_expr e1 in
      let branch (pp,de) =
        let vm, pp = make_ppattern pp vtv in
Andrei Paskevich's avatar
Andrei Paskevich committed
665 666
        let lenv = Mstr.fold (fun s pv -> add_local s (LetV pv)) vm lenv in
        pp, expr lenv de in
667
      e_case e1 (List.map branch bl)
Andrei Paskevich's avatar
Andrei Paskevich committed
668 669 670 671 672
  | DEassert (ak, f) ->
      let ak = match ak with
        | Ptree.Aassert -> Aassert
        | Ptree.Aassume -> Aassume
        | Ptree.Acheck  -> Acheck in
673
      e_assert ak (create_pre lenv f)
674
  | DEabsurd ->
675
      e_absurd (ity_of_dity de.de_type)
Andrei Paskevich's avatar
Andrei Paskevich committed
676
  | DEraise (xs, de1) ->
677
      e_raise xs (expr lenv de1) (ity_of_dity de.de_type)
Andrei Paskevich's avatar
Andrei Paskevich committed
678
  | DEtry (de1, bl) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
679
      let e1 = expr lenv de1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
680 681 682
      let branch (xs,id,de) =
        let vtv = vty_value xs.xs_ity in
        let pv = create_pvsymbol (Denv.create_user_id id) vtv in
Andrei Paskevich's avatar
Andrei Paskevich committed
683 684
        let lenv = add_local id.id (LetV pv) lenv in
        xs, pv, expr lenv de in
Andrei Paskevich's avatar
Andrei Paskevich committed
685
      e_try e1 (List.map branch bl)
Andrei Paskevich's avatar
Andrei Paskevich committed
686 687 688 689
  | DEmark (x, de1) ->
      let ld = create_let_defn (Denv.create_user_id x) e_setmark in
      let lenv = add_local x.id ld.let_var lenv in
      e_let ld (expr lenv de1)
690 691 692 693 694 695
(*
  | DEany deff ->
      let aeff = {
        aeff_reads  = List.map (expr lenv) deff.deff_reads;
        aeff_writes = List.map (expr lenv) deff.deff_writes;
        aeff_raises = deff.deff_raises } in
696
      e_any aeff (ity_of_dity de.de_type)
697 698 699
*)
  | DEghost de1 ->
      e_ghost (expr lenv de1)
700 701 702
  | _ ->
      assert false (*TODO*)

Andrei Paskevich's avatar
Andrei Paskevich committed
703
and expr_rec lenv rdl =
704
  let step1 lenv (id, dity, lam) =
Andrei Paskevich's avatar
Andrei Paskevich committed
705 706 707
    let vta = match vty_of_dity dity with
      | VTarrow vta -> vta
      | VTvalue _ -> assert false in
708
    let ps = create_psymbol (Denv.create_user_id id) vta vars_empty in
709
    add_local id.id (LetA ps) lenv, (ps, lam)
Andrei Paskevich's avatar
Andrei Paskevich committed
710
  in
Andrei Paskevich's avatar
Andrei Paskevich committed
711
  let lenv, rdl = Util.map_fold_left step1 lenv rdl in
712
  let step2 (ps, lam) = ps, expr_lam lenv lam in
Andrei Paskevich's avatar
Andrei Paskevich committed
713 714
  create_rec_defn (List.map step2 rdl)

715 716
and expr_fun lenv x lam =
  let lam = expr_lam lenv lam in
717
  create_fun_defn (Denv.create_user_id x) lam
Andrei Paskevich's avatar
Andrei Paskevich committed
718

719
and expr_lam lenv (bl, var, p, e, q, xq) =
720 721
  let binder (id, ghost, dity) =
    let vtv = vty_value ~ghost (ity_of_dity dity) in
722
    create_pvsymbol (Denv.create_user_id id) vtv in
723
  let pvl = List.map binder bl in
724
  let add_binder pv = add_local pv.pv_vs.vs_name.id_string (LetV pv) in
Andrei Paskevich's avatar
Andrei Paskevich committed
725 726
  let lenv = List.fold_right add_binder pvl lenv in
  let e = expr lenv e in
Andrei Paskevich's avatar
Andrei Paskevich committed
727
  let ty = match e.e_vty with
Andrei Paskevich's avatar
Andrei Paskevich committed
728
    | VTarrow _ -> ty_unit
729
    | VTvalue vtv -> ty_of_ity vtv.vtv_ity in
730 731 732 733
  let mk_variant (t,r) = { v_term = create_pre lenv t; v_rel = r } in
  let add_exn m (xs,f) =
    let f = create_post lenv "result" (ty_of_ity xs.xs_ity) f in
    Mexn.add_new (DuplicateException xs) xs f m in
Andrei Paskevich's avatar
Andrei Paskevich committed
734
  { l_args = pvl;
735 736
    l_variant = List.map mk_variant var;
    l_pre = create_pre lenv p;
Andrei Paskevich's avatar
Andrei Paskevich committed
737
    l_expr = e;
738 739
    l_post = create_post lenv "result" ty q;
    l_xpost = List.fold_left add_exn Mexn.empty xq;
Andrei Paskevich's avatar
Andrei Paskevich committed
740
  }
741

742 743
(** Type declaration *)

744
type tys = ProgTS of itysymbol | PureTS of tysymbol
745 746 747 748 749 750 751 752 753

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)

754 755 756
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
757 758 759
  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
760 761 762 763 764 765 766 767 768 769
  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

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811