如何声明与标准ML中任何功能匹配的类型?
我正在尝试声明具有两个条目的记录类型,一个名为id
是string
,另一种名为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有类型的意思是“任何功能”;例如,
int->真实
和字符串 - > bool
是完全独立的类型,也无法创建两者之间模棱两可的类型。但是,您可以创建类型函数采用两种类型并返回适当的记录类型:
在上述声明之后,
(int,real)主题
(例如)(例如)将意味着{算法:int->真实,ID:字符串}
。是的 - “ alpha”和“ beta”正是您发音
'a
和'b
的方式。它们不是一个单独的概念。 :-)There's no type that means "any function"; for example,
int -> real
andstring -> 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:
After the above declaration,
(int, real) subject
(for example) will mean{algorithm: int -> real, id: string}
.Yes — "alpha" and "beta" are just how you pronounce
'a
and'b
, respectively. They're not a separate concept. :-)