GADTs meet their match George Karachalias(Ghent University, Belgium) Tom Schrijvers (KU Leuven, Belgium) Dimitrios Vytiniotis(Microsoft Research Cambridge,

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Functional Programming Lecture 10 - type checking.
Static Contract Checking for Haskell Dana N. Xu University of Cambridge Ph.D. Supervisor: Simon Peyton Jones Microsoft Research Cambridge.
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.
Let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Algebra 1: Solving Equations with variables on BOTH sides.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
1 Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Joint work with Simon Peyton Jones Microsoft Research.
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.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
1 Haskell Kevin Atkinson John Simmons. 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
Warm-up: Write the inverse of the relation shown by the mapping below (-1, -2) (-1, 3) (0, 3) (1, 5) (0, 8)
Replacement Set 2.2 Replacement Sets I can: Use an equation to determine the range for a given domain 2.2 – FUN FUNCTIONS Find yif you know x.
Simon Peyton Jones, Stephanie Weirich, Richard Eisenberg, Dimitrios Vytiniotis Microsoft Research University of Pennsylvania April 2016.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Functional Dependencies in System FC
Advanced Functional Programming 2010
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Functional Programming
Type Checking and Type Inference
Laziness and Infinite Datastructures
Conditional Expressions
Programming Languages
PROGRAMMING IN HASKELL
ML: a quasi-functional language with strong typing
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
smallest number of inserts/deletes to turn arg#1 into arg#2
Functional Programming Lecture 2 - Functions
PROGRAMMING IN HASKELL
Solving Equations by Factoring and Problem Solving
Microsoft Research University of Pennsylvania
Programming Languages
Extended Static Checking for Haskell (ESC/Haskell)
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Solve and Graph 2x + 3 < 9 2x + 3 = x = x = 3
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lazy Programming Lazy evaluation:
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming Lecture 2 - Functions
6.8 Solving Equations by Factoring
Solving Systems by Substitution
PROGRAMMING IN HASKELL
Solving Equations by Factoring
Variables.
Identities.
Static Contract Checking for Haskell
Presentation transcript:

GADTs meet their match George Karachalias(Ghent University, Belgium) Tom Schrijvers (KU Leuven, Belgium) Dimitrios Vytiniotis(Microsoft Research Cambridge, UK) Simon Peyton Jones(Microsoft Research Cambridge, UK)

Checking Pattern Matching Exhaustiveness  Does a match cover all cases? Redundancy  Do all equations have an accessible right hand side? Laziness  How does left-to-right evaluation order affect the above? Reasoning about more exotic features? 2

PATTERN MATCHING 3

Checking Pattern Matching zip :: [a] -> [b] -> [(a,b)] zip [] [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys 4

Checking Pattern Matching zip :: [a] -> [b] -> [(a,b)] zip [] [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys Prelude> zip [] [True] *** Exception: :8:7-59: Non-exhaustive patterns in function zip 5

Checking Pattern Matching zip :: [a] -> [b] -> [(a,b)] zip [] [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys 6

Checking Pattern Matching zip :: [a] -> [b] -> [(a,b)] zip [] [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys :12:7: Warning: Pattern match(es) are non-exhaustive In an equation for `zip': Patterns not matched: [] (_ : _) (_ : _) [] 7

Pattern Matching with GADTs data Nat = Z | S Nat data Vec :: Nat -> a -> * where VN :: Vec Z a VC :: a -> Vec n a -> Vec (S n) a vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) 8

Pattern Matching with GADTs data Nat = Z | S Nat data Vec :: Nat -> a -> * where VN :: Vec Z a VC :: a -> Vec n a -> Vec (S n) a vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) 9

Pattern Matching with GADTs vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) 10

Pattern Matching with GADTs vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) :12:7: Warning: Pattern match(es) are non-exhaustive In an equation for `vzip': Patterns not matched: VN (VC _ _) (VC _ _) VN 11

Pattern Matching with GADTs vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) 12 False warning! 

Pattern Matching with GADTs vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) vzip _ _ = error “vzip” 13 No warning! 

Laziness 14

Laziness f :: Bool -> Bool -> Int f _ True = 1 f True True = 2 f _ _ = 3 :12:7: Warning: Pattern match(es) are overlapped In an equation for `f': f True True =... 15

Laziness f :: Bool -> Bool -> Int f _ True = 1 f True True = 2 f _ _ = 3 Prelude> f undefined False *** Exception: Prelude.undefined 16

Laziness f :: Bool -> Bool -> Int f _ True = 1 f True True = 2 f _ _ = 3 Prelude> f undefined False 3 17

Laziness f :: Bool -> Bool -> Int f _ True = 1 f True True = 2 f _ _ = 3 :12:7: Warning: Pattern match(es) have inaccessible right hand side In an equation for `f': f True True =... 18

Uniform Solution 19 GADTs GuardsLaziness

ABSTRACT INTERPRETATION OF PATTERN MATCHING 20

Abstractions Value Abstractions u::= x | K u 1 … u n Value abstractions v::= Γ Ⱶ u 1 … u n ▹ ΔValue vector abstractions S::= {v 1, …, v m }Value set abstractions Constraints Δ ::=τ ~ τType constraints |x ≈ eTerm equalities |x ≈ ⊥ Strictness constraints 21

patVectProc Uncovered 1 Algorithm Structure 22 desugarP P 1n p 11 … p 1n SU1SU1 SU0SU0 Covered 1 Divergent 1 All possible values

Algorithm Structure 23 desugar P P 1n P P 2n P m1... P mn patVectProc p 11 … p 1n p 21 … p 2n p m1 … p mn SU1SU1 SU0SU0 SUnSUn Covered 1 Covered 2 Covered n Divergent 1 Divergent 2 Divergent n Uncovered …………

Modular Constraint Solving 24 Covered Divergent Uncovered Γ Ⱶ us ▹ Δ Term Equalities x ≈ e Type Constraints τ ~ τ, … Strictness Constraints x ≈ ⊥

Interpretation of Results 25 CoveredDivergentWarning ØØRedundant Ø{…}Inaccessible rhs {…}Ø- - Final Uncovered SetWarning Ø- {…}Non-exhaustive

Example vzip :: Vec n a -> Vec n b -> Vec n (a,b) vzip VN VN = VN vzip (VC x xs) (VC y ys) = VC (x,y) (vzip xs ys) vzip _ _ = error “vzip” 26

Example 27 (VC c cs) b ▹ {n ~ S k} VN (VC c cs) ▹ {n ~ Z, n ~ S k} VN a b ▹ {} VN VN ▹ {n ~ Z, n ~ Z} ab ▹ {a ≈ ⊥ } VNb ▹ {n ~ Z, b ≈ ⊥ } vzip :: Vec n a -> Vec n b -> Vec n (a,b)

Example 28 (VC c cs) VN ▹ {n ~ S k, n ~ Z} (VC x xs) (VC y ys) (VC c cs) b ▹ {n ~ S k} (VC x xs) (VC y ys) ▹ {n ~ S k, n ~ S l} (VC c cs) b ▹ {b ≈ ⊥, n ~ S k} Exhaustive! vzip :: Vec n a -> Vec n b -> Vec n (a,b)

Example 29 {} _ {} Redundant! vzip :: Vec n a -> Vec n b -> Vec n (a,b)

There is more… Full algorithm  Pattern translation  Guard handling  Abstract interpretation Motivating examples Meta-theory Related work 30

IMPLEMENTATION 31

ghc-stage1: panic! (the 'impossible' happened) (GHC version for x86_64-unknown-linux): checkMatches Please report this as a GHC bug:

Implementation GHC branch (wip/gadtpm) 504 LoC (vs. 588 LoC) GHC Bug reports / Feature requests: GADTs:#3927, #4139, #6124, #8970 Literals:#322, #2204, #5724, #8016, #8853 (#1307, #5762, #7669, #8494, #9113, #9951) 33

Solver Instantiation 34 Covered Divergent Uncovered Γ Ⱶ us ▹ Δ Term Equalities x ≈ e Type Constraints τ ~ τ, … Strictness Constraints x ≈ ⊥ OutsideIn(X)Minimal Solver

Resolve

Resolve

Performance data T = A | B | C f A A = … f B B = … f C C = … 37 Maximum set sizePattern matches(%) 1 – % 10 – % 100 – % 54 x 54 = 3025

Summary Uniform framework for GADTs, Guards & Laziness  Abstract Interpretation of Pattern Matching  Modularity in Constraint Solving Implementation in Glasgow Haskell Compiler  Kind polymorphism, Associated Types, Closed Type Families, etc.  Closed several bug reports 38

Related Work Dependent Pattern Matching & GADTs  Coquand, Norell, Xi, Dunfield, etc. Compilation of Pattern Matching  Augustsson, Laville, Maranget, etc. Lazy Pattern Matching  Maranget, Sestoft, etc. Krishnaswami, Garrigue & Normand, etc. 39

Future Work Improve reasoning about term level equalities  External SMT solver (Z3, Zeno, HipSpec, etc)?  Undecidable in the general case Application to Closed Type Families 40

GADTs meet their match George Karachalias Tom Schrijvers Dimitrios Vytiniotis Simon Peyton Jones