Simon Peyton Jones (Microsoft Research) Max Bolingbroke (University of Cambridge)

Similar documents
Termination Combinators Forever

Week 5 Tutorial Structural Induction

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Static Contract Checking for Haskell

Extended Static Checking for Haskell (ESC/Haskell)

Shell CSCE 314 TAMU. Functions continued

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Lecture 6: Sequential Sorting

3.7 Denotational Semantics

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

Advanced features of Functional Programming (Haskell)

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

Simon Peyton Jones Microsoft Research August 2013

CSCE 314 Programming Languages

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Foundations of Computation

Shell CSCE 314 TAMU. Higher Order Functions

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Foundations of Computer Science Spring Mathematical Preliminaries

CS422 - Programming Language Design

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Type Processing by Constraint Reasoning

Combining Static and Dynamic Contract Checking for Curry

Symmetry in Type Theory

CSci 450: Org. of Programming Languages Overloading and Type Classes

Induction for Data Types

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

More fun with recursion

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

We show that the composite function h, h(x) = g(f(x)) is a reduction h: A m C.

A general introduction to Functional Programming using Haskell

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

CSc 372 Comparative Programming Languages

Faster Haskell. Neil Mitchell

Programming with Universes, Generically

INTRODUCTION TO HASKELL

From Types to Contracts

CS558 Programming Languages

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

1 Problem Description

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS153: Compilers Lecture 14: Type Checking

GADTs. GADTs in Haskell

Type families and data kinds

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

CSCI-GA Scripting Languages

CMSC th Lecture: Graph Theory: Trees.

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

An introduction introduction to functional functional programming programming using usin Haskell

Shared Subtypes. Subtyping Recursive Parameterized Algebraic Data Types

Advanced Functional Programming

Critical Analysis of Computer Science Methodology: Theory

2.8 Universal Turing Machines and the Halting Problem

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Topics in Software Testing

Lecture 3: Recursion; Structural Induction

Haskell Overview II (2A) Young Won Lim 8/9/16

6.854J / J Advanced Algorithms Fall 2008

n = 1 What problems are interesting when n is just 1?

List Functions, and Higher-Order Functions

9.5 Equivalence Relations

6.852 Lecture 17. Atomic objects Reading: Chapter 13 Next lecture: Atomic snapshot, read/write register

Local Verification of Global Invariants in

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Once Upon a Polymorphic Type

Certified Programming with Dependent Types

Programming Languages Fall 2013

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Monads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS 161 Computer Security

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Haskell Types, Classes, and Functions, Currying, and Polymorphism

-t:ma. u V:. 1! *VV; jf >*4: t i f n - v i ' ONABI E :VISITd:Rs ; 8 A EASTBOURNE, SAT. 5? -tesiste- COLLEGE. D ressm aker.

CS205: Scalable Software Systems

The design of a programming language for provably correct programs: success and failure

(l) Represent each of the sets A, B and C using bit strings. Then, use bit string representation and bitwise logical operations to find

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

Propositional Logic. Part I

An introduction to functional programming. July 23, 2010

Programming Languages

The Substitution Model

Power Set of a set and Relations

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

CS 457/557: Functional Languages

Transcription:

Simon Peyton Jones (Microsoft Research) Max Bolingbroke (University of Cambridge) 2011

...in compilers...in supercompilers...in theorem provers It s a useful black box. But it should be modularly separated from the rest of your compiler/theorem prover/whatever (The typical reality is otherwise.)

Online termination detection Given a sequence of values, x0, x1, x2......presented one by one......yell stop if it looks as if the sequence might be diverging Guarantee never to let through an infinite sequence Delay stop as long as possible Values includes pairs, strings, trees...

data TTest a testlist :: TTest a -> [a] -> Bool Postpone: where do TTests come from? Note: testlist is inherently inefficient for the present one at a time situation

data History a inithistory :: TTest a -> History a data TestResult a = Stop Continue (History a) test :: History a -> a -> TestResult a Intuitively the History accumulates (some abstraction of) the values seem so far

The goal: a library that makes it easy to construct values of type TTest a, that Are definitely sound: they do not admit infinite sequences Are lenient as possible: they do not blow the whistle too soon

intt :: TTest Int boolt :: TTest Bool pairt :: TTest a -> TTest b -> TTest (a, b) eithert :: TTest a -> TTest b -> TTest (Either a b) wrapt :: (a -> b) -> TTest b -> TTest a Just the usual type-directed combinator library

How do we implement a TTest? Find a strictly-decreasing measure bounded below. This is VERY INCONVENIENT in many cases. Think about a sequence of syntax trees. Well-studied problem, standard approach: use a well-quasi order (WQO).

Definition A transitive binary relation is a WQO iff For any infinite sequence x 0, x 1, x 2... there exists i<j st x i x j Theorem: every WQO is reflexive

newtype TTest a = TT (a -> a -> Bool) data History a = H (a->a->bool) [a] inithistory :: TTest a -> History a inithistory (TT wqo) = H wqo [] test :: History a -> a -> TestResult a test (H wqo vs) v any (`wqo` v) vs = Stop otherwise = Continue (H wqo (v:vs)) New goal: a (trusted) library that helps you to define (sparse) WQOs, that really are WQOs

finitet :: Finite a => TTest a finitet = TT (==) class Eq a => Finite a where elements :: [a] Is (==) a WQO on finite sets? Yes. Odd; we don t use the methods of Finite Instead, Finite is really a proof obligation: There are only a finite number of elements of a (==) is reflexive

eithert:: TTest a -> TTest b -> TTest (Either a b) eithert (TT wqo_a) (TT wqo_b) = TT wqo where (Left x) `wqo` (Left y) = x `wqo_a` y (Right x) `wqo` (Right y) = x `wqo_b` y _ `wqo` _ = False Is this a WQO? Why?

pairt:: TTest a -> TTest b -> TTest (a,b) pairt (TT wqo_a) (TT wqo_b) = TT wqo where (x1,x2) `wqo` (y1,y2) =...

pairt:: TTest a -> TTest b -> TTest (a,b) pairt (TT wqo_a) (TT wqo_b) = TT wqo where (x1,x2) `wqo` (y1,y2) = x1 `wqo_a` y1 && x2 `wqo_b` y2 But is this a WQO? For any infinite sequence (x0,y0), (x1,y1),... can we be sure there is an i<j, st xi xj, and yi yj? Yes, and the proof is both simple and beautiful

Theorem. If ( ) is a WQO, then for any infinite sequence x 0, x 1, x 2,... there is a finite N such that for any i>n there is a j>i such that x i x j That is, after some point N, every x i is a later x j Proof: Consider {x i j>i. x i x j } Corollary: every infinite sequence has a chain x i1 x i2 x i3...

wrapt:: (b->a) -> TTest a -> TTest b wrapt f (TT wqo_a) = TT wqo_b where x `wqo_b` y = f x `wqo_a` f y instance CoFunctor TTest where cofmap = wrapt Exercise: modify the implementation of TTest and History to avoid the repeated reapplication of f.

unwrap :: [a] -> Either () (a, [a]) unwrap [] = Left () unwrap (x:xs) = Right (x,xs) listt :: forall a. TTest a -> TTest [a] listt telt = tlist where tlist :: TTest [a] tlist = cofmap unwrap $ eithert finitet (pairt telt tlist) The types are right We are only using library combinators Does it work?

unwrap :: [a] -> Either () (a, [a]) unwrap [] = Left () unwrap (x:xs) = Right (x,xs) listt :: forall a. TTest a -> TTest [a] listt telt = tlist where tlist :: TTest [a] tlist = cofmap unwrap $ eithert finitet (pairt telt tlist) Consider [], [1], [1,1], [1,1,1], [1,1,1,1],... An infinite sequence... accepted! What has gone wrong?

We assumed that tlist was WQO when proving that it is a WQO! tlist :: TTest [a] tlist = cofmap unwrap $ eithert finitet (pairt telt tlist) Sort-of solution: make the combinators strict, so tlist is bottom...but we still want a termination checker for lists!

wqol :: WQO a -> [a] -> [a] -> Bool wqol we [] ys = True wqol we (x:xs) [] = False wqol we (x:xs) (y:ys) = (x `we` y && wqol we xs ys) wqol we (x:xs) ys This actually is a WQO The proof is not obvious, at all Q1: find an elegant proof Couple : See if they match at the root Dive : See if the first arg matches inside the recursive component of the second arg

Function to get the recursive children of a t-value rect :: (t -> [t]) -> TTest t -> TTest t A couple tester: match at the root listt :: TTest t -> TTest [t] listt telt = tlist where tlist :: TTest [a] tlist = rect kids $ cofmap unwrap $ eithert finitet (pairt telt tlist) kids [] = [] kids (x:xs) = [xs]

Function to get the recursive children of a t-value rect :: (t -> [t]) -> TTest t A couple tester: -> TTest t match at the root rect kids ~(TT wqo_top)= TT wqo where x `wqo` y = x `wqo_top` y) any (x `wqo`) (kids y)

Function to get the recursive children of a t-value rect :: (t -> [t]) -> TTest t A couple tester: -> TTest t match at the root rect kids ~(TT wqo_top)= TT wqo where x `wqo` y = x `wqo_top` y) any (x `wqo`) (kids y) Q2: is this the best formulation? Q3: what is the proof obligation for kids Q4: solve nasty interaction with cofmap Q5: Elucidate relationship to R+

A combinator library for online termination testing A useful black box, never previously abstracted out as such Encapsulates tricky theorems inside a nice, compositional interface