Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Sets in Programming Languages

Sets can be specified and manipulated in logic programming languages like PROLOG. Membership in a set is encoded using the binary predicate mem(X,S), which specifies whether element X is a member of set S. The empty set is represented as [], and union and intersection of sets S1 and S2 are represented as constructions union(S1,S2) and inter(S1,S2). Membership in the union is defined by membership in either S1 or S2, while membership in the intersection requires membership in both S1 and S2. Properties like subset and equality relations can be defined in terms of membership.

Uploaded by

Vibhanshu Lodhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Sets in Programming Languages

Sets can be specified and manipulated in logic programming languages like PROLOG. Membership in a set is encoded using the binary predicate mem(X,S), which specifies whether element X is a member of set S. The empty set is represented as [], and union and intersection of sets S1 and S2 are represented as constructions union(S1,S2) and inter(S1,S2). Membership in the union is defined by membership in either S1 or S2, while membership in the intersection requires membership in both S1 and S2. Properties like subset and equality relations can be defined in terms of membership.

Uploaded by

Vibhanshu Lodhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

0.

Sets
Sets are a simple yet surprisingly important mathematical notion. In fact, there was
a time when it was believed that all of mathematics could be formalised using the
Theory of Sets (Set Theory). Even though that dream was never realised (we shall
see later why), sets have proven themselves extraordinarily useful in computer
science.

We shall not worry too much about defining sets here; let’s assume that you already
have a good idea about sets. Instead we shall confine ourselves to reminding
ourselves about certain basic concepts and properties about sets, and later how to
specify and then implement them in an algorithmic framework. In the following,
we do not assume a universe from which elements are drawn — those restrictions
will come later.

0.1 Set Membership and Equality


The primary notion that we shall assume is the idea of an element x being a member
of a set S, which we shall write as x ∈ S. If an element y is not a member of a set S,
we write y ∉ S. A commonly used notation for sets uses braces to delimit the set,
and commas to separate the members. For example, {0,1,2} is a set.

The first notion we encounter about sets is that two sets are equal if they have the
same members. More precisely:
Definition 0.1 (Set Equality) Two sets S1, S2 are equal, written S1 = S2, exactly
when x ∈ S1 if and only if x ∈ S2 for any element x.

From the mathematical definition of set equality, one can show the following.
Exercise 0.0 (Reflexivity of set equality). Show that S = S, for any set S.
Exercise 0.1 (Symmetry of set equality). Show that if S1 = S2 then S2 = S1 for any
sets S1, S2.
Proof. Suppose S1 = S2. So (by 0.1), x ∈ S1 if and only if x ∈ S2.
Therefore, x ∈ S2 if and only if x ∈ S1 . That is, S2 = S1. (By definition 0.1) □
Exercise 0.2 (Transitivity of set equality). Show that if S1 = S2 and S2 = S3, then
S1 = S3 for any sets S1, S2, S3.
[We will soon find that the properties of reflexivity, symmetry and transitivity apply in many
contexts; together these properties characterise a notion of equivalence. When an
equivalence is maintained within every construction, it is called a congruence.]
This notion of “extensionality” implies that members of a set are not listed multiple
times (there is a different notion, called a multiset, where the number of times an
element appears does indeed matter) and that the order in which the members are
listed is also not important (as is the case in a sequence or list).

0.2 The Empty Set


We shall assert that sets exist. If we were to ask some to prove the existence of sets,
they could (in a constructive sense) prove this by producing a set as a witness. What
is the simplest set to produce? The empty set! The empty set, written as {}, has no
elements as members. That is, x ∉ {} for any element x. Since the number of
elements in the empty set is zero, we will sometimes playfully also write 0 to denote
the empty set.

Page 1 of 11
0.3 Specifying sets and membership
Given the importance of sets in most mathematics, most programming languages
(oddly enough) do not directly support sets. Many languages support lists, where
multiple occurrences and the order of elements does matter.

Let us start by specifying sets (and programming) in a declarative language,


PROLOG. This language is based on first-order logic. We will learn more about
PROLOG later. The focus in this section will be on specifying membership
properties of sets, and constructions over sets. You can get started on using
PROLOG by visiting the URL http://tau-prolog.org in a browser.

We will represent sets using PROLOG lists, with the empty list, written [],
representing the empty set. (Note that this will only allow us to represent finite
sets.) The variable X denotes any element (note the capital/upper case letter; a
word beginning with a capital letter in PROLOG indicates it is a logical variable).

Note however that we are only specifying sets in terms of their abstract properties
related to membership, not implementing them, as we would in a Data Structures
course.

The binary predicate mem(X,S)encodes x ∈ S.


/* membership of X in S */

/* no element is a member of the empty set */


mem(X,[]) :- fail.

/* X is a member of a set containing X, of course! */


mem(X,[X|_]) :- !.
/* if X is not the first chosen element of S,
then it may be a later element */
mem(X,[_|R]) :- mem(X,R).

Checking if any element (denoted by the logical variable X) is a member of the


empty set results in failure (which in PROLOG represents the boolean notion of
“false”). And if X appears in the list encoding the set S, at the first position, then (of
course, denoted by !) x ∈ S; otherwise X may appear later in the list, which can be
checked recursively by mem(X,R), where R denotes the tail of the list representing
set S. The symbol :- should be read as “if”, and the last line of code above can be
read as “(if X isn’t the first element of the list representing set S, then) x ∈ S if X
appears in the tail (represented by variable R) of the list.”

0.4 Subsets
The next important notion is that of subsets.
Definition 0.2 (Subset). A set S1 is a subset of another set S2, written S1 ⊆ S2, if
for any element x, whenever x ∈ S1 then x ∈ S2.
[Note that we will be liberal in considering a set to be a subset of itself. When we need to be
pedantic, and want to exclude this case, we will use the terminology “proper subset”]
Using just this specification of the notion of subset in terms of membership, one can
show the following:
Page 2 of 11
Exercise 0.3 (Reflexivity of subset). Show that S ⊆ S, for any set S.
Exercise 0.4 (Transitivity of subset). Show that if S1 ⊆ S2 and S2 ⊆ S3, then
S1 ⊆ S3 for any sets S1, S2, S3.
Exercise 0.5 (Antisymmetry of subset). Show that if S1 ⊆ S2 and S2 ⊆ S1, then
S1 = S2 for any sets S1, S2.
Proof: Let S1 ⊆ S2. Therefore if x ∈ S1 then x ∈ S2.
Now if also S2 ⊆ S1, then if x ∈ S2 then x ∈ S1. Thus x ∈ S1 if and only if x ∈ S2.
So S1 = S2.
[We will soon find that the properties of reflexivity and transitivity apply in many contexts;
together these properties characterise a notion of pre-ordering or a quasi-ordering. When
antisymmetry also applies, the notion is called a partial ordering. When a partial ordering is
preserved under every construction, it is called a pre-congruence.]

Definition 0.3 (Least and greatest elements in a partially ordered set). Let (S, ⊑ )
be any set with a partially ordering. An element x ∈ S is called a least element of S
if for any element y ∈ S, x ⊑ y. Dually, an element z ∈ S is called a greatest
element of S if for any element y ∈ S, y ⊑ z.
[Note that in a partially ordered set, least and greatest elements are unique (due to
antisymmetry). In a quasi-ordered set, however, we may have multiple minimal or maximal
elements.]

A specification of the subset relationship S1 ⊆ S2 is easy to program recursively,


based the predicate mem(X,R).
/* subset */
subset([],L) :- !.
subset([X|R],L) :- mem(X,L), subset(R,L).
The empty set 0 is (of course!) a subset of any set S2 (represented here by list L).
The set S1 with (first) element X is a subset of set S2 (represented by L) if X is a
member of set S2 and if the rest of set S1 (represented by list R) is a subset of set S2.
(The conjunction and is represented by a comma between mem(X,L) and
the recursive call subset(R,L).

/* equal sets */
eqset(L1, L2) :- subset(L1,L2), subset(L2,L1).
Antisymmetry allows us to define a set equality predicate using subset(L1,L2).

0.5 Union and Intersection of sets


Definition 0.4 (Union) The union of two sets S1, S2, written S1 ∪ S2, is defined as
the least set S (under the subset ordering) such that x ∈ S if (x ∈ S1 or x ∈ S2 (or
both)). In “set builder notation”, S1 ∪ S2 = {x | x ∈ S1 ∨ x ∈ S2}.
/* Union */
mem(X,union(S1,S2)) :- mem(X,S1).
mem(X,union(S1,S2)) :- mem(X,S2).
Union can be represented as a construction, denoted union(S1,S2), and can be
defined using the disjunction of two clauses, the first checking if x ∈ S1 and the
second checking if x ∈ S2.

[ Note that the construction union(S1,S2) is just that — a “construction” that is


as yet uninterpreted, and used only to say that we can perform such an operation on
sets. So if you try, as an enterprising student in this class did to check if
Page 3 of 11
eqset(union([1,2],[3]), [1,2,3]), you will be disappointed to find that
the answer is not what mathematics will have you expect. That is because
the only thing we have specified is membership of the union construction on two
sets. This construction does not simplify automatically to the set theoretic
(mathematical) union of the two sets. In other words, union(_,_) is “only
syntax”! We will wait for an implementation of union of sets (represented as lists)
later. Hopefully, the implementation will satisfy the membership properties that
the specification says it should]

The next interesting concept is set theory is that of intersection of two sets.
Definition 0.5 (Intersection) The intersection of two sets S1, S2, written S1 ∩ S2, is
defined as the greatest set S less than S1, S2 under the subset ordering such that
x ∈ S if (x ∈ S1 and x ∈ S2). In “set builder notation”,
S1 ∩ S2 = {x | x ∈ S1 ∧ x ∈ S2}.
/* Intersection */
mem(X,inter(S1,S2)) :- mem(X,S1),mem(X,S2).
We can represent intersection as a construction, denoted inter(S1,S2), where
there is a single clause that takes a conjunction of the membership checks.

From the mathematical specifications of empty set, union and intersection, prove
the following (from first principles).

Exercise 0.6 (Subset of union): Show that S1 ⊆ S1 ∪ S2 and S2 ⊆ S1 ∪ S2.


Exercise 0.7 (Intersection-subset): Show that S1 ∩ S2 ⊆ S1 and S1 ∩ S2 ⊆ S2.

It is only slightly harder to show the following.


Exercise 0.8 (Subset-union): Show that S1 ⊆ S2 if and only if S1 ∪ S2 = S2.
Exercise 0.9 (Subset-intersection): Show that S1 ⊆ S2 if and only if S1 ∩ S2 = S1.

Exercise 0.10 (Commutativity): Show that S1 ∪ S2 = S2 ∪ S1.


Exercise 0.11 (Associativity): Show that (S1 ∪ S2) ∪ S3 = S1 ∪ (S2 ∪ S3).
Exercise 0.12 (Idempotence): S ∪ S = S.
Exercise 0.13 (Identity): Show that S ∪ 0 = S = 0 ∪ S.

Union and Intersection exhibit a nice duality — see the relationship between the
statements in Exercises 0.10-1.12 with those in 0.14-0.16; 0.18-0.19 with 0.20-0.21;
and 0.22 with 0.23.

Exercise 0.14 (Commutativity): Show that S1 ∩ S2 = S2 ∩ S1.


Exercise 0.15 (Associativity): Show that (S1 ∩ S2) ∩ S3 = S1 ∩ (S2 ∩ S3).
Exercise 0.16 (Idempotence): S ∩ S = S.
Exercise 0.17 (Annihilation): Show that S ∩ 0 = 0 = 0 ∩ S.

In fact, union and intersection exhibit (nice) dual distributive properties as well:

Exercise 0.18 (Left Distributivity of union over intersection): Show that


S1 ∪ (S2 ∩ S3) = (S1 ∪ S2) ∩ (S1 ∪ S3).
Exercise 0.19 (Right Distributivity of union over intersection): Show that
(S1 ∩ S2) ∪ S3 = (S1 ∪ S3) ∩ (S2 ∪ S3).
Page 4 of 11
Exercise 0.20 (Left Distributivity of intersection over union): Show that
S1 ∩ (S2 ∪ S3) = (S1 ∩ S2) ∪ (S1 ∩ S3).
Exercise 0.21 (Right Distributivity of intersection over union): Show that
(S1 ∪ S2) ∩ S3 = (S1 ∩ S3) ∪ (S2 ∩ S3).

Exercise 0.22 (Absorption intersection-union): Show that S1 ∩ (S1 ∪ S2) = S1.


Exercise 0.23 (Absorption union-intersection): Show that S1 ∪ (S1 ∩ S2) = S1.

0.6 Programs as objects about which we can reason.


Programming is not coding. Programming is the representation of abstractions,
usually mathematical abstractions, in terms of the constructs of a programming
language, such that we can reason about the correctness of the representation.

Programming Activity 0.0: Design tests for checking that the programs written
above indeed behave the way they are expect to. You may wish to check that
membership behaves the way it should on empty and non-empty sets, where a
particular element is and is not a member. You should check that membership
works irrespective of order of elements in a list representation of a set, and
irrespective of whether duplicates are present in the list representation or not.

For each of the exercises, you should design concrete tests and check that
membership (and non-membership) behave as indicated by the property.

0.7 Set Difference and Complement


Definition 0.6 (Set Difference) The difference of two sets S1, S2, written S1 − S2, is
defined as the greatest set S such that x ∈ S if (x ∈ S1 and x ∉ S2). In “set builder
notation”, S1 − S2 = {x | x ∈ S1 ∧ x ∉ S2}.
/* Set Difference */
mem(X, diff(S1, S2)) :- mem(X, S1),
\+ mem(X,S2).
We can represent set difference as a construction, denoted diff(S1,S2), where
there is a single clause that checks the membership in S1 and non-membership in
S2. The PROLOG operator \+ denotes negation of the predicate that follows.

Definition 0.7 (Complement) The complement of a set S, written S, is defined as


the set S comprising all elements not in S, that is x ∈ S if x ∉ S. In “set builder
notation”, S = {x | x ∉ S}.
/* Complement */
mem(X, compl(S)) :- \+ mem(X, S).
Note that complementation of a set is a special case of set difference. Assume that
we have a universal set . Then S = − S. Alternatively, one can think of set
difference as a “relative complement”.

The notion of a universal set allows us to state the identity for intersection, dual to
Exercise 0.13:
Exercise 0.24 (Identity of intersection): Show that S ∩ = S = ∩ S.
Page 5 of 11
𝒰
𝒰
𝒰
𝒰
as well as the annihilator for union, dual to Exercise 0.17
Exercise 0.25 (Annihilation for union): Show that S ∪ = = ∪ S.

Further, once we have a complementation operation, we can complete the story for
the boolean algebraic laws, by presenting the so-called De Morgan Laws:
Exercise 0.26 (Complement of union): Show that S1 ∪ S2 = S1 ∩ S2.
Exercise 0.27 (Complement of intersection): Show that S1 ∩ S2 = S1 ∪ S2.

Exercise 0.28 (Complements: empty/universal set): Show that 0 = and 0 = .


Exercise 0.29 (Involution of complement): Show that S = S.
Exercise 0.30 (Union with complement): Show that S ∪ S = .
Exercise 0.30 (Intersection with complement): Show that S ∩ S = 0.

Exercise 0.31 (Difference-Complement-Union): Show that S1 − S2 = S1 ∩ S2.

Definition 0.8 (Symmetric Difference) The symmetric difference of two sets S1, S2,
written S1 ⊖ S2, is defined as the set S such that x ∈ S if ((x ∈ S1 and x ∉ S2) or
(x ∈ S2 and x ∉ S1)).
Exercise 0.32: Show that S1 ⊖ S2 = (S1 − S2) ∪ (S2 − S1).

0.8 Powerset.
Definition 0.9 (Powerset of S) The powerset of a given set S, written (S ) or
more punnily as 2S , is defined as the set of all subsets of S. In “set builder notation”,
(S ) = {S′| S′ ⊆ S}.
Proposition 0.0: The powerset (S ) of a given set S forms a partially ordered set
under the subset ordering. We will write this as ( (S ), ⊆ ).
Exercise 0.33: Prove Proposition 0.0.
Notation: The powerset of the empty set 0 is a singleton set with a single element,
namely the empty set. That is 20 = {0}. So it makes sense to call this set 1,
and we can punnily write the equation 20 = 1.

Since the empty set is a subset of any set S, it can be considered the least subset of
any given set S, and hence the least element in ( (S ), ⊆ ) Likewise, since all
subsets of set S are contained in it, S is the greatest element in ( (S ), ⊆ ).
/* Powerset */
mem(X, power(S)) :- subset(X,S).
Every subset of S is a member of the powerset (S ).

0.9 Cartesian Product


Definition 0.10 (Cartesian Product) The (Cartesian) product of two sets S1, S2,
written S1 × S2, is defined as the set S such that the ordered pair (x, y) ∈ S if
(x ∈ S1 and y ∈ S2). In “set builder notation”, S1 × S2 = {(x, y) | x ∈ S1 ∧ y ∈ S2}.
/* Cartesian product */
mem((X,Y), cartesian(S1,S2)) :- mem(X,S1),
mem(Y,S2).
Cartesian product is a canonical construction on sets. Here we specify it using the
constructor cartesian(S1,S2)where membership is defined in terms of
Page 6 of 11
𝒫
𝒫
𝒫
𝒫
𝒫
𝒫
𝒫
𝒰
𝒰
𝒰
𝒰
𝒰

𝒰

membership of the respective elements of the ordered pair in the corresponding
sets.

What is the Cartesian product of a set S with the empty set 0? You should be able to
see that since (for any y) y ∉ 0, S × 0 = 0, and since (for any x) x ∉ 0, 0 × S = 0.
Which is a nice pun!

And what is the Cartesian product of a set S with the singleton set 1? You should be
able to see that S × 1 = {(x, 0) | x ∈ S}, and 1 × S = {(0, y) | y ∈ S}. While these
sets are not technically equal to the set S, we shall see (after we introduce the
concept of isomorphism ≅ between sets) that S × 1 ≅ S ≅ 1 × S. Another nice pun!

0.10 Binary Relations


Definition 0.11 (Binary Relation) A binary relation R between two sets S1, S2 is any
subset of S1 × S2. That is, R ⊆ S1 × S2.
Note that the empty set 0 and S1 × S2 are both binary relations. Since the Cartesian
product of sets is itself a set, binary relations can be partially ordered using the
usual notion of subset — in fact, the collection of binary relations over sets S1, S2
form a partially ordered set under ⊆, with 0 and S1 × S2 being the least and greatest
elements respectively.

Definition 0.12 (Total Relation) A relation R ⊆ S1 × S2 is called total if for each


x ∈ S1 there exists a y ∈ S2 such that (x, y) ∈ R. [Otherwise a relation is called
partial.]

Definition 0.13 (Onto Relation) A relation R ⊆ S1 × S2 is called onto (surjective,


epi-) if for each y ∈ S2 there exists a x ∈ S1 such that (x, y) ∈ R.

Definition 0.14 (1-1 Relation) A relation R ⊆ S1 × S2 is called 1-1 (injective,


mono-) if whenever (x1, y) ∈ R and (x2, y) ∈ R then x1 = x2. That is, any y ∈ S2 is
the “target” under R ⊆ S1 × S2 of at most one x ∈ S1

Definition 0.15 (Functional Relation) A relation R ⊆ S1 × S2 is called functional (a


function) if whenever (x, y1) ∈ R and (x, y2) ∈ R then y1 = y2. That is, any x ∈ S1 is
related under R ⊆ S1 × S2 to at most one y ∈ S2

When S1, S2 are the same, i.e., R ⊆ S × S, then we say “the relation R is over set S.”

Definition 0.16 (Identity Relation) Given any set S, the identity relation on S,
written id(S ) , is the binary relation comprising all pairs (x, x) for x ∈ S. In “set
builder notation”, id(S ) = {(x, x) | x ∈ S}.
/* Identity Relation on a set S */
mem((X,X), id(S)) :- mem(X,S), !.
The constructor id(S) is used to denote the identity relation over set S.

Definition 0.17 (Relational Composition) Given any two binary relations


R1 ⊆ S1 × S2, and R2 ⊆ S2 × S3 , their relational composition, written

Page 7 of 11
R1 ∘ R2 ⊆ S1 × S3, is the binary relation comprising all pairs (x, z) ∈ S1 × S3 such
that for some y ∈ S2 : (x, y) ∈ R1 and (y, z) ∈ R2. In “set builder notation”,
R1 ∘ R2 = {(x, z) ∈ S1 × S3 | ∃y ∈ S2 : (x, y) ∈ R1 ∧ (y, z) ∈ R2}.
/* Relational Composition */
mem((X,Z), comp(R1, R2)) :- mem((X,Y),R1),
mem((Y,Z),R2).
The constructor comp(R1, R2) is used to specify relational composition.

Proposition 0.1: For any binary relation R ⊆ S1 × S2, the relations id(S1) and
id(S2) are the left- and right- identities of relational composition. That is,
id(S1) ∘ R = R = R ∘ id(S2).
Exercise 0.34: Prove Proposition 0.1.

Proposition 0.2: Relational composition is associative. That is, for any binary
relations R1 ⊆ S1 × S2, R2 ⊆ S2 × S3 and R3 ⊆ S3 × S4: (R1 ∘ R2) ∘ R3 = R1 ∘ (R2 ∘ R3)
Exercise 0.35: Prove Proposition 0.2.

Proposition 0.3: Relational composition is monotone. That is, for any binary
relations R1, R′1 ⊆ S1 × S2, and R2, R′2 ⊆ S2 × S3 such that R1 ⊆ R′1 and R2 ⊆ R′2 .
Then R1 ∘ R2 ⊆ R′1 ∘ R′2.
Proof. Let (x, y) ∈ R1 and (y, z) ∈ R2. Then (x, z) ∈ R1 ∘ R2. But from the
definition of subset, (x, y) ∈ R′1 and (y, z) ∈ R′2. Therefore (x, z) ∈ R′1 ∘ R′2. □

Definition 0.18 (Relational Inverse) For any binary relation R ⊆ S1 × S2, its
inverse, written R − ⊆ S2 × S1, is the binary relation comprising all pairs (y, x) for
(x, y) ∈ R. In “set builder notation”, R − = {(y, x) ∈ S2 × S1 | (x, y) ∈ R}.
/* Inverse Relation */
mem( (Y,X), inv(R)) :- mem( (X,Y), R).
The constructor inv(R) is used to denote the inverse of the relation R.

Proposition 0.4: Relational inverse leaves the identity relations unchanged.


For all sets S: (id(S ))− = id(S )
Exercise 0.36: Prove Proposition 0.4.

Proposition 0.5: Relational inverse is involutive. For all relations R ⊆ S1 × S2:


(R −)− = R.
Exercise 0.37: Prove Proposition 0.5.

Proposition 0.6: . For all relations R1 ⊆ S1 × S2 and R2 ⊆ S2 × S3:


(R1 ∘ R2)− = R2− ∘ R1−
Exercise 0.38: Prove Proposition 0.6.

0.11 Properties of Binary Relations over a set

Definition 0.19 (Reflexivity) A binary relation R ⊆ S × S, is reflexive if for all


x ∈ S, (x, x) ∈ R.
Proposition 0.7: (i) The identity relation id(S ) is reflexive.
(ii) The relation S × S is reflexive.
Exercise 0.39: Prove Proposition 0.7.
Page 8 of 11










Definition 0.20 (Symmetry) A binary relation R ⊆ S × S, is symmetric if
whenever (x, y) ∈ R , then (y, x) ∈ R.
Proposition 0.8: (i) The identity relation id(S ) is symmetric.
(ii) The relation S × S is symmetric.
Exercise 0.40: Prove Proposition 0.8.

Definition 0.21 (Transitivity) A binary relation R ⊆ S × S is transitive if whenever


(x, y) ∈ R and (y, z) ∈ R, then (x, z) ∈ R.
Proposition 0.9: (i) The identity relation id(S ) is transitive.
(ii) The relation S × S is transitive.
Exercise 0.41: Prove Proposition 0.9.

Proposition 0.10: A relation R ⊆ S × S is transitive if and only if R ∘ R ⊆ R


Exercise 0.41: Prove Proposition 0.10.

0.12 Closure Operations on Binary Relations over a set.


Given a relation R ⊆ S × S, a closure is a “constructed” relation R′ that contains R ,
i,e., R ⊆ R′, such R′ also has certain properties.

Definition 0.22 (Reflexive Closure) For any binary relation R ⊆ S × S, its reflexive
closure, written R = ⊆ S × S, is the least reflexive relation containing R. That is, (i)
R ⊆ R = , (ii) R = is reflexive, and (iii) for any other R′ ⊆ S × S such that R ⊆ R′
and R′ is reflexive, R = ⊆ R′.

Proposition 0.11: R = = R ∪ id(S ).


/* Reflexive Closure */
mem( (X,X), refclos(R,S)) :- mem(X,S), !.
mem( (X,Y), refclos(R,S)) :- mem((X,Y), R), !.
We use Proposition 0.11 to specify membership of the reflexive closure of R, denoted
using the construction refclos(R,S).

Exercise 0.42: Prove Proposition 0.11.

Definition 0.23 (Symmetric Closure) For any binary relation R ⊆ S × S, its


symmetric closure, written R ↔ ⊆ S × S, is the least symmetric relation containing
R. That is, (i) R ⊆ R ↔ , (ii) R ↔ is symmetric, and (iii) for any other R′ ⊆ S × S
such that R ⊆ R′ and R′ is symmetric, R ↔ ⊆ R′.

Proposition 0.12: R ↔ = R ∪ R −
/* Symmetric Closure */
mem((X,Y), symclos(R)) :- mem((X,Y), R), !.
mem((X,Y), symclos(R)) :- mem((X,Y), inv(R)).
We use Proposition 0.12 to specify membership of the symmetric closure of R,
denoted using the construction symclos(R).

Exercise 0.43: Prove Proposition 0.12.

Page 9 of 11











Definition 0.24 (Transitive Closure) For any binary relation R ⊆ S × S, its
transitive closure, written R + ⊆ S × S, is the least transitive relation containing R.
That is, (i) R ⊆ R + , (ii) R + is transitive, and (iii) for any other R′ ⊆ S × S such
that R ⊆ R′ and R′ is transitive, R + ⊆ R′.

Definition 0.25 (i-fold composition) The i-fold composition of a binary relation is


defined (inductively) as follows:
R1 = R
R 1+i = R ∘ R i for i ≥ 1.
[Note that we could have alternatively have defined R i+1 = R i ∘ R for i ≥ 1.]

Proposition 0.13 R j+k = R j ∘ R k


Proof: By induction on j ≥ 1.
Base Case (j = 1). R 1+k = R ∘ R k = R 1 ∘ R k
Induction Hypothesis (IH): Suppose we have shown for j = n that R n+k = R n ∘ R k
Induction Step: R (1+n)+k = R 1+(n+k) = R ∘ R n+k (Assoc of + & Definition 0.25)
= R ∘ (R n ∘ R k ) (by IH)
= (R ∘ R n) ∘ R k (by associativity of composition)
= R 1+n ∘ R k = R j ∘ R k

Proposition 0.14 R + = Ri

i≥1
R i is a transitive relation containing R

Proof. We first show that
i≥1
(i) Since R 1 = R, from the definition of (generalised) union, R ⊆ Ri

i≥1
i i
⋃ ⋃
(ii) Let (x, y) ∈ R and (y, z) ∈ R . So, from the definition of union,
i≥1 i≥1
(x, y) ∈ R j and (y, z) ∈ R k for some j, k ≥ 1. Therefore (x, z) ∈ R j+k
(Proposition 0.13). But since R j+k ⊆ R i by the definition of generalised

i≥1
R i. So R i is a transitive relation.
⋃ ⋃
union (for j + k = i) , (x, z) ∈
i≥1 i≥1
(iii) Let R′ ⊆ S × S be such that R ⊆ R′ and R′ is transitive. (So by Proposition
0.10, R′ ∘ R′ ⊆ R′)
We show by induction that for each i ≥ 1, R i ⊆ R′.
Base case (i = 1). By assumption: R 1 = R ⊆ R′.
Induction hypothesis: Suppose we have shown that for i = k that R i ⊆ R′.
Induction step: Consider R 1+k = R ∘ R k.
But R ⊆ R′ (by assumption) and R k ⊆ R′ (by IH).
So by Proposition 0.3 and 0.10, R 1+k = R ∘ R k ⊆ R′ ∘ R′ ⊆ R′.
Hence by induction, for each i ≥ 1, R i ⊆ R′.

Now by the definition of generalised union, since for each i ≥ 1: R i ⊆ R′,


R i ⊆ R′. □

i≥1
/* Transitive Closure */
mem((X,Y), transclos(R)) :- mem((X,Y), R), !.
Page 10 of 11





















mem((X,Z), transclos(R)) :- mem((X,Y), R),
mem((Y,Z), transclos(R)), !.

Proposition 0.14 allows us to provide an inductive specification for the transitive


closure of a relation, denoted by construction transclos(R).

The closure operations can be combined.

Definition 0.26 (Reflexive Symmetric Closure) For any binary relation R ⊆ S × S,


its reflexive-symmetric closure, written R ⇔ ⊆ S × S, is the least reflexive and
symmetric relation containing R. That is, (i) R ⊆ R ⇔ , (ii) R ⇔ is reflexive, (iii)
R ⇔ is symmetric, and (iv) for any other R′ ⊆ S × S such that R ⊆ R′ and R′ is
reflexive and symmetric, R ⇔ ⊆ R′.
Proposition 0.15: R ⇔ = id(S ) ∪ R ∪ R −
Exercise 0.44: Prove Proposition 0.15.

Definition 0.27 (Reflexive Transitive Closure) For any binary relation R ⊆ S × S,


its reflexive-transitive closure, written R* ⊆ S × S, is the least reflexive and
transitive relation containing R. That is, (i) R ⊆ R* , (ii) R* is reflexive, (iii) R* is
transitive, and (iv) for any other R′ ⊆ S × S such that R ⊆ R′ and R′ is reflexive
and transitive, R* ⊆ R′.

Let us posit R 0 = id(S )


Ri

Proposition 0.16: R* =
i≥0
Exercise 0.44: Prove Proposition 0.16.

Definition 0.28 (Reflexive Symmetric Transitive Closure) For any binary relation
R ⊆ S × S, its reflexive-symmetric-transitive closure, written R ≡ ⊆ S × S, is the
least reflexive, symmetric and transitive relation containing R. That is,
(i) R ⊆ R ≡, (ii) R ≡ is reflexive, (ii) R ≡ is symmetric, (iv) R ≡ is transitive, and (v)
for any other R′ ⊆ S × S such that R ⊆ R′ and R′ is reflexive, symmetric and
transitive, R ≡ ⊆ R′.

Proposition 0.17: R ≡ = ( R i )↔, which is the least equivalence relation



i≥0
containing R .
Exercise 0.45: Prove Proposition 0.17.

Page 11 of 11











You might also like