CS 321 Programming Languages and Compilers. Prolog

Similar documents
Prolog. Reading: Sethi, Chapter 11. Overview. Predicate Calculus. Substitution and Unication. Introduction to Prolog. Prolog Inference Rules

Prolog. Intro to Logic Programming

Prolog. Logic Programming vs Prolog

Lecture 4: January 12, 2015

Introduction to predicate calculus

Logical reasoning systems

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

An introduction to logic programming with Prolog

First-Order Logic (FOL)

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

Logic Languages. Hwansoo Han

Chapter 16. Logic Programming Languages ISBN

Constraint Solving. Systems and Internet Infrastructure Security

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

6. Inference and resolution

Prolog Programming. Lecture Module 8

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

Chapter 16. Logic Programming Languages

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

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

Advanced Logic and Functional Programming

CS 561: Artificial Intelligence

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

Logic Programming Languages

Implementação de Linguagens 2016/2017

THE PREDICATE CALCULUS

What is a Logic Program. Introduction to Logic Programming. Introductory Examples. Introductory Examples. Foundations, First-Order Language

COMP4418 Knowledge Representation and Reasoning

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

Introduction to Logic Programming

Chapter 16. Logic Programming Languages ISBN

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

Chapter 9: Constraint Logic Programming

Part I Logic programming paradigm

Automatic Reasoning (Section 8.3)

Notes. Notes. Introduction. Notes. Propositional Functions. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry.

Programming Paradigms

Symbolic Programming Declarative Programming

Fundamentals of Prolog

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

Introduction Prolog Activity! Prolog. Dr. Mattox Beckman. University of Illinois at Urbana-Champaign Department of Computer Science

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

Knowledge & Reasoning

Database Theory VU , SS Introduction to Datalog. Reinhard Pichler. Institute of Logic and Computation DBAI Group TU Wien

The Metalanguage λprolog and Its Implementation

Introduction to Prolog

Programming Languages Prolog Programming Project Due Wednesday, December 5 th, 2001

Prolog-2 nd Lecture. Prolog Predicate - Box Model

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Week 7 Prolog overview

Quantification. Using the suggested notation, symbolize the statements expressed by the following sentences.

Automated Reasoning Systems

Lecture 7: January 15, 2014

Computational Logic Introduction to Logic Programming

Chapter 1.3 Quantifiers, Predicates, and Validity. Reading: 1.3 Next Class: 1.4. Motivation

Syntax: Terms Arity: is the number of arguments of a structure. Constructors are represented

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

What is all the Fuzz about?

Computation Club: Gödel s theorem

References. Topic #16: Logic Programming. Motivation. What is a program? Prolog Program Structure

zebra puzzle continued recap Prolog concepts family relations

Logic as a Programming Language

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

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

CS 4700: Artificial Intelligence

Logic Programming and Resolution Lecture notes for INF3170/4171

Brief Introduction to Prolog

First Order Predicate Logic CIS 32

Declarative Programming Prolog CS360

Prolog Introduction. Gunnar Gotshalks PI-1

6.034 Notes: Section 11.1

First Order Logic Semantics (3A) Young W. Lim 6/2/17

IN112 Mathematical Logic

IN112 Mathematical Logic

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

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

First Order Logic Semantics (3A) Young W. Lim 6/1/17

COMPUTER SCIENCE TRIPOS

Introduction to Logic Programming in Prolog 1 / 39

PROLOG PROgramming in LOGic

MIDTERM EXAM (Solutions)

Introduction (1A) Young Won Lim 9/12/13

Computational Logic. SLD resolution. Damiano Zanardini

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

Prolog 2. Unification and Recursion

The Prolog to Mercury transition guide

Range Restriction for General Formulas

LOGIC AND DISCRETE MATHEMATICS

Chapter 24. Active Database Concepts and Triggers. Outline. Trigger Example. Event-Condition-Action (ECA) Model

Prolog - 3 Prolog search trees + traces

Introduction to Data Management CSE 344. Lecture 14: Datalog

Programming Systems in Artificial Intelligence Introduction

Logic as a framework for NL semantics. Outline. Syntax of FOL [1] Semantic Theory Type Theory

6.034 Notes: Section 10.1

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

Logic Programming Paradigm

Introduction to Programming Paradigms. Prepared By: Neeraj Raheja

Visual Prolog Tutorial

Exercises on the Fundamentals of Prolog

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

Transcription:

CS 321 Programming Languages and Compilers Prolog

Prolog PROgramming LOGic Algorithm = Logic + Control Logic programming deals with computing relations rather than functions. To understand Prolog one must understand logic: the rules of deduction in first order logic control: the way Prolog implements this logic, I.e. the search strategy that Prolog adopts. 2

What is is Prolog Prolog is a typeless language with a very simple syntax. Prolog is a logical programming language. Prolog uses Horn clauses implements resolution» using a depth first strategy» unification Atoms, lists and records are the data structures. 3

Declarative Languages Also known as logic programming Examples: Prolog, SQL, Datalog. Typical of database systems and artificial intelligence. Declarative specifications: Specify what you want, not how to compute it. E.g. Find X and Y such that 3*X+2*Y=1 X-Y=4 4

Classical First-Order Logic simplest form of logical statements is an atomic formula. e.g. man(tom) woman(mary) married(tom,mary) More complex formulas can be built up using logical connectives:,,, X, X. 5

Examples of First Order Logic smart(tom) w dumb(tom) smart(tom) w tall(tom) 5dumb(tom) X married(tom,x) X loves(tom,x) X [married(tom,x) v female(x) v human(x)] 6

Logical Implication rich(tom) w 5 smart(tom) This implies that if tom is smart, then he must be rich. So, we often write this as rich(tom) 7 smart(tom) In general, P 7 Q and Q 6 P are abbreviations for P w 5 Q Examples: X[(person(X) v smart(x)) 6 rich(x)] X mother(john,x) X [mother(john,x) v Y[mother(john,Y) 6 Y=X] ] 7

Horn Rules Logic programming is based on formulas called Horn rules. These have the form x,..., x k [A B1 B2... B j] 1 Examples: X,Y[A(X) 7 B(X,Y) v C(Y)] X[A(X) 7 B(X)] X[A(X,d) 7 B(X,e)] A(c,d) 7B(d,e) X A(X) X A(X,d) A(c,d) Note that atomic formulas are also Horn rules, often called facts. A set of Horn rules is called a Logic Program. 8

Logical Inference with Horn Rules Logic programming is based on a simple idea: From rules and facts, derive more facts. Example 1. Given the facts A, B, C, D, and the rules: 1. E 7 A v B 2. F 7 C v D 3. G 7 E v F From 1, derive E; from 2, derive F; from 3, derive G. 9

Logical Inference Example 2: Given these facts: man(plato) man(socrates) and this rule: X [man(x) 6 mortal(x)] derive: mortal(plato), mortal(socrates). 10

Recursive Inference Example, given X[mortal(X) 6 mortal(son_of(x))] mortal(plato) derive: mortal(son_of(plato)) (using X=plato) mortal(son_of(son_of(plato))) (using X=son_of(plato)) mortal(son_of(son_of(son_of(plato)))) (using X=son_of(son_of(plato))) 11

Logic Programming Horn rules correspond to programs, and a form of Horn inference corresponds to execution. For example, consider the rule: X,Y p(x) 7 q(x,y) v r(x,y) v s(x,y) This rule can be interpreted as a program where p is the program name, q, r, s are subroutine names, X is a parameter of the program, and Y is a local variable. 12

Non-Horn Formulas The following formulas are not Horn: A 6 5 B A w B A w B 6 C X[A(X) 7 B(X)] A 7 (B 7 C) X[flag(X) 6 [red(x) w white(x)]] X Y[wife(X) 6 married(x,y)] 13

Non-Horn Inference Non-Horn inference is more complex that with Horn formulas alone. Example: A 7 B A 7 C B w C (non-horn) We can infer A, but only by doing case analysis either B or C is true. if B then A if C then A Therefore, A. Non-Horn formulas do not correspond to programs, and non-horn inference does not correspond to execution. 14

Logical Equivalence Many non-horn formulas can be put into Horn form by using either: logical equivalence Skolemization (a subject for a later course). Example of logical equivalence: 5 A 7 5 B / 5 A w 5 (5 B) / 5 A w B / B w 5 A / B 7 A (Horn) 15

Logical Laws 5 5 A / A 5(A w B) / 5 A v 5 B A w (B v C) / (A w B) v (A w C) A 7 B / A w 5 B Example using logical equivalence and laws: A 7 (B w C) / A w 5 (B w C) / A w (5B) v (5 C) / (A w 5B) v (A w 5 C) / (A 7 B) v (A 7 C) (Horn) 16

Non-convertible Formulas In general, rules of the following form cannot be converted into Horn form: x[(a1... A n ) (B1... B m )] For example, (A w B) 7 (C v D) (A w B) 7 C (A w B) i.e., if it is possible to infer a non-trivial disjunction from a set of formulas, then the set is inherently non-horn. 17

Prolog Syntax is Horn clauses of terms. Proof process involves SLD Resolution unification + substitution: pattern matching between terms + binding unresolved variables as needed. automatic backtracking: if one attempt fails, try again until all search paths are exhausted. SLD stands for Selecting a literal, using a Linear strategy, restricted to Definite clauses. The name SLD was coined by researchers in automatic theorem proving before the birth of logic programming 18

Prolog Notation For convenience, we don t write the universal quantifiers, so a rule: X [p(x) 7 (q(x) v r(x))] is written as p(x) 7 q(x), r(x). We also use the Prolog conventions: variables begin with upper case (A, B, X, Y, Big, Small, ACE) constants begin with lower case (a, b, x, y, plato, aristotle) 19

Prolog Syntax < fact > 6 < term >. < rule > 6 < term > :- < terms >. < query > 6 < terms >. < term > 6 < number > < atom > <variable> < atom > ( < terms > ) < terms > 6 < term > < term >, < terms > 20

Syntax Integers: base 10 atoms: user defined, supplied name starts with lower case: john, student2 Variables begin with upper case: Who, X _ can be used in place of variable name Structures student(ali, freshman, 194). Lists [x, y, Z ] [ Head Tail ] [ ]» syntactic sugar for. ( Head, Tail ) 21

Prolog Introduction /* list of facts in prolog, stored in an ascii file, family.pl */ mother(mary, ann). mother(mary, joe). mother(sue, mary ). father(mike, ann). father(mike, joe). grandparent(sue, ann). /* reading the facts from a file */ 1?- consult ( family.pl ). family.pl compiled, 0.00 sec, 828 bytes 22

Comments are either bound by /*, */ or any characters following the %. Structures are just relationships. There are no inputs or outputs for the variables of the structures. The swipl documentation of the built-in predicates does indicate how the variables should be used. pred(+var1, -var2, +var3). + indicates input variable - indicates output variable You can also consult a file with a pl extension by?- [family]. 23

/* Prolog the order of the facts and rules is the order it is searched in */ /* Variation from pure logic model */ 2?- father( X, Y ). X = mike /* italics represents computer output */ Y = ann ; /* I type ; to continue searching the data base */ X = mike Y = joe ; no 3?- father( X, joe). X = mike ; no 24

/* Rules */ parent( X, Y ) : mother( X, Y ). /* If mother( X,Y ) then parent( X,Y ) */ parent( X, Y ) : father( X, Y ). /* If the facts are later then grandparent(sue, ann). redundant */ /* if parent( X,Y ) and parent(y,z ) then grandparent( X,Z ). */ grandparent( X, Z ) : parent( X, Y ),parent(y, Z ). 25

or parent( X, Y ) : mother( X, Y ); father( X, Y ). has same effect as: parent( X, Y ) : mother( X, Y ). parent( X, Y ) : father( X, Y ). 26

mother(mary, ann). mother(mary, joe). mother(sue, mary ). father(mike, ann). father(mike, joe). parent( X, Y ) : mother( X, Y ). parent( X, Y ) : father( X, Y ).?- parent( X, joe). X = mary yes parent ( X, joe). binding Y = joe X = mary mother( X, joe). mother( mary, ann). /* fails */ mother (mary, joe). X = mary 27

?- parent( X, ann), parent( X, joe). X = mary; X = mike yes?- grandparent(sue, Y ). Y = ann; Y = joe yes 28

/* specification of factorial n! */ factorial(0,1). factorial(n, M): N1 is N 1, factorial (N1, M1), M is N*M1. 29

r1: addtoset( X,L,L ) :- member( X, L ). r2: addtoset( X,L,[X L] ). Pattern?-addToSet(a, [b,c], O). r1 r2 addtoset( X, L, L ) unification addtoset(a,[b,c],[b,c] addtoset(a, [b,c],[a [b,c]] subgoals member(a, [b,c]). FAILS backtrack addtoset( X, L, [X L] ) succeeds 30

Recursion in Prolog trivial, or boundary cases general cases where the solution is constructed from solutions of (simpler) version of the original problem itself. What is the length of a list? THINK: The length of a list, [ e Tail ], is 1 + the length of Tail What is the boundary condition? The list [ ] is the boundary. The length of [ ] is 0. Where do we store the value of the length?-- accumulator -- length( [ ], 0 ). length([h T], N) :- length(t,nx), N is Nx + 1 31

Recursion mylength( [ ], 0). mylength( [X Y], N): mylength(y, Nx), N is Nx+1.? mylength( [1, 7, 9], X ). X = 3? - mylength(jim, X ). no? - mylength(jim, X ). Jim = [ ] X = 0 32

Recursion mymember( X, [X _ ] ). mymember( X, [ _ Z ] ) : mymember( X, Z ). % equivalently: However swipl will give a warning % Singleton variables : Y W mymember( X, [X Y] ). mymember( X, [W Z ] ) : mymember( X, Z ). 1? mymember(a, [b, c, 6] ). no 2? mymember(a, [b, a, 6] ). yes 3? mymember( X, [b, c, 6] ). X = b; X = c; X = 6; no 33