Home > Back-end >  Understanding the type of ($) operator
Understanding the type of ($) operator

Time:07-06

The module GHC/Base.hs contains the following operator definition:

($) :: forall r a (b :: TYPE r). (a -> b) -> a -> b
f $ x =  f x
  • What does the universally quantified variable r mean ?
  • Isn't the signature (a -> b) -> a -> b sufficiently general ?
  • What's TYPE ?

CodePudding user response:

TYPE provides support for representation polymorphism.

The types you are used to have kind Type (formerly known as *). But there are other kinds[*] of types. Type is just an alias for TYPE LiftedRep. We could more formally write the "usual" type for ($) as

($) :: forall (a :: Type) (b :: Type) . (a -> b) -> a -> b
-- ($) :: forall (a :: TYPE LiftedRep) (b :: TYPE LiftedRep) . (a -> b) -> a -> b

The introduction of r just means that we aren't restricting b to "ordinary" types. b doesn't have to have kind TYPE LiftedRep; it can have kind TYPE r for any r that is a valid argument to TYPE.


[*] "kind" in the English sense, not the formal concept called "kind" in Haskell's type system.

  • Related