checking and comparing two Racket contracts?

荒凉一梦 提交于 2019-12-11 13:29:42

问题


Is there a way to compare them? This doesn't work for instance:

(equal? (flat-contract integer?) (flat-contract integer?))

回答1:


  1. First of all, note that the function flat-contract is for backward-compatibility, so you probably should not use it. From the documentation:

    This function is a holdover from before predicates could be used directly as flat contracts. It exists today for backwards compatibility.

  2. So your question is really to ask if two predicates are the same or not. In general this problem is undecidable due to the halting problem. However, for your purpose, you might be able to get away with referential equality.

    > ;; both are in the same memory address
      (eq? integer? integer?) 
    #t
    > ;; obviously not in the same memory address
      (eq? integer? real?)
    #f
    

    Please note its caveat

    > ;; although they look the same syntactically, 
      ;; the function objects are in different memory address
      (eq? (lambda (x) x) (lambda (x) x))
    #f
    > (define x (lambda (x) x))
    > (define y x)
    > ;; both are in the same memory address
      (eq? x y)
    #t
    > ;; flat-contract creates a function in a new memory address
      (eq? (flat-contract integer?) (flat-contract integer?))
    #f
    



回答2:


For certain kinds of contracts, you can use contract-equivalent?:

> (contract-equivalent? (flat-contract integer?) (flat-contract integer?))
#true
> (contract-equivalent? (and/c integer? positive?) (and/c integer? positive?))
#true
> (contract-equivalent? (or/c integer? string?) (or/c string? integer?))
#true

This returns #true when the contract system can prove that they are equivalent.

However, as the documentation notes, a #false result doesn't mean that they're not equivalent, it just means it doesn't know:

This function is conservative, so it may return #false when c1 does, in fact, accept the same set of values that c2 does.

> (contract-equivalent? integer? integer?)
#true
> (contract-equivalent? (lambda (x) (integer? x))
                        (lambda (x) (integer? x)))
#false


来源:https://stackoverflow.com/questions/53999357/checking-and-comparing-two-racket-contracts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!