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

标签 haskell

我所说的“类型函数参数”是指这个

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

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

> :kind Wrapper Maybe Int
Wrapper Maybe Int :: *

现在对我来说问题是我实际上在 Wrapper 的值中使用了 f,而我想忽略它:

> newtype Wrapper f a = Wrapper a
> :kind Wrapper
Wrapper :: * -> * -> *

你猜怎么着! 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’

那么如何以相同的方式构造类型(Wrapper Maybe Int),而不需要在我的 Wrapper 值中包含具体的 Maybe 值?

最佳答案

原来我只需要使用语言扩展:

> {-# LANGUAGE KindSignatures #-}

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

> :kind Wrapper
Wrapper :: (* -> *) -> * -> *

关于haskell - 声明具有未使用的类型函数参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507964/

相关文章:

haskell - 向堆栈项目添加依赖项?

haskell - 在 Haskell 中设置 argv[0]?

Haskell:为具有许多字段的数据类型定义适当的接口(interface)

haskell - 避免繁琐的Ord实现

Haskell 加法运算

haskell - 非平衡二叉树

list - Haskell 无限递归

haskell - 在 Haskell 中处理复杂组合的 POD(OO 中的普通旧数据)的推荐方法是什么?

haskell - 在 Haskell 中生成随机数

haskell - 将语义应用于自由单子(monad)