| 1 |
-- FILE: /home/diacon/LANG/Cafe/prog/blist.mod |
| 2 |
-- CONTENTS: behavioural specification of a list object featuring |
| 3 |
-- use of coherence operations |
| 4 |
-- AUTHOR: Razvan Diaconescu |
| 5 |
-- DIFFICULTY: *** |
| 6 |
|
| 7 |
mod! BARE-NAT { |
| 8 |
|
| 9 |
[ NzNat Zero < Nat ] |
| 10 |
|
| 11 |
op 0 : -> Zero |
| 12 |
op s_ : Nat -> NzNat |
| 13 |
} |
| 14 |
|
| 15 |
mod! TRIV+ (X :: TRIV) { |
| 16 |
op err : -> ?Elt |
| 17 |
} |
| 18 |
|
| 19 |
-- list object |
| 20 |
mod* LIST { |
| 21 |
protecting (TRIV+) |
| 22 |
|
| 23 |
*[ List ]* |
| 24 |
|
| 25 |
op nil : -> List |
| 26 |
op cons : Elt List -> List -- method {coherent} |
| 27 |
bop car : List -> ?Elt -- attribute |
| 28 |
bop cdr : List -> List -- method |
| 29 |
|
| 30 |
vars E E' : Elt |
| 31 |
var L : List |
| 32 |
|
| 33 |
eq car(nil) = err . |
| 34 |
eq car(cons(E, L)) = E . |
| 35 |
beq cdr(nil) = nil . |
| 36 |
beq cdr(cons(E, L)) = L . |
| 37 |
} |
| 38 |
|
| 39 |
select LIST(NAT) . |
| 40 |
|
| 41 |
red car(cons(1, nil)) . |
| 42 |
red car(cdr(cons(1,nil))) . |
| 43 |
red car(cdr(cdr(cons(1, nil)))) . |
| 44 |
|
| 45 |
-- behavioural equivalence on lists |
| 46 |
mod* LIST-BEQ { |
| 47 |
protecting(LIST + BARE-NAT) |
| 48 |
-- 2nd order cdr function on List |
| 49 |
bop cdr : Nat List -> List |
| 50 |
|
| 51 |
eq cdr(0, L:List) = L . |
| 52 |
eq cdr(s(N:Nat), L:List) = cdr(N, cdr(L)) . |
| 53 |
-- behavioural equivalence relation |
| 54 |
op _R[_]_ : List Nat List -> Bool {coherent} |
| 55 |
|
| 56 |
eq L:List R[N:Nat] L':List = car(cdr(N, L)) == car(cdr(N, L')) . |
| 57 |
} |
| 58 |
|
| 59 |
-- proof of coherence of cons |
| 60 |
open LIST-BEQ . |
| 61 |
op n : -> Nat . |
| 62 |
ops l1 l2 : -> List . |
| 63 |
op e : -> Elt . |
| 64 |
** hypothesis |
| 65 |
beq l1 = l2 . |
| 66 |
** proof by case analysis |
| 67 |
red cons(e, l1) R[0] cons(e, l2) . |
| 68 |
red cons(e, l1) R[s n] cons(e, l2) . |
| 69 |
|
| 70 |
close |
| 71 |
|
| 72 |
mod* LIST+ { protecting (LIST) |
| 73 |
op cons : Elt List -> List {coherent} |
| 74 |
} |
| 75 |
mod* LIST+-BEQ { pr(LIST-BEQ + LIST+) } |
| 76 |
|
| 77 |
-- |
| 78 |
eof |
| 79 |
-- |
| 80 |
$Id: blist.mod,v 1.1.1.1 2003-06-19 08:30:10 sawada Exp $ |