Debian Codegraph @

doc/refman/cloudsync.md

raw
1 CloudSync
2 =========
3
4 In the following we will model a very simple protocol for cloud
5 synchronization of a set of PCs. The full code of the actual
6 specification, as well as parts of the verification proof score will
7 be included and discussed.
8
9 Besides giving an example of a specification and verification, we also
10 try to explain several of the most important concepts in CafeOBJ
11 using rather simple examples.
12
13 Protocol
14 --------
15
16 One cloud computer and arbitrary many PCs have one value each that
17 they want to keep in sync. This value is a natural number, and higher
18 values mean more recent (like SVN revision numbers).
19
20 The Cloud can be in two states, *idle* and *busy*, while the PCs can
21 be on of the following three states: *idle*, *gotvalue*, *updated*.
22 The Cloud as well as all PCs are initially in the *idle* state.
23 When a PC connects to the cloud, three things happen:
24
25 1. the cloud changes into *busy* state
26 2. the PC reads the value of the cloud and saves it in a temporary
27 location
28 3. the PC changes into *gotvalue* state
29
30 In the *gotvalue* state the PC compares his own value against the
31 value it got from the cloud, and updates accordingly (changes either
32 the cloud or the own value to the larger one). After this the PC
33 changes into the *updated* state.
34
35 From the *update* state both the Cloud and the PC return into the
36 *idle* state.
37
38 TODO include a graphic that shows this TODO
39
40 Specification
41 -------------
42
43 We will now go through the full specification with explanations of
44 some of the points surfacing. We are starting with two modules that
45 specify the possible states the cloud and the PCs can be in:
46 `````
47 mod! CLLABEL {
48 [ClLabelLt < ClLabel]
49 ops idlecl busy : -> ClLabelLt {constr} .
50 eq (L1:ClLabelLt = L2:ClLabelLt) = (L1 == L2) .
51 }
52 mod! PCLABEL {
53 [PcLabelLt < PcLabel]
54 ops idlepc gotvalue updated : -> PcLabelLt {constr} .
55 eq (L1:PcLabelLt = L2:PcLabelLt) = (L1 == L2) .
56 }
57 `````
58 Both modules define two new sorts each, the actual label, and literals
59 for the labels. One can see that we declare the signatures of the
60 literal labels with the [`ops`](#op) keyword, which introduces several
61 operators of the same signature at the same time.
62
63 The last equation in each models provides a definition of equality by
64 using the _behavioral_ equality `==`.
65 The predicate `==` is the equivalence predicate defined via
66 reduction. Thus, the two axioms given above state that two literals
67 for labels are the same if they are syntactically the same, since they
68 cannot be rewritten anymore.
69
70 Furthermore, note that we choose different names for the *idle* state
71 of the PCs and the cloud, to have easy separation.
72
73 The next module introduces a parametrized pair module. Parametrizing
74 modules is a very powerful construction, and common in object oriented
75 programming languages. In principle we leave open what are the actual
76 components of the pairs, and only specify the operational behavior on
77 a single pair.
78
79 In this and the next example of the multi-set, there are no additional
80 requirements on the sorts that can be used to instantiate a pair (or
81 multi-set). In a more general setting the argument after the double
82 colon `::` refers to a sort, and an instantiation must be adequate for
83 this sort (details require deeper understanding of homomorphism).
84 `````
85 mod! PAIR(X :: TRIV,Y :: TRIV) {
86 [Pair]
87 op <_,_> : Elt.X Elt.Y -> Pair {constr}
88 op fst : Pair -> Elt.X
89 op snd : Pair -> Elt.Y
90 eq fst(< A:Elt.X,B:Elt.Y >) = A .
91 eq snd(< A:Elt.X,B:Elt.Y >) = B .
92 }
93 `````
94
95 The next module is also parametrized, axiomatizing the concept of
96 multi-set where a certain element can appear multiple times in the
97 multi-set. We want to use this module to present another feature,
98 namely the option to specify additional properties of some
99 operators. In this case we are specifying that the constructor for
100 sets is associative `assoc`, commutative `comm`, and has as identity
101 the `empty` set.
102
103 While it is easily possible to add associativity and commutativity as
104 axioms directly, this is not advisable, especially for
105 commutativity. Assume adding the simple equation `eq A * B = B * A .`.
106 This defines a rewrite rule from left to right. But since `A` and `B`
107 are variables the can be instantiated with arbitrary subterms, and one
108 would end up with an infinite rewriting.
109 `````
110 mod MULTISET(X :: TRIV) {
111 [ Elt.X < MultiSet ]
112 op empty : -> MultiSet {constr} .
113 -- associative and commutative set constructor with identity empty
114 op (_ _) : MultiSet MultiSet -> MultiSet { constr assoc comm id: empty }
115 }
116 `````
117
118 With all this set up we can defined the cloud state as a pair of a
119 natural number, and a state. Here we see how a parametrized module is
120 instantiated. The details of the renaming for the second element are a
121 bit involved, but thinking about renaming of sorts and operators to
122 match the ones given is the best idea.
123
124 Having this in mind we see that when we put the `CLLABEL` into the
125 second part of the pair, we tell the system that it should use the
126 `ClLabel` sort for the instantiation of the `Elt` sort, and not the
127 `ClLabelLt` sort.
128
129 Furthermore, after the instantiation we rename the final outcome
130 again. In this case we rename the `Pair` to `ClState`, and the
131 operators to their cousins with extension in the name.
132 `````
133 mod! CLSTATE {
134 pr(PAIR(NAT, CLLABEL{sort Elt -> ClLabel})*
135 {sort Pair -> ClState, op fst -> fst.clstate, op snd -> snd.clstate })
136 }
137 `````
138
139 The PC state is now very similar, only that we have to have a triple
140 (`3TUPLE` is a builtin predicate of CafeOBJ), since we need one
141 additional place for the temporary value. In the same way as above we
142 rename the `Elt` to `PcLabel` and the outcome back to `PcState`.
143 `````
144 mod! PCSTATE {
145 pr(3TUPLE(NAT, NAT, PCLABEL{sort Elt -> PcLabel})*{sort 3Tuple -> PcState})
146 }
147 `````
148
149 As we will have an arbitrary set of PCs, we define the multi-set of
150 all PC states, by instatiating the multi-set from above with the just
151 defined `PcState` sort, and rename the result to `PcStates`.
152 `````
153 mod! PCSTATES {
154 pr(MULTISET(PCSTATE{sort Elt -> PcState})*{sort MultiSet -> PcStates})
155 }
156 `````
157
158 Finally, the state of the whole system is declared as a pair of the
159 cloud state and the pc states.
160 `````
161 mod! STATE {
162 pr(PAIR(CLSTATE{sort Elt -> ClState},
163 PCSTATES{sort Elt -> PcStates})*{sort Pair -> State})
164 }
165 `````
166
167 The final part is to specify transitions. We have described the
168 protocol by a state machine, and the following transitions will model
169 the transitions in this machine.
170
171 The first transition is the initialization of the synchronization by
172 reading the cloud value, saving it into the local register, and both
173 partners go into busy state.
174
175 Note that, since we have declared multi-set as commutative and
176 associative, we can assume that the first element of the multi-set is
177 actually the one we are acting on.
178
179 Transitions are different from axioms in the sense that the do not
180 state that two terms are the same, but only that one terms can change
181 into another.
182 `````
183 mod! GETVALUE { pr(STATE)
184 trans[getvalue]:
185 < < ClVal:Nat , idlecl > ,
186 ( << PcVal:Nat ; OldClVal:Nat ; idlepc >> S:PcStates ) >
187 =>
188 < < ClVal , busy > , ( << PcVal ; ClVal ; gotvalue >> S ) > .
189 }
190 `````
191
192 The next transition is the critical part, the update of the side with
193 the lower value. Here we are using the built-in `if ... then ... else
194 ... fi` operator.
195 `````
196 mod! UPDATE { pr(STATE)
197 trans[update]:
198 < < ClVal:Nat , busy > ,
199 ( << PcVal:Nat ; GotClVal:Nat ; gotvalue >> S:PcStates ) >
200 =>
201 if PcVal <= GotClVal then
202 < < ClVal , busy > , ( << GotClVal ; GotClVal ; updated >> S ) >
203 else
204 < < PcVal , busy > , ( << PcVal ; PcVal ; updated >> S ) >
205 fi .
206 }
207 `````
208
209 The last transition is sending the both sides of the synchronization
210 into the idle states.
211 `````
212 mod! GOTOIDLE { pr(STATE)
213 trans[gotoidle]:
214 < < ClVal:Nat , busy > ,
215 ( << PcVal:Nat ; OldClVal:Nat ; updated >> S:PcStates ) >
216 =>
217 < < ClVal , idlecl > , ( << PcVal ; OldClVal ; idlepc >> S ) > .
218 }
219 `````
220
221 This completes the complete specification of the protocol, and we are
222 defining a module `CLOUD` that collects all that.
223 `````
224 mod! CLOUD { pr(GETVALUE + UPDATE + GOTOIDLE) }
225 `````
226
227
228
229 Verification
230 ------------
231
232 Aim of the verification is to show *correctness* in the sense that no
233 two PCs are at the same time in the busy state. The idea of the proof
234 is to show using induction on the length of transition sequences from
235 initial states to reachable states, that for all reachable states this
236 property is fulfilled.
237
238 More specific, we give a characterization of initial states, and show
239 that for initial states the property holds (base case of the
240 induction). Then we show that for all possible transitions, if the
241 target property holds at the beginning of the transition, it also
242 holds at the end of the transition.
243
244 Combining this with a (meta-level) induction proof on the length of
245 transition sequences, we show that the target property holds for all
246 reachable states.
247
248 Like with loop invariants in other verification schemes, it turns out
249 that a single target property, the exclusion property mentioned above,
250 does not suffice to hold over transitions, i.e., act as transition
251 invariant. Thus, we have to extended it with additional properties.
252
253 The first part of this mini-tutorial on the specification of CloudSync
254 contained the full code, but in the following we will, due to space
255 reasons, only include partial code. The latest version of the
256 CloudSync code can be obtained from \cite{preining:cafeobj}.
257
258 But let us start with the definition of predicates for the initial
259 states. The first step is to define some elementary functions on
260 states, counting how many PCs are in a certain state:
261 `````
262 mod! STATEfuncs {
263 pr(NAT + STATE)
264 -- no pc in gotvalue state
265 pred zero-gotvalue : State .
266 pred zero-updated : State .
267 ...
268 }
269 `````
270
271 We are collecting a set of predicates, indicated by their predicate
272 name, and define `apply` as an operator that checks each single
273 predicate against a state, and forms the conjunct of the results.
274 `````
275 mod! APPLYPREDS {
276 pr(STATE)
277 [PredName < PredNameSeq]
278 op (_ _) : PredNameSeq PredNameSeq -> PredNameSeq {assoc} .
279 op apply : PredNameSeq State -> Bool .
280 eq apply(P:PredName PS:PredNameSeq, S:State) = apply(P,S) and apply(PS,S) .
281 }
282 `````
283
284 Characterization of the initial state is easy, as it only requires
285 that all PCs as well as the cloud is in idle state.
286 `````
287 mod! INITPREDS {
288 ...
289 op cl-is-idle-name : -> PredName .
290 op pcs-are-idle-name : -> PredName .
291 ...
292 }
293 `````
294
295 In the following we define the predicate specifying initial states:
296 `````
297 mod! INITIALSTATE {
298 pr(INITPREDS)
299 op init-name : -> PredNameSeq .
300 eq init-name = cl-is-idle-name pcs-are-idle-name .
301 pred init : State .
302 eq init(S:State) = apply(init-name, S) .
303 }
304 `````
305
306 Let us now turn to the most difficult part, that is finding an
307 invariant. This is not a one-shot technique, but mostly iterative. One
308 starts with a set of predicates, and realizes that the proofs don't
309 work out properly, due to some missing properties. Thus, we add new
310 predicates and iterate until the induction proof finally succeeds.
311
312 In the following case we ended up with five different predicates that
313 combined worked as invariant:
314
315 `cloud-idle-pcs-idle`
316 : If the cloud is in the idle state, then all the pcs are also in
317 the idle state.
318
319 `pc-clval`
320 : If the cloud is in busy state, then the value of the cloud and
321 the value in the temporary storage area of any PCs in the
322 `gotvalue` or `updated` states agree.
323
324 `one-active`
325 : At most one PC is out of the idle state.
326
327 `gotvalue-cloud-value`
328 : If a PC is in the `gotvalue` state, then the value saved in the
329 temporary storage area and the one of the cloud agree.
330
331 `goal`
332 : If a PC is in the `updated` state, then the value of the PC and
333 the value of the cloud agree.
334
335 See the mentioned web-page for the full code of these modules.
336
337 In addition to the necessity to introduce additional predicates to
338 obtain an invariant, it also often turns out that some properties, or
339 lemmas, have to be stated or proven so that the verification can work
340 out. In our case some properties on `if_then_else_fi` constructs, as
341 well as consequences of rewriting are included in a module
342 `NECESSARYFACTS`.
343
344 The final - and one of the most important parts - is the proof of the
345 two properties:
346
347 - base case: if a state satisfies the initial state predicate, it also
348 satisfies the invariant:
349 `````
350 red init(S) implies invariant(S) .
351 `````
352 - induction step: if a state satisfies the invariant, and we apply a
353 transition, then the next state also satisfies the invariant:
354 `````
355 red inv-condition(S, SS) .
356 ````
357
358 In both cases we cannot work with a general variable `S`, as in this
359 case no rewriting can take place, and we will not obtain true. What
360 has to be done is to provide a covering set of state expressions,
361 i.e., a set of terms such that every possible instance of a state is
362 also an instance of one of these terms. In our case this is quite easy
363 to provide and consists of six different state terms, combining the
364 three possibilities for a PC with two options of states for the cloud:
365 `````
366 ops s1 s2 s3 s4 t1 t2 t3 t4 : -> State .
367 eq s1 = < < N , idlecl > , ( << M ; K ; idlepc >> PCS ) > .
368 eq s2 = < < N , idlecl > , ( << M ; K ; gotvalue >> PCS ) > .
369 eq s3 = < < N , idlecl > , ( << M ; K ; updated >> PCS ) > .
370 eq t1 = < < N , busy > , ( << M ; K ; idlepc >> PCS ) > .
371 eq t2 = < < N , busy > , ( << M ; K ; gotvalue >> PCS ) > .
372 eq t3 = < < N , busy > , ( << M ; K ; updated >> PCS ) > .
373 `````
374 It is easy to see that any arbitrary state term can be obtained as
375 instance of one of these six state terms.
376
377 What we then show is that the above properties do hold for each of
378 these terms, and thus for each of the reachable states. In details, we
379 show that:
380 `````
381 red init(s1) implies invariant(s1) .
382 red init(s2) implies invariant(s2) .
383 red init(s3) implies invariant(s3) .
384 red init(t1) implies invariant(t1) .
385 red init(t2) implies invariant(t2) .
386 red init(t3) implies invariant(t3) .
387 `````
388 all of these expressions reduce to `true`. And furthermore, all of the
389 following expressions, too:
390 `````
391 red inv-condition(s1, SS) .
392 red inv-condition(s2, SS) .
393 red inv-condition(s3, SS) .
394 red inv-condition(t1, SS) .
395 red inv-condition(t2, SS) .
396 red inv-condition(t3, SS) .
397 `````
398 Unfortunately, in the case of `t2` this didn't turn out to be directly
399 possible, and a further case distinction was necessary to complete the
400 proof.
401
402 This concludes the presentation of the CloudSync protocol. We
403 described the cloud protocol using a _state system_ and
404 transitions. This is just one way of implementation. There are other
405 approaches to specification using purely term-based expressions that
406 do not use transitions, but equational theory only.
407 One of the strength of CafeOBJ is that it does not require any
408 specific approach to modeling, but allows for freedom in choosing
409 methodology.
410
411