TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ a ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ ] [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ a ] → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: → [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 filter :: [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
TYPE WILDCARDS SYNTAX filter filter pred xs 12 / 29 [ ] = [ ] filter pred ( x : xs ) | pred x = x : filter pred xs | otherwise =
NAMED WILDCARDS SYNTAX filter = filter pred x xs pred x = x filter pred xs otherwise = filter pred xs 13 / 29
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 =
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 =
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 =
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 =
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 =
NAMED WILDCARDS SYNTAX 14 / 29 eq :: Eq a ⇒ a → a → Bool eq x y = x ≡ y
NAMED WILDCARDS SYNTAX 14 / 29 eq :: Eq x ⇒ x → x → Bool eq x y = x ≡ y
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
NAMED WILDCARDS SYNTAX x 14 / 29 eq :: Eq x ⇒ x → x → eq x y = x ≡ y
NAMED WILDCARDS SYNTAX x 14 / 29 Inferred: Eq Bool ⇒ Bool → Bool → Bool eq :: Eq x ⇒ x → x → eq x y = x ≡ y
NAMED WILDCARDS SYNTAX x 14 / 29 Inferred: Bool → Bool → Bool eq :: Eq x ⇒ x → x → eq x y = x ≡ y
CONSTRAINT WILDCARDS SYNTAX 15 / 29
CONSTRAINT WILDCARDS SYNTAX -- class Eq a => Ord x 15 / 29 bar :: Ord a ⇒ a → a → Bool bar x y = x ≡ y
CONSTRAINT WILDCARDS SYNTAX -- class Eq a => Ord x 15 / 29 bar :: Ord ⇒ a → a → Bool bar x y = x ≡ y
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
CONSTRAINT WILDCARDS SYNTAX 16 / 29
CONSTRAINT WILDCARDS SYNTAX 16 / 29 foo :: ( Show a , Num a ) ⇒ a → String foo x = show ( x + 1 )
CONSTRAINT WILDCARDS SYNTAX 16 / 29 foo :: a ⇒ a → String foo x = show ( x + 1 )
CONSTRAINT WILDCARDS SYNTAX 16 / 29 Infer? Show a ⇒ a → String foo :: a ⇒ a → String foo x = show ( x + 1 )
CONSTRAINT WILDCARDS SYNTAX 16 / 29 Infer? Num a ⇒ a → String foo :: a ⇒ a → String foo x = show ( x + 1 )
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
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
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
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
EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29
EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ( Show a , Num a ) ⇒ a → String foo x = show ( x + 1 )
EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ⇒ a → String foo x = show ( x + 1 )
EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 Inferred constraints: ( Show a , Num a ) foo :: ⇒ a → String foo x = show ( x + 1 )
EXTRA-CONSTRAINTS WILDCARD SYNTAX 18 / 29 foo :: ( Num a , ) ⇒ a → String foo x = show ( x + 1 )
EXTRA-CONSTRAINTS WILDCARD SYNTAX Inferred constraints: Show a 18 / 29 foo :: ( Num a , ) ⇒ a → String foo x = show ( x + 1 )
EXTRA-CONSTRAINTS WILDCARD SYNTAX 19 / 29
EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: Show a ⇒ a → a
EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: ⇒ a → a
EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 Inferred constraints: Show a Inferred: Show a ⇒ a → a bar :: ⇒ a → a
EXTRA-CONSTRAINTS WILDCARD SYNTAX bar x = show x 19 / 29 bar :: ( Num a , ) ⇒ a → a
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
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
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
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
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
IDEA FORMALISATION 21 / 29
IDEA FORMALISATION secondArg x = x 21 / 29 secondArg :: → → Bool
IDEA FORMALISATION secondArg x = x 21 / 29
IDEA = type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ
IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� �
IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ]
IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ] ⇒ secondArg :: α → β → β
IDEA = Constraints type FORMALISATION x 21 / 29 x β � �� � ���� : α → β → γ secondArg ���� ���� ���� α β γ ⇝ ( β ∼ γ ) � �� � Solve the constraints: [ γ �→ β ] ⇒ secondArg :: α → β → β ⇒ Generalise: secondArg :: ∀ a b . a → b → b
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