简体   繁体   English

代表管道的函数列表的类型化 Racket 类型

[英]Typed Racket type for list of functions representing pipeline

I have a function in untyped Racket that takes arguments representing an input, a list of functions and symbols that represent a pipeline, and a symbol to stop applying functions before (I know that's not very clear but the code is beneath).我在无类型 Racket 中有一个 function,它采用代表输入的 arguments、代表管道的函数和符号列表以及停止应用函数的符号(我知道这不是很清楚,但代码在下面)。 I'm trying to convert it to Typed Racket, but can't figure out how to get the type system to understand what's going on.我正在尝试将其转换为 Typed Racket,但无法弄清楚如何让类型系统了解正在发生的事情。 I know I can just use cast to force it to work, but I wonder if there is a better way?我知道我可以使用cast来强制它工作,但我想知道是否有更好的方法?

(define (apply-steps on steps stop-symbol)
  (match steps
    ['() on]
    [(cons step rst)
     (cond
       [(equal? stop-symbol (car step)) ((cadr step) on)]
       [else (apply-steps ((cadr step) on) rst stop-symbol)])]))

I need to somehow specify a type for the signature something like:我需要以某种方式指定签名的类型,例如:

(apply-steps [on : T1] 
             [steps : (List (Pairof Symbol (-> T1 T2)) (Pairof Symbol (-> T2 T3)) ... (Pairof Symbol (-> Tn-1 Tn))]
             [stop-symbol : Symbol]) : Tn

Thanks!谢谢!

There are a couple of problems:有几个问题:

  • One of your inputs is essentially a list of functions.您的输入之一本质上是一个函数列表。 You want it to have type (List (-> F1 F2) (-> F2 F3)... (-> Fn Fn+1)) , but I don't think Typed Racket can support this kind of things.您希望它具有类型(List (-> F1 F2) (-> F2 F3)... (-> Fn Fn+1)) ,但我不认为 Typed Racket 可以支持这种事情。 If you write it out, it involves unbounded number of type variables, depending on the length of the list?如果你写出来,它涉及无限数量的类型变量,取决于列表的长度? How can you even finitely specify the type.[1].你怎么能有限地指定类型。[1]。

    To workaround this, I suggest you changing the requirement to (List (-> AA) (-> AA)... (-> AA)) = (Listof (-> AA)) .要解决此问题,我建议您将要求更改为(List (-> AA) (-> AA)... (-> AA)) = (Listof (-> AA))

  • According to the given type, each element is a (Pairof Function Symbol) , but from your function, it looks like it expects a (List Symbol Function) (this is caught of Typed Racket when I tried out your program).根据给定的类型,每个元素都是一个(Pairof Function Symbol) ,但是从您的 function 来看,它看起来需要一个(List Symbol Function) (当我试用您的程序时,这是由 Typed Racket 捕获的)。

Applying the fixes, you would get:应用修复程序,您将获得:

(: apply-steps (All (A) (-> A (Listof (List Symbol (-> A A))) Symbol A)))
(define (apply-steps on steps stop-symbol)
  (match steps
    ['() on]
    [(cons step rst)
     (cond
       [(equal? stop-symbol (car step)) ((cadr step) on)]
       [else (apply-steps ((cadr step) on) rst stop-symbol)])]))

By the way, if you use regular Racket, you can just use for/fold with a #:final clause to implement this function too.顺便说一句,如果你使用常规的 Racket,你也可以使用for/fold#:final子句来实现这个 function。

(define (apply-steps on steps stop-symbol)
  (for/fold ([on on]) ([step (in-list steps)])
    (match-define (list sym f) step)
    #:final (equal? stop-symbol sym)
    (f on)))

Unfortunately, #:final is not supported by Typed Racket (yet), so you can't use it here.不幸的是,Typed Racket(还)不支持#:final ,所以你不能在这里使用它。

[1]: OK, so one possibility might be: for any two adjacent elements in the list, they must be (-> AB) and (-> BC) for some A , B , C . [1]:好的,所以一种可能性可能是:对于列表中的任何两个相邻元素,对于某些ABC ,它们必须是(-> AB)(-> BC) Still, I don't think Typed Racket can support arbitrary higher-order predicate like this.不过,我不认为 Typed Racket 可以支持像这样的任意高阶谓词。

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

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