我可以使用哪种类型的环境允许对函数的不同调用来假设不同的polymoprohic类型

发布于 2025-01-25 04:41:33 字数 1269 浏览 1 评论 0原文

这个问题连接到我问的另一个问题在这里

但是这个问题更集中在我在试图理解不同类型注释(例如(类型a))中写的这一简单的代码: vers :type a。

let f = fun id x -> 
    let i = id 4 in
    let f = id 4.0 in
       (Int.to_float i) +. f;;

此代码不键入检查。我知道这是因为Checker“ Infers” ID:int-> ...来自让i = id 4与推断id:float-> ...在下一行上。

虽然我希望上述代码以某种方式告诉函数id是类型a - >允许这种类型在呼叫站点之间变化。 (即,我们可以在每个调用ID上选择其他'a)。

我收集到诸如类型A。(类型A)之类的注释:是否精确地让我以某种方式告诉类型的检查器。

这是我尝试的一件事。我注释了id的“绑定发生”,以使用typea。尝试使其“显式多构态”。但是语法不会让我实际使用type a。 ...作为类型注释。

let f = fun (id : type a.a -> a) x -> begin 
               (* ^^^^ syntax error: type expected *) 
         let i = id 4 in
         let f = id 4.0 in
         (Int.to_float i) +. f
end;;

有点混乱/具有讽刺意味的错误。它指着“类型”一词,并说有期望的类型。我收集错误意味着它期望“类型”而不是关键字type。但是我以为(我想错误地)类型A。 ...实际上将被视为有效的“类型”。

因此,最后一个问题有一些的方法来注释我的代码并使其键入检查吗?还是不可能?

This question is connected to another question I asked here.

But this question is a bit more focused on this simple piece of code that I wrote in trying to understand the different type annotations like (type a): versus : type a..

let f = fun id x -> 
    let i = id 4 in
    let f = id 4.0 in
       (Int.to_float i) +. f;;

This code doesn't type check. I understand that is because the checker 'infers' id : int -> ... from let i = id 4 but that conflicts with inferring id : float -> ... on next line.

Intuitively though I would expect the above code to be 'fine' if we can somehow tell the type system that the function id is of type a -> a and this type is allowed to vary between call sites. (i.e. we can pick a different 'a on every call to id).

I gather that annotations like type a. and (type a) : are there precisely to let me tell that to the type checker somehow.

Here's one of the things I tried. I annotated the 'binding occurrence' of id to use type a. to try and make it 'explicitly polymorphic'. But the syntax won't let me actually use type a. ... there as a type annotation.

let f = fun (id : type a.a -> a) x -> begin 
               (* ^^^^ syntax error: type expected *) 
         let i = id 4 in
         let f = id 4.0 in
         (Int.to_float i) +. f
end;;

A bit confusing/ironic that error. It points at the word 'type' and says that what is expected there is type. I gather the error means it expects 'a type' rather than the keyword type. However I thought (mistakenly I guess) that type a. ... would actually considered as a valid 'type'.

So, finally the question is there some way to annotate my piece of code and make it type check? Or is it not possible?

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

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

发布评论

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

评论(1

无名指的心愿 2025-02-01 04:41:33

这里的根本问题是,这需要更高的多态性,OCAML不隐含地支持。解决方法是通过将函数包裹在记录或对象中来显式,这允许在字段或方法级别上进行通用量化。

type f = { f: 'a. 'a -> 'a }

let f = fun id ->
   let i = id.f 4 in
   let f = id.f 4.0 in
   float_of_int i +. f

请参阅多态性及其局限性 OCAML手册以提供更详细的解释。

The underlying problem here is that this requires higher-rank polymorphism, which OCaml does not support implicitly. The workaround is to make it explicit by wrapping the function in a record or object, which allows for universal quantification on a field or method level.

type f = { f: 'a. 'a -> 'a }

let f = fun id ->
   let i = id.f 4 in
   let f = id.f 4.0 in
   float_of_int i +. f

See the section on Polymorphism and its limitations in the OCaml manual for a more detailed explanation.

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