| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/cws.mod |
| 2 |
-- CONTENTS: behavioural specification of a counter with switch object |
| 3 |
-- featuring concurrent object composition with synchronization |
| 4 |
-- AUTHOR: Shuusaku Iida |
| 5 |
-- DIFFICULTY: *** |
| 6 |
|
| 7 |
-- ------------------------------------------------------------- |
| 8 |
-- ON-OFF |
| 9 |
-- ------------------------------------------------------------- |
| 10 |
mod! ON-OFF { |
| 11 |
[ Value ] |
| 12 |
|
| 13 |
ops on off : -> Value |
| 14 |
} |
| 15 |
|
| 16 |
-- ------------------------------------------------------------- |
| 17 |
-- SWITCH |
| 18 |
-- ------------------------------------------------------------- |
| 19 |
mod* SWITCH { |
| 20 |
protecting(ON-OFF) |
| 21 |
|
| 22 |
*[ Switch ]* |
| 23 |
|
| 24 |
op init : -> Switch |
| 25 |
bop on_ : Switch -> Switch -- method |
| 26 |
bop off_ : Switch -> Switch -- method |
| 27 |
bop state_ : Switch -> Value -- attribute |
| 28 |
|
| 29 |
var S : Switch |
| 30 |
|
| 31 |
eq state init = off . |
| 32 |
eq state(on S) = on . |
| 33 |
eq state(off S) = off . |
| 34 |
} |
| 35 |
|
| 36 |
-- ------------------------------------------------------------- |
| 37 |
-- COUNTER |
| 38 |
-- ------------------------------------------------------------- |
| 39 |
mod* COUNTER { |
| 40 |
protecting(INT) |
| 41 |
|
| 42 |
*[ Counter ]* |
| 43 |
|
| 44 |
op init : -> Counter |
| 45 |
bop add : Int Counter -> Counter -- method |
| 46 |
bop read : Counter -> Int -- attribute |
| 47 |
|
| 48 |
var I : Int |
| 49 |
var C : Counter |
| 50 |
|
| 51 |
eq read(init) = 0 . |
| 52 |
eq read(add(I, C)) = I + read(C) . |
| 53 |
} |
| 54 |
|
| 55 |
-- -------------------------------------------- |
| 56 |
-- concurrent connection of SWITCH and COUNTER |
| 57 |
-- -------------------------------------------- |
| 58 |
mod* COUNTER-WITH-SWITCH { |
| 59 |
protecting(SWITCH + COUNTER) |
| 60 |
|
| 61 |
*[ Cws ]* |
| 62 |
|
| 63 |
op init : -> Cws |
| 64 |
bop put : Int Cws -> Cws -- method |
| 65 |
bop add_ : Cws -> Cws -- method |
| 66 |
bop sub_ : Cws -> Cws -- method |
| 67 |
bop read : Cws -> Int -- attribute |
| 68 |
bop counter_ : Cws -> Counter -- projection function |
| 69 |
bop switch_ : Cws -> Switch -- projection function |
| 70 |
|
| 71 |
var N : Int |
| 72 |
var C : Cws |
| 73 |
|
| 74 |
eq read(C) = read(counter C) . -- abbreviation equation for "read" |
| 75 |
|
| 76 |
-- ------------------------------------- |
| 77 |
-- equations for switch |
| 78 |
-- ------------------------------------- |
| 79 |
eq switch(init) = init . |
| 80 |
eq switch put(N, C) = switch C . |
| 81 |
eq switch add(C) = on(switch C) . |
| 82 |
eq switch sub(C) = off(switch C) . |
| 83 |
|
| 84 |
-- ------------------------------------- |
| 85 |
-- equations for counter |
| 86 |
-- ------------------------------------- |
| 87 |
eq counter(init) = init . |
| 88 |
ceq counter(put(N, C)) = add(N, counter(C)) |
| 89 |
if state(switch(C)) == on . |
| 90 |
ceq counter(put(N, C)) = add(-(N), counter(C)) |
| 91 |
if state(switch(C)) == off . |
| 92 |
eq counter add(C) = counter C . |
| 93 |
eq counter sub(C) = counter C . |
| 94 |
} |
| 95 |
|
| 96 |
-- ----------------------------------------------------------------- |
| 97 |
-- proof module (adding hidden equivalence relation to |
| 98 |
-- COUNTER-WITH-SWITCH) |
| 99 |
-- ----------------------------------------------------------------- |
| 100 |
module CWS-PROOF { |
| 101 |
protecting (COUNTER-WITH-SWITCH) |
| 102 |
bop addc : Int Cws -> Cws -- a derived method |
| 103 |
|
| 104 |
-- ------------------------------------- |
| 105 |
-- eqns for the derived method addc |
| 106 |
-- ------------------------------------- |
| 107 |
var C : Cws |
| 108 |
var N : Int |
| 109 |
|
| 110 |
ceq addc(N, C) = put(N, C) if state(switch C) == on . |
| 111 |
ceq addc(N, C) = put(-(N), C) if state(switch C) == off . |
| 112 |
|
| 113 |
-- the behavioural equivalence |
| 114 |
op _R_ : Cws Cws -> Bool { coherent } |
| 115 |
vars C1 C2 : Cws |
| 116 |
|
| 117 |
eq C1 R C2 = switch(C1) =*= switch(C2) and |
| 118 |
counter(C1) =*= counter(C2) . |
| 119 |
|
| 120 |
-- a lemma |
| 121 |
eq -(- N) = N . |
| 122 |
} |
| 123 |
|
| 124 |
--> --------------------------------------------------------------- |
| 125 |
--> sub put(m, add put(n, sub init)) R put(n, sub put(m, add init)) |
| 126 |
--> --------------------------------------------------------------- |
| 127 |
open CWS-PROOF . |
| 128 |
ops m n : -> Nat . |
| 129 |
--> should be true |
| 130 |
red sub(put(m, add(put(n, sub(init))))) R put(n, sub(put(m, add(init)))) . |
| 131 |
--> should be true |
| 132 |
red read(put(m, add(put(n, sub(init))))) == |
| 133 |
read(put(n, sub(put(m, add(init))))) . |
| 134 |
close |
| 135 |
|
| 136 |
-- ------------------------------------------------------ |
| 137 |
-- Theorem: COUNTER-WITH-SWITCH is a (correct) concurrent |
| 138 |
-- conection of SWITCH and COUNTER |
| 139 |
-- ------------------------------------------------------ |
| 140 |
-- the synchronization part consists only of a hidden sort |
| 141 |
-- the synchronization morphisms are obvious |
| 142 |
-- the morphism \psi_1 : SWITCH -> COUNTER-WITH-SWITCH is: |
| 143 |
-- -- init -> init |
| 144 |
-- -- on -> add |
| 145 |
-- -- off -> sub |
| 146 |
-- -- state -> state switch |
| 147 |
-- the morphism \psi_2 : COUNTER -> COUNTER-WITH-SWITCH is: |
| 148 |
-- -- init -> init |
| 149 |
-- -- add -> addc ** defined in CWS-PROOF |
| 150 |
-- -- read -> read |
| 151 |
|
| 152 |
--> prove that COUNTER-WITH-SWITCH refines COUNTER via \psi_2 |
| 153 |
|
| 154 |
open CWS-PROOF . |
| 155 |
op c : -> Cws . |
| 156 |
op n : -> Int . |
| 157 |
--> case 1: state(switch c) = on . |
| 158 |
eq state(switch c) = on . |
| 159 |
|
| 160 |
red read(addc(n, c)) == n + read(c) . |
| 161 |
close |
| 162 |
|
| 163 |
open CWS-PROOF . |
| 164 |
op c : -> Cws . |
| 165 |
op n : -> Int . |
| 166 |
--> case 2: state(switch c) = off . |
| 167 |
eq state(switch c) = off . |
| 168 |
|
| 169 |
red read(addc(n, c)) == n + read(c) . |
| 170 |
close |
| 171 |
|
| 172 |
--> prove that COUNTER-WITH-SWITCH refines SWITCH via \psi_1 |
| 173 |
|
| 174 |
open CWS-PROOF . |
| 175 |
op c : -> Cws . |
| 176 |
op n : -> Int . |
| 177 |
|
| 178 |
red state switch add(c) == on . |
| 179 |
red state switch sub(c) == off . |
| 180 |
close |
| 181 |
|
| 182 |
--> prove the commutativity eqns corresponding to the methods |
| 183 |
open CWS-PROOF . |
| 184 |
op c : -> Cws . |
| 185 |
op n : -> Int . |
| 186 |
--> case 1: |
| 187 |
eq state(switch c) = on . |
| 188 |
red add(addc(n, c)) R addc(n, add(c)) . |
| 189 |
red sub(addc(n, c)) R addc(n, sub(c)) . |
| 190 |
close |
| 191 |
|
| 192 |
open CWS-PROOF . |
| 193 |
op c : -> Cws . |
| 194 |
op n : -> Int . |
| 195 |
--> case 2: state(switch c) = off . |
| 196 |
eq state(switch c) = off . |
| 197 |
red add(addc(n, c)) R addc(n, add(c)) . |
| 198 |
red sub(addc(n, c)) R addc(n, sub(c)) . |
| 199 |
close |
| 200 |
|
| 201 |
--> prove the commutativity eqns corresponding to the attributes |
| 202 |
open CWS-PROOF . |
| 203 |
op c : -> Cws . |
| 204 |
op n : -> Int . |
| 205 |
red state(switch put(n, c)) == state(switch c) . |
| 206 |
red read(counter add(c)) == read(counter c) . |
| 207 |
red read(counter sub(c)) == read(counter c) . |
| 208 |
close |
| 209 |
-- |
| 210 |
eof |
| 211 |
-- |
| 212 |
$Id: cws.mod,v 1.1.1.1 2003-06-19 08:30:10 sawada Exp $ |