CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Functional Design and Programming Lecture 11: Functional reasoning.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Sparkle A theorem prover for the functional language Clean Maarten de Mol University of Nijmegen February 2002.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Transformation and Analysis of Haskell Source Code Neil Mitchell 
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Compiling Functional Programs Mooly Sagiv Chapter 7
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
COMP313A Functional Programming (1)
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe.
First Order Haskell Neil Mitchell York University λ.
Sized Types Inference1 “Calculating Sized Types” by Wei-Ngan Chin and Siau-Cheng Khoo published in PEPM00 and HOSC01 Presented by Xu Na.
CATCH 1 : Case and Termination Checking for Haskell Neil Mitchell (supervised by Colin Runciman) 1 Name courtesy of Mike Dodds.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Hoogle Neil Mitchell. Haskell Types 101 isURI :: String -> Bool (||) :: Bool -> Bool -> Bool or :: [Bool] -> Bool id :: a ->
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Static Techniques for V&V. Hierarchy of V&V techniques Static Analysis V&V Dynamic Techniques Model Checking Simulation Symbolic Execution Testing Informal.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Haskell With Go Faster Stripes Neil Mitchell. Catch: Project Overview Catch checks that a Haskell program won’t raise a pattern match error head [] =
Simon Peyton Jones, Stephanie Weirich, Richard Eisenberg, Dimitrios Vytiniotis Microsoft Research University of Pennsylvania April 2016.
Conditional Expressions
Types CSCE 314 Spring 2016.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
COP4020 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Extended Static Checking for Haskell (ESC/Haskell)
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Static Contract Checking for Haskell
Presentation transcript:

CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)

The Aim Take a Haskell program Analyse it Prove statically that there are no “unsafe pattern matches” No additional user work Termination – not in 18 minutes!

Is this safe? risers [] = [] risers [x] = [[x]] risers (x:y:etc) = if x <= y then (x:s):ss else [x]:(s:ss) where (s:ss) = risers (y:etc)

Yes risers [] = [] risers [x] = [[x]] -– ((x:[]):[]) risers (x:y:etc) = if x <= y then (x:s):ss else [x]:(s:ss) where (s:ss) = risers (y:etc)

How does Catch work? Transform to reduced Haskell Apply transformations on reduced Haskell Generate a condition for case safety Propagate this condition Figure out if the precondition is True

Pattern Matches if x then f else g f (x:xs) = x f x = ys where (y:ys) let (a,b) = y in (b,a) f x | null x = [] | otherwise = tail x [x | Just x <- xs] f [x] = x do (x:xs) <- f y return xs \(a,b) -> a ++ b case x of [] -> True (a:b) -> a

Reduced Haskell Only simple case, functions, applications, constructors data [] = [] | (:) hd tl map f xs = case xs of [] -> [] (:) -> f xs.hd : map f xs.tl

Generating Reduced Haskell Fully automatic Uses Yhc’s Core language Yhc is a fork of nhc98 Specify –core or –corep to see it Some additional transformations Remove a few let’s By the end, reduced Haskell

Transformations About 8 are applied Reachability Eliminate dead code Arity raising Take out points free code odd = not. even Defunctionalisation [Reynolds 72] Remove all higher order functions

The Checker itself Operates on a simple first order language Uses constraints of the form: From the expression, if I follow any valid path, I get to one of the constructors

Constraints, intro by example head (x:xs) = x fromJust (Just x) = x foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs)

Constraints with paths mapHead x = case x of [] -> [] (:) -> head x.hd : mapHead x.tl ^ ^ …

Dealing with recursion Just keep expanding it x ^ x.a ^ x.aa ^ x.aaa ^ x.aaaa At a certain depth, give up x.aaaa -> x.aaa* Simplify after x ^ x.a ^ x.aa ^ x.aaa ^ x.aaa* = x.a*

Going back to Risers True Risers is safe

Other programs Soda (Word search) One minor tweak required Was safe already Adjoxo (XOX checker) One fix requried Was NOT safe before Improves code readability

State of play Have a working prototype Full Haskell 98 A number of Haskell 98 libraries Works on 1-2 page programs Still lots to do A bit slow in some cases Some programs don’t work yet

Conclusion CATCH is a practical tool for detecting pattern match errors Uses a constraint language to prove safety A release is coming soon (2 months)

Transformation rules

Yhc vs GHC Core GHC Core is: More complex (letrec’s, lambda’s) Lacks source position information Piles and piles of type information Slower to generate Harder to change GHC Less like the original code