Functional Dependencies in System FC

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.
Let should not be generalized Dimitrios Vytiniotis, Simon Peyton Jones Microsoft Research, Cambridge TLDI’1 0, Madrid, January 2010 Tom Schrijvers K.U.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Section Differentials Tangent Line Approximations A tangent line approximation is a process that involves using the tangent line to approximate a.
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 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
System F with type equality coercions Simon Peyton Jones (Microsoft) Manuel Chakravarty (University of New South Wales) Martin Sulzmann (University of.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
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 COMP313A Programming Languages Functional Programming (7)
GADTs GADTs in Haskell. ADT vs GADT Algebraic Datatype Data List a = Nil | Cons a (List a) Data Tree a b = Tip a | Node (Tree a b) b | Fork (Tree a b)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
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.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Defining Classes Modules and ADTs CSCE 314 Spring 2016.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
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.
© 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.
GADTs meet their match George Karachalias(Ghent University, Belgium) Tom Schrijvers (KU Leuven, Belgium) Dimitrios Vytiniotis(Microsoft Research Cambridge,
Polymorphic Functions
String is a synonym for the type [Char].
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Section 2 – CSE341 Patrick Larson, Spring 2013.
PROGRAMMING IN HASKELL
Koen Lindström Claessen
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
Section 2 – CSE341 Konstantin Weitz.
Extended Static Checking for Haskell (ESC/Haskell)
Secondary Math Line Segments.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Advanced Functional Programming
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Lazy Programming Lazy evaluation:
How do you determine if a function is linear or non linear
Records and Type Classes
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Accepting Strings.
CSEP505: Programming Languages Lecture 9: Haskell Typeclasses and Monads; Dan Grossman Autumn 2016.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
PROGRAMMING IN HASKELL
Records and Type Classes
Presentation transcript:

Functional Dependencies in System FC George Karachalias & Tom Schrijvers

Functional Dependencies vs. Associated Types

Functional Dependencies class Collection c e where empty :: c insert :: e -> c -> c ins2 x y col = insert x (insert y col) ins2 :: (Collection c e1, Collection c e2) => e1 -> e2 -> c -> c too general Mark P. Jones. Type Classes with Functional Dependencies

Functional Dependencies class Collection c e | c -> e where empty :: c insert :: e -> c -> c ins2 :: Collection c e => e -> e -> c -> c ins2 x y col = insert x (insert y col) Mark P. Jones. Type Classes with Functional Dependencies

Functional Dependencies (Instance) class Collection c e | c -> e where empty :: c insert :: e -> c -> c instance Collection [a] a where empty = [] insert x xs = (x:xs) Mark P. Jones. Type Classes with Functional Dependencies

Associated Types class Collection c where type Elem c empty :: c insert :: Elem c -> c -> c ins2 :: Collection c => Elem c -> Elem c -> c -> c ins2 x y col = insert x (insert y col) Manuel M. T. Chakravarty, Gabriele Keller, Simon Peyton Jones. Associated Type Synonyms

Associated Types (Instance) class Collection c where type Elem c empty :: c insert :: Elem c -> c -> c instance Collection [a] where type Elem [a] = a empty = [] insert x xs = (x:xs) Manuel M. T. Chakravarty, Gabriele Keller, Simon Peyton Jones. Associated Type Synonyms

FDs in System FC class C a b | a -> b instance C Int Bool f :: C Int b => b -> Bool f x = x

FDs in System FC class C a b | a -> b instance C Int Bool f :: C Int b => b -> Bool f x = x Couldn’t match expected type ‘Bool’ with actual type ‘b’

FDs in System FC class C a b | a -> b instance C Int Bool f :: C Int b => b -> Bool f x = x

FDs in System FC class C a b | a -> b instance C Int Bool f :: C Int b => b -> Bool f x = x (b ~ Bool)

ATs in System FC class C a where type F a instance C Int where type F Int = Bool f :: C Int => F Int -> Bool f x = x

“Death to Functional Dependencies Say I” - Simon Peyton Jones quote: https://ghc.haskell.org/trac/ghc/ticket/1614#comment:2 picture: https://i.ytimg.com/vi/2WgxPxxfT7E/maxresdefault.jpg

The Problem: Lack of Evidence

Lack of Evidence class C a b | a -> b instance C Int Bool f :: C Int b => b -> Bool f x = x data CDict a b = MkCDict cIntBool :: CDict Int Bool cIntBool = MkCDict f :: CDict Int b -> b -> Bool f d x = x

Translation of Functional Dependencies to Associated Types Tom Schrijvers, Martin Sulzmann, Simon PJ, Manuel Chakravarty. Towards Open Type Functions

Strategy Class Declarations Class Instances For every FD generate a new Associated Type For every FD add an equivalent equality in the context Class Instances For every FD generate an appropriate instance for the respective Associated Type Tom Schrijvers, Martin Sulzmann, Simon PJ, Manuel Chakravarty. Towards Open Type Functions

In Search of a Proof data Nat = Zero | Succ Nat class Add (x :: Nat) (y :: Nat) (z :: Nat) | x y -> z instance Add Zero m m instance Add n m k => Add (Succ n) m (Succ k)

In Search of a Proof class Add (x :: Nat) (y :: Nat) (z :: Nat) | x y -> z class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y }

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance Add Zero m m instance Add Zero m m where type Fd Zero m = ???

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance Add Zero m m instance Add Zero m m where type Fd Zero m = m

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance (Add n m k) => Add (Succ n) m (Succ k) instance (Add n m k) => Add (Succ n) m (Succ k) where type Fd (Succ n) m = ???

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance (Add n m k) => Add (Succ n) m (Succ k) instance (Add n m k) => Add (Succ n) m (Succ k) where type Fd (Succ n) m = Succ k

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance (Add n m k) => Add (Succ n) m (Succ k) instance (Add n m k) => Add (Succ n) m (Succ k) where type Fd (Succ n) m = Succ k k ~ Fd n m

In Search of a Proof class Fd x y ~ z => Add (x :: Nat) (y :: Nat) (z :: Nat) where { type Fd x y } instance (Add n m k) => Add (Succ n) m (Succ k) instance (Add n m k) => Add (Succ n) m (Succ k) where type Fd (Succ n) m = Succ (Fd n m)

FUNCTIONAL DEPENDENCIES From FDs to System FC FUNCTIONAL DEPENDENCIES ASSOCIATED TYPES TYPE CLASSES ADTs GADTs TYPE FAMILIES System FC

Bi-directional Instances Schrijvers, Tom and Sulzmann, Martin. Unified Type Checking for Type Classes and Type Families

Bi-directional Declarations class Eq a where (==) :: a -> a -> Bool class Eq a => Ord a where (>) :: a -> a -> Bool f :: Ord a => a -> a -> Bool f x y = (x == y) data EqD a = MkEqD { (==) :: a -> a -> Bool } data OrdD a = MkOrdD { (>) :: a -> a -> Bool eqd :: EqD a } f :: OrdD a -> a -> a -> Bool f d x y = (==) (eqd d) x y

Non-Bi-directional Instances class Eq a where (==) :: a -> a -> Bool instance Eq a => Eq [a] where x == y = all (zipWith (==) x y) data EqD a = MkEqD { (==) :: a -> a -> Bool } eqList :: EqD a -> EqD [a] eqList d = MkEqD { (==) = \x y -> all (zipWith ((==) d) x y

FDs vs. Type Families data Nat = Zero | Succ Nat data Vec :: Nat -> * -> * where VN :: Vec Zero a VC :: a -> Vec n a -> Vec (Succ n) a

FDs vs. Type Families type family Add (x :: Nat) (y :: Nat) :: Nat type instance Add Zero m = m type instance Add (Succ n) m = Succ (Add n m) append :: Vec n a -> Vec m a -> Vec (Add n m) a append VN ys = ys append (VC x xs) ys = VC x (append xs ys)

FDs vs. Type Families class Add (x :: Nat) (y :: Nat) (z :: Nat) | x y -> z instance Add Zero m m instance Add n m k => Add (Succ n) m (Succ k) append :: Add n m k => Vec n a -> Vec m a -> Vec k a append VN ys = ys append (VC x xs) ys = VC x (append xs ys)

Thank You!

Functional Dependencies in System FC George Karachalias & Tom Schrijvers