这个 GHC 功能叫什么?类型定义中的“forall”

Rei*_*ica 5 haskell ghc forall

我了解到,您可以ContT变压器中重新定义,以使r类型参数隐式化(并且可以使用显式指定TypeApplications),即:

-- | Same as `ContT` but with the `r` made implicit
type ContT ::
  forall (r :: Type).
  (Type -> Type) ->
  Type ->
  Type
data ContT m a where
  ContT ::
    forall r m a.
    {runContT :: (a -> m r) -> m r} ->
    ContT @r m a

type ContVoid :: (Type -> Type) -> Type -> Type
type ContVoid = ContT @()
Run Code Online (Sandbox Code Playgroud)

我没有意识到这在 GHC 中是可能的。更大的功能是什么,用来指代这种使用隐式类型参数定义类型族的方法,该方法是在类型定义中使用指定的forall(在上面的示例中,指的是外部forall- 而不是forall简单地统一了内部r) ?

Ice*_*ack 5

没有人将其(不可见的相关量化)用于此目的(不使用相关性),但它与隐式给出参数相同Type -> ..

type EITHER :: forall (a :: Type) (b :: Type). Type
data EITHER where
 LEFT  :: a -> EITHER @a @b
 RIGHT :: b -> EITHER @a @b

eITHER :: (a -> res) -> (b -> res) -> (EITHER @a @b -> res)
eITHER left right = \case
 LEFT  a -> left  a
 RIGHT b -> right b
Run Code Online (Sandbox Code Playgroud)

您还可以使用“可见相关量化”,其中forall->是 的可见对应项forall.,所以就像a不出现在..forall (a :: Type) -> ..一样:Type -> ..

type EITHER :: forall (a :: Type) -> forall (b :: Type) -> Type
data EITHER a b where
 LEFT  :: a -> EITHER a b
 RIGHT :: b -> EITHER a b

eITHER :: (a -> res) -> (b -> res) -> (EITHER a b -> res)
eITHER left right = \case
 LEFT  a -> left  a
 RIGHT b -> right b
Run Code Online (Sandbox Code Playgroud)

  • [此](https://downloads.haskell.org/~ghc/9.0.1/docs/html/users_guide/exts/poly_kinds.html?highlight=dependent%20quantification#implicit-quantification-in-type-synonyms-and -type-family-instances) 解释了可见的参数,但这最终是“不使用”量化的常规种类多态性。这是不寻常的,通常量词 *k* 出现在 `Proxy :: forall (k :: Type) 类型的其余部分中。k -> 类型`。如果没有,就像在类型论中,函数箭头“A -> B”是依赖函数“pi (a :: A) -> B”的特例,如果 *a* 没有出现在*B* (3认同)