[racket] Confusing blame message

3 views
Skip to first unread message

Daniel Prager

unread,
Sep 2, 2013, 8:22:34 AM9/2/13
to us...@racket-lang.org
Consider this file:

--------------------------------------------
#lang racket
(define/contract (twice x)
  (-> number? number?)
  (* 2 x))

(twice 'foo)
--------------------------------------------

When run in DrRacket 5.3.6 this yields the following error:

twice: contract violation
 expected: number?
 given: 'foo
 in: the 1st argument of
      (-> number? number?)
 contract from: (function twice)
 blaming: .../blame-test.rkt
 at: .../blame-test.rkt: 2.18

The final line attributes blame to the twice function (line 2, col 18), but clearly the blame in this instance lies with the client code (line 6), consistent with the first six lines of the error message.

Is this the intended behaviour?


Many thanks

Dan

Matthias Felleisen

unread,
Sep 2, 2013, 9:48:03 AM9/2/13
to Daniel Prager, us...@racket-lang.org
This is a misinterpretation of the last line. If you read the entire message as a sentence, you will see that the "at: ..." means "this is where you find the contract that was broken". The "expected:"/"given" pairing means that the function was called, it expected a number, and the caller applied it to the symbol 'foo instead. 

If you run this program

#lang racket
(define/contract (twice x)
  (-> number? number?)
  'foo)

(twice 2)

the error message will say 

twice: broke its contract
  promised: number?
  produced: 'foo
  in: the range of
      (-> number? number?)
  contract from: (function twice)
  blaming: (function twice)
  at: unsaved-editor665:2.18

meaning that foo was called, it promised to return a number, but it produced the symbol 'foo instead. 

;; --- 

I will admit that you're not the first one who has tripped over this error message [format] and that you aren't the last one. But changing it isn't easy and adding information isn't easy. The problem is that we wish to keep the messages uniform across all functions but not all functions can benefit from the actual contract system. 

-- Matthias




Daniel Prager

unread,
Sep 2, 2013, 4:57:56 PM9/2/13
to Matthias Felleisen, us...@racket-lang.org
Hi Matthias

Thanks for the explanation - it helps.  I played with the kind of post-condition "broken contract" example that you gave, and find it less problematic, since the blame is local to the function / supplier.

If the error in my original example was reformatted by moving the at clause to the top (easy?) and supplemented with the line number of the guilty client [or supplier] code (hard?) I think it would reduce the confusion and improve the utility.  I.e.

twice: contract violation 
     at: .../blame-test.rkt - line 2, column 18
     expected: number?
     given: 'foo
     in: the 1st argument of
          (-> number? number?)
     contract from: (function twice)
     blaming: .../blame-test.rkt - line 6, column 1
     
What do you think?


Kind regards

Dan

Matthias Felleisen

unread,
Sep 2, 2013, 5:36:59 PM9/2/13
to Daniel Prager, us...@racket-lang.org

You're right on. Let's see what we can do -- Matthias
Reply all
Reply to author
Forward
0 new messages