1 Logic Programming Logic Programming And Prolog And Prolog MacLennan - Chapter 13 MacLennan - Chapter 13 ECE Department of Tehran University ECE Department of Tehran University Programming Language Design Course Programming Language Design Course Student Lecture Student Lecture Sadegh Dorri Nogourani Sadegh Dorri Nogourani <[email protected]> <[email protected]>
51
Embed
1 Logic Programming And Prolog MacLennan - Chapter 13 ECE Department of Tehran University Programming Language Design Course Student Lecture Sadegh Dorri.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Logic ProgrammingLogic Programming
And PrologAnd Prolog
MacLennan - Chapter 13MacLennan - Chapter 13
ECE Department of Tehran UniversityECE Department of Tehran UniversityProgramming Language Design CourseProgramming Language Design CourseStudent LectureStudent Lecture
Sorting procedurally:Sorting procedurally:1. Find the min in the remained numbers.2. Swap it with the first number.3. Repeat steps 1,2 until no number remains.
Sorting nonprocedurally:Sorting nonprocedurally:1. B is a sorting of A ↔ B is a permutation of A and B is
ordered.2. B is ordered ↔ for each i<j: B[i] ≤ B[j]
Which is Which is higher levelhigher level??
4ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
A.T.P:A.T.P: Developing programs that can construct formal proofs Developing programs that can construct formal proofs of propositions stated in a symbolic language.of propositions stated in a symbolic language.
ConstructConstruct the desired result to prove its existence (most the desired result to prove its existence (most A.T.P.’s).A.T.P.’s).
In In Logic ProgrammingLogic Programming, , programs are expressed in the form of programs are expressed in the form of propositions and the theorem prover constructs the result(s).propositions and the theorem prover constructs the result(s).
J. A. Robinson: A program is a theory (in some logic) and J. A. Robinson: A program is a theory (in some logic) and computation is deduction from the theory.computation is deduction from the theory.
5ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Programming In Logic (Prolog)Programming In Logic (Prolog)
Developed in Developed in Groupe d’Intelligence ArtificielleGroupe d’Intelligence Artificielle (GIA)(GIA) of the University of Marseilles (early 70s) to process of the University of Marseilles (early 70s) to process a natural language (French).a natural language (French).
Interpreters: Algol-W (72), FORTRAN (73), Pascal Interpreters: Algol-W (72), FORTRAN (73), Pascal (76), Implemented on many platforms (Now)(76), Implemented on many platforms (Now)
Application in AI since mid-70sApplication in AI since mid-70sSuccessor to LISP for AI appsSuccessor to LISP for AI apps
Not standardized (but has ISO standard now)Not standardized (but has ISO standard now)
?- ?- sibling(A,B).sibling(A,B).A = jeffrey, B = georgeA = jeffrey, B = george ; ; A = george, B = jeffreyA = george, B = jeffrey ; ; A = cindy, B = victorA = cindy, B = victor ; ; A = victor, B = cindyA = victor, B = cindy ; ; NoNo
SWI Prolog
9ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
ClausesClauses
Programs are constructed from A number of Programs are constructed from A number of clausesclauses: <head> : <head> :-:- <body> <body>Clauses have three forms:Clauses have three forms: hypotheses (facts) conditions (rules) goals
Both <headBoth <head>> and <body> are composed of and <body> are composed of relationshipsrelationships (also called (also called predicationspredications or or literalsliterals))
assertions (database)
questions
10ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
RelationshipsRelationships
Represent properties of and relations among the Represent properties of and relations among the individualsindividualsA relationship is application of a A relationship is application of a predicatepredicate to one or to one or more more termstermsTerms:Terms: atoms (or constants): john, 25, … variables (begin with uppercase letters): X, … compounds
Horn clause formHorn clause form: : At most one relationship in At most one relationship in <head><head>
11ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Compound TermsCompound Terms
It is It is moremore convenient to describe individuals without convenient to describe individuals without giving them names (giving them names (expressionsexpressions or or compoundscompounds as as terms).terms).using using functors functors (tags):(tags):d(X, plus(U,V), plus(DU,DV)) :- d(X,U,DU), d(X,V,DV).
or using or using infix functorsinfix functors::d(X, U+V, DU+DV) :- d(X,U,DU), d(X,V,DV).
Very inefficient! (Why such a decision?)Very inefficient! (Why such a decision?)Use of Use of ‘is’‘is’ operator (unidirectional) operator (unidirectional)
15ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
PrinciplesPrinciples
SimplicitySimplicity Small number of built-in data types and operations
RegularityRegularity Uniform treatment of all data types as predicates
and terms
16ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Data StructuresData Structures
Compound termsCompound terms can represent data structures can represent data structures
Example: Example: ListsLists in LISP in LISP
(car (cons X L)) = X(cdr (cons X L)) = L(cons (car L) (cdr L)) = L, for nonnull L
17ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
How to accomplish (car (cons ‘(a b) ‘(c d)))?How to accomplish (car (cons ‘(a b) ‘(c d)))?
18ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Some Syntactic SugarSome Syntactic Sugar
Using Using ‘.’‘.’ infix functor (in some systems) infix functor (in some systems) instead of cons:instead of cons: Clauses?
Most Prolog systems allow the abbreviation:Most Prolog systems allow the abbreviation: [X1, X2, …, Xn] = X1. X2. … .Xn.nil
[ ] = nil ‘.’ is right associative!
19ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Component SelectionComponent Selection
Implicitly done by Implicitly done by pattern matchingpattern matching ( (unificationunification).).append( [ ], L, L).append( X.P, L, X.Q) :- append(P,L,Q).
Compare with LISP append:Compare with LISP append:(defun append (M L) (if (null M) L (cons (car M) (append (cdr M) L)) ))
Taking apartTaking apart in terms of in terms of putting togetherputting together!! What X and P are cons’d to create M? What number do I add to 3 to get 5 (instead of 5-3)
Efficient!?Efficient!?
20ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Complex StructuresComplex Structures
A tree using lists (in LISP): A tree using lists (in LISP): (times (plus x y) (plus y 1))
22ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Why Not Predicates? (cont.)Why Not Predicates? (cont.)
Waste use of intermediate (temporary) Waste use of intermediate (temporary) variablesvariablesLess readabilityLess readabilityUnexpected answers!Unexpected answers!sum(x,1,z).:- d(x,z,D).No Why? What did you expect? How to correct it?
23ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Closed World ModelClosed World Model
AllAll that is true is what can be that is true is what can be provedproved on the basis of the facts and on the basis of the facts and rules in the database.rules in the database.
Very reasonable in Very reasonable in object-orientedobject-oriented apps (modeling a real or apps (modeling a real or imagined world)imagined world) All existing objects are defined. No object have a given property which cannot be found in db.
Not suitable for Not suitable for mathematical problemsmathematical problems (Why?) (Why?) An object is generally take to exist if its existance doesn’t contradict the
axioms.
PredicatesPredicates are better for OO-relationships, are better for OO-relationships, CompoundsCompounds for for mathematical ones (Why?)mathematical ones (Why?) We cannot assume existance of 1+0 whenever needed.
24ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
An Argument!An Argument!
What’s the answer?What’s the answer?equal(X,X).:- equal(f(Y),Y).?
What’s the What’s the logicallogical meaning? ( meaning? (occurs checkoccurs check))Any Any otherother meaning?meaning?Can it be represented in a Can it be represented in a finite amountfinite amount of of memory?memory?Should we Should we detectdetect it? it?
25
Control StructuresControl Structures
13.413.4
26ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Algorithm = Logic + ControlAlgorithm = Logic + Control
N. Wirth: N. Wirth: Program = data structure + algorithmProgram = data structure + algorithmR. Kowalski: R. Kowalski: Algorithm = logic + control Algorithm = logic + control
In conventional programming:In conventional programming: Logic of a program is closely related to its control A change in order of statements alters the meaning of program
In (pure) logic programming:In (pure) logic programming: Logic (logic phase) is determined by logical interrelationships of the
clauses not their order. Control (control phase) affects the order in which actions occur in time
and only affects the efficiency of programs.
Orthogonality PrincipleOrthogonality Principle
27ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Top-Down vs. Bottom-Up ControlTop-Down vs. Bottom-Up Control
Top-down ≈ Top-down ≈ RecursionRecursion:: Try to reach the hypotheses
from the goal.
Bottom-up ≈ Bottom-up ≈ IterationIteration:: Try to reach the goal from the
We have seen We have seen logicallogical and and recordrecord (data structure) (data structure) interpretationsinterpretations..
Clauses can also be viewed as Clauses can also be viewed as procedure invocationsprocedure invocations:: <head>: proc. definition <body>: proc. body (a series of proc. calls) Multiple definitions: branches of a conditional (case) fib() example…
Procedure calls can be executed in any order or even Procedure calls can be executed in any order or even concurrently! (pure logic)concurrently! (pure logic)
Input/Output params are not distinguished!Input/Output params are not distinguished! fib(3,3) ↔ true. fib(3,F) ↔ F=3. fib(N,3) ↔ N=3. fib(N,F) ↔ ?
29ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Unify, Fail, Redo…Unify, Fail, Redo…
Heavy use of Heavy use of unificationunification, , backtrackingbacktracking and and recursionrecursion..Unification (Prolog pattern matching – from Unification (Prolog pattern matching – from WikipediaWikipedia):): One-time assignment (binding) uninst. var with atom/term/another uninst. var (aliasing) (occurs check) atom with the same atom compound with compound if top predicates and arities of the terms are
identical and if the parameters can be unified simultaneously We can use ‘=‘ operator to explicitly unify two terms
Backtracking:Backtracking: Make another choice if a choice (unif./match) failes or want to find
other answers. In logic prog. It is the rule rather than the exception. Very expensive!
30ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Prolog’s Control RegimeProlog’s Control Regime
Prolog lang. is Prolog lang. is defineddefined to use to use depth-firstdepth-first search:search: Top to bottom (try the clauses in order of entrance) Left to right In pure logic prog., some complete deductive algorithm such as
Robinson’s resolution algorithm must be implemented.
DFS other than BFSDFS other than BFS Needs much fewer memory Doesn’t work for an infinitely deep tree (responsibility of programmer)
Some programs may fail if clauses and subgoals are not ordered Some programs may fail if clauses and subgoals are not ordered correctly (pp.471-474)correctly (pp.471-474)
33ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Cut! Cut!
‘‘!’!’: Discard choice points of parent frame and frames created : Discard choice points of parent frame and frames created after the parent frame.after the parent frame.
Always is satisfied.Always is satisfied.Used to guarantee termination or control execution order.Used to guarantee termination or control execution order.
i.e. in the goal i.e. in the goal :- p(X,a), !:- p(X,a), ! Only produce the 1st answer to X Probably only one X satisfies p and trying to find another one leads to an
infinite search!
i.e. in the rule i.e. in the rule color(X,red) :- red(X), !.color(X,red) :- red(X), !. Don’t try other choices of red (mentioned above) and color if X satisfies
36ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Higher-Order RulesHigher-Order Rules
Logic programming is limited to first-order logic: Logic programming is limited to first-order logic: can’t bind variables to predicates themselves.can’t bind variables to predicates themselves.
e.g.e.g. redred (f-reduction) is illegal: (p(x,y,z) ↔ z=f(x,y)) (f-reduction) is illegal: (p(x,y,z) ↔ z=f(x,y))red(P,I,[ ],I).red(P,I,X.L,S) :- red(P,I,L,T), P(X,T,S).
But is legal if the latter be defined as:But is legal if the latter be defined as:red(P,I,X.L,S):- red(P,I,L,T), Q=..[P,X,T,S], call(Q).
What’s the difference?
37ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
In LISP, both code and data are In LISP, both code and data are first-orderfirst-order objects, objects, but in Prolog aren’t.but in Prolog aren’t.
Robinson Robinson resolution algorithmresolution algorithm is refutation complete is refutation complete for for first-orderfirst-order predicate logic. predicate logic.
Gödel’s Gödel’s incompleteness theoremincompleteness theorem: No algorithm is : No algorithm is refutation complete for refutation complete for higher-orderhigher-order predicate logic. predicate logic.
So, Prolog So, Prolog indirectlyindirectly supports higher-order rules. supports higher-order rules.
38ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Negative FactsNegative Facts
How to define How to define nonsiblingnonsibling? Logically…? Logically…nonsibling(X,Y) :- X = Y.nonsibling(X,Y) :- mother(M1,X), mother(M2,Y), M1 \= M2.nonsibling(X,Y) :- father(F1,X), father(F2,Y), F1 \= F2.
But if parents of X or Y are not in database?But if parents of X or Y are not in database? What is the answer of nonsibling? Can be solved by…nonsibling(X,Y) :- no_parent(X).nonsibling(X,Y) :- no_parent(Y). How to define no_parent?
39ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Negative Facts (cont.)Negative Facts (cont.)
Problem:Problem: There is no There is no positivepositive fact expressing fact expressing the the absenceabsence of parent. of parent.
Cause: Cause: Horn clauses are limited to C :- P1,P2,…,Pn ≡ C holds if P1^P2^…^Pn hold. No conclusion if P1^P2^…^Pn don’t hold! If, not iff
40ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Cut-failCut-fail
Solutions:Solutions:Stating Stating allall negative facts such as no_parent negative facts such as no_parent Tedious Error-prone Negative facts about sth are usually much more than positive facts
about it
““Cut-fail”Cut-fail” combination combination nonsibling(X,Y) is satisfiable if sibling(X,Y) is not (i.e. sibling(X,Y) is
unsatisfiable) nonsibling(X,Y) :- sibling(X,Y), !, fail. nonsibling(X,Y). how to define ‘fail’ ?!
41ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
46ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Prolog vs. Logic ProgrammingProlog vs. Logic Programming
Definite control strategyDefinite control strategy Programmers make explicit use of it and the result
have little to do with logic Reasoning about the order of events in Prolog is
comparable in difficaulty with most imperative of conventional programming languages
CutCut doesn’t make any sense in logic! doesn’t make any sense in logic!
notnot doesn’t correspond to logical negation doesn’t correspond to logical negation
47ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Improving EfficiencyImproving Efficiency
Prolog is far from an efficient language.Prolog is far from an efficient language.
So, it’s applications are limited to apps in So, it’s applications are limited to apps in which:which: Performance is not important Difficult to implement in a conventional lang.
New methods are inventedNew methods are invented
Some compilers produce code comparable to Some compilers produce code comparable to LISPLISP
48ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
PurePure logic programs prove the possibility of logic programs prove the possibility of nonprocedural programming.nonprocedural programming.InIn PrologProlog, DFS requires programmers to think in , DFS requires programmers to think in terms of terms of operationsoperations and their proper and their proper orderingordering in time in time (procedurally).(procedurally).AndAnd Prolog’s control regime is more Prolog’s control regime is more unnaturalunnatural than than conventional languages.conventional languages.So,So, there is still much more important work to be there is still much more important work to be done before nonprocedural programming becomes done before nonprocedural programming becomes practicalpractical..
49ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Covered Sections of MacLennanCovered Sections of MacLennan
13.113.1
13.213.2
13.313.3
13.413.4 except topics starting on pp. 471, 475, 477, 484,
485, 486, 488
13.513.5
50ECE Dep. of Tehran Univ. - Programming Language DesignECE Dep. of Tehran Univ. - Programming Language Design
Presentation ReferencesPresentation References
Colmerauer, Alain, Philippe Roussel, Colmerauer, Alain, Philippe Roussel, The Birth of Prolog, The Birth of Prolog, Nov. 1992, Nov. 1992, URL: URL: http://www.lim.univ-mrs.fr/~colmer/ArchivesPublications/HistoireProlog/http://www.lim.univ-mrs.fr/~colmer/ArchivesPublications/HistoireProlog/19november92.pdf19november92.pdf
MacLennan, Bruce J., Principles of Programming Languages: Design, MacLennan, Bruce J., Principles of Programming Languages: Design, Evaluation and Implementation,Evaluation and Implementation, 3rd ed, Oxford University Press, 1999 3rd ed, Oxford University Press, 1999
Merritt, Dennis, “Prolog Under the Hood: An Honest Look”Merritt, Dennis, “Prolog Under the Hood: An Honest Look” , , PC AI PC AI magazine, Sep/Oct 1992magazine, Sep/Oct 1992
““Unification”Unification”, Wikipedia, the free encyclopedia, 25 Sep. 2005, URL: , Wikipedia, the free encyclopedia, 25 Sep. 2005, URL: http://en.wikipedia.org/wiki/Unificationhttp://en.wikipedia.org/wiki/Unification