author | paulson |
Fri, 19 Sep 1997 16:12:21 +0200 | |
changeset 3685 | 5b8c0c8f576e |
parent 2901 | 4e92704cf320 |
child 3842 | b55686a7b22c |
permissions | -rw-r--r-- |
1476 | 1 |
(* Title: HOL/Hoare/Hoare.thy |
1335 | 2 |
ID: $Id$ |
1476 | 3 |
Author: Norbert Galm & Tobias Nipkow |
1335 | 4 |
Copyright 1995 TUM |
5 |
||
6 |
Sugared semantic embedding of Hoare logic. |
|
7 |
*) |
|
8 |
||
9 |
Hoare = Arith + |
|
10 |
||
11 |
types |
|
1476 | 12 |
'a prog (* program syntax *) |
13 |
pvar = nat (* encoding of program variables ( < 26! ) *) |
|
14 |
'a state = pvar => 'a (* program state *) |
|
15 |
'a exp = 'a state => 'a (* denotation of expressions *) |
|
16 |
'a bexp = 'a state => bool (* denotation of boolean expressions *) |
|
1384 | 17 |
'a com = 'a state => 'a state => bool (* denotation of programs *) |
1335 | 18 |
|
19 |
syntax |
|
1476 | 20 |
"@Skip" :: 'a prog ("SKIP") |
21 |
"@Assign" :: [id, 'a] => 'a prog ("_ := _") |
|
22 |
"@Seq" :: ['a prog, 'a prog] => 'a prog ("_;//_" [0,1000] 999) |
|
23 |
"@If" :: [bool, 'a prog, 'a prog] => 'a prog |
|
1335 | 24 |
("IF _//THEN// (_)//ELSE// (_)//END") |
1476 | 25 |
"@While" :: [bool, bool, 'a prog] => 'a prog |
1335 | 26 |
("WHILE _//DO {_}// (_)//END") |
27 |
||
1476 | 28 |
"@Spec" :: [bool, 'a prog, bool] => bool ("{_}//_//{_}") |
1335 | 29 |
|
1558 | 30 |
constdefs |
31 |
(* denotational semantics *) |
|
1335 | 32 |
|
1476 | 33 |
Skip :: 'a com |
1558 | 34 |
"Skip s s' == (s=s')" |
35 |
||
1476 | 36 |
Assign :: [pvar, 'a exp] => 'a com |
1558 | 37 |
"Assign v e s s' == (s' = (%x.if x=v then e(s) else s(x)))" |
38 |
||
1476 | 39 |
Seq :: ['a com, 'a com] => 'a com |
1558 | 40 |
"Seq c c' s s' == ? s''. c s s'' & c' s'' s'" |
41 |
||
1476 | 42 |
Cond :: ['a bexp, 'a com, 'a com] => 'a com |
1558 | 43 |
"Cond b c c' s s' == (b(s) --> c s s') & (~b s --> c' s s')" |
44 |
||
2901 | 45 |
consts |
46 |
Iter :: [nat, 'a bexp, 'a com] => 'a com |
|
47 |
primrec Iter nat |
|
48 |
"Iter 0 b c = (%s s'.~b(s) & (s=s'))" |
|
49 |
"Iter (Suc n) b c = (%s s'. b(s) & Seq c (Iter n b c) s s')" |
|
1558 | 50 |
|
2901 | 51 |
constdefs |
52 |
While :: ['a bexp, 'a bexp, 'a com] => 'a com |
|
53 |
"While b I c s s' == ? n. Iter n b c s s'" |
|
1335 | 54 |
|
1476 | 55 |
Spec :: ['a bexp, 'a com, 'a bexp] => bool |
1558 | 56 |
"Spec p c q == !s s'. c s s' --> p s --> q s'" |
1335 | 57 |
|
58 |
end |
|
59 |
||
60 |
ML |
|
61 |
||
62 |
||
63 |
(*** term manipulation ***) |
|
64 |
||
65 |
(* name_in_term:bool (name:string,trm:term) bestimmt, ob in trm der Name name als Konstante, |
|
66 |
freie Var., scheme-Variable oder als Name fuer eine Lambda-Abstraktion vorkommt *) |
|
67 |
||
1476 | 68 |
fun name_in_term (name,Const (s,t)) =(name=s) |
69 |
| name_in_term (name,Free (s,t)) =(name=s) |
|
2239 | 70 |
| name_in_term (name,Var ((s,i),t)) =(name=s ^ Int.toString i) |
1476 | 71 |
| name_in_term (name,Abs (s,t,trm)) =(name=s) orelse (name_in_term (name,trm)) |
72 |
| name_in_term (name,trm1 $ trm2) =(name_in_term (name,trm1)) orelse (name_in_term (name,trm2)) |
|
73 |
| name_in_term (_,_) =false; |
|
1335 | 74 |
|
75 |
(* variant_name:string (name:string,trm:term) liefert einen von name abgewandelten Namen (durch Anhaengen |
|
76 |
von genuegend vielen "_"), der nicht in trm vorkommt. Im Gegensatz zu variant_abs beruecktsichtigt es |
|
77 |
auch Namen von scheme-Variablen und von Lambda-Abstraktionen in trm *) |
|
78 |
||
1476 | 79 |
fun variant_name (name,trm) =if name_in_term (name,trm) |
80 |
then variant_name (name ^ "_",trm) |
|
81 |
else name; |
|
1335 | 82 |
|
83 |
(* subst_term:term (von:term,nach:term,trm:term) liefert den Term, der aus |
|
84 |
trm entsteht, wenn alle Teilterme, die gleich von sind, durch nach ersetzt |
|
85 |
wurden *) |
|
86 |
||
1476 | 87 |
fun subst_term (von,nach,Abs (s,t,trm)) =if von=Abs (s,t,trm) |
88 |
then nach |
|
89 |
else Abs (s,t,subst_term (von,nach,trm)) |
|
90 |
| subst_term (von,nach,trm1 $ trm2) =if von=trm1 $ trm2 |
|
91 |
then nach |
|
92 |
else subst_term (von,nach,trm1) $ subst_term (von,nach,trm2) |
|
93 |
| subst_term (von,nach,trm) =if von=trm |
|
94 |
then nach |
|
95 |
else trm; |
|
1335 | 96 |
|
97 |
||
98 |
(* Translation between program vars ("a" - "z") and their encoding as |
|
99 |
natural numbers: "a" <==> 0, "b" <==> Suc(0), ..., "z" <==> 25 *) |
|
100 |
||
1476 | 101 |
fun is_pvarID s = size s=1 andalso "a"<=s andalso s<="z"; |
1335 | 102 |
|
103 |
fun pvarID2pvar s = |
|
104 |
let fun rest2pvar (i,arg) = |
|
105 |
if i=0 then arg else rest2pvar(i-1, Syntax.const "Suc" $ arg) |
|
106 |
in rest2pvar(ord s - ord "a", Syntax.const "0") end; |
|
107 |
||
1476 | 108 |
fun pvar2pvarID trm = |
109 |
let |
|
110 |
fun rest2pvarID (Const("0",_),i) =chr (i + ord "a") |
|
111 |
| rest2pvarID (Const("Suc",_) $ trm,i) =rest2pvarID (trm,i+1) |
|
112 |
in |
|
113 |
rest2pvarID (trm,0) |
|
114 |
end; |
|
1335 | 115 |
|
116 |
||
117 |
(*** parse translations: syntax -> semantics ***) |
|
118 |
||
119 |
(* term_tr:term (name:string,trm:term) ersetzt in trm alle freien Variablen, die eine pvarID |
|
120 |
darstellen, durch name $ pvarID2pvar(pvarID). Beispiel: |
|
121 |
bei name="s" und dem Term "a=b & a=X" wird der Term "s(0)=s(Suc(0)) & s(0)=X" geliefert *) |
|
122 |
||
123 |
fun term_tr (name,Free (s,t)) = if is_pvarID s |
|
1476 | 124 |
then Syntax.free name $ pvarID2pvar s |
125 |
else Free (s,t) |
|
1335 | 126 |
| term_tr (name,Abs (s,t,trm)) = Abs (s,t,term_tr (name,trm)) |
1476 | 127 |
| term_tr (name,trm1 $ trm2) = term_tr (name,trm1) $ term_tr (name,trm2) |
1335 | 128 |
| term_tr (name,trm) = trm; |
129 |
||
130 |
fun bool_tr B = |
|
131 |
let val name = variant_name("s",B) |
|
132 |
in Abs (name,dummyT,abstract_over (Syntax.free name,term_tr (name,B))) end; |
|
133 |
||
134 |
fun exp_tr E = |
|
135 |
let val name = variant_name("s",E) |
|
136 |
in Abs (name,dummyT,abstract_over (Syntax.free name,term_tr (name,E))) end; |
|
137 |
||
1476 | 138 |
fun prog_tr (Const ("@Skip",_)) = Syntax.const "Skip" |
1335 | 139 |
| prog_tr (Const ("@Assign",_) $ Free (V,_) $ E) = |
140 |
if is_pvarID V |
|
141 |
then Syntax.const "Assign" $ pvarID2pvar V $ exp_tr E |
|
142 |
else error("Not a valid program variable: " ^ quote V) |
|
1476 | 143 |
| prog_tr (Const ("@Seq",_) $ C $ C') = |
1335 | 144 |
Syntax.const "Seq" $ prog_tr C $ prog_tr C' |
145 |
| prog_tr (Const ("@If",_) $ B $ C $ C') = |
|
146 |
Syntax.const "Cond" $ bool_tr B $ prog_tr C $ prog_tr C' |
|
147 |
| prog_tr (Const ("@While",_) $ B $ INV $ C) = |
|
148 |
Syntax.const "While" $ bool_tr B $ bool_tr INV $ prog_tr C; |
|
149 |
||
150 |
fun spec_tr [P,C,Q] = Syntax.const "Spec" $ bool_tr P $ prog_tr C $ bool_tr Q; |
|
151 |
||
152 |
val parse_translation = [("@Spec",spec_tr)]; |
|
153 |
||
154 |
||
155 |
(*** print translations: semantics -> syntax ***) |
|
156 |
||
2708
c3b86dcd340a
added comment: print translations do not mark tokens;
wenzelm
parents:
2239
diff
changeset
|
157 |
(* Note: does not mark tokens *) |
c3b86dcd340a
added comment: print translations do not mark tokens;
wenzelm
parents:
2239
diff
changeset
|
158 |
|
1335 | 159 |
(* term_tr':term (name:string,trm:term) ersetzt in trm alle Vorkommen von name $ pvar durch |
160 |
entsprechende freie Variablen, welche die pvarID zu pvar darstellen. Beispiel: |
|
1476 | 161 |
bei name="s" und dem Term "s(0)=s(Suc(0)) & s(0)=X" wird der Term "a=b & a=X" geliefert *) |
1335 | 162 |
|
1476 | 163 |
fun term_tr' (name,Free (s,t) $ trm) =if name=s |
164 |
then Syntax.free (pvar2pvarID trm) |
|
165 |
else Free (s,t) $ term_tr' (name,trm) |
|
166 |
| term_tr' (name,Abs (s,t,trm)) =Abs (s,t,term_tr' (name,trm)) |
|
167 |
| term_tr' (name,trm1 $ trm2) =term_tr' (name,trm1) $ term_tr' (name,trm2) |
|
168 |
| term_tr' (name,trm) =trm; |
|
1335 | 169 |
|
1476 | 170 |
fun bexp_tr' (Abs(_,_,b)) =term_tr' (variant_abs ("s",dummyT,b)); |
1335 | 171 |
|
1476 | 172 |
fun exp_tr' (Abs(_,_,e)) =term_tr' (variant_abs ("s",dummyT,e)); |
1335 | 173 |
|
1476 | 174 |
fun com_tr' (Const ("Skip",_)) =Syntax.const "@Skip" |
175 |
| com_tr' (Const ("Assign",_) $ v $ e) =Syntax.const "@Assign" $ Syntax.free (pvar2pvarID v) $ exp_tr' e |
|
176 |
| com_tr' (Const ("Seq",_) $ c $ c') =Syntax.const "@Seq" $ com_tr' c $ com_tr' c' |
|
177 |
| com_tr' (Const ("Cond",_) $ b $ c $ c') =Syntax.const "@If" $ bexp_tr' b $ com_tr' c $ com_tr' c' |
|
178 |
| com_tr' (Const ("While",_) $ b $ inv $ c) =Syntax.const "@While" $ bexp_tr' b $ bexp_tr' inv $ com_tr' c; |
|
1335 | 179 |
|
1476 | 180 |
fun spec_tr' [p,c,q] =Syntax.const "@Spec" $ bexp_tr' p $ com_tr' c $ bexp_tr' q; |
1335 | 181 |
|
1476 | 182 |
val print_translation =[("Spec",spec_tr')]; |