哈斯克尔中的“foreach”

Rei*_*ica 2 haskell dependent-type

此代码类型检查:

type T :: Type -> foreach (r :: Type) -> Type -> Type
data T k r a

type T' :: Type -> Type
type T' a = T a Identity a
Run Code Online (Sandbox Code Playgroud)
  • 为什么编译器在这里需要一个 kind Type -> Type(即Identity)类型?Identity如果您替换为 say ,它无法进行类型检查Int
  • 什么是foreach关键字,在 Haskell 中如何使用它?

lef*_*out 7

它不是关键字,只是隐式量化的类型变量。就像你写一个函数一样

foo :: Int -> foreach r -> Double
foo = undefined

main = print ( foo 123 (Just 'w')
             , foo 789 [False, True, False] )
Run Code Online (Sandbox Code Playgroud)

...soforeach r可以与Maybe Char或统一[Bool],但不能统一,例如Float

现在,在您的示例中,情况更加奇怪,因为您还没有应用任何Identity内容。这意味着foreachin 的类型T a Identity a实际上似乎是s签名->中部分应用的运算符,提升了一级......时髦的东​​西。IdentityType -> Type

  • *打脸*当然!不过,`foreach` *将*成为一个关键字,作为 [dependent haskell](https://gitlab.haskell.org/ghc/ghc/-/wikis/dependent-haskell)的一部分。 (2认同)