mlw_wp.ml 77.7 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2015   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5 6 7 8 9 10
(*                                                                  *)
(*  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.                           *)
(*                                                                  *)
(********************************************************************)
Andrei Paskevich's avatar
Andrei Paskevich committed
11

12
open Stdlib
Andrei Paskevich's avatar
Andrei Paskevich committed
13 14 15
open Ident
open Ty
open Term
16
open Decl
Andrei Paskevich's avatar
Andrei Paskevich committed
17 18 19 20 21
open Theory
open Mlw_ty
open Mlw_ty.T
open Mlw_expr

22
let debug = Debug.register_info_flag "whyml_wp"
Andrei Paskevich's avatar
Andrei Paskevich committed
23
  ~desc:"Print@ details@ of@ verification@ conditions@ generation."
24

25
let no_track = Debug.register_flag "wp_no_track"
Andrei Paskevich's avatar
Andrei Paskevich committed
26 27
  ~desc:"Do@ not@ remove@ redundant@ type@ invariant@ conditions@ from@ VCs."

28
let no_eval = Debug.register_flag "wp_no_eval"
Andrei Paskevich's avatar
Andrei Paskevich committed
29
  ~desc:"Do@ not@ simplify@ pattern@ matching@ on@ record@ datatypes@ in@ VCs."
30

Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
31 32
let lemma_label = Ident.create_label "why3:lemma"

33
(** Marks *)
Andrei Paskevich's avatar
Andrei Paskevich committed
34 35 36 37

let ts_mark = create_tysymbol (id_fresh "'mark") [] None
let ty_mark = ty_app ts_mark []

38
let ity_mark = ity_pur ts_mark []
39 40

let fresh_mark () = create_vsymbol (id_fresh "'mark") ty_mark
41

Andrei Paskevich's avatar
Andrei Paskevich committed
42 43 44 45 46 47 48 49
let fs_at =
  let ty = ty_var (create_tvsymbol (id_fresh "a")) in
  create_lsymbol (id_fresh "at") [ty; ty_mark] (Some ty)

let fs_old =
  let ty = ty_var (create_tvsymbol (id_fresh "a")) in
  create_lsymbol (id_fresh "old") [ty] (Some ty)

50
let mark_theory =
51
  let uc = create_theory ~path:["why3";"Mark"] (id_fresh "Mark") in
52 53 54
  let uc = add_ty_decl uc ts_mark in
  close_theory uc

55 56
let th_mark_at =
  let uc = create_theory (id_fresh "WP builtins: at") in
57
  let uc = use_export uc mark_theory in
Andrei Paskevich's avatar
Andrei Paskevich committed
58
  let uc = add_param_decl uc fs_at in
59 60 61 62 63
  close_theory uc

let th_mark_old =
  let uc = create_theory (id_fresh "WP builtins: old") in
  let uc = use_export uc th_mark_at in
Andrei Paskevich's avatar
Andrei Paskevich committed
64 65 66
  let uc = add_param_decl uc fs_old in
  close_theory uc

67
let fs_now = create_lsymbol (id_fresh "%now") [] (Some ty_mark)
Andrei Paskevich's avatar
Andrei Paskevich committed
68
let t_now = fs_app fs_now [] ty_mark
Andrei Paskevich's avatar
Andrei Paskevich committed
69
let e_now = e_ghost (e_lapp fs_now [] (ity_pur ts_mark []))
Andrei Paskevich's avatar
Andrei Paskevich committed
70

71 72
(* [vs_old] appears in the postconditions given to the core API,
   which expects every vsymbol to be a pure part of a pvsymbol *)
Andrei Paskevich's avatar
Andrei Paskevich committed
73
let pv_old = create_pvsymbol ~ghost:true (id_fresh "%old") ity_mark
74 75
let vs_old = pv_old.pv_vs
let t_old  = t_var vs_old
76

Andrei Paskevich's avatar
Andrei Paskevich committed
77 78
let t_at_old t = t_app fs_at [t; t_old] t.t_ty

79
let ls_absurd = create_lsymbol (id_fresh "absurd") [] None
80
let t_absurd  = t_label_add Split_goal.stop_split (ps_app ls_absurd [])
81

82
let mk_t_if f = t_if f t_bool_true t_bool_false
83
let to_term t = if t.t_ty = None then mk_t_if t else t
84

85
(* any vs in post/xpost is either a pvsymbol or a fresh mark *)
86
let ity_of_vs vs =
87
  if Ty.ty_equal vs.vs_ty ty_mark then ity_mark else (restore_pv vs).pv_ity
88 89 90

(* replace every occurrence of [old(t)] with [at(t,'old)] *)
let rec remove_old f = match f.t_node with
Andrei Paskevich's avatar
Andrei Paskevich committed
91
  | Tapp (ls,[t]) when ls_equal ls fs_old -> t_at_old (remove_old t)
92 93 94 95 96 97 98 99
  | _ -> t_map remove_old f

(* replace every occurrence of [at(t,'now)] with [t] *)
let rec remove_at f = match f.t_node with
  | Tapp (ls, [t; { t_node = Tapp (fs,[]) }])
    when ls_equal ls fs_at && ls_equal fs fs_now -> remove_at t
  | _ -> t_map remove_at f

100 101 102 103
(* replace [at(t,'old)] with [at(t,lab)] everywhere in formula [f] *)
let old_mark lab t = t_subst_single vs_old (t_var lab) t

(* replace [at(t,lab)] with [at(t,'now)] everywhere in formula [f] *)
Andrei Paskevich's avatar
Andrei Paskevich committed
104 105
let erase_mark lab t = t_subst_single lab t_now t

Andrei Paskevich's avatar
Andrei Paskevich committed
106 107
(* retreat to the point of the current postcondition's ['old] *)
let backstep fn q xq =
Andrei Paskevich's avatar
Andrei Paskevich committed
108 109 110
  let lab = fresh_mark () in
  let f = fn (old_mark lab q) (Mexn.map (old_mark lab) xq) in
  erase_mark lab f
111

112
(** WP utilities *)
113 114 115 116 117 118 119

let default_exn_post xs _ =
  let vs = create_vsymbol (id_fresh "result") (ty_of_ity xs.xs_ity) in
  create_post vs t_true

let default_post vty ef =
  let vs = create_vsymbol (id_fresh "result") (ty_of_vty vty) in
120
  create_post vs t_true, Mexn.mapi default_exn_post ef.eff_raises
121

122 123 124 125 126
let wp_label e f =
  let loc = if f.t_loc = None then e.e_loc else f.t_loc in
  let lab = Ident.Slab.union e.e_label f.t_label in
  t_label ?loc lab f

Andrei Paskevich's avatar
Andrei Paskevich committed
127
let expl_pre       = Ident.create_label "expl:precondition"
128
let expl_post      = Ident.create_label "expl:postcondition"
Andrei Paskevich's avatar
Andrei Paskevich committed
129
let expl_xpost     = Ident.create_label "expl:exceptional postcondition"
130
let expl_assume    = Ident.create_label "expl:assumption"
Andrei Paskevich's avatar
Andrei Paskevich committed
131 132
let expl_assert    = Ident.create_label "expl:assertion"
let expl_check     = Ident.create_label "expl:check"
MARCHE Claude's avatar
MARCHE Claude committed
133
let expl_absurd    = Ident.create_label "expl:unreachable point"
134
let expl_type_inv  = Ident.create_label "expl:type invariant"
Andrei Paskevich's avatar
Andrei Paskevich committed
135 136
let expl_loop_init = Ident.create_label "expl:loop invariant init"
let expl_loop_keep = Ident.create_label "expl:loop invariant preservation"
137 138
let expl_loopvar   = Ident.create_label "expl:loop variant decrease"
let expl_variant   = Ident.create_label "expl:variant decrease"
139

140 141 142 143 144 145 146 147 148 149 150 151 152
let lab_has_expl =
  let expl_regexp = Str.regexp "expl:\\(.*\\)" in
  Slab.exists
    (fun l ->
       Str.string_match expl_regexp l.lab_string 0)

let rec wp_expl l f =
  if lab_has_expl f.t_label then f
  else match f.t_node with
    | _ when Slab.mem Split_goal.stop_split f.t_label -> t_label_add l f
    | Tbinop (Tand,f1,f2) -> t_label_copy f (t_and (wp_expl l f1) (wp_expl l f2))
    | Teps _ -> t_label_add l f (* post-condition, push down later *)
    | _ -> f
153

154
let wp_and ~sym f1 f2 =
155 156
  if sym then t_and_simp f1 f2 else t_and_asym_simp f1 f2

157
let wp_ands ~sym fl =
158 159
  if sym then t_and_simp_l fl else t_and_asym_simp_l fl

160
let wp_implies f1 f2 = t_implies_simp f1 f2
161

162 163
let wp_let v t f = t_let_close_simp v t f

164 165
let wp_forall vl f = t_forall_close_simp vl [] f

166 167
let is_equality_for v f = match f.t_node with
  | Tapp (ps, [{ t_node = Tvar u }; t])
168
    when ls_equal ps ps_equ && vs_equal u v && t_v_occurs v t = 0 ->
169 170 171 172
      Some t
  | _ ->
      None

173 174 175 176 177
let wp_forall_post v p f =
  (* we optimize for the case when a postcondition
     is of the form (... /\ result = t /\ ...) *)
  let rec down p = match p.t_node with
    | Tbinop (Tand,l,r) ->
178 179 180 181 182 183
        let t, l, r =
          let t, l = down l in
          if t <> None then t, l, r else
            let t, r = down r in t, l, r
        in
        t, if t = None then p else t_label_copy p (t_and_simp l r)
184
    | _ ->
185 186
        let t = is_equality_for v p in
        t, if t = None then p else t_true
187 188 189 190 191 192
  in
  if ty_equal v.vs_ty ty_unit then
    t_subst_single v t_void (wp_implies p f)
  else match down p with
    | Some t, p -> wp_let v t (wp_implies p f)
    | _ -> wp_forall [v] (wp_implies p f)
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207
let t_and_subst v t1 t2 =
  (* if [t1] defines variable [v], return [t2] with [v] replaced by its
     definition. Otherwise return [t1 /\ t2] *)
  match is_equality_for v t1 with
  | Some def -> t_subst_single v def t2
  | None -> t_and_simp t1 t2

let t_implies_subst v t1 t2 =
  (* if [t1] defines variable [v], return [t2] with [v] replaced by its
     definition. Otherwise return [t1 -> t2] *)
  match is_equality_for v t1 with
  | Some def -> t_subst_single v def t2
  | None -> t_implies_simp t1 t2

208
let regs_of_writes eff = Sreg.union eff.eff_writes eff.eff_ghostw
Andrei Paskevich's avatar
Andrei Paskevich committed
209
let exns_of_raises eff = Sexn.union eff.eff_raises eff.eff_ghostx
210

211 212
let open_post q =
  let v, f = open_post q in
213
  v, Slab.fold wp_expl q.t_label f
214

215 216 217 218 219 220 221 222 223 224 225
let open_unit_post q =
  let v, q = open_post q in
  t_subst_single v t_void q

let create_unit_post =
  let v = create_vsymbol (id_fresh "void") ty_unit in
  fun q -> create_post v q

let vs_result e =
  create_vsymbol (id_fresh ?loc:e.e_loc "result") (ty_of_vty e.e_vty)

226 227 228 229 230 231
(** WP state *)

type wp_env = {
  prog_known : Mlw_decl.known_map;
  pure_known : Decl.known_map;
  global_env : Env.env;
Andrei Paskevich's avatar
Andrei Paskevich committed
232 233 234 235 236
  ps_int_le  : Term.lsymbol;
  ps_int_ge  : Term.lsymbol;
  ps_int_lt  : Term.lsymbol;
  ps_int_gt  : Term.lsymbol;
  fs_int_pl  : Term.lsymbol;
237
  fs_int_mn  : Term.lsymbol;
238
  letrec_var : term list Mint.t;
239
}
240

241 242 243 244 245 246 247 248 249
let decrease_alg ?loc env old_t t =
  let oty = t_type old_t in
  let nty = t_type t in
  let quit () =
    Loc.errorm ?loc "no default order for %a" Pretty.print_term t in
  let ts = match oty with { ty_node = Tyapp (ts,_) } -> ts | _ -> quit () in
  let csl = Decl.find_constructors env.pure_known ts in
  if csl = [] then quit ();
  let sbs = ty_match Mtv.empty (ty_app ts (List.map ty_var ts.ts_args)) oty in
250
  let add_arg fty acc =
251 252 253
    let fty = ty_inst sbs fty in
    if ty_equal fty nty then
      let vs = create_vsymbol (id_fresh "f") nty in
254 255
      pat_var vs, t_or_simp (t_equ (t_var vs) t) acc
    else pat_wild fty, acc in
256
  let add_cs (cs,_) =
257
    let pl, f = Lists.map_fold_right add_arg cs.ls_args t_false in
258 259 260 261 262
    t_close_branch (pat_app cs pl oty) f in
  t_case old_t (List.map add_cs csl)

let decrease_rel ?loc env old_t t = function
  | Some ls -> ps_app ls [t; old_t]
263 264
  | None when ty_equal (t_type old_t) ty_int
           && ty_equal (t_type t) ty_int ->
265
      t_and
266
        (ps_app env.ps_int_le [t_nat_const 0; old_t])
267 268 269
        (ps_app env.ps_int_lt [t; old_t])
  | None -> decrease_alg ?loc env old_t t

270
let decrease loc lab env olds varl =
271
  let rec decr pr olds varl = match olds, varl with
272 273 274 275 276 277 278 279 280 281
    | [], [] -> (* empty variant *)
        t_true
    | [old_t], [t, rel] ->
        t_and_simp pr (decrease_rel ?loc env old_t t rel)
    | old_t::_, (t,_)::_ when not (oty_equal old_t.t_ty t.t_ty) ->
        Loc.errorm ?loc "cannot use lexicographic ordering"
    | old_t::olds, (t,rel)::varl ->
        let dt = t_and_simp pr (decrease_rel ?loc env old_t t rel) in
        let pr = t_and_simp pr (t_equ old_t t) in
        t_or_simp dt (decr pr olds varl)
282
    | _ -> assert false
Andrei Paskevich's avatar
Andrei Paskevich committed
283
  in
284 285 286 287
  t_label ?loc lab (decr t_true olds varl)

let expl_variant = Slab.add Split_goal.stop_split (Slab.singleton expl_variant)
let expl_loopvar = Slab.add Split_goal.stop_split (Slab.singleton expl_loopvar)
Andrei Paskevich's avatar
Andrei Paskevich committed
288

289 290
(** Reconstruct pure values after writes *)

291
let trace_model_regexp = Str.regexp "model_trace:"
292

293 294 295 296 297 298 299
let is_trace_label l =
  try
    ignore(Str.search_forward trace_model_regexp l.lab_string 0);
    true
  with Not_found -> false

let add_to_trace_label labels to_add =
300 301 302 303 304 305
  try
    let trace_label = Slab.choose (Slab.filter is_trace_label labels) in
    let labels_without_trace = Slab.remove trace_label labels in
    let new_trace_label = Ident.create_label (trace_label.lab_string^"@"^to_add) in
    Slab.add new_trace_label labels_without_trace
  with Not_found -> labels
306 307 308 309 310

let mk_var id label ?loc ty =
  let new_labels = add_to_trace_label id.id_label label in

  create_vsymbol (id_fresh ~label:new_labels ?loc id.id_string) ty
311 312 313 314 315 316 317 318 319 320 321 322 323 324

(* replace "contemporary" variables with fresh ones *)
let rec subst_at_now now mvs t = match t.t_node with
  | Tvar vs when now ->
      begin try t_var (Mvs.find vs mvs) with Not_found -> t end
  | Tapp (ls, _) when ls_equal ls fs_old -> assert false
  | Tapp (ls, [_; mark]) when ls_equal ls fs_at ->
      let now = match mark.t_node with
        | Tvar vs when vs_equal vs vs_old -> assert false
        | Tapp (ls,[]) when ls_equal ls fs_now -> true
        | _ -> false in
      t_map (subst_at_now now mvs) t
  | Tlet _ | Tcase _ | Teps _ | Tquant _ ->
      (* do not open unless necessary *)
325
      let mvs = Mvs.set_inter mvs (t_vars t) in
326 327 328 329 330 331
      if Mvs.is_empty mvs then t else
      t_map (subst_at_now now mvs) t
  | _ ->
      t_map (subst_at_now now mvs) t

(* generic expansion of an algebraic type value *)
332
let analyze_var fn_down fn_join lkm km vs ity =
333 334
  let var_of_fd fd =
    create_vsymbol (id_fresh "y") (ty_of_ity fd.fd_ity) in
335
  let branch (cs,fdl) =
336 337 338
    let vl = List.map var_of_fd fdl in
    let pat = pat_app cs (List.map pat_var vl) vs.vs_ty in
    let t = fn_join cs (List.map2 fn_down vl fdl) vs.vs_ty in
339
    t_close_branch pat t in
340
  let csl = Mlw_decl.inst_constructors lkm km ity in
341
  t_case_simp (t_var vs) (List.map branch csl)
342

343 344
(* given a map of modified regions, construct the updated value of [vs] *)
let update_var env (mreg : vsymbol Mreg.t) vs =
345
  let rec update vs { fd_ity = ity; fd_mut = mut } =
346 347
    (* are we a mutable variable? *)
    let get_vs r = Mreg.find_def vs r mreg in
348
    let vs = Opt.fold (fun _ -> get_vs) vs mut in
349 350
    (* should we update our value further? *)
    let check_reg r _ = reg_occurs r ity.ity_vars in
351
    if ity_immutable ity || not (Mreg.exists check_reg mreg) then t_var vs
352 353
    else analyze_var update fs_app env.pure_known env.prog_known vs ity in
  update vs { fd_ity = ity_of_vs vs; fd_ghost = false; fd_mut = None }
354

355 356 357 358
(* given a map of modified regions, update every affected variable in [f] *)
let update_term env (mreg : vsymbol Mreg.t) f =
  (* [vars] : modified variable -> updated value *)
  let update vs _ = match update_var env mreg vs with
359 360
    | { t_node = Tvar nv } when vs_equal vs nv -> None
    | t -> Some t in
361
  let vars = Mvs.mapi_filter update (t_vars f) in
362
  (* [vv'] : modified variable -> fresh variable *)
363
  let new_var vs _ = mk_var vs.vs_name "model_quantify" ?loc:f.t_loc vs.vs_ty in
364
  let vv' = Mvs.mapi new_var vars in
365
  (* update modified variables *)
366
  let update v t f = wp_let (Mvs.find v vv') t f in
367 368
  Mvs.fold update vars (subst_at_now true vv' f)

369 370 371 372 373
let get_single_region_of_var vs =
  match (ity_of_vs vs).ity_node with
    | Ityapp (_,_,[r]) -> Some r
    | _ -> None

374 375
(* look for a variable with a single region equal to [reg] *)
let var_of_region reg f =
376
  let test acc vs =
377 378
    match get_single_region_of_var vs with
    | Some r when reg_equal r reg -> Some vs
379
    | _ -> acc in
380
  t_v_fold test None f
381

382
let quantify ?loc:(l=None) model_info env regs f =
383 384 385 386 387 388
  (* mreg : modified region -> vs *)
  let get_var reg () =
    let ty = ty_of_ity reg.reg_ity in
    let id = match var_of_region reg f with
      | Some vs -> vs.vs_name
      | None -> reg.reg_name in
389 390 391 392
    let l = match l with
      | None -> id.id_loc
      | _ -> l in
    mk_var id model_info ?loc:l ty in
393 394 395
  let mreg = Mreg.mapi get_var regs in
  (* quantify over the modified resions *)
  let f = update_term env mreg f in
396
  wp_forall (List.rev (Mreg.values mreg)) f
397

398 399 400 401 402 403 404 405
(** Invariants *)

let get_invariant km t =
  let ty = t_type t in
  let ts = match ty.ty_node with
    | Tyapp (ts,_) -> ts
    | _ -> assert false in
  let rec find_td = function
406
    | (its,_,inv) :: _ when ts_equal ts its.its_ts -> inv
407 408 409 410 411 412 413 414
    | _ :: tdl -> find_td tdl
    | [] -> assert false in
  let pd = Mid.find ts.ts_name km in
  let inv = match pd.Mlw_decl.pd_node with
    | Mlw_decl.PDdata tdl -> find_td tdl
    | _ -> assert false in
  let sbs = Ty.ty_match Mtv.empty (t_type inv) ty in
  let u, p = open_post (t_ty_subst sbs Mvs.empty inv) in
415
  wp_expl expl_type_inv (t_subst_single u t p)
416 417 418 419 420

let ps_inv = Term.create_psymbol (id_fresh "inv")
  [ty_var (create_tvsymbol (id_fresh "a"))]

let full_invariant lkm km vs ity =
421
  let rec update vs { fd_ity = ity } =
422
    if not (ity_has_inv ity) then t_true else
423 424 425 426 427 428 429 430 431
    (* what is our current invariant? *)
    let f = match ity.ity_node with
      | Ityapp (its,_,_) when its.its_inv ->
          if Debug.test_flag no_track
          then get_invariant km (t_var vs)
          else ps_app ps_inv [t_var vs]
      | _ -> t_true in
    (* what are our sub-invariants? *)
    let join _ fl _ = wp_ands ~sym:true fl in
432
    let g = analyze_var update join lkm km vs ity in
433 434 435
    (* put everything together *)
    wp_and ~sym:true f g
  in
436
  update vs { fd_ity = ity; fd_ghost = false; fd_mut = None }
437 438

(** Value tracking *)
439 440 441

type point = int
type value = point list Mls.t (* constructor -> field list *)
442

443
type state = {
444 445
  st_km   : Mlw_decl.known_map;
  st_lkm  : Decl.known_map;
446
  st_mem  : value Hint.t;
447
  st_next : point ref;
448 449
}

450
(* dead code
451 452 453
type names = point Mvs.t  (* variable -> point *)
type condition = lsymbol Mint.t (* point -> constructor *)
type lesson = condition list Mint.t (* point -> conditions for invariant *)
454
*)
455 456 457 458

let empty_state lkm km = {
  st_km   = km;
  st_lkm  = lkm;
459
  st_mem  = Hint.create 5;
460
  st_next = ref 0;
461 462 463
}

let next_point state =
464
  let res = !(state.st_next) in incr state.st_next; res
465

466
let make_value state ty =
467 468 469
  let get_p _ = next_point state in
  let new_cs cs = List.map get_p cs.ls_args in
  let add_cs m (cs,_) = Mls.add cs (new_cs cs) m in
470
  let csl = match ty.ty_node with
471 472
    | Tyapp (ts,_) -> Decl.find_constructors state.st_lkm ts
    | _ -> [] in
473 474
  List.fold_left add_cs Mls.empty csl

475
let match_point state ty p =
476
  try Hint.find state.st_mem p with Not_found ->
477
  let value = make_value state ty in
478
  if not (Mls.is_empty value) then
479
    Hint.replace state.st_mem p value;
480 481
  value

482 483 484 485 486 487 488 489 490 491 492 493 494 495
let rec open_pattern state names value p pat = match pat.pat_node with
  | Pwild -> names
  | Pvar vs -> Mvs.add vs p names
  | Papp (cs,patl) ->
      let add_pat names p pat =
        let value = match_point state pat.pat_ty p in
        open_pattern state names value p pat in
      List.fold_left2 add_pat names (Mls.find cs value) patl
  | Por _ ->
      let add_vs vs s = Mvs.add vs (next_point state) s in
      Svs.fold add_vs pat.pat_vars names
  | Pas (pat,vs) ->
      open_pattern state (Mvs.add vs p names) value p pat

496 497 498 499
let rec point_of_term state names t = match t.t_node with
  | Tvar vs ->
      Mvs.find vs names
  | Tapp (ls, tl) ->
500
      begin match Mid.find ls.ls_name state.st_lkm with
501 502 503 504 505 506 507 508 509
        | { Decl.d_node = Decl.Ddata tdl } ->
            let is_cs (cs,_) = ls_equal ls cs in
            let is_cs (_,csl) = List.exists is_cs csl in
            if List.exists is_cs tdl
            then point_of_constructor state names ls tl
            else point_of_projection state names ls (List.hd tl)
        | _ -> next_point state
      end
  | Tlet (t1, bt) ->
510
      let p1 = point_of_term state names t1 in
511
      let v, t2 = t_open_bound bt in
512 513 514 515 516 517 518
      let names = Mvs.add v p1 names in
      point_of_term state names t2
  | Tcase (t1,[br]) ->
      let pat, t2 = t_open_branch br in
      let p1 = point_of_term state names t1 in
      let value = match_point state pat.pat_ty p1 in
      let names = open_pattern state names value p1 pat in
519
      point_of_term state names t2
520 521 522 523
  | Tcase (t1,bl) ->
      (* we treat here the case of a value update: the value
         of each branch must be a distinct constructor *)
      let p = next_point state in
524
      let ty = Opt.get t.t_ty in
525
      let p1 = point_of_term state names t1 in
526
      let value = match_point state (Opt.get t1.t_ty) p1 in
527 528 529 530 531 532 533 534 535 536 537 538
      let branch acc br =
        let pat, t2 = t_open_branch br in
        let ls = match t2.t_node with
          | Tapp (ls,_) -> ls | _ -> raise Exit in
        let names = open_pattern state names value p1 pat in
        let p2 = point_of_term state names t2 in
        let v2 = match_point state ty p2 in
        Mls.add_new Exit ls (Mls.find_exn Exit ls v2) acc
      in
      begin try
        let value = List.fold_left branch Mls.empty bl in
        let value = Mls.set_union value (make_value state ty) in
539
        Hint.replace state.st_mem p value
540 541 542
      with Exit -> () end;
      p
  | Tconst _ | Tif _ | Teps _ -> next_point state
543 544 545 546
  | Tquant _ | Tbinop _ | Tnot _ | Ttrue | Tfalse -> assert false

and point_of_constructor state names ls tl =
  let p = next_point state in
547
  let pl = List.map (point_of_term state names) tl in
548
  let value = make_value state (Opt.get ls.ls_value) in
549
  let value = Mls.add ls pl value in
550
  Hint.replace state.st_mem p value;
551 552 553
  p

and point_of_projection state names ls t1 =
554
  let ty = Opt.get t1.t_ty in
555
  let csl = match ty.ty_node with
556
    | Tyapp (ts,_) -> Decl.find_constructors state.st_lkm ts
557 558 559
    | _ -> assert false in
  match csl with
    | [cs,pjl] ->
560
        let p1 = point_of_term state names t1 in
561
        let value = match_point state ty p1 in
562 563 564 565 566 567 568
        let rec find_p pjl pl = match pjl, pl with
          | Some pj::_, p::_ when ls_equal ls pj -> p
          | _::pjl, _::pl -> find_p pjl pl
          | _ -> assert false in
        find_p pjl (Mls.find cs value)
    | _ -> next_point state (* more than one, can't choose *)

569 570 571 572 573 574
let rec track_values state names lesson cond f = match f.t_node with
  | Tapp (ls, [t1]) when ls_equal ls ps_inv ->
      let p1 = point_of_term state names t1 in
      let condl = Mint.find_def [] p1 lesson in
      let contains c1 c2 = Mint.submap (fun _ -> ls_equal) c2 c1 in
      if List.exists (contains cond) condl then
575
        lesson, t_label_copy f t_true
576 577 578 579
      else
        let good c = not (contains c cond) in
        let condl = List.filter good condl in
        let l = Mint.add p1 (cond::condl) lesson in
580
        l, t_label_copy f (get_invariant state.st_km t1)
581 582 583 584 585 586 587 588 589 590 591 592 593 594
  | Tbinop (Timplies, f1, f2) ->
      let l, f1 = track_values state names lesson cond f1 in
      let _, f2 = track_values state names l cond f2 in
      lesson, t_label_copy f (t_implies_simp f1 f2)
  | Tbinop (Tand, f1, f2) ->
      let l, f1 = track_values state names lesson cond f1 in
      let l, f2 = track_values state names l cond f2 in
      l, t_label_copy f (t_and_simp f1 f2)
  | Tif (fc, f1, f2) ->
      let _, f1 = track_values state names lesson cond f1 in
      let _, f2 = track_values state names lesson cond f2 in
      lesson, t_label_copy f (t_if_simp fc f1 f2)
  | Tcase (t1, bl) ->
      let p1 = point_of_term state names t1 in
595
      let value = match_point state (Opt.get t1.t_ty) p1 in
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
      let is_pat_var = function
        | { pat_node = Pvar _ } -> true | _ -> false in
      let branch l br =
        let pat, f1, cb = t_open_branch_cb br in
        let learn, cond = match bl, pat.pat_node with
          | [_], _ -> true, cond (* one branch, can learn *)
          | _, Papp (cs, pl) when List.for_all is_pat_var pl ->
              (try true, Mint.add_new Exit p1 cs cond (* can learn *)
              with Exit -> false, cond) (* contradiction, cannot learn *)
          | _, _ -> false, cond (* complex pattern, will not learn *)
        in
        let names = open_pattern state names value p1 pat in
        let m, f1 = track_values state names lesson cond f1 in
        let l = if learn then m else l in
        l, cb pat f1
      in
612
      let l, bl = Lists.map_fold_left branch lesson bl in
613
      l, t_label_copy f (t_case_simp t1 bl)
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  | Tlet (t1, bf) ->
      let p1 = point_of_term state names t1 in
      let v, f1, cb = t_open_bound_cb bf in
      let names = Mvs.add v p1 names in
      let l, f1 = track_values state names lesson cond f1 in
      l, t_label_copy f (t_let_simp t1 (cb v f1))
  | Tquant (Tforall, qf) ->
      let vl, trl, f1, cb = t_open_quant_cb qf in
      let add_vs s vs = Mvs.add vs (next_point state) s in
      let names = List.fold_left add_vs names vl in
      let l, f1 = track_values state names lesson cond f1 in
      l, t_label_copy f (t_forall_simp (cb vl trl f1))
  | Tbinop ((Tor|Tiff),_,_) | Tquant (Texists,_)
  | Tapp _ | Tnot _ | Ttrue | Tfalse -> lesson, f
  | Tvar _ | Tconst _ | Teps _ -> assert false

let track_values lkm km f =
  let state = empty_state lkm km in
  let _, f = track_values state Mvs.empty Mint.empty Mint.empty f in
  f
634

635 636
(** Weakest preconditions *)

637
let rec wp_expr env e q xq =
Andrei Paskevich's avatar
Andrei Paskevich committed
638
  let f = wp_desc env e q xq in
639
  if Debug.test_flag debug then begin
640
    Format.eprintf "@[--------@\n@[<hov 2>e = %a@]@\n" Mlw_pretty.print_expr e;
641 642 643
    Format.eprintf "@[<hov 2>q = %a@]@\n" Pretty.print_term q;
    Format.eprintf "@[<hov 2>f = %a@]@\n----@]@." Pretty.print_term f;
  end;
644
  f
645

646
and wp_desc env e q xq = match e.e_node with
647 648 649
  | Elogic t ->
      let v, q = open_post q in
      let t = wp_label e t in
650 651 652
      (* NOTE: if you replace this t_subst by t_let or anything else,
         you must handle separately the case "let mark = 'now in ...",
         which requires 'now to be substituted for mark in q *)
653 654 655 656
      if ty_equal v.vs_ty ty_mark then
        t_subst_single v (to_term t) q
      else
        t_let_close_simp v (to_term t) q
Andrei Paskevich's avatar
Andrei Paskevich committed
657 658 659
  | Evalue pv ->
      let v, q = open_post q in
      let t = wp_label e (t_var pv.pv_vs) in
660
      t_subst_single v t q
661 662 663
  | Earrow _ ->
      let q = open_unit_post q in
      (* wp_label e *) q (* FIXME? *)
664
  | Elet ({ let_sym = LetV v; let_expr = e1 }, e2)
665
    when Opt.equal Loc.equal v.pv_vs.vs_name.id_loc e1.e_loc ->
666 667 668 669 670
    (* we push the label down, past the implicitly inserted "let" *)
      let w = wp_expr env (e_label_copy e e2) q xq in
      let q = create_post v.pv_vs w in
      wp_expr env e1 q xq
  | Elet ({ let_sym = LetV v; let_expr = e1 }, e2) ->
671
      let w = wp_expr env e2 q xq in
672
      let q = create_post v.pv_vs w in
673
      wp_label e (wp_expr env e1 q xq)
674 675 676 677
  | Elet ({ let_sym = LetA _; let_expr = e1 }, e2) ->
      let w = wp_expr env e2 q xq in
      let q = create_unit_post w in
      wp_label e (wp_expr env e1 q xq)
678 679
  | Erec (fdl, e1) ->
      let fr = wp_rec_defn env fdl in
680 681 682
      let fe = wp_expr env e1 q xq in
      let fr = wp_ands ~sym:true fr in
      wp_label e (wp_and ~sym:true fr fe)
683 684 685 686 687 688 689 690 691 692
  | Eif (e1, e2, e3) ->
      let res = vs_result e1 in
      let test = t_equ (t_var res) t_bool_true in
      (* if both branches are pure, do not split *)
      let w =
        let get_term e = match e.e_node with
          | Elogic t -> to_term t
          | Evalue v -> t_var v.pv_vs
          | _ -> raise Exit in
        try
693 694
          let r2 = wp_label e2 (get_term e2) in
          let r3 = wp_label e3 (get_term e3) in
695 696 697 698 699 700 701 702 703
          let v, q = open_post q in
          t_subst_single v (t_if_simp test r2 r3) q
        with Exit ->
          let w2 = wp_expr env e2 q xq in
          let w3 = wp_expr env e3 q xq in
          t_if_simp test w2 w3
      in
      let q = create_post res w in
      wp_label e (wp_expr env e1 q xq)
704 705 706 707 708 709 710 711 712 713 714
  (* optimization for the particular case let _ = e1 in e2 *)
  | Ecase (e1, [{ ppat_pattern = { pat_node = Term.Pwild }}, e2]) ->
      let w = wp_expr env e2 q xq in
      let q = create_post (vs_result e1) w in
      wp_label e (wp_expr env e1 q xq)
  (* optimization for the particular case let () = e1 in e2 *)
  | Ecase (e1, [{ ppat_pattern = { pat_node = Term.Papp (cs,[]) }}, e2])
    when ls_equal cs fs_void ->
      let w = wp_expr env e2 q xq in
      let q = create_unit_post w in
      wp_label e (wp_expr env e1 q xq)
715 716 717 718
  | Ecase (e1, bl) ->
      let res = vs_result e1 in
      let branch ({ ppat_pattern = pat }, e) =
        t_close_branch pat (wp_expr env e q xq) in
719
      let w = t_case_simp (t_var res) (List.map branch bl) in
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
      let q = create_post res w in
      wp_label e (wp_expr env e1 q xq)
  | Eghost e1 ->
      wp_label e (wp_expr env e1 q xq)
  | Eraise (xs, e1) ->
      let q = try Mexn.find xs xq with
        Not_found -> assert false in
      wp_label e (wp_expr env e1 q xq)
  | Etry (e1, bl) ->
      let branch (xs,v,e) acc =
        let w = wp_expr env e q xq in
        let q = create_post v.pv_vs w in
        Mexn.add xs q acc in
      let xq = List.fold_right branch bl xq in
      wp_label e (wp_expr env e1 q xq)
735 736
  | Eassert (Aassert, f) ->
      let q = open_unit_post q in
737
      let f = wp_expl expl_assert f in
738 739 740
      wp_and ~sym:false (wp_label e f) q
  | Eassert (Acheck, f) ->
      let q = open_unit_post q in
741
      let f = wp_expl expl_check f in
742 743 744
      wp_and ~sym:true (wp_label e f) q
  | Eassert (Aassume, f) ->
      let q = open_unit_post q in
745
      let f = wp_expl expl_assume f in
746
      wp_implies (wp_label e f) q
Andrei Paskevich's avatar
Andrei Paskevich committed
747
  | Eabsurd ->
748
      wp_label e (wp_expl expl_absurd t_absurd)
749 750
  | Eany spec ->
      let p = wp_label e (wp_expl expl_pre spec.c_pre) in
Andrei Paskevich's avatar
Andrei Paskevich committed
751 752
      let p = t_label ?loc:e.e_loc p.t_label p in
      (* TODO: propagate call labels into tyc.c_post *)
753
      let w = wp_abstract "any" env spec.c_effect spec.c_post spec.c_xpost q xq in
754
      wp_and ~sym:false p w
755 756
  | Eapp (e1,_,spec) ->
      let p = wp_label e (wp_expl expl_pre spec.c_pre) in
757
      let p = t_label ?loc:e.e_loc p.t_label p in
758
      let d =
759 760
        if spec.c_letrec = 0 || spec.c_variant = [] then t_true else
        let olds = Mint.find_def [] spec.c_letrec env.letrec_var in
761
        if olds = [] then t_true (* we are out of letrec *) else
762
        decrease e.e_loc expl_variant env olds spec.c_variant in
763
      (* TODO: propagate call labels into tyc.c_post *)
764
      let w = wp_abstract ~loc:e1.e_loc "call" env spec.c_effect spec.c_post spec.c_xpost q xq in
765
      let w = wp_and ~sym:true d (wp_and ~sym:false p w) in
766 767
      let q = create_unit_post w in
      wp_expr env e1 q xq (* FIXME? should (wp_label e) rather be here? *)
768 769
  | Eabstr (e1, spec) ->
      let p = wp_label e (wp_expl expl_pre spec.c_pre) in
770 771 772 773 774 775
      (* every exception uncovered in spec is passed to xq *)
      let c_xq = Mexn.set_union spec.c_xpost xq in
      let w1 = backstep (wp_expr env e1) spec.c_post c_xq in
      (* so that now we don't need to prove these exceptions again *)
      let lost = Mexn.set_diff (exns_of_raises e1.e_effect) spec.c_xpost in
      let c_eff = Sexn.fold_left eff_remove_raise e1.e_effect lost in
776
      let w2 = wp_abstract ~loc:e1.e_loc "abstract" env c_eff spec.c_post spec.c_xpost q xq in
777
      wp_and ~sym:false p (wp_and ~sym:true (wp_label e w1) w2)
778 779 780 781 782 783 784 785
  | Eassign (pl, e1, reg, pv) ->
      (* if we create an intermediate variable npv to represent e1
         in the post-condition of the assign, the call to wp_abstract
         will have to update this variable separately (in addition to
         all existing variables in q that require update), creating
         duplication.  To avoid it, we try to detect whether the value
         of e1 can be represented by an existing pure term that can
         be reused in the post-condition. *)
786 787
      let rec get_term d = match d.e_node with
        | Eghost e | Elet (_,e) | Erec (_,e) -> get_term e
788 789 790
        | Evalue v -> vs_result e1, t_var v.pv_vs
        | Elogic t -> vs_result e1, t
        | _ ->
791
            let ity = ity_of_expr e1 in
792 793
            let id = id_fresh ?loc:e1.e_loc "o" in
            (* must be a pvsymbol or restore_pv will fail *)
794
            let npv = create_pvsymbol id ~ghost:e1.e_ghost ity in
795
            npv.pv_vs, t_var npv.pv_vs
796
      in
797 798 799
      let res, t = get_term e1 in
      let t = fs_app pl.pl_ls [t] pv.pv_vs.vs_ty in
      let c_q = create_unit_post (t_equ t (t_var pv.pv_vs)) in
800
      let eff = eff_write eff_empty reg in
801
      let w = wp_abstract "assign" env eff c_q Mexn.empty q xq in
802
      let q = create_post res w in
803
      wp_label e (wp_expr env e1 q xq)
Andrei Paskevich's avatar
Andrei Paskevich committed
804 805 806
  | Eloop (inv, varl, e1) ->
      (* TODO: what do we do about well-foundness? *)
      let i = wp_expl expl_loop_keep inv in
807
      let olds = List.map (fun (t,_) -> t_at_old t) varl in
808 809 810 811
      let i = if varl = [] then i else
        let d = decrease e.e_loc expl_loopvar env olds varl in
        wp_and ~sym:true i d in
      let q = create_unit_post i in
Andrei Paskevich's avatar
Andrei Paskevich committed
812
      let w = backstep (wp_expr env e1) q xq in
Andrei Paskevich's avatar
Andrei Paskevich committed
813
      let regs = regs_of_writes e1.e_effect in
814
      let w = quantify ~loc:e1.e_loc "loop"  env regs (wp_implies inv w) in
Andrei Paskevich's avatar
Andrei Paskevich committed
815 816
      let i = wp_expl expl_loop_init inv in
      wp_label e (wp_and ~sym:true i w)
Andrei Paskevich's avatar
Andrei Paskevich committed
817 818 819 820 821 822 823 824
  | Efor ({pv_vs = x}, ({pv_vs = v1}, d, {pv_vs = v2}), inv, e1) ->
      (* wp(for x = v1 to v2 do inv { I(x) } e1, Q, R) =
             v1 > v2  -> Q
         and v1 <= v2 ->     I(v1)
                         and forall S. forall i. v1 <= i <= v2 ->
                                                 I(i) -> wp(e1, I(i+1), R)
                                       and I(v2+1) -> Q *)
      let gt, le, incr = match d with
825 826 827 828
        | Mlw_expr.To     -> env.ps_int_gt, env.ps_int_le, env.fs_int_pl
        | Mlw_expr.DownTo -> env.ps_int_lt, env.ps_int_ge, env.fs_int_mn
      in
      let one = t_nat_const 1 in
Andrei Paskevich's avatar
Andrei Paskevich committed
829 830 831 832
      let v1_gt_v2 = ps_app gt [t_var v1; t_var v2] in
      let v1_le_v2 = ps_app le [t_var v1; t_var v2] in
      let q = open_unit_post q in
      let wp_init =
833
        wp_expl expl_loop_init (t_subst_single x (t_var v1) inv) in
Andrei Paskevich's avatar
Andrei Paskevich committed
834
      let wp_step =
835
        let next = fs_app incr [t_var x; one] ty_int in
836 837
        let post = wp_expl expl_loop_keep (t_subst_single x next inv) in
        wp_expr env e1 (create_unit_post post) xq in
Andrei Paskevich's avatar
Andrei Paskevich committed
838
      let wp_last =
839
        let v2pl1 = fs_app incr [t_var v2; one] ty_int in
Andrei Paskevich's avatar
Andrei Paskevich committed
840 841 842
        wp_implies (t_subst_single x v2pl1 inv) q in
      let wp_good = wp_and ~sym:true
        wp_init
843
        (quantify "loop" env (regs_of_writes e1.e_effect)
Andrei Paskevich's avatar
Andrei Paskevich committed
844
           (wp_and ~sym:true
845
              (wp_forall [x] (wp_implies
Andrei Paskevich's avatar
Andrei Paskevich committed
846 847
                (wp_and ~sym:true (ps_app le [t_var v1; t_var x])
                                  (ps_app le [t_var x;  t_var v2]))
848
                (wp_implies inv wp_step)))
Andrei Paskevich's avatar
Andrei Paskevich committed
849 850 851 852 853 854 855
              wp_last))
      in
      let wp_full = wp_and ~sym:true
        (wp_implies v1_gt_v2 q)
        (wp_implies v1_le_v2 wp_good)
      in
      wp_label e wp_full
856

857
and wp_abstract ?loc:(l=None) model env c_eff c_q c_xq q xq =
Andrei Paskevich's avatar
Andrei Paskevich committed
858 859 860 861 862 863
  let regs = regs_of_writes c_eff in
  let exns = exns_of_raises c_eff in
  let quantify_post c_q q =
    let v, f = open_post q in
    let c_v, c_f = open_post c_q in
    let c_f = t_subst_single c_v (t_var v) c_f in
864
    let f = wp_forall_post v c_f f in
865
    quantify ~loc:l model env regs f
Andrei Paskevich's avatar
Andrei Paskevich committed
866 867 868 869 870 871
  in
  let quantify_xpost _ c_xq xq =
    Some (quantify_post c_xq xq) in
  let proceed c_q c_xq =
    let f = quantify_post c_q q in
    (* every xs in exns is guaranteed to be in c_xq and xq *)
872 873
    assert (Mexn.set_submap exns xq);
    assert (Mexn.set_submap exns c_xq);
Andrei Paskevich's avatar
Andrei Paskevich committed
874 875 876
    let xq = Mexn.set_inter xq exns in
    let c_xq = Mexn.set_inter c_xq exns in
    let mexn = Mexn.inter quantify_xpost c_xq xq in
877
    (* FIXME? This wp_ands is asymmetric in Pgm_wp *)