02 Sets and Relations
02 Sets and Relations
Copyright 2001-22, Matt Dwyer, John Hatcliff, Rod Howell, Laurence Pilard, and Cesare Tinelli.
Created by Cesare Tinelli and Laurence Pilard at the University of Iowa from notes originally developed by Matt Dwyer, John Hatcliff, Rod Howell at Kansas
State University. These notes are copyrighted materials and may not be used in other course settings outside of the University of Iowa in their current form
or modified form without the express written permission of one of the copyright holders. During this course, students are prohibited from selling notes to or
being paid for taking notes by any person or commercial firm without the express written permission of one of the copyright holders.
These Notes
• review the concepts of sets and relations required to work with
the Alloy language
A set’s objects are drawn from a larger domain of objects all of which have
the same type --- sets are homogeneous
Examples: domain
{ 2, 4, 5, 6, … } set of integers
{ red, yellow, blue } set of colors
{ true, false } set of boolean values
{ red, true, 2 } for us, not a set!
Form a new set of elements drawn from domain D by including exactly the
elements that satisfy predicate (i.e., Boolean function) P
Examples:
{ x : N | x < 10} Naturals less than 10
{ x : Z | (! y : Z | x = 2y) } Even integers
{x:N|x>x} Empty set of natural numbers
Examples:
– # {red, yellow, blue} = 3
– # {1, 23} = 2
–#Z=?
Cardinalities are defined for infinite sets too but we’ll be mostly concerned
with the cardinality of finite sets
Pow (S) ! { e | e - S }
Example:
Pow ({a,b,c}) = { ., {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c} }
Partitions:
– Partition Person into Child, Adult
– Partition Residence into Home, DormRoom, Apartment
Example:
{ }
(red, true), (red, false),
PrimaryColor x Boolean = (blue, true), (blue, false),
(yellow, true), (yellow, false)
domain co-domain
Examples:
Parent : Person x Person = { (John, June), (John, Sam) }
Square : Z x N = { (1, 1), (-1, 1), (-2, 4) }
ClassGrades : Person x { A, B, C, D, F } = { (Kim, A), (Alex, B) }
Example:
FavoriteBeer : Person x Beer x Price
= { (John, Miller, $2), (Ted, Heineken, $4), (Steve, Miller, $2) }
One
Many
One
One-to-One Many-to-Many
Many
Many
One
One
Examples:
– { (2, red), (3, blue), (5, red) } ✓
– { (4, 2), (6, 3), (8, 4) } ✓
– { (2, red), (3, blue), (2, blue) } ✘
1 Square 4
-2 Many-to-one
-1 1
A function is an
X-to-one relation
ClassGrades
Todd A
One-to-one
Vic B
Examples:
– Squares : Z -> N = {…, (-1,1), (0,0), (1, 1), (2,4), …} total
– SquareRoot : N -> N = { (x, y) : N x N | y2 = x) } partial
Surjective Function
Relations
Partial Functions
Surjective
Total
Bijective
Injective
– #(ClassGrades $ { (Matt, C) }) = 3
Example:
Not limited to
– s = { (red,1), (blue,2) } binary relations
– r = { (1,2), (2,4), (3,6) }
– s ; r = { (red,2), (blue,4) }
CS:5810 -- Formal Methods in Software Engineering Fall 2022 32
Relation Transitive Closure
Intuitively, the transitive closure r+ of a binary relation r: S x S is the
result of adding a direct link (a,b) to r for every a and b where b is
reachable from a along r :
r+ ≡ r ! (r ; r) ! (r ; r ; r) ! …
Formally, r+ ≡ smallest transitive relation containing r
Example:
– GrandParent = Parent ; Parent
– GrandGrandParent = Parent ; GrandParent
– Ancestor = Parent !"GrandParent !"GrandGrandParent !"#"$"Parent+
CS:5810 -- Formal Methods in Software Engineering Fall 2022 33
Relation Transpose
Intuitively, the transpose ~r of a relation r: S x T is the relation
obtained by reversing all the pairs in r
~r ≡ { (b,a) | (a,b) ∈ r }
Example:
– ChildOf = ~Parent
– DescendantOf = (~Parent)+
David Garlan’s slides from Lecture 3 of his course of Software Models entitled
“Sets, Relations, and Functions”
(http://www.cs.cmu.edu/afs/cs/academic/class/15671-f97/www/ )