Sie sind auf Seite 1von 31

Sets, lists, and maps

Chris George
United Nations University
International Institute for Software Technology
Macao SAR, China

Chris George, UNU-IIST

Sets
finite and infinite sets
set type expressions
set operators
set value expressions
example of specification using sets

Chris George, UNU-IIST

Sets
A set is:
an unordered

collection
of
values

of same type

Examples:
{1,3,5}
00
00 00
00 00
00
{ John , Peter , Ann }

Chris George, UNU-IIST

Set Type Expressions


type expr-set
{v1 , . . . , vn }
where n 0, vi : type expr
type expr-infset
{v1 , . . . , vn },
{v1 , . . . , vn , . . . }
where n 0, vi : type expr

Chris George, UNU-IIST

Associated Built-in Operators


: T-infset T-infset T-infset

{1, 2, 3} {3, 4} = {1, 2, 3, 4}

: T-infset T-infset T-infset

{1, 2, 3} {3, 4} = {3}

\ : T-infset T-infset T-infset

{1, 2, 3} \ {3, 4} = {1, 2}

: T T-infset Bool

4 {1, 2, 3} = false

6 : T T-infset Bool

4 6 {1, 2, 3} = true

Chris George, UNU-IIST

: T-infset T-infset Bool

{1, 3} {1, 2, 3} = true


{1, 2, 3} {1, 2, 3} = false

: T-infset T-infset Bool

{1, 3} {1, 2, 3} = true


{1, 2, 3} {1, 2, 3} = true
{1, 2, 3} {1, 3} = false

and are similar

card : T-infset Nat

Chris George, UNU-IIST

card {1, 2, 5, 2, 2, 1, 5} = 3
card {n | n : Nat} chaos

Overloading of hd
Theory:

hd : T-infset T
hd(s) as x post x s
pre s 6= {}
Example:
hd {1,2} {1,2}
i.e.
hd {1,2} = 1 hd {1,2} = 2
NB The overloading of hd for sets was added after the RSL book
and method book were written.
Chris George, UNU-IIST

Set Value Expressions


Enumerated: {expr1 ,...,exprn }
{1,2}
{1,2,1}
Ranged: {integer-expr1 .. integer-expr2 }
{3 .. 7} = {3,4,5,6,7}
{3 .. 3} = {3}
{3 .. 2} = {}
Comprehended: {expr1 | typing1 ,...,typingn logical-expr2 }
{2n | n : Nat n 3}

Chris George, UNU-IIST

scheme RESOURCE MANAGER =


class
type
Resource,
Pool = Resource-set
value

obtain : Pool Pool Resource


obtain(p) as (p1,r1) post r1 p p1 = p \ {r1}
pre p 6= {},

release : Resource Pool Pool


release(r,p) p {r}
pre r 6 p
end
Chris George, UNU-IIST

Lists
finite and infinite lists
list type expressions
list value expressions
list indexing
list operators
example of specification using lists

Chris George, UNU-IIST

10

Lists
A list is:
an ordered

collection
of
values

of same type

Examples:
h1,3,3,1,5i
htrue,false,truei

Chris George, UNU-IIST

11

List Type Expressions


type expr
hv1 , ..., vn i
where n 0, vi : type expr
type expr
hv1 , ..., vn i,
hv1 , ..., vn , ...i
where n 0, vi : type expr

Chris George, UNU-IIST

12

List Value Expressions


Enumerated: hexpr1 ,...,exprn i
h1,3,3,1,5i
htrue,false,truei
Ranged: hinteger-expr1 .. integer-expr2 i
h3 .. 7i = h3,4,5,6,7i
h3 .. 3i = h3i
h3 .. 2i = hi
Comprehended: hexpr1 | binding in list-expr2 logical-expr3 i
h2n | n in h0 .. 3ii
hn | n in h0 .. 100i is even(n)i

Chris George, UNU-IIST

13

List Indexing
Basic form:
list-expr(integer-expr1 )
Example:
h2,5,3i(2) = 5

Chris George, UNU-IIST

14

Associated Built-in Operators


b : T T T

hd : T T

tl : T T

he1 ,...,en i b hen+1 ,... i = he1 ,...,en ,en+1 ,... i


hd he1 ,e2 ,... i = e1
tl he1 ,e2 ,... i = he2 ,... i

len : T Nat

len he1 ,...,en i = n


len il chaos

elems : T T-infset

elems he1 ,e2 ,... i = {e1 ,e2 ,... }

inds : T Nat-infset

inds fl = {1 .. len fl}


inds il = {idx | idx : Nat idx 1}

Chris George, UNU-IIST

15

Overloading of and 6
: T T Bool

0 0

0 0 0 0 0 0

6 : T T Bool

0 0

0 0 0 0 0 0

d h a , b , c i = false
a 6 h a , b , c i = false

NB The overloading of and 6 for lists (and maps) was added after
the RSL book and method book were written.

Chris George, UNU-IIST

16

scheme QUEUE =
class
type
Element,
Queue = Element
value
empty : Queue = hi,
enq : Element Queue Queue
enq(e,q) q b hei,

deq : Queue Queue Element


deq(q) (tl q,hd q)
pre q 6= empty
end
Chris George, UNU-IIST

17

scheme SORTING = class


value
sort : Int Int
sort(l) as l1 post is permutation(l1,l) is sorted(l1)
is permutation : Int Int Bool,
is permutation(l1,l2) ( i : Int count(i, l1) = count(i, l2)),
count : Int Int Nat
count(i, l) card {idx | idx : Nat idx inds l l(idx) = i},
is sorted : Int Bool
is sorted(l)
( idx1,idx2 : Nat {idx1,idx2}inds l idx1<idx2 l(idx1)l(idx2))
end
Chris George, UNU-IIST

18

Case expressions
Lists are often analysed by case expressions, as in:
value
reverse : T T
reverse(l)
case l of
hi hi,
hhibt reverse(t)bhhi
end

Chris George, UNU-IIST

19

Maps
map type expressions
map value expressions
map application
map operators
example of specification using maps

Chris George, UNU-IIST

20

Maps
A map is:
an unordered

collection
of
pairs of values

Examples:
00

00

00

00

00

00

[ Klaus 7 7, John 7 2, Mary 7 7]


[1 7 2, 5 7 10]

Chris George, UNU-IIST

21

Maps may be:


infinite
partial
non-deterministic

Chris George, UNU-IIST

22

Map Type Expressions


type expr1
m type expr2
[v1 7 w1 , . . . , vn 7 wn ]
where n 0, vi : type expr1 , wi : type expr2
and vi = vj wi = wj
Finite and deterministic when applied to elements in the domain

type expr1
m type expr2
[v1 7 w1 , . . . , vn 7 wn ],
[v1 7 w1 , . . . , vn 7 wn , . . . ],
where n 0, vi : type expr1 , wi : type expr2
May be infinite and may be non-deterministic when applied to
elements in the domain
Chris George, UNU-IIST

23

NB The original RSL book only has


m , but with the meaning of
m .
Finite maps were introduced and the symbols changed in the method
book.

Chris George, UNU-IIST

24

Examples
Nat
m Bool
[]
[ 0 7 true ]
[ 0 7 true, 1 7 true ]

Nat
m Bool
[]
[ 0 7 true ]
[ 0 7 true, 1 7 true ]
[ 0 7 true, 0 7 false ]
[ 0 7 true, 0 7 false, 1 7 true ]
Chris George, UNU-IIST

25

Map Value Expressions


0

Enumerated: [expr1 7 expr1 ,. . . ,exprn 7 exprn ]


[ 3 7 true, 5 7 false ]
00
00
00
00
00
00
[ Klaus 7 7, John 7 2, Mary 7 7 ]
Comprehended: [expr1 7 expr2 | typing1 ,...,typingn logical-expr3 ]
[n 7 2n | n : Nat n 2] = [0 7 0, 1 7 2, 2 7 4]
[n 7 2n | n : Nat] = [0 7 0, 1 7 2, 2 7 4, . . . ]

Chris George, UNU-IIST

26

Map Application
Basic form:
map-expr(expr1 )
Examples:
00

00

00

00

00

00

00

00

[ Klaus 7 7, John 7 2, Mary 7 7]( John ) = 2


[3 7 true, 3 7 false](3) = true de false
Application is always to values in the domain; otherwise the result is
non-terminating (in fact swap, a kind of deadlock).

Chris George, UNU-IIST

27

Associated Built-in Operators

dom : (T1
m T2 ) T1 -infset

rng : (T1
m T2 ) T2 -infset

: (T1
m T2 ) (T1
m T2 )

(T1
m T2 )

: (T1
m T2 ) (T1
m T2 )

(T1
m T2 )
Chris George, UNU-IIST

dom [ 3 7 true, 5 7 false ] = {3, 5}


dom [ 3 7 true, 5 7 false, 5 7 true ] =
{3, 5}
rng [ 3 7 false, 5 7 false ] = {false}
rng [ 3 7 false, 5 7 false, 5 7 true ] =
{false, true}
[ 3 7 true, 5 7 false ] [ 5 7 true ] =
[ 3 7 true, 5 7 true ]
[ 3 7 true, 5 7 false ] [ 5 7 true ]
= [ 3 7 true, 5 7 false, 5 7 true ]
28

\ : (T1
m T2 ) T1 -infset

(T1
m T2 )

/ : (T1
m T2 ) T1 -infset

(T1
m T2 )

: (T2
m T3 ) (T1
m T2 )

(T1
m T3 )

: T1 (T1
m T2 ) Bool
Chris George, UNU-IIST

m\s=
[ d 7 m(d) | d : T1 d dom m d 6 s ]
[ 3 7 true, 5 7 false ] \ {5, 7} = [ 3 7 true ]
m/s=
[ d 7 m(d) | d : T1 d dom m d s ]
[ 3 7 true, 5 7 false ] / {5, 7} = [ 5 7 false ]
m1

m2 =
[ x 7 m1 (m2 (x)) | x : T1
x dom m2 m2 (x) dom m1 ]
[ 3 7 true, 5 7 false ]
00
00
00
00
[ Klaus 7 3, John 7 7 ]
00
00
= [ Klaus 7 true ]
3 [ 3 7 true ] = true
29

scheme DATABASE =
class
type
Database = Key
m Data,
Key, Data
value
empty : Database = [ ],
insert : Key Data Database Database
insert(k,d,db) db [ k 7 d ],
remove : Key Database Database
remove(k,db) db \ {k},

Chris George, UNU-IIST

30

defined : Key Database Bool


defined(k,db) k db,

lookup : Key Database Data


lookup(k,db) db(k)
pre defined(k,db)
end

Chris George, UNU-IIST

31

Das könnte Ihnen auch gefallen