Rule Based Programming: 3rd Year, 2nd Semester
Rule Based Programming: 3rd Year, 2nd Semester
Logic programming
Propositional logic
Introduction to PROLOG
Programming paradigms: declarative
● You tell the computer exactly what to do.
● Hard to debug.
● Hard to implement - it needs as much data
as possible.
● Slow execution (limited control by a good
programmer).
● It can solve almost anything you can
describe.
Go to C309.
Inference
Knowledge Base Inference Rules
Engine
Logic programming = Rule based programming restricted to rules and facts modelled
in a formal logic.
The inference engines inference power is supported by the extensive deduction
mechanisms of first-order and predicational logic. Logic programming usually involves
less facts and rules than CLIPS for the same deductive power, but in CLIPS you can
model almost any deductive system you want.
A proof obtained using logic programming is usually already proved as valid. Not the
case for CLIPS, you have to demonstrate the validity of your inference rules. You can
use logic programming to prove or discover theorems, you cannot do this
(reasonably) with CLIPS.
Please consult the referenced additional information, it includes everything you
need for this and the following 2 courses.
Brief history of logic programming
Alan Robinson: A Machine-Oriented Logic Based on the Resolution Principle (1965)
https://drive.google.com/open?id=1oqpo0IIiLsEOsWtQXMRXLWWKiReeMrhX
First description of resolution over first-order logic predicates as a way for a computer to solve problems
Robert Kowalski and Donal Kuehner: Linear Resolution with Selection Function (1971)
http://www.doc.ic.ac.uk/~rak/papers/sl.pdf
First description of the actual way most logic programming works today
Rule of thumb: If you pronounce the formula in natural language and it makes sense, it’s
well formed. Are these WFF?
¬α∧¬, α¬α, ∃p∧∨∀p
Propositional logic
The syntax of propositional logic: you can prove a well formed formula 𝜎: (p→r) as a
consequence of a set of formulas F: (p→r)∧(r→ q) using implicit properties/rules
(deductive reasoning): F ⊢ 𝜎
The semantics of propositional logic: you can prove that a well formed formula is TRUE if
a set of F of formulas are TRUE: F ⊨ 𝜎 . This might not be equivalent to F ⊢ 𝜎 !
If the logical system is sound then we can say that if F ⊢ 𝜎 then F ⊨ 𝜎. If the logical system
is complete then we can say that if F ⊨ 𝜎 then F ⊢ 𝜎.
A system (model) is sound if you can’t use it to prove something that is wrong.
Consider a deduction system built to check whether any statement is TRUE or
FALSE. If every claim made by the system is accurate (is provable following logical
reasoning from the known facts) then the system is sound.
A system is complete if it can prove as true or false every statement. If for all possible
statements the system answers TRUE or FALSE, the system is complete.
A system can be sound and not complete: assign value of TRUE only to tautologies. A
system can be complete and not sound: assign value of TRUE to every input
statement.
Truth table has to have 2n lines, where n is the number of variables in the formula.
Complexity is therefore exponential.
Gödel theorems
First completeness theorem: If a formula is logically valid then there is a finite deduction (a formal proof) of the
formula. This means that we can solve anything is exponential time (SAT problem).
Proving that you can prove the validity of a formula (check the truth value of a formula for a model - assignment of
values for variables - in the truth table) in polynomial time would make you the most famous student of our faculty
ever. Every problem can be described as a formula, checking the correct answer can be done in polynomial time (P
problems). If that answer can also be proved (deducted from the available data) in polynomial time, then P=NP (see
Artificial Intelligence course 2).
First incompleteness theorem: Any consistent formal system F within which a certain amount of elementary arithmetic
can be carried out is incomplete; i.e., there are statements of the language of F which can neither be proved nor
disproved in F. Thus no (useful) open world deductive system is complete. Example: “This statement is false.”
Is there a way to use first-order logic for inference rules and deduction engine, well formed formulas as a knowledge
base, and still get a complete system? Next week: deductive systems, Horn clauses
A system (model) is sound if you can’t use it to prove something that is wrong.
Consider a deduction system built to check whether any statement is TRUE or
FALSE. If every claim made by the system is accurate (is provable following logical
reasoning from the known facts) then the system is sound.
A system is complete if it can prove as true or false every statement. If for all possible
statements the system answers TRUE or FALSE, the system is complete.
A system can be sound and not complete: assign value of TRUE only to tautologies. A
system can be complete and not sound: assign value of TRUE to every input
statement.
A constant is a single field value, always starts with lowercase and ends with a dot
white.
he_is_a_student.
?- eats(cat,What).
? denotes a query - an assertion that PROLOG has to check (deduce from the KB).
The second field in the query is a variable which will be unified (matched) to the second field in the only relation in
the KB. The output will be
What=mice
yes
To delete a fact use retract(X). X has to be previously unified with that fact.
Using variables and queries (II)
eats(cat, mice).
eats(cat, elephants).
?- eats(cat,What).
What=mice
Yes
What=elephants
Yes
The commands within the file are executed when loading the file. If the file contains queries
they are performed after loading.
Syllogisms - the first inference rules
Major premise: All men (M) are mortal (P). Another example:
Cats (M) are good hunters (P).
Minor premise: Socrates (S) is a man (M). Dogs (S) are as good hunters (P) as are cats (M).
Conclusion: Socrates (S) is mortal (P). Dogs (S) are good hunters (P).
All values X for which human(X) is tru will also have mortal (X) as true.
First if second. Conditions are on the right side, actions on the left! Opposite of CLIPS.
human(socrates).
?- mortal(socrates).
Yes
Inference rules (II)
mortal(X) :- human(X).
human(socrates).
?- mortal(Who).
Who=socrates
Yes
Variables referenced on the left don’t have to appear on the right. Major difference from CLIPS!
nice(weather) :- not(rain).
Map coloring in PROLOG (I)
Assign colours to the neighbours of Romania so that no two neighbouring countries have the same color associated
with it.
X and Y are neighbours if they have colours associated with them and
they are different
Map coloring in PROLOG (II)
Tell PROLOG which countries are neighbours:
map(Romania,Ukraine, Moldova, Bulgaria, Serbia, Hungary) :-
neighbour(Romania, Ukraine),neighbour(Romania, Moldova),
neighbour(Romania, Bulgaria),neighbour(Romania, Serbia),
neighbour(Romania, Hungary),
neighbour(Ukraine, Moldova),neighbour(Ukraine, Hungary),
neighbour(Bulgaria, Serbia),
neighbour(Serbia, Hungary).
.
Next week
Deductive systems
Horn clauses