Debian Codegraph @

doc/examples/bank-account.mod

raw
1 -- FILE: /home/diacon/LANG/Cafe/prog/bank-account.mod
2 -- CONTENTS: a bank account system specification featuring
3 -- concurrent object composition,
4 -- behavioural verification
5 -- automatic generation of case analysis by meta-level encoding
6 -- AUTHORS: Shuusaku Iida & Razvan Diaconescu
7 -- DIFFICULTY: ****
8
9 -- counter object
10 mod* COUNTER(X :: TRIV) {
11 protecting(INT)
12
13 *[ Counter ]*
14
15 op init-counter : Elt -> Counter -- initial state
16 op no-counter : -> Counter -- error
17 bop add : Int Counter -> Counter -- method
18 bop amount_ : Counter -> Nat -- attribute
19
20 var ID : Elt
21 var I : Int
22 var C : Counter
23
24 beq add(I, no-counter) = no-counter .
25 eq amount init-counter(ID) = 0 .
26 ceq amount add(I, C) = I + amount(C) if I + amount(C) >= 0 .
27 ceq amount add(I, C) = amount(C) if I + amount(C) < 0 .
28 }
29
30 -- user identifiers
31 mod* USER-ID {
32 [ UId ]
33 }
34
35 -- account object
36 mod* ACCOUNT {
37 protecting(COUNTER(X <= view to USER-ID { sort Elt -> UId })
38 *{ hsort Counter -> Account,
39 op init-counter -> init-account,
40 op no-counter -> no-account })
41 }
42
43 -- system of accounts as a dynamic object
44 mod* ACCOUNT-SYS {
45 protecting(ACCOUNT)
46
47 *[ AccountSys ]*
48
49 op init-account-sys : -> AccountSys
50 bop add-account : UId Nat AccountSys -> AccountSys
51 bop del-account : UId AccountSys -> AccountSys
52 bop deposit : UId Nat AccountSys -> AccountSys
53 bop withdraw : UId Nat AccountSys -> AccountSys
54 bop balance : UId AccountSys -> Nat
55 bop account : UId AccountSys -> Account {memo}
56
57 vars U U' : UId
58 var A : AccountSys
59 var N : Nat
60
61 eq account(U, init-account-sys) = no-account .
62 ceq account(U, add-account(U', N, A)) = add(N, init-account(U))
63 if U == U' .
64 ceq account(U, add-account(U', N, A)) = account(U, A)
65 if U =/= U' .
66 ceq account(U, del-account(U', A)) = no-account
67 if U == U' .
68 ceq account(U, del-account(U', A)) = account(U, A)
69 if U =/= U' .
70 ceq account(U, deposit(U', N, A)) = add(N, account(U, A))
71 if U == U' .
72 ceq account(U, deposit(U', N, A)) = account(U, A)
73 if U =/= U' .
74 ceq account(U, withdraw(U', N, A)) = add(-(N), account(U, A))
75 if U == U' .
76 ceq account(U, withdraw(U', N, A)) = account(U, A)
77 if U =/= U' .
78
79 eq balance(U, A) = amount account(U, A) .
80 }
81
82 -- behavioural equivalence for the account object
83 mod BEQ-ACCOUNT {
84 protecting(ACCOUNT)
85
86 -- op _=*=_ : Account Account -> Bool { coherent }
87
88 vars A1 A2 : Account
89
90 eq A1 =*= A2 = amount(A1) == amount(A2) .
91 }
92
93 -- -------------------------------------------------------------
94 -- proof that =*= is hidden congruence
95 -- -------------------------------------------------------------
96 -- case 1: i + amount(a1) >= 0
97 open BEQ-ACCOUNT .
98 -- hypothesis
99 ops a1 a2 : -> Account .
100 op i : -> Int .
101 eq i + amount(a2) >= 0 = true .
102 -- eq a1 =*= a2 = true .
103 eq amount(a1) = amount(a2) .
104 red add(i, a1) =*= add(i, a2) .
105 close
106
107 -- case 2: i + amount(a1) < 0
108 open BEQ-ACCOUNT .
109 -- hypothesis
110 ops a1 a2 : -> Account .
111 op i : -> Int .
112 eq i + amount(a2) < 0 = true .
113 -- eq a1 =*= a2 = true .
114 eq amount(a1) = amount(a2) .
115 red add(i, a1) =*= add(i, a2) .
116 close
117
118 -- behavioural equivalence for the system of accounts
119 mod BEQ-ACCOUNT-SYS {
120 protecting(BEQ-ACCOUNT)
121 protecting(ACCOUNT-SYS)
122
123 op _R[_]_ : AccountSys UId AccountSys -> Bool { coherent }
124
125 vars BA1 BA2 : AccountSys
126 var U : UId
127
128 eq BA1 R[ U ] BA2 = account(U, BA1) =*= account(U, BA2) .
129 }
130
131 -- proof that withdraw(u, n, ba) =b= ba if the user u does not have any account
132 open BEQ-ACCOUNT-SYS .
133 op ba : -> AccountSys .
134 ops u u' : -> UId .
135 op n : -> Nat .
136 -- assumption
137 eq account(u, ba) = no-account .
138 red withdraw(u, n, ba) R[ u ] ba .
139 red withdraw(u, n, ba) R[ u' ] ba .
140 close
141
142
143 mod PROOF {
144 -- organize the proof of the behavioural property
145 protecting(BEQ-ACCOUNT-SYS)
146
147 op ba : -> AccountSys
148 ops u u1 u2 : -> UId
149 ops n1 n2 n01 n02 m1 m1' m2 m2' : -> Nat
150
151 -- atomic cases
152 eq n1 =/= 0 = true .
153 eq n2 =/= 0 = true .
154 eq n01 == 0 = true .
155 eq n02 == 0 = true .
156 eq n1 <= m1 = true .
157 eq n01 <= m1 = true .
158 eq n1 > m1' = true .
159 eq n2 <= m2 = true .
160 eq n02 <= m2 = true .
161 eq n2 > m2' = true .
162
163 op state-of-system : Nat Nat -> AccountSys
164 ops W1W2 W2W1 : Nat Nat Nat Nat -> AccountSys
165 -- the meta-level encoding generating the proof term containing all cases
166 op TERM : UId Nat Nat Nat Nat -> Bool
167 op TERM1 : UId Nat Nat -> Bool
168 op TERM2 : UId -> Bool
169 op RESULT : -> Bool
170
171 var U : UId
172 vars N1 N2 M1 M2 : Nat
173
174 -- basic assumptions
175 eq state-of-system(M1, M2) = add-account(u1, M1,
176 add-account(u2, M2, ba)) .
177
178 eq W1W2(N1, N2, M1, M2) =
179 withdraw(u1, N1, withdraw(u2, N2, state-of-system(M1, M2))) .
180
181 eq W2W1(N1, N2, M1, M2) =
182 withdraw(u2, N2, withdraw(u1, N1, state-of-system(M1, M2))) .
183
184 -- the parameterized proof term
185 eq TERM(U, N1, N2, M1, M2) = W1W2(N1, N2, M1, M2) R[U] W2W1(N1, N2, M1, M2) .
186
187 eq TERM1(U, N2, M2) = TERM(U, n1, N2, m1, M2) and
188 TERM(U, n1, N2, m1', M2) and
189 TERM(U, n01, N2, m1, M2) .
190
191 eq TERM2(U) = TERM1(U, n2, m2) and
192 TERM1(U, n2, m2') and
193 TERM1(U, n02, m2) .
194
195 eq RESULT = TERM2(u) and TERM2(u1) and TERM2(u2) .
196 }
197
198 -- set tram path brute .
199 -- tram red RESULT .
200 -- tram reduce in PROOF : RESULT .
201 reduce in PROOF : RESULT .
202 eof
203 --
204 $Id: bank-account.mod,v 1.1.1.1 2003-06-19 08:30:10 sawada Exp $