Download presentation
Presentation is loading. Please wait.
Published byBlaise Wood Modified over 9 years ago
1
Introduction to Prolog Terms & Matching Math
2
Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things n There are more kinds of atoms n There are also terms & lists –Complex or compound things
3
Atoms n Identifiers starting with lower-case letter –mark, di, bob n Numbers: integers & floating point –7, 1208, -4, 3.1416, 6.02e23 n Sequences of special characters –=, :-, ->, *->, –=, :-, ->, *->, n Single-quoted strings –‘Tom’, ‘23-skidoo’, ‘I am not a number!’, ‘3’
4
Terms n Term consists of functor and arguments –Just like a fact n Functor says what kind of a thing it is –Arguments differentiate it from others point(2, 5) point(7, –13) date(2002, jan, 17) –Terms can be arguments in facts, rules & terms Two different points on the plane Today’s date
5
Using Dates % date(Year, Month, Day) %Gregorian calendar date % Year is a number % Month is a TLA for a month % Day is a number in the range 1..31 % born(Person, DateOfBirth) born(alex, date(1994, apr, 24)). born(zachary, date(2001, dec, 4)).
6
Exercise n Write born/2 for the members of your family born(alex, date(1994, apr, 24)).
7
Matching Terms n Variables match anything: atom, term, … ?- born(alex, Date). Date = date(1994, apr, 24) Yes n Can put variables in terms ?- born(Who, date(1994, _Month, _Day)). Who = alex Yes What’s Alex’s date of birth? Who was born in 1994?
8
Rules with Terms n Can use terms in rules born_in_year(Who, Year) :- born(Who, date(Year, _Month, _Day)). born_in_month(Who, Month) :- born(Who, date(_Year, Month, _Day)). n Everything works just like you’d expect ?- born_in_month(zachary, M). M = dec
9
Non-Printing Variables in Rules n Suppose we’d written: born_in_year(Who, Year) :- born(Who, date(Year, Month, Day)). n Code works, but Prolog complains –Warning: Singleton variables: [Month, Day] n Use variables to make two arguments same –so wonders why those variables only used once
10
Single Use Variables n Variables used only once may be a mistake –typed one name wrong n Using _Var lets Prolog know that you know that it’s only used once – and that’s OK n Can also use anonymous variable, _ –“anonymous” variable = has no name –use it multiple times – each a different variable
11
Anonymous Variables n Used when you *really* don’t care what value is filled in –don’t want it printed out –don’t need it to be the same as any other n Each use of _ is a different variable born_in_year(Who, Year) :- born(Who, date(Year, _, _)).
12
Anonymous Variables n Can use at the top level, too –doesn’t print the value (starts with _) –a different variable each time –(note – they may get the same value) ?- parent(P, _), parent(_, C). P = mark C = alex yes 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
13
On Using Anonymous Variables n In general, use named variables n Start with _ if used only once n Use anonymous variable only if there are a lot of single-use variables & no good, short names to use for them born_in_year(Who, Year) :- born(Who, date(Year, _Month, _Day)).
14
Exercise n Write a rule for born_in_same_year/2, which says that the two people mentioned in the arguments were born in the same year. % born_in_same_year(Person_1, Person_2) % Person_1 & Person_2 born in same year %Note: perhaps on different days! born_in_same_year(Person_1, Person_2) :- ….
15
Terms in Terms n Line segment defined by two points seg( point(0, 0), point(4, 3) ) seg( point(1, 1), point(3, 4) ) n Triangle defined by three points triangle( point(0, 0), point(4, 3), point(3, 4) ) n Two lines determine an angle angle( seg( point(0, 0), point(4, 3) ), seg( point(1, 1), point(3, 4) ) )
16
Terms in Facts and Rules n A line segment is vertical if one point is directly above the other –The two points have the same X coordinate vertical( seg( point(X, _Y1), point(X, _Y2) ) ). n And that’s a fact –Note: vertical/1 – the only arg. is a line seg. –Has only one clause in its definition –Variables appear inside the term
17
Asking About Line Segments n Is the line segment from (3,4) to (3,7) vertical? ?- vertical( seg( point(3, 4), point(3, 7) ) ). yes n How about from (0,0) to (3,4)? ?- vertical( seg( point(0, 0), point(3, 4) ) ). no X can’t be both 0 and 3 they have the same X coordinate
18
Asking 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) ) ). X = 3, Y1 = 4, X = 3, Y2 = 7 X = 3, Y1 = 4, X = 3, Y2 = 7yes vertical( seg( point(X, Y1), point(X, Y2) ) ). vertical( seg( point(X, Y1), point(X, Y2) ) ). ?- vertical( seg( point(0, 0), point(3, 4) ) ). X = 0, Y1 = 0, X = 3 (fails) X = 0, Y1 = 0, X = 3 (fails)no
19
Generating Points n Is there line segment between (3,4) and a point on the x-axis that is vertical? –On the x-axis means has y-coordinate zero ?- vertical( seg( point(3, 4), point(X, 0) ) ). X = 3 yes n Yes there is; it has x-coordinate 3
20
Asking About Line Segments vertical( seg( point(X 0, Y1), point(X 0, Y2) ) ). vertical( seg( point(X 0, Y1), point(X 0, Y2) ) ). ?- vertical( seg( point(3, 4), point(X, 0) ) ). X 0 = 3, Y1 = 4, X 0 = X, Y2 = 0 X 0 = 3, Y1 = 4, X 0 = X, Y2 = 0 X = 3 yes
21
Variables in Variables n Same as above – different way to ask ?- P = point(_, 0), vertical( seg( point(3, 4), P ) ). P = point(3, 0) yes n Equals sign just gives a value to a variable n Value of _ not given explicitly (anonymous) n Complete value of P printed (no underscore) –Value of _ is part of P’s value
22
Note Order Independence n Can put P = point(_,0) before or after ?- vertical( seg( point(3, 4), P ) ), P = point(_, 0). P = point(3,0) yes n Also note symmetry of = ?- vertical( seg( point(3, 4), P ) ), point(_, 0) = P. P = point(3,0) yes
23
Equals is Not Assignment n Equals sign does not change the value –Just matches one side with the other ?- P = mark, P = di. no n P is mark – it has to to satisfy first = –So it can’t also be di
24
Equals Is Matching n Try to fill in all the variables so that the two sides are exactly the same n Is there a point on both the x-axis and the y- axis? ?- point(X, 0) = point(0, Y). X = 0 Y = 0 yes point(X,0) is a point somewhere on the x-axis point(0,Y) is a point somewhere on the y-axis We want them to be the same point, so use =
25
Matching n Get all the pieces to be the same –variable matches anything –atom matches same atom –term matches term with same functor and same arguments ?- bob(1, 2, X) = bob(X, 2, Y). X = 1 Y = 1 bob matches bob 1 matches X 2 matches 2 X (1) matches Y
26
Exercise n Do these match? What values do Vars get? –fred and F –fred and wilma –Thelma and louise(1,2,3) –tom(a,b,c) and tommy(a,b,c) –seg(pt(X,1), pt(2,X)) and seg(Z, pt(U,3)) –a(X,Y,Y,Z) and a(1,2,Z,3)
27
Making It Match n seg(pt(X,1), pt(2,X)) n seg( Z, pt(U,3)) Z=pt(X,1) Z=pt(X,1) U=2 U=2 X=3 X=3 Z=pt(3,1) Z=pt(3,1)
28
Failing to Match n a(X,Y,Y,Z) n a(1,2,Z,3) X=1 Y=2 Y=2 Y=Z Y=Z Z=2 Z=2 Z=3 impossible: Z=2 Z=3 impossible: Z=2
29
Exercise n Don’t usually use =/2 –just let Prolog give variables a value ?- on_x_axis(P), vertical( seg( point(3,4), P ) ). P = point(3, 0) Yes n A point is on the x axis if its y coordinate is zero. Write a fact to express that. on_x_axis( … ).
30
Equals is Not Math n Beginners often do this: ?- X = 3 * 17 – 12 * 5. X = 3*17 – 12*5 yes n It didn’t do the math! n Equality is matching –Math is not (just) matching
31
So What About Math n Use the is/2 predicate (infix syntax, like =) ?- X is 3 * 17 – 12 * 5. X = –9 yes n Can use all usual math operations and functions –Multiply with *, divide /, power ^ or **
32
Calculating Distance n Calculate distance between points distance(point(X1,Y1), point(X2,Y2), D) :- D is sqrt((X1–X2)^2 + (Y1–Y2)^2). n Will calculate a distance ?- distance(point(0,0), point(3,4), D). D = 5 yes
33
(Not) Calculating Points n Will not calculate a point to fit a distance ?- distance(point(0,0), point(3,Y), 5). ERROR: Arguments are not sufficiently instantiated ^ Exception: (7) … n Type an n on the exception line –Prolog says “no debug” and returns to prompt –Couldn’t calculate D because didn’t know Y Note: SWI-Prolog throws exceptions
34
“Extra-Logical” Predicates n Some predicates don’t commute –Y = 5, X is Y + 1 fine –X is Y + 1, Y = 5 no good n Since logical AND is commutative, Prolog’s comma is not quite the same –Prolog has a flow of control –It is a programming language, after all
35
Aside: Constraints n Some Prologs allow under-specified math –and other under-specified stuff as well n “Freeze” a computation until it’s instantiated “enough” –{X is Y + 1}, Y = 5 fine in BNR Prolog –Waits until it knows Y before it calculates X –AKA “constraint” on X
36
Aside: Logical Math n Some Prologs can do logical math –give two arguments… –…it fills in the third ?- sum( 5, X, 12 ), product( X, Y, 63 ). X = 7 Y = 9 Yes
37
Atoms, Terms & Lists n Atom –indivisible item n Term –compound item n List –sequence of items –(or of items & lists) –lots of useful built-in predicates for these
38
Lists n List = sequence of values –[bob, brian, cathy, mark, david, loretta] –[1, 2, 3, 4, 5] –[birds(4, calling), hens(3, french), doves(2, turtle), partridge(1, in(tree(1,pear)))] –[X, Y, Z, Z, Y] –[1, brian, doves(2, turtle), Z]
39
Representations of Lists n Written in [square] brackets –Note: (parentheses), [brackets], {braces} –mean different things in computer languages n Elements, separated, by, commas n Each element can be anything –atom, term, variable, or list n Simplest list is the empty list: []
40
Facts with Lists n List can be an argument of a fact –or of any term % family(Husband, Wife, Children) family(mark, di, [alex, zachary]). % isa_polygon( P ) isa_polygon( poly([P1, P2, P3 | PMore]) ) :- are_points([P1, P2, P3 | PMore]).
41
Next Time n More on lists n Sections 3.1 & 3.2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.