Prolog. Logic Programming vs Prolog

Similar documents
Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Prolog. Intro to Logic Programming

Logic Languages. Hwansoo Han

Programming Paradigms

n Bottom-up (LR) parsing n Handle n LR item n Characteristic Finite State Machine (CFSM) n SLR(1) parsing tables n Conflicts in SLR(1)

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

CS 321 Programming Languages and Compilers. Prolog

Chapter 16. Logic Programming Languages

Prolog Programming. Lecture Module 8

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

Chapter 16. Logic Programming Languages ISBN

Introduction to predicate calculus

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

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

Chapter 16. Logic Programming Languages ISBN

Logic Programming Languages

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

Constraint Solving. Systems and Internet Infrastructure Security

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

Fundamentals of Prolog

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

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

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

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

CSC324 Logic & Relational Programming Illustrated in Prolog

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Automated Reasoning PROLOG and Automated Reasoning 13.4 Further Issues in Automated Reasoning 13.5 Epilogue and References 13.

ACSC300: Logic Programming

Advanced Logic and Functional Programming

COMP4418 Knowledge Representation and Reasoning

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

Implementação de Linguagens 2016/2017

INTRODUCTION TO PROLOG

First-Order Logic (FOL)

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

Week 7 Prolog overview

Recursion, Structures, and Lists

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

6.034 Notes: Section 11.1

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics

Operational Semantics

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

DATABASE THEORY. Lecture 11: Introduction to Datalog. TU Dresden, 12th June Markus Krötzsch Knowledge-Based Systems

An introduction to logic programming with Prolog

Logic (or Declarative) Programming Foundations: Prolog. Overview [1]

Logic Programming and Resolution Lecture notes for INF3170/4171

Predicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth

proof through refutation

Chapter 2 & 3: Representations & Reasoning Systems (2.2)

Topic B: Backtracking and Lists

Last Week. Today. Prolog: recursion examples. Recursion in Prolog. CSC326 Programming Languages (Week 12,Friday)

Functional Logic Programming. Kristjan Vedel

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

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

Data Integration: Logic Query Languages

Logical reasoning systems

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

will take you everywhere.

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

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

The Prolog to Mercury transition guide

Prolog - 3 Prolog search trees + traces

Q1:a. Best first search algorithm:

10. Logic Programming With Prolog

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.

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

LOGIC AND DISCRETE MATHEMATICS

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

Lecture 17 of 41. Clausal (Conjunctive Normal) Form and Resolution Techniques

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

6. Inference and resolution

Introduction to Logic Programming in Prolog 1 / 39

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

First-Order Predicate Logic. CSCI 5582, Fall 2007

CS 381: Programming Language Fundamentals

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

Prolog 2. Unification and Recursion

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

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

UNIT 2 A. I. LANGUAGES-2: PROLOG

zebra puzzle continued recap Prolog concepts family relations

Logic Programming: The Prolog Language

Tests, Backtracking, and Recursion

Brief Introduction to Prolog

Logic Programming in Prolog

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

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

Lecture 4: January 12, 2015

PROLOG PROgramming in LOGic

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Lecture 6: Inductive Logic Programming

Deduction Rule System vs Production Rule System. Prof. Bob Berwick. Rules Rule

Artificial Intelligence

Prolog. (Programming in Logic) Prepared by: Tanay Kumar Saha

Part I Logic programming paradigm

6.034 Artificial Intelligence, Fall 2006 Prof. Patrick H. Winston. Problem Set 1

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

Declarative Programming Prolog CS360

Transcription:

Language constructs Prolog Facts, rules, queries through examples Horn clauses Goal-oriented semantics Procedural semantics How computation is performed? Comparison to logic programming 1 Logic Programming vs Prolog Logic programming languages are t procedural or functional. Specify relations between objects larger(3,2) father(tom,jane) Separate logic from control: Separate the What (logic) from the How (control) Programmer declares what facts and relations are true father(x,jane):- male(x),parent(x,jane). System determines how to use facts to solve problems State relationships and query them as in logic 2 1

Logic Programming vs Prolog Computation engine: theorem-proving and recursion Uses unification, resolution, backward chaining, backtracking Problem description is higher-level than imperative languages 3 Prolog As database management Program is a database of facts Simple queries with constants and variables ( binding ), conjunctions and disjunctions Rules to derive additional facts Two interpretations Declarative: related to logic Procedural: searching for answers to queries Search trees and rule firings can be traced 4 2

Facts likes(eve, pie). likes(al, eve). likes(eve, tom). likes(eve, eve). food(pie). food(apple). person(tom). predicates constants 5 Queries (Asking Questions) likes(eve, pie). likes(al, eve). likes(eve, tom). likes(eve, eve). food(pie). food(apple). person(tom). variable?-likes(al,eve).?-likes(al,who). yes query Who=eve?-likes(al, pie)?-likes(eve,w). answer W=pie ;?-likes(eve,al). W=tom ; W=eve ;?-likes(person,food). answer with variable binding force search for more answers 6 3

Harder Queries likes(eve, pie). likes(al, eve). likes(eve, tom). likes(eve, eve). food(pie). food(apple). person(tom).?-likes(a,b). A=eve,B=pie ; A=al,B=eve ;?-likes(d,d). D=eve ;?-likes(eve,w), person(w). W=tom?-likes(al,V), likes(eve,v). V=eve ; and 7 Harder Queries likes(eve, pie). likes(al, eve). likes(eve, tom). likes(eve, eve). food(pie). food(apple). person(tom). same binding?-likes(eve,w),likes(w,v). W=eve,V=pie ; W=eve,V=tom ; W=eve,V=eve?-likes(eve,W),person(W),food(V). W=tom,V=pie ; W=tom,V=apple or?-likes(eve,v),(person(v);food(v)). V=pie ; V=tom ;?-likes(eve,w),\+likes(al,w). W=pie ; W=tom ; t 8 4

Rules likes(eve, pie). likes(al, eve). likes(eve, tom). likes(eve, eve). food(pie). food(apple). person(tom). What if you want to ask the same question often? Add a rule to the database: rule1:-likes(eve,v),person(v).?-rule1. yes 9 Rules likes(eve, pie). food(pie). likes(al, eve). food(apple). likes(eve, tom). person(tom). likes(eve, eve). rule1:-likes(eve,v),person(v). rule2(v):-likes(eve,v),person(v).?-rule2(h). H=tom ;?-rule2(pie). Note rule1 and rule2 are just like any other predicate! 10 5

Queen Victoria Example male(albert). a fact female(alice). Facts are put in a file. male(edward). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert).?- [family]. loads file yes?- male(albert). a query yes?- male(alice).?- parents(edward,victoria,albert). yes?- parents(bullwinkle,victoria,albert). cf Clocksin and Mellish 11 Queen Victoria Example, cont. Problem: facts alone do t 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 <return> then more answers given when more answers left, return Variable X has been unified to all possible values that make female(x) true. Performed by pattern match search Variables capitalized, predicates and constants are lower case 12 6

Queen Victoria Example, cont.?-sister_of(x,y):- female(x),parents(x,m,f),parents(y,m,f).?- sister_of(alice,y). a rule Y = edward?- sister_of(alice, victoria). Prolog program consists of facts, rules, and queries A query is a proposed fact, needing to be proven If query has variables and is provable, answer is yes If query has variables, proof process causes some variables to be bound to values which are reported (called a substitution) 13 Horn Clauses A Horn Clause is: c h 1^ h 2 ^ h 3 ^ ^h n Antecedents(h s): conjunction of zero or more conditions which are atomic formulae in predicate logic Consequent(c): an atomic formula in predicate logic Meaning of a Horn clause: The consequent is true if the antecedents are all true c is true if h 1, h 2, h 3,..., and h n are all true sister_of(x,y):- female(x),parents(x,m,f),parents(y,m,f). X is the sister of Y, if X is female, X s parents are M and F, and Y s parents are M and F. 14 7

Horn Clauses In Prolog, a Horn clause c h 1 ^ ^h n is written c :- h 1,..., h n. Horn Clause is a Clause Consequent is a Goal or a Head Antecedents are Subgoals or Tail Horn Clause with No Tail is a Fact male(edward). dependent on other conditions Horn Clause with Tail is a Rule father(albert,edward) :- male(edward),parents(edward,m,albert). 15 Horn Clauses Variables may appear in the antecedents and consequent of a Horn clause: c(x 1,...,X n ) :- h(x 1,...,X m,y 1,...,Y k ). For all values of X 1,...,X n, the formula c(x 1,...,X n ) is true if there exist values of Y 1,...,Y k such that the formula h(x 1,...,X n,y 1,...,Y k ) is true Call Y i an auxiliary variable. Its value will be bound to make consequent true, but t reported by Prolog, because it doesn t appear in the consequent. 16 8

Declarative Semantics father(x,jane) :- male(x),parents(jane,y,x). Scope of X is this rule Y is an auxiliary variable, X is a variable jane is a constant Goal-oriented (declarative) semantics: father(x,jane) is true for those values of X which make subgoals male(x) and parents(jane,y,x) true. Recursively apply this reasoning until reach rules that are facts; called backwards chaining 17 Example?-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 = alice Y = alice ; What s wrong here? (1)male(albert). (2)female(alice). (3)male(edward). (4)female(victoria). (5)parents(edward,victoria,albert). (6)parent(alice,victoria,albert). Example shows -subgoal order of evaluation -argument invertability -backtracking -computation in rule order 18 9

Rule Ordering and Unification Rule ordering (from first to last) used in search Unification requires all instances of the same variable in a rule to get the same value Unification does t require differently named variables to get different values: sister_of(alice, alice) All rules searched if requested by successive typing of ; 19 Example sis(x,y):-female(x),parents(x,m,f), parents(y,m,f),\+(x==y).?-sis(x,y). last subgoal disallows X,Y to have same value X=alice Y=edward ; = means unifies with == means same in value 20 10

Negation as Failure \+(P) succeeds when P fails Called negation by failure, defined: t(x):-x,!,fail. t(_). Which means if X succeeds in first rule, then the rule is forced to fail by the last subgoal (fail).we cant backtrack over the cut (!) in the first rule, and the cut prevents us from accessing the second rule. if X fails, then the second rule succeeds, because _ (or don t_care) unifies with anything. 21 Procedural Semantics?-sister_of(X,Y): female(x),parents(x,m,f),parents(y,m,f). Semantics: First find an X to make female(x) true Second find an M and F to make parents(x,m,f) true for that X. Third find a Y to make parents(y,m,f) true for those M,F This algorithm is recursive; each find works on a new copy of the facts+rules. eventually, each find must be resolved by appealing to facts. Process is called backward chaining. 22 11

Transitive Relations parents(jane,sally,bob). parents(john,sally,bob). parents(sally,mary,al). parents(bob,ann,mike). parents(mary,lee,joe). Y is ancestor of X ancestor(x,y):- parents(x,y,_). ancestor(x,y):- parents(x,_,y). Family tree ancestor(x,y):- parents(x,z,_),ancestor(z,y). lee joe ancestor(x,y):- parents(x,_,z),ancestor(z,y).?-ancestor(jane,x). X= sally ; X=ann ; X= bob ; X=mike ; ann mike mary al X= mary ; sally bob X= al ; X= lee ; X= joe ; jane john 23 Logic Programming vs Prolog Logic Programming: Nondeterministic Arbitrarily choose rule to expand first Arbitrarily choose subgoal to explore first Results don't depend on rule and subgoal ordering Prolog: Deterministic Expand first rule first Explore first(leftmost) subgoal first Results may depend on rule and subgoal ordering 24 12

Minimal Prolog Syntax <rule> ::= (<head> :- <body>.) <fact>. <head> ::= <predicate> <fact> ::= <predicate> <body> ::= <predicate> {, <predicate> } <predicate> ::= <functor> (<term> {,<term>} ) <term> ::= <integer> <atom> <variable> <predicate> <query> ::=?- <predicate> 25 13