Prolog. Intro to Logic Programming

Similar documents
Prolog. Logic Programming vs Prolog

Prolog - 3 Prolog search trees + traces

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Logic Languages. Hwansoo Han

Implementação de Linguagens 2016/2017

The current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday.

CS 321 Programming Languages and Compilers. Prolog

Programming Paradigms

CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4. Hojjat Ghaderi, Fall 2006, University of Toronto

n Bottom-up (LR) parsing n Characteristic Finite State Machine (CFSM) n SLR(1) parsing table n Conflicts in SLR(1) n LR parsing variants

3 Lists. List Operations (I)

10. Logic Programming With Prolog

CSC324 Logic & Relational Programming Illustrated in Prolog

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Prolog Programming. Lecture Module 8

Presentation Overview. Programming Language Families. Presentation Overview. Procedural Programming. Procedural Paradigm

Chapter 16. Logic Programming Languages

Recursion, Structures, and Lists

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Chapter 16. Logic Programming Languages ISBN

COP4020 Programming Languages. Logic programming and Prolog Prof. Xin Yuan

Operational Semantics

COP4020 Programming Languages. Logical programming with Prolog Prof. Xin Yuan

Logic Programming. Let us have airline flight information of the form: 1. Application Domains: 2. Definitions

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

Notes for Chapter 12 Logic Programming. The AI War Basic Concepts of Logic Programming Prolog Review questions

Connection between Lists and Terms. Prolog-Lecture 2. Unifying Lists. Unifying Lists. Steps to a Recursive Predicate. List Predicates.

Logic Programming Languages

First-Order Logic (FOL)

An introduction to logic programming with Prolog

Fundamentals of Prolog

Introduction to predicate calculus

COP4020 Programming Languages. Prolog Chris Lacher Based on Robert van Engelen

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Lecture 11: Feb. 10, 2016

Advanced Logic and Functional Programming

Chapter 16. Logic Programming Languages ISBN

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Brief Introduction to Prolog

Logic Programming: The Prolog Language

Module 7. Knowledge Representation and Logic (Rule based Systems) Version 2 CSE IIT, Kharagpur

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

Lecture 16: Logic Programming in Prolog

What is Prolog? - 1. A Prolog Tutorial. Prolog Programming. What is Prolog? - 2. » Declaring some facts about objects and their relationships

Logic Programming Paradigm

Prolog. Prolog: Programmation en Logique R.A. Kowalski: Predicate logic as a programming language, Proc IFIP 1974, pp.

Announcements. The current topic: Prolog. Logic programming. Logic programming. Lab 2 has been marked.

SAT solver of Howe & King as a logic program

Functional Logic Programming. Kristjan Vedel

Logic Programming and Resolution Lecture notes for INF3170/4171

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

ACSC300: Logic Programming

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships

The Prolog to Mercury transition guide

proof through refutation

Exercises on the Fundamentals of Prolog

Arithmetic Terms are Symbolic

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

Part I Logic programming paradigm

IN112 Mathematical Logic

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

INTRODUCTION TO PROLOG

6. Inference and resolution

CS 381: Programming Language Fundamentals

Logical reasoning systems

IN112 Mathematical Logic

Lecture 4: January 12, 2015

Logic Programming. CITS 3242 Programming Paradigms. Topic 16: Part IV: Advanced Topics

Topic B: Backtracking and Lists

COP4020 Programming Languages. Prolog Prof. Robert van Engelen

LOGIC AND DISCRETE MATHEMATICS

Resolution (14A) Young W. Lim 6/14/14

Backtracking. Ch. 5 Controlling Backtracking. Backtracking. Backtracking Example. Example. Example

UNIT 2 A. I. LANGUAGES-2: PROLOG

Announcements. The current topic: Prolog. Logic programming. Logic programming. Lab 2 has been marked.

Software Paradigms (Lesson 6) Logic Programming

List of lectures. Lecture content. Symbolic Programming. TDDA69 Data and Program Structure Symbolic and Logic Programming Cyrille Berger

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

Week 7 Prolog overview

Tests, Backtracking, and Recursion

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

Map coloring example

Concepts of programming languages

For Wednesday. No reading Chapter 9, exercise 9. Must be proper Horn clauses

Chapter 5. Pure PROLOG. Foundations of Logic Programming

Artificial Intelligence

Topic 1: Introduction to Knowledge- Based Systems (KBSs)

Lecture 6: Inductive Logic Programming

MIDTERM EXAM (Solutions)

CSE 452: Programming Languages. Prolog Statements. Loading the Knowledge Base. Logical Programming Languages Part 2

(More) Propositional Logic and an Intro to Predicate Logic. CSCI 3202, Fall 2010

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

The Metalanguage λprolog and Its Implementation

PROLOG PROgramming in LOGic

Programming Paradigms

CAP 5602 Summer, Lesson 4: Loops. The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop

Transcription:

Prolog Logic programming (declarative) Goals and subgoals Prolog Syntax Database example rule order, subgoal order, argument invertibility, backtracking model of execution, negation by failure, variables Data structures (lists, trees) Recursive Functions: append, member Lazy evaluation, terms as trees, Prolog search trees Goal-oriented semantics Prolog-1, CS5314 BG Ryder 1 Intro to Logic Programming Specifies relations between objects larger (2,1), father (tom, jane) Separates control in PL from description of desired outcome father(x, jane) :- male(x), parent(x, jane). Computation engine: theorem proving and recursion Higher-level PL than imperative languages More accessible to non-technical people Prolog-1, CS5314 BG Ryder 2 1

Horn Clauses Conjunct of 0 or more conditions which are atomic formulae in predicate logic (constants, predicates, functions) h 1 h 2 h n c Means c if h 1, h 2,, h n are all true Can have variables in the h i s or c c( x 1,x 2,,x m ) if h(x 1, x 2,,x m,y 1,,y k ) means for all objects x 1, x 2,, x m, c holds if there are objects y 1,,y k such that h holds. father( X, jane) :- male(x), parents(x, Y, jane) Prolog-1, CS5314 BG Ryder 3 Logic Programming Goal-oriented semantics goal is true for those values of variables which make each of the subgoals true father(x, jane) will be true if male(x) and parents(x,y,jane) are true with specific values for X and Y recursively apply this reasoning until reach rules that are facts. called backwards chaining Prolog-1, CS5314 BG Ryder 4 2

Logic Programming Nondeterminism Choice of rule to expand subgoal by Choice of subgoal to explore first father(x,jane):- male(x), parents(x, Y, jane). father (X,jane):- father (X,Y), brother(y, jane). which rule to use first? which subgoal to explore first? Prolog tries rules in sequential order and proves subgoals from left to right. - Deterministic! Prolog-1, CS5314 BG Ryder 5 Victoria Database Program male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert).?- male(albert). yes?- male(alice). false?-female(x). X = alice ; X = victoria. variable predicate constants victoria.pl from Clocksin and Mellish By responding <cr> you quit the query ; <cr> you continue to find another variable binding that makes the query true, if possible. Prolog-1, CS5314 BG Ryder 6 3

Victoria Example Problem: facts alone do not make interesting programs possible. Need variables and deductive rules.?-female(x). a query or proposed fact X = alice ; ; asks for more answers X = victoria. if user types <cr> then no more answers given when no more satisfying answers are left. Variable X has been unified to all possible values that make female(x) true. Performed by pattern match search Prolog-1, CS5314 BG Ryder 7 Prolog Syntax in EBNF 15 jane X <term> <integer> <atom> <variable> <functor> (<term> {, <term>} ) head :- body <rule> <predicate> (<term> {,<term>} ):- <term> {, <term>}. <fact> <fact> <functor> (<term>) {, <term>} ). <query>?- <functor>(<term>{,<term>}). a proposed fact that must be proven Prolog-1, CS5314 BG Ryder 8 4

Prolog Syntax, cont. Prolog program consists of facts, rules, and queries A query is a proposed fact, needing to be proven If query has no variables and is provable, answer is yes If query has variables, the proof process causes some variables to be bound to values which are reported (called a substitution) Variable names are capitalized, predicate names and constants are lower case. Names (e.g., predicates, functors with terms, clauses) come from first order logic. Prolog-1, CS5314 BG Ryder 9 Victoria Example, cont. sister_of(x,y) :- female(x),parents(x,m,f), parents(y,m,f).?- sister_of(alice, Y). Y = edward?- sister_of(x,y). X = alice Y = edward ; X =Y, Y = alice ; false 3. female(alice). 4. female(victoria). 5. parents(edward,victoria,albert). 6. parents(alice,victoria,albert). first answer from 3.+6.+5. second answer from 3.+6.+6. Subgoal order, argument invertibility, backtracking, rule order Prolog-1, CS5314 BG Ryder 10 5

Victoria Example, cont. sis(x,y) :- female(x), parents(x,m,f), parents(y,m,f),\+(x==y).?- sis(x,y). X = alice Y = edward ; false = means unifies with == means same in value \+ (P) succeeds when P fails; called negation by failure Prolog-1, CS5314 BG Ryder 11 Negation by Failure not(x) :- X,!, fail. not( _ ). if X succeeds in first rule, then the goal fails because of the last term. if we type ; the cut (!) will prevent us from backtracking over it or trying the second rule so there is no way to undue the fail. if X fails in the first rule, then the goal fails because subgoal X fails. the system tries the second rule which succeeds, since _ (don t care variable) unifies with anything. Prolog-1, CS5314 BG Ryder 12 6

Lists list head tail [a,b,c] a [b,c] a b [a, [b, c], d] a [ [b,c], d] c [ ] [X Y] X Y a list consists of a sequence of terms a b c [ ] d [ ] Prolog-1, CS5314 BG Ryder 13 Unifying Lists [X,Y,Z] = [john, likes, fish] X = john, Y = likes, Z = fish [cat] = [X Y] X = cat, Y = [ ] [1 2] versus [1, 2] 1 1 2 2 [ ] Prolog-1, CS5314 BG Ryder 14 7

Lists Sequence of elements separated by commas, or [ first element rest_of_list ] Like Scheme notation [ car(list) cdr(list)] [ [the Y] Z] = [ [X, hare] [ is, here] ] the Y Z X is hare [ ] here [ ] Prolog-1, CS5314 BG Ryder 15 Lists [X, abc, Y] =? [ X, abc Y] thre is no value binding for Y, to make these two trees isomorphic. X abc Y [ ] X abc Y Be careful of unifications that mix list syntax modes. Prolog-1, CS5314 BG Ryder 16 8

Lists [a,b Z] =? [ X Y] X = a, Y = [b Z], Z = _ look at the trees to see why this works! [ a, b, c] = [ X Y] X = a, Y = [b,c]. don t care variable unifies with anything Prolog-1, CS5314 BG Ryder 17 Member_of Function member(a, [A B] ). member(a, [B C]) :- member (A, C). goal-oriented semantics: can get value assignment for goal member(a,[b C]) by showing truth of subgoal member(a,c) and retaining value bindings of the variables Prolog-1, CS5314 BG Ryder 18 9

?- member(a,[a,b]). yes?- member(a,[b,c]). false Example?- member(x,[a,b,c]). X = a ; X = b ; X = c ; false Try this last query with trace. Invertibility of Prolog arguments 1. member(a, [A B] ). 2. member(a, [B C]) :- member (A, C). Prolog-1, CS5314 BG Ryder 19 Example?- member(a,[b, c, X]). X= a ; 1. member(a, [A B] ). false 2. member(a, [B C]) :- member (A, C).?- member(x,y). X = _123 Y = [ X _124] ) ; X = _123 Y = [_125, X _126] ; X = _123 Y = [ _127, _128, X _129] Lazy evaluation of a priori unbounded list structure. Unbound X variable is first element, then second element, then third element, in a sequence of generated lists of increasing length. Prolog-1, CS5314 BG Ryder 20 10

Prolog Search Tree X=A=a, B=[b,c]`` member(x,[a,b,c]) X=a success X=A=b, B =[c] X=b success X=A,B=a,C=[b,c] member(x,[b,c]) X=A =c, B =[ ] X=c success X=A,B =b,c =[c] member(x,[c]) X=A B =c, C =[ ] member(a,[ ]) 1. member(a, [A B] ). 2. member(a, [B C]) :- member (A, C). fail fail Prolog-1, CS5314 BG Ryder 21 1. member(a, [A B] ). 2. member(a, [B C]) :- member (A, C).?- member(x, [a,b,c]). match rule 1. member(a, [A B] ) so X = A = a, B = [b,c] X = a ; match rule 2. member(a, [B C]) so X = A, B = a, C = [b,c] then evaluate subgoal member(x, [b,c]) match rule 1. member(a,[a B ]) so X = b, B = [c] X = b ; match rule 2. member(a,[b C ]) so X = A, B = b, C = [c] then evaluate subgoal member(x, [c]) match rule 1. member(a,[a B ]) so X=A = c, B =[ ] X = c ; match rule 2. member(a,[b C ]) so X=A, B =c,c =[ ], but member(x, [ ]) is unsatisfiable, no Prolog-1, CS5314 BG Ryder 22 11

Another Search Tree member(a, [b,c,x]) fail, a can t unify with b B = b, C = [c,x] member(a,[c, X]) B =c, C =[X] Prolog-1, CS5314 BG Ryder fail, a can t unify with c 1. member(a,[a B] ). 2. member(a,[b C]) :- member (A,C). X=a, B = [ ] success member(a,[ X]). fail, can t unify [ ] with a list X=B, C = [ ] member(a,[ ]) fail, ditto 23 Prolog Search Trees A formalism to consider all possible computation paths Leaves are success nodes or failures where computation cannot proceed To model Prolog, leftmost subgoal is tried first Label edges with variable bindings that occur by unification There can be infinite branches in the tree, representing non-terminating computations (performed lazily (i.e., generate as needed) by Prolog); Prolog-1, CS5314 BG Ryder 24 12

Another Member_of Function Equivalent set of rules: mem(a,[a _] ). mem(a,[_ C]) :- mem(a,c). Can examine search tree and see the variables which have been excised were auxiliary variables in the clauses. Prolog-1, CS5314 BG Ryder 25 Append Function append ([ ],A,A). append([a B],C,[A D]):- append(b,c,d). Build a list?- append([a],[b],y). Y = [ a,b ] Break a list into constituent parts?- append(x,[b],[a,b]). X = [ a ]?- append([a],y,[a,b]). Y = [ b ] Prolog-1, CS5314 BG Ryder 26 13

More Append?- append(x, Y, [a,b]). X = [ ] Y = [ a, b ] ; X = [a] Y = [b] ; X = [a,b] Y = [ ] ; false append ([ ],A,A). append([a B],C,[A D]):- append(b,c,d). Prolog-1, CS5314 BG Ryder 27 Still More Append Generating an unbounded number of lists?- append(x, [b], Y). X = [ ] Y = [b] ; X = [ _169] Y = [ _169, b] ; X = [ _169, _170 ] Y = [ _169, _170, b] ; etc. append ([ ],A,A). append([a B],C,[A D]):- append(b,c,d). Prolog-1, CS5314 BG Ryder 28 14

Common Beginner s Errors Compile-time Forget ending. Misspelled functors Need to override precedences with (..) Runtime Infinite loops - check your recursion Variables instantiated with unexpected values Circular definitions Giving wrong numbers of arguments to a clause Prolog-1, CS5314 BG Ryder 29 15