0% found this document useful (0 votes)
47 views21 pages

Rule Based Programming: 3rd Year, 2nd Semester

The document provides an overview of logic programming and Prolog: - It discusses declarative programming paradigms where the computer is told the goal and uses deduction to reach it. Prolog uses backward chaining to check if a hypothesis can be proven from known facts. - A knowledge base in Prolog contains constants, variables, and relations. Relations associate labels with fields, and variables represent unknown fields that can be unified. - Queries in Prolog check if a deduction can be made by attempting to unify variables in the query with facts in the knowledge base. Multiple unifications may be possible, returning multiple solutions.

Uploaded by

Dany Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views21 pages

Rule Based Programming: 3rd Year, 2nd Semester

The document provides an overview of logic programming and Prolog: - It discusses declarative programming paradigms where the computer is told the goal and uses deduction to reach it. Prolog uses backward chaining to check if a hypothesis can be proven from known facts. - A knowledge base in Prolog contains constants, variables, and relations. Relations associate labels with fields, and variables represent unknown fields that can be unified. - Queries in Prolog check if a deduction can be made by attempting to unify variables in the query with facts in the knowledge base. Multiple unifications may be possible, returning multiple solutions.

Uploaded by

Dany Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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.

Reminder from course 1 about declarative programming.


We are still in the paradigm of declarative programming: we define goals for the
computer and it uses an inference/deduction/reasoning engine to reach our goal.
The goal for CLIPS was: what can you infer from these facts and these rules?
The goal for PROLOG: is this fact provable (demonstrable) from these facts and
these rules?
Forward and backward chaining
● Forward chaining (data driven reasoning)
○ start from facts, see what can be inferred;
○ what can be proved from the known facts?
○ no control over possible outcome;
○ CLIPS
● Backward chaining
○ start from a hypothesis, see if it’s supported by the facts;
○ can this be proven from the known facts?
○ outcome is always true/false - logical programming;
○ PROLOG

Reminder from course 2 about forward and backward chaining of inferences.


Forward chaining starts from facts and infers everything it can (CLIPS), resulting in
nothing, new facts or deletion of facts.
Backward chaining starts from a hypothesis (fact) and checks if it can be inferred from
the known facts. Result is always true or false.
Logic programming

Inference
Knowledge Base Inference Rules
Engine

Restricted to a formal logic


Additional information: https://www.cs.cmu.edu/~fp/courses/lp/lectures/lp-all.pdf

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

Alain Colmerauer: Un système de communication homme-machine (1973)


http://alain.colmerauer.free.fr/alcol/ArchivesPublications/HommeMachineFr/HoMa.pdf
First implementation of a logic programming language
Well formed formulas
● A formula is a sequence of symbols (α, β, γ, …), variables (p, q, v, …), logical
operators (¬, ∧,∨, →,↔) and parenthesis.
● TRUE and FALSE are well-formed formulas (WFF).
● Each primitive symbol in (α, β, γ, …) and (p, q, v, …) is a WFF.
● If A and B are WFF, then so are ¬A, A∧B, A∨B, A→B, A↔B. The result can be
included in parenthesis and it’s still a WFF.
● If p is a variable and A is a WFF formula then ∃pA, ∀pA are WFF.

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.

A parallel can be made to statistical evaluation metrics: if a system has 100%


accuracy, it’s sound. If it has 100% recall, it’s complete.
Is this formula TRUE or FALSE?
Truth values have to be explicitly assigned to formulas with variables replaced by each
possible value. p r truth value

𝜎: p→r TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE TRUE

FALSE FALSE TRUE

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 parallel can be made to statistical evaluation metrics: if a system has 100%


accuracy, it’s sound. If it has 100% recall, it’s complete.
PROLOG
PROLOG = programmation en logique

4 major revisions, still in wide scale use today.

Academic usage: everybody studying logical or rule based programming.


Industry usage: IBM’s DeepQA (https://www.ibm.com/watson), Apache’s UIMA
(https://uima.apache.org), ideal for CSP problems and fast prototyping.

Multiple implementations, check:


https://en.wikipedia.org/wiki/Comparison_of_Prolog_implementations

Recommended: SWIProlog: https://www.swi-prolog.org or https://swish.swi-prolog.org


Knowledge base in PROLOG
The Knowledge Base in PROLOG contains:
● Constants
● Variables
● Relations

A constant is a single field value, always starts with lowercase and ends with a dot

white.
he_is_a_student.

At prompt use assert(white).


Relations and variables
Relations in PROLOG are labels associated with zero or more fields.
Starts with a lowercase letter, ends with a dot.
After the first field it has a pair of parentheses containing all the other fields.
attends (Student, class).
snow (white).

Variables are fields starting with uppercase.


They have no implied value (like constants) and usually appear in relations, as in the above
example.
Can be unified (linked) in many ways, the simplest one is:
Student = john.
Using variables and queries (I)
eats(cat, mice).

Lets see if cat eats something.

?- 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

A second check will prompt the next unification possible.

What=elephants

Yes

If you just want to check if a a value can be unified, use _ : ?- eats(cat,_).


Run a PROLOG code file
Default extension is .pl. Use .pl or .pro.

Loading a file: ?- consult(file).

Loading multiple files: ?- consult([“file1.pl”,’file2’]). The extension is


assumed to be .pl or .pro, is not explicitly required.

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).

256 types of syllogisms, but:


Is the conclusion supported? No conclusion can have two negative premises.
S has to be in exactly one premise. No conclusion
M-P M- middle term can have two particular premises (both referring S).
P-predicate An affirmative conclusion must have two affirmative
S-M premises.
S-P S-subject A negative conclusion (no S) must have one
negative premise.
An universal conclusion (any S) must have two
If all M are P and S is M then S is P.
universal premises.
Inference rules (I)
mortal(X) :- human(X).

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

You can have multiple condition in a rule.

carnivore(X) :- eats (X, mice), eats (X, elephants).

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.

Predicates for colours:


colour(red).
colour(yellow).
colour(blue).
colour(green).

Tell PROLOG that neighbours have to have different colours:

neighbour(X,Y) :- colour(X), colour(Y), X\==Y.

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).

We want a map on which those countries are neighbours:


?- map(Romania,Ukraine, Moldova, Bulgaria, Serbia, Hungary)

Bulgaria = Ukraine, Ukraine = yellow,


Hungary = green,
This is just the first solution. PROLOG looks for values to unify with the
Moldova = Serbia, Serbia = blue, variables in the query. Some variables are unified with others
Romania = red (Bulgaria=Ukraine), which means they have the same colour.

.
Next week
Deductive systems

Horn clauses

PROLOG inference engine

PROLOG cuts and recursion

You might also like