简体   繁体   中英

`forall {..}` in GHC 9

This is valid syntax in GHC 9. What do the {..} mean (as distinct from (..) which GHC 8.10 requires here)?

ign :: forall {f :: Type -> Type} {p}. Applicative f => p -> f ()
ign _ = pure ()

See 6.4.14.1. Inferred vs. specified type variables :

  • .. is a specified type
  • {..} is an inferred type

forall a. and forall {a}. are invisible quantifiers that GHC will instantiate automatically by unification.

This means writing const True EQ actually instantiates a and b without the users help.

const :: forall a b. a -> b -> a
const a _ = a

If the user wants to explicitly instantiate them they can "override their visibility", using visible type applications. That's why they are specified types. (although "specifi able " might be a more accurate terminology)

>> :set -XTypeApplications
>> :t const @Bool @Ordering True EQ
const @Bool @Ordering True EQ :: Bool

If for some reason we only want to specify b (without summoning the "snail squad": @_ , umm " partial type signatures ") we can make a an inferred type. Then the first type is dropped

const2 :: forall {a} b. a -> b -> a
const2 a _ = a

>> :t const2 @Ordering True EQ
const2 @Ordering True EQ :: Bool

For your example it means ghc must infer the type of f and p . You cannot write ign @IO @Int .


This becomes more useful when you have kind polymorphism. If you define

type    Apply :: forall (k :: Type). (k -> Type) -> (k -> Type)
newtype Apply f a where
  MkApply :: forall (k :: Type) (f :: k -> Type) (a :: k). f a -> Apply @k f a

you must specify the kind k when instantiating MkApply @Type @[] @Int but this kind is implied by both [] and Int .

so you might prefer marking k as inferred in MkApply so you can write MkApply @[] @Int

type    Apply :: forall (k :: Type). (k -> Type) -> (k -> Type)
newtype Apply f a where
  MkApply :: forall {k :: Type} (f :: k -> Type) (a :: k). f a -> Apply @k f a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM