| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/bag.mod |
| 2 |
-- CONTENTS: behavioural specification of bags featuring use of |
| 3 |
-- coherent methods |
| 4 |
-- AUTHOR: Razvan Diaconescu |
| 5 |
-- DIFFICULTY: *** |
| 6 |
|
| 7 |
-- user-defined naturals |
| 8 |
mod! BARE-NAT { |
| 9 |
|
| 10 |
[ NzNat Zero < Nat ] |
| 11 |
|
| 12 |
op 0 : -> Zero |
| 13 |
op s_ : Nat -> NzNat |
| 14 |
} |
| 15 |
|
| 16 |
-- naturals with a predecesor function |
| 17 |
mod! BARE-NATP { |
| 18 |
protecting(BARE-NAT) |
| 19 |
|
| 20 |
op p_ : Nat -> Nat |
| 21 |
|
| 22 |
eq p 0 = 0 . |
| 23 |
eq p s N:Nat = N . |
| 24 |
} |
| 25 |
|
| 26 |
red in BARE-NATP : p s s p p s 0 . |
| 27 |
|
| 28 |
--> version with PUT and TAKE as bop methods: |
| 29 |
mod* BAG(X :: TRIV) { |
| 30 |
protecting(BARE-NATP) |
| 31 |
|
| 32 |
*[ Bag ]* |
| 33 |
|
| 34 |
op empty : -> Bag |
| 35 |
bop put : Elt Bag -> Bag -- method |
| 36 |
bop take : Elt Bag -> Bag -- method |
| 37 |
bop get : Bag Elt -> Nat -- attribute |
| 38 |
|
| 39 |
vars E E' : Elt |
| 40 |
var B : Bag |
| 41 |
|
| 42 |
eq get(empty, E) = 0 . |
| 43 |
cq get(put(E, B), E') = get(B, E') if E =/= E' . |
| 44 |
eq get(put(E, B), E) = s(get(B, E)) . |
| 45 |
cq get(take(E, B), E') = get(B, E') if E =/= E' . |
| 46 |
eq get(take(E, B), E) = p(get(B, E)) . |
| 47 |
} |
| 48 |
|
| 49 |
--> proof that =*= is hidden congruence |
| 50 |
-- the system cannot prove it by itself because of the case analysis |
| 51 |
open BAG . |
| 52 |
ops b1 b2 : -> Bag . |
| 53 |
ops e e' : -> Elt . |
| 54 |
var E : Elt |
| 55 |
-- hypothesis |
| 56 |
eq get(b1, E) = get(b2, E) . |
| 57 |
-- case e =/= e' |
| 58 |
red get(put(e, b1), e') == get(put(e, b2), e') . |
| 59 |
red get(take(e, b1), e') == get(take(e, b2), e') . |
| 60 |
-- case e == e' |
| 61 |
red get(put(e, b1), e) == get(put(e, b2), e) . |
| 62 |
red get(take(e, b1), e) == get(take(e, b2), e) . |
| 63 |
close |
| 64 |
|
| 65 |
--> version with PUT and TAKE as coherent methods |
| 66 |
mod* BAG(X :: TRIV) { |
| 67 |
protecting(BARE-NATP) |
| 68 |
|
| 69 |
*[ Bag ]* |
| 70 |
|
| 71 |
op empty : -> Bag |
| 72 |
op put : Elt Bag -> Bag |
| 73 |
op take : Elt Bag -> Bag |
| 74 |
bop get : Bag Elt -> Nat -- attribute |
| 75 |
|
| 76 |
vars E E' : Elt |
| 77 |
var B : Bag |
| 78 |
|
| 79 |
eq get(empty, E) = 0 . |
| 80 |
cq get(put(E, B), E') = get(B, E') if E =/= E' . |
| 81 |
eq get(put(E, B), E) = s(get(B, E)) . |
| 82 |
cq get(take(E, B), E') = get(B, E') if E =/= E' . |
| 83 |
eq get(take(E, B), E) = p(get(B, E)) . |
| 84 |
} |
| 85 |
|
| 86 |
--> proof that coherence of put & take is a consequence of the rest of spec |
| 87 |
open BAG . |
| 88 |
ops b1 b2 : -> Bag . |
| 89 |
ops e e' : -> Elt . |
| 90 |
var E : Elt |
| 91 |
-- hypothesis |
| 92 |
eq get(b1, E) = get(b2, E) . |
| 93 |
-- case e =/= e' |
| 94 |
red get(put(e, b1), e') == get(put(e, b2), e') . |
| 95 |
red get(take(e, b1), e') == get(take(e, b2), e') . |
| 96 |
-- case e == e' |
| 97 |
red get(put(e, b1), e) == get(put(e, b2), e) . |
| 98 |
red get(take(e, b1), e) == get(take(e, b2), e) . |
| 99 |
close |
| 100 |
|
| 101 |
mod* BAG+ { protecting (BAG) |
| 102 |
op put : Elt Bag -> Bag {coherent} |
| 103 |
op take : Elt Bag -> Bag {coherent} |
| 104 |
} |
| 105 |
|
| 106 |
--> take(e', put(e, b)) =*= put(e, take(e', b)) |
| 107 |
-- open . |
| 108 |
open BAG+ . |
| 109 |
ops e e' e1 : -> Elt . |
| 110 |
op b : -> Bag . |
| 111 |
red get(take(e', put(e, b)), e1) == get(put(e, take(e', b)), e1) . |
| 112 |
red get(take(e', put(e, b)), e) == get(put(e, take(e', b)), e) . |
| 113 |
red get(take(e', put(e, b)), e') == get(put(e, take(e', b)), e') . |
| 114 |
--> take(e, put(e, b)) is NOT beh equiv to put(e, take(e, b)) |
| 115 |
red get(take(e, put(e, b)), e) == get(put(e, take(e, b)), e) . |
| 116 |
close |
| 117 |
eof |
| 118 |
-- |
| 119 |
$Id: bag.mod,v 1.1.1.1 2003-06-19 08:30:10 sawada Exp $ |