partial type signatures
play

Partial Type Signatures Thomas Winant Dominique Devriese Frank - PowerPoint PPT Presentation

HIW14 September 6th 2014 Partial Type Signatures Thomas Winant Dominique Devriese Frank Piessens Tom Schrijvers 2 / 29 PARTIAL TYPE SIGNATURE 3 / 29 PARTIAL TYPE SIGNATURE 3 / 29 foo file = do ... ? ... PARTIAL TYPE SIGNATURE 3


  1. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ a ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  2. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  3. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  4. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  5. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  6. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  7. TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  8. NAMED WILDCARDS SYNTAX filter = filter pred x xs pred x = x filter pred xs otherwise = filter pred xs 13 / 29

  9. NAMED WILDCARDS SYNTAX filter filter pred xs 13 / 29 filter :: ( a → Bool ) → [ a ] → [ a ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  10. NAMED WILDCARDS SYNTAX filter filter pred xs 13 / 29 filter :: ( x → x ) → [ x ] → [ x ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  11. NAMED WILDCARDS SYNTAX filter filter pred xs 13 / 29 Inferred: ( Bool → Bool ) → [ Bool ] → [ Bool ] filter :: ( x → x ) → [ x ] → [ x ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  12. NAMED WILDCARDS SYNTAX filter filter pred xs 13 / 29 filter :: ( x → Bool ) → [ x ] → [ x ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  13. NAMED WILDCARDS SYNTAX filter filter pred xs 13 / 29 Inferred: ( w _ x → Bool ) → [ w _ x ] → [ w _ x ] filter :: ( x → Bool ) → [ x ] → [ x ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =

  14. NAMED WILDCARDS SYNTAX 14 / 29 eq :: Eq a ⇒ a → a → Bool eq x y = x ≡ y

  15. NAMED WILDCARDS SYNTAX 14 / 29 eq :: Eq x ⇒ x → x → Bool eq x y = x ≡ y

  16. NAMED WILDCARDS SYNTAX 14 / 29 Inferred: Eq w _ x ⇒ w _ x → w _ x → Bool eq :: Eq x ⇒ x → x → Bool eq x y = x ≡ y

  17. NAMED WILDCARDS SYNTAX x 14 / 29 eq :: Eq x ⇒ x → x → eq x y = x ≡ y

  18. NAMED WILDCARDS SYNTAX x 14 / 29 Inferred: Eq Bool ⇒ Bool → Bool → Bool eq :: Eq x ⇒ x → x → eq x y = x ≡ y

  19. NAMED WILDCARDS SYNTAX x 14 / 29 Inferred: Bool → Bool → Bool eq :: Eq x ⇒ x → x → eq x y = x ≡ y

  20. CONSTRAINT WILDCARDS SYNTAX 15 / 29

  21. CONSTRAINT WILDCARDS SYNTAX -- class Eq a => Ord x 15 / 29 bar :: Ord a ⇒ a → a → Bool bar x y = x ≡ y

  22. CONSTRAINT WILDCARDS SYNTAX -- class Eq a => Ord x 15 / 29 bar :: Ord ⇒ a → a → Bool bar x y = x ≡ y

  23. CONSTRAINT WILDCARDS SYNTAX Mismatch: inferred Eq a vs. annotated Ord -- class Eq a => Ord x 15 / 29 bar :: Ord ⇒ a → a → Bool bar x y = x ≡ y

  24. CONSTRAINT WILDCARDS SYNTAX 16 / 29

  25. CONSTRAINT WILDCARDS SYNTAX 16 / 29 foo :: ( Show a , Num a ) ⇒ a → String foo x = show ( x + 1 )

  26. CONSTRAINT WILDCARDS SYNTAX 16 / 29 foo :: a ⇒ a → String foo x = show ( x + 1 )

  27. CONSTRAINT WILDCARDS SYNTAX 16 / 29 Infer? Show a ⇒ a → String foo :: a ⇒ a → String foo x = show ( x + 1 )

  28. CONSTRAINT WILDCARDS SYNTAX 16 / 29 Infer? Num a ⇒ a → String foo :: a ⇒ a → String foo x = show ( x + 1 )

  29. CONSTRAINT WILDCARDS a Yes Bool x x x Eq No Bool a x SYNTAX Eq No Bool a a Eq Compromise 17 / 29 ▶ Only named wildcards in constraints… ▶ …when present in the rest of the type

  30. CONSTRAINT WILDCARDS a Yes Bool x x x Eq No Bool a SYNTAX x Eq No Eq Compromise 17 / 29 ▶ Only named wildcards in constraints… ▶ …when present in the rest of the type ⇒ a → a → Bool

  31. CONSTRAINT WILDCARDS No Yes Bool x x x Eq 17 / 29 SYNTAX Eq No Eq Compromise ▶ Only named wildcards in constraints… ▶ …when present in the rest of the type ⇒ a → a → Bool x ⇒ a → a → Bool

  32. CONSTRAINT WILDCARDS No Yes Eq No SYNTAX Eq Compromise Eq 17 / 29 ▶ Only named wildcards in constraints… ▶ …when present in the rest of the type ⇒ a → a → Bool x ⇒ a → a → Bool x ⇒ x → x → Bool

  33. EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29

  34. EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ( Show a , Num a ) ⇒ a → String foo x = show ( x + 1 )

  35. EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ⇒ a → String foo x = show ( x + 1 )

  36. EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 Inferred constraints: ( Show a , Num a ) foo :: ⇒ a → String foo x = show ( x + 1 )

  37. EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ( Num a , ) ⇒ a → String foo x = show ( x + 1 )

  38. EXTRA-CONSTRAINTS WILDCARD SYNTAX Inferred constraints: Show a 18 / 29 foo :: ( Num a , ) ⇒ a → String foo x = show ( x + 1 )

  39. EXTRA-CONSTRAINTS WILDCARD SYNTAX 19 / 29

  40. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: Show a ⇒ a → a

  41. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: ⇒ a → a

  42. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 Inferred constraints: Show a Inferred: Show a ⇒ a → a bar :: ⇒ a → a

  43. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: ( Num a , ) ⇒ a → a

  44. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 Inferred constraints: Show a Inferred: ( Num a , Show a ) ⇒ a → a bar :: ( Num a , ) ⇒ a → a

  45. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x Proposed simplification: ignore annotated constraints 19 / 29 Inferred constraints: Show a Inferred: ( Num a , Show a ) ⇒ a → a bar :: ( Num a , ) ⇒ a → a

  46. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x Proposed simplification: ignore annotated constraints 19 / 29 Inferred constraints: Show a Inferred: ( ✘✘✘✘ ✘ Num a , Show a ) ⇒ a → a bar :: ( Num a , ) ⇒ a → a

  47. EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x Proposed simplification: ignore annotated constraints 19 / 29 Inferred constraints: Show a Inferred: Show a ⇒ a → a bar :: ( Num a , ) ⇒ a → a

  48. FORMALISATION Partial Type Signatures for Haskell . Thomas Winant, Dominique Devriese, Frank Piessens, Tom Schrijvers. In Practical Aspects of Declarative Languages 2014 (PADL’14) 20 / 29

  49. IDEA FORMALISATION 21 / 29

  50. IDEA FORMALISATION secondArg x = x 21 / 29 secondArg :: → → Bool

  51. IDEA FORMALISATION secondArg x = x 21 / 29

  52. IDEA = type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ

  53. IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� �

  54. IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ]

  55. IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ] ⇒ secondArg :: α → β → β

  56. IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ] ⇒ secondArg :: α → β → β ⇒ Generalise: secondArg :: ∀ a b . a → b → b

  57. IDEA = Constraints type FORMALISATION x 21 / 29 x secondArg :: → → Bool β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ] ⇒ secondArg :: α → β → β ⇒ Generalise: secondArg :: ∀ a b . a → b → b

Recommend


More recommend