Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking and comparing two Racket contracts?

Tags:

racket

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

(equal? (flat-contract integer?) (flat-contract integer?))
like image 765
JRR Avatar asked Aug 26 '26 03:08

JRR


1 Answers

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
like image 73
Alex Knauth Avatar answered Aug 31 '26 04:08

Alex Knauth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!