9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint...

28
9 October 2006 Foundations of Logic and Constrain t Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: http://ssdi.di.fct.unl.pt/flcp
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    237
  • download

    2

Transcript of 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint...

Page 1: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 1

Foundations of

Logic and Constraint

Programming

Lecturer: Pedro Barahona

Web page: http://ssdi.di.fct.unl.pt/flcp

Page 2: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 2

Propositional Logic

Logic Programming is based on logic.

A main objective of logic is to formalise certain forms of reasoning, namely to allow the inference of a certain proposition (conclusion) from other propositions (Theory).

Example: Conclusion C:home logically follows from theory T, where T:{P1, P2, P3} and

P1: sunny cloudy rainy, P2: rainy homeP3: sunny cloudy

Symbolically, T |= C

Formal Logic achieves this by means of inference rules. Many systems exist but an important one is based on a single inference rule: resolution.

Page 3: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 3

Propositional Logic - Resolution

Resolution works with sets of clauses (disjunctions of literals)

P1: sunny cloudy rainy P2: rainy home ( rainy home) P3a: sunny P3b: cloudy

A single inference rule (Resolution): A P P BA B

Inferences correspond to refutations

T |== C T {C} |==

Refutation

{P1, P2, P3a, P3b, C } |==

( sunny cloudy)

Page 4: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 4

Propositional Logic – (Linear) Resolution

Example of a Refutation: {P1, P2, P3a, P3b, C } |==

There are other forms of organising the resolution of clauses. An important one is arranging them in a linear sequence, starting with the conclusion (negated).

rainy

(sunny cloudy rainy) sunny

cloudy (cloudy rainy)(rainy home) home

rainy

(sun cloudy rainy)

sun

cloudy

(sun cloudy)

(rainy home)home

rainy

cloudy

Page 5: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 5

Propositional Logic - Definite Clauses

Proving that a set of general clauses is contradictory is an NP-complete problem, for which there are no known algorithms that execute in polinomial time.

However, if clauses are definite, their refutation becomes polinomial in the number of clauses.

Definite clauses, also known as Horn clauses, are those where at most one literal may be positive.

Definite clauses come in two “flavours”: Facts and Rules.

A fact is a clause without negative literal. A rule is a clause with negative literals that can be written in an if-then format.

For example:

Fact: sunny Rule: rainy busy home

home ← rainy, busy

Page 6: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 6

Resolution with Definite Clauses

A linear deduction through resolution can be presented as a mechanism for answering queries, in theories expressed in definite clauses.

Example: Answering Query Q: happy from Theory

P1: happy ← summer, warm P2: warm ← sunny

P3: sunny P4: summer

P1: happy ← summer, warm

P2: warm ← sunny

P3: sunny

P4: summer

Q0: ← happy

Q1: ← summer, warm Q0, P1

Q2: ← warm Q1, P4

Q3: ← sunny Q2, P2

Q4: ← □ Q3, P3

P1: happy summer warm

P2: warm sunny

P3: sunny

P4: summer

Q0: happy

Q1: summer warm Q0, P1

Q2: warm Q1, P4

Q3: sunny Q2, P2

Q4: Q3, P3

Page 7: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 7

Deduction and Logic Inference

The previous slides illustrate the process of deduction,

Reasoning aims at inferring propositions from those of the theory, by means of the rules of an inference (deduction) system.

A proposition C is deduced from a theory T in a system S, if it can be inferred from the propositions of T by repeated application of the inference rules of S.

Symbolically: T |-- C

In alternative, the focus may be on the the truth values of propositions.

Reasoning aims at finding logical consequences of the theory.

A proposition C is a logical consequence of a theory T, if whenever all the propositions of the theory are true so is C.

Symbolically: T |== C

In general, it is important that these two approaches are equivalent, for a given logic and inference system S. This is guaranteed by means of soundness and completeness theorems.

This is the case with propositional logic and the resolution system.

Page 8: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 8

Soundness and Completeness of Propositional Logic

Theorem: Resolution is sound and complete for propositional logic.

Soundness: Any proposition that is inferred from a theory by means of resolution is a logical consequence of the theory.

Simbolically: (T |-- C) (T |== C)

Completeness: Any proposition that is a logical consequence of a theory can be inferred from it by means of resolution on propositions of the theory.

Simbolically: (T |== C) (T |-- C)

Note 1: Resolution is not the only inferrence system for propositional logic, but is the one where logic programming is based on.

Note 2: Similar results are obtained for First-Order Logic (FOL) restricted to definite clauses (full FOL is only semi-decidable).

Page 9: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 9

First Order Logic (Horn Clauses)

In first order logic, literals may have “arguments” (variables, constants or more complex terms).

Example:P1a: connect(X,Y) ← direct(X,Y). P1b: connect(X,Y) ← direct(X,Z), connect(Z,Y)P2: direct(lisbon, london).P3: direct(london, sidney).

Deduction is now more complex, since variables (that are assumed to be universally quiantified) can now be substituted for other terms.

Q0: ← connect(lisbon,sidney)

Q1: ← direct(lisbon,Z),connect(Z,sidney) {X/lisbon, Y/sidney}

Q2: ← connect(london,sidney) {Z/london}

Q3: ← direct(london,sidney) {X1/london, Y1/sidney}Q4: ← □

Page 10: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 10

First Order Logic (Horn Clauses)

P1a: connect(X,Y) ← direct(X,Y). P1b: connect(X,Y) ← direct(X,Z), connect(Z,Y)P2: direct(lisbon, london).P3: direct(london, sidney).

Queries may also be more “informative than Yes/No, if they include variables.

Q0: ← connect(W,U)Q1: ← direct(X,Z),connect(Z,Y) {W/X, U/Y}Q2: ← connect(london,Y) {X/lisbon, Z/london}Q3: ← direct(london,Y) {X1/london, Y1/Y}Q4: ← □ {Y/sidney}

In this case, the answer is not only Yes, but it also allows the naming of the cities that are connected, by inspection of the substitutions that are performed in the deduction, and their composition.

{W/X, U/Y}{X/lisbon, Z/london} = {W/lisbon, U/Y, ...} {W/lisbon, U/Y, ...} {Y/sidney} = {W/lisbon, U/Sidney, ...}

i.e. Lisbon (W) is connected to Sidney (U).

Page 11: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 11

Complex Terms

The fragment of logic that only admits as arguments constants and variables (known as DATALOG) although convenient for its “simplicity” (given a finite set of constants, there is only a finite set of literals), but is also quite limited.

More expressive literals use functions as arguments. These more complex terms are also subject to substitutions.

In the previous theory, the atom connect can be augmented with a third argument, representing the whole path between the connected points.

P1a: connect(X,Y,p(Y,Z)) ← direct(X,Y). P1b: connect(X,Y,p(X,P)) ← direct(X,Z), connect(Z,Y,P)P2: direct(lisbon, london).P3: direct(london, sidney).

This new theory allows the answer not only to include initial and final airports, but also the complete path between them.

Page 12: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 12

First Order Logic (Horn Clauses)

P1a: connect(X,Y,p(X,Y)) ← direct(X,Y). P1b: connect(X,Y,p(X,P)) ← direct(X,Z), connect(Z,Y,P)P2: direct(lisbon, london).P3: direct(london, sidney).

The following query reports the whole path in the answer.

Q0: ← connect(W,U,Q)

Q1: ← direct(X,Z),connect(Z,Y,P) {W/X, U/Y, Q/p(X,P)}

Q2: ← connect(london,Y, P) {X/lisbon, Z/london}

Q3: ← direct(london,Y) {X1/london, Y1/Y, P/p(london,Y)}

Q4: ← □ {Y/sidney}

The path Q can now be obtained via the composition of the various substitutions.

{W/X, U/Y, Q/p(X,P)}{X/lisbon, Z/london} =

{W/lisbon, U/Y, Q/p(lisbon,P), ...}

{W/lisbon, U/Y, Q/p(lisbon,P),...} {X1/london, Y1/Y, P/p(london,Y)} =

{W/lisbon, U/Y, Q/p(lisbon,p(london,Y)),...}

{W/lisbon, U/Y, Q/p(lisbon,p(london,Y)),...} {Y/sidney} ={W/lisbon, U/sidney, Q/p(lisbon,p(london,sidney)),...}

Page 13: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 13

Logic and Logic Programming

Logic Programming is based in logic, and in particular in the Resolution System. In fact, the theories addressed before, are also logic programs.

% A simple logic programconnect(X,Y,p(X,Y)):- direct(X,Y). connect(X,Y,p(X,P)):- direct(X,Z), connect(Z,Y,P).direct(lisbon, london).direct(london, sidney).

Logic Programming is declarative. A program specifies the logical relationships between different literals, and not the way in which they are computed and manipulated during the execution of the program.

In principle, the programmer does not have to be concerned with the execution of the program. All that is required from her/him is to guarantee that the facts and rules correctly model her/his problem.

The language “executor” computes the answers to the queries, according to some specific policy of applying the resolution inference rule.

?- connect(A,B,Path).

A = lisbon, B = sidney, Path = p(lisbon,p(london,sidney)

Page 14: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 14

Functions and Data Structures: Lists

Functional terms are the way in which complex data structures may be used in logic programming

An important data structure is the List, for which special syntax exist.

Example of a List: [A, B, C, D]

Internal representation of the list:

•(A, •(B, •(C, •(D, nil)))) where • is the functor.

Special Syntax The list •(Head,Tail) is represented as [Head|Tail] The nil list is represented as [].

Example:

connect(X,Y,[X , Y]):- direct(X,Y).

connect(X,Y,[X | P]):- direct(X,Z), connect(Z,Y,P).

A

B

C

D nil

Page 15: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 15

Declarative Languages

Logic Programming is an example of a declarative programming paradigm that should be contrasted with the more conventional imperative programming paradigm.

Imperative Programming Languages Declaration part defines possible states (of variables) Statement part defines transformation on states Close to von Neumann architecture (states memory) Description on how results are computed Example Languages: Pascal, C

Declarative Programming Languages Abstraction from states and state transformation Direct formulation of mathematical objects (functions, relations, constraints) Description of what is computed Example Languages: Prolog, Haskell, Curry, SICStus Prolog, Eclipse

Page 16: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 16

Declarative Languages

Logic Programming is one of several declarative programming paradigms. Other paradigms and languages exist with similar, but distinct characteristics.

Logic Programming Languages Example: Prolog

Functional Programming Languages Example: Haskell

Integrated (Functional-Logic) Programming Languages Example: Curry, Mercury

Constraint Logic Programming Languages Example: SICStus Prolog, Eclipse

Page 17: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 17

Advantages of Declarative Programming

Specifications are programs. In fact, programs are executable specifications.

The computation mechanism is not part of the language. Declarative languages require such a (hidden) executor.

Thinking declaratively (what are things) is easier than thinking procedurally (how things are built).

Declarative Programs are therefore much simpler to understand, develop and prove (correct).

The output of a logic program is a logical consequence of the program.

Logic Programs are flexible. Arguments can be used in a variety of “modes” (input/output/mixed)

Page 18: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 18

Flexibility of Logic Programming

The program below defines whether X belongs to a list L.

% predicate member

member(X,[X|_]). % X is the head of the list

member(X,[_|L]):- member(X,L).% or X is member of its tail

Flexibility and expressivity is illustrated by the following queries

?- member(1, [1,2,3,4])yes

?- member(5, [1,2,3,4])no

?- member(X, [1,2,3,4])X = 1?; X = 2?; X = 3?; X = 4?; no.

?- member(1, L).L = [1|_]?; X = [_,1|_]?; ...

Page 19: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 19

Flexibility of Logic Programming

This flexibility is quite important to use and reuse definitions in different contexts.

Example: Predicate member_both, is valid when X is both a member of lists L1 and L2.

% predicate member_both

member_both(X,L1,L2):-

member(X,L1), % X is member of L1

member(X,L2), % and X is a member of L2

Once defined this new predicate, it can be used in different queries.

?- member_both(4, [1,2,3,4], [5,6,7,8]) no

?- member_both(X, [1,2,3,4], [2,3,6,7]) X = 2?; X = 3?; no.

Notice that in this last query, X is an “output” argument in the first “call” to member, but is an “input” argument in the second “call”.

Page 20: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 20

Comparison with Imperative Programming

Procedures can be defined in imperative languages to achieve similar effects. However, they are much more cumbersome to program.

The following PASCAL procedure returns, in an array c, the common elements of arrays a and b.

type list = array[1..n] of integer;procedure member_both(a,b: list; var c: list); var i,j,k: integer; begin k := 1; for i := 1 to n do for j := 1 to n do if a[i] = b[j] then

begin c[k] := a[i]; k := k + 1 end;

end

However, checking whether an integer belongs to arrays a and b requires inspection of the returned array c !

Page 21: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 21

Logic Programming Shortcomings: Termination

As a programming language, (an executor of) Prolog has “built in” a strategy to search among the possible deductions.

Because there is no universally optimal strategy, that used in Prolog may cause problems, namely in program termination. This can be illustrated by a small modification of the previous program

P1a: connect(X,Y):- direct(X,Y). P1b: connect(X,Y):- direct(X,Z), connect(Z,Y).P2: direct(lisbon, london).P3: direct(london, sidney).

If the clause P1a and P1b are swapped and rewritten as shown below

P1b: connect(X,Y):- connect(Z,Y), direct(X,Z).P1a: connect(X,Y):- direct(X,Y).

the query ?- connect (X,Y) would not terminate.

Nor would, the more strict query ?- connect(X,sidney).

Page 22: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 22

Prolog Handling of Termination: Declarativeness?

Provisions were made in the design of the Prolog language to circumvent some of the problems with termination.

The introduction of the cut (!), is one of the most used of such constructs. The problem is illustrated with the “more efficient” definition of member.

% predicate member (modified)

member(X,[X|_]):- !. % if X is the head of the list

member(X,[_|L]):- member(X,L). % do not search in its tail

In this definition once X is found in the head of the list, it is no more searched in the tail of the list. However, the declarativeness of logic programming is lost, since this only works if X is an “input” argument.

?- member(3, [1,2,3,4])yes

?- member(5, [1,2,3,4])no

?- member(X, [1,2,3,4])X = 1?; no.

Page 23: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 23

Logic Programming Shortcomings: Arithmetics?

Other pitfalls of Logic Programming are related to the handling of arithmetics.

In Logic Programming, all arguments are considered terms of the Herbrand universe, with no implicit semantics.

Integer operations can be defined in such universe with a base element (0) and a sucessor function s/1. For example, the set of integers may be defined with predicate

% predicate is_integer/1is_integer(0).is_integer(s(X)) :- is_integer(X)

The sum of integers may also be defined with with predicate is_sum/3% predicate sum/3

sum(0,X,X).sum(s(X),Y,s(Z)) :- sum(X,Y,Z)

Other operations could be defined similarly% predicate is_prod/3

prod(0,X,0).prod(s(X),Y, Z):-

prod(X,Y,W), sum(Y,W,Z).

Page 24: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 24

Logic Programming Shortcomings: Arithmetics?

This logic handling of arithmentics is declarative. For example, operations are reversible

?- sum_2(X, s(0), s(s(s(0)))) % ? X+1 = 3X = s(s(0)); no. % X = 2

?- sum_2(X, Y, s(s(s(0)))) % ? X+Y = 3X = 0, Y = s(s(s(0))); % X = 0; Y = 3;

X = s(0); Y = s(s(0)); % X = 1; Y = 2; X = s(s(0)); Y = s(0); % X = 2; Y = 1; X = s(s(s(0))); Y = 0; no. % X = 3; Y = 0;

However, such pure approach is VERY inefficient (and quite cumbersome). Representing “number” N requires a term of size O(N) Suming M with N requires O(M) “calls” of predicate is_sum Multiplying M with N requires M “calls” to predicate sum with N as first argument,

i.e. O(M*N) calls of predicate sum

Lack of typing is also a potential problem: sum(s(0),a, s(a)) ?

Page 25: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 25

Prolog Handling of Arithmetics: Declarativeness?

Prolog assumes some implicit typing for some special terms (integers) to “bypass” such problems .

Hence the sum of two integers (teturning an integer) may be defined by the more “realistic” predicate is_sum_2, using the built_in predicate is/2.

% predicate sum_2/3sum_2(X,Y,Z) :- Z is X+Y.

Such “fix” destroys somehow the declarativeness of the language. The predicate works as expected, when both X and Y are provided as “input”

?- sum_2(3, 5, 8)yes

?- sum_2(3, 5, X)X = 8?; no.

but Prolog is not able to handle inverse operations (it generates an error message)

?- sum_2(3, X, 8). ????

?- sum_2(X, Y, 8). ????

Page 26: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 26

Constraints and Logic Programming

Declarativeness may be maintained in “logic programmes” if some variables have certain “types”, i.e. range over certain domains, and the algebra of such domains is considered in the executor.

Assume for example predicates defined over rational numbers and take predicates

% predicates over rationals {} lin1(X,Y):- {2*X + 3*Y = 7}. lin2(X,Y):- {3*X – 2*Y = 4}.

and take query

?- lin1(X,Y), lin2(X,Y).

Although pure Prolog would not be able to handle such query, constraint logic programming, an extension of logic programming for particular domains, is able to handle this query and report the correct answer

X = 2; Y = 1; no.

Other important domains (finite, sets, ...) are usually considered in the CLP(X) scheme.

Page 27: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 27

Plan of the Course

- Foundations of Logic Programming

- Logic Programming by example

- Unification

- SLD Derivations

- Interpretations and Declarative Semantics of LP

- Search. Execution Trees and Operational Semantics of LP

- Negation (by failure) in Logic Programming

- Foundations of Constraint Logic Programming

- Constraints: Modelling examples

- LP and CLP – Rewritting systems and Operational Semanbtics

- Boolean Unification: CLP(B)

- Algebra of Linear Constraints over Rationals: CLP(Q)

- Finite Domains: Modelling, Consistency maintenance (general)

- Finite Domains: Global constraints and specialised consistency maintenance.

Page 28: 9 October 2006 Foundations of Logic and Constraint Programming 1 Foundations of Logic and Constraint Programming Lecturer: Pedro Barahona Web page: .

9 October 2006 Foundations of Logic and Constraint Programming 28

Bibliography

- Logic Programming

- Text Books:

- Krzysztof Apt, From Logic Programming to Prolog, Prentic Hall, 1997

- Leon Sterling and Ehud Shapiro, The Art of Prolog, MIT Press, 1986

- (Logic) Constraint Programming

- Text Books:

- Dina Rechter, Constraint Processing, Morgan Kauffman, 2003

- Krzysztof Apt, Principles of Constraint Programming, Cambridge University Press, 2003

- K. Marriot and P. Stuckey, Programming with Constraints – An Introduction, MIT Press, 1998

- Web Page

- http://ssdi.di.fct.unl.pt/mcl/flcp