如何声明与标准ML中任何功能匹配的类型?

发布于 2025-02-08 18:13:27 字数 609 浏览 3 评论 0原文

我正在尝试声明具有两个条目的记录类型,一个名为idstring,另一种名为algorithm可以是的记录类型任何功能。

根据我研究的内容,我需要使用参数多态性来声明记录中算法条目的类型。这样的事情:

type subject = {algorithm: 'a -> 'b, id: string}

但是,这会产生以下错误,

subject.sml:1: error: 'a has not been declared in type declaration
subject.sml:1: error: 'b has not been declared in type declaration

如何声明与任何函数匹配的函数类型?使用“ alpha”和“ beta”类型是这样做的正确方法?如果是这样,如何指代这些类型?如果没有,这是如何实现的?是否有类似于bool真实仅表示“所有函数”或“任何函数”的类型?

I'm trying to declare a record type that has two entries, one named id that is a string, and another named algorithm that can be any function.

According to what I've investigated, I need to use parametric polymorphism to declare the type of the algorithm entry in the record. Something like this:

type subject = {algorithm: 'a -> 'b, id: string}

But this yields the following error

subject.sml:1: error: 'a has not been declared in type declaration
subject.sml:1: error: 'b has not been declared in type declaration

How can one declare a function type that matches any function? Is using "alpha" and "beta" types the correct way to do this? If so, how does one refer to these types? If not, how is this achieved? Is there a type like bool or real that just means "all functions" or "any function"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2025-02-15 18:13:27

没有类型的意思是“任何功能”;例如,int->真实字符串 - > bool是完全独立的类型,也无法创建两者之间模棱两可的类型。

但是,您可以创建类型函数采用两种类型并返回适当的记录类型:

type ('a, 'b) subject = {algorithm: 'a -> 'b, id: string}

在上述声明之后,(int,real)主题(例如)(例如)将意味着{算法:int->真实,ID:字符串}

使用“ alpha”和“ beta”类型的正确方法?如果是这样,如何指这些类型?

是的 - “ alpha”和“ beta”正是您发音'a'b的方式。它们不是一个单独的概念。 :-)

There's no type that means "any function"; for example, int -> real and string -> bool are completely separate types, and there's no way to create a type that's ambiguous between the two of them.

However, you can create a type function that takes two types and returns the appropriate record type:

type ('a, 'b) subject = {algorithm: 'a -> 'b, id: string}

After the above declaration, (int, real) subject (for example) will mean {algorithm: int -> real, id: string}.

Is using "alpha" and "beta" types the correct way to do this? If so, how does one refer to these types?

Yes — "alpha" and "beta" are just how you pronounce 'a and 'b, respectively. They're not a separate concept. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文