CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Some Prolog Prolog is a logic programming language
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
CS344: Principles of Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 11, 12: Perceptron Training 30 th and 31 st Jan, 2012.
Chapter 11 :: Logic Languages
1. An Overview of Prolog.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 10– Club and Circuit Examples.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
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.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 9,10,11- Logic; Deduction Theorem 23/1/09 to 30/1/09.
CS344: Introduction to Artificial Intelligence (associated lab: CS386) Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture–5 and 6: Propositional Calculus.
CS621 : Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12- Completeness Proof; Self References and Paradoxes 16 th August,
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 7– Predicate Calculus and Knowledge Representation.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
CS344: Introduction to Artificial Intelligence (associated lab: CS386) Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 27, 28: Prolog 17 th and 21.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 22, 23- Prolog.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 21- Forward Probabilities and Robotic Action Sequences.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
CS344 : Introduction to Artificial Intelligence
CS344: Introduction to Artificial Intelligence (associated lab: CS386) Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 5: Monotonicity 13 th Jan, 2011.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
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.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
CS621: Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 28– Interpretation; Herbrand Interpertation 30 th Sept, 2010.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS621 : Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12- Completeness Proof; Self References and Paradoxes 16 th August,
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
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.
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.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 17– Theorems in A* (admissibility, Better performance.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
CS621: Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lectures 18, 19, 20– A* Monotonicity 2 nd, 6 th and 7 th September, 2010.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 4- Logic.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 5- Deduction Theorem.
Section 16.5, 16.6 plus other references
Prolog Concepts.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
CS621: Artificial Intelligence
Tests, Backtracking, and Recursion
Prolog Concepts.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog Concepts.
Presentation transcript:

CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog

An example Prolog Program

Shows path with mode of conveyeance from city C 1 to city C 2 :-use_module(library(lists)). byCar(auckland,hamilton). byCar(hamilton,raglan). byCar(valmont,saarbruecken). byCar(valmont,metz). byTrain(metz,frankfurt). byTrain(saarbruecken,frankfurt ). byTrain(metz,paris). byTrain(saarbruecken,paris). byPlane(frankfurt,bangkok). byPlane(frankfurt,singapore). byPlane(paris,losAngeles). byPlane(bangkok,auckland). byPlane(losAngeles,auckland). go(C1,C2) :- travel(C1,C2,L), show_path(L). travel(C1,C2,L) :- direct_path(C1,C2,L). travel(C1,C2,L) :- direct_path(C1,C3,L1),travel(C 3,C2,L2),append(L1,L2,L). direct_path(C1,C2,[C1,C2,' by car']):- byCar(C1,C2). direct_path(C1,C2,[C1,C2,' by train']):- byTrain(C1,C2). direct_path(C1,C2,[C1,C2,' by plane']):- byPlane(C1,C2). show_path([C1,C2,M|T]) :- write(C1),write(' to '),write(C2),write(M),nl,show_p ath(T).

Questions based on facts Answered by matching Two facts match if their predicates are same (spelt the same way) and the arguments each are same. If matched, prolog answers yes, else no. No does not mean falsity. Questions

Prolog does theorem proving When a question is asked, prolog tries to match transitively. When no match is found, answer is no. This means not provable from the given facts.

Variables Always begin with a capital letter ?- likes (john,X). ?- likes (john, Something). But not ?- likes (john,something)

Example of usage of variable Facts: likes(john,flowers). likes(john,mary). likes(paul,mary). Question: ?- likes(john,X) Answer: X=flowers and wait ; mary ; no

Conjunctions Use ‘,’ and pronounce it as and. Example Facts: likes(mary,food). likes(mary,tea). likes(john,tea). likes(john,mary) ?- likes(mary,X),likes(john,X). Meaning is anything liked by Mary also liked by John?

Backtracking (an inherent property of prolog programming) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds. X=food 2. Satisfy likes(john,food)

Backtracking (continued) Returning to a marked place and trying to resatisfy is called Backtracking likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal fails 2. Return to marked place and try to resatisfy the first goal

Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds again, X=tea 2. Attempt to satisfy the likes(john,tea)

Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal also suceeds 2. Prolog notifies success and waits for a reply

Rules Statements about objects and their relationships Expess If-then conditions I use an umbrella if there is a rain use(i, umbrella) :- occur(rain). Generalizations All men are mortal mortal(X) :- man(X). Definitions An animal is a bird if it has feathers bird(X) :- animal(X), has_feather(X).

Syntax :- Read ‘:-’ as ‘if’. E.G. likes(john,X) :- likes(X,cricket). “John likes X if X likes cricket”. i.e., “John likes anyone who likes cricket”. Rules always end with ‘.’.

Another Example sister_of (X,Y):- female (X), parents (X, M, F), parents (Y, M, F). X is a sister of Y is X is a female and X and Y have same parents

Question Answering in presence of rules Facts male (ram). male (shyam). female (sita). female (gita). parents (shyam, gita, ram). parents (sita, gita, ram).

Question Answering: Y/N type: is sita the sister of shyam? female(sita) parents(sita,M,F)parents(shyam,M,F) parents(sita,gita,ram) parents(shyam,gita,ram) success ?- sister_of (sita, shyam)

Question Answering: wh-type: whose sister is sita? female(sita) parents(sita,M,F)parents(Y,M,F) parents(sita,gita,ram) parents(Y,gita,ram) Success Y=shyam parents(shyam,gita,ram) ?- ?- sister_of (sita, X)

Exercise 1. From the above it is possible for somebody to be her own sister. How can this be prevented?

Prolog Program Flow, BackTracking and Cut Controlling the program flow

Prolog’s computation Depth First Search Pursues a goal till the end Conditional AND; falsity of any goal prevents satisfaction of further clauses. Conditional OR; satisfaction of any goal prevents further clauses being evaluated.

Control flow (top level) Given g:- a, b, c. (1) g:- d, e, f; g. (2) If prolog cannot satisfy (1), control will automatically fall through to (2).

Control Flow within a rule Taking (1), g:- a, b, c. If a succeeds, prolog will try to satisfy b, succeding which c will be tried. For ANDed clauses, control flows forward till the ‘.’, iff the current clause is true. For ORed clauses, control flows forward till the ‘.’, iff the current clause evaluates to false.

What happens on failure REDO the immediately preceding goal.

Fundamental Principle of prolog programming Always place the more general rule AFTER a specific rule.

CUT Cut tells the system that IF YOU HAVE COME THIS FAR DO NOT BACKTRACK EVEN IF YOU FAIL SUBSEQUENTLY. ‘CUT’ WRITTEN AS ‘!’ ALWAYS SUCCEEDS.

Fail This predicate always fails. Cut and Fail combination is used to produce negation. Since the LHS of the neck cannot contain any operator, A  ~B is implemented as B :- A, !, Fail.