wp2.mlw 12.2 KB
Newer Older
MARCHE Claude's avatar
MARCHE Claude committed
1

2 3 4
(** {1 A certified WP calculus} *)

(** {2 A simple imperative language, syntax and semantics} *)
MARCHE Claude's avatar
MARCHE Claude committed
5 6 7

theory Imp

8
(** terms and formulas *)
MARCHE Claude's avatar
MARCHE Claude committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

type datatype = Tint | Tbool

type operator = Oplus | Ominus | Omult | Ole

type ident = int

type term =
  | Tconst int
  | Tvar ident
  | Tderef ident
  | Tbin term operator term

type fmla =
  | Fterm term
  | Fand fmla fmla
  | Fnot fmla
  | Fimplies fmla fmla
  | Flet ident term fmla
  | Fforall ident datatype fmla

use import int.Int
use import bool.Bool

type value =
  | Vint int
  | Vbool bool

use map.Map as IdMap
type env = IdMap.map ident value

40
(** semantics of formulas *)
MARCHE Claude's avatar
MARCHE Claude committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

function eval_bin (x:value) (op:operator) (y:value) : value =
  match x,y with
  | Vint x,Vint y ->
     match op with
     | Oplus -> Vint (x+y)
     | Ominus -> Vint (x-y)
     | Omult -> Vint (x*y)
     | Ole -> Vbool (if x <= y then True else False)
     end
  | _,_ -> Vbool False
  end

function get_env (i:ident) (e:env) : value = IdMap.get e i

function eval_term (sigma:env) (pi:env) (t:term) : value =
  match t with
  | Tconst n -> Vint n
  | Tvar id -> get_env id pi
  | Tderef id -> get_env id sigma
  | Tbin t1 op t2 ->
     eval_bin (eval_term sigma pi t1) op (eval_term sigma pi t2)
  end

predicate eval_fmla (sigma:env) (pi:env) (f:fmla) =
  match f with
  | Fterm t -> eval_term sigma pi t = Vbool True
  | Fand f1 f2 -> eval_fmla sigma pi f1 /\ eval_fmla sigma pi f2
  | Fnot f -> not (eval_fmla sigma pi f)
  | Fimplies f1 f2 -> eval_fmla sigma pi f1 -> eval_fmla sigma pi f2
  | Flet x t f ->
      eval_fmla sigma (IdMap.set pi x (eval_term sigma pi t)) f
  | Fforall x Tint f ->
     forall n:int. eval_fmla sigma (IdMap.set pi x (Vint n)) f
  | Fforall x Tbool f ->
     forall b:bool.
        eval_fmla sigma (IdMap.set pi x (Vbool b)) f
  end

80 81
(** substitution of a reference `r` by a logic variable `v`
   warning: proper behavior only guaranted if `v` is fresh *)
MARCHE Claude's avatar
MARCHE Claude committed
82

83
let rec function subst_term (e:term) (r:ident) (v:ident) : term =
MARCHE Claude's avatar
MARCHE Claude committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  match e with
  | Tconst _ -> e
  | Tvar _ -> e
  | Tderef x -> if r=x then Tvar v else e
  | Tbin e1 op e2 -> Tbin (subst_term e1 r v) op (subst_term e2 r v)
  end

predicate fresh_in_term (id:ident) (t:term) =
  match t with
  | Tconst _ -> true
  | Tvar v -> id <> v
  | Tderef _ -> true
  | Tbin t1 _ t2 -> fresh_in_term id t1 /\ fresh_in_term id t2
  end

lemma eval_subst_term:
  forall sigma pi:env, e:term, x:ident, v:ident.
    fresh_in_term v e ->
    eval_term sigma pi (subst_term e x v) =
    eval_term (IdMap.set sigma x (IdMap.get pi v)) pi e

lemma eval_term_change_free :
  forall t:term, sigma pi:env, id:ident, v:value.
    fresh_in_term id t ->
    eval_term sigma (IdMap.set pi id v) t = eval_term sigma pi t

predicate fresh_in_fmla (id:ident) (f:fmla) =
  match f with
  | Fterm e -> fresh_in_term id e
MARCHE Claude's avatar
MARCHE Claude committed
113
  | Fand f1 f2   | Fimplies f1 f2 ->
MARCHE Claude's avatar
MARCHE Claude committed
114 115 116
      fresh_in_fmla id f1 /\ fresh_in_fmla id f2
  | Fnot f -> fresh_in_fmla id f
  | Flet y t f -> id <> y /\ fresh_in_term id t /\ fresh_in_fmla id f
117
  | Fforall y _ f -> id <> y /\ fresh_in_fmla id f
MARCHE Claude's avatar
MARCHE Claude committed
118 119
  end

120
let rec function subst (f:fmla) (x:ident) (v:ident) : fmla =
MARCHE Claude's avatar
MARCHE Claude committed
121 122 123 124 125
  match f with
  | Fterm e -> Fterm (subst_term e x v)
  | Fand f1 f2 -> Fand (subst f1 x v) (subst f2 x v)
  | Fnot f -> Fnot (subst f x v)
  | Fimplies f1 f2 -> Fimplies (subst f1 x v) (subst f2 x v)
126
  | Flet y t f -> Flet y (subst_term t x v) (subst f x v)
MARCHE Claude's avatar
MARCHE Claude committed
127 128 129 130 131 132 133 134 135 136 137 138 139
  | Fforall y ty f -> Fforall y ty (subst f x v)
  end


lemma eval_subst:
  forall f:fmla, sigma pi:env, x:ident, v:ident.
    fresh_in_fmla v f ->
    (eval_fmla sigma pi (subst f x v) <->
     eval_fmla (IdMap.set sigma x (IdMap.get pi v)) pi f)

lemma eval_swap:
  forall f:fmla, sigma pi:env, id1 id2:ident, v1 v2:value.
    id1 <> id2 ->
MARCHE Claude's avatar
MARCHE Claude committed
140
    (eval_fmla sigma (IdMap.set (IdMap.set pi id1 v1) id2 v2) f <->
MARCHE Claude's avatar
MARCHE Claude committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    eval_fmla sigma (IdMap.set (IdMap.set pi id2 v2) id1 v1) f)

lemma eval_change_free :
  forall f:fmla, sigma pi:env, id:ident, v:value.
    fresh_in_fmla id f ->
    (eval_fmla sigma (IdMap.set pi id v) f <-> eval_fmla sigma pi f)

(* statements *)

type stmt =
  | Sskip
  | Sassign ident term
  | Sseq stmt stmt
  | Sif term stmt stmt
  | Sassert fmla
  | Swhile term fmla stmt

lemma check_skip:
  forall s:stmt. s=Sskip \/s<>Sskip

161
(** small-steps semantics for statements *)
MARCHE Claude's avatar
MARCHE Claude committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

inductive one_step env env stmt env env stmt =

  | one_step_assign:
      forall sigma pi:env, x:ident, e:term.
        one_step sigma pi (Sassign x e)
                 (IdMap.set sigma x (eval_term sigma pi e)) pi
                 Sskip

  | one_step_seq:
      forall sigma pi sigma' pi':env, i1 i1' i2:stmt.
        one_step sigma pi i1 sigma' pi' i1' ->
          one_step sigma pi (Sseq i1 i2) sigma' pi' (Sseq i1' i2)

  | one_step_seq_skip:
      forall sigma pi:env, i:stmt.
        one_step sigma pi (Sseq Sskip i) sigma pi i

  | one_step_if_true:
      forall sigma pi:env, e:term, i1 i2:stmt.
        eval_term sigma pi e = (Vbool True) ->
          one_step sigma pi (Sif e i1 i2) sigma pi i1

  | one_step_if_false:
      forall sigma pi:env, e:term, i1 i2:stmt.
        eval_term sigma pi e = (Vbool False) ->
          one_step sigma pi (Sif e i1 i2) sigma pi i2

  | one_step_assert:
      forall sigma pi:env, f:fmla.
        eval_fmla sigma pi f ->
          one_step sigma pi (Sassert f) sigma pi Sskip

  | one_step_while_true:
      forall sigma pi:env, e:term, inv:fmla, i:stmt.
        eval_fmla sigma pi inv ->
        eval_term sigma pi e = (Vbool True) ->
          one_step sigma pi (Swhile e inv i) sigma pi (Sseq i (Swhile e inv i))

  | one_step_while_false:
      forall sigma pi:env, e:term, inv:fmla, i:stmt.
        eval_fmla sigma pi inv ->
        eval_term sigma pi e = (Vbool False) ->
          one_step sigma pi (Swhile e inv i) sigma pi Sskip

207
(***
MARCHE Claude's avatar
MARCHE Claude committed
208 209 210 211 212 213 214 215

  lemma progress:
    forall s:state, i:stmt.
      i <> Sskip ->
      exists s':state, i':stmt. one_step s i s' i'

*)

216
 (** many steps of execution *)
MARCHE Claude's avatar
MARCHE Claude committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

 inductive many_steps env env stmt env env stmt int =
   | many_steps_refl:
     forall sigma pi:env, i:stmt. many_steps sigma pi i sigma pi i 0
   | many_steps_trans:
     forall sigma1 pi1 sigma2 pi2 sigma3 pi3:env, i1 i2 i3:stmt, n:int.
       one_step sigma1 pi1 i1 sigma2 pi2 i2 ->
       many_steps sigma2 pi2 i2 sigma3 pi3 i3 n ->
       many_steps sigma1 pi1 i1 sigma3 pi3 i3 (n+1)

lemma steps_non_neg:
  forall sigma1 pi1 sigma2 pi2:env, i1 i2:stmt, n:int.
    many_steps sigma1 pi1 i1 sigma2 pi2 i2 n -> n >= 0

lemma many_steps_seq:
  forall sigma1 pi1 sigma3 pi3:env, i1 i2:stmt, n:int.
    many_steps sigma1 pi1 (Sseq i1 i2) sigma3 pi3 Sskip n ->
    exists sigma2 pi2:env, n1 n2:int.
      many_steps sigma1 pi1 i1 sigma2 pi2 Sskip n1 /\
      many_steps sigma2 pi2 i2 sigma3 pi3 Sskip n2 /\
      n = 1 + n1 + n2


240

MARCHE Claude's avatar
MARCHE Claude committed
241 242
predicate valid_fmla (p:fmla) = forall sigma pi:env. eval_fmla sigma pi p

243
(** {3 Hoare triples} *)
MARCHE Claude's avatar
MARCHE Claude committed
244

245
(** partial correctness *)
MARCHE Claude's avatar
MARCHE Claude committed
246 247 248 249 250
predicate valid_triple (p:fmla) (i:stmt) (q:fmla) =
    forall sigma pi:env. eval_fmla sigma pi p ->
      forall sigma' pi':env, n:int. many_steps sigma pi i sigma' pi' Sskip n ->
        eval_fmla sigma' pi' q

251 252
(*** total correctness *)
(***
MARCHE Claude's avatar
MARCHE Claude committed
253 254 255 256 257 258
predicate total_valid_triple (p:fmla) (i:stmt) (q:fmla) =
    forall s:state. eval_fmla s p ->
      exists s':state, n:int. many_steps s i s' Sskip n /\
        eval_fmla s' q
*)

259 260
end

261

262 263 264
theory TestSemantics

use import Imp
265
use import map.Const
266

267 268
function my_sigma : env = Const.const (Vint 0)
function my_pi : env = Const.const (Vint 42)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

goal Test13 :
  eval_term my_sigma my_pi (Tconst 13) = Vint 13

goal Test42 :
  eval_term my_sigma my_pi (Tvar 0) = Vint 42

goal Test0 :
  eval_term my_sigma my_pi (Tderef 0) = Vint 0

goal Test55 :
  eval_term my_sigma my_pi (Tbin (Tvar 0) Oplus (Tconst 13)) = Vint 55

goal Ass42 :
    let x = 0 in
    forall sigma' pi':env.
      one_step my_sigma my_pi (Sassign x (Tconst 42)) sigma' pi' Sskip ->
        IdMap.get sigma' x = Vint 42

goal If42 :
    let x = 0 in
    forall sigma1 pi1 sigma2 pi2:env, i:stmt.
      one_step my_sigma my_pi
        (Sif (Tbin (Tderef x) Ole (Tconst 10))
             (Sassign x (Tconst 13))
             (Sassign x (Tconst 42)))
        sigma1 pi1 i ->
      one_step sigma1 pi1 i sigma2 pi2 Sskip ->
        IdMap.get sigma2 x = Vint 13

end

301
(** {2 Hoare logic} *)
302 303 304 305 306 307

theory HoareLogic

use import Imp


308
(** Hoare logic rules (partial correctness) *)
MARCHE Claude's avatar
MARCHE Claude committed
309

310 311 312 313 314 315 316
lemma consequence_rule:
  forall p p' q q':fmla, i:stmt.
  valid_fmla (Fimplies p' p) ->
  valid_triple p i q ->
  valid_fmla (Fimplies q q') ->
  valid_triple p' i q'

MARCHE Claude's avatar
MARCHE Claude committed
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 348 349 350 351 352 353 354
lemma skip_rule:
  forall q:fmla. valid_triple q Sskip q

lemma assign_rule:
  forall q:fmla, x id:ident, e:term.
  fresh_in_fmla id q ->
  valid_triple (Flet id e (subst q x id)) (Sassign x e) q

lemma seq_rule:
  forall p q r:fmla, i1 i2:stmt.
  valid_triple p i1 r /\ valid_triple r i2 q ->
  valid_triple p (Sseq i1 i2) q

lemma if_rule:
  forall e:term, p q:fmla, i1 i2:stmt.
  valid_triple (Fand p (Fterm e)) i1 q /\
  valid_triple (Fand p (Fnot (Fterm e))) i2 q ->
  valid_triple p (Sif e i1 i2) q

lemma assert_rule:
  forall f p:fmla. valid_fmla (Fimplies p f) ->
  valid_triple p (Sassert f) p

lemma assert_rule_ext:
  forall f p:fmla.
  valid_triple (Fimplies f p) (Sassert f) p

lemma while_rule:
  forall e:term, inv:fmla, i:stmt.
  valid_triple (Fand (Fterm e) inv) i inv ->
  valid_triple inv (Swhile e inv i) (Fand (Fnot (Fterm e)) inv)

lemma while_rule_ext:
  forall e:term, inv inv':fmla, i:stmt.
  valid_fmla (Fimplies inv' inv) ->
  valid_triple (Fand (Fterm e) inv') i inv' ->
  valid_triple inv' (Swhile e inv i) (Fand (Fnot (Fterm e)) inv')

355
(*** frame rule ? *)
MARCHE Claude's avatar
MARCHE Claude committed
356

357 358
end

359
(** {2 WP calculus} *)
360 361 362 363

module WP

use import Imp
MARCHE Claude's avatar
MARCHE Claude committed
364

365
use set.Fset as Set
MARCHE Claude's avatar
MARCHE Claude committed
366

MARCHE Claude's avatar
MARCHE Claude committed
367
predicate assigns (sigma:env) (a:Set.set ident) (sigma':env) =
MARCHE Claude's avatar
MARCHE Claude committed
368
  forall i:ident. not (Set.mem i a) ->
MARCHE Claude's avatar
MARCHE Claude committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    IdMap.get sigma i = IdMap.get sigma' i

lemma assigns_refl:
  forall sigma:env, a:Set.set ident. assigns sigma a sigma

lemma assigns_trans:
  forall sigma1 sigma2 sigma3:env, a:Set.set ident.
    assigns sigma1 a sigma2 /\ assigns sigma2 a sigma3 ->
    assigns sigma1 a sigma3

lemma assigns_union_left:
  forall sigma sigma':env, s1 s2:Set.set ident.
    assigns sigma s1 sigma' -> assigns sigma (Set.union s1 s2) sigma'

lemma assigns_union_right:
  forall sigma sigma':env, s1 s2:Set.set ident.
    assigns sigma s2 sigma' -> assigns sigma (Set.union s1 s2) sigma'

MARCHE Claude's avatar
MARCHE Claude committed
387

MARCHE Claude's avatar
MARCHE Claude committed
388 389 390 391 392 393 394 395
predicate stmt_writes (i:stmt) (w:Set.set ident) =
  match i with
  | Sskip | Sassert _ -> true
  | Sassign id _ -> Set.mem id w
  | Sseq s1 s2 | Sif _ s1 s2 -> stmt_writes s1 w /\ stmt_writes s2 w
  | Swhile _ _ s -> stmt_writes s w
  end

MARCHE Claude's avatar
MARCHE Claude committed
396

397 398 399 400 401
  let rec compute_writes (s:stmt) : Set.set ident
   ensures {
     forall sigma pi sigma' pi':env, n:int.
       many_steps sigma pi s sigma' pi' Sskip n ->
       assigns sigma result sigma' }
402
   variant { s }
403
  = match s with
MARCHE Claude's avatar
MARCHE Claude committed
404 405 406 407 408 409 410 411
    | Sskip -> Set.empty
    | Sassign i _ -> Set.singleton i
    | Sseq s1 s2 -> Set.union (compute_writes s1) (compute_writes s2)
    | Sif _ s1 s2 -> Set.union (compute_writes s1) (compute_writes s2)
    | Swhile _ _ s -> compute_writes s
    | Sassert _ -> Set.empty
    end

412 413
  val fresh_from_fmla (q:fmla) : ident
    ensures { fresh_in_fmla result q }
MARCHE Claude's avatar
MARCHE Claude committed
414

415 416
  val abstract_effects (i:stmt) (f:fmla) : fmla
    ensures { forall sigma pi:env. eval_fmla sigma pi result ->
MARCHE Claude's avatar
MARCHE Claude committed
417
        eval_fmla sigma pi f /\
418
(***
MARCHE Claude's avatar
MARCHE Claude committed
419 420
        forall sigma':env, w:Set.set ident.
        stmt_writes i w /\ assigns sigma w sigma' ->
421 422 423 424 425
        eval_fmla sigma' pi result
*)
        forall sigma' pi':env, n:int.
           many_steps sigma pi i sigma' pi' Sskip n ->
           eval_fmla sigma' pi' result
MARCHE Claude's avatar
MARCHE Claude committed
426 427
     }

428
  use import HoareLogic
429

430 431
  let rec wp (i:stmt) (q:fmla)
    ensures { valid_triple result i q }
432
    variant { i }
433
  = match i with
MARCHE Claude's avatar
MARCHE Claude committed
434 435 436 437 438 439 440 441 442 443 444 445
    | Sskip -> q
    | Sseq i1 i2 -> wp i1 (wp i2 q)
    | Sassign x e ->
        let id = fresh_from_fmla q in Flet id e (subst q x id)
    | Sif e i1 i2 ->
        Fand (Fimplies (Fterm e) (wp i1 q))
             (Fimplies (Fnot (Fterm e)) (wp i2 q))
    | Sassert f ->
       Fimplies f q (* liberal wp, no termination required *)
       (* Fand f q *) (* strict wp, termination required *)
    | Swhile e inv i ->
        Fand inv
MARCHE Claude's avatar
MARCHE Claude committed
446 447 448
          (abstract_effects i
            (Fand
                (Fimplies (Fand (Fterm e) inv) (wp i inv))
Andrei Paskevich's avatar
Andrei Paskevich committed
449
                (Fimplies (Fand (Fnot (Fterm e)) inv) q)))
MARCHE Claude's avatar
MARCHE Claude committed
450 451 452 453 454 455 456 457

    end


end



458
(***
MARCHE Claude's avatar
MARCHE Claude committed
459
Local Variables:
MARCHE Claude's avatar
MARCHE Claude committed
460
compile-command: "why3ide wp2.mlw"
MARCHE Claude's avatar
MARCHE Claude committed
461 462
End:
*)