(** {1 A certified WP calculus} *) (** {2 A simple imperative language with expressions, syntax and semantics} *) theory ImpExpr use import int.Int use import bool.Bool (** types and values *) type datatype = TYunit | TYint | TYbool type value = Vvoid | Vint int | Vbool bool (** terms and formulas *) type operator = Oplus | Ominus | Omult | Ole type ident = int constant result : ident = (-1) type term = | Tvalue value | 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 map.Map as IdMap type env = IdMap.map ident value (** semantics of formulas *) 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 | Tvalue v -> v | 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 TYint f -> forall n:int. eval_fmla sigma (IdMap.set pi x (Vint n)) f | Fforall x TYbool f -> forall b:bool. eval_fmla sigma (IdMap.set pi x (Vbool b)) f | Fforall x TYunit f -> eval_fmla sigma (IdMap.set pi x Vvoid) f end (** substitution of a reference [r] by a logic variable [v] warning: proper behavior only guaranted if [v] is fresh *) function subst_term (e:term) (r:ident) (v:ident) : term = match e with | Tvalue _ | 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 | Tvalue _ -> 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 | Fand f1 f2 | Fimplies f1 f2 -> 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 | Fforall y ty f -> id <> y /\ fresh_in_fmla id f end function subst (f:fmla) (x:ident) (v:ident) : fmla = 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) | Flet y t f -> Flet y (subst_term t x v) (subst f x v) | 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 -> (eval_fmla sigma (IdMap.set (IdMap.set pi id1 v1) id2 v2) f <-> 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) (* expressions *) type expr = | Evalue value | Ebin expr operator expr | Evar ident | Ederef ident | Eassign ident expr | Eseq expr expr | Elet ident expr expr | Eif expr expr expr | Eassert fmla | Ewhile expr fmla expr constant void : expr = Evalue Vvoid (* lemma check_skip: forall s:stmt. s=Sskip \/s<>Sskip *) (** small-steps semantics for statements *) inductive one_step env env expr env env expr = | one_step_assign_ctxt: forall sigma pi sigma' pi':env, x:ident, e e':expr. one_step sigma pi e sigma' pi' e' -> one_step sigma pi (Eassign x e) sigma' pi' (Eassign x e') | one_step_assign_value: forall sigma pi:env, x:ident, v:value. one_step sigma pi (Eassign x (Evalue v)) (IdMap.set sigma x v) pi void | one_step_seq_ctxt: forall sigma pi sigma' pi':env, e1 e1' e2:expr. one_step sigma pi e1 sigma' pi' e1' -> one_step sigma pi (Eseq e1 e2) sigma' pi' (Eseq e1' e2) | one_step_seq_value: forall sigma pi:env, e:expr. one_step sigma pi (Eseq void e) sigma pi e | one_step_let_ctxt: forall sigma pi sigma' pi':env, id:ident, e1 e1' e2:expr. one_step sigma pi e1 sigma' pi' e1' -> one_step sigma pi (Elet id e1 e2) sigma' pi' (Elet id e1' e2) | one_step_let_value: forall sigma pi:env, id:ident, v:value, e:expr. one_step sigma pi (Elet id (Evalue v) e) sigma (IdMap.set pi id v) e | one_step_if_ctxt: forall sigma pi sigma' pi':env, e1 e1' e2 e3:expr. one_step sigma pi e1 sigma' pi' e1' -> one_step sigma pi (Eif e1 e2 e3) sigma' pi' (Eif e1' e2 e3) | one_step_if_true: forall sigma pi:env, e1 e2:expr. one_step sigma pi (Eif (Evalue (Vbool True)) e1 e2) sigma pi e1 | one_step_if_false: forall sigma pi:env, e1 e2:expr. one_step sigma pi (Eif (Evalue (Vbool False)) e1 e2) sigma pi e2 | one_step_assert: forall sigma pi:env, f:fmla. eval_fmla sigma pi f -> one_step sigma pi (Eassert f) sigma pi void | one_step_while: forall sigma pi:env, e:expr, inv:fmla, e':expr. one_step sigma pi (Ewhile e inv e') sigma pi (Eif e (Eseq e' (Ewhile e inv e')) void) (*** lemma progress: forall s:state, i:expr. i <> Sskip -> exists s':state, i':expr. one_step s i s' i' *) (** many steps of execution *) inductive many_steps env env expr env env expr int = | many_steps_refl: forall sigma pi:env, i:expr. many_steps sigma pi i sigma pi i 0 | many_steps_trans: forall sigma1 pi1 sigma2 pi2 sigma3 pi3:env, i1 i2 i3:expr, 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:expr, n:int. many_steps sigma1 pi1 i1 sigma2 pi2 i2 n -> n >= 0 lemma many_steps_seq: forall sigma1 pi1 sigma3 pi3:env, e1 e2:expr, n:int. many_steps sigma1 pi1 (Eseq e1 e2) sigma3 pi3 void n -> exists sigma2 pi2:env, n1 n2:int. many_steps sigma1 pi1 e1 sigma2 pi2 void n1 /\ many_steps sigma2 pi2 e2 sigma3 pi3 void n2 /\ n = 1 + n1 + n2 lemma many_steps_let: forall sigma1 pi1 sigma3 pi3:env, id:ident, e1 e2:expr, v2:value, n:int. many_steps sigma1 pi1 (Elet id e1 e2) sigma3 pi3 (Evalue v2) n -> exists sigma2 pi2:env, v1:value, n1 n2:int. many_steps sigma1 pi1 e1 sigma2 pi2 (Evalue v1) n1 /\ many_steps sigma2 (IdMap.set pi2 id v1) e2 sigma3 pi3 (Evalue v2) n2 /\ n = 1 + n1 + n2 predicate valid_fmla (p:fmla) = forall sigma pi:env. eval_fmla sigma pi p (** {3 Hoare triples} *) (** partial correctness *) predicate valid_triple (p:fmla) (e:expr) (q:fmla) = forall sigma pi:env. eval_fmla sigma pi p -> forall sigma' pi':env, v:value, n:int. many_steps sigma pi e sigma' pi' (Evalue v) n -> eval_fmla sigma' (IdMap.set pi' result v) q (*** total correctness *) (*** predicate total_valid_triple (p:fmla) (i:expr) (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 *) end theory TestSemantics use import ImpExpr function my_sigma : env = IdMap.const (Vint 0) function my_pi : env = IdMap.const (Vint 42) goal Test13 : eval_term my_sigma my_pi (Tvalue (Vint 13)) = Vint 13 goal Test13expr : many_steps my_sigma my_pi (Evalue (Vint 13)) my_sigma my_pi (Evalue (Vint 13)) 0 goal Test42 : eval_term my_sigma my_pi (Tvar 0) = Vint 42 goal Test42expr : many_steps my_sigma my_pi (Evar 0) my_sigma my_pi (Evalue (Vint 42)) 1 goal Test0 : eval_term my_sigma my_pi (Tderef 0) = Vint 0 goal Test0expr : many_steps my_sigma my_pi (Ederef 0) my_sigma my_pi (Evalue (Vint 0)) 1 goal Test55 : eval_term my_sigma my_pi (Tbin (Tvar 0) Oplus (Tvalue (Vint 13))) = Vint 55 goal Test55expr : many_steps my_sigma my_pi (Ebin (Evar 0) Oplus (Evalue (Vint 13))) my_sigma my_pi (Evalue (Vint 55)) 3 goal Ass42 : let x = 0 in forall sigma' pi':env. one_step my_sigma my_pi (Eassign x (Evalue (Vint 42))) sigma' pi' void -> IdMap.get sigma' x = Vint 42 goal If42 : let x = 0 in forall sigma1 pi1 sigma2 pi2:env, e:expr. one_step my_sigma my_pi (Eif (Ebin (Ederef x) Ole (Evalue (Vint 10))) (Eassign x (Evalue (Vint 13))) (Eassign x (Evalue (Vint 42)))) sigma1 pi1 e -> one_step sigma1 pi1 e sigma2 pi2 void -> IdMap.get sigma2 x = Vint 13 end (** {2 Hoare logic} *) theory HoareLogic use import ImpExpr (** Hoare logic rules (partial correctness) *) lemma consequence_rule: forall p p' q q':fmla, e:expr. valid_fmla (Fimplies p' p) -> valid_triple p e q -> valid_fmla (Fimplies q q') -> valid_triple p' e q' lemma value_rule: forall q:fmla, v:value. fresh_in_fmla result q -> valid_triple q (Evalue v) q lemma assign_rule: forall p q:fmla, x:ident, e:expr. valid_triple p e (subst q x result) -> valid_triple p (Eassign x e) q lemma seq_rule: forall p q r:fmla, e1 e2:expr. valid_triple p e1 r /\ valid_triple r e2 q -> valid_triple p (Eseq e1 e2) q lemma let_rule: forall p q r:fmla, id:ident, e1 e2:expr. fresh_in_fmla result p -> valid_triple p e1 r /\ valid_triple (Flet result (Tvar id) r) e2 q -> valid_triple p (Elet id e1 e2) q (* lemma if_rule: forall e:expr, p q:fmla, i1 i2:expr. valid_triple (Fand p (Fterm e)) i1 q /\ valid_triple (Fand p (Fnot (Fterm e))) i2 q -> valid_triple p (Eif e e1 e2) q *) lemma assert_rule: forall f p:fmla. valid_fmla (Fimplies p f) -> valid_triple p (Eassert f) p lemma assert_rule_ext: forall f p:fmla. valid_triple (Fimplies f p) (Eassert f) p (* lemma while_rule: forall e:term, inv:fmla, i:expr. 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:expr. 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') *) (*** frame rule ? *) end (** {2 WP calculus} *) (* module WP use import Imp use set.Set predicate assigns (sigma:env) (a:Set.set ident) (sigma':env) = forall i:ident. not (Set.mem i a) -> 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' predicate expr_writes (i:expr) (w:Set.set ident) = match i with | Sskip | Sassert _ -> true | Sassign id _ -> Set.mem id w | Sseq s1 s2 | Sif _ s1 s2 -> expr_writes s1 w /\ expr_writes s2 w | Swhile _ _ s -> expr_writes s w end let rec compute_writes (s:expr) : Set.set ident = { } match s with | 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 { forall sigma pi sigma' pi':env, n:int. many_steps sigma pi s sigma' pi' Sskip n -> assigns sigma result sigma' } val fresh_from_fmla (q:fmla) : { } ident { fresh_in_fmla result q } val abstract_effects (i:expr) (f:fmla) : { } fmla { forall sigma pi:env. eval_fmla sigma pi result -> eval_fmla sigma pi f /\ (*** forall sigma':env, w:Set.set ident. expr_writes i w /\ assigns sigma w sigma' -> 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 } use HoareLogic let rec wp (i:expr) (q:fmla) = { true } match i with | 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 (abstract_effects i (Fand (Fimplies (Fand (Fterm e) inv) (wp i inv)) (Fimplies (Fand (Fnot (Fterm e)) inv) q))) end { valid_triple result i q } end *) (*** Local Variables: compile-command: "why3ide wp3.mlw" End: *)