prolog introduction - eecs.yorku.ca · what is a prolog program? – 2! relationships can have any...

34
PI-1 © Gunnar Gotshalks Prolog Introduction

Upload: others

Post on 12-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-1 © Gunnar Gotshalks!

Prolog Introduction!

Page 2: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-2 © Gunnar Gotshalks!

Physical Symbol System Hypothesis!

A necessary and sufficient condition for intelligence is the representation and manipulation of symbols.!

Page 3: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-3 © Gunnar Gotshalks!

Physical Symbol System Hypothesis – 2!

A necessary and sufficient condition for intelligence is the representation and manipulation of symbols.!

Prolog can represent and manipulate symbols!

Page 4: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-4 © Gunnar Gotshalks!

Physical Symbol System Hypothesis – 3!

A necessary and sufficient condition for intelligence is the representation and manipulation of symbols.!

Prolog can represent and manipulate symbols!

Prolog can do symbolic computing!

Page 5: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-5 © Gunnar Gotshalks!

What is symbolic computing?!

◊  Computing on !» Non-numbers !» Non-character-string !» Use atoms instead of numbers and strings !

Page 6: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-6 © Gunnar Gotshalks!

What is symbolic computing? – 2!

◊  Computing on !»  Non-numbers !»  Non-character-string !»  Use atoms instead of numbers and strings !

◊  Building structures from atoms !» Lists, trees, terms, clauses, propositions, etc.!

Page 7: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-7 © Gunnar Gotshalks!

What is symbolic computing? – 3!

◊  Computing on !»  Non-numbers !»  Non-character-string !»  Use atoms instead of numbers and strings !

◊  Building structures from atoms !»  Lists, trees, terms, clauses, propositions, etc.!

◊  Computing that uses descriptions and creates descriptions!

Page 8: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-8 © Gunnar Gotshalks!

Palindrome example!

◊  Declarative (Prolog-like) !palindrome ( X ) if reverse ( X, X ).!X is a palindrome if the reverse of X is X

! !reverse ( [], [] ). ! ! Reverse of empty is empty!reverse ( W ^ X, Y ) ! Reverse of W^X is Y !if reverse (X, Z) ! Reverse of X is Z and append ( Z, W, Y ). Appending W to Z is Y!

reverse of X! W!

Z!

Y!

Page 9: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-9 © Gunnar Gotshalks!

Alternatives to symbolic AI!

◊  Connectionist models!» Based on brain metaphor!» Model individual neurons and their connections!» Properties!

> Parallel, distributed, sub-symbolic!» Examples!

> Neural nets, associative memories!

Page 10: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-10 © Gunnar Gotshalks!

Alternatives to symbolic AI – 2!

◊  Emergent models!» Based on an evolution metaphor!» Potential solutions compete and evolve!» Properties!

> Massively parallel!> Complex behaviour evolves out of simple

behaviour!» Examples!

> Genetic algorithms!> Cellular automata!> Artificial life!

Page 11: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-11 © Gunnar Gotshalks!

What is a Prolog Program?!

◊  A program consists of a database containing one or more facts!

> A fact is a relationship between a collection of objects!

» dog ( fido ).!> Fido is a dog

– it is true that Fido is a dog!» mother ( mary , joe ).!

> Mary is the mother of Joe – it is true that Mary is the mother of Joe!

» compete ( ali , leila , tennis).!> Ali and Leila compete in tennis

– it is true that Ali and Leila compete in tennis!

Page 12: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-12 © Gunnar Gotshalks!

What is a Prolog Program? – 2!

◊  Relationships can have any number of objects!

◊  Names are usually chosen to be meaningful!» Within Prolog, names are just arbitrary strings. It

is people who give meaning to names.!

Page 13: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-13 © Gunnar Gotshalks!

What is a Prolog Program? – 3!

◊  And a program consists of a database of zero or more rules!> A rule is an if...then relationship of facts!

» use ( umbrella ) :- weather ( raining ).!> use an umbrella if weather is raining!

» use ( umbrella ) :- weather ( raining ) , own ( umbrella ).!> use an umbrella if weather is raining and you own an

umbrella!» use ( umbrella ) :- weather ( raining ) ,

( own ( umbrella ) ; borrow ( umbrella ) ).!> use an umbrella if weather is raining and you either

own an umbrella or can borrow an umbrella!

Page 14: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-14 © Gunnar Gotshalks!

More on rules!

◊  Rules have the general structure!   head :- body!

» Only one fact can be in the head – the consequent!» The body is a boolean combination of predicates!» Use , (and) and ; (or) and () (parenthesis) to

logically organize the "condition" – the antecedent!

◊  Rules are written backwards to!» emphasize the backward chaining for database

search!» be more regular in structure, since the head is only

one predicate!

Page 15: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-15 © Gunnar Gotshalks!

Constants!

◊  Constants are names that begin with lower case letters!» ali, leila, tennis, dog, fido, mother, mary, joe,

umbrella, raining, weather, own, borrow!» names of relationships are constants!

Page 16: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-16 © Gunnar Gotshalks!

Variables!

◊  In place of constants in facts and rules one can have variables!» variables are names that begin with upper case

letters!> X, Y, Who, Whom, List, Person!   loves ( Everyone , barney ).!> Everyone loves barney

– for all values of Everyone it is the case that loves(Everyone, barney) is true.!

  noisy ( Singer ) :- valkyrie ( Singer ) ; tenor ( Singer ).!

> A Singer is noisy if they are a Valkyrie or a tenor!

Page 17: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-17 © Gunnar Gotshalks!

Variables – 2!

  dwarf ( Person ) :- brother ( Person, Other ) , dwarf ( Other ).!

> A person is a dwarf, if they the brother of other and the other is a dwarf!

» Variables can also begin with _ (underscore)!   _ (anonymous variable)

_1 _abc (not anonymous variable )!

Page 18: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-18 © Gunnar Gotshalks!

Running a Prolog Program!

◊  Programs are stored in one or more files that are consulted!

◊  On Prism to run SWI Prolog enter!   % swipl!

◊  The following prompt appears!   ?-!

◊  Consult the appropriate file(s) – add to the database!   ?- consult ( 'ring.pl’ ).!

> SWI-prolog does not have a reconsult predicate, only consult is used!

> The following is an abbreviation!   ?- [ ring , tower , 'utilities.pl' ].!

Page 19: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-19 © Gunnar Gotshalks!

Running a Prolog Program – 2!

◊  Make zero or more queries (next slides)!

◊  Exit prolog!   ?- CTRL-d /* and for consult ( user ) */!

  consult ( user ) enables you to enter facts & rules into the database without storing them in a file. It is not an effective way to work with Prolog, as it is error prone!

Page 20: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-20 © Gunnar Gotshalks!

Queries!

◊  A query in Prolog is boolean combination of predicates – like the antecedent of a rule!

> A query is like a rule, except we leave out the consequent true!

  true :- dwarf ( alberich ).!> becomes simply!

  dwarf ( alberich ).!

◊  Use comma (and), semicolon (or) and parenthesis to form a query expression!

◊  Most common is to have a single predicate!

Page 21: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-21 © Gunnar Gotshalks!

Queries – 2!

◊  Answer is a binding of the variables that make the query expression true – if no variables then the answer is yes. If no such binding exists, the answer is no!

◊  The database is searched to match the query.!

◊  The search!» Uses backward chaining!»  is depth first!»  is sequential through the database from first to last!

◊  Try the exercise on ring.pl!

Page 22: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-22 © Gunnar Gotshalks!

Structures!

◊  Structures are a means of grouping a collection of other objects!» Structures are also called compound terms, or

complex terms!» The name of a structure is called a functor!» The items within a structure are called components!

◊  The general pattern is!   functor ( component_1 , component_2 ,

... component_n )!

Page 23: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-23 © Gunnar Gotshalks!

Structures – 2!

◊  Components can also be structures – recursive definition!   If component_1 = functor1 ( comp1, comp2 )!

> giving!   functor ( functor1 ( comp1, comp2 ),

component_2 , ... component_n )!

>  from!   functor ( component_1 , component_2 ,

... component_n )!

Page 24: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-24 © Gunnar Gotshalks!

Example structures!

◊  Books have authors and titles, so we could have!   book ( dickens , great_expectations )!

◊  People have books. In particular, Leila could have Great Expectations!   has ( leila , book ( dickens , great_expectations ) )!

◊  Facts in Prolog are structures where the predicate is the functor of a structure and the arguments of the predicate are the components of the structure!

Page 25: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-25 © Gunnar Gotshalks!

Characters!

◊  Prolog is based on the ASCII character set!

◊  Characters are treated as small integers 0 .. 127!

◊  Characters may be!» printed!»  read from a file or keyboard!» compared!»  take part in arithmetic operations!

◊  Characters are distinguished as !» printing – visible on the paper!» nonprinting – look like whitespace!

Page 26: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-26 © Gunnar Gotshalks!

Operators!

◊  All operators in Prolog are functors, even , ; and :- !>  A rule such as !

  dwarf ( Person ) :- brother ( Person , Other ) , dwarf ( Other ) .!

>  is a shorthand for !

  :- ( dwarf ( Person )!   , , ( brother ( Person , Other )!   , dwarf ( Other ) !   )!   ).!

Page 27: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-27 © Gunnar Gotshalks!

Operators – 2!

◊  Arithmetic and relational operators are also functors, thus!   a + b * c internally is + ( a , * ( b , c) )!

◊  This is inconvenient so Prolog permits operators to be written in standard infix notation!» You will learn later how you can define your own

infix operators!

Page 28: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-28 © Gunnar Gotshalks!

Arithmetic!

◊  The arithmetic operators do not do arithmetic. No assignments are made!

>  It is simply pattern matching – infix operators are simply a convenience for expressing a structure!

  5 = 4 + 1. ==> no 4 + 1 = 4 + 1. ==> yes 1 + 4 = 4 + 1. ==> no !

> Use the operator is to do arithmetic!   5 is 4 + 1. ==> yes 1 + 4 is 4 + 1. ==> no !

◊  Arithmetic is only done on the right!!

◊  Right hand side is evaluated using arithmetic, then a pattern match is made with the left hand side.!

Page 29: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-29 © Gunnar Gotshalks!

Arithmetic – 2!

◊  Can use variables in arithmetic expressions for pattern matching!   A = 4 + 1. ==> A has the pattern "4+1"

– spaces removed!   A is 4 + 1. ==> A has as value the pattern 5!

>  In some Prologs the latter expression simply responds yes, so try the following.!

  A is 4 + 1 , A = 5. ==> A = 5 is the binding for true!> More complex example!

  B is 3 + 2 , C is B * 5 , A is C + B ! ! ! !==> B = 5, C = 25, A =30 !

Page 30: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-30 © Gunnar Gotshalks!

Lists!

◊  Lists are a ubiquitous structure in many programming languages. The syntax changes (to protect the innocent?)!» Actually () are used to delimit structure

components and to provide precedence for operators, so using them for lists as well would be confusing.!

◊  The structure is!   [ item-1 , item-2 , ... , item-n ]!   [ a , b , c ]!   [ a , [ b , c ] , [ [ [ d ] ] ] , e , [ ] ]!

◊  The empty list is [ ]!

Page 31: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-31 © Gunnar Gotshalks!

Lists - 2!

◊  Lists have a head (first) and a tail (rest), thus!   [ Head | Tail ]!   [ a, b, c, d ] = [ a | [ b, c, d ] ]!   [ a, b, c, d ] = [ a, b | [ c, d ] ]!

◊  You do not have operators to extract the head and tail, all you have is pattern matching!»  [ a , b, c, d ] = [ First, Second | Rest ]

First = a! !Second = b ! Rest = [ c, d ]!» We will look at example Prolog utilities on lists to

demonstrate!

◊  Empty list has no head or tail!   [ ] ≠ [ _ | _ ]!

Page 32: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-32 © Gunnar Gotshalks!

Equal pattern matching!

» Standard match!> A = B !> A \= B !

!» Arithmetic values!

> A is expression!>  expr1 =:= expr2 !equal values

expr1 =\= expr2 !not equal values!

Page 33: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-33 © Gunnar Gotshalks!

Strict pattern match!

» == !structures identical!»  \== !structures not identical!

>  f(a, b) == f(a, b) –> yes!>  f(a, b) == f(a, X) –> no!

Page 34: Prolog Introduction - eecs.yorku.ca · What is a Prolog Program? – 2! Relationships can have any number of objects! Names are usually chosen to be meaningful! » Within Prolog,

PI-34 © Gunnar Gotshalks!

SWIPL Help!

◊  You can get help with the following predicates.!   help. !Brings up a help window from which you

! !can search for informationhelp(functor).! !Brings up a help window for the predicate

! !with the name “functor”help(is).! !Brings up a help window for the predicate ! !named “is”.!