Understanding Prolog: Instantiation, Equalities, and Search, Slides of Advanced Computer Programming

An excerpt from 'modern programming languages' 2nd edition, focusing on prolog. It covers topics such as instantiation, real values and integers, comparisons, equalities, and the use of predicates like is, abs, sqrt, gcd, and findall. The text also includes examples of prolog code for sum, fact, and subseq, as well as an explanation of how prolog handles search and problem space solutions.

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani šŸ‡®šŸ‡³

4.5

(2)

83 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Third Look At Prolog
Chapter Twenty-Two Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Understanding Prolog: Instantiation, Equalities, and Search and more Slides Advanced Computer Programming in PDF only on Docsity!

A Third Look At Prolog

Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Outline

 Numeric computation in Prolog

 Problem space search

– Knapsack

– 8-queens

 Farewell to Prolog

Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Evaluating Expressions

 The predefined predicate is can be used to

evaluate a term that is a numeric expression

 is(X,Y) evaluates the term Y and unifies

X with the resulting atom

 It is usually used as an operator

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 4

?- X is 1+2*3.

X = 7.

Docsity.com

Instantiation Is Required

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 5

?- Y=X+2, X=1.

Y = 1+2,

X = 1.

?- Y is X+2, X=1. ERROR: is/2: Arguments are not sufficiently instantiated ?- X=1, Y is X+2. X = 1, Y = 3.

Docsity.com

Real Values And Integers

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 7

?- X is 1/2. X = 0.5.

?- X is 1.0/2.0. X = 0.5.

?- X is 2/1. X = 2.

?- X is 2.0/1.0. X = 2.0.

There are two numeric types:

integer and real.

Most of the evaluable

predicates are overloaded for

all combinations.

Prolog is dynamically typed;

the types are used at runtime

to resolve the overloading.

But note that the goal 2=2.

would fail.

Docsity.com

Comparisons

 Numeric comparison operators:

<, >, =<, >=, =:=, ==

 To solve a numeric comparison goal, Prolog

evaluates both sides and compares the

results numerically

 So both sides must be fully instantiated

Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Equalities In Prolog

 We have used three different but related

equality operators:

– X is Y evaluates Y and unifies the result with X:

3 is 1+2 succeeds, but 1+2 is 3 fails

– X = Y unifies X and Y, with no evaluation: both

3 = 1+2 and 1+2 = 3 fail

– X =:= Y evaluates both and compares: both

3 =:= 1+2 and 1+2 =:= 3 succeed

(and so does 1 =:= 1.0)

 Any evaluated term must be fully instantiated

Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 10

Example: mylength

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 11

mylength([],0). mylength([_|Tail], Len) :- mylength(Tail, TailLen), Len is TailLen + 1.

?- mylength([a,b,c],X). X = 3.

?- mylength(X,3). X = [_G266, _G269, _G272].

Docsity.com

Example: sum

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 13

sum([],0). sum([Head|Tail],X) :- sum(Tail,TailSum), X is Head + TailSum.

?- sum([1,2,3],X). X = 6.

?- sum([1,2.5,3],X). X = 6.5.

Docsity.com

Example: gcd

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 14

gcd(X,Y,Z) :- X =:= Y, Z is X. gcd(X,Y,Denom) :- X < Y, NewY is Y - X, gcd(X,NewY,Denom). gcd(X,Y,Denom) :- X > Y, NewX is X - Y, gcd(NewX,Y,Denom).

Note: not just

gcd(X,X,X)

Docsity.com

Cutting Wasted Backtracking

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 16

gcd(X,Y,Z) :- X =:= Y, Z is X, !. gcd(X,Y,Denom) :- X < Y, NewY is Y - X, gcd(X,NewY,Denom), !. gcd(X,Y,Denom) :- X > Y, NewX is X - Y, gcd(NewX,Y,Denom).

If this rule succeeds, there’s

no point in trying the others

Same here.

With those cuts, this test is

unnecessary (but we’ll leave

it there).

Docsity.com

Example: fact

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 17

fact(X,1) :- X =:= 1, !. fact(X,Fact) :- X > 1, NewX is X - 1, fact(NewX,NF), Fact is X * NF.

?- fact(5,X). X = 120.

?- fact(20,X). X = 2432902008176640000.

?- fact(-2,X). false.

Docsity.com

Problem Space Search

 Prolog’s strength is (obviously) not numeric

computation

 The kinds of problems it does best on are

those that involve problem space search

– You give a logical definition of the solution

– Then let Prolog find it

Chapter Twenty-Two Modern Programming Languages, 2nd ed. (^) Docsity.com 19

The Knapsack Problem

 You are packing for a camping trip

 Your pantry contains these items:

 Your knapsack holds 4 kg.

 What choice <= 4 kg. maximizes calories?

Chapter Twenty-Two Modern Programming Languages, 2nd ed. 20

Item Weight in kilograms Calories bread 4 9200 pasta 2 4600 peanut butter 1 6700 baby food 3 6900

Docsity.com