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

  • View

  • Download

Embed Size (px)

Text of 1 Artificial Intelligence Prolog for. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows...

  • *Artificial IntelligenceProlog


  • *SWI-PrologSWI-Prolog is a good, standard Prolog for Windows and LinuxIt's licensed under GPL, therefore freeDownloadable from: http://www.swi-prolog.org/

  • What is Prolog?A logic programming languageProlog 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 ProgrammingCreates logical models that describe the world in which a problem existsUses first-order logic (but can also be used with propositional logic)A subset of declarative programmingCreates a set of conditions that describe a solution spaceTwo phases: declaration and interpretation

  • *SyllogismsProlog is all about programming in logic.Aristotle described syllogisms 2300 years agoSample syllogism:Socrates is a man.All men are mortal.Therefore, Socrates is mortal.This is logic. Can Prolog do it?

  • *Forward and backward reasoningA syllogism gives two premises, then asks, "What can we conclude?"This is forward reasoning -- from premises to conclusionsit's inefficient when you have lots of premisesInstead, you ask Prolog specific questionsProlog uses backward reasoning -- from (potential) conclusions to facts

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

  • *Facts, rules, and queriesFact: 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 ICreate your "database" (program) in any editorSave it as text only, with a .pl extensionHere's the complete program:man(socrates). mortal(X) :- man(X).

  • *Running Prolog IIProlog is completely interactive. Begin byDouble-clicking on your .pl file, orDouble-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 swiplThen, ask your question at the prompt:?- mortal(socrates).Prolog responds:Yes

  • *Prolog is a theorem proverProlog'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 knowProlog supplies values for variables when it can?- mortal(X). X = socrates

  • *Syntax I: StructuresA structure consists of a name and zero or more arguments.Omit the parentheses if there are no argumentsExample structures:sunshineman(socrates)path(garden, south, sundial)

  • *Syntax II: Base ClausesA base clause is just a structure that represents a simple fact.Example base clauses:debug_on.loves(john, mary).loves(mary, bill).

  • *Syntax III: Nonbase ClausesA 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: PredicatesA 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: ProgramsA 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 atomsVariables begin with a capital letter: X, Socrates, _resultAtoms do not begin with a capital letter: x, socratesAtoms 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 dont care what their value is.

  • *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 problemsCapitalization 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 stringDont forget the period! (But you can put it on the next line.)

  • *Backtrackingloves(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.

  • *Readingsloves(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.

  • *Monotonic logicStandard logic is monotonic: once you prove something is true, it is true foreverLogic isn't a good fit to realityIf the wallet is in the purse, and the purse in is the car, we can conclude that the wallet is in the carBut what if we take the purse out of the car?

  • *Nonmonotonic logicProlog uses nonmonotonic logicFacts and rules can be changed at any timesuch facts and rules are said to be dynamicassert(...) adds a fact or ruleretract(...) removes a fact or ruleassert and retract are said to be extralogical predicates

  • *Limitations of backtrackingIn Prolog, backtracking over something generally undoes itOutput can't be undone by backtrackingNeither can assert and retract be undone by backtrackingPerform any necessary testing before you use write, nl, assert, or retract

  • *The Notion of UnificationUnification is when two things become oneUnification is kind of like assignmentUnification is kind of like equality in algebraUnification is mostly like pattern matchingExample:loves(ade, X) can unify with loves(ade, asake)and in the process, X gets unified with asake

  • *Unification IAny value can be unified with itself.weather(sunny) = weather(sunny)A variable can be unified with another variable.X = YA variable can be unified with (instantiated to) any Prolog value.Topic = weather(sunny)

  • *UnificationOnce 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 wayIf we havefemale(X) = female(jane) thenwrite(X) will write jane.

  • *Writing Prolog ProgramsSuppose 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, abikerich(X) then tests whether Abike is rich

  • *Clauses as CasesA predicate consists of multiple clauses, each of which represents a casegrandson(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.

  • *OrderingClauses are always tried in orderbuy(X) :- good(X). buy(X) :- cheap(X). cheap(Java 2 Complete). good(Thinking in Java). What will buy(X) choose first?

  • *Ordering IITry 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).

  • *Basic and derived clausesYou can often choose which facts you want to be "basic" and which derivedson(isaac, steven). child(X, Y) :- son(X, Y).male(isaac). child(isaac, steven). son(X, Y) :- male(X), child(X, Y).

  • *ArithmeticThe 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 =:= =\= > >= <
  • Step 1: DeclarationCreating a knowledge base (KB) of sentences describing the worldDeclaring factsfun(ai). (AI is fun!)likes(mark,bacon). (Mark likes bacon)Defining rulesheartbroken(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 Baseorbits(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: InterpretationDeriving new sentences from the sentences in the KB by making queriesUses a Prolog interpreterSWI-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 exampleparents(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). % Whats 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).