1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog Nonmonotonic logic.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
1. An Overview of Prolog.
Primitive Recursive Functions (Chapter 3)
CS 461 – Nov. 9 Chomsky hierarchy of language classes –Review –Let’s find a language outside the TM world! –Hints: languages and TM are countable, but.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
PIIT Computer Science Summer Camp - Alice July 10, 2012 Brenda Parker Computer Science Department MTSU.
PHYS 2020 Pseudocode. Real Programmers Program in Pencil!  You can save a lot of time if you approach programming in a methodical way.  1) Write a clear.
Programming Assignment: Planning Goal: gaining familiarity with theorem-proving technology.
CSE115/ENGR160 Discrete Mathematics 02/28/12
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.
Expert System Human expert level performance Limited application area Large component of task specific knowledge Knowledge based system Task specific knowledge.
Fall 2004COMP 3351 Recursively Enumerable and Recursive Languages.
1 The Chomsky Hierarchy. 2 Unrestricted Grammars: Rules have form String of variables and terminals String of variables and terminals.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
1 Uncountable Sets continued Theorem: Let be an infinite countable set. The powerset of is uncountable.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Halting Problem. Background - Halting Problem Common error: Program goes into an infinite loop. Wouldn’t it be nice to have a tool that would warn us.
Induction and recursion
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Section 1.2 Linear Equations and Rational Equations
Formal Models of Computation Part II The Logic Model
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
General Programming Introduction to Computing Science and Programming I.
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: 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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Day Problems For each solution write and graph an inequality.
CSE Winter 2008 Introduction to Program Verification January 15 tautology checking.
1 Turing’s Thesis. 2 Turing’s thesis: Any computation carried out by mechanical means can be performed by a Turing Machine (1930)
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
In this section, we will learn about: Differentiating composite functions using the Chain Rule. DERIVATIVES 3.5 The Chain Rule.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
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.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Recursively Enumerable and Recursive Languages
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Solving Systems by Substitution (isolated) Solving Systems by Substitution (not isolated)
1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution).
1 TP #4: Control Facilities n Last TP exercises solved; n Don’t care variables « _ »; n Or « ; »; n Cut.
COSC 2P93 Prolog: Debugging
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CPSC 121: Models of Computation 2008/9 Winter Term 2
Tests, Backtracking, and Recursion
Section 1.2 Linear Equations and Rational Equations
Solving Systems of Equations by Substitution
Chapter 5: Control Structure
Lecture 6 - Recursion.
Presentation transcript:

1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs; n Exercise

© L.Lúcio, Miscellaneous Info n IMPORTANT: The exercices for the TPs can be downloaded from n Comments can be added in prolog using the « /* …… */ » notation: /* piece of code about animals */ habitat(bear,forest). habitat(elephant,savanah). habitat(cat,house). n If you want to add a (dynamic) fact or rule without using the source text file, use the assert predicate: n Example: assert(habitat(whale,sea)). n If you want to remove a (dynamic) fact or rule from the program (already in the interpreter), use the retract predicate: n Example: retract(habitat(whale,sea)).

© L.Lúcio, Last TP exercises solved n 1. The « livesin » relation: livesin(italy,mario). livesin(france,jean). livesin(portugal,maria). … n 1.1. “Who are the people that live in Switzerland?” ?- livesin(switzerland,X). n Download the solution from and load it into the interpreter.

© L.Lúcio, Last TP exercises solved (cont) n 2.1. The nationality rules: italian(X) :- wasborn(italy,X). swiss(X) :- wasborn(switzerland,X). portuguese(X) :- wasborn(portugal,X). … 3. The immigrant rule : ?- immigrant(X) :- livesin(Y,X),wasborn(Z,X),Y\==Z. n 2. The « wasborn » relation: wasborn(italy,mario). wasborn(switzerland,jean). wasborn(portugal,maria). …

© L.Lúcio, Last TP exercises solved (cont) n 4. “Give me the names of all the french immigrants in England”: ?- wasborn(france,X),livesin(england,X). or ?- french(X),livesin(england,X).

© L.Lúcio, How does Prolog answer questions? n Taking the immigrant example from the TP: immigrant(fabien) … livesin(england,carry). livesin(switzerland,fabien). livesin(england,veronique). … wasborn(england,carry). wasborn(france,fabien). wasborn(france,veronique). … immigrant(X) :- livesin(Y,X),wasborn(Z,X),Y\==Z. … TRUE livesin(Y,fabien),wasborn(Z,fabien),Y\==Z X = fabien (unification) wasborn(Z,fabien),switzerland\==Z Y = switzerland switzerland\==france Z = france

© L.Lúcio, How does Prolog answer questions? (cont) n Prolog tries to prove the theorem (question) that the user entered. In fact Prolog it is a theorem prover, with axioms (facts) and rules. n It is possible to follow the mechanism of Prolog by enabling the « trace » mode. It displays in text mode the search tree: ?- trace. Yes ?- immigrant(fabien). Call: (7) immigrant(fabien) ? creep Call: (8) livesin(_G478, fabien) ? creep Exit: (8) livesin(switzerland, fabien) ? creep Call: (8) wasborn(_G478, fabien) ? creep Exit: (8) wasborn(france, fabien) ? creep Call: (8) switzerland\==france ? creep Exit: (8) switzerland\==france ? creep Exit: (7) immigrant(fabien) ? creep Yes Temporary variables created by the interpreter!

© L.Lúcio, How does Prolog answer questions? (cont) n Now taking the example from question 4, let’s assume we want to write a rule that will print (on the screen, to a file) all the immigrants of nationality X that live in country Y: First approach: all_immigrants(X,Y) :- wasborn(X,Z),livesin(Y,Z),print(Z) n « print » is a predefined predicate in Prolog, like « \== » n Why doesn’t this first approach work and it only gives one answer? Pierre is also a french immigrant in England!… ?- all_immigrants(france,england). veronique Yes.

© L.Lúcio, How does Prolog answer questions? (cont) n In fact the answer is correct. What we asked for was a proof that the fact all_immigrants(france,england) is true, and Prolog was able to prove it; n The real question we asked was « Is there at least one french person living in England? » n If we want to get Prolog to tell us all the results for which the proof is possible, we have to use one more variable: all_immigrants(X,Y,Z) :- wasborn(X,Z),livesin(Y,Z),print(Z)

© L.Lúcio, How does Prolog answer questions? (cont) n Prolog tells us it was able to find two instances of X for which the fact all_immigrants(France,england,X) exists: « veronique » and « pierre »; n Try to use « trace » to check how Prolog finds the answers for the question. ?- all_immigrants(france,england,X). veronique X = veronique ; pierre X = pierre ; No

© L.Lúcio, Recursive Prolog programs n What is a recursive program? n Example: given a family tree using the relation « parentof », how can we define a rule that says whether someone is a predecessor of somebody else? n A predecessor of somebody is either a direct parent… or n There is a chain of parents between the predecessor and that person. n How do we express this in Prolog? parent(john,clara). parent(clara,mike). parent(mike,maria). JohnClaraMikeMaria predecessor

© L.Lúcio, Recursive Prolog programs (cont) n The program is recursive because it uses itself in order to be able to calculate the result; n There has to be a STOP condition (otherwise the program will call itself forever). n Recursion is the way LOOPS are made in Prolog! predecessor(X,Y) :- parent(X,Y). predecessor(X,Y) :- parent(X,Z),predecessor(Z,Y). n EXERCISE: How would we change the program to check if somebody is a descendant of somebody else? (use the predecessor program at: )