| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/bset.mod |
| 2 |
-- CONTENTS: behavioural specification of a set object featuring |
| 3 |
-- use of coherence operations, |
| 4 |
-- simplicity of behavioural proofs, and |
| 5 |
-- object refinement |
| 6 |
-- AUTHOR: Razvan Diaconescu |
| 7 |
-- DIFFICULTY: *** |
| 8 |
|
| 9 |
mod* BASICSETS (X :: TRIV) { |
| 10 |
-- protecting(PROPC) |
| 11 |
|
| 12 |
*[ Set ]* |
| 13 |
|
| 14 |
op empty : -> Set |
| 15 |
op add : Elt Set -> Set -- method {coherent} |
| 16 |
bop _in_ : Elt Set -> Bool -- attribute |
| 17 |
|
| 18 |
vars E E' : Elt |
| 19 |
var S : Set |
| 20 |
|
| 21 |
eq E in add(E', S) = (E == E') or (E in S) . |
| 22 |
eq E in empty = false . |
| 23 |
} |
| 24 |
|
| 25 |
** proof score for behavioural coherence of add(_) |
| 26 |
open BASICSETS . |
| 27 |
ops e e' : -> Elt . |
| 28 |
ops s s' : -> Set . |
| 29 |
** hypothesis |
| 30 |
beq s = s' . |
| 31 |
** behavioural coherence of add(_) by case analysis |
| 32 |
red e in add(e, s) == e in add(e, s') . |
| 33 |
red e' in add(e, s) == e' in add(e, s') . |
| 34 |
close |
| 35 |
|
| 36 |
mod* BASICSETS+ { protecting (BASICSETS) |
| 37 |
op add : Elt Set -> Set {coherent} |
| 38 |
} |
| 39 |
|
| 40 |
mod* SETS { |
| 41 |
protecting(BASICSETS+) |
| 42 |
|
| 43 |
op _U_ : Set Set -> Set -- {coherent} |
| 44 |
op _&_ : Set Set -> Set -- {coherent} |
| 45 |
op not : Set -> Set -- {coherent} |
| 46 |
|
| 47 |
var E : Elt |
| 48 |
vars S1 S2 : Set |
| 49 |
|
| 50 |
eq E in S1 U S2 = (E in S1) or (E in S2) . |
| 51 |
eq E in S1 & S2 = (E in S1) and (E in S2) . |
| 52 |
eq E in not(S1) = not (E in S1) . |
| 53 |
} |
| 54 |
|
| 55 |
** proof score for beh coherence of _U_, _&_, and not(_) |
| 56 |
open SETS . |
| 57 |
ops s1 s2 s1' s2' : -> Set . |
| 58 |
op e : -> Elt . |
| 59 |
** by theorem of constants |
| 60 |
ceq S1 =*= S2 = true if (e in S1) == (e in S2) . |
| 61 |
** hypothesis |
| 62 |
beq s1 = s1' . |
| 63 |
beq s2 = s2' . |
| 64 |
** beh coherence of _U_ |
| 65 |
red (s1 U s2) =*= (s1' U s2') . |
| 66 |
** beh coherence of _&_ |
| 67 |
red (s1 & s2) =*= (s1' & s2') . |
| 68 |
** beh coherence of not_ |
| 69 |
red (not(s1)) =*= (not(s1')) . |
| 70 |
close |
| 71 |
|
| 72 |
mod* SETS+ { protecting (SETS) |
| 73 |
op _U_ : Set Set -> Set {coherent} |
| 74 |
op _&_ : Set Set -> Set {coherent} |
| 75 |
op not : Set -> Set {coherent} |
| 76 |
} |
| 77 |
|
| 78 |
** *************************** *** |
| 79 |
**> Some behavioral properties *** |
| 80 |
** *************************** *** |
| 81 |
open SETS+ . |
| 82 |
op e : -> Elt . |
| 83 |
ops s1 s2 s3 : -> Set . |
| 84 |
** by theorem of constants |
| 85 |
ceq S1:Set =*= S2:Set = true if (e in S1) == (e in S2) . |
| 86 |
**> commutativity of _U_ |
| 87 |
red (s1 U s2) =*= (s2 U s1) . |
| 88 |
**> associativity of _U_ |
| 89 |
red (s1 U (s2 U s3)) =*= ((s1 U s2) U s3) . |
| 90 |
**> idempotency of _U_ |
| 91 |
red (s1 U s1) =*= s1 . |
| 92 |
**> empty is the unity of _U_ |
| 93 |
red (empty U s1) =*= s1 . |
| 94 |
**> commutativity of _&_ |
| 95 |
red (s1 & s2) =*= (s2 & s1) . |
| 96 |
**> associativity of _&_ |
| 97 |
red (s1 & (s2 & s3)) =*= ((s1 & s2) & s3) . |
| 98 |
**> idempotency of _&_ |
| 99 |
red (s1 & s1) =*= s1 . |
| 100 |
**> empty & S is empty |
| 101 |
red (empty & s1) =*= empty . |
| 102 |
**> distributivity |
| 103 |
red (s1 & (s2 U s3)) =*= ((s1 & s2) U (s1 & s3)) . |
| 104 |
red (s1 U (s2 & s3)) =*= ((s1 U s2) & (s1 U s3)) . |
| 105 |
**> de Morgan laws |
| 106 |
red (not(s1 U s2)) =*= (not(s1) & not(s2)) . |
| 107 |
red (not(s1 & s2)) =*= (not(s1) U not(s2)) . |
| 108 |
**> double negation |
| 109 |
red (not(not(s1))) =*= s1 . |
| 110 |
close |
| 111 |
|
| 112 |
-- ============================================================== |
| 113 |
-- REFINEMENT of the BASIC-SET object by LIST object |
| 114 |
-- ============================================================== |
| 115 |
|
| 116 |
mod! TRIV+ (X :: TRIV) { |
| 117 |
op err : -> ?Elt |
| 118 |
} |
| 119 |
|
| 120 |
-- the list object |
| 121 |
mod* LIST { |
| 122 |
protecting (TRIV+) |
| 123 |
|
| 124 |
*[ List ]* |
| 125 |
|
| 126 |
op nil : -> List |
| 127 |
op cons : Elt List -> List {coherent} -- method |
| 128 |
-- provable from the rest of spec |
| 129 |
bop car : List -> ?Elt -- attribute |
| 130 |
bop cdr : List -> List -- method |
| 131 |
|
| 132 |
vars E E' : Elt |
| 133 |
var L : List |
| 134 |
|
| 135 |
eq car(nil) = err . |
| 136 |
eq car(cons(E, L)) = E . |
| 137 |
beq cdr(nil) = nil . |
| 138 |
beq cdr(cons(E, L)) = L . |
| 139 |
} |
| 140 |
|
| 141 |
-- LIST refines BASICSETS+ via the morphism |
| 142 |
-- add |--> cons |
| 143 |
-- E in L |--> (E == car(L)) or-else (car(L) =/= err and-also E in cdr(L)) |
| 144 |
|
| 145 |
mod* LIST' { |
| 146 |
protecting (LIST) |
| 147 |
-- a derived attribute |
| 148 |
op _in_ : Elt List -> Bool {coherent} |
| 149 |
-- coherence provable from the rest of spec |
| 150 |
vars E E' : Elt |
| 151 |
var L : List |
| 152 |
|
| 153 |
eq E in L = (E == car(L)) or-else (car(L) =/= err and-also E in cdr(L)) . |
| 154 |
} |
| 155 |
|
| 156 |
open LIST'(NAT) . |
| 157 |
red 1 in cons(2,cons(3,cons(4,nil))) . |
| 158 |
red 1 in cdr(cons(1,cons(2,cons(3,cons(4,nil))))) . |
| 159 |
red 1 in cdr(cons(1,cons(2,cons(3,cons(4,cons(1,nil)))))) . |
| 160 |
close |
| 161 |
|
| 162 |
-- proof that LIST refines BASICSETS |
| 163 |
open LIST' . |
| 164 |
ops e e1 e2 : -> Elt . |
| 165 |
op l : -> List . |
| 166 |
-- basic cases |
| 167 |
eq e1 in l = true . |
| 168 |
eq e2 in l = false . |
| 169 |
-- case analysis |
| 170 |
red e in nil == false . |
| 171 |
red e1 in cons(e,l) == true . |
| 172 |
red e2 in cons(e,l) == false . |
| 173 |
red e in cons(e,l) == true . |
| 174 |
close |
| 175 |
-- |
| 176 |
eof |
| 177 |
-- |
| 178 |
$Id: bset.mod,v 1.2 2007-02-05 04:44:54 sawada Exp $ |