logic programming - unideb.hu · logic programming •based on automatic theorem proving. ......

18
Logic Programming Based on automatic theorem proving. Controlled deduction: algorithm = logic + control. Logic: declarative (logic) description of the problem. Control: a theorem-proving strategy. The problem is described in terms of relations, represented as facts and rules. A program is made up of facts, rules, and a query (goal). Running a program: trying to prove the goal.

Upload: others

Post on 16-May-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Logic Programming

• Based on automatic theorem proving.

• Controlled deduction: algorithm = logic + control.• Logic: declarative (logic) description of the problem.

• Control: a theorem-proving strategy.

• The problem is described in terms of relations, represented as facts and rules.

• A program is made up of facts, rules, and a query(goal).

• Running a program: trying to prove the goal.

Page 2: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Prolog

• Primary representative of logic programming languages.

• First interpreter was created by Alain Colmerauer and Philippe Roussel in Marseille, France, in 1972.

• Prolog = programmation en logique (programming in logic).

• Mathematical foundation: first-order predicate calculusand SLD resolution.

• Logically, the Prolog engine tries to find a resolution refutation of the negated query.

• The language has purely logical, metalogical, and extralogical (imperative) features.

Page 3: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Types in Prolog

• Prolog is a single-type language. The only type is term.

• A term can be a simple term or a compound term.

• A simple term can be a constant or a variable.

• A constant can be a number or an atom (name).

• A number can be an integer or a float.

• A compound term (or structure) is composed of an atom and a comma-separated list of arguments in parentheses. The arguments are terms. The number of arguments is called the term’s arity. The combination of the name and arity is called the term’s functor.

Page 4: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Types in Prolog

• Examples of numbers: 2016, -0.5.

• An atom is a general-purpose name with no inherent meaning. The name is either an identifier beginning with a lowercase letter, or an arbitrary sequence of characters enclosed in single quotes. E.g., x, red, 'Taco', 'some atom'.

• A variable is a placeholder for an arbitrary term. It’s an identifier beginning with an uppercase letter or an underscore. E.g., X, Var, _x, _.

• Examples of compound terms: truck_year('Mazda', 1986), 'Person_Friends'(zelda, [tom, jim]).

Page 5: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Types in Prolog

Special cases of compound terms:

• List: an ordered collection of terms, denoted by square brackets with the terms separated by commas. E.g., [], [1, 2, 3], [red, green, blue].

• String: a sequence of characters surrounded by double quotes. Equivalent to a list of (numeric) character codes or one-char atoms. E.g., ″to be, or not to be″.

Page 6: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Prolog Program Elements

• Prolog programs describe relations between objects of the model, defined by means of clauses. Pure Prolog is restricted to definite clauses (Horn clauses with exactly one positive literal).

• A subset of clauses specifying a given relation (i.e., having the same functor) is called a predicate and is denoted by its functor in the form name/arity, e.g., father/2.

• Execution of a Prolog program is initiated by the user’s posting of a single goal, called the query.

• Further program elements may include declarations(modifying the way predicates are executed) and directives (setting the runtime environment).

Page 7: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Clauses

• There are two types of clauses: facts and rules.

• A rule is of the formHead :- Body.

and is read as “Head is true if Body is true.” The :- is called the neck operator.

• Clauses with empty bodies are called facts:Head.

is equivalent toHead :- true.

The built-in predicate true/0 is always true.

Page 8: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Example Clauses and Goal

father(john, frank). %fact

father(frank, peter). %fact

grandfather(X, Z) :- father(X, Y), father(Y, Z). %rule

?- grandfather(Somebody, peter). %goal

Page 9: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Execution of a Prolog Program

A computation may be represented by a search tree, defined as follows:

• Each node represents the current goal and a local substitution.

• The root node represents the initial goal and an empty substitution.

• There are two kinds of leaf nodes:• Nodes with an empty goal are called success nodes.• Nodes with a goal such that there is no clause whose head is unifiable

with the first predicate of the current goal are called failure nodes.

• A nonleaf node has as many children as there are clauses whose head is unifiable with the first predicate of the current goal. If this number is greater than 1, then this node is called a choice point, otherwise it is a deterministic node.

Page 10: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Execution of a Prolog Program

• In case of a nonleaf node, the first predicate of the current goal is unified with the head of the next possible clause (in the order of appearance).• If that clause is a fact, then the first predicate is deleted

from the new current goal.

• If that clause is a rule, then the first predicate is substituted with the body of the rule in the new current goal.

• The local substitution in the child node will contain the variable bindings required for the unification.

Page 11: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Execution of a Prolog Program

• If a failure node is reached (i.e., there is a temporary failure), all variable bindings that were made since the most recent choice point was created are undone, and execution continues with the next alternative of that choice point. This is called backtracking.

• If a success node is reached, the system answers with “Yes,” along with the answer substitution, i.e., the composition of all the local substitutions along the path starting from the root up to the success node, restricted to the variables of the initial goal. If, at this point, the user decides to continue the search, then a backtracking is done.

• If a backtracking is needed from the root node, the system answers with “No,” as no (more) solutions exist.

Page 12: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Example Execution

grandfather(Somebody, peter)

X = Somebody, Z = peter

father(Somebody, Y), father(Y, peter)

Somebody = john, Y = frank

father(frank, peter)

success node

Somebody = frank, Y = peter

father(peter, peter)

failure node

father(john, frank).

father(frank, peter).

grandfather(X, Z) :- father(X, Y), father(Y, Z).

?- grandfather(Somebody, peter).

Page 13: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Exercise

Draw the search tree for the following problem:

p(X, Y) :- q(X), r(X, Y).

p(X, Y) :- s(X).

s(d).

q(a).

q(b).

q(c).

r(b, b1).

r(c, c1).

?- p(U, V).

Page 14: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Exercise

Draw the search tree for the following problem:p(a).

p(X) :- q(X), r(X).

p(X) :- u(X).

q(X) :- s(X).

r(a).

r(b).

s(a).

s(b).

s(c).

u(d).

?- p(X).

Page 15: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Unification Rules

Unification of two terms t1 and t2:

• If t1 and t2 are constants, then t1 and t2 unify only if they are the same atom or the same number.

• If t1 is a variable, and t2 is a term that is not a variable, then t1 and t2 unify, and t1 is instantiated to t2.

• Similarly, if t2 is a variable, and t1 is a term that is not a variable, then t1 and t2 unify, and t2 is instantiated to t1.

• If both t1 and t2 are variables, then t1 and t2 unify, and both of them are instantiated to a new shared variable.

Page 16: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Unification Rules

Unification of two terms t1 and t2:

• If t1 and t2 are compound terms, then t1 and t2unify only if• they have the same functor,

• all their corresponding arguments unify, and

• the variable instantiations are compatible.

Page 17: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Unification Rules

Unification of two terms t1 and t2:

• If t1 and t2 are empty lists, then t1 and t2 unify.

• If t1 and t2 are nonempty lists, then t1 and t2unify only if• their heads unify,

• their tails unify, and

• the variable instantiations are compatible.

Page 18: Logic Programming - unideb.hu · Logic Programming •Based on automatic theorem proving. ... (logic) description of the problem. •Control: a theorem-proving strategy. •The problem

Unification

• The =/2 predicate tests whether its two arguments unify.

• Most implementations of Prolog skip the “occurs check” for efficiency, i.e., the goal

?- X = f(X).

will respond yes. Unification with the occurs check can be done using the predicate unify_with_occurs_check/2.