Chapter_1_Introduction to Formal Language, Logic and Proof
Chapter_1_Introduction to Formal Language, Logic and Proof
ANUJ GHIMIRE
Course Overview and
Objective
About TOC
It is one of the most fundamental course of Computer
science.
Will help to understand how people thought about the
computer Science and Automation.
It is mainly about what type of problem can really
compute mechanically, how fast and how much of
space does it take to do so.
This course is about the fundamental capabilities and
limitations of computers.
About TOC
Nowadays, the Theory of Computation can be divided
into the following three areas:
Automata Theory
Deals with definitions and properties of different types of
“computation models” (FA, PDA, Turing Machines).
Computability Theory
Tries to classify problems as being solvable or unsolvable.
Complexity Theory
Classifies problems according to their degree of “difficulty”.
Give a rigorous proof that problems that seem to be “hard” are
really “hard”.
Objectives of TOC
To provide basic understanding of theory to:
Build formal mathematical models of computation.
Analyse the inherent capabilities and limitations of these
models.
Course Goals:
Simple practical tools you can use in later courses, projects,
etc. The course will provide you with tools to model
complicated systems and analyse them.
Inherent limits of computers: problems that no computer can
solve.
Better fluency with formal mathematics (closely related to
skill at debugging programs).
Applications of TOC
Design of Compiler
Pattern Matching
Design of the Embedded System (Traffic Light System,
Attendance through the Bio-Metrics).
Natural Language Processing
Vending Machines (ATM, Token Generation).
Syllabus
Syllabus
Syllabus
Syllabus
Evaluation Criteria
c c
d d
Types of Relation
Equivalence Relation:
A relation that is reflexive, symmetric and transitive are
called equivalence relation.
Partial Order Relation:
A relation R on a set S is called a “Partial ordering” or a
“Partial order”, if R is reflexive, anti-symmetric and
transitive.
Function
A function from a set A to a set B is a binary relation R
on A and B with the following special property:
for each element a ∈ A, there is exactly one ordered pair
in R with first component a.
Let C be the set of cities and S be the set of states; and
let
R1 = {(x, y): x ∈ C, y ∈ S, and x is a city in state y}
R2 = {(x, y): x ∈ S, y ∈ C, and y is a city in state x}.
Here the relation R1 is a function, since each city is in
one and only one state, but R2 is not a function, since
some states have more than one city.
Function
Suppose every element of S occurs exactly once as the
first element of an ordered pair.
In Fig shown, every element of S has exactly one arrow
arising from it.
S
This kind of relation is called a “function”.
Function
A function is said to map an element in its domain to
an element in its range.
Every element in S in the domain, i.e., every element
of S is mapped to some element in the range.
No element in the domain maps to more than one
element in the range.
In general, we use letters such as f, g, and h for
functions and we write f : A B to indicate that f is a
function from A to B.
Function
If a is any element of A we write f (a) for that element
b of B such that ( a, b) ∈ f ; since f is a function, there
is exactly one b ∈ B with this property, so f (a) denotes
a unique object.
The object f (a) is called the image of a under f.
Types of Function
One-to-One Function (Injection)
Onto Function (Surjection)
One-to-One Onto function (Bijection)
Types of Function
One-to-One Function (Injection)
A function f : AB is said to be one-to-one if different
elements in the domain A have distinct images in the
range.
Types of Logic
Propositional Logic
Predicate Logic
Fuzzy Logic ( Studied in AI……)
Proposition
A declarative statement that is either true or
false but not the both are called propositions
Eg: Today is Friday.
2+2=5
Pashupati Nath lies in Kaski district.
However following are not propositions
What is your name? (this is a question)
Please keep silence. (this is a request)
X is even number. (depends on what X represents)
Variables are used to represent the
propositions.
Eg:
P: Today is Friday.
Q: Kathmandu is capital city of Nepal.
R: It is raining
Here P,Q and R are variables.
Propositional Logic
Logic that deals with propositions for
reasoning is called propositional logic.
Hypothesis Conclusion
(necessary condition) (sufficient condition)
Some variety of implications
P only if Q
Q if P
Q when P
Q is necessary for p
Q provided that P
if P,Q
Q follows from P
a sufficient condition for Q is P.
P Q P→Q
T T T
T F F
F T T
F F T
Indirect approach
Fig: Truth table of implication
Let ‘P’ & ‘Q’ be propositions then implication
of P and Q is denoted as ‘P↔Q’ and read as:
“P if and only if Q”
Here P and Q both are sufficient and necessary
conditions
P Q P↔Q
T T T
T F F
F T F
F F T
T T
T F
F T
F F
(P→Q)∧(Q →P)
T T T T T
T F F T F
F T T F F
F F T T T
(P→¬Q)↔(¬P∨Q)
P Q ¬P ¬Q P→¬Q ¬P∨Q (P→¬Q) ↔(¬P∨Q)
T T
T F
F T
F F
(P→¬Q)↔(¬P∨Q)
P Q ¬P ¬Q P→¬Q ¬P∨Q (P→¬Q) ↔(¬P∨Q)
T T F F F T F
T F F T T F F
F T T F T T T
F F T T T T T
(¬P↔R)∨[(¬Q∧P)→¬R]
P Q R ¬P ¬Q ¬R ¬P↔R ¬Q∧P (¬Q∧P) (¬P↔R)∨[(¬Q∧P)→¬R]
→¬R
T T T
T T F
T F T
T F F
F T T
F T F
F F T
F F F
(¬P↔R)∨[(¬Q∧P)→¬R]
P Q R ¬P ¬Q ¬R ¬P↔R ¬Q∧P (¬Q∧P) (¬P↔R)∨[(¬Q∧P)→¬R]
→¬R
T T T F F F F F T T
T T F F F T T F T T
T F T F T F F T F F
T F F F T T T T T T
F T T T F F T F T T
F T F T F T F F T T
F F T T T F T F T T
F F F T T T F F T T
These terms are defined in terms of conditional
statements
Let P→Q be an implication (conditional) statement
then:
Inverse :
An inverse of the conditional statement is the negation
of both the hypothesis and the conclusion.
P ¬P P∧¬P
T F F
F T F
Contingency is a compound proposition
whose truth values are combination of both
true and false.
Eg. ¬P∨Q is a contingency
P Q ¬P ¬P∨Q
T T F T
T F F F
F T T T
F F T T
Let P and Q be two compound propositions
then P and Q are said to be logically
equivalent if their truth values are always
same under same condition of truth values of
constituent propositons.
Eg.
Implication and its contra-positive are
logically equivalent.
i.e: P→Q ≡¬Q→¬P
P→Q ≡¬Q→¬P
P Q ¬P ¬Q P→Q ¬Q→¬P
T T F F T T
T F F T F F
F T T F T T
F F T T T T
Show that following compound propositions
are logically equivalent using truth table.
P↔Q ≡ [(P→Q)∧(Q→P)]
[P∨(Q∧R)] ≡ [(P∨Q)∧(P∨R)]
There are simple three steps to translate the
sentences into propositional logic statements
Identify all the individual sentences in the given
sentence and represent them by different variables.
Identify all the connectives used in given sentence.
Write an expression in terms of variables and
connectives.
Note: we assume there is no negative statements in the
given sentences
Obtained compound propositional will be the
required statement of propositional logic.
Eg:. You can access the college internet only if you are a
computer science student or you are not a fresher.
Solution
Individual sentences are:
P: You can access the college internet
Q: You are a computer science student
R: You are a fresher
Connectives are:
You can access the college internet only if you are a
computer science student or you are not a fresher.
Only if i.e implication (→)
Or i.e disjunction (∨)
Not i.e negation(¬)
Propositional Logic representation:
P →(Q∨¬R)
Hiking is safe along the trail if and only if berries are ripe along
the trail and bears have not been seen along the trail.
Solution
Individual sentences are:
P: Hiking is safe along the trail
Q: Berries are ripe along the trail
R: Bears have been seen along the trail.
Connectives are:
Hiking is safe along the trail if and only if berries are ripe
along the trail and bears have not been seen along the trail.
If and only if i.e double-implication (↔)
and i.e conjunction (∧)
Not i.e negation(¬)
Propositional Logic representation:
P ↔(Q∧¬R)
Arguments:
◦ An argument in propositional logic is the sequence
of propositions.
◦ The last statement is the conclusion and all its
preceding statements are called premises (or
hypothesis).
◦ The argument is valid if the premises imply the
conclusion
A valid argument is one where the conclusion
follows from the truth values of the premises.
To deduce new statements from the statements
whose truth that we already known, Rules of
Inference are used.
Rules of Inference in propositional logic are:
Addition
Simplification
Modus Ponens
Modus Tollens
Hypothetical Syllogism
Disjunctive Syllogism
Conjunction
Resolution
Addition:
If P is a premise, we can use Addition rule to derive
P∨Q
Example:
“He studies very hard and he gets best grade”: P∧Q
P: He studies very hard.
Q: He gets best grade
Example:
"If it rains, I will not go to school”, P→Q
“If I don't go to school, I won't need to do homework”.
Q→R
Therefore − “If it rains, I won't need to do homework.” P→R
Conjunction :
If P and Q are two premises, we can use
conjunction to derive P∧Q
Example:
“He is good at study”, P
“He gets highest marks in TOC”. Q
Therefore − “He is good at study and he gets highest
marks in TOC.” P∧Q
Resolution :
If P∨Q and ¬Q∨R are two premises, we can use resolution
to derive P∨R
P∨Q
¬Q∨R
∴ P∨R
Addition Conjunction
Simplification Disjunctive
Syllogism
Hypothetical
Modus Ponens Syllogism
2 ¬p Using simplification on 1.
5 ¬r →s Given hypothesis
Its complement is
Diagonalization Principle
This corresponds to the diagonal set D = { a, d, f}.
Indeed, D is different from each row of the array; for
D, because of the way it is constructed, differs from the
first row in the first position, from the second row in
the second position, and so on.
The diagonalization principle holds for infinite sets as
well, for the same reason: The diagonal set D always
differs from the set Ra on the question of whether a is
an element, and hence cannot be the same as Ra for
any a.
Diagonalization Principle
The diagonalization principle to make the proof are as
follows:
Assume condition for contradiction.
Find the reversed diagonal and check whether it is
different from each row in table or not.
If it is different from each row in table, it contradict the
assumed condition proving the theorem.
The set of real numbers in {0, 1} is uncountable
can be proved using diagonalization principle.
Diagonalization Principle
In the theory of computation, diagonalization is a
fundamental technique used to show that certain
problems are undecidable or uncomputable.
The basic idea behind diagonalization is to construct a
problem that cannot be solved by any algorithm or
Turing machine.
One famous example of diagonalization is the proof
that the halting problem is undecidable.
The halting problem is the problem of determining,
given a program and an input, whether the program
will eventually halt or run forever.
Diagonalization Principle
Alan Turing proved that there is no algorithm that can
solve the halting problem for all possible programs and
inputs.
Another example of diagonalization is the proof that there
are infinitely many prime numbers.
This proof uses a technique called Euclid's proof by
contradiction, which involves assuming that there are only a
finite number of primes and then constructing a new prime
that contradicts that assumption.
Overall, diagonalization is a powerful tool in the theory of
computation that allows us to prove important results
about the limits of computation.
1.4: Alphabets, Language and
Operations
Alphabet
Alphabet is a finite non-empty set of symbols.
The symbols can be the letters such as {a, b, c},
bits {1, 0} ,digits {0,1,2,3...…..9}, common characters
like $, #, etc.
It is denoted by ∑.
∑={0,1} is a binary alphabet.
∑={0,1,2,….9} is a decimal alphabet.
∑={a, b, c,……, z, A, B, C,……….Z} is an English alphabet.
String
String is a finite sequence of symbols taken from some
alphabet (∑), where each symbol is element of ∑.
It is denoted by w.
Example: Strings over the binary alphabet ∑{ 0, 1 } can
be w={0,1,00,11,001,1101,1111}
w={aab, abcb, b, cc} are four strings over the alphabet
∑ { a, b, c }.
String
The number of symbols in a string w is called its
length, denoted by |w|.
If W=aabab then |w| is 5.
The empty string denoted by ε(epsilon) is string
having length zero. i.e w= ε then |w| =0.
The set of all string over an alphabet is denoted by ∑* .
Example ∑={a,b,c} then
∑*={ε, a, b, c, aa, ab, bc, ca, aca, aacb, ……….}
Power of Alphabet
The set of all strings of certain length k from an
alphabet is the kth power of alphabet i.e.:
∑k = {w: |w|= k}
For ∑={0,1}
∑0 ={ε}
∑1 ={0,1}
∑2 ={00, 01, 10, 11}
∑3 ={000, 001, 010, 011, 100,101, 110, 111}
Kleen Closure
The set of all the strings over an alphabet ∑ is called
kleen closure of ∑ and is denoted by ∑* thus, kleen
closure is set of all the strings over alphabet ∑ with
length 0 or more.
∑* = ∑0 ∑1 ∑2 ∑3 ………
If ∑={a} then ∑*= {an, n=0,1,2,3…….}
∑*= {ε, a, aa, aaa, aaaa, ………….}
Positive Closure
The set of all the strings over an alphabet ∑ is called
positive closure of ∑ except ε(epsilon) and is denoted
by ∑+,.
∑+ = ∑1 ∑2 ∑3 ………
If ∑={a} then ∑+= {an, n=1,2,3…….}
∑+= {a, aa, aaa, aaaa, ………….}
String Operation
String Reversal:
For any string w, its reversal denoted. wR, is a string
spelled backward.
Example: w={bbabba} then wR ={abbabb}.
String Concatenation:
Two strings over the same alphabet can be combined to
form a third by operation of concatenation.
The concatenation of strings x and y written xoy or
simply xy is the string x followed by the string y.
If x=aba and y=bba then xy=ababba and yx=bbaaba
Note: concatenating the empty string ε with another
string, the result is just the other string.
String Operation
Suffix of a string:
String is called suffix of a string w if it is obtained by removing
zero or more leading symbols in w.
For example: If w= abcd then,
s=bcd, s=cd, s=d are suffix of w
s is proper suffix if s ≠ w.
Prefix of a string:
String is called prefix of a string w if it is obtained by removing
zero or more trailing symbols in w.
For example: If w= abcd then,
s=a, s=ab, s=abc are prefix of w
s is proper prefix if s ≠ w.
String Operation
Substring:
A strings s is called substring of a string w if it is obtained
by removing one or more leading or trailing symbols in w.
It is proper substring if s ≠ w.
A string v is substring of a string w if and only if there are
strings x and y such that w=xvy
In other word s is a substring of w if s appears
consecutively within w. e.g " deck" is a substring of
abcdeckabcjkl.
Language
A language denoted by L over an alphabet ∑ is subset
of all the strings that can be formed out of ∑. i.e. a
language is subset of kleen closure over an alphabet
∑ :L ∑* (set of strings chosen from ∑* defines
language).
For example:
→ Set of all strings over ∑= {0,1} that ends with 1.
L={1, 01 ,11, 011, 0111,.......}
→ English language is a set of strings taken from
the alphabet ={a, b, c,.....,z, A, B, C,.......Z}.
L= {dbms, TOC, Graphics}.
Language
Example
For ∑ = { 0, 1}
L= { x : x є { 0,1 } * | x has even number of Zero's }
then 011011100 є L , 1111 є L , 1011011000 ∉ L .
The infinite language L are denoted as
L= { w є ∑* : w has property P}
Empty Language
A language is empty if it does not have any strings
within it.
ε is an empty language and is a language over any
alphabet.
It does not contain any string.
Membership in Language
Membership in a language defines association of some
string within the language.
A membership problem is the question of deciding
whether a given string is a member of some particular
language or not i.e. whether the string belongs to the
given language or not.
Example: Given, L= {DBMS, TOC, GRAPHICS}, then for
w= "DBMS" the membership of w is true on L and for
w= "HELLO" the membership of w is false in L.
Regular Expression
Regular Expression is a formula for representing a
language in terms of a form combined using 3
operations union, concatenation and kleen closure.
A regular expression is a string that describe a whole set
of strings, according to certain syntax rules.
Any regular expression is recursively defined as:
Empty state (φ), empty string(ε) symbol of input
alphabet (∑) are regular expression
Let R1 and R₂ be regular expression then union of R1 and
R2 denoted by (R1 + R2) is also Regular Expression.
Regular Expression
Let R1 and R₂ be regular expression then concatenation
of R1 and R2 denoted by (R1 . R2) is also Regular
Expression.
Let R be regular expression then kleen closure of R
denoted by R* is also Regular Expression.
Note: Every regular expression is associated with
some language.
Regular Expression
Example of regular expression, Assume that ∑ = {a, b, c}
Zero or more: a* means “zero or more a’s”,
To say “zero or more ab’s,” i.e., {ε, ab, abab, …..} you need to say
(ab)*.
One or more: Since a* means “zero or more a’s”, you can use
aa* (or equivalently a*a) to mean “one or more a’s”.
Similarly to describe ‘one or more ab’s”, that is {ab, abab,
ababab, ……}, you can use ab(ab)*.
Zero or one: It can be described as an optional ‘a’ with (a + ε).
Any string at all: To describe any string at all (with ∑ = { a, b, c}
you can use (a + b + c)*.
Regular Expression
Any non-empty string: This is written any character from
∑ = {a, b, c} followed by any string at all:
(a + b + c) (a + b + c)*
Any string not containing ..........: To describe any string at
all that does not contain an ‘a’ (with ∑ ={a, b, c}), you can
use (b + c)*.
Any string containing exactly one ........: To describe any
string that contains exactly one ‘a’ (with ∑ ={a, b, c}) put
“any string not containing an a”, on either side of the ‘a’
like: (b + c)* a (b + c)* or a(b+c)* or (b+c)*a
Regular Expression
Example:
(0+1)*=
0*+1*=
(01)*=
0* .1*=
1.1*=
Regular Expression
(0+1)*={ ∈,0,1,00,101,1101,00101,…………}
0*+1*={ ∈,0,1,00,11,111,0000,…………}
(01)*={∈,01,0101,010101,……………}
0* .1*= { ∈,0,1,00,11,000,01,001,0111……}
1.1*={1,11,111,11111,……………}
Regular Expression
Precedence of Regular-Expression Operators
The star Operator (* ) is of highest precedence.
Next in precedence comes the concatenation (.) or dot
operator.
Finally, unions (+) are grouped with their operands.
For example:
The expression 10*+0 is grouped (1(0)*)+0.
Regular Language
The regular languages are those languages that can be
constructed from the three set operations:
Union
Concatenation
Kleen star.
Let ∑ be an alphabet. The class of “regular languages”
over ∑ is defined inductively as follows:
ф is a regular language so as empty string {ε} is regular
language.
For each symbol σ ε Σ. {σ} is a regular language.
If L1, L2, L3 are languages, then so is L₁ L2 L3 …….
are also regular.
Regular Language
If L1, L2 languages, then so is L₁ o L2 o L3 o ………... are
also regular
If L is a regular language, then so is L*.
Nothing else is a regular language unless its construction
follows from the above rules.
Algebraic Law for RL
Commutative law for union: L + M = M + L
Associative law for union: (L + M) + N = L + (M + N)
Associative law for concatenation: (LM)N = L(MN) ,
Note that there is no commutative law for
Concatenation, i.e. LM ≠ ML
The identity for union is: L + ф = ф + L = L
The identity for concatenation is: L. ε = ε. L = L
Left distributive law: L(M + N) = LM + LN
Right distributive law: (M + N)L = LM + LN
Idempotent law: L + L = L
Algebraic Law for RE
Laws Involving Closure
(L*)* = L* – i.e. closing an already closed expression does
not change the language
ф*=ε
ε*=ε
L+ = L. L* = L*L
Some Questions
Write a regular expression for the language. L: {w ∈ {a, b}*:
w has even number of 'a's followed by odd number of ‘b’ s
Write a regular expression for the set of strings over ∑{0, 1 }
with exactly two 0's.
Write regular expressions for the language which generates
strings of even length over the alphabet ∑{a, b}.
Describe as simply as possible in English the language
corresponding to the regular expression a*b(a*ba*b)*a*
Write a regular expression for the language in which strings
start and end with same symbol over alphabet ∑{a, b }.
Write a regular expression for the language in which strings
start and end with different symbol over alphabet ∑{a, b }.
Some Questions
Q1. Answer: (aa)*(bb)*b
Q2. Answer: 1* 0 1*0 1*
Q3. Answer: (aa+ab+ba+bb)*
End of Chapter 1
Thank You !!!!