introduction to prolog terms & matching math. atoms and terms n mark, alex, di, bob are atoms...

41
Introduction to Prolog Introduction to Prolog Terms & Matching Terms & Matching Math Math

Upload: blaise-wood

Post on 28-Dec-2015

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Introduction to PrologIntroduction to Prolog

Terms & MatchingTerms & Matching

MathMath

Page 2: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Atoms and TermsAtoms and Terms

mark, alex, di, bob are mark, alex, di, bob are atomsatoms– Not variablesNot variables– Not stringsNot strings– Just Just things – simple things – simple thingsthings

There are more kinds of atomsThere are more kinds of atoms There are also There are also terms terms & & listslists

– Complex Complex or or compound compound thingsthings

Page 3: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

AtomsAtoms

Identifiers starting with lower-case letterIdentifiers starting with lower-case letter– markmark, , didi, , bobbob

Numbers: integers & floating pointNumbers: integers & floating point– 77, , 12081208, , -4-4, , 3.14163.1416, , 6.02e236.02e23

Sequences of special charactersSequences of special characters– ==, , :-:-, , ->->, , *->*->, , <=*=><=*=>

Single-quoted stringsSingle-quoted strings– ‘‘Tom’Tom’, , ‘23-skidoo’‘23-skidoo’, , ‘I am not a number!’‘I am not a number!’, , ‘3’‘3’

Page 4: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

TermsTerms

Term consists of functor and argumentsTerm consists of functor and arguments– Just like a factJust like a fact

Functor says what Functor says what kindkind of a thing it is of a thing it is– Arguments differentiate it from othersArguments differentiate it from others

point(2, 5)point(2, 5)

point(7, –13)point(7, –13)

date(2002, jan, 17)date(2002, jan, 17)– Terms can be arguments in facts, rules & Terms can be arguments in facts, rules & termsterms

Two different points on the plane

Today’s date

Page 5: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Using DatesUsing Dates

%% date(Year, Month, Day)%% date(Year, Month, Day)

%% Gregorian calendar dateGregorian calendar date

% % Year is a numberYear is a number

% % Month is a TLA for a monthMonth is a TLA for a month

% % Day is a number in the range 1..31Day is a number in the range 1..31

%% born(Person, DateOfBirth)%% born(Person, DateOfBirth)

born(alex, date(1994, apr, 24)).born(alex, date(1994, apr, 24)).

born(zachary, date(2001, dec, 4)).born(zachary, date(2001, dec, 4)).

Page 6: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

ExerciseExercise

Write born/2 for the members of your Write born/2 for the members of your familyfamily

born(alex, date(1994, apr, 24)).born(alex, date(1994, apr, 24)).

Page 7: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Matching TermsMatching Terms

Variables match anything: atom, term, …Variables match anything: atom, term, …?- ?- born(alex, Date).born(alex, Date).Date = date(1994, apr, 24)Date = date(1994, apr, 24)YesYes Can put variables Can put variables inin terms terms?- ?- born(Who, date(1994, _Month, _Day)).born(Who, date(1994, _Month, _Day)).Who = alexWho = alexYesYes

What’s Alex’s date of birth?

Who was born in 1994?

Page 8: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Rules with TermsRules with Terms

Can use terms in rulesCan use terms in rulesborn_in_year(Who, Year) :-born_in_year(Who, Year) :-

born(Who, date(Year, _Month, _Day)).born(Who, date(Year, _Month, _Day)).born_in_month(Who, Month) :-born_in_month(Who, Month) :-

born(Who, date(_Year, Month, _Day)).born(Who, date(_Year, Month, _Day)). Everything works just like you’d expectEverything works just like you’d expect?- ?- born_in_month(zachary, M).born_in_month(zachary, M).M = decM = dec

Page 9: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Non-Printing Variables in RulesNon-Printing Variables in Rules

Suppose we’d written:Suppose we’d written:born_in_year(Who, Year) :-born_in_year(Who, Year) :-

born(Who, date(Year, Month, Day)).born(Who, date(Year, Month, Day)). Code works, but Prolog complainsCode works, but Prolog complains

– Warning: Singleton variables: [Month, Day]Warning: Singleton variables: [Month, Day] Use variables to make two arguments sameUse variables to make two arguments same

– so wonders why those variables only used onceso wonders why those variables only used once

Page 10: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Single Single UseUse Variables Variables

Variables used only once may be a mistakeVariables used only once may be a mistake– typed one name wrongtyped one name wrong

Using _Var lets Prolog know that you know Using _Var lets Prolog know that you know that it’s only used once – and that’s OKthat it’s only used once – and that’s OK

Can also use Can also use anonymousanonymous variable, _ variable, _– ““anonymous” variable = has no nameanonymous” variable = has no name– use it multiple times – each a different variableuse it multiple times – each a different variable

Page 11: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Anonymous VariablesAnonymous Variables

Used when you *really* don’t care what Used when you *really* don’t care what value is filled invalue is filled in– don’t want it printed outdon’t want it printed out– don’t need it to be the same as any otherdon’t need it to be the same as any other

Each use of _ is a different variableEach use of _ is a different variableborn_in_year(Who, Year) :-born_in_year(Who, Year) :-

born(Who, date(Year, _, _)).born(Who, date(Year, _, _)).

Page 12: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Anonymous VariablesAnonymous Variables

Can use at the top level, tooCan use at the top level, too– doesn’t print the value (starts with _)doesn’t print the value (starts with _)– a different variable each timea different variable each time– (note – they (note – they maymay get the same value) get the same value)

?- ?- parent(P, _), parent(_, C).parent(P, _), parent(_, C).

P = markP = markC = alexC = alexyesyes

Q: Are there P and C such that -- P is someone’s parent, and C is someone’s child?A: yes, mark is someone’s parent and alex is

someone’s child

Page 13: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

On Using Anonymous VariablesOn Using Anonymous Variables

In general, use named variablesIn general, use named variables Start with _ if used only onceStart with _ if used only once Use anonymous variable only if there are a Use anonymous variable only if there are a

lot of single-use variables & no good, short lot of single-use variables & no good, short names to use for themnames to use for them

born_in_year(Who, Year) :-born_in_year(Who, Year) :-born(Who, date(Year, _Month, _Day)).born(Who, date(Year, _Month, _Day)).

Page 14: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

ExerciseExercise

Write a rule for born_in_same_year/2, which Write a rule for born_in_same_year/2, which says that the two people mentioned in the says that the two people mentioned in the arguments were born in the same year.arguments were born in the same year.

%% born_in_same_year(Person_1, Person_2)%% born_in_same_year(Person_1, Person_2)% % Person_1 & Person_2 born in same yearPerson_1 & Person_2 born in same year%% Note: perhaps on different days!Note: perhaps on different days!

born_in_same_year(Person_1, Person_2) :-born_in_same_year(Person_1, Person_2) :-

……..

Page 15: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Terms in TermsTerms in Terms

Line segment defined by two pointsLine segment defined by two pointsseg( point(0, 0), point(4, 3) )seg( point(0, 0), point(4, 3) )seg( point(1, 1), point(3, 4) )seg( point(1, 1), point(3, 4) ) Triangle defined by three pointsTriangle defined by three pointstriangle( point(0, 0), point(4, 3), point(3, 4) )triangle( point(0, 0), point(4, 3), point(3, 4) ) Two lines determine an angleTwo lines determine an angleangle( seg( point(0, 0), point(4, 3) ), angle( seg( point(0, 0), point(4, 3) ),

seg( point(1, 1), point(3, 4) ) ) seg( point(1, 1), point(3, 4) ) )

Page 16: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Terms in Facts and RulesTerms in Facts and Rules

A line segment is A line segment is verticalvertical if one point is if one point is directly above the otherdirectly above the other– The two points have the same X coordinateThe two points have the same X coordinate

vertical( seg( point(X, _Y1), point(X, _Y2) ) ).vertical( seg( point(X, _Y1), point(X, _Y2) ) ). And And that’sthat’s a fact a fact

– Note: vertical/1 – the only arg. is a line seg.Note: vertical/1 – the only arg. is a line seg.– Has only one clause in its definitionHas only one clause in its definition– Variables appear Variables appear inside inside the termthe term

Page 17: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Asking About Line SegmentsAsking About Line Segments

Is the line segment from (3,4) to (3,7) Is the line segment from (3,4) to (3,7) vertical?vertical?

?- ?- vertical( seg( point(3, 4), point(3, 7) ) ).vertical( seg( point(3, 4), point(3, 7) ) ).yesyes How about from (0,0) to (3,4)?How about from (0,0) to (3,4)??- ?- vertical( seg( point(0, 0), point(3, 4) ) ).vertical( seg( point(0, 0), point(3, 4) ) ).nono X can’t be both 0 and 3

they have the same X coordinate

Page 18: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Asking About Line SegmentsAsking About Line Segments

vertical( seg( point(X, Y1), point(X, Y2) ) ).vertical( seg( point(X, Y1), point(X, Y2) ) ).

?- ?- vertical( seg( point(3, 4), point(3, 7) ) ).vertical( seg( point(3, 4), point(3, 7) ) ).

X = 3, Y1 = 4, X = 3, Y2 = 7X = 3, Y1 = 4, X = 3, Y2 = 7yesyes

vertical( seg( point(X, Y1), point(X, Y2) ) ).vertical( seg( point(X, Y1), point(X, Y2) ) ).

?- ?- vertical( seg( point(0, 0), point(3, 4) ) ).vertical( seg( point(0, 0), point(3, 4) ) ).

X = 0, Y1 = 0, X = 3 (X = 0, Y1 = 0, X = 3 (failsfails))nono

Page 19: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Generating PointsGenerating Points

Is there line segment between (3,4) and a Is there line segment between (3,4) and a point on the x-axis that is vertical?point on the x-axis that is vertical?– On the x-axis means has y-coordinate zeroOn the x-axis means has y-coordinate zero

?- ?- vertical( seg( point(3, 4), point(X, 0) ) ).vertical( seg( point(3, 4), point(X, 0) ) ).X = 3X = 3yesyes Yes there is; it has x-coordinate 3Yes there is; it has x-coordinate 3

Page 20: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Asking About Line SegmentsAsking About Line Segments

vertical( seg( point(Xvertical( seg( point(X00, Y1), point(X, Y1), point(X00, Y2) ) )., Y2) ) ).

?- ?- vertical( seg( point(3, 4), point(X, 0) ) ).vertical( seg( point(3, 4), point(X, 0) ) ).

XX00 = 3, Y1 = 4, X = 3, Y1 = 4, X00 = X, Y2 = 0 = X, Y2 = 0

X = 3X = 3yesyes

Page 21: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Variables in VariablesVariables in Variables

Same as above – different way to askSame as above – different way to ask?- ?- P = point(_, 0), vertical( seg( point(3, 4), P ) ).P = point(_, 0), vertical( seg( point(3, 4), P ) ).P = point(3, 0)P = point(3, 0)yesyes Equals sign just gives a value to a variableEquals sign just gives a value to a variable Value of _ not given explicitly (anonymous)Value of _ not given explicitly (anonymous) Complete Complete value of P printed (no underscore)value of P printed (no underscore)

– Value of _ isValue of _ is part part of P’s valueof P’s value

Page 22: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Note Order IndependenceNote Order Independence

Can put P = point(_,0) before or afterCan put P = point(_,0) before or after?- ?- vertical( seg( point(3, 4), P ) ), P = point(_, 0).vertical( seg( point(3, 4), P ) ), P = point(_, 0).P = point(3,0)P = point(3,0)yesyes Also note symmetry of =Also note symmetry of =?- ?- vertical( seg( point(3, 4), P ) ), point(_, 0) = P.vertical( seg( point(3, 4), P ) ), point(_, 0) = P.P = point(3,0)P = point(3,0)yesyes

Page 23: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Equals is Equals is NotNot Assignment Assignment

Equals sign does not Equals sign does not change change the valuethe value– Just Just matchesmatches one side with the other one side with the other

?- ?- P = mark, P = di.P = mark, P = di.nono P is mark – it P is mark – it has has to to satisfy first =to to satisfy first =

– So it can’t So it can’t alsoalso be di be di

Page 24: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Equals Equals IsIs Matching Matching

Try to fill in all the variables so that the two Try to fill in all the variables so that the two sides are sides are exactlyexactly the same the same

Is there a point on both the x-axis and the y-Is there a point on both the x-axis and the y-axis?axis?

?- ?- point(X, 0) = point(0, Y).point(X, 0) = point(0, Y).X = 0X = 0Y = 0Y = 0yesyes

point(X,0) is a point somewhere on the x-axispoint(0,Y) is a point somewhere on the y-axisWe want them to be the same point, so use =

Page 25: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

MatchingMatching

Get all the pieces to be the sameGet all the pieces to be the same– variable matches anythingvariable matches anything– atom matches same atomatom matches same atom– term matches term with same functor and same term matches term with same functor and same

argumentsarguments

?- ?- bob(1, 2, X) = bob(X, 2, Y).bob(1, 2, X) = bob(X, 2, Y).

X = 1X = 1

Y = 1Y = 1

bob matches bob1 matches X2 matches 2X (1) matches Y

Page 26: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

ExerciseExercise

Do these match? What values do Vars get?Do these match? What values do Vars get?– fred and Ffred and F– fred and wilmafred and wilma– Thelma and louise(1,2,3)Thelma and louise(1,2,3)– tom(a,b,c) and tommy(a,b,c)tom(a,b,c) and tommy(a,b,c)– seg(pt(X,1), pt(2,X)) and seg(Z, pt(U,3))seg(pt(X,1), pt(2,X)) and seg(Z, pt(U,3))– a(X,Y,Y,Z) and a(1,2,Z,3)a(X,Y,Y,Z) and a(1,2,Z,3)

Page 27: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Making It MatchMaking It Match

seg(pt(X,1), pt(2,X))seg(pt(X,1), pt(2,X)) seg( Z, pt(U,3))seg( Z, pt(U,3))

Z=pt(X,1)Z=pt(X,1)

U=2U=2

X=3X=3

Z=pt(3,1)Z=pt(3,1)

Page 28: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Failing to MatchFailing to Match

a(X,Y,Y,Z)a(X,Y,Y,Z) a(1,2,Z,3)a(1,2,Z,3)

X=1X=1

Y=2Y=2

Y=ZY=Z

Z=2Z=2

Z=3 Z=3 impossible: Z=2impossible: Z=2

Page 29: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

ExerciseExercise

Don’t usually use =/2Don’t usually use =/2– just let Prolog give variables a valuejust let Prolog give variables a value?- ?- on_x_axis(P), vertical( seg( point(3,4), P ) ).on_x_axis(P), vertical( seg( point(3,4), P ) ).P = point(3, 0)P = point(3, 0)YesYes

A point is on the A point is on the xx axis if its axis if its yy coordinate is coordinate is zero. Write a zero. Write a factfact to express that. to express that.on_x_axis( … ).on_x_axis( … ).

Page 30: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Equals is Equals is NotNot Math Math

Beginners often do this:Beginners often do this:?- ?- X = 3 * 17 – 12 * 5.X = 3 * 17 – 12 * 5.X = 3*17 – 12*5X = 3*17 – 12*5yesyes It didn’t do the math!It didn’t do the math! Equality is Equality is matchingmatching

– Math is not (just) matchingMath is not (just) matching

Page 31: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

So What About MathSo What About Math

Use the is/2 predicate (infix syntax, like =)Use the is/2 predicate (infix syntax, like =)?- ?- X is 3 * 17 – 12 * 5.X is 3 * 17 – 12 * 5.X = –9X = –9yesyes Can use all usual math operations and Can use all usual math operations and

functionsfunctions– Multiply with *, divide /, power ^ or **Multiply with *, divide /, power ^ or **

Page 32: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Calculating DistanceCalculating Distance

Calculate distance between pointsCalculate distance between pointsdistance(point(X1,Y1), point(X2,Y2), D) :-distance(point(X1,Y1), point(X2,Y2), D) :-

D is sqrt((X1–X2)^2 + (Y1–Y2)^2).D is sqrt((X1–X2)^2 + (Y1–Y2)^2). Will calculate a distanceWill calculate a distance?- ?- distance(point(0,0), point(3,4), D).distance(point(0,0), point(3,4), D).D = 5D = 5yesyes

Page 33: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

(Not) Calculating Points(Not) Calculating Points

Will Will not not calculate a point to fit a distancecalculate a point to fit a distance

?- ?- distance(point(0,0), point(3,Y), 5).distance(point(0,0), point(3,Y), 5).ERROR: Arguments are not sufficiently instantiatedERROR: Arguments are not sufficiently instantiated^ Exception: (7) …^ Exception: (7) …

Type an Type an nn on the exception line on the exception line– Prolog says “no debug” and returns to promptProlog says “no debug” and returns to prompt– Couldn’t calculate D because didn’t know YCouldn’t calculate D because didn’t know Y

Note: SWI-Prolog throws exceptionsNote: SWI-Prolog throws exceptions

Page 34: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

““Extra-Logical” PredicatesExtra-Logical” Predicates

Some predicates don’t commuteSome predicates don’t commute– Y = 5, X is Y + 1 Y = 5, X is Y + 1 finefine– X is Y + 1, Y = 5X is Y + 1, Y = 5 no goodno good

Since logical AND is commutative, Since logical AND is commutative, Prolog’s comma is not Prolog’s comma is not quitequite the same the same– Prolog has a Prolog has a flow of controlflow of control– It is a programming language, after allIt is a programming language, after all

Page 35: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Aside: ConstraintsAside: Constraints

Some Prologs allow under-specified mathSome Prologs allow under-specified math– and other under-specified stuff as welland other under-specified stuff as well

““Freeze” a computation until it’s Freeze” a computation until it’s instantiated “enough”instantiated “enough”– {X is Y + 1}, Y = 5{X is Y + 1}, Y = 5 fine in BNR Prolog fine in BNR Prolog– Waits until it knows Y before it calculates XWaits until it knows Y before it calculates X– AKA “constraint” on XAKA “constraint” on X

Page 36: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Aside: Logical MathAside: Logical Math

Some Prologs can do logical mathSome Prologs can do logical math– give two arguments…give two arguments…– ……it fills in the thirdit fills in the third

?- ?- sum( 5, X, 12 ), product( X, Y, 63 ).sum( 5, X, 12 ), product( X, Y, 63 ).

X = 7X = 7

Y = 9Y = 9

YesYes

Page 37: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Atoms, Terms & ListsAtoms, Terms & Lists

AtomAtom– indivisible itemindivisible item

TermTerm– compound itemcompound item

ListList– sequence of itemssequence of items– (or of items & lists)(or of items & lists)– lots of useful built-in predicates for theselots of useful built-in predicates for these

Page 38: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

ListsLists

List = sequence of valuesList = sequence of values– [bob, brian, cathy, mark, david, loretta][bob, brian, cathy, mark, david, loretta]– [1, 2, 3, 4, 5][1, 2, 3, 4, 5]– [birds(4, calling), hens(3, french), doves(2, turtle), [birds(4, calling), hens(3, french), doves(2, turtle),

partridge(1, in(tree(1,pear)))]partridge(1, in(tree(1,pear)))]– [X, Y, Z, Z, Y][X, Y, Z, Z, Y]– [1, brian, doves(2, turtle), Z][1, brian, doves(2, turtle), Z]

Page 39: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Representations of ListsRepresentations of Lists

Written in [square] bracketsWritten in [square] brackets– Note: (parentheses), [brackets], {braces}Note: (parentheses), [brackets], {braces}– mean different things in computer languagesmean different things in computer languages

Elements, separated, by, commasElements, separated, by, commas Each element can be Each element can be anythinganything

– atom, term, variable, or listatom, term, variable, or list Simplest list is the empty list: []Simplest list is the empty list: []

Page 40: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Facts with ListsFacts with Lists

List can be an argument of a factList can be an argument of a fact– or of any termor of any term

%% family(Husband, Wife, Children)%% family(Husband, Wife, Children)family(mark, di, [alex, zachary]).family(mark, di, [alex, zachary]).

%% isa_polygon( P )%% isa_polygon( P )isa_polygon( poly([P1, P2, P3 | PMore]) ) :-isa_polygon( poly([P1, P2, P3 | PMore]) ) :-

are_points([P1, P2, P3 | PMore]).are_points([P1, P2, P3 | PMore]).

Page 41: Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things

Next TimeNext Time

More on listsMore on lists Sections 3.1 & 3.2Sections 3.1 & 3.2