【问题标题】:"Inference for polymorphic keyword functions not supported"“不支持多态关键字函数的推断”
【发布时间】:2020-07-12 13:38:16
【问题描述】:

以下代码产生名义错误:

(: f (∀ (a) (-> [#:x a] (U Integer a))))
(define (f #:x [x #f]) (or x 0))

(f #:x 3)

好的,但是(f #:x (cast 3 Integer)) 仍然会产生同样的错误。其具体值球拍试图推断的类型变量在哪里,即我需要明确指定其值的类型变量?

注意:我尝试转换 (cast (f #:x (cast 3 Integer)) Integer),但 DrRacket 仅突出显示内部的 (f ...) 表单 wrt 类型错误。

【问题讨论】:

  • 如果你熟悉 Haskell,Haskell 的 :: 的 typed-racket 等效项是 ann,而不是 cast。想想ann 就像::,而cast 更像是通过Data.Dynamic 或其他东西转换类型

标签: parametric-polymorphism typed-racket


【解决方案1】:

解决此问题的方法是注释 f 本身,而不是输入或输出。

此外,您不应为此使用cast,而应使用ann 而不是cast 为表达式分配类型。使用 ann 看起来像 (ann 3 Integer)

注释f 有两种方法,anninst 都可以。

ann

使用ann,您可以将f 的类型细化为其类型的非多态版本。原来的类型是(∀ (a) (-> [#:x a] (U Integer a))),所以带有Integer的非多态版本是(-> [#:x Integer] Integer)。注释f

(ann f (-> [#:x Integer] Integer))

在上下文中,在函数调用中注释f

((ann f (-> [#:x Integer] Integer)) #:x 3)

inst

ann 的示例很长,因为它重写了 f 类型的专用版本。对于专门的多态类型,更短的方法是使用inst

(inst f Integer)

通过将类型参数a 替换为Integer,这会自动为您指定(-> [#:x Integer] Integer) 的类型。在上下文中,在函数调用中注释f

((inst f Integer) #:x 3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2019-07-08
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多