[racket] Typed Racket: verbose error message?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Fri Feb 18 18:56:24 EST 2011

I was working on the following snippet of code in Typed Racket:

(define-type InstructionSequence (U Symbol instruction-sequence))

(define-struct: instruction-sequence ([needs : (Listof Symbol)]
                                      [modifies : (Listof Symbol)]
                                      [statements : (Listof Any)])

(: registers-modified (InstructionSequence -> (Listof Symbol)))
(define (registers-modified s)
  (if (symbol? s) '() (instruction-sequence-modifies s)))

(: modifies-register? (InstructionSequence Symbol -> Boolean))
(define (modifies-register? seq reg)
  (memq reg (registers-modified seq)))

Typed Racket responded with a message that said:

. Type Checker: Polymorphic function memq could not be applied to arguments:
Argument 1:
  Expected: a
  Given:    Symbol
Argument 2:
  Expected: (Listof a)
  Given:    (Listof Symbol)

Result type:     (U (Listof a) False)
Expected result: Boolean
 in: (memq reg (registers-modified seq))

It took me a long time to finally figure out that the error was trying
to tell me that the result type was wrong, because the parts of the
error message that was talking about Argument 1 and Argument 2
distracted me from looking at the rest of the error message.  Can this
be improved?

(The fix to my code is to make sure the result is boolean by doing
(and (memq ...) #t).

Posted on the users mailing list.