简体   繁体   English

Typed Racket中是否有不安全的演员function?

[英]Is there an unsafe cast function in Typed Racket?

Is there an unsafe version of the cast function in Typed Racket? Typed Racket 中的cast function 是否存在不安全版本?


I'd like to use this to be able to add a proposition that I logically know about, but the type system can't figure out.我想用它来添加一个我在逻辑上知道的命题,但类型系统无法弄清楚。

For instance, I might have a predicate that checks that a list has exactly two elements:例如,我可能有一个谓词来检查列表是否正好有两个元素:

#lang typed/racket

(: func (-> (Listof String) Boolean))
(define (func x) (eq? (length x) 2))

I'd like to inform the type system of a proposition that the input list has exactly two elements:我想通知一个命题的类型系统,输入列表正好有两个元素:

(: func2 (-> (Listof String) Boolean : (List String String)))
(define func2
  (cast func (-> (Listof String) Boolean : (List String String))))

However, when trying to do this, I get an error like the following:但是,在尝试执行此操作时,我收到如下错误:

Type Checker: Type (-> (Listof String) Boolean : (List String String))
could not be converted to a contract: cannot generate contract for
function type with props or objects.                              
in: (-> (Listof String) Boolean : (List String String))

Is there some version of the cast function where I can tell the type system, "Just trust me here."是否有一些版本的cast function 我可以告诉类型系统,“在这里相信我。”


I'm imagining this function being similar to unsafeCoerce in Haskell.我想象这个 function 类似于unsafeCoerce中的 unsafeCoerce。

It is possible to define an unsafe-cast with the type (All (A) (-> Any A)) by using unsafe-require/typed .可以使用unsafe-require/typed定义类型为(All (A) (-> Any A))unsafe-cast However if you do this, please turn off the optimizer .但是,如果您这样做,请关闭优化器

If the typed racket optimizer is on, it will trust the types to the point of breaking memory safety if they're off.如果类型化球拍优化器打开,它会信任类型,如果它们关闭,则会破坏 memory 安全性。 You might also want to turn off the optimizer not just in the module you use unsafe-cast in, but also other typed modules that interact with it.您可能还希望不仅在使用unsafe-cast的模块中关闭优化器,还希望在与其交互的其他类型模块中关闭优化器。

Turn off the optimizer for a typed racket module using #:no-optimize :使用#:no-optimize关闭类型化球拍模块的优化器:

#lang typed/racket #:no-optimize

And then, with great care, and putting #:no-optimize on probably every typed module in your program, you can use unsafe-require/typed :然后,非常小心,并把#:no-optimize放在程序中可能每个类型的模块上,你可以使用unsafe-require/typed

#lang typed/racket #:no-optimize
(require typed/racket/unsafe)
(unsafe-require/typed racket/function
                      [(identity unsafe-cast)
                       (All (A) (-> Any A))])

However, for your particular example, I personally would try to avoid unsafe-cast by rewriting the function:但是,对于您的特定示例,我个人会尝试通过重写 function 来避免unsafe-cast

(: func2 (-> (Listof String) Boolean : (List String String)))
(define (func2 x)
  (and (cons? x) (cons? (rest x)) (empty? (rest (rest x)))))

This function avoids computing the length of long lists when it doesn't need to, and passes the typechecker with the (List String String) proposition.这个 function 在不需要时避免计算长列表的长度,并使用(List String String)命题通过类型检查器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM