如果SML.NET有函子,为什么F#不能? [英] If SML.NET had functors why can't F#?

查看:130
本文介绍了如果SML.NET有函子,为什么F#不能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题始于

  1. 我翻译的"工作程序员的ML "( WorldCat )中使用了仿函数的F#.
  2. 最终希望翻译"纯功能数据结构"( WorldCat )使用函子.
  3. 阅读"类别和结构-类别理论导论为工作中的计算机科学家"( WorldCat ),作者是Andrea Asperti和Giuseppe Longo.
  4. 不了解所有内容,主要是类别理论.
  1. My translating of "ML for the Working Programmer" (WorldCat) by L. C. PAULSON to F# which uses functors for the examples.
  2. Eventual desire to translate "Purely Functional Data Structures" (WorldCat) by Chris Okasaki which uses functors.
  3. Reading "CATEGORIES TYPES AND STRUCTURES - An Introduction to Category Theory for the working computer scientist" (WorldCat) by Andrea Asperti and Giuseppe Longo.
  4. Not understanding it all, mostly the category theory.

SML.NET 可以做仿函数并且可以与Microsoft一起使用. NET.
*请参阅: SML.NET用户指南第4.8节. 2类的类型和函子?

SML.NET can do functors and worked with Microsoft .NET.
* See: SML.NET User Guide Section 4.8.2 Class types and functors?

由于Microsoft .NET中的某些限制,我一直看到F#不能做真正的仿函数.
* 可以在.NET(C#/F#)中对ML函数进行完全编码吗?
* 函子是否有解决方法?

I keep seeing that F# cannot do true functors because of some limitation in Microsoft .NET.
* Can ML functors be fully encoded in .NET (C#/F#)?
* Any workaround for functor?

因此,如果SML.NET可以在.NET上做函子,那为什么不能F#? SML.NET做了F#不能做什么?

So if SML.NET could do functors on .NET then why can't F#? What did SML.NET do that F# can't?

我对范畴论中的函子了解得越多,我就越了解它们的美,并渴望将其包含在F#中.

The more I learn about functors coming from category theory, the more I see the beauty of them and desire to have them in F#.

编辑

为了更好地理解类别理论与函数式编程之间的关系,请参见以下 Q& A CS:StackExchange .

In a pursuit to better understand the relation between category theory and functional programming see these Q&A at CS:StackExchange.

推荐答案

.NET没有基本限制,可以阻止函子在F#中实现.没错,它们不能直接在.NET元数据中表示,但是其他F#语言功能(如联合类型)也不能.具有函子的语言(例如Standard ML,OCaml)的编译器具有名为 defunctorize 的过程;它就像C ++模板扩展一样,通过将函子专门化为常规模块来展平"函子.

There's no fundamental limitation of .NET that stops functors from being implemented in F#. True, they can't be represented directly in .NET metadata, but neither can other F# language features like union types. Compilers for languages with functors (e.g., Standard ML, OCaml) have a pass called defunctorize; it works just like C++ template expansion, in that it "flattens" the functors by specializing them into normal modules.

F#编译器可以执行相同的操作,但是您必须问:它将如何在其他.NET语言中公开?由于函子不能在.NET类型系统中直接进行 编码,因此您需要想出一些方法来表示它们.如果该表示法很难/不可能在C#或VB.NET中使用,则包含F#仿函数仍然有意义吗? F#成功的重要部分在于它能够轻松地与C#和VB.NET(双向)互操作.

The F# compiler could do the same thing, but you then have to ask: how will this be exposed to other .NET languages? Since functors can't be directly encoded in the .NET type system, you'd need to come up with some way to represent them; and if that representation is difficult/impossible to use from C# or VB.NET, would it still make sense to include F# functors? A non-trivial part of F#'s success comes from it's ability to easily interop (in both directions) with C# and VB.NET.

不要误会我的意思-我很想在F#中使用仿函数,它们对于处理一些目前非常痛苦和/或没有它们无法实现的情况非常有用.我只是指出,该语言还没有(也许永远不会)有函子的主要原因是互操作性问题尚未解决.元数据编码问题实际上是最简单的部分.

Don't get me wrong -- I'd love to have functors in F#, they'd be really useful to handle a few cases which are currently painful and/or impossible to implement without them. I'm just pointing out that the main reason the language doesn't yet (and maybe won't ever) have functors is that the interop issue hasn't been solved; the metadata-encoding issue is actually the easy part.

MLton 解压缩阶段的代码:

EDIT 2: Code for the defunctorize pass of MLton: defunctorize.fun

更新:我曾考虑过在.NET类型系统中实际上如何表达函子,因此我进行了一些实验.它不是很漂亮,但是可以工作-所以现在我们知道,至少有一天F#可以支持函子是合理的.实际上,您在实验代码中看到的复杂性将全部被编译器/语言隐藏.如果您想检查一下: experimental-functors

Update: I had a thought about how functors actually could be expressed within the .NET type system, so I put together a little experiment. It isn't pretty, but it works -- so now we know it's at least plausible that F# could one day support functors. In practice, the complexity you see in my experimental code would all be hidden by the compiler/language. If you want to check it out: experimental-functors

这篇关于如果SML.NET有函子,为什么F#不能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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