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.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Some Prolog Prolog is a logic programming language
Prolog Nonmonotonic logic.
Inference Rules Universal Instantiation Existential Generalization
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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 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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
Prolog. SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free Downloadable from:
4. PROLOG Data Objects And PROLOG Arithmetic
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.
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.
Prolog CS 171/271. Before we begin… Download and install SWI-Prolog, version 5.6.4: bin/nph-download/SWI- Prolog/w32pl564.exe.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
11-Jun-15 Prolog II Unification and clause order.
Logical and Rule-Based Reasoning Part I. Logical Models and Reasoning Big Question: Do people think logically?
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.
First Order Logic Chapter 7. PL is a Weak Representational Language §Propositional Logic (PL) is not a very expressive language because: §Hard to identify.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Getting Started with Visual Prolog
Derived from csc.villanova.edu/~dmatusze/8310summer2001/index.html/prolog1.ppt1 Prolog.
1.  Provides the ability to access individual assertions. e.g. in Predicate calculus we may say: P denotes “It rained on Tuesday” but in predicate calculus.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
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.
1 CSC 8520 Spring Paula Matuszek Slides taken from David Matuszek,
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
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.
9/19/01 1 Prolog Tim Finin Tim Finin University of Maryland Baltimore County.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
23-Feb-16 Prolog II Unification and clause order.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
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:
PROLOG.
Prolog.
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Unification and clause order
Prolog III.
Prolog Concepts.
Predicate Logic: Syntax.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog III Lists 8-Jul-19.
Prolog Concepts.
PROLOG.
Presentation transcript:

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 Downloadable from: prolog.org/ prolog.org

What is Prolog? A logic programming language –“Prolog” stands for “Programming in Logic” (or “Programmation en Logique” in French) Designed in 1972 by Alain Colmerauer and Robert Kowalski (much later than Lisp) Used in AI programming, expert systems, and computational linguistics

Logic Programming Creates logical models that describe the world in which a problem exists Uses first-order logic (but can also be used with propositional logic) A subset of declarative programming –Creates a set of conditions that describe a solution space –Two phases: declaration and interpretation

5 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?

6 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

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

8 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

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

10 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'). –On a mac, opening a terminal window and typing swipl Then, ask your question at the prompt: –?- mortal(socrates). Prolog responds: –Yes

11 Prolog is a theorem prover Prolog's "Yes" or “true” means "I can prove it" -- Prolog's "No" or “false” means "I can't prove it" –?- mortal(plato). False. 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

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

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

14 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

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

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

17 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' Variables consisting of, or beginning with, _ are called anonymous variables. They are used when we don’t care what their value is.

18 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'

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

20 Backtracking loves(femi, X) :- female(X), rich(X). female(ajoke). female(asake). rich(asake). Suppose we ask: loves(femi, X). –female(X) = female(ajoke), X = ajoke. –rich(ajoke) fails. –female(X) = female(asake), X = asake. –rich(asake) succeeds.

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

22 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?

23 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

24 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

25 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like equality in algebra Unification is mostly like pattern matching Example: –loves(ade, X) can unify with loves(ade, asake) –and in the process, X gets unified with asake

26 Unification I Any value can be unified with itself. –weather(sunny) = weather(sunny) A variable can be unified with another variable. –X = Y A variable can be unified with (“instantiated to”) any Prolog value. –Topic = weather(sunny)

27 Unification Once a variable has been unified with a value, it continues to have that value for the rest of the clause, and can be used that way If we have –female(X) = female(jane) then –write(X) will write jane.

28 Writing Prolog Programs Suppose the database contains loves(femi, X) :- female(X), rich(X). female(jane). and we ask who Femi loves, ?- loves(chuck, Woman). female(X) finds a value for X, say, abike rich(X) then tests whether Abike is rich

29 Clauses as Cases A predicate consists of multiple clauses, each of which represents a “case” grandson(X,Y) :- son(X,Z), son(Z,Y). grandson(X,Y) :- son(X,Z), daughter(Z,Y). abs(X, Y) :- X < 0, Y is -X. abs(X, X) :- X >= 0.

30 Ordering Clauses are always tried in order buy(X) :- good(X). buy(X) :- cheap(X). cheap(‘Java 2 Complete’). good(‘Thinking in Java’). What will buy(X) choose first?

31 Ordering II Try to handle more specific cases (those having more variables instantiated) first. dislikes(john, bill). dislikes(john, X) :- rich(X). dislikes(X, Y) :- loves(X, Z), loves(Z, Y).

32 Basic and derived clauses You can often choose which facts you want to be "basic" and which derived son(isaac, steven). child(X, Y) :- son(X, Y). male(isaac). child(isaac, steven). son(X, Y) :- male(X), child(X, Y).

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

Step 1: Declaration Creating a knowledge base (KB) of sentences describing the world Declaring facts – fun(ai). (AI is fun!) – likes(mark,bacon). (Mark likes bacon) Defining rules – heartbroken(X) :- loves(X,Y), not(loves(Y,X)). (X is heartbroken if X loves Y and Y does not love X… *sniff*) Sentences end with a period

A simple Knowledge Base orbits(mercury, sun). orbits(venus, sun). orbits(earth, sun). orbits(mars, sun). orbits(moon, earth). orbits(phobos, mars). orbits(deimos, mars). planet(P) :- orbits(P,sun). satellite(S) :- orbits(S,P), planet(P).

Step 2: Interpretation Deriving new sentences from the sentences in the KB by making queries Uses a Prolog interpreter –SWI-Prolog, GNU Prolog, etc. Knowledge bases must first be loaded/consulted using consult(‘filename.pl’).

Some simple queries ?- consult(‘solar.pl’). % Is the moon a satellite? ?- satellite(moon). % Is the sun a planet? ?- planet(sun). % Is Uranus a planet? ?- planet(uranus). % What are the planets? ?- planet(Planet). % What objects orbit Mars? ?- orbits(X, mars). % Is the moon made of cheese? ?- made_of_cheese(moon).

Another KB example parents(william, diana, charles). parents(henry, diana, charles). parents(charles, elizabeth, philip). parents(diana, frances, edward). parents(anne, elizabeth, philip). parents(andrew, elizabeth, philip). parents(edwardW, elizabeth, philip). married(diana, charles). married(elizabeth, philip). married(frances, edward). married(anne, mark). parent(C,M) :- parents(C,M,D). parent(C,D) :- parents(C,M,D). sibling(X,Y) :- parents(X,M,D), parents(Y,M,D). % What’s wrong with this? aORuDirect(C, A) :- parent(C,P), sibling(P,A). aORuMarr(C, A) :- aORuDirect(C,X), married(X,A). aORuMarr(C, A) :- aORuDirect(C,X), married(A,X). aORu(C,A) :- aORuDirect(C,A). aORu(C,A) :- aORuMarr(C,A).

Predicates Used to express facts or statements about objects and their relationships Examples: likes heartbroken orbits Have arguments – teaches(jpv,cs171). (2 arguments) Names/atoms are alphanumeric, can contain underscores, begin with a lowercase letter Meanings of predicates, arguments, and order of arguments are arbitrary (but should be descriptive and consistent!)

Variables Can be used to stand for any object Are instantiated during matching Examples: Planet X Scope is statement-wide Usually used in rules, but can also be used in facts – goes_to_heaven(Dog) :- dog(Dog). (All dogs go to heaven.) – must_perish(Thing). (All things must perish.) Variable names are alphanumeric, can contain underscores, must begin with an uppercase letter Use the underscore to indicate an anonymous variable – female(X) :- mother(X,_). (All mothers are female, regardless of who the child is.)

Queries Describe problems to be solved Consist of goals to be satisfied –Example: father(X) :- male(X), parent(X,_). Goals are male(X) and parent (X,_). –Goals are checked against facts in the KB –Rules are satisfied if all the goals in the “if” part (in the body, separated by commas) are satisfied –Variables can take on any value Press semi-colon to look for further matches Uses backtracking to satisfy goals in all possible ways (“brute-forcing” it)

Prolog and FOL Rules are usually in the form of Horn clauses – :- is  reversed Backtracking in Prolog is known in FOL terms as backward chaining –Begins with a goal (the query) –Recursively builds a set of substitutions that satisfy the goals necessary to conclude the goal All variables in Prolog are assumed to be universally quantified (though skolemization can still be done)