9

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 ?
F. Zer
  • 1,081
  • 7
  • 9
  • I'm not qualified to write an answer myself, but google-chasing some terminology from the comments before that definition leads to https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/representation_polymorphism.html (6.4.12.2 makes a specific example of `($)`). – hobbs Jul 05 '22 at 22:13
  • 1
    `TYPE` allows for representation polymorphism. `Type` (formerly known as `*`) is just a type alias for `TYPE LiftedRep`. As you might guess from that example, there are other things (of kind `RuntimeRep`) that you can pass to `TYPE` to get other "kinds" of types. (Kind in quotes just to emphasize that I'm not talking about kinds in the formal sense.) – chepner Jul 05 '22 at 22:15
  • 1
    (Above comment is just a rough summary of the first couple of paragraphs in the page hobbs linked to.) – chepner Jul 05 '22 at 22:16
  • I realize that "levity" means "liftedness" here, but "levity polymorphism" still reads to me like "it works on funny, punny, serious, and downright unfunny types." :) – hobbs Jul 05 '22 at 22:36
  • Related: https://stackoverflow.com/questions/56311510/what-does-type-signature-for-undefined-mean-in-haskell – danidiaz Jul 06 '22 at 18:54

1 Answers1

8

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.

chepner
  • 497,756
  • 71
  • 530
  • 681