THE COUNTDOWN PROBLEM Graham Hutton University of Nottingham.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CALCULATING AN EXCEPTIONAL MACHINE Graham Hutton and Joel Wright University of Nottingham.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Number Theory and Cryptography
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Synergy: A New Algorithm for Property Checking
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
CSE 830: Design and Theory of Algorithms
Boolean Unification EE219B Presented by: Jason Shamberger March 1, 2000.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
0 PROGRAMMING IN HASKELL Chapter 11 - The Countdown Problem.
Describing Syntax and Semantics
SOLVING SUDOKU Graham Hutton University of Nottingham (with thanks to Richard Bird)
LIAL HORNSBY SCHNEIDER
Induction and recursion
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Genetic Algorithm.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
TH EDITION Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 1 Equations and Inequalities Copyright © 2013, 2009, 2005 Pearson Education,
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
Analysis of Algorithms
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
CSE Winter 2008 Introduction to Program Verification January 31 proofs through simplification.
Chapter 2 Real Numbers and algebraic expressions ©2002 by R. Villar All Rights Reserved Re-engineered by Mistah Flynn 2015.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Using Sequential Containers Lecture 8 Hartmut Kaiser
What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
String is a synonym for the type [Char].
Algorithms and Problem Solving
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Mathematical Induction Recursion
PROGRAMMING IN HASKELL
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
Copyright © 2017, 2013, 2009 Pearson Education, Inc.
PROGRAMMING IN HASKELL
Applied Discrete Mathematics Week 9: Integer Properties
PROGRAMMING IN HASKELL
Algorithms and Problem Solving
Verifying a compiler for a simple language with exceptions (MPC 04).
This Lecture Substitution model
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Recursive Thinking.
Presentation transcript:

THE COUNTDOWN PROBLEM Graham Hutton University of Nottingham

1 What Is Countdown? zA popular quiz programme on British television that has been running for almost 20 years. zBased upon an original French version called "Des Chiffres et Des Lettres". zIncludes a numbers game that we shall refer to as the countdown problem.

2 Example Using the numbers and the arithmetic operators  construct an expression whose value is

3 Rules zAll the numbers, including intermediate results, must be integers greater than zero. zEach of the source numbers can be used at most once when constructing the expression. zWe abstract from other rules that are adopted on television for pragmatic reasons.

4 For our example, one possible solution is zThere are 780 solutions for this example. zChanging the target number to gives an example that has no solutions. Notes: 831 (25-10)  (50+1) 765 =

5 Evaluating Expressions Operators: data Op = Add | Sub | Mul | Div Apply an operator: apply :: Op  Int  Int  Int apply Add x y = x + y apply Sub x y = x - y apply Mul x y = x * y apply Div x y = x `div` y

6 Decide if the result of applying an operator to two integers greater than zero is another such: valid :: Op  Int  Int  Bool valid Add _ _ = True valid Sub x y = x > y valid Mul _ _ = True valid Div x y = x `mod` y == 0 Expressions: data Expr = Val Int | App Op Expr Expr

7 eval :: Expr  [Int] eval (Val n) = [n | n > 0] eval (App o l r) = [apply o x y | x  eval l, y  eval r, valid o x y] Return the overall value of an expression, provided that it is an integer greater than zero: Either succeeds and returns a singleton list, or fails and returns the empty list.

8 Specifying The Problem Return a list of all possible ways of selecting zero or more elements from a list: subbags :: [a]  [[a]] For example: > subbags [1,2] [[],[1],[2],[3],[1,2],[2,1]]

9 Return a list of all the values in an expression: values :: Expr  [Int] values (Val n) = [n] values (App _ l r) = values l ++ values r Decide if an expression is a solution for a given list of source numbers and a target number: solution :: Expr  [Int]  Int  Bool solution e ns n = elem (values e) (subbags ns) && eval e == [n]

10 Brute Force Implementation Return a list of all possible ways of splitting a list into two non-empty parts: nesplit :: [a]  [([a],[a])] For example: > nesplit [1,2,3,4] [([1],[2,3,4]),([1,2],[3,4]),([1,2,3],[4])]

11 Return a list of all possible expressions whose values are precisely a given list of numbers: exprs :: [Int]  [Expr] exprs [] = [] exprs [n] = [Val n] exprs ns = [e | (ls,rs)  nesplit ns, l  exprs ls, r  exprs rs, e  combine l r] The key function in this lecture.

12 combine :: Expr  Expr  [Expr] combine l r = [App o l r | o  [Add,Sub,Mul,Div]] Combine two expressions using each operator: solutions :: [Int]  Int  [Expr] solutions ns n = [e | ns'  subbags ns, e  exprs ns', eval e == [n]] Return a list of all possible expressions that solve an instance of the countdown problem:

13 Correctness Theorem Our implementation returns all the expressions that satisfy our specification of the problem: elem e (solutions ns n) solution e ns n

14 How Fast Is It? System: Compiler: Example: One solution: All solutions: solutions [1,3,7,10,25,50] 765 1GHz Pentium-III laptop GHC version seconds seconds

15 zMany of the expressions that are considered will typically be invalid - fail to evaluate. zFor our example, only around 5 million of the 33 million possible expressions are valid. zCombining generation with evaluation would allow earlier rejection of invalid expressions. Can We Do Better?

16 results :: [Int]  [Result] results ns = [(e,n) | e  exprs ns, n  eval e] type Result = (Expr,Int) Valid expressions and their values: Specification of a function that fuses together the generation and evaluation of expressions: Applying Program Fusion

17 results [] = [] results [n] = [(Val n,n) | n > 0] results ns = [res | (ls,rs)  nesplit ns, lx  results ls, ry  results rs, res  combine' lx ry] We can now calculate an implementation: combine' :: Result  Result  [Result] where

18 solutions' :: [Int]  Int  [Expr] solutions' ns n = [e | ns'  subbags ns, (e,m)  results ns', m == n] Return a list of all possible expressions that solve an instance of the countdown problem: Correctness Theorem: solutions'solutions =

19 How Fast Is It Now? Example: One solution: All solutions: solutions' [1,3,7,10,25,50] seconds 5.76 seconds Around 10 times faster. Around 20 times faster.

20 zMany expressions will be essentially the same using simple arithmetic properties, such as: zExploiting such properties would considerably reduce the search and solution spaces. Can We Do Better? x  yy  x x  1 x = =

21 Exploiting Properties Strengthening the valid predicate to take account of commutativity and identity properties: valid :: Op  Int  Int  Bool valid Add x y = True valid Sub x y = x > y valid Mul x y = True valid Div x y = x `mod` y == 0 x  y x  y && x  1 x  y && x  1 && y  1 x  y && y  1

22 Using this new predicate gives a new version of our specification of the countdown problem. zThe new specification is sound with respect to our original specification. zIt is also complete up to equivalence of expr- essions under the exploited properties. Notes:

23 Using the new predicate also gives a new version of our implementation, written as solutions''. zThe new implementation requires no new proof of correctness, because none of our previous proofs depend upon the definition of valid. zHence our previous correctness results still hold under changes to this predicate. Notes:

24 How Fast Is It Now? Example: Valid: Solutions: solutions'' [1,3,7,10,25,50] ,000 expressions 49 expressions Around 20 times less. Around 16 times less.

25 One solution: All solutions: 0.04 seconds 0.86 seconds Around 2 times faster. Around 7 times faster. More generally, our program usually produces a solution to problems from the television show in an instant, and all solutions in under a second.

26 Is Carol Now Redundant? Vorderman said of the computer program: "It's a nice idea, but thankfully the bosses of Countdown don't want to do away with me and would prefer to keep it human against human!" How the computer program would handle a makeover show, or dressing up for an awards ceremony remains to be seen. (London Evening Standard, 1/11/2001)

27 Further Work zUsing memoisation or tabulation techniques to avoiding repeated computations. zFurther reducing the search and solution spaces by exploiting extra arithmetic properties. zConstructing an algorithm in which expressions are generated from the bottom-up.