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

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
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).
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to Sets. A set is just a collection of stuff But the stuff must be written inside curly braces Each item in the curly braces is separated.
Introduction to Python
Chapter 2 Writing Simple Programs
JavaScript, Third Edition
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Relations And Functions. Objective All students will be able to determine whether a relation is a function and identify the domain and range of a function.
Advanced Math Chapter P
Formal Models of Computation Part II The Logic Model
LING 388: Language and Computers Sandiway Fong Lecture 4.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Computer Science 101 Introduction to Programming.
Relations And Functions. A relation from non empty set A to a non empty set B is a subset of cartesian product of A x B. This is a relation The domain.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Input, Output, and Processing
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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 Facts, Questions & Rules Atoms & Variables.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Data Abstraction Chapter 6. Birthday Information n Suppose we want to represent information about people –perhaps for a family tree –name & date of birth,
REVIEW A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x values.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Relations And Functions. A relation is a set of ordered pairs {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Warm Up What algebraic equation shows the sentence four plus a number divided by six is equal to the product of twelve and the same number?
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Relations And Functions © 2002 by Shawna Haider. A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
REVIEW A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x values.
Section 16.5, 16.6 plus other references
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Topics Designing a Program Input, Processing, and Output
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
ICS103 Programming in C Lecture 3: Introduction to C (2)
Data types and variables
String and Lists Dr. José M. Reyes Álamo.
Chapter Two: Syntax and Meaning of Prolog Programs
Topics Designing a Program Input, Processing, and Output
Chapter 2 Syntax and meaning of prolog programs
For loops Taken from notes by Dr. Neil Moore
Topics Designing a Program Input, Processing, and Output
Relations And Functions.
Relations And Functions.
Unit 3: Variables in Java
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Introduction to Prolog Terms & Matching Math

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

Atoms n Identifiers starting with lower-case letter –mark, di, bob n Numbers: integers & floating point –7, 1208, -4, , 6.02e23 n Sequences of special characters –=, :-, ->, *->, –=, :-, ->, *->, n Single-quoted strings –‘Tom’, ‘23-skidoo’, ‘I am not a number!’, ‘3’

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

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 % born(Person, DateOfBirth) born(alex, date(1994, apr, 24)). born(zachary, date(2001, dec, 4)).

Exercise n Write born/2 for the members of your family born(alex, date(1994, apr, 24)).

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?

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

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

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

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, _, _)).

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

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

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) :- ….

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

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

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

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

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

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

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

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

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

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 =

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

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)

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)

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

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( … ).

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

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 **

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

(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

“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

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

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

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

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]

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: []

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

Next Time n More on lists n Sections 3.1 & 3.2