Section 16.5, 16.6 plus other references

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Chapter 11 :: Logic Languages
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Getting Started with Visual Prolog
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS 363 Comparative Programming Languages Logic Programming Languages.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Logic Programming Languages
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Chapter 2 - Introduction to C Programming
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Chapter 2 - Introduction to C Programming
Tests, Backtracking, and Recursion
Logic Programming Languages
Chapter 2 - Introduction to C Programming
Prolog a declarative language
Chapter 2 - Introduction to C Programming
Prolog a declarative language
Prolog a declarative language
Chapter 11 :: Logic Languages
Chapter 2 - Introduction to C Programming
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2: Prolog (Introduction and Basic Concepts)
Lisp.
PROLOG.
Presentation transcript:

Section 16.5, 16.6 plus other references Prolog Language for programming in logic Programmer expresses facts and rules User expresses queries (goals) Prolog inferencing engine tries to prove the goal using the existing facts and rules Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references The basics Getting into prolog % gprolog /usr/bin/gprolog | ?- prolog prompt Loading a prolog file | ?- [file]. File should end with a .pl extension Interpreter is now sitting waiting for you to ask questions Exiting prolog – type ctrl-D Comments – put inside of /* …. */ Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Facts Simple predicate statement followed by a period Predicates and constants should begin with a lower case letter and can consist of any alphanumeric character or underscore Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Examples of facts likes(jim, mary). likes(jim, marshmellows). likes(jim, books). likes(fred, wine). likes(mary, wine). likes(mary, food). owns(jim, car). owns(jim, marshmellows). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Rules head :- body. Head is a single predicate Body can be a conjunction of predicates, separated by commas Rule states that head is true if body is true Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Rule examples likes(jim, X) :- likes(X, wine), likes(X, food). X represents a variable that can be instantiated during the inferencing process Variables begin with a capital letter and can consist of any alphanumeric characters or underscore Section 16.5, 16.6 plus other references

Prolog queries (goals) Looks like a prolog fact, but when entered at the prolog prompt, prolog interprets it as a question Prolog attempts to use the facts and rules to prove the query Prolog uses a closed world assumption – if something can't be proven to be true than it must be false Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Query examples | ?- likes(jim, marshmellows). Yes | ?- owns(jim, X). X = car; X = marshmellows; No Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Query example | ?- likes(jim, X). X = mary; X = marshmellows; X = books; No Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Exciting life problem Express the following as prolog facts and rules All people that are wealthy and are smart are happy. Those people that read are smart. Kate is wealthy. John can read and is wealthy. Cindy can read. Happy people have exciting lives. Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Exciting life problem Express the following as a query Can anyone be found with an exciting life? Section 16.5, 16.6 plus other references

Inferencing process of prolog To prove a goal: Q Either Q matches a fact Inferencing process finds a sequence of rules to prove Q P2 :- P1 (P1 is true so P2 is true) P3 :- P2 . Q :- Pn (Pn is true so Q is true) Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Forward chaining Start with the facts and use rules to find sequence of matching propositions that lead up to the goal P1 P2 P3 . Q Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Backward chaining Start with goal and use propositions to get to a fact Q Pn . P2 P1 This is the typical approach in Prolog implementations Section 16.5, 16.6 plus other references

Using exciting life example: goal: smart(john) | smart(X) (smart(john) matches smart(X)) canread(X) (try canread(X) for X = john; | this matches fact) canread(john)) Section 16.5, 16.6 plus other references

Using exciting life example Rules like: happy(x) :- wealthy(x), smart(x) Mean that both wealthy(x) and smart(x) need to be solved in order to get happy(x) Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Resolution process Depth-first – solves first (leftmost) subgoal before working on other subgoals (wealthy(x) before smart(x)) Backtracking – reconsider a subgoal and try to solve it in another way (with possibly a different instantiation of variables) Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references List data structure Items separated by commas, enclosed in brackets [a, b, c] [this, and, that] [1, and, 2] [[jay, baron, grayson, ethan], [cindy]] Bar operator can be used to separate list into components [X | Y] /* X is the first element in the list, Y is tail */ [X, Y | Z] /* X is first element, Y is second element and Z is the rest of the list */ Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Example Suppose or prolog program consists of p([1, 2, 3]). p([this, that, [and, all, this]]). p([[inside, out], and, more]). Queries - how does prolog respond? | ?- p([X|Y]). | ?- p([X, Y | Z]). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Example continued More queries | ?- p([X, Y, Z | _]). /* _ is an anonymous placeholder */ | ?- p([_, _, [_|X]]). Section 16.5, 16.6 plus other references

A First Prolog function mylast(X, Y) – evaluates to Yes if X is a list and Y is the last element of the list X /* [X] is a list containing one element (X), thus X is the last */ mylast([X], X). /* [_|Y] is a list containing more than one element. */ mylast([_|Y], Z) :- mylast(Y, Z). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Mymember function Lets write a mymember function that takes 2 arguments, an element and a list, and returns yes if the element is in the list Base case: the element is at the head of the list Recursive case: an element is a member of a list if the element is a member of the tail Example query: mymember(a, [b, a, d]). Section 16.5, 16.6 plus other references

Evenelements function Takes as input 2 arguments. The first argument is a list containing an even number of elements. The second argument is a list containing the even numbered elements of the first list. (Numbering starts at 1.) Example queries: | ?- evenelements([a, b, c, d], [b, d]). | ?- evenelements([a, b, c, d], X). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Myappend function Takes three arguments, all lists, and returns yes if the third argument is equal to the append of the first argument to the second. Example queries | ?- myappend([1, 2], [3, 4], X). | ?- myappend([1, 2], [3, 4], [1, 2, 3, 4]). | ?- myappend(X, [3, 4], [1, 2, 3, 4]). | ?- myappend(X, Y, [1, 2, 3, 4]). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Myappend function Base cases An empty list appended to a list is that list A list appended to the empty list is that list Need recursive rule that moves closer to base cases Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Prefix function Takes 2 arguments and evaluates to yes if the first argument is a prefix of the second Example queries | ?- prefix([a, b], [a, b, c, d]). | ?- prefix(X, [a, b, c]). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Sublist function Takes two arguments and returns yes if the first is a sublist of the second Example queries: | ?- sublist([2, 3, 4], [1, 2, 3, 4, 5]). | ?- sublist(X, [1, 2, 3]). Section 16.5, 16.6 plus other references

Debugging prolog programs spy(prefix). Every call to prefix is traced Type <ret> to step through calls to the function nospy(prefix). Turns off trace of function prefix Section 16.5, 16.6 plus other references

Some Prolog Predicates atom(X) – evaluates to Yes if X is bound to an atom | ?- atom(a). Yes | ?- atom([a]). No integer(X) – evaluates to Yes if X is bound to an integer | ?- integer(a). No | ?- integer(3). Yes Similar functions: float, number Section 16.5, 16.6 plus other references

More Prolog Predicates \+(X) - evaluates to Yes if X evaluates to No (can not be satisfied). This function used to be called not. example rule: sibling(X, Y) :- parent(M, Y), parent(M, X), \+(X = Y). length(X, Y) – evaluates to Yes if X is a list whose length is Y | ?- length([a, b], X). X = 2 | ?- length(a, X). No Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Arithmetic in prolog Is operator v is expression. Expression is an arithmetic expression that is evaluated If the value of expression can be unified with v then the query succeeds Examples X is 3 + 5. Yes, by setting X to 8 8 is 3 + 5. Yes. 3 + 5 is 3 + 5. No, right side is evaluated, left is not Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Arithmetic in Prolog Arithmetic operators +, -, *, / with usual precedence Comparison operators =:= is equal to (only works on numbers) =\= is not equal to =< less than or equal to >= greater than or equal to > < = is equal for non-numeric constants \= is not equal for non-numeric constants Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Examples | ?- 3 =:= 3. Yes | ?- 3 < 4. Yes | ?- 3 * 4 < 4 * 4. Yes | ?- baron =:= grayson. Prolog error | ?- 3 = 2 + 1. No, because 2 + 1 isn’t evaluated. Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Problem. Given the following facts: birthyear(baron, 1994). birthyear(grayson, 1996). birthyear(ethan, 1999). birthyear(ben, 1994). birthyear(eli, 1994). year(2008). Section 16.5, 16.6 plus other references

Section 16.5, 16.6 plus other references Problem continued Add the following rules. age(X, Y) which returns yes if the age of X is Y younger(X, Y) which returns yes if X is younger than Y older(X, Y). sameage(X, Y) which returns yes if X and Y are the same age differentage(X, Y) fourteenyearold(X) which returns true if X is fourteen years old Section 16.5, 16.6 plus other references