Download Logic programming and Prolog Relation vs mapping The logic

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Knowledge representation and reasoning wikipedia , lookup

Reactive programming wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Relational model wikipedia , lookup

Functional programming wikipedia , lookup

Corecursion wikipedia , lookup

Structured programming wikipedia , lookup

Prolog wikipedia , lookup

Logic programming wikipedia , lookup

Transcript
Logic programming and Prolog
• The logic programming paradigm
• Logic programming
Relation vs mapping
Relation
Mapping
– Relations
– Resolution
• Prolog
–
–
–
–
Variables and terms
Clauses and relations
Control
”Closed-World” assumption
The logic programming paradigm
• This paradigm considers a program as describing a
relation between input and output. The functional
paradigm says that programs are mappings
(functions). Both are (more or less) declarative!
• Since relations are more general than mappings
logic programming may be regarded as being on a
higher level of abstraction than functional and
imperative programming.
• Relations in logic languages can be compared to
functions / subprograms in other languages.
How do we use the concepts?
• Mapping
– Given a, decide m(a)
• Relation :
–
–
–
–
Given a and b, decide if R(a,b) is true.
Given a, find all y such that R(a,y) is true.
Given b, find all x such that R(x,b) is true.
Find all x and y such that R(x,y) is true
Input
Output
Many to many
Input
Output
Many to one
Relations and mappings
• Let S and T be sets.
• R is a relation between S and T
if for every x ∈ S and y ∈ T
R(x,y) is either true or false.
Ex: greater than (>) for integers.
• m is a mapping (function) between S and T
if for (every) x ∈ S
there exists an m(x) ∈ T
Ex: sin(x)
Symbolic Logic
• Logic which can be used for the basic needs of formal
logic:
– Express propositions
– Express relationships between propositions
– Describe how new propositions can be inferred from other
propositions
• Particular form of symbolic logic used for logic
programming called predicate calculus
Logical Operators
Quantifiers
Name
Symbol
Example
Meaning
negation
¬
¬a
not a
conjunction
∩
a∩b
a and b
disjunction
∪
a∪b
a or b
equivalence
≡
a≡b
a is equivalent to b
implication
⊃
⊂
a⊃b
a⊂b
a implies b
b implies a
Predicate logic
• Logic programming is based on
first order predicate logic
• A formalism for describing logical truths (relations).
natural(0)
∀n, natural(n) → natural(successor(n))
• Together with inference rules (using resolution) we
can infer or prove new truths (propositions)
A ! B och B ! C
A!C
A och A ! C
C
Horn clauses
• Resolution - an inference principle that allows
goals to be computed from given propositions.
• ”Automatic” (computerized) inference cannot
handle all of first order predicate logic.
• Most systems are therefore limited to a subset
called Horn clauses.
A1 and A2 and … and An → B
natural(0).
natural(x) → natural(successor(x)).
Name
Example
Meaning
universal
∀X.P
For all X, P is true
existential
∃X.P
There exists a value of X such that
P is true
Logic programming languages
• A notation for writing logical propositions,
together with algorithms which implements the
inference rules.
• The logic program consists of the propositions
that are given (axioms), decribes the problem.
• The propositions you want to derive are given as
input to the program (”queries”, ”goals”)
• Can the goal be derived from the given axioms /
relations? Execution of the program gives a
yes/no answer
Horn clauses in logic languages
• A0 if A1 and ... and An
– Where Ai has the form R i (...), and R i is the name of a
relation.
– Means that if A1 ...An all are true, then A0 is true.
– Special case when n = 0.
A0. is true.
• Procedural interpretation
– To prove A0 , A1 to An must first be proved.
– A0 is a subprogram head, and A1 to An its body
Logic programs
• A logic program consists of a collection of Horn
clauses.
– The restriction to Horn clauses makes it possible to make
a fairly efficient implementation of logic programs.
• To execute a program means to test the truth of a
given assumption/goal (A).
– If we can infer from the program that A is true we say
that A succeeds, otherwise we fail.
• A failure does not imply that A is false!
Alternative paths
• If an Ai fails we must investigate if there is
another clause that matches A.
• When no clause matches A we have failed.
• This gives rise to a search tree.
• In logic programming in general the search
algorithm is undefined.
• In an implementation of a logic programming
language (e.g Prolog) the search algorithm must
be defined.
Prolog - basic elements
• The ”building blocks” (terms) of Prolog are
constants (numbers, atoms), variables and
structures. Lists are a special kind of structure
(and strings are a special kind of lists).
• Atoms
– Atoms have no properties except for their
distinguishing name.
Ex red, blue, green....
• Variables; are not declared, can be instantiated
(bound to a value) or uninstantiated
Resolution
• The testing is implemented using resolution.
• If the program contains a clause
A0.
which matches A (can be unified w A), it succeeds.
• If the program contains a clause
A0 if A 1 and ... and An.
such that A0 matches A we continue by testing A1
to An separately as subgoals.
• If all of them succeeds we conclude that A is true.
Prolog
• Prolog was developed gradually during the 70:ies
and several dialects appeared.
• Nowadays the Edinburgh dialect is accepterad as
standard.
• Japan: Fifth Generation Computing System
(1980:ies)
• A Prolog program consists of a collection of Horn
clauses defining relations.
• A precise definition of the method of logical
inference.
Types
• Prolog is untyped.
• Numbers, atoms and structures can be used
together, no type checks!
• All values can be compared with each other
(using =)
Structures
• Basically a relation (a predicate)
• Can be used to represent data, regarded as
tuples/records, or as ”function calls”.
• The tag (functor) is used to distinguish between
structures that represents unrelated data, but have
the same values. (Cf constructors in ML). E.g
point(2,3), rational(2,3).
• Structures can be nested.
Lists
• Lists are a subset of all structures where
– [] represents the empty list, and
– '.' is the structure tag
.(1,.(2,.(3,.(4,[]))))
• Abbreviations:
[1,2,3,4]
[x|xs] = .(x,xs)
Variables
• Variables in Prolog denotes a fix but unknown
value (not updateable).
• Always starts with a capital letter to be
distinguished from structures and atoms.
• Variables are implicitely declared by occuring in
a clause, and their scope is exactly that clause.
Terms
• A term may be a:
–
–
–
–
variable
numerical literal (number)
atom
structure (contains terms).
• Terms are arguments to the relations.
– R(t1 ,...,tn ) where t1 ... tn are terms.
Clauses and relations
• A0 :- A1, A2, ..., An.
– ":-" stands for if and "," stands for and
• A0 matches a proposition A if there exists a
substitution of terms for variables such that A0
and A becomes equivalent. A and A0 is unified.
• age(P,Y) matches age(david,42) under
{P = david,Y = 42}
• age(P,33) matches age(olof,A) under
{P = olof, A = 33}
• The scope of a relation is the whole program.
Example
star(sun).
star(sirius).
star(betelgeuse).
?- star(sun).
Yes
?- star(jupiter).
False
Example
orbits(mercury,sun).
orbits(venus,sun)
…
?- orbits(venus,sun).
Yes
?- orbits(B,sun).
B = mercury
B = venus
…
Example
planet(B) :- orbits(B,sun).
?- planet(venus).
Yes
?- planet(B).
B = mercury
B = venus
…
Questions
• Question (goal statement): does there exist
instances such that the question becomes true?
• Clauses (rule statement): A0 is true for all values
of the variables such that A1 ...An are true.
• Recursive clauses:
element(X,[X|Ys]).
element(X,[Y|Ys]) :- X \== Y, element(X,Ys).
Example
append([],Xs,Xs).
append([Y|Ys],Xs,[Y|Zs]) :append(Xs,Ys,Zs).
?- append([1,2],[3,4],Zs).
Zs = [1,2,3,4]
?- append([1,2],Ys,[1,2,3,4]).
Ys = [3,4]
?- append(Xs,Ys,[1,2,3]).
Xs = [] Ys = [1,2,3]
Xs = [1] Ys = [2,3]
…
Example
• Sorting
sort(Xs,Ys) :permutation(Xs,Ys),sorted(Ys).
• Greatest common divisor
gcd(X,0,X).
gcd(X,Y,W) :Y \= 0,Z is X mod Y, gcd(Y,Z,W).
Control in Prolog
• The behaviour of Prolog programs depends on
the order of the resolution.
neighbor(A,B) :- neighbor(B,A).
neighbor(A,B) :- star(A),orbits(B,A).
• If the non-recursive case always is used first no
problems occur, but if the recursive case is used
first we get infinite recursion.
• The user can affect efficiency a great deal (which
can be good or bad).
Control, cont.
• Bottom-up resolution, forward chaining
– Begin with facts and rules of database and attempt to
find sequence that leads to goal
– Works well with a large set of possibly correct answers
• Top-down resolution, backward chaining
– Begin with goal and attempt to find sequence that leads
to set of facts in database
– Works well with a small set of possibly correct answers
• Prolog implementations use backward chaining
Control in Prolog
• If the proof (the matching) of a subgoal fails
(given a certain instantiation of the variables),
backtrackning is needed
• Backtrackning can be interrupted using ”cut”, ”!”
Ex: a,b,!,c,d
• Contrary against the basic idea with Prolog (what,
not how). Can be misused the same way as jumps
in imperative lang.
Negation
• The closed world means that Prolog does not
distinguish between false and unknown.
• The negationen of something unknown is true(!)
star(sun).
star(betelgeuse).
?- not(star(sirius)).
yes
Control, cont
• When a clause has more than one goal the possible
searches/matches can be shown as a search tree. Should
search be done depth-first or breadth-first?
• Prolog uses depth-first search
• Demands less resources, but can lead to infinite recursion
in nasty cases (ex left recursion).
• In every clause, the goals on the right side are tested from
left to right. If there are several clauses in a relation the
textually first is tested first.
The closed world
• A proposition will fail if we cannot deduce that it
is true.
• That does not necessarily imply that it is false, it
can also mean that it is unknown.
• Prolog assumes that all relevant facts are in the
program, i.e all that is true is possible to deduce
from the program.
• This is called the closed-world assumption.
Logic languages vs functional languages
• L:relations, F:functions. Relations can be used in
several ways. L potentially non-deterministic.
Unification more powerful than pattern matching.
• F: often higher order functions
• F: often strongly typed. L: often untyped
• F: simpler to implement, simpler to understand
A merry, functional, slightly logical,
but not too object-oriented Christmas!
…and don’t forget to consider the
original semantics of Christmas!