how do we bind type variables how should we bind type

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


  1. How do we bind type variables? ?

  2. How should we bind type variables? ?

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

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

  5. a ! [[a]] ! [[a]] prefix :: prefix x yss = map xcons yss xcons :: [a] ! [a] where xcons ys = x : ys �A�����.��� a ������ a1 ! Couldn't match ‘a1’ with ‘a’ 
 ‘a1’ is bound in 
 xcons :: ∀ a1. [a1] -> [a1]

  6. {-# LANGUAGE ScopedTypeVariables #-} a ! [[a]] ! [[a]] ∀ a. prefix :: prefix x yss = map xcons yss xcons :: [a] ! [a] where xcons ys = x : ys Ok, one module loaded.

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

  8. Type signatures are useful • type-class ambiguity show :: Show a ⇒ a ! String read :: Read a ⇒ String ! a 
 normalize :: String ! String 
 normalize = show . read ��.�����������.�?�������

  9. Type signatures are useful • type-class ambiguity • polymorphic recursion data T a = Leaf a | Node (T [a]) (T [a]) �����?���.�A����?�����??.�� leaves :: T a ! [a] leaves (Leaf x) = [x] leaves (Node t1 t2) = concat (leaves t1 ++ leaves t2)

  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����?�����??.��

  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+)

  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) �����?���.�A����?�����??.�� matchG :: G a ! a matchG MkInt = 5 matchG MkFun = (10+)

  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 ��������� a test x = ambig x

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

  15. Solution: ScopedTypeVariables ScopedTypeVariable

  16. ScopedTypeVariables a ! [[a]] ! [[a]] ∀ a. prefix :: prefix x yss = map xcons yss xcons :: [a] ! [a] where xcons ys = x : ys �� prefix (x::a) yss = map xcons yss where xcons :: [a] ! [a] xcons ys = x : ys �.������?���.�A��

  17. ScopedTypeVariables prefix (x::a) yss = map xcons yss where xcons :: [a] ! [a] xcons ys = 1 : x : ys Ok, one module loaded. λ> :t prefix prefix :: Num a ⇒ a ! [[a]] ! [[a]]

  18. ScopedTypeVariables prefix (x::a) yss = map xcons yss where xcons :: [a] ! [a] xcons ys = True : x : ys Couldn't match a with Bool Arbitrary Rule: type variables ? must be variables

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

  20. Existentials data Ticker where MkT :: ∀ a. a ! (a ! a) �D�?�����.� ! (a ! Int) ! Ticker tick :: Ticker ! Ticker val upd toInt) tick (MkT = MkT newVal upd toInt where newVal = upd val

  21. Existentials data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker val upd toInt) tick (MkT = MkT newVal upd toInt where newVal :: a newVal = upd val ��.���?����?�

  22. Existentials data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker ( val tick (MkT ::a) upd toInt) = MkT newVal upd toInt where newVal :: a newVal = upd val ����������.���������� a

  23. Existentials data Elab where MkE :: Show a ⇒ [Maybe (Tree (a, Int))] ! Elab .��.������?���.�A������ ����� a ���A����������

  24. Existentials type family F a data ExF where MkF :: Typeable a ⇒ F a ! ExF .��.������?���.�A������ ����� a ���A���������� ����??����

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

  26. Solution: ScopedTypeVariables ScopedTypeVariable

  27. Partial Solution: ScopedTypeVariables ScopedTypeVariable Contribution: Pattern type applications

  28. Pattern type applications data Ticker where MkT :: ∀ a. a ! (a ! a) ! (a ! Int) ! Ticker tick :: Ticker ! Ticker @a val upd toInt) tick (MkT = MkT newVal upd toInt where newVal :: a newVal = upd val

  29. Pattern type applications Explicit binding of 
 type variables 
 always works

  30. Universals vs Existentials data UnivEx a where MkUE :: a ! b ! UnivEx a A�����?.� �D�?�����.� :: UnivEx τ case ue of MkUE @a @b x y ! ... ���.��.�?������ τ ������� ��������������� a �

  31. Universals vs Existentials ���.��.�?������ τ ������� ��������������� a � Uniformity data Confused a where MkC :: a ~ b ⇒ b ! Confused a ��.���?��D�?�����.�� ¯\_( � )_/¯

  32. Universals & Existentials ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j

  33. Universals & Existentials ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j �����.�����.����?����.��.�����

  34. Universals & Existentials ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j �D���������?A��������.��A����?

  35. Universals & Existentials �A.��������������.��.���? ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j

  36. Universals & Existentials ���?��A��������?��.��� ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j

  37. Universals & Existentials ��?A��������.��A����? ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j

  38. Universals & Existentials ".??A����������(),���A.�����?���" ... K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m Γ ⊢ K @τ 1..m p 1..n : T σ 1..j "�������������������� ���������.�����.����?"

  39. Example ".??A����������(),���A.�����?���" data Example where ... MkEx :: ∀ a b. K : ∀ a 1..m . Q ⇒ η 1..n → T φ 1..j (a ~ Maybe b) ⇒ Example Γ, Q, φ 1..j ~ σ 1..j ⊩ τ 1..m ~ a 1..m case x :: Example of MkEx @a @b ! ... Γ ⊢ K @τ 1..m p 1..n : T σ 1..j MkEx @(Maybe b) @b ! ... MkEx @(Maybe b) ! ... "�������������������� ���������.�����.����?" MkEx @a @(Maybe b) ! ...

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

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

  42. Next Steps Implementation: My Nguyen Binding type variables in λ-expressions �����.����.������D�

  43. Type Variables in Patterns Richard A. Eisenberg Joachim Breitner Bryn Mawr College DFINITY Foundation rae@cs.brynmawr.edu joachim@dfinity.org Simon Peyton Jones Microsoft Research, Cambridge simonpj@microsoft.com Friday, September 28, 2018 Haskell Symposium St. Louis, MO, USA

Recommend


More recommend