author nipkow Wed Oct 14 15:26:31 1998 +0200 (1998-10-14) changeset 5646 7c2ddbaf8b8c parent 5645 b872b209db69 child 5647 4e8837255b87
New many-sorted version.
 src/HOL/Hoare/Examples.ML file | annotate | diff | revisions src/HOL/Hoare/Examples.thy file | annotate | diff | revisions src/HOL/Hoare/Hoare.ML file | annotate | diff | revisions src/HOL/Hoare/Hoare.thy file | annotate | diff | revisions src/HOL/Hoare/List_Examples.ML file | annotate | diff | revisions src/HOL/Hoare/List_Examples.thy file | annotate | diff | revisions src/HOL/Hoare/README.html file | annotate | diff | revisions src/HOL/Hoare/ROOT.ML file | annotate | diff | revisions
```     1.1 --- a/src/HOL/Hoare/Examples.ML	Wed Oct 14 11:51:11 1998 +0200
1.2 +++ b/src/HOL/Hoare/Examples.ML	Wed Oct 14 15:26:31 1998 +0200
1.3 @@ -1,94 +1,178 @@
1.4  (*  Title:      HOL/Hoare/Examples.thy
1.5      ID:         \$Id\$
1.6 -    Author:     Norbert Galm
1.8 -
1.9 -Various arithmetic examples.
1.10 +    Author:     Norbert Galm & Tobias Nipkow
1.12  *)
1.13
1.14 -open Examples;
1.15 +(*** ARITHMETIC ***)
1.16
1.17  (*** multiplication by successive addition ***)
1.18
1.19 -Goal
1.20 - "{m=0 & s=0} \
1.21 -\ WHILE m ~= a DO {s = m*b} s := s+b; m := Suc(m) END\
1.22 -\ {s = a*b}";
1.23 -by (hoare_tac 1);
1.25 +Goal "|- VARS m s. \
1.26 +\  {m=0 & s=0} \
1.27 +\  WHILE m~=a \
1.28 +\  INV {s=m*b} \
1.29 +\  DO s := s+b; m := m+1 OD \
1.30 +\  {s = a*b}";
1.31 +by(hoare_tac (Asm_full_simp_tac) 1);
1.33
1.34 -
1.35  (*** Euclid's algorithm for GCD ***)
1.36
1.37 -Goal
1.38 -" {0<A & 0<B & a=A & b=B}   \
1.39 -\ WHILE a ~= b  \
1.40 -\ DO  {0<a & 0<b & gcd A B = gcd a b} \
1.41 -\      IF a<b   \
1.42 -\      THEN   \
1.43 -\           b:=b-a   \
1.44 -\      ELSE   \
1.45 -\           a:=a-b   \
1.46 -\      END   \
1.47 -\ END   \
1.48 +Goal "|- VARS a b. \
1.49 +\ {0<A & 0<B & a=A & b=B} \
1.50 +\ WHILE  a~=b  \
1.51 +\ INV {0<a & 0<b & gcd A B = gcd a b} \
1.52 +\ DO IF a<b THEN b := b-a ELSE a := a-b FI OD \
1.53  \ {a = gcd A B}";
1.54 +by (hoare_tac (K all_tac) 1);
1.55
1.56 -by (hoare_tac 1);
1.57  (*Now prove the verification conditions*)
1.58  by Auto_tac;
1.59  by (etac gcd_nnn 4);
1.60  by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, gcd_diff_l]) 3);
1.61  by (force_tac (claset(),
1.62 -	       simpset() addsimps [not_less_iff_le, order_le_less]) 2);
1.63 -by (asm_simp_tac (simpset() addsimps [less_imp_le, gcd_diff_r]) 1);
1.64 +               simpset() addsimps [not_less_iff_le, le_eq_less_or_eq]) 2);
1.65 +by (asm_simp_tac (simpset() addsimps [gcd_diff_r,less_imp_le]) 1);
1.66  qed "Euclid_GCD";
1.67
1.68 -
1.69 -(*** Power by interated squaring and multiplication ***)
1.70 +(*** Power by iterated squaring and multiplication ***)
1.71
1.72 -Goal
1.73 -" {a=A & b=B}   \
1.74 -\ c:=1;   \
1.75 -\ WHILE b~=0   \
1.76 -\ DO {A^B = c * a^b}   \
1.77 -\      WHILE b mod 2=0   \
1.78 -\      DO  {A^B = c * a^b}  \
1.79 -\           a:=a*a;   \
1.80 -\           b:=b div 2   \
1.81 -\      END;   \
1.82 -\      c:=c*a;   \
1.83 -\      b:= b - 1 \
1.84 -\ END   \
1.85 +Goal "|- VARS a b c. \
1.86 +\ {a=A & b=B} \
1.87 +\ c := 1; \
1.88 +\ WHILE b ~= 0 \
1.89 +\ INV {A^B = c * a^b} \
1.90 +\ DO  WHILE b mod 2 = 0 \
1.91 +\     INV {A^B = c * a^b} \
1.92 +\     DO  a := a*a; b := b div 2 OD; \
1.93 +\     c := c*a; b := b-1 \
1.94 +\ OD \
1.95  \ {c = A^B}";
1.96 -
1.97 -by (hoare_tac 1);
1.98 -
1.99 +by(hoare_tac (Asm_full_simp_tac) 1);
1.100  by (exhaust_tac "b" 1);
1.101  by (hyp_subst_tac 1);
1.102  by (asm_full_simp_tac (simpset() addsimps [mod_less]) 1);
1.103  by (asm_simp_tac (simpset() addsimps [mult_assoc]) 1);
1.104 -
1.105  qed "power_by_mult";
1.106
1.107 -(*** factorial ***)
1.108 -
1.109 -Goal
1.110 -" {a=A}   \
1.111 -\ b:=1;   \
1.112 -\ WHILE a~=0    \
1.113 -\ DO  {fac A = b*fac a} \
1.114 -\      b:=b*a;   \
1.115 -\      a:=a-1   \
1.116 -\ END   \
1.117 +Goal "|- VARS a b. \
1.118 +\ {a=A} \
1.119 +\ b := 1; \
1.120 +\ WHILE a ~= 0 \
1.121 +\ INV {fac A = b * fac a} \
1.122 +\ DO b := b*a; a := a-1 OD \
1.123  \ {b = fac A}";
1.124 -
1.125 -by (hoare_tac 1);
1.126 +by (hoare_tac Asm_full_simp_tac 1);
1.127  by Safe_tac;
1.128  by (exhaust_tac "a" 1);
1.129  by (ALLGOALS
1.130      (asm_simp_tac
1.132  by (Fast_tac 1);
1.133 +qed"factorial";
1.134
1.135 -qed"factorial";
1.136 +(*** LISTS ***)
1.137 +
1.138 +Goal "|- VARS y x. \
1.139 +\ {x=X} \
1.140 +\ y:=[]; \
1.141 +\ WHILE x ~= [] \
1.142 +\ INV {rev(x)@y = rev(X)} \
1.143 +\ DO y := (hd x # y); x := tl x OD \
1.144 +\ {y=rev(X)}";
1.145 +by (hoare_tac Asm_full_simp_tac 1);
1.146 +by (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
1.147 +by Safe_tac;
1.148 +by (ALLGOALS(Asm_full_simp_tac ));
1.149 +qed "imperative_reverse";
1.150 +
1.151 +Goal
1.152 +"|- VARS x y. \
1.153 +\ {x=X & y=Y} \
1.154 +\ x := rev(x); \
1.155 +\ WHILE x~=[] \
1.156 +\ INV {rev(x)@y = X@Y} \
1.157 +\ DO y := (hd x # y); \
1.158 +\    x := tl x \
1.159 +\ OD \
1.160 +\ {y = X@Y}";
1.161 +by (hoare_tac Asm_full_simp_tac 1);
1.162 +by (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
1.163 +by Safe_tac;
1.164 +by (ALLGOALS(Asm_full_simp_tac));
1.165 +qed "imperative_append";
1.166 +
1.167 +
1.168 +(*** ARRAYS ***)
1.169 +
1.170 +(* Search for 0 *)
1.171 +Goal
1.172 +"|- VARS A i. \
1.173 +\ {True} \
1.174 +\ i := 0; \
1.175 +\ WHILE i < length A & A!i ~= 0 \
1.176 +\ INV {!j. j<i --> A!j ~= 0} \
1.177 +\ DO i := i+1 OD \
1.178 +\ {(i < length A --> A!i = 0) & \
1.179 +\  (i = length A --> (!j. j < length A --> A!j ~= 0))}";
1.180 +by (hoare_tac Asm_full_simp_tac 1);
1.181 +by(blast_tac (claset() addSEs [less_SucE]) 1);
1.182 +qed "zero_search";
1.183 +
1.184 +(*
1.185 +The `partition' procedure for quicksort.
1.186 +`A' is the array to be sorted (modelled as a list).
1.187 +Elements of A must be of class order to infer at the end
1.188 +that the elements between u and l are equal to pivot.
1.189 +
1.190 +Ambiguity warnings of parser are due to := being used
1.191 +both for assignment and list update.
1.192 +*)
1.193 +Goal
1.194 +"[| leq == %A i. !k. k<i --> A!k <= pivot; \
1.195 +\   geq == %A i. !k. i<k & k<length A --> pivot <= A!k |] ==> \
1.196 +\ |- VARS A u l.\
1.197 +\ {0 < length(A::('a::order)list)} \
1.198 +\ l := 0; u := length A - 1; \
1.199 +\ WHILE l <= u \
1.200 +\  INV {leq A l & geq A u & u<length A & l<=length A} \
1.201 +\  DO WHILE l < length A & A!l <= pivot \
1.202 +\      INV {leq A l & geq A u & u<length A & l<=length A} \
1.203 +\      DO l := l+1 OD; \
1.204 +\     WHILE 0 < u & pivot <= A!u \
1.205 +\      INV {leq A l & geq A u  & u<length A & l<=length A} \
1.206 +\      DO u := u-1 OD; \
1.207 +\     IF l <= u THEN A := A[l := A!u, u := A!l] ELSE SKIP FI \
1.208 +\  OD \
1.209 +\ {leq A u & (!k. u<k & k<l --> A!k = pivot) & geq A l}";
1.210 +(* expand and delete abbreviations first *)
1.211 +by(asm_simp_tac HOL_basic_ss 1);
1.212 +by(REPEAT(etac thin_rl 1));
1.213 +by (hoare_tac Asm_full_simp_tac 1);
1.214 +    by(asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
1.215 +    by(Clarify_tac 1);
1.216 +    by(asm_full_simp_tac (simpset() addsimps [nth_list_update]
1.219 +  br conjI 1;
1.220 +   by(Clarify_tac 1);
1.221 +   bd (pred_less_imp_le RS le_imp_less_Suc) 1;
1.222 +   by(blast_tac (claset() addSEs [less_SucE]) 1);
1.223 +  br less_imp_diff_less 1;
1.224 +  by(Blast_tac 1);
1.225 + by(Clarify_tac 1);
1.226 + by(asm_simp_tac (simpset() addsimps [nth_list_update]
1.228 + by(Clarify_tac 1);
1.229 + by(trans_tac 1);
1.230 +by(Clarify_tac 1);
1.231 +by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
1.232 +br conjI 1;
1.233 + by(Clarify_tac 1);
1.234 + br order_antisym 1;
1.235 +  by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
1.236 + by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
1.237 +by(Clarify_tac 1);
1.238 +by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
1.239 +qed "Partition";
```
```     2.1 --- a/src/HOL/Hoare/Examples.thy	Wed Oct 14 11:51:11 1998 +0200
2.2 +++ b/src/HOL/Hoare/Examples.thy	Wed Oct 14 15:26:31 1998 +0200
2.3 @@ -1,9 +1,9 @@
2.4  (*  Title:      HOL/Hoare/Examples.thy
2.5      ID:         \$Id\$
2.6      Author:     Norbert Galm
2.9
2.10 -Various arithmetic examples.
2.11 +Various examples.
2.12  *)
2.13
2.14  Examples = Hoare + Arith2
```
```     3.1 --- a/src/HOL/Hoare/Hoare.ML	Wed Oct 14 11:51:11 1998 +0200
3.2 +++ b/src/HOL/Hoare/Hoare.ML	Wed Oct 14 15:26:31 1998 +0200
3.3 @@ -1,226 +1,210 @@
3.4  (*  Title:      HOL/Hoare/Hoare.ML
3.5      ID:         \$Id\$
3.6 -    Author:     Norbert Galm & Tobias Nipkow
3.8 +    Author:     Leonor Prensa Nieto & Tobias Nipkow
3.10
3.11 -The verification condition generation tactics.
3.12 +Derivation of the proof rules and, most importantly, the VCG tactic.
3.13  *)
3.14
3.15 -open Hoare;
3.16 -
3.17 -(*** Hoare rules ***)
3.18 +(*** The proof rules ***)
3.19
3.20 -val SkipRule = prove_goalw thy [Spec_def,Skip_def]
3.21 -  "(!!s. p(s) ==> q(s)) ==> Spec p Skip q"
3.22 -  (fn prems => [fast_tac (claset() addIs prems) 1]);
3.23 +Goalw [Valid_def] "p <= q ==> Valid p SKIP q";
3.24 +by(Auto_tac);
3.25 +qed "SkipRule";
3.26
3.27 -val AssignRule = prove_goalw thy [Spec_def,Assign_def]
3.28 -  "(!!s. p s ==> q(%x. if x=v then e s else s x)) ==> Spec p (Assign v e) q"
3.29 -  (fn prems => [fast_tac (claset() addIs prems) 1]);
3.30 +Goalw [Valid_def] "p <= {s. (f s):q} ==> Valid p (Basic f) q";
3.31 +by(Auto_tac);
3.32 +qed "BasicRule";
3.33
3.34 -val SeqRule = prove_goalw thy [Spec_def,Seq_def]
3.35 -  "[| Spec p c (%s. q s); Spec (%s. q s) c' r |] ==> Spec p (Seq c c') r"
3.36 -  (fn prems => [cut_facts_tac prems 1, Fast_tac 1]);
3.37 +Goalw [Valid_def] "[| Valid P c1 Q; Valid Q c2 R |] ==> Valid P (c1;c2) R";
3.38 +by(Asm_simp_tac 1);
3.39 +by(Blast_tac 1);
3.40 +qed "SeqRule";
3.41
3.42 -val IfRule = prove_goalw thy [Spec_def,Cond_def]
3.43 -  "[| !!s. p s ==> (b s --> q s) & (~b s --> q' s); \
3.44 -\     Spec (%s. q s) c r; Spec (%s. q' s) c' r |] \
3.45 -\  ==> Spec p (Cond b c c') r"
3.46 -  (fn [prem1,prem2,prem3] =>
3.47 -     [REPEAT (rtac allI 1),
3.48 -      REPEAT (rtac impI 1),
3.49 -      dtac prem1 1,
3.50 -      cut_facts_tac [prem2,prem3] 1,
3.51 -      fast_tac (claset() addIs [prem1]) 1]);
3.52 -
3.53 -val strenthen_pre = prove_goalw thy [Spec_def]
3.54 -  "[| !!s. p s ==> p' s; Spec p' c q |] ==> Spec p c q"
3.55 -  (fn [prem1,prem2] =>[cut_facts_tac [prem2] 1,
3.56 -                       fast_tac (claset() addIs [prem1]) 1]);
3.57 +Goalw [Valid_def]
3.58 + "[| p <= {s. (s:b --> s:w) & (s~:b --> s:w')}; \
3.59 +\    Valid w c1 q; Valid w' c2 q |] \
3.60 +\ ==> Valid p (IF b THEN c1 ELSE c2 FI) q";
3.61 +by(Asm_simp_tac 1);
3.62 +by(Blast_tac 1);
3.63 +qed "CondRule";
3.64
3.65 -val lemma = prove_goalw thy [Spec_def,While_def]
3.66 -  "[| Spec (%s. I s & b s) c I; !!s. [| I s; ~b s |] ==> q s |] \
3.67 -\  ==> Spec I (While b I c) q"
3.68 -  (fn [prem1,prem2] =>
3.69 -     [REPEAT(rtac allI 1), rtac impI 1, etac exE 1, rtac mp 1, atac 2,
3.70 -      etac thin_rl 1, res_inst_tac[("x","s")]spec 1,
3.71 -      res_inst_tac[("x","s'")]spec 1, induct_tac "n" 1,
3.72 -      Simp_tac 1,
3.73 -      fast_tac (claset() addIs [prem2]) 1,
3.74 -      simp_tac (simpset() addsimps [Seq_def]) 1,
3.75 -      cut_facts_tac [prem1] 1, fast_tac (claset() addIs [prem2]) 1]);
3.76 +Goal "! s s'. Sem c s s' --> s : I Int b --> s' : I ==> \
3.77 +\     ! s s'. s : I --> iter n b (Sem c) s s' --> s' : I & s' ~: b";
3.78 +by(induct_tac "n" 1);
3.79 + by(Asm_simp_tac 1);
3.80 +by(Simp_tac 1);
3.81 +by(Blast_tac 1);
3.82 +val lemma = result() RS spec RS spec RS mp RS mp;
3.83
3.84 -val WhileRule = lemma RSN (2,strenthen_pre);
3.85 +Goalw [Valid_def]
3.86 + "[| p <= i; Valid (i Int b) c i; (i Int -b) <= q |] \
3.87 +\ ==> Valid p (WHILE b INV {i} DO c OD) q";
3.88 +by(Asm_simp_tac 1);
3.89 +by(Clarify_tac 1);
3.90 +bd lemma 1;
3.91 +ba 2;
3.92 +by(Blast_tac 1);
3.93 +by(Blast_tac 1);
3.94 +qed "WhileRule";
3.95
3.96 -
3.97 -(*** meta_spec used in StateElimTac ***)
3.98 +(*** The tactics ***)
3.99
3.100 -val meta_spec = prove_goal HOL.thy
3.101 -  "(!!s x. PROP P s x) ==> (!!s. PROP P s (x s))"
3.102 -  (fn prems => [resolve_tac prems 1]);
3.103 -
3.104 -
3.105 -(**************************************************************************************************)
3.106 -(*** Funktion zum Generieren eines Theorems durch Umbennenen von Namen von Lambda-Abstraktionen ***)
3.107 -(*** in einem bestehenden Theorem. Bsp.: "!a.?P(a) ==> ?P(?x)" aus "!x.?P(x) ==> ?P(?x)"        ***)
3.108 -(**************************************************************************************************)
3.109 +(*****************************************************************************)
3.110 +(** The function Mset makes the theorem                                     **)
3.111 +(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
3.112 +(** where (x1,...,xn) are the variables of the particular program we are    **)
3.113 +(** working on at the moment of the call. For instance, (found,x,y) are     **)
3.114 +(** the variables of the Zero Search program.                               **)
3.115 +(*****************************************************************************)
3.116
3.117 -(* rename_abs:term (von:string,nach:string,trm:term) benennt in trm alle Lambda-Abstraktionen
3.118 -   mit Namen von in nach um *)
3.119 +local open HOLogic in
3.120
3.121 -fun rename_abs (von,nach,Abs (s,t,trm)) =
3.122 -    if von=s
3.123 -	then Abs (nach,t,rename_abs (von,nach,trm))
3.124 -        else Abs (s,t,rename_abs (von,nach,trm))
3.125 -  | rename_abs (von,nach,trm1 \$ trm2)   =rename_abs (von,nach,trm1) \$ rename_abs (von,nach,trm2)
3.126 -  | rename_abs (_,_,trm)                =trm;
3.127 +(** maps (%x1 ... xn. t) to [x1,...,xn] **)
3.128 +fun abs2list (Const ("split",_) \$ (Abs(x,T,t))) = Free (x, T)::abs2list t
3.129 +  | abs2list (Abs(x,T,t)) = [Free (x, T)]
3.130 +  | abs2list _ = [];
3.131 +
3.132 +(** maps {(x1,...,xn). t} to [x1,...,xn] **)
3.133 +fun mk_vars (Const ("Collect",_) \$ T) = abs2list T
3.134 +  | mk_vars _ = [];
3.135
3.136 -(* ren_abs_thm:thm (von:string,nach:string,theorem:thm) benennt in theorem alle Lambda-Abstraktionen
3.137 -   mit Namen von in nach um. Vorgehen:
3.138 -        - Term t zu thoerem bestimmen
3.139 -        - Term t' zu t durch Umbenennen der Namen generieren
3.140 -        - Certified Term ct' zu t' erstellen
3.141 -        - Thoerem ct'==ct' anlegen
3.142 -        - Nach der Regel "[|P==Q; P|] ==> Q" wird aus "ct'==ct'" und theorem das Theorem zu ct'
3.143 -          abgeleitet (ist moeglich, da t' mit t unifiziert werden kann, da nur Umnbenennungen) *)
3.144 -
3.145 -fun ren_abs_thm (von,nach,theorem)      =
3.146 -        equal_elim
3.147 -                (reflexive (cterm_of (#sign (rep_thm theorem))
3.148 -			    (rename_abs (von,nach,#prop (rep_thm theorem)))))
3.149 -                theorem;
3.150 -
3.151 +(** abstraction of body over a tuple formed from a list of free variables.
3.152 +Types are also built **)
3.153 +fun mk_abstupleC []     body = absfree ("x", unitT, body)
3.154 +  | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
3.155 +                               in if w=[] then absfree (n, T, body)
3.156 +        else let val z  = mk_abstupleC w body;
3.157 +                 val T2 = case z of Abs(_,T,_) => T
3.158 +                        | Const (_, Type (_,[_, Type (_,[T,_])])) \$ _ => T;
3.159 +       in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT)
3.160 +          \$ absfree (n, T, z) end end;
3.161
3.162 -(****************************************************************************)
3.163 -(*** Taktik zum Anwenden eines Theorems theorem auf ein Subgoal i durch   ***)
3.164 -(***  - Umbenennen von Lambda-Abstraktionen im Theorem                    ***)
3.165 -(***  - Instanziieren von freien Variablen im Theorem                     ***)
3.166 -(***  - Composing des Subgoals mit dem Theorem                            ***)
3.167 -(****************************************************************************)
3.168 +(** maps [x1,...,xn] to (x1,...,xn) and types**)
3.169 +fun mk_bodyC []      = Const ("()", unitT)
3.170 +  | mk_bodyC (x::xs) = if xs=[] then x
3.171 +               else let val (n, T) = dest_Free x ;
3.172 +                        val z = mk_bodyC xs;
3.173 +                        val T2 = case z of Free(_, T) => T
3.174 +                                         | Const ("Pair", Type ("fun", [_, Type
3.175 +                                            ("fun", [_, T])])) \$ _ \$ _ => T;
3.176 +                 in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) \$ x \$ z end;
3.177
3.178 -(* - rens:(string*string) list, d.h. es koennen verschiedene Lambda-Abstraktionen umbenannt werden
3.179 -   - insts:(cterm*cterm) list, d.h. es koennen verschiedene Variablen instanziiert werden *)
3.180 +fun dest_Goal (Const ("Goal", _) \$ P) = P;
3.181
3.182 -fun comp_inst_ren_tac rens insts theorem i      =
3.183 -        let fun compose_inst_ren_tac [] insts theorem i                     =
3.184 -	      compose_tac (false,
3.185 -			   cterm_instantiate insts theorem,nprems_of theorem) i
3.186 -	      | compose_inst_ren_tac ((von,nach)::rl) insts theorem i       =
3.187 -                        compose_inst_ren_tac rl insts
3.188 -			  (ren_abs_thm (von,nach,theorem)) i
3.189 -        in  compose_inst_ren_tac rens insts theorem i  end;
3.190 +(** maps a goal of the form:
3.191 +        1. [| P |] ==> |- VARS x1 ... xn. {._.} _ {._.} or to [x1,...,xn]**)
3.192 +fun get_vars thm = let  val c = dest_Goal (concl_of (thm));
3.193 +                        val d = Logic.strip_assums_concl c;
3.194 +                        val Const _ \$ pre \$ _ \$ _ = dest_Trueprop d;
3.195 +      in mk_vars pre end;
3.196
3.197
3.198 -(***************************************************************    *********)
3.199 -(*** Taktik zum Eliminieren des Zustandes waehrend Hoare-Beweisen                               ***)
3.200 -(***    Bsp.: "!!s. s(Suc(0))=0 --> s(Suc(0))+1=1" wird zu "!!s b. b=0 --> b+1=1"               ***)
3.201 -(****************************************************************************)
3.202 +(** Makes Collect with type **)
3.203 +fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm
3.204 +                      in Collect_const t \$ trm end;
3.205 +
3.206 +fun inclt ty = Const ("op <=", [ty,ty] ---> boolT);
3.207 +
3.208 +(** Makes "Mset <= t" **)
3.209 +fun Mset_incl t = let val MsetT = fastype_of t
3.210 +                 in mk_Trueprop ((inclt MsetT) \$ Free ("Mset", MsetT) \$ t) end;
3.211 +
3.212
3.213 -(* pvars_of_term:term list (name:string,trm:term) gibt die Liste aller Programm-Variablen
3.214 -   aus trm zurueck. name gibt dabei den Namen der Zustandsvariablen an.
3.215 -        Bsp.: bei name="s" und dem Term "s(Suc(Suc(0)))=s(0)" (entspricht "c=a")
3.216 -              wird [0,Suc(Suc(0))] geliefert (Liste ist i.A. unsortiert) *)
3.217 +fun Mset thm = let val vars = get_vars(thm);
3.218 +                   val varsT = fastype_of (mk_bodyC vars);
3.219 +                   val big_Collect = mk_CollectC (mk_abstupleC vars
3.220 +                         (Free ("P",varsT --> boolT) \$ mk_bodyC vars));
3.221 +                   val small_Collect = mk_CollectC (Abs("x",varsT,
3.222 +                           Free ("P",varsT --> boolT) \$ Bound 0));
3.223 +                   val impl = implies \$ (Mset_incl big_Collect) \$
3.224 +                                          (Mset_incl small_Collect);
3.225 +                   val cimpl = cterm_of (#sign (rep_thm thm)) impl
3.226 +   in  prove_goalw_cterm [] cimpl (fn prems =>
3.227 +                              [cut_facts_tac prems 1,Blast_tac 1]) end;
3.228
3.229 -fun pvars_of_term (name,trm)    =
3.230 -  let fun add_vars (name,Free (s,t) \$ trm,vl) =
3.231 -            if name=s then if trm mem vl then vl else trm::vl
3.235 -	| add_vars (_,_,vl)                   =vl
3.236 -  in add_vars (name,trm,[]) end;
3.237 +end;
3.238
3.239
3.240 -(* VarsElimTac: Taktik zum Eliminieren von bestimmten Programmvariablen aus dem Subgoal i
3.241 - - v::vl:(term) list  Liste der zu eliminierenden Programmvariablen
3.242 - - meta_spec:thm      Theorem, welches zur Entfernung der Variablen benutzt wird
3.243 -		      z.B.: "(!!s x. PROP P(s,x)) ==> (!!s. PROP P(s,x(s)))"
3.244 - - namexAll:string    Name von    ^                                  (hier "x")
3.245 - - varx:term          Term zu                                      ^ (hier Var(("x",0),...))
3.246 - - varP:term          Term zu                                  ^     (hier Var(("P",0),...))
3.247 - - type_pvar:typ      Typ der Programmvariablen (d.h. 'a bei 'a prog, z.B.: nat, bool, ...)
3.248 +(*****************************************************************************)
3.249 +(** Simplifying:                                                            **)
3.250 +(** Some useful lemmata, lists and simplification tactics to control which  **)
3.251 +(** theorems are used to simplify at each moment, so that the original      **)
3.252 +(** input does not suffer any unexpected transformation                     **)
3.253 +(*****************************************************************************)
3.254 +
3.255 +val Compl_Collect = prove_goal thy "-(Collect b) = {x. ~(b x)}"
3.256 +    (fn _ => [Fast_tac 1]);
3.257 +
3.258 +(**Simp_tacs**)
3.259
3.260 - Vorgehen:
3.261 -      - eliminiere jede pvar durch Anwendung von comp_inst_ren_tac. Dazu:
3.262 -      - Unbenennung in meta_spec: namexAll wird in den Namen der Prog.-Var. umbenannt
3.263 -	z.B.: fuer die Prog.-Var. mit Namen "a" ergibt sich
3.264 -	      meta_spec zu "(!! s a. PROP P(s,a)) ==> (!! s. PROP P(s,x(s)))"
3.265 -      - Instanziierungen in meta_spec:
3.266 -	      varx wird mit "%s:(type_pvar) state. s(pvar)" instanziiert
3.267 -	      varP wird entsprechend instanziiert. Beispiel fuer Prog.-Var. "a":
3.268 -	 - zu Subgoal "!!s. s(Suc(0)) = s(0) ==> s(0) = 1" bestimme Term ohne "!!s.":
3.269 -		trm0 = "s(Suc(0)) = s(0) ==> s(0) = 1" (s ist hier freie Variable)
3.270 -	 - substituiere alle Vorkommen von s(pvar) durch eine freie Var. xs:
3.271 -		trm1 = "s(Suc(0)) = xs ==> xs = 1"
3.272 -	 - abstrahiere ueber xs:
3.273 -		trm2 = "%xs. s(Suc(0)) = xs ==> xs = 1"
3.274 -	 - abstrahiere ueber restliche Vorkommen von s:
3.275 -		trm3 = "%s xs. s(Suc(0)) = xs ==> xs = 1"
3.276 -	 - instanziiere varP mit trm3
3.277 -*)
3.278 +val before_set2pred_simp_tac =
3.279 +  (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym,Compl_Collect]));
3.280 +
3.281 +val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split]));
3.282 +
3.283 +(*****************************************************************************)
3.284 +(** set2pred transforms sets inclusion into predicates implication,         **)
3.285 +(** maintaining the original variable names.                                **)
3.286 +(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
3.287 +(** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
3.288 +(** are first simplified by "before_set2pred_simp_tac", that returns only   **)
3.289 +(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
3.290 +(** transformed.                                                            **)
3.291 +(** This transformation may solve very easy subgoals due to a ligth         **)
3.292 +(** simplification done by (split_all_tac)                                  **)
3.293 +(*****************************************************************************)
3.294
3.295 -(* StateElimTac: tactic to eliminate all program variable from subgoal i
3.296 -    - applies to subgoals of the form "!!s:('a) state. P(s)",
3.297 -        i.e. the term  Const("all",_) \$ Abs ("s",pvar --> 'a,_)
3.298 -    -   meta_spec has the form "(!!s x. PROP P(s,x)) ==> (!!s. PROP P(s,x(s)))"
3.299 -*)
3.300 +fun set2pred i thm = let fun mk_string [] = ""
3.301 +                           | mk_string (x::xs) = x^" "^mk_string xs;
3.302 +                         val vars=get_vars(thm);
3.303 +                         val var_string = mk_string (map (fst o dest_Free) vars);
3.304 +      in ((before_set2pred_simp_tac i) THEN_MAYBE
3.305 +          (EVERY [rtac subsetI i,
3.306 +                  rtac CollectI i,
3.307 +                  dtac CollectD i,
3.308 +                  (TRY(split_all_tac i)) THEN_MAYBE
3.309 +                  ((rename_tac var_string i) THEN
3.310 +                   (full_simp_tac (HOL_basic_ss addsimps [split]) i)) ])) thm
3.311 +      end;
3.312 +
3.313 +(*****************************************************************************)
3.314 +(** BasicSimpTac is called to simplify all verification conditions. It does **)
3.315 +(** a light simplification by applying "mem_Collect_eq", then it calls      **)
3.316 +(** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
3.317 +(** and transforms any other into predicates, applying then                 **)
3.318 +(** the tactic chosen by the user, which may solve the subgoal completely.  **)
3.319 +(*****************************************************************************)
3.320 +
3.321 +fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac];
3.322
3.323 -val StateElimTac = SUBGOAL (fn (Bi,i) =>
3.324 -  let val Const _ \$ Abs (_,Type ("fun",[_,type_pvar]),trm) = Bi
3.325 -      val _ \$ (_ \$ Abs (_,_,_ \$ Abs (namexAll,_,_))) \$
3.326 -			    (_ \$ Abs (_,_,varP \$ _ \$ (varx \$ _))) =
3.327 -			    #prop (rep_thm meta_spec)
3.328 -      fun vtac v i st = st |>
3.329 -	  let val cterm = cterm_of (#sign (rep_thm st))
3.330 -	      val (_,_,_ \$ Abs (_,_,trm),_) = dest_state (st,i);
3.331 -	      val (sname,trm0) = variant_abs ("s",dummyT,trm);
3.332 -	      val xsname = variant_name ("xs",trm0);
3.333 -	      val trm1 = subst_term (Free (sname,dummyT) \$ v,
3.334 -				     Syntax.free xsname,trm0)
3.335 -	      val trm2 = Abs ("xs", type_pvar,
3.336 -			      abstract_over (Syntax.free xsname,trm1))
3.337 -	  in
3.338 -	      comp_inst_ren_tac
3.339 -		[(namexAll,pvar2pvarID v)]
3.340 -		[(cterm varx,
3.341 -		  cterm (Abs  ("s",Type ("nat",[]) --> type_pvar,
3.342 -			       Bound 0 \$ v))),
3.343 -		 (cterm varP,
3.344 -		  cterm (Abs ("s", Type ("nat",[]) --> type_pvar,
3.345 -			      abstract_over (Free (sname,dummyT),trm2))))]
3.346 -		meta_spec i
3.347 -	  end
3.348 -      fun vars_tac [] i      = all_tac
3.349 -	| vars_tac (v::vl) i = vtac v i THEN vars_tac vl i
3.350 -  in
3.351 -      vars_tac (pvars_of_term (variant_abs ("s",dummyT,trm))) i
3.352 -  end);
3.353 +fun BasicSimpTac tac =
3.354 +  simp_tac (HOL_basic_ss addsimps [mem_Collect_eq,split])
3.355 +  THEN_MAYBE' MaxSimpTac tac;
3.356 +
3.357 +(** HoareRuleTac **)
3.358 +
3.359 +fun WlpTac Mlem tac i = rtac SeqRule i THEN  HoareRuleTac Mlem tac false (i+1)
3.360 +and HoareRuleTac Mlem tac pre_cond i st = st |>
3.361 +        (*abstraction over st prevents looping*)
3.362 +    ( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i)
3.363 +      ORELSE
3.364 +      (FIRST[rtac SkipRule i,
3.365 +             EVERY[rtac BasicRule i,
3.366 +                   rtac Mlem i,
3.367 +                   split_simp_tac i],
3.368 +             EVERY[rtac CondRule i,
3.369 +                   HoareRuleTac Mlem tac false (i+2),
3.370 +                   HoareRuleTac Mlem tac false (i+1)],
3.371 +             EVERY[rtac WhileRule i,
3.372 +                   BasicSimpTac tac (i+2),
3.373 +                   HoareRuleTac Mlem tac true (i+1)] ]
3.374 +       THEN (if pre_cond then (BasicSimpTac tac i) else (rtac subset_refl i)) ));
3.375
3.376
3.377 -(*** tactics for verification condition generation ***)
3.378 -
3.379 -(* pre_cond:bool gibt an, ob das Subgoal von der Form Spec(?Q,c,p) ist oder nicht. Im Fall
3.380 -   von pre_cond=false besteht die Vorbedingung nur nur aus einer scheme-Variable. Die dann
3.381 -   generierte Verifikationsbedingung hat die Form "!!s.?Q --> ...". "?Q" kann deshalb zu gegebenen
3.382 -   Zeitpunkt mittels "rtac impI" und "atac" gebunden werden, die Bedingung loest sich dadurch auf. *)
3.383 -
3.384 -fun WlpTac i = (rtac SeqRule i) THEN (HoareRuleTac (i+1) false)
3.385 -and HoareRuleTac i pre_cond st = st |>
3.386 -	(*abstraction over st prevents looping*)
3.387 -    ( (WlpTac i THEN HoareRuleTac i pre_cond)
3.388 -      ORELSE
3.389 -      (FIRST[rtac SkipRule i,
3.390 -	     rtac AssignRule i,
3.391 -	     EVERY[rtac IfRule i,
3.392 -		   HoareRuleTac (i+2) false,
3.393 -		   HoareRuleTac (i+1) false],
3.394 -	     EVERY[rtac WhileRule i,
3.395 -		   Asm_full_simp_tac (i+2),
3.396 -		   HoareRuleTac (i+1) true]]
3.397 -       THEN
3.398 -       (if pre_cond then (Asm_full_simp_tac i) else (atac i))) );
3.399 -
3.400 -val hoare_tac =
3.401 -  SELECT_GOAL
3.402 -    (EVERY[HoareRuleTac 1 true, ALLGOALS StateElimTac, prune_params_tac]);
3.403 -
3.404 +(** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **)
3.405 +(** the final verification conditions                                       **)
3.406 +
3.407 +fun hoare_tac tac i thm =
3.408 +  let val Mlem = Mset(thm)
3.409 +  in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end;
```
```     4.1 --- a/src/HOL/Hoare/Hoare.thy	Wed Oct 14 11:51:11 1998 +0200
4.2 +++ b/src/HOL/Hoare/Hoare.thy	Wed Oct 14 15:26:31 1998 +0200
4.3 @@ -1,196 +1,193 @@
4.4  (*  Title:      HOL/Hoare/Hoare.thy
4.5      ID:         \$Id\$
4.6 -    Author:     Norbert Galm & Tobias Nipkow
4.8 +    Author:     Leonor Prensa Nieto & Tobias Nipkow
4.10
4.11  Sugared semantic embedding of Hoare logic.
4.12 +Strictly speaking a shallow embedding (as implemented by Norbert Galm
4.13 +following Mike Gordon) would suffice. Maybe the datatype com comes in useful
4.14 +later.
4.15  *)
4.16
4.17 -Hoare = Arith +
4.18 +Hoare  = Main +
4.19
4.20  types
4.21 -  pvar = nat                                    (* encoding of program variables ( < 26! ) *)
4.22 -  'a state = pvar => 'a                         (* program state *)
4.23 -  'a exp = 'a state => 'a                       (* denotation of expressions *)
4.24 -  'a bexp = 'a state => bool                    (* denotation of boolean expressions *)
4.25 -  'a com = 'a state => 'a state => bool         (* denotation of programs *)
4.26 +    'a bexp = 'a set
4.27 +    'a assn = 'a set
4.28 +    'a fexp = 'a =>'a
4.29 +
4.30 +datatype
4.31 + 'a com = Basic ('a fexp)
4.32 +   | Seq ('a com) ('a com)               ("(_;/_)"      [61,60] 60)
4.33 +   | Cond ('a bexp) ('a com) ('a com)    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
4.34 +   | While ('a bexp) ('a assn) ('a com)  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
4.35 +
4.36 +syntax
4.37 +  "@assign"  :: id => 'b => 'a com        ("(2_ :=/ _ )" [70,65] 61)
4.38 +  "@annskip" :: 'a com                    ("SKIP")
4.39 +
4.40 +translations
4.41 +            "SKIP" == "Basic id"
4.42 +
4.43 +types 'a sem = 'a => 'a => bool
4.44 +
4.45 +consts iter :: nat => 'a bexp => 'a sem => 'a sem
4.46 +primrec
4.47 +"iter 0 b S = (%s s'. s ~: b & (s=s'))"
4.48 +"iter (Suc n) b S = (%s s'. s : b & (? s''. S s s'' & iter n b S s'' s'))"
4.49 +
4.50 +consts Sem :: 'a com => 'a sem
4.51 +primrec
4.52 +"Sem(Basic f) s s' = (s' = f s)"
4.53 +"Sem(c1;c2) s s' = (? s''. Sem c1 s s'' & Sem c2 s'' s')"
4.54 +"Sem(IF b THEN c1 ELSE c2 FI) s s' = ((s  : b --> Sem c1 s s') &
4.55 +                                      (s ~: b --> Sem c2 s s'))"
4.56 +"Sem(While b x c) s s' = (? n. iter n b (Sem c) s s')"
4.57 +
4.58 +constdefs Valid :: ['a bexp, 'a com, 'a bexp] => bool
4.59 +  "Valid p c q == !s s'. Sem c s s' --> s : p --> s' : q"
4.60
4.61
4.62 -(* program syntax *)
4.63 -
4.64  nonterminals
4.65 -  prog
4.66 +  vars
4.67
4.68  syntax
4.69 -  "@Skip"       :: prog                         ("SKIP")
4.70 -  "@Assign"     :: [id, 'a] => prog             ("_ := _")
4.71 -  "@Seq"        :: [prog, prog] => prog         ("_;//_" [0,1000] 999)
4.72 -  "@If"         :: [bool, prog, prog] => prog   ("IF _//THEN//  (_)//ELSE//  (_)//END")
4.73 -  "@While"      :: [bool, bool, prog] => prog   ("WHILE _//DO {_}//  (_)//END")
4.74 -  "@Spec"       :: [bool, prog, bool] => bool   ("{_}//_//{_}")
4.75 -
4.76 -
4.77 -(* denotational semantics *)
4.78 -
4.79 -constdefs
4.80 -  Skip          :: 'a com
4.81 -  "Skip s s' == (s=s')"
4.82 -
4.83 -  Assign        :: [pvar, 'a exp] => 'a com
4.84 -  "Assign v e s s' == (s' = (%x. if x=v then e(s) else s(x)))"
4.85 +  ""		     :: "id => vars"		       ("_")
4.86 +  "_vars" 	     :: "[id, vars] => vars"	       ("_ _")
4.87
4.88 -  Seq           :: ['a com, 'a com] => 'a com
4.89 -  "Seq c c' s s' == ? s''. c s s'' & c' s'' s'"
4.90 -
4.91 -  Cond          :: ['a bexp, 'a com, 'a com] => 'a com
4.92 -  "Cond b c c' s s' == (b(s) --> c s s') & (~b s --> c' s s')"
4.93 -
4.94 -consts
4.95 -  Iter          :: [nat, 'a bexp, 'a com] => 'a com
4.96 -
4.97 -primrec
4.98 -  "Iter 0 b c = (%s s'.~b(s) & (s=s'))"
4.99 -  "Iter (Suc n) b c = (%s s'. b(s) & Seq c (Iter n b c) s s')"
4.100 -
4.101 -constdefs
4.102 -  While         :: ['a bexp, 'a bexp, 'a com] => 'a com
4.103 -  "While b I c s s' == ? n. Iter n b c s s'"
4.104 -
4.105 -  Spec          :: ['a bexp, 'a com, 'a bexp] => bool
4.106 -  "Spec p c q == !s s'. c s s' --> p s --> q s'"
4.107 +syntax
4.108 + "@hoare_vars" :: [vars, 'a assn,'a com,'a assn] => bool
4.109 +                 ("|- VARS _.// {_} // _ // {_}" [0,0,55,0] 50)
4.110 +syntax ("" output)
4.111 + "@hoare"      :: ['a assn,'a com,'a assn] => bool
4.112 +                 ("|- {_} // _ // {_}" [0,55,0] 50)
4.113
4.114  end
4.115
4.116  ML
4.117
4.118 -
4.119 -(*** term manipulation ***)
4.120 -
4.121 -(* name_in_term:bool (name:string,trm:term) bestimmt, ob in trm der Name name als Konstante,
4.122 -   freie Var., scheme-Variable oder als Name fuer eine Lambda-Abstraktion vorkommt *)
4.123 +(** parse translations **)
4.124
4.125 -fun name_in_term (name,Const (s,t))    = (name=s)
4.126 -  | name_in_term (name,Free (s,t))     = (name=s)
4.127 -  | name_in_term (name,Var (ix,t))  = (name= string_of_indexname ix)
4.128 -  | name_in_term (name,Abs (s,t,trm))  = (name=s) orelse
4.129 -                                         (name_in_term (name,trm))
4.130 -  | name_in_term (name,trm1 \$ trm2)    = (name_in_term (name,trm1)) orelse
4.131 -                                         (name_in_term (name,trm2))
4.132 -  | name_in_term (_,_)                 = false;
4.133 -
4.134 -(* variant_name:string (name:string,trm:term) liefert einen von name
4.135 -   abgewandelten Namen (durch Anhaengen von genuegend vielen "_"), der nicht
4.136 -   in trm vorkommt. Im Gegensatz zu variant_abs beruecktsichtigt es auch Namen
4.137 -   von scheme-Variablen und von Lambda-Abstraktionen in trm *)
4.138 +fun mk_abstuple []     body = absfree ("x", dummyT, body)
4.139 +  | mk_abstuple [v]    body = absfree ((fst o dest_Free) v, dummyT, body)
4.140 +  | mk_abstuple (v::w) body = Syntax.const "split" \$
4.141 +                              absfree ((fst o dest_Free) v, dummyT, mk_abstuple w body);
4.142
4.143 -(*This could be done more simply by calling Term.variant, supplying a list of
4.144 -  all free, bound and scheme variables in the term.*)
4.145 -fun variant_name (name,trm) = if name_in_term (name,trm)
4.146 -			      then variant_name (name ^ "_",trm)
4.147 -                              else name;
4.148 -
4.149 -(* subst_term:term (von:term,nach:term,trm:term) liefert den Term, der aus
4.150 -trm entsteht, wenn alle Teilterme, die gleich von sind, durch nach ersetzt
4.151 -wurden *)
4.152 +
4.153 +fun mk_fbody v e []      = Syntax.const "()"
4.154 +  | mk_fbody v e [x]     = if v=x then e else x
4.155 +  | mk_fbody v e (x::xs) = Syntax.const "Pair" \$ (if v=x then e else x) \$
4.156 +                           mk_fbody v e xs;
4.157
4.158 -fun subst_term (von,nach,Abs (s,t,trm)) =if von=Abs (s,t,trm)
4.159 -                                                then nach
4.160 -                                                else Abs (s,t,subst_term (von,nach,trm))
4.161 -  | subst_term (von,nach,trm1 \$ trm2)   =if von=trm1 \$ trm2
4.162 -                                                then nach
4.163 -                                                else subst_term (von,nach,trm1) \$ subst_term (von,nach,trm2)
4.164 -  | subst_term (von,nach,trm)           =if von=trm
4.165 -                                                then nach
4.166 -                                                else trm;
4.167 +fun mk_fexp v e xs = mk_abstuple xs (mk_fbody v e xs);
4.168
4.169
4.170 -(* Translation between program vars ("a" - "z") and their encoding as
4.171 -   natural numbers: "a" <==> 0, "b" <==> Suc(0), ..., "z" <==> 25 *)
4.172 +(* bexp_tr & assn_tr *)
4.173 +(*all meta-variables for bexp except for TRUE and FALSE are translated as if they
4.174 +  were boolean expressions*)
4.175 +
4.176 +fun bexp_tr (Const ("TRUE", _)) xs = Syntax.const "TRUE"
4.177 +  | bexp_tr b xs = Syntax.const "Collect" \$ mk_abstuple xs b;
4.178 +
4.179 +fun assn_tr r xs = Syntax.const "Collect" \$ mk_abstuple xs r;
4.180
4.181 -fun is_pvarID s = size s=1 andalso "a"<=s andalso s<="z";
4.182 +(* com_tr *)
4.183 +
4.184 +fun assign_tr [Free (V,_),E] xs = Syntax.const "Basic" \$
4.185 +                                      mk_fexp (Free(V,dummyT)) E xs
4.186 +  | assign_tr ts _ = raise TERM ("assign_tr", ts);
4.187
4.188 -fun pvarID2pvar s =
4.189 -  let fun rest2pvar (i,arg) =
4.190 -            if i=0 then arg else rest2pvar(i-1, Syntax.const "Suc" \$ arg)
4.191 -  in rest2pvar(ord s - ord "a", Syntax.const "0") end;
4.192 +fun com_tr (Const("@assign",_) \$ Free (V,_) \$ E) xs =
4.193 +               assign_tr [Free (V,dummyT),E] xs
4.194 +  | com_tr (Const ("Basic",_) \$ f) xs = Syntax.const "Basic" \$ f
4.195 +  | com_tr (Const ("Seq",_) \$ c1 \$ c2) xs = Syntax.const "Seq" \$
4.196 +                                                 com_tr c1 xs \$ com_tr c2 xs
4.197 +  | com_tr (Const ("Cond",_) \$ b \$ c1 \$ c2) xs = Syntax.const "Cond" \$
4.198 +                                  bexp_tr b xs \$ com_tr c1 xs \$ com_tr c2 xs
4.199 +  | com_tr (Const ("While",_) \$ b \$ I \$ c) xs = Syntax.const "While" \$
4.200 +                                         bexp_tr b xs \$ assn_tr I xs \$ com_tr c xs
4.201 +  | com_tr trm _ = trm;
4.202 +
4.203 +(* triple_tr *)
4.204
4.205 -fun pvar2pvarID trm     =
4.206 -        let
4.207 -                fun rest2pvarID (Const("0",_),i)                =chr (i + ord "a")
4.208 -                  | rest2pvarID (Const("Suc",_) \$ trm,i)        =rest2pvarID (trm,i+1)
4.209 -        in
4.210 -                rest2pvarID (trm,0)
4.211 -        end;
4.212 +fun vars_tr (x as Free _) = [x]
4.213 +  | vars_tr (Const ("_vars", _) \$ (x as Free _) \$ vars) = x :: vars_tr vars
4.214 +  | vars_tr t = raise TERM ("vars_tr", [t]);
4.215 +
4.216 +fun hoare_vars_tr [vars, pre, prg, post] =
4.217 +      let val xs = vars_tr vars
4.218 +      in Syntax.const "Valid" \$
4.219 +           assn_tr pre xs \$ com_tr prg xs \$ assn_tr post xs
4.220 +      end
4.221 +  | hoare_vars_tr ts = raise TERM ("hoare_vars_tr", ts);
4.222 +
4.223 +
4.224 +
4.225 +val parse_translation = [("@hoare_vars", hoare_vars_tr)];
4.226
4.227
4.228 -(*** parse translations: syntax -> semantics ***)
4.229 +(*****************************************************************************)
4.230 +
4.231 +(*** print translations ***)
4.232
4.233 -(* term_tr:term (name:string,trm:term) ersetzt in trm alle freien Variablen, die eine pvarID
4.234 -   darstellen, durch name \$ pvarID2pvar(pvarID). Beispiel:
4.235 -   bei name="s" und dem Term "a=b & a=X" wird der Term "s(0)=s(Suc(0)) & s(0)=X" geliefert *)
4.236 +fun dest_abstuple (Const ("split",_) \$ (Abs(v,_, body))) =
4.237 +                            subst_bound (Syntax.free v, dest_abstuple body)
4.238 +  | dest_abstuple (Abs(v,_, body)) = subst_bound (Syntax.free v, body)
4.239 +  | dest_abstuple trm = trm;
4.240
4.241 -fun term_tr (name,Free (s,t)) = if is_pvarID s
4.242 -                                then Syntax.free name \$ pvarID2pvar s
4.243 -                                else Free (s,t)
4.244 -  | term_tr (name,Abs (s,t,trm)) = Abs (s,t,term_tr (name,trm))
4.245 -  | term_tr (name,trm1 \$ trm2)  = term_tr (name,trm1) \$ term_tr (name,trm2)
4.246 -  | term_tr (name,trm) = trm;
4.247 +fun abs2list (Const ("split",_) \$ (Abs(x,T,t))) = Free (x, T)::abs2list t
4.248 +  | abs2list (Abs(x,T,t)) = [Free (x, T)]
4.249 +  | abs2list _ = [];
4.250
4.251 -fun bool_tr B =
4.252 -  let val name = variant_name("s",B)
4.253 -  in Abs (name,dummyT,abstract_over (Syntax.free name,term_tr (name,B))) end;
4.254 +fun mk_ts (Const ("split",_) \$ (Abs(x,_,t))) = mk_ts t
4.255 +  | mk_ts (Abs(x,_,t)) = mk_ts t
4.256 +  | mk_ts (Const ("Pair",_) \$ a \$ b) = a::(mk_ts b)
4.257 +  | mk_ts t = [t];
4.258
4.259 -fun exp_tr E =
4.260 -  let val name = variant_name("s",E)
4.261 -  in Abs (name,dummyT,abstract_over (Syntax.free name,term_tr (name,E))) end;
4.262 +fun mk_vts (Const ("split",_) \$ (Abs(x,_,t))) =
4.263 +           ((Syntax.free x)::(abs2list t), mk_ts t)
4.264 +  | mk_vts (Abs(x,_,t)) = ([Syntax.free x], [t])
4.265 +  | mk_vts t = raise Match;
4.266 +
4.267 +fun find_ch [] i xs = (false, (Syntax.free "not_ch",Syntax.free "not_ch" ))
4.268 +  | find_ch ((v,t)::vts) i xs = if t=(Bound i) then find_ch vts (i-1) xs
4.269 +              else (true, (v, subst_bounds (xs,t)));
4.270 +
4.271 +fun is_f (Const ("split",_) \$ (Abs(x,_,t))) = true
4.272 +  | is_f (Abs(x,_,t)) = true
4.273 +  | is_f t = false;
4.274 +
4.275 +(* assn_tr' & bexp_tr'*)
4.276 +
4.277 +fun assn_tr' (Const ("Collect",_) \$ T) = dest_abstuple T
4.278 +  | assn_tr' (Const ("op Int",_) \$ (Const ("Collect",_) \$ T1) \$
4.279 +                                   (Const ("Collect",_) \$ T2)) =
4.280 +            Syntax.const "op Int" \$ dest_abstuple T1 \$ dest_abstuple T2
4.281 +  | assn_tr' t = t;
4.282
4.283 -fun prog_tr (Const ("@Skip",_)) = Syntax.const "Skip"
4.284 -  | prog_tr (Const ("@Assign",_) \$ Free (V,_) \$ E) =
4.285 -      if is_pvarID V
4.286 -      then Syntax.const "Assign" \$ pvarID2pvar V \$ exp_tr E
4.287 -      else error("Not a valid program variable: " ^ quote V)
4.288 -  | prog_tr (Const ("@Seq",_) \$ C \$ C') =
4.289 -      Syntax.const "Seq" \$ prog_tr C \$ prog_tr C'
4.290 -  | prog_tr (Const ("@If",_) \$ B \$ C \$ C') =
4.291 -      Syntax.const "Cond" \$ bool_tr B \$ prog_tr C \$ prog_tr C'
4.292 -  | prog_tr (Const ("@While",_) \$ B \$ INV \$ C) =
4.293 -      Syntax.const "While" \$ bool_tr B \$ bool_tr INV \$ prog_tr C;
4.294 +fun bexp_tr' (Const ("Collect",_) \$ T) = dest_abstuple T
4.295 +  | bexp_tr' t = t;
4.296 +
4.297 +(*com_tr' *)
4.298
4.299 -fun spec_tr [P,C,Q] = Syntax.const "Spec" \$ bool_tr P \$ prog_tr C \$ bool_tr Q;
4.300 +fun mk_assign f =
4.301 +  let val (vs, ts) = mk_vts f;
4.302 +      val (ch, which) = find_ch (vs~~ts) ((length vs)-1) (rev vs)
4.303 +  in if ch then Syntax.const "@assign" \$ fst(which) \$ snd(which)
4.304 +     else Syntax.const "@skip" end;
4.305
4.306 -val parse_translation = [("@Spec",spec_tr)];
4.307 +fun com_tr' (Const ("Basic",_) \$ f) = if is_f f then mk_assign f
4.308 +                                           else Syntax.const "Basic" \$ f
4.309 +  | com_tr' (Const ("Seq",_) \$ c1 \$ c2) = Syntax.const "Seq" \$
4.310 +                                                 com_tr' c1 \$ com_tr' c2
4.311 +  | com_tr' (Const ("Cond",_) \$ b \$ c1 \$ c2) = Syntax.const "Cond" \$
4.312 +                                           bexp_tr' b \$ com_tr' c1 \$ com_tr' c2
4.313 +  | com_tr' (Const ("While",_) \$ b \$ I \$ c) = Syntax.const "While" \$
4.314 +                                               bexp_tr' b \$ assn_tr' I \$ com_tr' c
4.315 +  | com_tr' t = t;
4.316
4.317
4.318 -(*** print translations: semantics -> syntax ***)
4.319 -
4.320 -(* Note: does not mark tokens *)
4.321 -
4.322 -(* term_tr':term (name:string,trm:term) ersetzt in trm alle Vorkommen von name \$ pvar durch
4.323 -   entsprechende freie Variablen, welche die pvarID zu pvar darstellen. Beispiel:
4.324 -        bei name="s" und dem Term "s(0)=s(Suc(0)) & s(0)=X" wird der Term "a=b & a=X" geliefert *)
4.325 -
4.326 -fun term_tr' (name,Free (s,t) \$ trm) =
4.327 -      if name=s then Syntax.free (pvar2pvarID trm)
4.328 -      else Free (s,t) \$ term_tr' (name,trm)
4.329 -  | term_tr' (name,Abs (s,t,trm)) = Abs (s,t,term_tr' (name,trm))
4.330 -  | term_tr' (name,trm1 \$ trm2) = term_tr' (name,trm1) \$ term_tr' (name,trm2)
4.331 -  | term_tr' (name,trm) = trm;
4.332 -
4.333 -fun bexp_tr' (Abs(_,_,b)) = term_tr' (variant_abs ("s",dummyT,b));
4.334 -
4.335 -fun exp_tr' (Abs(_,_,e)) = term_tr' (variant_abs ("s",dummyT,e));
4.336 -
4.337 -fun com_tr' (Const ("Skip",_)) = Syntax.const "@Skip"
4.338 -  | com_tr' (Const ("Assign",_) \$ v \$ e) =
4.339 -      Syntax.const "@Assign" \$ Syntax.free (pvar2pvarID v) \$ exp_tr' e
4.340 -  | com_tr' (Const ("Seq",_) \$ c \$ c') =
4.341 -      Syntax.const "@Seq" \$ com_tr' c \$ com_tr' c'
4.342 -  | com_tr' (Const ("Cond",_) \$ b \$ c \$ c') =
4.343 -       Syntax.const "@If" \$ bexp_tr' b \$ com_tr' c \$ com_tr' c'
4.344 -  | com_tr' (Const ("While",_) \$ b \$ inv \$ c) =
4.345 -       Syntax.const "@While" \$ bexp_tr' b \$ bexp_tr' inv \$ com_tr' c;
4.346 -
4.347 -fun spec_tr' [p,c,q] =
4.348 -       Syntax.const "@Spec" \$ bexp_tr' p \$ com_tr' c \$ bexp_tr' q;
4.349 -
4.350 -val print_translation = [("Spec",spec_tr')];
4.351 +fun spec_tr' [p, c, q] =
4.352 +  Syntax.const "@hoare" \$ assn_tr' p \$ com_tr' c \$ assn_tr' q
4.353 +
4.354 +val print_translation = [("Valid", spec_tr')];
```
```     5.1 --- a/src/HOL/Hoare/List_Examples.ML	Wed Oct 14 11:51:11 1998 +0200
5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,27 +0,0 @@
5.4 -Goal
5.5 -"{x=X} \
5.6 -\ y := []; \
5.7 -\ WHILE x ~= [] \
5.8 -\ DO { rev(x)@y = rev(X)} \
5.9 -\    y := hd x # y; x := tl x \
5.10 -\ END \
5.11 -\{y=rev(X)}";
5.12 -by (hoare_tac 1);
5.13 -by (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
5.14 -by Safe_tac;
5.15 -by (Asm_full_simp_tac 1);
5.16 -qed "imperative_reverse";
5.17 -
5.18 -Goal
5.19 -"{x=X & y = Y} \
5.20 -\ x := rev(x); \
5.21 -\ WHILE x ~= [] \
5.22 -\ DO { rev(x)@y = X@Y} \
5.23 -\    y := hd x # y; x := tl x \
5.24 -\ END \
5.25 -\{y = X@Y}";
5.26 -by (hoare_tac 1);
5.27 -by (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
5.28 -by Safe_tac;
5.29 -by (Asm_full_simp_tac 1);
5.30 -qed "imperative_append";
```
```     6.1 --- a/src/HOL/Hoare/List_Examples.thy	Wed Oct 14 11:51:11 1998 +0200
6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,1 +0,0 @@
6.4 -List_Examples = Hoare + List
```
```     7.1 --- a/src/HOL/Hoare/README.html	Wed Oct 14 11:51:11 1998 +0200
7.2 +++ b/src/HOL/Hoare/README.html	Wed Oct 14 15:26:31 1998 +0200
7.3 @@ -1,6 +1,59 @@
7.5
7.6 -<H2>Semantic Embedding of Hoare Logic</H2>
7.7 +<H2>Hoare Logic for a Simple WHILE Language</H2>
7.8 +
7.9 +<H1>The language and logic<H1>
7.10 +
7.11 +This directory contains an implementation of Hoare logic for a simple WHILE
7.12 +language. The  are
7.13 +<UL>
7.14 +<LI> SKIP
7.15 +<LI> _ := _
7.16 +<LI> _ ; _
7.17 +<LI> <kbd>IF _ THEN _ ELSE _ FI<kbd>
7.18 +<LI> WHILE _ INV {_} DO _ OD
7.19 +</UL>
7.20 +Note that each WHILE-loop must be annotated with an invariant.
7.21 +<P>
7.22 +
7.23 +After loading theory Hoare, you can state goals of the form
7.24 +<PRE>
7.25 +|- VARS x y ... . {P} prog {Q}
7.26 +</PRE>
7.27 +where <kbd>prog</kbd> is a program in the above language, <kbd>P</kbd> is the
7.28 +precondition, <kbd>Q</kbd> the postcondition, and <kbd>x y ...<kbd> is the
7.29 +list of all <i>program variables</i> in <kbd>prog</kbd>. The latter list must
7.30 +be nonempty and it must include all variables that occur on the left-hand
7.31 +side of an assignment in <kbd>prof</kbd>. Example:
7.32 +<PRE>
7.33 +|- VARS x. {x = a} x := x+1 {x = a+1}
7.34 +</PRE>
7.35 +The (normal) variable <kbd>a</kbd> is merely used to record the initial
7.36 +value of <kbd>x</kbd> and is not a program variable. Pre and postconditions
7.37 +can be arbitrary HOL formulae mentioning both program variables and normal
7.38 +variables.
7.39 +<P>
7.40 +
7.41 +The implementation hides reasoning in Hoare logic completely and provides a
7.42 +tactic hoare_tac for generating the verification conditions in ordinary
7.43 +logic:
7.44 +<PRE>
7.45 +by(hoare_tac tac i);
7.46 +</PRE>
7.47 +applies the tactic to subgoal <kbd>i</kbd> and applies the parameter
7.48 +<kbd>tac</kbd> to all generated verification conditions. A typical call is
7.49 +<PRE>
7.50 +by(hoare_tac Asm_full_simp_tac 1);
7.51 +</PRE>
7.52 +which, given the example goal above, solves it completely.
7.53 +<P>
7.54 +
7.55 +IMPORTANT:
7.56 +This is a logic of partial correctness. You can only prove that your program
7.57 +does the right thing <i>if</i> it terminates, but not <i>that</i> it
7.58 +terminates.
7.59 +
7.60 +<H1>Notes on the implementation</H1>
7.61
7.62  This directory contains a sugared shallow semantic embedding of Hoare logic
7.63  for a while language. The implementation closely follows<P>
```
```     8.1 --- a/src/HOL/Hoare/ROOT.ML	Wed Oct 14 11:51:11 1998 +0200
8.2 +++ b/src/HOL/Hoare/ROOT.ML	Wed Oct 14 15:26:31 1998 +0200
8.3 @@ -1,10 +1,9 @@
8.4  (*  Title:      HOL/IMP/ROOT.ML
8.5      ID:         \$Id\$
8.6      Author:     Tobias Nipkow