SLIDE 1
How do we bind type variables? ? How should we bind type - - PowerPoint PPT Presentation
How do we bind type variables? ? How should we bind type - - PowerPoint PPT Presentation
How do we bind type variables? ? How should we bind type variables? ? a ! [[a]] ! [[a]] prefix :: prefix x yss = map xcons yss where xcons ys = x : ys a ! [[a]] ! [[a]] prefix :: prefix x yss = map xcons yss xcons :: [a] ! [a] where
SLIDE 2
SLIDE 3
xcons ys = x : ys prefix :: prefix x yss = map xcons yss where a ! [[a]] ! [[a]]
SLIDE 4
prefix :: prefix x yss = map xcons yss where xcons ys = x : ys xcons :: [a] ! [a] a ! [[a]] ! [[a]]
SLIDE 5
prefix :: prefix x yss = map xcons yss where xcons ys = x : ys xcons :: [a] ! [a] Couldn't match ‘a1’ with ‘a’ ‘a1’ is bound in xcons :: ∀ a1. [a1] -> [a1]
A.aa1!
a ! [[a]] ! [[a]]
SLIDE 6
prefix :: prefix x yss = map xcons yss where xcons ys = x : ys xcons :: [a] ! [a] a ! [[a]] ! [[a]] ∀ a. {-# LANGUAGE ScopedTypeVariables #-} Ok, one module loaded.
SLIDE 7
Type signatures are useful Goal: Allow a type signature on any expression
SLIDE 8
Type signatures are useful
- type-class ambiguity
show :: Show a ⇒ a ! String read :: Read a ⇒ String ! a normalize :: String ! String normalize = show . read
..?
SLIDE 9
Type signatures are useful
- type-class ambiguity
- polymorphic recursion
data T a = Leaf a | Node (T [a]) (T [a]) leaves :: T a ! [a] leaves (Leaf x) = [x] leaves (Node t1 t2) = concat (leaves t1 ++ leaves t2)
?.A???.
SLIDE 10
Type signatures are useful
- type-class ambiguity
- polymorphic recursion
- higher-rank types
Scrap Your Boilerplate [TLDI '03]: everywhere :: (∀ a. Data a ⇒ a ! a) ∀ a. Data a ⇒ a ! a !
?.A???.
SLIDE 11
Type signatures are useful
- type-class ambiguity
- polymorphic recursion
- higher-rank types
- GADTs
data G a where MkInt :: G Int MkFun :: G (Int ! Int) matchG :: G a ! a matchG MkInt = 5 matchG MkFun = (10+)
SLIDE 12
Type signatures are useful
- type-class ambiguity
- polymorphic recursion
- higher-rank types
- GADTs
data G a where MkInt :: G Int MkFun :: G (Int ! Int) matchG :: G a ! a matchG MkInt = 5 matchG MkFun = (10+)
?.A???.
SLIDE 13
Type signatures are useful
- type-class ambiguity
- polymorphic recursion
- higher-rank types
- GADTs
- inherent ambiguity
type family F a ambig :: Typeable a ⇒ F a ! Int test :: Char ! Int test x = ambig x
. a
SLIDE 14
Type signatures are useful Goal: Allow a type signature on any expression
SLIDE 15
Solution: ScopedTypeVariable ScopedTypeVariables
SLIDE 16
ScopedTypeVariables
prefix :: prefix x yss = map xcons yss where xcons ys = x : ys xcons :: [a] ! [a] a ! [[a]] ! [[a]] ∀ a.
- prefix (x::a) yss = map xcons yss
where xcons :: [a] ! [a] xcons ys = x : ys
.?.A
SLIDE 17
ScopedTypeVariables
prefix (x::a) yss = map xcons yss where xcons :: [a] ! [a] xcons ys = x : ys Ok, one module loaded. λ> :t prefix prefix :: 1 : Num a ⇒ a ! [[a]] ! [[a]]
SLIDE 18
ScopedTypeVariables
prefix (x::a) yss = map xcons yss where xcons :: [a] ! [a] xcons ys = x : ys True : Couldn't match a with Bool
Rule: type variables must be variables Arbitrary ?
SLIDE 19
ScopedTypeVariables What is the specification of anyway? Typing rules! Contribution:
SLIDE 20
Existentials
data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker
D?.
tick :: Ticker ! Ticker tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val
SLIDE 21
data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a
.?? Existentials
SLIDE 22
data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a ( ::a)
.a Existentials
SLIDE 23
Existentials
data Elab where MkE :: Show a ⇒ [Maybe (Tree (a, Int))] ! Elab
..?.A aA
SLIDE 24
Existentials
type family F a data ExF where MkF :: Typeable a ⇒ F a ! ExF
..?.A aA ??
SLIDE 25
Type signatures are useful Goal: Allow a type signature on any expression
SLIDE 26
Solution: ScopedTypeVariable ScopedTypeVariables
SLIDE 27
Solution: ScopedTypeVariable ScopedTypeVariables Partial Contribution: Pattern type applications
SLIDE 28
Pattern type applications
data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker tick (MkT = MkT newVal upd toInt where val upd toInt) newVal = upd val newVal :: a @a
SLIDE 29
Pattern type applications Explicit binding of type variables always works
SLIDE 30
Universals vs Existentials
data UnivEx a where MkUE :: a ! b ! UnivEx a
A?. D?.
case ue of MkUE @a @b x y ! ...
:: UnivEx τ
..?τ a
SLIDE 31
Universals vs Existentials ..?τ a Uniformity
data Confused a where MkC :: a ~ b ⇒ b ! Confused a
.?D?. ¯\_()_/¯
SLIDE 32
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m
SLIDE 33
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m ..?..
SLIDE 34
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m D?A.A?
SLIDE 35
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m A...?
SLIDE 36
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m ?A?.
SLIDE 37
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m ?A.A?
SLIDE 38
Universals & Existentials Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m ".??A(),A.?" " ..?"
SLIDE 39
Γ ⊢ K @τ1..m p1..n : T σ1..j ... K : ∀ a1..m. Q ⇒ η1..n → T φ1..j Γ, Q, φ1..j ~ σ1..j ⊩ τ1..m ~ a1..m ".??A(),A.?" " ..?"
data Example where MkEx :: ∀ a b. (a ~ Maybe b) ⇒ Example
Example
case x :: Example of MkEx @a @b ! ... MkEx @(Maybe b) @b ! ... MkEx @(Maybe b) ! ... MkEx @a @(Maybe b) ! ...
SLIDE 40
Why this behavior? It's exactly how pattern signatures would work.
SLIDE 41
In the paper: full specification with typing rules Upshot: we can easily drop the variable restriction
SLIDE 42
Next Steps
Implementation: My Nguyen Binding type variables in λ-expressions
..D
SLIDE 43
Type Variables in Patterns
Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu
Friday, September 28, 2018 Haskell Symposium
- St. Louis, MO, USA
Joachim Breitner DFINITY Foundation joachim@dfinity.org Simon Peyton Jones Microsoft Research, Cambridge simonpj@microsoft.com