CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.

Slides:



Advertisements
Similar presentations
Applications Computational LogicLecture 11 Michael Genesereth Spring 2004.
Advertisements

Artificial Intelligence: Natural Language and Prolog
Chapter 6 Writing a Program
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
How to Factor Quadratics of the Form
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Modular Arithmetic Several important cryptosystems make use of modular arithmetic. This is when the answer to a calculation is always in the range 0 –
Overview of Lecture Partitioning Evaluating the Null Hypothesis ANOVA
CS4026 Formal Models of Computation Running Haskell Programs – power.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Formal Models of Computation Part II The Logic Model
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
SQL: The Query Language Part 2
Solve Multi-step Equations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Order of Operations Lesson
Discrete Math Recurrence Relations 1.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Modern Programming Languages, 2nd ed.
ABC Technology Project
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
Hash Tables.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
Presented by: Tutorial Services The Math Center
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
1 User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their.
25 seconds left…...
Computing Transformations
Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.
18-Dec-14 Pruning. 2 Exponential growth How many leaves are there in a complete binary tree of depth N? This is easy to demonstrate: Count “going left”
Pointers and Arrays Chapter 12
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Testing Hypotheses About Proportions
Introduction to Recursion and Recursive Algorithms
1 Functions and Applications
Order of Operations And Real Number Operations
Bart Jansen 1.  Problem definition  Instance: Connected graph G, positive integer k  Question: Is there a spanning tree for G with at least k leaves?
Chapter 8 Improving the User Interface
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Ch. 11: Cantor’s Infinity!. N = {1, 2, 3, 4, 5, 6, …} “the natural numbers” Z = {…, –3, –2, –1, 0, 1, 2, 3, …} “the integers” Q = {all quotients “a/b”
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
For Friday No reading Prolog Handout 2. Homework.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Chapter 11 :: Logic Languages
Presentation transcript:

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut

formal models of computation 2 Arithmetic Arithmetic expressions trees like any other terms: Prolog does not treat arithmetic expressions differently, unless asked to do so. In order to evaluate arithmetic expressions we use the built-in is ?- X = 10 + (2 * 4). X = 10 + (2 * 4) ? yes ? - X * ?- X is 10 + (2 * 4). X = 18 ? yes ? -

formal models of computation 3 Arithmetic The syntax of the built-in is is: Variable is ArithmeticExpression For instance: Result is (10 * 7) NewRes is OldRes + 1 Variables that appear on the expression –Must be instantiated when expression is evaluated –Otherwise execution error! We can delay the evaluation until all variables have value: ?- Exp = A + B, A = 3, B = 4, Res is Exp. A = 3, B = 4, Exp = Res = 7

formal models of computation 4 Evaluation in Haskell and Prolog In Haskell, expressions are always evaluated if they contribute to the final result. Terms stand for their results. In Prolog, every term stands for itself. Evaluation only happens when explicitly invoked by a built-in like is. Prolog functors (even arithmetic ones) are like Haskell constructors (e.g. :)

formal models of computation 5 Arithmetic: an Example How can we find the size (no. elements) of a list? Simple recursive formulation: –The number of elements of the empty list is zero –The number of elements of a list [X|Xs] is one plus the number of elements of Xs. Predicate length with 2 arguments: –1 st argument is the list –2 nd argument is the size of the list For example: ?- length([a,b,c,d],Length). Length = 4 ? ?- length([1,[2,3],4],L). L = 3 ? ?-

formal models of computation 6 Arithmetic: an Example First attempt length(L,S):- L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] S is 1 + length(Xs,S). % its length is 1 plus length of tail length(L,S):- % S is the length of list L L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] S is 1 + length(Xs,S). % its length is 1 plus length of tail Careful! Predicate calls are true or false ! This expression does not make sense! length(L,S):- % S is the length of list L L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] S is 1 + length(Xs,S). % its length is 1 plus length of tail

formal models of computation 7 Arithmetic: an Example Second attempt (Can you see whats wrong?) length(L,S):- % S is the length of list L L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] S is 1 + SXs, % length S is 1 plus length of tail length(Xs,SXs). % get the length SXs of tail Xs length(L,S):- % S is the length of list L L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] S is 1 + SXs, % length S is 1 plus length of tail length(Xs,SXs). % get the length SXs of tail Xs Careful! When this expression is evaluated, the value of SXs wont yet exist!!

formal models of computation 8 Arithmetic: an Example Third attempt: Simplified version: length(L,S):- % S is the length of list L L = [], % if L is an empty list S = 0. % its length S is zero length(L,S):- % otherwise L = [X|Xs], % if L is a list [X|Xs] length(Xs,SXs), % get the length SXs of its tail Xs S is 1 + SXs. % add 1 to the length of its tail length([],0). % the empty list has length 0 length([_|Xs],S):- % a non-empty list [_|Xs] has size S length(Xs,SXs), % get the length SXs of its tail Xs S is 1 + SXs. % add 1 to the length of its tail length([],0). % the empty list has length 0 length([_|Xs],S):- % a non-empty list [_|Xs] has size S length(Xs,SXs), % get the length SXs of its tail Xs S is 1 + SXs. % add 1 to the length of its tail Anonymous variable, used as place holder. In this context, we dont care what the value of the element is – we just want to count them!!

formal models of computation 9 Arithmetic: Summary Arithmetic: via is built-in. Some operators: +, –, *, / (add, subtract, multiply, divide) // (integer division) mod (modulo) All variables on expression must have values, otherwise execution error!! Because of this restriction, we ought to bear in mind the order in which Prolog proves/executes the body of a clause (or a query).

formal models of computation 10 Failure-driven loops Prolog offers a built-in predicate fail which always fails: We can use this predicate to define a failure- driven loop, an alternative to recursion: ?- fail. no p(a). p(b). p(c). loopFail:- % loopFail succeeds if p(X), % we can prove p(X) and write(X), % write the value of X and nl, % skip a line. fail. % fail and BACKTRACK!! loopFail. % if no (more) answers, stop

formal models of computation 11 Failure-driven loops Execution: p(a). p(b). p(c). loopFail:- p(X), write(X), nl, fail. loopFail. ?- loopFail. loopFail:- p(X 1 ),write(X 1 ),nl,fail. loopFail:- p(X 1 ),write(X 1 ),nl,fail. loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /a} loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /a} ?- loopFail. a loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /a} Backtrack!! loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /a} Backtracking skips over built-ins!! loopFail:- p(X 1 ),write(X 1 ),nl,fail. loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /b} loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /b} loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /b} Backtrack!! loopFail:- p(X 1 ),write(X 1 ),nl,fail. loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /c} loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /c} loopFail:- p(X 1 ),write(X 1 ),nl,fail. {X 1 /c} Backtrack!! loopFail:- p(X 1 ),write(X 1 ),nl,fail. No more values for p(X) ! loopFail. ?- loopFail. a b ?- loopFail. a b c ?- loopFail. a b c yes ?-

formal models of computation 12 Controlling Backtracking via cuts (!) Prologs backtracking mechanism may, in some cases, lead to inefficiencies. We can control backtracking via the built-in !, called cut. The ! is used as an ordinary predicate, in the body of the clause or query – it always succeeds! However, the ! causes the execution to commit to the current clause and to the solutions (proofs) of the goals to its left. Example: p(A,B,C,D):- q(A), r(A,B), !, s(B,C),t(A,D).

formal models of computation 13 Controlling Backtracking via cuts (!) Example: p(a). p(b). p(c). q(b,2). q(c,3). s(2). s(3). r(Y):- p(X),q(X,Y),!,s(Y). ?- r(Ans). r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /a} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /a} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /a} Fail!!Backtrack… r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} ?- r(Ans). Ans = 2 ? ?- r(Ans). Ans = 2 ? ; Force backtrack… r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} No other proof for s(Y 1 ) ! r(Y 1 ):- p(X 1 ),q(X 1,Y 1 ),!,s(Y 1 ). {X 1 /b,Y 1 /2} Cannot backtrack over ! ?- r(Ans). Ans = 2 ? ; no ?- Force backtrack…

formal models of computation 14 Commitment in Haskell and Prolog In some ways, the Prolog cut (!) is similar in spirit to a Haskell guard (|) –Both cause a commitment to the current clause/case But there are differences as well: –Guards come before the critical tests, cuts come after –Cuts also commit to choices made in subgoals on the left Lets look at some ways in which the cut can be useful

formal models of computation 15 Avoiding unnecessary work with ! Example: Suppose –customer(X) picks out a customer from a database sorted (decreasing order) by amount of money spent; –eligiblePrize(X) checks if amount of money spent makes customer eligible to win a prize; –If the best (first) customer does not qualify, why let Prolog try all the other customers with less money spent? prize(X):- customer(X),eligiblePrize(X).

formal models of computation 16 Avoiding unnecessary work with ! Surely, this is a lot better: This new version saves unnecessary attempts! We can only add this cut because we know the first answer is the only one that is any good… prize(X):- customer(X),!,eligiblePrize(X).

formal models of computation 17 Avoiding unnecessary work with ! Another type of case: disjoint cases In Prolog: Lets try this: if X < 3 then Y = 0. if X 3 and X < 6 then Y = 1. if X 6 then Y = 2. ?- range(1,Y), Y > 2. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. ? ?- 0 > 2. Backtrack!

formal models of computation 18 Avoiding unnecessary work with ! Another (more concrete) case: In Prolog: Lets try this: if X < 3 then Y = 0. if X 3 and X < 6 then Y = 1. if X 6 then Y = 2. ?- range(1,Y), Y > 2. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. ?- 1>=3, 1 2. Backtrack!

formal models of computation 19 Avoiding unnecessary work with ! Another (more concrete) case: In Prolog: Lets try this: if X < 3 then Y = 0. if X 3 and X < 6 then Y = 1. if X 6 then Y = 2. ?- range(1,Y), Y > 2. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. range(X,0):- X < 3. range(X,1):- X >= 3, X < 6. range(X,2):- X >= 6. ?- 1 >= 6, 2 > 2. Backtrack! ?- range(1,Y), Y > 2. no Fail!!

formal models of computation 20 Avoiding unnecessary work with ! The values of Y are mutually exclusive –There is no point in trying different clauses!! –Lets add cuts to reflect this: The previous query is: ?- range(1,Y), Y > 2. range(X,0):- X < 3,!. range(X,1):- X >= 3, X < 6,!. range(X,2):- X >= 6. % no need to add a cut here!! range(X,0):- X < 3,!. range(X,1):- X >= 3, X < 6,! range(X,2):- X >= 6. ? ?- 0 > 2. Backtrack! ?- range(1,Y), Y > 2. no Fail!!

formal models of computation 21 Cuts can be necessary: Avoiding bad loops Example: build a list with decreasing numbers countDown(0,[]). countDown(N,[N|Ns]):- NN is N – 1, countDown(NN,Ns). ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ; ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ; ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ; ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ; LOOP!! Cause: Being asked for more solutions, PROLOG will match countDown(0,[]) with the recursive rule. This will force it to compute countDown(-1,Ns), and so on until the stack overflows.

formal models of computation 22 Avoiding bad loops with cuts Fixing the problem with a !: countDown(0,[]):- !. countDown(N,[N|Ns]):- NN is N – 1, countDown(NN,Ns). The new countDown on the left still has a problem: if we try ?- countDown(-1,L). The program would loop forever (actually, a stack overflow will stop it). Can you fix it? Incidentally… ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ?- countDown(5,Nos). Nos = [5,4,3,2,1] ? ; no ?-

formal models of computation 23 When to use cuts (!) Cuts arent always necessary – dont add them just in case!! There is usually no reason to add more than one ! on one clause. There is no easy way to tell when to use cuts: –You have seen some cases as guidelines… –Is the first answer enough? Do we need all answers? –Is there a risk of an accidental loop? –Will backtracking allow clauses to be wrongly used? Ultimately, we (programmers) should be able to decide where/if to add cuts…

formal models of computation 24 Cut-Fail Combination We can combine ! and fail to represent exceptions: –First clause defines when different fails –Second clause is an else, where all other cases are dealt; –Notice the anonymous variables – they are not the same!! different(X,X):- !,fail. different(_,_).

formal models of computation 25 Negation as Failure The cut-fail combination allows us to define a kind of logical negation: –not(Goal) is true if Goal is false –Built-in call(Goal) attempts to prove Goal For the in-house logicians: –not(G) means G cannot be proved –not(G) does not mean G can be proved false –Can you tell these apart? This is called negation as failure –It is OK if we adopt the closed world assumption not(G):- call(G),!,fail. % if G holds, then fail not(_). % otherwise not(G) holds

formal models of computation 26 Declarative vs. Procedural Meanings A Prolog program has two meanings –Declarative: the logical relationships (i.e. the results) –Procedural: the results and how they were computed Good Prolog programs: –Exploit the declarative side of logics (relationships) –Take into account procedural aspects (efficiency). Declarative programs: –Allow for multiple answers and multiple uses of a predicate (e.g., the member predicate) Procedural programs: –Single answers, single use of predicates

formal models of computation 27 Control in Logic Programming In logic programming, you have to think about control as well as logic: –To appropriately time arithmetic –To avoid loops –To avoid unnecessary backtracking The cut is an explicit control mechanism. The ordering of clauses and goals within clauses is less explicit but just as important But you also need to think about the logic!