Prolog. SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free Downloadable from:

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog Nonmonotonic logic.
Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
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).
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
1 DCP 1172 Introduction to Artificial Intelligence Chang-Sheng Chen Topics Covered: Introduction to Nonmonotonic Logic.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Prolog II. 2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
11-Jun-15 Prolog II Unification and clause order.
VIM: The basics Tang Wai-Chung, Matthew (MaFai) 29/12/2006.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Introduction to C Programming
1 About Prolog Lu Han Written at Modified at
Formal Models of Computation Part II The Logic Model
1 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Getting Started with Visual Prolog
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Derived from csc.villanova.edu/~dmatusze/8310summer2001/index.html/prolog1.ppt1 Prolog.
1 Artificial Intelligence Prolog for. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free.
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.
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.
Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
1 CSC 8520 Spring Paula Matuszek Slides taken from David Matuszek,
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Jigar Gaglani.  Programming paradigm is a fundamental style of computer programming  Paradigms differ in concepts and abstractions used to represent.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
9/19/01 1 Prolog Tim Finin Tim Finin University of Maryland Baltimore County.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
Knowledge Based Information System
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
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.
23-Feb-16 Prolog II Unification and clause order.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
CSC201: Computer Programming
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Prolog.
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Prolog Lists.
Unification and clause order
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Prolog III.
Prolog Lists.
Predicate Logic: Syntax.
Lisp.
Prolog III Lists 8-Jul-19.
Prolog Concepts.
Presentation transcript:

Prolog

SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free Downloadable from: SWI-Prolog/

Open Prolog On the Macintosh, Open Prolog is probably the best free (actually, "postcardware") implementation Differences from SWI-Prolog: –Doesn't require (or recognize) dynamic –Uses the "Enter" key, not the "Return" key –Uses reconsult rather than consult

Syllogisms “Prolog” is all about programming in logic. Aristotle described syllogisms 2300 years ago Sample syllogism: –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal. This is logic. Can Prolog do it?

Forward and backward reasoning A syllogism gives two premises, then asks, "What can we conclude?" –This is forward reasoning -- from premises to conclusions –it's inefficient when you have lots of premises Instead, you ask Prolog specific questions –Prolog uses backward reasoning -- from (potential) conclusions to facts

Syllogisms in Prolog Syllogism Socrates is a man. All men are mortal. Is Socrates mortal? man(socrates). mortal(X) :- man(X). ?- mortal(socrates). Prolog

Facts, rules, and queries Fact: Socrates is a man. man(socrates). Rule: All men are mortal. mortal(X) :- man(X). Query: Is Socrates mortal? mortal(socrates). Queries have the same form as facts

Running Prolog I Create your "database" (program) in any editor Save it as text only, with a.pl extension Here's the complete program: man(socrates). mortal(X) :- man(X).

Running Prolog II Prolog is completely interactive. Begin by –Double-clicking on your.pl file, or –Double-clicking on the Prolog application and consulting your file at the ?- prompt: ?- consult('C:\\My Programs\\adv.pl'). Then, ask your question at the prompt: –?- mortal(socrates). Prolog responds: –Yes

Prolog is a theorem prover Prolog's "Yes" means "I can prove it" -- Prolog's "No" means "I can't prove it" –?- mortal(plato). No This is the closed world assumption: the Prolog program knows everything it needs to know Prolog supplies values for variables when it can –?- mortal(X). X = socrates

Syntax I: Structures A structure consists of a name and zero or more arguments. Omit the parentheses if there are no arguments Example structures: –sunshine –man(socrates) –path(garden, south, sundial)

Syntax II: Base Clauses A base clause is just a structure. A base clause represents a simple fact. Example base clauses: –debug_on. –loves(john, mary). –loves(mary, bill).

Syntax III: Nonbase Clauses A nonbase clause is a structure, a turnstile (meaning IF), and a list of structures. Example nonbase clauses: –mortal(X) :- man(X). –mortal(X) :- woman(X) –happy(X) :- healthy(X), wealthy(X), wise(X). The comma between structures means AND

Syntax IV: Predicates A predicate is a collection of clauses with the same functor (name) and arity (number of arguments). loves(john, mary). loves(mary, bill). loves(chuck, X) :- female(X), rich(X).

Syntax V: Programs A program is a collection of predicates. Predicates can be in any order. Clauses within a predicate are used in the order in which they occur.

Syntax VI: Variables and atoms Variables begin with a capital letter: X, Socrates, _result Atoms do not begin with a capital letter: x, socrates Atoms containing special characters, or beginning with a capital letter, must be enclosed in single quotes: –'C:\\My Documents\\examples.pl'

Syntax VII: Strings are atoms In a quoted atom, a single quote must be doubled or backslashed: –'Can''t, or won\'t?' Backslashes in file names must also be doubled: –'C:\\My Documents\\examples.pl'

Common problems Capitalization is extremely important! No space is allowed between a functor and its argument list: man(socrates), not man (socrates). Double quotes indicate a list of ASCII character values, not a string Don’t forget the period! (But you can put it on the next line.)

Backtracking loves(chuck, X) :- female(X), rich(X). female(jane). female(mary). rich(mary) Suppose we ask: loves(chuck, X). –female(X) = female(jane), X = jane. –rich(jane) fails. –female(X) = female(mary), X = mary. –rich(mary) succeeds.

Backtracking and Beads Each Prolog call is like a “bead” in a string of beads: call fail exit redo Each structure has four ports: call, exit, redo, fail Exit ports connect to call ports; fail ports connect to redo ports

Calls as nested beads loves(chuck, X) :- female(X), rich(X). loves(chuck, X) female(X)rich(X) call fail exit redo

Additional answers female(jane). female(mary). female(susan). ?- female(X). X = jane ; X = mary Yes female(jane) female(mary) female(susan) female(X)

Readings loves(chuck, X) :- female(X), rich(X). Declarative reading: Chuck loves X if X is female and rich. Approximate procedural reading: To find an X that Chuck loves, first find a female X, then check that X is rich. Declarative readings are almost always preferred.

Monotonic logic Standard logic is monotonic: once you prove something is true, it is true forever Logic isn't a good fit to reality If the wallet is in the purse, and the purse in is the car, we can conclude that the wallet is in the car But what if we take the purse out of the car?

Nonmonotonic logic Prolog uses nonmonotonic logic Facts and rules can be changed at any time –such facts and rules are said to be dynamic assert(...) adds a fact or rule retract(...) removes a fact or rule assert and retract are said to be extralogical predicates

Examples of assert and retract assert(man(plato)). assert((loves(chuck,X) :- female(X), rich(X))). retract(man(plato)). retract((loves(chuck,X) :- female(X), rich(X))). Notice that we use double parentheses for rules –this is to avoid a minor syntax problem –assert(foo :- bar, baz). –How many arguments did we give to assert ?

Limitations of backtracking In Prolog, backtracking over something generally undoes it Output can't be undone by backtracking Neither can assert and retract be undone by backtracking Perform any necessary testing before you use write, nl, assert, or retract

Modeling "real life" Real life isn't monotonic; things change Prolog is superb for modeling change Games are often a model of real (or fantasy!) life Prolog is just about ideal for adventure games

Starting Prolog Welcome to SWI-Prolog (Version 3.3.0) Copyright (c) University of Amsterdam. All rights reserved. For help, use ?- help(Topic). or ?- apropos(Word). ?- consult('C:\\_Prolog\\dragon.pl'). % C:\_Prolog\dragon.pl compiled 0.00 sec, 14,560 bytes Yes

Instructions ?- start. Enter commands using standard Prolog syntax. Available commands are: start. -- to start the game. n. s. e. w. -- to go in that direction. take(Object). -- to pick up an object. drop(Object). -- to put down an object. use(Object). -- to use an object. attack. -- to attack an enemy. look. -- to look around you again. instructions. -- to see this message again. halt. -- to end the game and quit.

Starting out You are in a meadow. To the north is the dark mouth of a cave; to the south is a small building. Your assignment, should you decide to accept it, is to recover the famed Bar-Abzad ruby and return it to this meadow. Yes

Going south ?- s. You are in a small building. The exit is to the north. The room is devoid of furniture, and the only feature seems to be a small door to the east. There is a flashlight here. Yes

Taking things, locked doors ?- take(flashlight). OK. Yes ?- e. The door appears to be locked. You can't go that way. Yes

Some time later... ?- use(key). The closet is no longer locked. Yes Later still... ?- look. You are in a big, dark cave. The air is fetid. There is a chest here.

Essential facts Where I am at present: –i_am_at(meadow). Where other things are at: –at(flashlight, building). What I am holding: –holding(key). Which facts may be changed: –:- dynamic i_am_at/1, at/2, holding/1.

Input and output Input is unpleasant; we avoid it by giving commands (as questions) directly to Prolog –take(flashlight). write(...) outputs its one argument nl ends the line (writes a newline) describe(closet) :- write('You are in an old storage closet.'), nl.

The map cave_entrancecave meadow buildingcloset N W E S

Implementing the map path(cave, w, cave_entrance). path(cave_entrance, e, cave). path(meadow, s, building). path(building, n, meadow). Could have done this instead: –path(cave, w, cave_entrance). path(X, e, Y) :- path(Y, w, X).

listing listing(predicate) is a good way to examine the current state of the program ?- listing(at). –at(key, cave_entrance). at(flashlight, building). at(sword, closet). Yes

North, south, east, west The commands n, s, e, w all call go. n :- go(n). s :- go(s). e :- go(e). w :- go(w).

go go(Direction) :- i_am_at(Here), path(Here, Direction, There), retract(i_am_at(Here)), assert(i_am_at(There)), look. go(_) :- write('You can''t go that way.').

take take(X) :- i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(holding(X)), write('OK.'), nl.

You can't always take take(A) :- holding(A), write('You\'re already holding it!'), nl. take(A) :- (actually take something, as before). take(A) :- write('I don\'t see it here.'), nl.

Making things fail A predicate will fail if it doesn't succeed You can explicitly use fail fail works like this: This often isn't strong enough; it doesn't force the entire predicate to fail fail call fail

cut The "cut," written !, is a commit point –It commits to the clause in which it occurs, and –everything before it in that clause Using cut says: Don't try any other clauses, and don't backtrack past the cut ! callexit

cut-fail The cut-fail combination: !, fail means really fail It commits to this clause, then fails This means no other clauses of this predicate will be tried, so the predicate as a whole fails

A locked door path(building, e, closet) :- locked(closet), write('The door appears to be locked.'), nl, !, fail. path(building, e, closet). If the closet door isn't locked, the first clause fails "normally," and the second clause is used If the closet door is locked, the cut prevents the second clause from ever being reached

Dropping objects drop(A) :- holding(A), i_am_at(B), retract(holding(A)), assert(at(A, B)), write('OK.'), nl. drop(A) :- write('You aren\'t holding it!'), nl.

What else is Prolog good for? Prolog is primarily an AI (Artificial Intelligence) language It's second only to LISP in popularity It's more popular in Britain than in the U.S. Prolog is also a very enjoyable language in which to program (subjective opinion, obviously!)

Prolog vs. LISP Unlike LISP, Prolog provides: –built-in theorem proving –built in Definite Clause Grammars, good for parsing natural language If you just want to use these tools, Prolog is arguably better If you want to build your own theorem prover or parser, LISP is clearly better

The End