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

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

问题描述

这个问题开始于

  1. 我翻译的ML for the Working Programmer"(WorldCat) 由 LC PAULSON 到 F# 使用函子作为示例.
  2. 最终希望翻译纯函数数据结构"(WorldCat)作者 Chris Okasaki,使用函子.
  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 可以做函子并与微软合作.网络.
* 请参阅: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#.

编辑

为了更好地理解范畴论和函数式编程之间的关系,请参阅这些问答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# 语言功能(如联合类型)也不能.带有函子的语言(例如,标准 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.

编辑 2:MLton解体 传递代码:defunctorize.fun

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天全站免登陆