了解函数签名中的类型相等性
Understanding type equality in function signature
在 GPipe 中有一个 function 用于写入纹理:
writeTexture2D :: forall ctx b c h w os f m. (ContextHandler ctx, MonadIO m, BufferFormat b, ColorSampleable c, BufferColor (Color c (ColorElement c)) h ~ b, h ~ HostFormat b) => Texture2D os (Format c) -> Level -> StartPos2 -> Size2 -> [h] -> ContextT ctx os m ()
我无法理解签名中使用类型相等的部分。有一个 tutorial 谈论它,但没有详细解释我认为显而易见的内容。
BufferColor (Color c (ColorElement c)) h ~ b, h ~ HostFormat b
所以我知道的是:
- 有一个
BufferFormat
的实例用于 b
BufferFormat
class 有关联类型 HostFormat
h ~ HostFormat b
告诉我 h
必须是 HostFormat
对于给定的 b
?
h ~ b
这告诉我 h 和 b 必须是同一类型 ?
但是当我查看 BufferFormat b
的 instances 时,我认为没有任何这样的实例 b
等于它自己的 HostFormat b
?我哪里出错了?
BufferColor (Color c (ColorElement c)) h
必须等于 b
,而不是 h
。
BufferColor
is a two-argument type family。类型族在类型级别的行为有点像函数。您放入类型,并在 return 中接收另一种类型。
并且"type family application"的优先级高于类型相等运算符~
.
在 GPipe 中有一个 function 用于写入纹理:
writeTexture2D :: forall ctx b c h w os f m. (ContextHandler ctx, MonadIO m, BufferFormat b, ColorSampleable c, BufferColor (Color c (ColorElement c)) h ~ b, h ~ HostFormat b) => Texture2D os (Format c) -> Level -> StartPos2 -> Size2 -> [h] -> ContextT ctx os m ()
我无法理解签名中使用类型相等的部分。有一个 tutorial 谈论它,但没有详细解释我认为显而易见的内容。
BufferColor (Color c (ColorElement c)) h ~ b, h ~ HostFormat b
所以我知道的是:
- 有一个
BufferFormat
的实例用于b
BufferFormat
class 有关联类型HostFormat
h ~ HostFormat b
告诉我h
必须是HostFormat
对于给定的b
?h ~ b
这告诉我 h 和 b 必须是同一类型 ?
但是当我查看 BufferFormat b
的 instances 时,我认为没有任何这样的实例 b
等于它自己的 HostFormat b
?我哪里出错了?
BufferColor (Color c (ColorElement c)) h
必须等于 b
,而不是 h
。
BufferColor
is a two-argument type family。类型族在类型级别的行为有点像函数。您放入类型,并在 return 中接收另一种类型。
并且"type family application"的优先级高于类型相等运算符~
.