繁体   English   中英

GHC 9 中的`forall {..}`

[英]`forall {..}` in GHC 9

这是 GHC 9 中的有效语法。 {..}是什么意思(与 GHC 8.10 在这里要求的(..)不同)?

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

6.4.14.1。 推断与指定类型变量

  • ..指定类型
  • {..}推断类型

forall a. forall {a}. 是 GHC 将通过统一自动实例化的不可见量词。

这意味着编写const True EQ实际上会在没有用户帮助的情况下实例化ab

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

如果用户想要显式实例化它们,他们可以使用可见类型应用程序“覆盖它们的可见性”。 这就是为什么它们是指定的类型。 (尽管“特定”可能是更准确的术语)

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

如果出于某种原因我们只想指定b (没有召唤“蜗牛小队”: @_ ,嗯“ 部分类型签名”),我们可以制作一个推断类型。 然后第一种类型被丢弃

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

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

对于您的示例,这意味着 ghc 必须推断fp的类型。 你不能写ign @IO @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

您必须在实例化MkApply @Type @[] @Int时指定类型k ,但[]Int都暗示了这种类型。

所以你可能更喜欢在MkApply k标记为推断,这样你就可以编写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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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