Algebraic
Foundations of
Computer Science
(AFCS)
Prof.Dr. F.L.
Tiplea
Closures Algebraic Foundations of Computer Science.
Structural
induction Closures.
Definitions by
induction
Definitions by
recursion Ferucio Laurentiu Tiplea
Course readings
Department of Computer Science
AL.I.Cuza University of Iasi
Iasi, Romania
E-mail:
[email protected] Spring 2013
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 1 / 25
Outline
Algebraic
Foundations of
Computer Science
(AFCS)
Prof.Dr. F.L. 1 Closures
Tiplea
Closures
Structural
induction
2 Structural induction
Definitions by
induction
Definitions by
recursion 3 Definitions by induction
Course readings
4 Definitions by recursion
5 Course readings
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 2 / 25
Closures example
Algebraic
Foundations of
Computer Science
(AFCS)
Example 1
Prof.Dr. F.L.
Tiplea Let A be a set of atomic propositions. The set PF (A) of
Closures propositional formulas over A is the least set which fulfills the
Structural following properties:
induction
Definitions by
a PF (A), for any a A (that is, A PF (A));
induction
if and are propositional formulas over A, then
Definitions by
recursion
Course readings , ( ), ( ), ( ), and ( )
are propositional formulas over A.
The three key features of PF (A):
1 includes A
2 closed under , , , ,
3 least set with the above properties
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 3 / 25
Constructors and closures
Algebraic
Foundations of
Computer Science
An n-ary constructor over a set V is a relation r from V n to V .
(AFCS)
That is, the elements of r are of the form ((a1 , . . . , an ), a).
Prof.Dr. F.L.
Tiplea
Closures
Given an n-ary constructor r and a set A, denote by r (A) the set:
Structural
induction r (A) = {a|(a1 , . . . , an A)(((a1 , . . . , an ), a) r )}
Definitions by
induction
Definitions by
recursion
Course readings Definition 2
Let A be a set and R be a set of constructors. The closure of A
under R is the least set B V with the properties:
A B;
B is closed under R, i.e., r (B) B, for any r R.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 4 / 25
Existence of Closures
Algebraic
Foundations of
Computer Science
(AFCS)
Theorem 3 (Existence of closures)
Prof.Dr. F.L.
Tiplea
Given a set A and a set R of constructors, the closure of A under
R exists and it is unique. Moreover, if R[A] denotes the closure of
Closures
A under R, then [
Structural
induction R[A] = Bm ,
Definitions by m0
induction
Definitions by where
recursion
Course readings B0 = A;
S
Bm+1 = Bm r R r (Bm ), for any m 0;
The closure of A under R is the union of a chain of sets:
[
B0 = A, B1 = B0 R(B0 ), B2 = B1 R(B1 ), . . . , Bm = R[A],
m0
S
where R(Bi ) = r R r (Bi ).
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 5 / 25
The set of natural numbers as a closure
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 4
Prof.Dr. F.L.
Tiplea
The successor of a set x, denoted S(x), is the set S(x) = x {x}.
Closures
Structural
induction Recall that natural numbers are defined as follows:
Definitions by
induction
0 = ;
Definitions by 1 = S(0) = {0} = {};
recursion
Course readings 2 = S(1) = {0, 1} = {, {}} etc.
Therefore, N is the closure of {0} under R = {S}.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 6 / 25
Closures of a binary relation
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 5
Prof.Dr. F.L.
Tiplea
The reflexive closure of a binary relation A A is the least
reflexive binary relation r () which includes .
Closures
Structural
induction
Definitions by
Fact: r () can be computed as follows:
induction
Definitions by r () = A
recursion
Course readings
Definition 6
The symmetric closure of a binary relation A A is the least
symmetric binary relation s() which includes .
Fact: s() can be computed as follows:
s() = 1
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 7 / 25
Closures of a binary relation
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 7
Prof.Dr. F.L.
Tiplea
The transitive closure of a binary relation A A is the least
transitive binary relation t() which includes .
Closures
Structural
induction
Definitions by Fact: t(), also denoted by + , can be computed as follows:
induction
Definitions by
[
recursion t() = + = m ,
Course readings m1
where
1 = and
m+1 = m , for all m 1.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 8 / 25
Closures of a binary relation
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 8
Prof.Dr. F.L.
Tiplea
The reflexive and transitive closure of a binary relation A A
is the least reflexive and transitive binary relation which
Closures
includes .
Structural
induction
Definitions by
induction
Fact: can be computed as follows:
Definitions by
recursion [
Course readings = t(r ()) = r (t()) = m ,
m0
where
0 = A and
m+1 = m , for all m 0.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 9 / 25
Closures of a binary relation
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 9
Prof.Dr. F.L.
Tiplea
The closure under equivalence of a binary relation A A is
the least equivalence relation equiv () which includes .
Closures
Structural
induction
Definitions by Fact: equiv () can be computed as follows:
induction
Definitions by
recursion equiv () = t(s(r ())) = t(r (s())) = r (t(s())).
Course readings
Remark 1
In general, s(t()) 6= t(s()).
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 10 / 25
Structural induction
Algebraic
Foundations of
Computer Science
(AFCS)
Theorem 10 (Structural induction)
Prof.Dr. F.L.
Tiplea
Let B = R[A] be the closure of A under R and let P be a property
such that:
Closures
Structural
P(a), for any a A;
induction
Definitions by
(P(a1 ) P(an ) P(a)), for any r R and
induction a1 , . . . , an , a B with ((a1 , . . . , an ), a) r .
Definitions by
recursion Then, P is satisfied by any a B.
Course readings
Remark 2
1 Structural induction is equivalent to mathematical induction.
2 Structural induction is more appropriate for proving properties
of closures than mathematical induction.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 11 / 25
Structural induction example
Algebraic
Foundations of
Computer Science
(AFCS)
Example 11
Prof.Dr. F.L.
Tiplea
Let A be a set of atomic propositions. The set PF (A) of
propositional formulas as defined in Example 1 is the closure of A
Closures
under some set of constructors (prove it!).
Structural
induction
Definitions by
induction
Let P() be the following property:
Definitions by
recursion P() : has as many left brackets as right brackets.
Course readings
By structural induction we can easily prove that P is satisfied by all
propositional formulas over A (prove it!).
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 12 / 25
Definitions by induction
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 12
Prof.Dr. F.L.
Tiplea
A set B is inductively defined by A and R if B = R[A].
Closures
Structural
induction If B = R[A], then B is obtained as follows:
Definitions by
induction
B0 = A;
Definitions by Bm+1 = Bm R(Bm ), for all m 0;
recursion S
Course readings B = m0 Bm .
If the chain
B0 , B1 , B2 , . . . , Bm , Bm+1 = Bm , Bm+2 = Bm , . . .
stabilizes to some set Bm , then its union is Bm and, therefore,
B = Bm .
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 13 / 25
Definitions by induction
Algebraic
Foundations of
Computer Science
A definition by induction corresponds to the following while-loop
(AFCS)
(that might be non-terminating):
Prof.Dr. F.L.
Tiplea
Closures Algorithm 1: Computing closures
Structural
induction input : set A and set R of constructors;
Definitions by
induction output: B = R[A];
Definitions by
recursion begin
Course readings B := A;
while R(B) 6 B do
B := B R(B)
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 14 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
Assume that B is inductively defined by A and R. It would be a
(AFCS)
good idea to define functions f on B in a recursive way as follows:
Prof.Dr. F.L.
Tiplea
Closures
define f for any a A;
Structural
induction
if ((a1 , . . . , an ), a) r and the function has already been
Definitions by defined for a1 , . . . , an , then define the function for a as a
induction
combinations of the values f (a1 ), . . . , f (an ) in the form
Definitions by
recursion
Course readings h(r )(f (a1 ), . . . , f (an )),
where h associates a (partial) function h(r ) to r .
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 15 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
The definition above has a main drawback: it could not work for
(AFCS)
some sets B. Just think that the element a above might be defined
Prof.Dr. F.L.
Tiplea in at least two different ways,
Closures
((a1 , . . . , an ), a) r
Structural
induction
Definitions by and
((a1 , . . . , am
), a) r .
induction
Definitions by
recursion
Course readings
In such a case, you must be assured that
h(r )(f (a1 ), . . . , f (an )) = h(r )(f (a1 ), . . . , f (am
)).
The easiest way to have this property fulfilled is to ask for each
element a B to have exactly one inductive construction of it from
A and R. If B has this property then it is called a free inductively
defined set.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 16 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
However, for inductively defined sets we can prove the following
(AFCS)
result:
Prof.Dr. F.L.
Tiplea
Closures Lemma 13
Structural
induction
Let B = R[A], C a set, g : A C, and h a function which
Definitions by
induction associates a partial function h(r ) : C n C to each r R, where n
Definitions by is the arity of r . Then, there exists a unique relation f B C
recursion
Course readings
such that:
(1) (a, g(a)) f , for any a A;
(2) if (a1 , b1 ), . . . , (an , bn ) f , ((a1 , . . . , an ), a) r and
h(r )(b1 , . . . , bn ), then (a, h(r )(b1 , . . . , bn )) f ;
(3) f is the least relation from B to C which satisfies (1) and (2).
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 17 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
(AFCS)
Definition 14
Prof.Dr. F.L.
Tiplea
A set B is called free inductively defined by A and R if, for any
a B,
Closures
Structural
either a A,
induction
Definitions by
or there exists a unique r R and a unique n-tuple
induction (a1 , . . . , an ) such that ((a1 , . . . , an ), a) r , where n is the arity
Definitions by
recursion
of r (for n = 0 we understand that a r ).
Course readings
Now, we can obtain the following important result.
Theorem 15 (Recursion theorem)
Let B, C, g, and h as in Lemma 13. If B is free inductively defined
by A and R, then the binary relation f from Lemma 13 is a
function.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 18 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
A slight extension of the recursion theorem is the following:
(AFCS)
Prof.Dr. F.L.
Tiplea
Theorem 16
Closures
Structural
Let B = R[A], C a set, g : A C, and h a function which
induction associates a partial function h(r ) : B n C n C to each r R,
Definitions by
induction
where n is the arity of r . If B is free inductively defined by A and
Definitions by R, then there exists a unique function f : B C such that:
recursion
Course readings
(1) f (a) = g(a), for any a A;
(2) f (a) = h(r )(a1 , . . . , an , f (a1 ), . . . , f (an )), for any a, a1 , . . . , an
with ((a1 , . . . , an ), a) r and
h(r )(a1 , . . . , an , f (a1 ), . . . , f (anr )), where n is the arity of r .
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 19 / 25
Definitions by recursion example
Algebraic
Foundations of
Computer Science
(AFCS)
Example 17
Prof.Dr. F.L.
Tiplea
Let PF (A) be the set of propositional formulas over A. It is easy to
see that this set is free inductively defined.
Closures
Define a function f : PF (A) N in a recursive way as follows:
Structural
induction
Definitions by f (a) = 1, for any a A;
induction
Definitions by f () = f (), for any PF (A);
recursion
Course readings f (( )) = f () + f (), for any , PF (A);
f (( )) = f () + f (), for any , PF (A);
f (( )) = f () + f (), for any , PF (A);
f (( )) = f () + f (), for any , PF (A).
The function f returns the length of propositional formulas.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 20 / 25
Definitions by recursion more examples
Algebraic
Foundations of
Computer Science
Pick up your favorite programming language and:
(AFCS)
Prof.Dr. F.L.
Tiplea show that its set of arithmetic and logic expressions is
inductively defined;
Closures
Structural
induction define recursively the length of an arithmetic expression;
Definitions by
induction define inductively the set of variables of an arithmetic
Definitions by
recursion
expression;
Course readings
define recursively the height of an arithmetic expression.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 21 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
An important particular case of the recursion theorem:
(AFCS)
Prof.Dr. F.L.
Tiplea Theorem 18 (Recursion theorem for N)
Closures Let A be a set, a A, and h : N A A be a function. Then,
Structural there exists a unique function f : N A such that:
induction
Definitions by (1) f (0) = a;
induction
Definitions by
(2) f (n + 1) = h(n, f (n)), for any n.
recursion
Course readings
This result can be strengthen to:
Theorem 19 (Parametric recursion theorem for N)
Let A and P be sets, and g : P A and h : P N A A be two
functions. Then, there exists a unique function f : P N A such
that:
(1) f (p, 0) = g(p), for any p P;
(2) f (p, n + 1) = h(p, n, f (p, n)), for any p P and n N.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 22 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
Addition, multiplication, and exponentiation on natural numbers
(AFCS)
are defined by recursion:
Prof.Dr. F.L.
Tiplea
Addition:
Closures x +0=x
Structural x + (n + 1) = (x + n) + 1;
induction
Definitions by
induction Multiplication:
Definitions by x 0=0
recursion
x (n + 1) = (x n) + x;
Course readings
Exponentiation:
x0 = 1
x n+1 = (x n ) x.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 23 / 25
Definitions by recursion
Algebraic
Foundations of
Computer Science
In some cases the value of a function f at a natural number n may
(AFCS)
depend on the values of f at 0, . . . , n 1 (Fibonaccis sequence is
Prof.Dr. F.L.
Tiplea such an example).
Closures
The recursion in such cases is called hereditary.
Structural
induction
Theorem 20 (Hereditary recursion theorem)
Definitions by S
induction
Let A be a set, S = nN An , and h : N S A be a function.
Definitions by
recursion Then, there exists a unique function f : N A such that
Course readings
f (n) = h(n, f |n ),
for any n N (recall that f |0 = f | = A0 ).
Exercise: Develop a parametric version of the hereditary recursion
theorem.
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 24 / 25
Course readings
Algebraic
Foundations of
Computer Science
1 F.L. Tiplea: Fundamentele Algebrice ale Informaticii, Ed.
(AFCS)
Polirom, Iasi, 2006, pag. 7079.
Prof.Dr. F.L.
Tiplea
Closures
2 F.L. Tiplea: Introducere n Teoria Multimilor, Ed. Univesitatii
Structural
induction Al.I.Cuza, Iasi, 1998, pag. 8390.
Definitions by
induction
Definitions by
recursion
Course readings
Prof.Dr. F.L. Tiplea (UAIC) Algebraic Foundations of Computer Science (AFCS) Spring 2013 25 / 25