9
votes

I defined a function true? for use with count in racket/list.

(define (true? expr)
  (and (boolean? expr) expr #t))

I noticed I could provide it numeric arguments and my function would happily return #f.

> (true? 6)
#f

So, I thought I would explore using a racket contract to make non-boolean arguments return an error in contract violation. So I put this code at the tope of my file:

(provide (contract-out
          [true?         (-> boolean? boolean?)]))

However, after adding the contract I still get the same behavior as above in the racket REPL. I don't understand how that could be. What am I missing?

2
Note that for your particular context, you may be able to use values, since Racket treats everything as true except for #f. E.g.: (count values '(how many #f true #f #f things #f))dyoo

2 Answers

21
votes

Contracts are usually enforced between modules. So you would have to try it from the outside perspective. The REPL, however, applies from inside the module you're working in.

An easy way to test from the outside is to use a test submodule. For example:

#lang racket

(define (true? expr)
  (and (boolean? expr) expr #t))

(provide (contract-out
          [true?         (-> boolean? boolean?)]))   

(module* test racket/base
  (require (submod "..")
           rackunit)
  (check-true (true? #t))
  (check-false (true? #f))
  (check-exn exn:fail:contract? (lambda () (true? 3))))

Change the contracts and re-run in DrRacket, and you should see your contracts in effect here, since the test module here is being treated as an external customer of the contract.


Alternatively, make another file that requires the first, and then you can see the effect of contracts there too. If the first file is called true-test.rkt, then you can make another module, and then:

 #lang racket
 (require "true-test.rkt")
 (true? 42)  ;; And _this_ should also raise a contract error.
14
votes

Danny Yoo gave an excellent answer. I just wanted to expand on it and note that Racket does give you more flexibility over where your contract is enforced (i.e., where to put the contract boundary). For example, you can use the define/contract form:

-> (define/contract (true? expr)
     (-> boolean? boolean?)
     (and (boolean? expr) expr #t))

which will establish contract checking between the definition of true? and all other code:

-> (true? "foo")
; true?: contract violation
;  expected: boolean?
;  given: "foo"
;  in: the 1st argument of
;       (-> boolean? boolean?)
;  contract from: (function true?)
;  blaming: top-level
;  at: readline-input:1.18
; [,bt for context]

I find define/contract particularly useful if I want to test something related to contracts at the REPL, where I don't always have a module. However, contract-out is the default recommendation because checking contracts at module boundaries is usually a good choice.