Beruflich Dokumente
Kultur Dokumente
Chris George
United Nations University
International Institute for Software Technology
Macao SAR, China
Sets
finite and infinite sets
set type expressions
set operators
set value expressions
example of specification using sets
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 }
: T T-infset Bool
4 {1, 2, 3} = false
6 : T T-infset Bool
4 6 {1, 2, 3} = true
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
Lists
finite and infinite lists
list type expressions
list value expressions
list indexing
list operators
example of specification using lists
10
Lists
A list is:
an ordered
collection
of
values
of same type
Examples:
h1,3,3,1,5i
htrue,false,truei
11
12
13
List Indexing
Basic form:
list-expr(integer-expr1 )
Example:
h2,5,3i(2) = 5
14
hd : T T
tl : T T
len : T Nat
elems : T T-infset
inds : T Nat-infset
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.
16
scheme QUEUE =
class
type
Element,
Queue = Element
value
empty : Queue = hi,
enq : Element Queue Queue
enq(e,q) q b hei,
17
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
19
Maps
map type expressions
map value expressions
map application
map operators
example of specification using maps
20
Maps
A map is:
an unordered
collection
of
pairs of values
Examples:
00
00
00
00
00
00
21
22
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
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
26
Map Application
Basic form:
map-expr(expr1 )
Examples:
00
00
00
00
00
00
00
00
27
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
\ : (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},
30
31