imp_n.why 5.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

theory Imp

(* identifiers *)

type ident

lemma ident_eq_dec : forall i1 i2:ident. i1=i2 \/ i1 <> i2

function mk_ident int : ident

axiom mk_ident_inj: forall i j:int. mk_ident i = mk_ident j -> i=j

(* expressions *)

type operator = Oplus | Ominus | Omult

type expr =
  | Econst int
  | Evar ident
  | Ebin expr operator expr

(* statements *)

type stmt =
  | Sskip
  | Sassign ident expr
  | Sseq stmt stmt
  | Sif expr stmt stmt
  | Swhile expr stmt

lemma check_skip:
33
  forall s:stmt. s=Sskip \/ s<>Sskip
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

(* program states *)

use map.Map as IdMap
use import int.Int

type state = IdMap.map ident int

(* semantics of expressions *)

function eval_bin (x:int) (op:operator) (y:int) : int =
  match op with
    | Oplus -> x+y
    | Ominus -> x-y
    | Omult -> x*y
  end

function eval_expr (s:state) (e:expr) : int =
  match e with
    | Econst n -> n
    | Evar x -> IdMap.get s x
    | Ebin e1 op e2 ->
       eval_bin (eval_expr s e1) op (eval_expr s e2)
  end

59 60
  use map.Const

61
  goal Test13 :
62
    let s = Const.const 0 in
63 64 65 66
    eval_expr s (Econst 13) = 13

  goal Test42 :
    let x = mk_ident 0 in
67
    let s = IdMap.set (Const.const 0) x 42 in
68 69 70 71
    eval_expr s (Evar x) = 42

  goal Test55 :
    let x = mk_ident 0 in
72
    let s = IdMap.set (Const.const 0) x 42 in
73 74 75 76 77 78 79 80 81 82 83 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 113 114 115
    eval_expr s (Ebin (Evar x) Oplus (Econst 13)) = 55


(* small-steps semantics for statements *)

inductive one_step state stmt state stmt =

  | one_step_assign:
      forall s:state, x:ident, e:expr.
        one_step s (Sassign x e)
                 (IdMap.set s x (eval_expr s e)) Sskip

  | one_step_seq:
      forall s s':state, i1 i1' i2:stmt.
        one_step s i1 s' i1' ->
          one_step s (Sseq i1 i2) s' (Sseq i1' i2)

  | one_step_seq_skip:
      forall s:state, i:stmt.
        one_step s (Sseq Sskip i) s i

  | one_step_if_true:
      forall s:state, e:expr, i1 i2:stmt.
        eval_expr s e <> 0 ->
          one_step s (Sif e i1 i2) s i1

  | one_step_if_false:
      forall s:state, e:expr, i1 i2:stmt.
        eval_expr s e = 0 ->
          one_step s (Sif e i1 i2) s i2

  | one_step_while_true:
      forall s:state, e:expr, i:stmt.
        eval_expr s e <> 0 ->
          one_step s (Swhile e i) s (Sseq i (Swhile e i))

  | one_step_while_false:
      forall s:state, e:expr, i:stmt.
        eval_expr s e = 0 ->
          one_step s (Swhile e i) s Sskip

  goal Ass42 :
    let x = mk_ident 0 in
116
    let s = Const.const 0 in
117 118 119 120 121 122
    forall s':state.
      one_step s (Sassign x (Econst 42)) s' Sskip ->
        IdMap.get s' x = 42

  goal If42 :
    let x = mk_ident 0 in
123
    let s = Const.const 0 in
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    forall s1 s2:state, i:stmt.
      one_step s
        (Sif (Evar x)
             (Sassign x (Econst 13))
             (Sassign x (Econst 42)))
        s1 i ->
      one_step s1 i s2 Sskip ->
        IdMap.get s2 x = 42

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


 (* many steps of execution *)

 inductive many_steps state stmt state stmt int =
   | many_steps_refl:
     forall s:state, i:stmt. many_steps s i s i 0
   | many_steps_trans:
     forall s1 s2 s3:state, i1 i2 i3:stmt, n:int.
       one_step s1 i1 s2 i2 ->
       many_steps s2 i2 s3 i3 n ->
       many_steps s1 i1 s3 i3 (n+1)

lemma steps_non_neg:
  forall s1 s2:state, i1 i2:stmt, n:int.
    many_steps s1 i1 s2 i2 n -> n >= 0

lemma many_steps_seq:
  forall s1 s3:state, i1 i2:stmt, n:int.
    many_steps s1 (Sseq i1 i2) s3 Sskip n ->
    exists s2:state, n1 n2:int.
      many_steps s1 i1 s2 Sskip n1 /\
      many_steps s2 i2 s3 Sskip n2 /\
      n = 1 + n1 + n2


(* formulas *)

type fmla =
  | Fterm expr
  | Fand fmla fmla
  | Fnot fmla
MARCHE Claude's avatar
MARCHE Claude committed
169
  | Fimplies fmla fmla
170 171 172 173 174 175

predicate eval_fmla (s:state) (f:fmla) =
  match f with
  | Fterm e -> eval_expr s e <> 0
  | Fand f1 f2 ->  eval_fmla s f1 /\ eval_fmla s f2
  | Fnot f ->  not (eval_fmla s f)
MARCHE Claude's avatar
MARCHE Claude committed
176
  | Fimplies f1 f2 ->  eval_fmla s f1 -> eval_fmla s f2
177 178
  end

Andrei Paskevich's avatar
Andrei Paskevich committed
179
(* substitution *)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

function subst_expr (e:expr) (x:ident) (t:expr) : expr =
  match e with
  | Econst _ -> e
  | Evar y -> if x=y then t else e
  | Ebin e1 op e2 -> Ebin (subst_expr e1 x t) op (subst_expr e2 x t)
  end

lemma eval_subst_expr:
  forall s:state, e:expr, x:ident, t:expr.
    eval_expr s (subst_expr e x t) =
      eval_expr (IdMap.set s x (eval_expr s t)) e

function subst (f:fmla) (x:ident) (t:expr) : fmla =
  match f with
  | Fterm e -> Fterm (subst_expr e x t)
  | Fand f1 f2 -> Fand (subst f1 x t) (subst f2 x t)
  | Fnot f -> Fnot (subst f x t)
MARCHE Claude's avatar
MARCHE Claude committed
198
  | Fimplies f1 f2 -> Fimplies (subst f1 x t) (subst f2 x t)
199 200 201 202 203 204
  end

lemma eval_subst:
  forall s:state, f:fmla, x:ident, t:expr.
    eval_fmla s (subst f x t) <-> eval_fmla (IdMap.set s x (eval_expr s t)) f

MARCHE Claude's avatar
MARCHE Claude committed
205 206
predicate valid_fmla (p:fmla) = forall s:state. eval_fmla s p

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
(* Hoare triples *)

predicate valid_triple (p:fmla) (i:stmt) (q:fmla) =
    forall s:state. eval_fmla s p ->
      forall s':state, n:int. many_steps s i s' Sskip n ->
        eval_fmla s' q

(* Hoare logic rules *)

lemma skip_rule:
  forall q:fmla. valid_triple q Sskip q

lemma assign_rule:
  forall q:fmla, x:ident, e:expr.
  valid_triple (subst q x e) (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:expr, 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 while_rule:
  forall e:expr, inv:fmla, i:stmt.
  valid_triple (Fand (Fterm e) inv) i inv ->
  valid_triple inv (Swhile e i) (Fand (Fnot (Fterm e)) inv)

MARCHE Claude's avatar
MARCHE Claude committed
239 240 241 242 243 244 245
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'

246 247 248 249 250 251 252 253 254
end



(*
Local Variables:
compile-command: "why3ide imp_n.why"
End:
*)