Week 7 Prolog overview

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

Introduction to predicate calculus

will take you everywhere.

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

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

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

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

6.034 Notes: Section 11.1

Hardware versus software

Week 5: Background. A few observations on learning new programming languages. What's wrong with this (actual) protest from 1966?

Logic Languages. Hwansoo Han

Chapter 16. Logic Programming Languages ISBN

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

CS103 Spring 2018 Mathematical Vocabulary

First-Order Logic (FOL)

Prolog. Logic Programming vs Prolog

Programming Language Concepts (20013) Preparation for the Final Exam

Prolog 2. Unification and Recursion

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

Chapter 2 Preview. Preview. History of Programming Languages. History of Programming Languages. History of Programming Languages

Lecture 19: Topological sort Part 1: Problem and math basis. Topological sort. The problem. Why we are doing this!

Introduction to Prolog

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

Programming Languages Third Edition

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

Logic Programming Paradigm

Logic Programming Languages

Lecture 19: Functions, Types and Data Structures in Haskell

INF5390 Kunstig intelligens. First-Order Logic. Roar Fjellheim

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

COMP3411: Artificial Intelligence 8. First Order Logic

Introduction to Artificial Intelligence 2 nd semester 2016/2017. Chapter 8: First-Order Logic (FOL)

Advanced Logic and Functional Programming

CSC Discrete Math I, Spring Sets

Software Paradigms (Lesson 6) Logic Programming


Chapter 16. Logic Programming Languages

Chapter 16. Logic Programming Languages ISBN

Denotational semantics

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

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

ACSC300: Logic Programming

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

Appendix 1. Description Logic Terminology

Appendix 1. Description Logic Terminology

Programming Paradigms

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014

Week 11: Case study: Designing, building, & testing a Person class Background for the Project Needed in many applications Is it possible? practical?

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

Functional Programming Languages (FPL)

4.3 FURTHER PROGRAMMING

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

6.001 Notes: Section 8.1

Q1:a. Best first search algorithm:

Prolog. Intro to Logic Programming

CS 360 Programming Languages Interpreters

First-Order Predicate Logic. Introduction to Intelligent Systems

1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order).

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

A set with only one member is called a SINGLETON. A set with no members is called the EMPTY SET or 2 N

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

Programming Language Concepts (20013) Preparation for the Final Exam


l A family of logic based KR formalisms l Distinguished by: l Decidable fragments of FOL l Closely related to Propositional Modal & Dynamic Logics

IF/Prolog V5.3. User s Guide. Siemens AG Austria

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

CS 321 Programming Languages and Compilers. Prolog

Visual Prolog Tutorial

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Software Paradigms (Lesson 7) Logic Programming & Software Engineering

Lecture 5 - Axiomatic semantics

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Lecture 3: Recursion; Structural Induction

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

LOGIC AND DISCRETE MATHEMATICS

Com S 541. Programming Languages I

Visualization Of A Finite First Order Logic Model. Master of Science Thesis in Computer Science and Engineering CHRISTIAN SCHLYTER

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Lecture 8: Summary of Haskell course + Type Level Programming

Programming Languages Week 6 Exercises

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

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

CS 161 Computer Security

What's that? Why? Is one "better" than the other? Terminology. Comparison. Loop testing. Some experts (e.g. Pezze & Young) call it structural testing

Random Oracles - OAEP

Topic B: Backtracking and Lists

Discrete Mathematics Lecture 4. Harper Langston New York University

CSC 501 Semantics of Programming Languages

For next Tuesday. Read chapter 8 No written homework Initial posts due Thursday 1pm and responses due by class time Tuesday

Functional Programming

Boolean Algebra & Digital Logic

Sec Negative Goals ( Not ) 21

CSC 533: Programming Languages. Spring 2015

Recursion, Structures, and Lists

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

Transcription:

Week 7 Prolog overview A language designed for A.I. Logic programming paradigm Programmer specifies relationships among possible data values. User poses queries. What data value(s) will make this predicate true? COMP 378 -- Spring. 2017 Mr.. Weisert Comparison with functional paradigm (Clojure, etc.) A functional program consists of a sequence of function definitions. (Lisp & Clojure) A logic program consists of a sequence of relation definitions. (Prolog) "The Prolog execution environment doesn't so much compute an answer, it deduces an answer from the relation definitions at hand. Rather than being given an expression to evaluate, the Prolog environment is given an expression which it interprets as a question." -- Bucknell University Prolog Manual. Prolog is different from other programming languages Prolog has no types. In fact, the basic logic programming environment has no literal values as such. Identifiers starting with lower-case letters denote data values, while all other identifiers denote variables. Though the basic elements of Prolog are typeless, most implementations have been enhanced to include character and integer values and operations. Also, Prolog has mechanisms for describing tuples and lists. See http://www.cs.ukzn.ac.za/~hughm/ai/readings/prologintro.pdf COMP 378, Spring 2017 1-4 Prolog introduction

Underlying concept of Prolog Built upon symbolic predicate logic. Closer to pure mathematics than most programming languages. Small and simple Statements specify boolean values: facts (always--true) rules (predicates--true for some values of component terms) But Prolog does not have assignments branches loops conditional statements But how can we express a process without those? Status in 2017 Prolog (~1972) is a much newer language than Lisp (1958). Nevertheless, Prolog standardization was late. Early examples use some different symbols. We've settled on ISO Prolog (1995) as a standard. Wikipedia article is clear and reasonably complete https://en.wikipedia.org/wiki/prolog#iso_prolog Read it and try to understand everything But skip sections that don't seem to apply to our examples. Asserting a fact A simple property is a 1-argument function: dog(snoopy). prime(13). Snoopy is a dog 13 is a prime number A binary relation is a 2-argument function: a. A subject-object or other non-symmetric relation likes(mary,chocolate). Mary likes chocolate mother(elizabeth,charles). Elizabeth is the mother of Charles larger(chicago, Baltimore) b. Or sometimes a symmetric relation brothers(william,harry). Harry and William are brothers Are those all the facts? No. Factual predicates can have any number of parameters: flew(gloria, Boston, American, 1988). Gloria flew to Boston in 1988 on American Air Lines. But 1- and 2-argument predicates are often the most useful in A.I. More complicated relationships can often be represented by multiple facts. COMP 378, Spring 2017 5-8 Prolog introduction

Expressing a rule A simple rule is an implication like the predicate logic P Q Except that the conclusion is on the left! Q P Why? Notation: Some older Prologs used Some simulated it by <- Most (including the standard) now use :- A query asks: Backward chaining "For what parameter values [variables in the expression] does the expression evaluate to true?" Logical connectives Conjuction (logical AND) is expressed by comma (or in some versions &) daughter(x,y) :- child(x,y),female(y). (Y is the daughter of X) Disjuction (inclusive OR) is expressed by multiple definition clauses: parent(x,y) :- daughter(y,x). parent(x,y) :- son(y,x). (X is a parent of Y assuming son and daughter are defined or taken as primitive) An important convention Variables (predicate parameters) begin with an upper-case letter. Many Prolog experts like to use single-character names for variables (as in elementary algebra). That's OK for short scopes, but in longer, more complicated code we apply similar criteria to choosing names in any programming language. Variables are bound in each statement. So if Q appears three times in the same statement Prolog tries to instantiate all three occurences to the same value. But if Q also appears in the next statement, it's a different variable. COMP 378, Spring 2017 9-12 Prolog introduction

No circular definitions Example: Three ways of defining parent relations: a. Taking mother and father as primitive: parent(x,y) :- mother(x,y). parent(x,y) :- father(x,y). b. Taking parent as primitive: mother(x,y) :- parent(x,y), female(x). father(x,y) :- parent(x,y), male(x). c. Taking child as primitive (inverse relation) parent(x,y) :- child(y,x). Which is best? Properties of binary relations (terminology review) In elementary predicate logic we learned that a binary relation R, using Prolog notation, is: Reflexive if R(X,X) is true for every X in the domain Symmetric if R(Y,X) is true whenever R(X,Y) is true. Transitive if R(X,Z) is true whenever R(X,Y) and R(Y,Z) are both true for the same Y. Asymmetric if R(Y,X) is false whenever R(X,Y) is true What about: Antisymmetric? Irreflexive? Which are most useful in A.I. (inference)? Imporant kinds of relation An equivalence relation (also called an RST relation) is reflexive, symmetric, and transitive -- example: equals(x,y) A simple ordering relation is transitive and antisymmetric -- example: subset(b,a) A strict simple ordering relation is transitive and irreflexive -- example: olderthan(x,y) In designing rules of inference we can and should take advantage of those to avoid many special cases. Choices In the simple example (family relations) we chose which relations to take as primitive (data base) and which to define in terms of the others (rule base).. To keep it simple we prefer to work bottom-up starting with the most primitive relations that we're going to assert as facts. mother(elizabeth,charles) male(charles). or possibly the inverse son(charles,elizabeth). female(elizabeth). What other choices were there? COMP 378, Spring 2017 13-16 Prolog introduction

Generally The simplest predicates should be the ones that we plan to assert as facts. son(charles,elizabeth). We can then define more complicated and more general rules: child(x,y) :- son(x,y). child(x,y) :- daughter(x,y). mother(x,y) :- child(y,x), female(x). Keep it simple! Avoid redundancy! An important distinction Note that we can't define circular relationships, even though some natural language dictionaries may do so: parent(x,y):- child(y,x). child(x,y) :- son(x,y). child(x,y) :- daughter(x,y). son(x,y) :- parent(y,x), male(y). In developing a very large rule base, we may forget and do that by accident. Most Prolog processors will tell us where the problem is. But we can and should... No! Exploit recursion where it makes sense Especially in defining a transitive relation: ancestor(x,y):= parent(x,y). ancestor(x,y):= parent(x,w),ancestor(w,y). Note the implied existential qualifier: there exists a W such that... Prolog will try to instantiate it. How will prolog evaluate What intermediate values will it instantiate? But be careful! What's wrong with this? sibling(x,y):= parent(w,x), parent(w,y). i.e. X and Y are siblings if they have a common parent. Isn't that reasonable? How can we fix it? (Oops! We need to make it irreflexive. That's easy!) sibling(x,y) := X\=Y, parent(w,x), parent(w,y). COMP 378, Spring 2017 17-20 Prolog introduction

Recursive query example parent(george5,edward8), ancestor(edward8,william). The query Failed! Try another choice. Backtrack to try another person whose parent was george5 Recursive query example Trying another path: What happens next? Recursive query example ancestor(elizabeth,william). Recursive query example ancestor(elizabeth,william). ancestor(charles,william). COMP 378, Spring 2017 21-24 Prolog introduction

Recursive query example ancestor(elizabeth,william). ancestor(charles,william). parent(charles,william). Success! Recursive query example ancestor(elizabeth,william). ancestor(charles,william). parent(charles,william). Success! Three uses for a 2-place query ancestor (george5, charles). Is it true that...? ancestor (george5, X). Who are George5's descendants? i.e. Whom is George5 the ancestor of? ancestor(x, George5). Who are George5's ancestors? i.e. Whom is George5 the descendant of? Stopping recursion As in procedural programming, the base (non-recursive) cases should usually be the first ones specified. This (below) could lead to an infinite recursion attempt. ancestor(x,y):= parent(x,w),ancestor(w,y). ancestor(x,y):= parent(x,y). See http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/recursion.html COMP 378, Spring 2017 25-28 Prolog introduction

Negation as failure Prolog lacks a direct way of proving that a proposition is false. ancestor(george6,rahmemanuel). Because it lacks world knowledge. This is called the "closed world assumption". Instead, it just fails to show that it's provable. Warning: It may report failure (acceptable), but it may get stuck in an infinite process (bad!). The programmer must guard against that. Lists So far we've used only simple arguments (constants and variables) to our predicates. Prolog also handles lists enclosed in square brackets with comma separators [superior,michigan,huron,erie,ontario] Decomposing Lists for processing Similar to Lisp and Clojure The first element is called the Head (like Lisp car or Clojure first) superior A list of remaining elements is the Tail (like Lisp cdr or Clojure rest) [michigan,huron,erie,ontario] Processing a list often consists of Doing something to/with/on the Head Recursive invocation on the Tail Prolog versus Lisp (i.e. backward- versus forward-chaining search emphasis) For reasons that aren't altogether clear: A.I. researchers in America prefer Lisp and Lisp-like languages (Scheme, Clojure) A.I. researchers in Europe and Asia prefer Prolog and Prolog-like languages. But they will all use either approach when the problem clearly calls for it. Serious A.I. practictioners should feel comfortable with both. COMP 378, Spring 2017 29-32 Prolog introduction