我可以为->编写更高阶的类型吗? b-> *? [英] Can I write a higher order type for a -> b -> *?
问题描述
我知道(->) a
是类型* -> *
的高阶类型,将其应用于类型实参b
会得到类型a -> b
I understand that (->) a
is a higher order type of kind * -> *
, that when applied to a type argument b
gives the type a -> b
我可以写一种类型的* -> *
应用于c
时会给出a -> b -> c
吗?
Can I write a type of kind * -> *
that when applied to c
would give a -> b -> c
?
如果没有,为什么不呢?也许使用某些语言扩展名和forall
?
If not, why not? Maybe using some language extensions and forall
?
这将允许我编写Functor和Applicative(以及其他类)的实例,其中函子结构为"a -> b ->
",如下所示:
This would let me write instances of Functor and Applicative (and other classes) where the functorial structure is "a -> b ->
" as in:
(<*>) :: Applicative t => t (c -> d) -> t c -> t d
(<*>) :: (a -> b -> c -> d) -> (a -> b -> c) -> a -> b -> d
作为二进制(咖喱)函数的组合器,这将很有用.
This would be useful as a combinator for binary (curried) functions.
NB.也许与有关类型(*-> *)的类的Functors和Applicatives相关. -> * 但我不确定,因为它越过我的头:-)
NB. Maybe this is related Functors and Applicatives for types of kind (* -> *) -> * but I'm not sure, because it went over my head :-)
推荐答案
不,您不能.您可以想象有许多语言功能可以支持此功能.例如类型级的lambda会很自然:
No, you can't. You could imagine many language features to support this; e.g. type-level lambdas would be a natural one:
instance Functor (\c. a -> b -> c) where ...
不幸的是,类型级别的lambda意味着我们必须在类型推断期间从一阶统一转变为高阶统一,这非常困难. (我想说的是犹豫不决,但我不确定.)
Unfortunately, type-level lambdas would mean we have to move from first-order unification to higher-order unification during type inference, which is notably difficult. (I want to say undecidable off the top of my head, but I'm not certain of this.)
如果通过newtype
包装器插入显式的类型检查提示,则可以到达一半.标准的是 Compose
:
You can get halfway there if you insert an explicit type-checking hint by way of a newtype
wrapper. The standard one is Compose
:
a -> b -> c ~= Compose (a ->) (b ->) c
\c. a -> b -> c ~= Compose (a ->) (b ->)
实际上,Compose (a ->) (b ->)
的Functor
和Applicative
实例恰好是您希望\c. a -> b -> c
的实例,但是在创建和使用此类型的值时会花费一些语法噪音.
And, indeed, the Functor
and Applicative
instances for Compose (a ->) (b ->)
are exactly the ones you would expect for \c. a -> b -> c
, at the cost of a bit of syntactic noise when creating and consuming values of this type.
这篇关于我可以为->编写更高阶的类型吗? b-> *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!