Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.

Slides:



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

Functional Programming Lecture 10 - type checking.
Formal Models of Computation Part II The Logic Model
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.
Static Contract Checking for Haskell Dana N. Xu University of Cambridge Ph.D. Supervisor: Simon Peyton Jones Microsoft Research Cambridge.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
An Abstract Interpretation Framework for Refactoring P. Cousot, NYU, ENS, CNRS, INRIA R. Cousot, ENS, CNRS, INRIA F. Logozzo, M. Barnett, Microsoft Research.
1 1 Strategic Programming in Java Pierre-Etienne Moreau Antoine Reilles Stratego User Day, December, 1 st, 2006.
Let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U.
Computing Fundamentals 2 Introduction to CafeOBJ Lecturer: Patrick Browne Lecture Room: K408 Lab Room: A308 Based on work by: Nakamura Masaki, João Pascoal.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Type checking © Marcelo d’Amorim 2010.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
System F with type equality coercions Simon Peyton Jones (Microsoft) Manuel Chakravarty (University of New South Wales) Martin Sulzmann (University of.
CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Generative type abstraction and type-level computation (Wrestling with System FC) Stephanie Weirich, Steve Zdancewic University of Pennsylvania Dimitrios.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Catriel Beeri Pls/Winter 2004/5 inductive-revisited 1 Inductive definitions revisited  Generated and Freely generated sets oPattern match, unification.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Semantics for MinML COS 441 Princeton University Fall 2004.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Deriving Combinator Implementations Lecture 4, Designing and Using Combinators John Hughes.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
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.
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
Functional Programming Lecture 3 - Lists Muffy Calder.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
GADTs meet their match George Karachalias(Ghent University, Belgium) Tom Schrijvers (KU Leuven, Belgium) Dimitrios Vytiniotis(Microsoft Research Cambridge,
Functional Dependencies in System FC
Polymorphic Functions
Functional Programming
Types CSCE 314 Spring 2016.
Types for Programs and Proofs
Sparkle a functional theorem prover
Haskell Chapter 2.
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Proving Properties of Recursive List Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Microsoft Research University of Pennsylvania
Extended Static Checking for Haskell (ESC/Haskell)
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
PROGRAMMING IN HASKELL
Records and Type Classes
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Static Contract Checking for Haskell
Records and Type Classes
Presentation transcript:

Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones

Type-level functions Functional programming at the type level! Examples Usefulness Interaction Type Checking Eliminating functional dependencies Examples Usefulness Interaction Type Checking Eliminating functional dependencies

Examples

Example: = 2? -- Peano numerals data Z data Succ n -- Abbreviations type One = Succ Z type Two = Succ One -- Type-level addition type family Sum m n type instance Sum Z n = n type instance Sum (Succ a) b = Succ (Sum a b) -- Peano numerals data Z data Succ n -- Abbreviations type One = Succ Z type Two = Succ One -- Type-level addition type family Sum m n type instance Sum Z n = n type instance Sum (Succ a) b = Succ (Sum a b) type function declaration Sum :: * -> * -> * type function declaration Sum :: * -> * -> * 1 st type function instance 2 nd type function instance

Example: = 2? -- Type-level addition type family Sum m n type instance Sum Z nat = nat type instance Sum (Succ a) b = Succ (Sum a b) -- Hypothesis hypthesis :: (Sum One One, Two) hypothesis = undefined -- Test test :: (a,a) -> Bool test _ = True -- Type-level addition type family Sum m n type instance Sum Z nat = nat type instance Sum (Succ a) b = Succ (Sum a b) -- Hypothesis hypthesis :: (Sum One One, Two) hypothesis = undefined -- Test test :: (a,a) -> Bool test _ = True

Example: = 2? -- Hypothesis hypthesis :: (Sum One One, Two) hypothesis = undefined -- Test test :: (a,a) -> Bool test _ = True -- Proof proof :: Bool proof = test hypothesis -- Hypothesis hypthesis :: (Sum One One, Two) hypothesis = undefined -- Test test :: (a,a) -> Bool test _ = True -- Proof proof :: Bool proof = test hypothesis static check: 1+1 = 2

Example: Length-indexed Lists -- Length-indexed lists : vectors data Vec e l where VNil :: Vec e Z VCons :: e -> Vec e l -> Vec e (Succ l) -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys) -- Length-indexed lists : vectors data Vec e l where VNil :: Vec e Z VCons :: e -> Vec e l -> Vec e (Succ l) -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys)

Example: Length-indexed Lists -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys) -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys) > let l1 = (VCons 1 VNil) l2 = (VCons a (VCons b VNil)) in vzip l1 l2 12

Example: Length-indexed Lists -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys) -- Safe zip: matched lengths vzip :: Vec a l -> Vec b l -> Vec (a,b) l vzip VNil VNil = VNil vzip (VCons x xs) (VCons y ys) = VCons (x,y) (vzip xs ys) > let l1 = (VCons 1 (VCons 2 VNil)) l2 = (VCons a (VCons b VNil)) in vzip l1 l2 VCons (1,a) (VCons (2,b) VNil) 2=2

Example: Length-indexed lists -- concatenation vconc :: Vec a m -> Vec a n -> Vec a ??? vconc VNil VNil = VNil Vconc (VCons x xs) ys = VCons x (vconc xs ys) -- concatenation vconc :: Vec a m -> Vec a n -> Vec a ??? vconc VNil VNil = VNil Vconc (VCons x xs) ys = VCons x (vconc xs ys)

Example: Length-indexed lists -- concatenation vconc :: Vec a m -> Vec a n -> Vec a (Sum m n) vconc VNil VNil = VNil Vconc (VCons x xs) ys = VCons x (vconc xs ys) -- concatenation vconc :: Vec a m -> Vec a n -> Vec a (Sum m n) vconc VNil VNil = VNil Vconc (VCons x xs) ys = VCons x (vconc xs ys) > let l1 = (VCons 1) l2 = (VCons a (VCons b VNil)) l3 = vconc l1 l1 in vzip l3 l2 VCons (1,a) (VCons (1,b) VNil) 1+1=2

Example: Collections (1/4) class Collection c where type Elem c add :: Elem c -> c -> c list :: c -> [Elem c] elem :: c -> Elem c Associated type function Elem :: * -> * Associated value function elem :: c -> Elem c

Example: Collections (2/4) instance Collection [e] where type Elem [e] = e instance Collection (Tree e) where type Elem (Tree e) = e instance Collection BitVector where type Elem BitVector = Bit type functions are open!

instance Collection [e] where type Elem [e] = e add :: e -> [e] -> [e] add x xs = x : xs list :: [e] -> [ e ] list xs = xs Example: Collections (3/4) instance Collection [e] where type Elem [e] = e add :: Elem [e] -> [e] -> [e] add x xs = x : xs list :: [e] -> [Elem [e]] list xs = xs

addAll :: c1 -> c2 -> c2 addAll xs ys = foldr add ys (list xs) Example: Collections (4/4) addAll :: Collection c1 => c1 -> c2 -> c2 addAll xs ys = foldr add ys (list xs) addAll :: Collection c1, Collection c2 => c1 -> c2 -> c2 addAll xs ys = foldr add ys (list xs) addAll :: Collection c1, Collection c2, Elem c1 ~ Elem c2=> c1 -> c2 -> c2 addAll xs ys = foldr add ys (list xs) > addAll [0,1,0,1,0] emptyBitVector context constraint, satisfied by caller context constraint, satisfied by caller Elem [Bit] ~ Elem BitVector

Summary of Examples functions over types open definitions stand-alone associated to a type class equational constraints in signature contexts usefulness limited on their own really great with GADTs type classes

Type Checking

Compiler Overview Haskell Constraints Type Checker Coercions System F C

Elem [Int] ~ Int ? γ Int :: Elem [Int] ~ Int ! Compiler Overview (elem [0]) (γ Int) == 0 elem [0] == 0γ : e.Elem [e] = e hypothesis axiom proof label witness cast

Basic Hindley-Milner [Int] ~ [Int] syntactic equality (unification) Type Functions Elem [Int] ~ Int syntactic equality modulo equational theory Type Checking = Syntactical?

Type Checking = Term Rewriting γ : e.Elem [e] = e Elem [Int] ~ Int Int γ Int Int Equational theory = given equations Toplevel equations: E t Context equations: E g Theorem proving Operational interpretation of theory = Term Rewriting System (Soundness!)

Completeness TRS must be Strongly Normalizing (SN): Confluent: every type has a canonical form Terminating TRS must be Strongly Normalizing (SN): Confluent: every type has a canonical form Terminating Practical & Modular Conditions: Confluence: no overlap of rule heads Terminating: Decreasing recursive calls No nested calls Practical & Modular Conditions: Confluence: no overlap of rule heads Terminating: Decreasing recursive calls No nested calls

Completeness Conditions Practical & Modular Conditions: Confluence: no overlap Elem [e] = e Elem [Int] = Bit Terminating: Decreasing recursive calls Elem [e] = Elem [[e]] No nested calls Elem [e] = Elem (F e) F e = [e] Practical & Modular Conditions: Confluence: no overlap Elem [e] = e Elem [Int] = Bit Terminating: Decreasing recursive calls Elem [e] = Elem [[e]] No nested calls Elem [e] = Elem (F e) F e = [e]

Term Rewriting Conditions EtEt EgEg E g We can do better: E t : satisfy conditions E g : free form(but no schema variabels) We can do better: E t : satisfy conditions E g : free form(but no schema variabels) EtEt Completion!

Type Checking Summary EtEt EgEg t1 ~ t2 E g t ~ t 1.Completion 2.Rewriting 3.Syntactic Equality Its all been implemented in GHC! Its all been implemented in GHC!

Eliminating Functional Dependencies

Functional Dependencies Using relations as functions Logic Programming! class Collection c e | c -> e where add :: e -> c -> c list :: c -> [e] elem :: c -> [e] instance Collection [e] e where...

FDs: Two Problems Haskell implementors are still debugging FD type checking Haskell programmers know Functional Programming FDs = Logic Programming

FDs: Solution Functional Dependencies Type Functions

FDs: Solution 2 Backward Compatibility??? Expressiveness lost??? automatic transformation

class FD c ~ e => Collection c e where type FD c add :: e -> c -> c list :: c -> [e] elem :: c -> e instance Collection [e] e where... class FD c ~ e => Collection c e where type FD c add :: e -> c -> c list :: c -> [e] elem :: c -> e instance Collection [e] e where type FD [e] = e class Collection c e | c -> e where add :: e -> c -> c list :: c -> [e] elem :: c -> [e] instance Collection [e] e where... class Collection c e | c -> e where type FD c add :: e -> c -> c list :: c -> [e] elem :: c -> e instance Collection [e] e where... FDs: Simple Transformation Minimal Impact: class and instance declarations only

class FD c ~ e => Collection c where type FD c add :: e -> c -> c list :: c -> [e] elem :: c -> e instance Collection [e] e where type FD [e] = e class Collection c where type FD c add :: FD c -> c -> c list :: c -> [FD c] elem :: c -> FD c instance Collection [e] where type FD [e] = e FDs: Advanced Transformation Drop dependent parameters class FD c ~ e => Collection c e where type FD c add :: e -> c -> c list :: c -> [e] elem :: c -> e instance Collection [e] e where type FD [e] = e

class FD c ~ e => Collection c e where type FD c addAll :: Collection c1 e, Collection c2 e =>... FDs: Advanced Transformation Bigger Impact: signature contexts too class Collection c where type FD c addAll :: Collection c1 e, Collection c2 e =>... class Collection c where type FD c addAll :: Collection c1, Collection c2, Elem c1 ~ Elem c2 =>...

Conclusion

Type checking with type functions = Term Rewriting sound, complete and terminating completion algorithm Seemless integration in Haskells rich type system Type classes GADTs Understanding functional dependencies better Type checking with type functions = Term Rewriting sound, complete and terminating completion algorithm Seemless integration in Haskells rich type system Type classes GADTs Understanding functional dependencies better

Future Work Improvements and variations Weaker termination conditions Closed type functions Rational tree types Efficiency Applications: Port libraries Revisit type hacks Write some papers! Improvements and variations Weaker termination conditions Closed type functions Rational tree types Efficiency Applications: Port libraries Revisit type hacks Write some papers!

Extra Slide

Rational tree types -- Non-recursive list data List e t = Nil | Cons e t -- Fixedpoint operator type family Fix (k :: *->*) type instance Fix k = k (Fix k) -- Tie the knot type List e = Fix (List e) = List e (List e (List e …))