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
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

type variables? ? How do we bind

slide-2
SLIDE 2

type variables? ? How we bind should

slide-3
SLIDE 3

xcons ys = x : ys prefix :: prefix x yss = map xcons yss where a ! [[a]] ! [[a]]

slide-4
SLIDE 4

prefix :: prefix x yss = map xcons yss where xcons ys = x : ys xcons :: [a] ! [a] a ! [[a]] ! [[a]]

slide-5
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
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
SLIDE 7

Type signatures are useful Goal: Allow a type signature on any expression

slide-8
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
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
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
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
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
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
SLIDE 14

Type signatures are useful Goal: Allow a type signature on any expression

slide-15
SLIDE 15

Solution: ScopedTypeVariable ScopedTypeVariables

slide-16
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
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
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
SLIDE 19

ScopedTypeVariables What is the specification of anyway? Typing rules! Contribution:

slide-20
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
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
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
SLIDE 23

Existentials

data Elab where MkE :: Show a ⇒ [Maybe (Tree (a, Int))] ! Elab

..?.A aA

slide-24
SLIDE 24

Existentials

type family F a data ExF where MkF :: Typeable a ⇒ F a ! ExF

..?.A aA ??

slide-25
SLIDE 25

Type signatures are useful Goal: Allow a type signature on any expression

slide-26
SLIDE 26

Solution: ScopedTypeVariable ScopedTypeVariables

slide-27
SLIDE 27

Solution: ScopedTypeVariable ScopedTypeVariables Partial Contribution: Pattern type applications

slide-28
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
SLIDE 29

Pattern type applications Explicit binding of
 type variables
 always works

slide-30
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
SLIDE 31

Universals vs Existentials ..?τ a Uniformity

data Confused a where MkC :: a ~ b ⇒ b ! Confused a

.?D?. ¯\_()_/¯

slide-32
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
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
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
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
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
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
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
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
SLIDE 40

Why this behavior? It's exactly how pattern signatures would work.

slide-41
SLIDE 41

In the paper: full specification with typing rules Upshot: we can easily drop the variable restriction

slide-42
SLIDE 42

Next Steps

Implementation: My Nguyen Binding type variables in λ-expressions

..D

slide-43
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