# HG changeset patch # User wenzelm # Date 1515776283 -3600 # Node ID 64d928bacddd818dd39e9926ccca688d348b6e0d # Parent 060efe5321898ae789b0fb9e699beab037d25bec prefer formal comments; diff -r 060efe532189 -r 64d928bacddd src/HOL/Hoare/Examples.thy --- a/src/HOL/Hoare/Examples.thy Fri Jan 12 17:14:34 2018 +0100 +++ b/src/HOL/Hoare/Examples.thy Fri Jan 12 17:58:03 2018 +0100 @@ -1,15 +1,15 @@ (* Title: HOL/Hoare/Examples.thy Author: Norbert Galm Copyright 1998 TUM +*) -Various examples. -*) +chapter \Various examples\ theory Examples imports Hoare_Logic Arith2 begin -(*** ARITHMETIC ***) +section \ARITHMETIC\ -(** multiplication by successive addition **) +subsection \multiplication by successive addition\ lemma multiply_by_add: "VARS m s a b {a=A & b=B} @@ -53,7 +53,8 @@ apply (auto simp add:int_distrib) done -(** Euclid's algorithm for GCD **) + +subsection \Euclid's algorithm for GCD\ lemma Euclid_GCD: "VARS a b {0 \Now prove the verification conditions\ apply auto apply(simp add: gcd_diff_r less_imp_le) apply(simp add: linorder_not_less gcd_diff_l) @@ -78,19 +79,22 @@ DO IF a max A B + 2}" apply vcg -(*Now prove the verification conditions*) + \ \Now prove the verification conditions\ apply auto apply(simp add: gcd_diff_r less_imp_le) apply(simp add: linorder_not_less gcd_diff_l) apply(erule gcd_nnn) done -(** Dijkstra's extension of Euclid's algorithm for simultaneous GCD and SCM **) -(* From E.W. Disjkstra. Selected Writings on Computing, p 98 (EWD474), - where it is given without the invariant. Instead of defining scm - explicitly we have used the theorem scm x y = x*y/gcd x y and avoided - division by mupltiplying with gcd x y. -*) + +subsection \Dijkstra's extension of Euclid's algorithm for simultaneous GCD and SCM\ + +text \ + From E.W. Disjkstra. Selected Writings on Computing, p 98 (EWD474), + where it is given without the invariant. Instead of defining \scm\ + explicitly we have used the theorem \scm x y = x * y / gcd x y\ and avoided + division by mupltiplying with \gcd x y\. +\ lemmas distribs = diff_mult_distrib diff_mult_distrib2 add_mult_distrib add_mult_distrib2 @@ -107,7 +111,8 @@ apply(simp add: distribs gcd_nnn) done -(** Power by iterated squaring and multiplication **) + +subsection \Power by iterated squaring and multiplication\ lemma power_by_mult: "VARS a b c {a=A & b=B} @@ -126,7 +131,8 @@ apply simp done -(** Factorial **) + +subsection \Factorial\ lemma factorial: "VARS a b {a=A} @@ -178,9 +184,10 @@ apply arith done -(** Square root **) -(* the easy way: *) +subsection \Square root\ + +\ \the easy way:\ lemma sqrt: "VARS r x {True} @@ -202,8 +209,7 @@ apply vcg_simp done -(* without multiplication *) - +\ \without multiplication\ lemma sqrt_without_multiplication: "VARS u w r {x=X} u := 1; w := 1; r := (0::nat); @@ -215,7 +221,7 @@ done -(*** LISTS ***) +section \LISTS\ lemma imperative_reverse: "VARS y x {x=X} @@ -270,9 +276,10 @@ done -(*** ARRAYS ***) +section \ARRAYS\ -(* Search for a key *) +subsection \Search for a key\ + lemma zero_search: "VARS A i {True} i := 0; @@ -297,15 +304,15 @@ apply(blast elim!: less_SucE) done -(* -The `partition' procedure for quicksort. -`A' is the array to be sorted (modelled as a list). -Elements of A must be of class order to infer at the end -that the elements between u and l are equal to pivot. +text \ + The \partition\ procedure for quicksort. + \<^item> \A\ is the array to be sorted (modelled as a list). + \<^item> Elements of \A\ must be of class order to infer at the end + that the elements between u and l are equal to pivot. -Ambiguity warnings of parser are due to := being used -both for assignment and list update. -*) + Ambiguity warnings of parser are due to \:=\ being used + both for assignment and list update. +\ lemma lem: "m - Suc 0 < n ==> m < Suc n" by arith @@ -327,7 +334,7 @@ IF l <= u THEN A := A[l := A!u, u := A!l] ELSE SKIP FI OD {leq A u & (!k. u A!k = pivot) & geq A l}" -(* expand and delete abbreviations first *) +\ \expand and delete abbreviations first\ apply (simp) apply (erule thin_rl)+ apply vcg_simp diff -r 060efe532189 -r 64d928bacddd src/HOL/Hoare/Hoare_Logic_Abort.thy --- a/src/HOL/Hoare/Hoare_Logic_Abort.thy Fri Jan 12 17:14:34 2018 +0100 +++ b/src/HOL/Hoare/Hoare_Logic_Abort.thy Fri Jan 12 17:58:03 2018 +0100 @@ -61,7 +61,7 @@ \[(@{const_syntax Valid}, K (Hoare_Syntax.spec_tr' @{syntax_const "_hoare_abort"}))]\ -(*** The proof rules ***) +section \The proof rules\ lemma SkipRule: "p \ q \ Valid p (Basic id) q" by (auto simp:Valid_def) @@ -115,17 +115,18 @@ SIMPLE_METHOD' (Hoare.hoare_tac ctxt (asm_full_simp_tac ctxt)))\ "verification condition generator plus simplification" -(* Special syntax for guarded statements and guarded array updates: *) - +\ \Special syntax for guarded statements and guarded array updates:\ syntax "_guarded_com" :: "bool \ 'a com \ 'a com" ("(2_ \/ _)" 71) "_array_update" :: "'a list \ nat \ 'a \ 'a com" ("(2_[_] :=/ _)" [70, 65] 61) translations "P \ c" == "IF P THEN c ELSE CONST Abort FI" "a[i] := v" => "(i < CONST length a) \ (a := CONST list_update a i v)" - (* reverse translation not possible because of duplicate "a" *) + \ \reverse translation not possible because of duplicate \a\\ -text\Note: there is no special syntax for guarded array access. Thus -you must write \j < length a \ a[i] := a!j\.\ +text \ + Note: there is no special syntax for guarded array access. Thus + you must write \j < length a \ a[i] := a!j\. +\ end diff -r 060efe532189 -r 64d928bacddd src/HOL/Hoare/SchorrWaite.thy --- a/src/HOL/Hoare/SchorrWaite.thy Fri Jan 12 17:14:34 2018 +0100 +++ b/src/HOL/Hoare/SchorrWaite.thy Fri Jan 12 17:58:03 2018 +0100 @@ -203,20 +203,20 @@ t := root; p := Null; WHILE p \ Null \ t \ Null \ \ t^.m INV {\stack. - List (S c l r) p stack \ (*i1*) - (\x \ set stack. m x) \ (*i2*) - R = reachable (relS{l, r}) {t,p} \ (*i3*) - (\x. x \ R \ \m x \ (*i4*) + List (S c l r) p stack \ \ \\i1\\ + (\x \ set stack. m x) \ \ \\i2\\ + R = reachable (relS{l, r}) {t,p} \ \ \\i3\\ + (\x. x \ R \ \m x \ \ \\i4\\ x \ reachable (relS{l,r}|m) ({t}\set(map r stack))) \ - (\x. m x \ x \ R) \ (*i5*) - (\x. x \ set stack \ r x = iR x \ l x = iL x) \ (*i6*) - (stkOk c l r iL iR t stack) (*i7*) } + (\x. m x \ x \ R) \ \ \\i5\\ + (\x. x \ set stack \ r x = iR x \ l x = iL x) \ \ \\i6\\ + (stkOk c l r iL iR t stack) \ \\i7\\} DO IF t = Null \ t^.m THEN IF p^.c - THEN q := t; t := p; p := p^.r; t^.r := q (*pop*) - ELSE q := t; t := p^.r; p^.r := p^.l; (*swing*) + THEN q := t; t := p; p := p^.r; t^.r := q \ \\pop\\ + ELSE q := t; t := p^.r; p^.r := p^.l; \ \\swing\\ p^.l := q; p^.c := True FI - ELSE q := p; p := t; t := t^.l; p^.l := q; (*push*) + ELSE q := p; p := t; t := t^.l; p^.l := q; \ \\push\\ p^.m := True; p^.c := False FI OD {(\x. (x \ R) = m x) \ (r = iR \ l = iL) }" (is "VARS c m l r t p q root {?Pre c m l r root} (?c1; ?c2; ?c3) {?Post c m l r}") diff -r 060efe532189 -r 64d928bacddd src/HOL/Hoare/Separation.thy --- a/src/HOL/Hoare/Separation.thy Fri Jan 12 17:14:34 2018 +0100 +++ b/src/HOL/Hoare/Separation.thy Fri Jan 12 17:58:03 2018 +0100 @@ -156,7 +156,7 @@ proofs. Here comes a simple example of a program proof that uses a law of separation logic instead.\ -(* a law of separation logic *) +\ \a law of separation logic\ lemma star_comm: "P ** Q = Q ** P" by(auto simp add:star_def ortho_def dest: map_add_comm)