1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic.

Presentation on theme: "1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic."— Presentation transcript:

1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic programming was invented; how it might be useful to you; and what you need to do to start programming.

2 Logic Programming School of Informatics, University of Edinburgh The Ideal Every program specification can be written in logic. Specifications are independent of computers. Rules of logic can prove the correctness of a specification even if computers didn’t exist. But proof can also be done by a computer smart enough to find the right proof. So specifications and programs are the same.  nearly

3 Logic Programming School of Informatics, University of Edinburgh Where Has This Worked? Symbolic reasoning systems (machine learning, planning, constraint solving, etc) Language systems (parsers, generators, etc) Verification systems (theorem provers, model checkers, etc). Multi-agent systems (reactive systems, performative handling, brokering, etc) Web systems (page parsing/generation, semantic webs)

4 Logic Programming School of Informatics, University of Edinburgh Axioms, Assumptions, Goals Your parent is your ancestor. Your parent’s ancestors are your ancestors. ancestor(X, Y)  parent(X, Y). ancestor(X, Y)  parent(X, Z) & ancestor(Z, Y). Given: parent(dave, thomas). parent(dave,helen). parent(helen, margaret), etc. We can prove:  A. ancestor(dave, A)  B. ancestor(B, margaret)  A,B. ancestor(B, A)

5 Logic Programming School of Informatics, University of Edinburgh Proof Involves Search ancestor(X, Y)  parent(X, Y). ancestor(X, Y)  parent(X, Z) & ancestor(Z, Y). parent(dave, thomas). parent(dave,helen). parent(helen, margaret).  A. ancestor(dave, A) parent(dave, Y) parent(dave, Z) & ancestor(Z, Y) parent(dave, helen) parent(helen, Y) parent(helen, margaret) A = Y Z = helen Y = margaret 1 1 2 3 3 4 4 5 5 6 6 parent(dave, thomas) 2 Y = thomas

6 Logic Programming School of Informatics, University of Edinburgh Practical Problem 1 Not all specifications are executable. X =  X 2 is a specification of square root but doesn’t compute square roots. We need to write specifications with an understanding of their future use.

7 Logic Programming School of Informatics, University of Edinburgh Practical Problem 2 Proof mechanisms that are unsophisticated can easily be fooled. e.g. Prolog has trouble with this: Given: p(X)  p(Y) & q(Y,X). p(1). q(1,2). Prove: p(1). Proof mechanisms that are sophisticated are difficult to operate as programming systems.

8 Logic Programming School of Informatics, University of Edinburgh Practical Problem 3 Some computable specifications consume a lot of resource before giving an answer. e.g. Prolog has trouble with this: Given: p  is_integer(X) & X > 100000. is_integer(1). is_integer(N)  is_integer(N1) & N is N + 1. Logic programming system designers worry about efficiency. So must programmers.

9 Logic Programming School of Informatics, University of Edinburgh Practical Problem 4 Programmers like to think about closed worlds so negation often is simplified to “I can’t prove it true in my current circumstances”. e.g. Prolog has trouble with this: Given: p(X)  not(q(X)) & r(X). q(2). r(1). Prove: p(X)

10 Logic Programming School of Informatics, University of Edinburgh A Logic Program’s Interpretation ancestor(X, Y)  parent(X, Y). ancestor(X, Y)  parent(X, Z) & ancestor(Z, Y). parent(dave, thomas). parent(dave,helen). parent(helen, margaret). Interpretation contains all ground conclusions:  A1,B1. ancestor(A1, B1)  A2,B2. parent(A2, B2) ancestor(dave, thomas). ancestor(dave,helen). ancestor(helen, margaret). ancestor(dave,margaret) parent(dave, thomas). parent(dave,helen). parent(helen, margaret).

11 Logic Programming School of Informatics, University of Edinburgh Everything is a Structured Term ancestor(X, Y)  parent(X, Y). ancestor(X, Y)  parent(X, Z) & ancestor(Z, Y). parent(dave, thomas). parent(dave,helen). parent(helen, margaret). parent X Z ancestor Z Y & Z Y  parent dave thomas

12 Logic Programming School of Informatics, University of Edinburgh Logic Programs Manipulate Terms sum(null, 0). sum(seq(First,Rest), N)  sum(Rest, N1) & N is N1 + 1. We can prove:  N. sum(null, N)  N. sum(seq(5,null), N)  N. sum(seq(5,seq(2,null)), N) and so on…. N = 0 N = 5 N = 9

13 Logic Programming School of Informatics, University of Edinburgh Logic Programming Learning Curve Time Learning effort Logic programming Imperative programming A crude caricature: Imperative programmers learn lots of little things Logic programmers learn one big thing

14 Logic Programming School of Informatics, University of Edinburgh Starting to Program in Prolog sicstus | ?- 1 < 2. yes | ?- not(1 < 2). no | ?- [library(lists)]. | ?- append([a,b], [c], X). X = [a,b,c] Unix command to start SICStus Prolog Goal, using a built-in predicate Goal, using a built-in logical operator which succeeds which fails Loading definitions from a (library) file Goal, using a loaded predicate. Result of variable binding

15 Logic Programming School of Informatics, University of Edinburgh Sources l Sterling & Shapiro “The Art of Prolog” l Clocksin & Mellish “Prolog Programming” l John Lloyd “Foundations of Logic Programming” Books: Organisation: Association for Logic Programming Main conference: International Conference on Logic Programming Main journal: Theory and Practice of LP (pre-2001 main journal was Journal of LP )

Download ppt "1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic."

Similar presentations