Prolog Nonmonotonic logic.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Solving Equations for a Specific Variable
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Modern Programming Languages
Prolog.
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.
1 DCP 1172 Introduction to Artificial Intelligence Chang-Sheng Chen Topics Covered: Introduction to Nonmonotonic Logic.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Prolog. SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free Downloadable from:
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.
SPARK 0.8 David Morley, Nov Core Data Types Includes values most often used by SPARK Primitive –Integer, e.g. 1 –Float, e.g., 1.0 –String, e.g.,
Reasoning System.  Reasoning with rules  Forward chaining  Backward chaining  Rule examples  Fuzzy rule systems  Planning.
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
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.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
CSNB234 ARTIFICIAL INTELLIGENCE
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
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.
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.
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.
Introduction to Prolog Facts, Questions & Rules Atoms & Variables.
Prolog. Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
 What are exponents?  Exponents are numbers that show how many times a base is used as a factor.  How do we use exponents?  Exponents will tell us.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Artificial Intelligence Lecture No. 24 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
IS IT LOGICAL? Ask yourself… DOES THIS MAKE SENSE??
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
1-1 An Introduction to Logical Programming Sept
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.
23-Feb-16 Prolog II Unification and clause order.
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.
Chapter 2 Syntax and meaning of prolog programs Part 1.
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;
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).
The Addition/Subtraction Principle of Equality
tautology checking continued
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Variables on Both Sides with Equations
Prolog.
Prolog syntax + Unification
Unification and clause order
Prolog III.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog III Lists 8-Jul-19.
Prolog Concepts.
PROLOG.
Presentation transcript:

Prolog Nonmonotonic logic

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?

Marking clauses as “Dynamic” Standard Prolog allows you to assert and retract clauses without any restrictions. SWI-Prolog and some others require you to mark variable clauses as “dynamic.” :- dynamic i_am_at/1, at/2, alive/0. The “:-” at the beginning says “do it now.”

Solving problems with dynamic If Prolog already knows a clause, and it’s static, it’s too late to mark it dynamic Prolog must see :- dynamic functor/arity before it sees any clauses of functor/arity. This includes clauses loaded in from an earlier consult You can restart SWI-Prolog, or… …you can use abolish(functor, arity)

Arithmetic The equals sign, =, means “unify.” 2 + 2 does not unify with 4. To force arithmetic to be performed, use “is”: X is 2 + 2, X = 4. Comparisons =:= =/= > >= < <= also force their operands to be evaluated. + - * / mod, when evaluated, have their usual meanings.

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 [Macintosh:~] dave% prolog % library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.1) Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. ?- 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 start. 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. true.

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. true.

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

Some time later... ?- use(key). The closet is no longer locked. true. 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: Where other things are at: 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_entrance cave meadow building closet N W E S

Implementing the map Could have done this instead: 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). true.

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).

Making predicates succeed ?- go(s). false. This works, but it isn’t very user friendly. Remember: A predicate can consist of more than one clause The clauses will be tried in order So we can get the following behavior: ?- go(s). You can't go that way. true. We could further improve this with an explanation, such as, “The door is locked.”, or, “The guard demands to see your ID.”

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). write('I don\'t see it here.'), nl.

Making things fail fail A predicate will fail if it doesn’t succeed You can explicitly use fail fail works like this: fail call This often isn’t strong enough; it doesn’t force the entire predicate to 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 ! call exit

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. 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