CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
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).
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 05 / 2006 Instructor: Michael Eckmann.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 330 Programming Languages 12 / 04 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
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.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS 363 Comparative Programming Languages Logic Programming Languages.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 25 / 2008 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
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.
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 09 / 30 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Section 16.5, 16.6 plus other references
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2006 Today’s Topics Questions / comments? Prolog features Prolog examples

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog We'll see this being executed with printing to make clear what's being called when and how append([], List, List). append([Head | List_1], List_2, [Head | List_3]) :- append (List_1, List_2, List_3). append( [bob, jo], [jake, darcie], Family).

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog As stated earlier, Prolog uses depth-first search and left-to-right evaluation. So, resolution problems can occur with recursion that is not tail recursion. ORDER OF PROPOSITIONS AND SUBGOALS MATTER Example: ancestor(X, X). ancestor(X, Y) :- ancestor(Z, Y), parent(X, Z). Problem because second proposition has 1 st subgoal same as proposition (left-recursion). Suppose it is given the query ancestor(mike, jane). It goes to ancestor(Z, jane) and instantiates Z with a value. Then it does the proposition again to determine if it is true... Just like the problem a recursive descent parser has (remember?) How to solve this?

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog Example: member(Element, [Element | _ ] ). member(Element, [ _ | List] ) :- member(Element, List). /* _ is anonymous variable, use when we won't use the variable in the proposition. */ These member propositions state that if first parameter is the head of the second parameter, then member is satisfied. If not, then Element is a member of the List if Element is in the tail of the List.

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog Cut (!) to prevent backtracking (to earlier subgoals before the cut during a resolution.) Recall, that prolog backtracks when a goal (or subgoal) fails. Let's say we have something like this on the right hand side of a proposition: –member(X, Lis), do_something(X, Lis, Y). If member is satisfied and then do_something is attempted and fails, backtracking will occur and cause member to be reattempted. Inside member's implementation, it will pick up where it left off and possibly not find another match. To make sure it doesn't backtrack, we can use the Cut. member(Element, [Element | _ ] ) :- !. member(Element, [ _ | List] ) :- member(Element, List). Now, Prolog cannot backtrack to reevaluate member once Element is found in the list.

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog Negation problem parent(bill, jake). parent(bill, shelly). sibling( X, Y) :- parent(M, X), parent(M, Y). goal sibling(X, Y). results in: X = jake Y = jake Because it goes through the facts first with parent(M, X) and instantiates them to bill and jake. Then parent(M,Y) starts at the beginning of the facts and tries to find a fact with bill as parent. It finds bill and jake again.

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog Negation problem solution parent(bill, jake). parent(bill, shelly). sibling( X, Y) :- parent(M, X), parent(M, Y), X <> Y. goal sibling(X, Y). results in: X = jake Y = shelly Because it goes through the facts first with parent(M, X) and instantiates them to bill and jake. Then parent(M,Y) starts at the beginning of the facts and tries to find a fact with bill as parent. It finds bill and jake again. But then X <> Y becomes jake <> jake which fails, so backtracking occurs to parent(M, Y). Prolog remembers where it left off in the list of parent facts, so it gets the next one (bill, shelly) that's good, so then X <> Y becomes jake <> shelly which is satisfied. Done.

Michael Eckmann - Skidmore College - CS Fall 2006 Prolog Fail Can be used (as a subgoal) to force a goal to fail, thus forcing backtracking. Only makes sense as the last subgoal of a goal (because any subgoals after it would never get tested.)

Michael Eckmann - Skidmore College - CS Fall 2006 Visual Prolog Programs have the following sections: –DOMAINS Used when you want to declare your own “types” Standard types are: char, byte, short, ushort, word, integer, unsigned, long, ulong, dword, real, string, symbol (add a * for list of a type, e.g. symbol*) –PREDICATES (relation declarations) States the relation name and types. e.g.: son(string, string). –CLAUSES (facts and rules section) –GOAL (query)

Michael Eckmann - Skidmore College - CS Fall 2006 Visual Prolog nondeterm –used for predicates that have more than one solution, when those predicates are used to determine whether a goal is true or not

Michael Eckmann - Skidmore College - CS Fall 2006 Visual Prolog A few other things: –use nl to output a new line –use write( ) to write strings, variables, etc. –use exit to exit from a program. –use readln(Var) to read input from keyboard into Var (can use any name starting with a capital letter for the variable) Let's look at a couple of programs I have ready to show you. –speed calculation based on time, distance –factorial –list example