声明具有未使用的类型函数参数的类型

MnZ*_*ZrK 3 haskell

通过'type function argument'我的意思是这个

> newtype Wrapper f a = Wrapper (f a)
> :kind Wrapper
Wrapper :: (* -> *) -> * -> *
Run Code Online (Sandbox Code Playgroud)

所以f这是一个类型函数参数,所以我可以像这样构造类型

> :kind Wrapper Maybe Int
Wrapper Maybe Int :: *
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我实际使用f的是值Wrapper,我想忽略它:

> newtype Wrapper f a = Wrapper a
> :kind Wrapper
Wrapper :: * -> * -> *
Run Code Online (Sandbox Code Playgroud)

你猜怎么着!f不再是类型函数,导致我以前的类型构造失败:

> :kind Wrapper Maybe Int
<interactive>:1:9: error:
    • Expecting one more argument to ‘Maybe’
      Expected a type, but ‘Maybe’ has kind ‘* -> *’
    • In the first argument of ‘Wrapper’, namely ‘Maybe’
      In the type ‘Wrapper Maybe Int’
Run Code Online (Sandbox Code Playgroud)

那么如何构造类型相同的way(Wrapper Maybe Int)而不需要Maybe在我的Wrapper值中具有具体的值?

MnZ*_*ZrK 7

结果我只需要使用语言扩展:

> {-# LANGUAGE KindSignatures #-}

> newtype Wrapper (f :: * -> *) a = Wrapper a

> :kind Wrapper
Wrapper :: (* -> *) -> * -> *
Run Code Online (Sandbox Code Playgroud)

  • 您还可以启用`PolyKinds`,然后`Wrapper`在`f`中变为类型多态. (5认同)