为什么没有像IMonad< T>在即将到来的.NET 4.0 [英] Why there is no something like IMonad<T> in upcoming .NET 4.0

查看:136
本文介绍了为什么没有像IMonad< T>在即将到来的.NET 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

......与所有这些新的(和不那么新的,如果再算上了IEnumerable)单子相关的东西?

... with all those new (and not so new if we count IEnumerable) monad-related stuff?

interface IMonad<T>
{
 SelectMany/Bind();
 Return/Unit();
}

这将使编写的任何类型的一元函数操作。或者,它不是那么重要?

That would allow to write functions that operate on any monadic type. Or it's not so critical?

推荐答案

想想什么 IMonad&LT签名; T&GT; 的方法必须是。在Haskell单子类型类定义为

Think about what the signature for IMonad<T>'s methods would have to be. In Haskell the Monad typeclass is defined as

class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b
  return :: a -> m a

这是棘手的直接翻译这一个C#接口,因为你需要能够引用具体实施亚型(MA或 ISpecificMonad&LT; a取代)内定义一般IMonad接口。 OK,而不是想有(例如)的IEnumerable&LT; T&GT; 实施 IMonad&LT; T&GT; 直接,我们' LL尝试融通IMonad实施伸到它可以通过一个单独的对象,以及具体的单子类型的实例,以任何需要把它当作一个单子(这是字典传递风格)。这将是 IMonad&LT; TMonad&GT; 和TMonad这里将不是T在的IEnumerable&LT; T&GT; ,而的IEnumerable&LT; T&GT; 本身。但是等等 - 这能不能成功,因为返回&lt签名; T&GT; 例如有让我们从的任意的T型到一个 TMonad&LT; T&GT; ,为的任意 TMonad&LT;&GT; 。 IMonad必须被定义为类似于

It's tricky to translate this directly to a C# interface because you need to be able to reference the specific implementing subtype ("m a" or ISpecificMonad<a>) within the definition of the general IMonad interface. OK, instead of trying to have (for example) IEnumerable<T> implement IMonad<T> directly, we'll try factoring the IMonad implementation out into a separate object which can be passed, along with the specific monad type instance, to whatever needs to treat it as a monad (this is "dictionary-passing style"). This will be IMonad<TMonad> and TMonad here will be not the T in IEnumerable<T>, but IEnumerable<T> itself. But wait -- this can't work either, because the signature of Return<T> for example has to get us from any type T to a TMonad<T>, for any TMonad<>. IMonad would have to be defined as something like

interface IMonad<TMonad<>> {

    TMonad<T> Unit<T>(T x);
    TMonad<U> SelectMany<T, U>(TMonad<T> x, Func<T, TMonad<U>> f);
}

用一个假设的C#的功能,将允许我们使用的类型构造的(如TMonad&LT;>)作为一般类型参数。当然,但是C#不具备该功能的(高kinded多态性)的。您可以在运行时具体化类型构造(的typeof(IEnumerable的&LT;&GT;)),但没有给他们的参数不能引用他们的类型签名。因此,除了在-100点的事情,实施这种正常就不仅需要增加一个普通的接口定义,但深增加的类型系统。

using a hypothetical C# feature that would allow us to use type constructors (like TMonad<>) as generic type parameters. But of course C# does not have this feature (higher-kinded polymorphism). You can reify type constructors at runtime (typeof(IEnumerable<>)) but can't refer to them in type signatures without giving them parameters. So besides the -100 points thing, implementing this "properly" would require not just adding another ordinary interface definition, but deep additions to the type system.

这是不是使用的接口机制为何有查询COM prehensions过自己的类型的能力是一种黑客攻击的(他们只是神奇的工作,如果有合适的签名正确的魔术方法名是那里)等等。

That's why the ability to have query comprehensions over your own types is kind of hacked on (they just "magically" work if the right magic method names with the right signatures are there) instead of using the interface mechanism etc.

这篇关于为什么没有像IMonad&LT; T&GT;在即将到来的.NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆