簡體   English   中英

這個 GHC 功能叫什么? 類型定義中的“forall”

[英]What is this GHC feature called? `forall` in type definitions

我了解到您可以從轉換器重新定義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 @()

我沒有意識到這在 GHC 中是可能的。 使用隱式類型參數定義類型族的這種方式的更大特征是什么,這是在類型定義中使用forall指定的(在上面的示例中,指的是外部forall - 而不是內部forall簡單地統一r )?

沒有人為此目的(不使用依賴項)使用此(不可見的依賴量化),但它與隱式給出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

您還可以使用“可見依賴量化”,其中forall->forall. ,所以forall (a:: Type) ->..Type ->..一樣,其中a沒有出現在..中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM