This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Click to edit Master subtitle style Chapter Twenty Two Modern Programming Languages, 2nd ed. A Third Look At Prolog Chapter Twenty Two Modern Programming Languages, 2nd ed. 11 Chapter Twenty Two Modern Programming Languages, 2nd ed. Outline ■ Numeric computation in Prolog ■ Problem space search – Knapsack – 8queens ■ Farewell to Prolog Chapter Twenty Two Modern Programming Languages, 2nd ed. 22 Chapter Twenty Two Modern Programming Languages, 2nd ed. Unevaluated Terms ■ Prolog operators allow terms to be written more concisely, but are not evaluated ■ These are all the same Prolog term: ■ That term does not unify with 7 Chapter Twenty Two Modern Programming Languages, 2nd ed. 33 +(1,*(2,3)) 1+ *(2,3) +(1,2*3) (1+(2*3)) 1+2*3 Chapter Twenty Two Modern Programming Languages, 2nd ed. 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. 44 ? X is 1+2*3. X = 7. Chapter Twenty Two Modern Programming Languages, 2nd ed. Instantiation Is Required Chapter Twenty Two Modern Programming Languages, 2nd ed. 55 ? 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. Chapter Twenty Two Modern Programming Languages, 2nd ed. Evaluable Predicates ■ For X is Y , the predicates that appear in Y have to be evaluable predicates ■ This includes things like the predefined operators + ,  , * and / ■ There are also other predefined evaluable predicates, like abs(Z) and sqrt(Z) Chapter Twenty Two Modern Programming Languages, 2nd ed. 66 Chapter Twenty Two Modern Programming Languages, 2nd ed. Real Values And Integers Chapter Twenty Two Modern Programming Languages, 2nd ed. 77 ? 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.0 would fail. Chapter Twenty Two Modern Programming Languages, 2nd ed. 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. 88 Chapter Twenty Two Modern Programming Languages, 2nd ed. Comparisons Chapter Twenty Two Modern Programming Languages, 2nd ed....
View
Full Document
 Spring '11
 Hamlen
 Modern Programming

Click to edit the document details