這是 GHC 9 中的有效語法。這是什么{..}意思(與(..)GHC 8.10 在這里要求的不同)?
ign :: forall {f :: Type -> Type} {p}. Applicative f => p -> f ()
ign _ = pure ()
uj5u.com熱心網友回復:
見6.4.14.1。推斷與指定型別變數:
從 9.0.1 版本開始,GHC 允許將用戶撰寫的型別或種類變數標記為inferred,而不是默認的specified。通過在大括號中將型別變數 binder 寫為
{tyvar}or{tyvar :: kind},新變數將被歸類為推斷的,而不是指定的。
..是指定型別{..}是推斷型別
forall a.并且forall {a}.是 GHC 將通過統一自動實體化的無形量詞。
const :: forall a b. a -> b -> a
const a _ = a
這意味著在沒有用戶幫助的情況下const True EQ實體化a ( @Bool) 和b ( )。@Ordering
如果用戶想要顯式實體化它們,他們可以使用可見型別應用程式“覆寫它們的可見性”。這就是為什么它們是指定的型別。(盡管“特定”可能是更準確的術語)
>> :set -XTypeApplications
>> :t const @Bool @Ordering True EQ
const @Bool @Ordering True EQ :: Bool
如果出于某種原因我們只想指定b(不呼叫“蝸牛小隊”:@_,嗯“部分型別簽名”),我們可以制作a推斷型別。然后第一種型別被丟棄
const2 :: forall {a} b. a -> b -> a
const2 a _ = a
>> :t const2 @Ordering True EQ
const2 @Ordering True EQ :: Bool
對于您的示例,這意味著 ghc 必須推斷f和p的型別。你不能寫ign @IO @Int。
當你有一種多型性時,這變得更加有用。如果你定義
-- MkApply @Type @[] @Int :: [Int] -> Apply @Type [] Int
-- MkApply @_ @[] @Int :: [Int] -> Apply @Type [] 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
you must specify the kind k when instantiating MkApply @Type @[] @Int but this kind is implied by both [] and Int. You might prefer marking k as inferred in MkApply so you can write MkApply @[] @Int
-- MkApply @[] @Int :: [Int] -> Apply @Type [] 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427293.html
上一篇:按型別列印資料型別的結構
下一篇:什么是單位型別?
