svn: r7982

This commit is contained in:
Matthias Felleisen 2007-12-12 23:41:46 +00:00
parent 75b2415a96
commit cc4d5cfdc1

View File

@ -1,74 +1,81 @@
#cs(module error mzscheme #lang mzscheme
(require (lib "etc.ss") (lib "list.ss"))
;; --------------------------------------------------------------------------
(provide check-arg check-arity check-proc check-result check-list-list find-non
tp-exn? number->ord)
;; (_ -> Boolean) (listof X) -> (union X false) (require (lib "etc.ss")
(define (find-non pred? l) (lib "list.ss"))
(let ([r (filter (compose not pred?) l)])
(if (null? r) #f (car r))))
#| Tests ------------------------------------------------------------------ ;; --------------------------------------------------------------------------
(provide check-arg check-arity check-proc check-result check-list-list
natural?
find-non tp-exn? number->ord)
(define (natural? w)
(and (number? w) (integer? w) (>= w 0)))
;; (_ -> Boolean) (listof X) -> (union X false)
(define (find-non pred? l)
(let ([r (filter (compose not pred?) l)])
(if (null? r) #f (car r))))
#| Tests ------------------------------------------------------------------
(not (find-non list? '((1 2 3) (a b c)))) (not (find-non list? '((1 2 3) (a b c))))
(symbol? (find-non number? '(1 2 3 a))) (symbol? (find-non number? '(1 2 3 a)))
(symbol? (find-non list? '((1 2 3) a (b c)))) (symbol? (find-non list? '((1 2 3) a (b c))))
|# |#
(define-struct (tp-exn exn) ()) (define-struct (tp-exn exn) ())
(define (tp-error name fmt . args)
(raise (make-tp-exn (string-append (format "~a: " name) (apply format fmt args))
(current-continuation-marks))))
(define (number->ord i) (define (tp-error name fmt . args)
(if (= i 0) (raise (make-tp-exn (string-append (format "~a: " name) (apply format fmt args))
"zeroth" (current-continuation-marks))))
(case (modulo i 10)
[(0 4 5 6 7 8 9) (format "~ath" i)]
[(1) (format "~ast" i)]
[(2) (format "~and" i)]
[(3) (format "~ard" i)])))
;; Symbol (union true String) String X -> void
(define (check-list-list pname condition pred given)
(when (string? condition)
(tp-error pname (string-append condition (format "~nin ~e" given)))))
;; Symbol (_ -> Boolean) String X X *-> X (define (number->ord i)
(define (check-result pname pred? expected given . other-given) (if (= i 0)
(if (pred? given) "zeroth"
given (case (modulo i 10)
(tp-error pname "result of type <~a> expected, given: ~a" expected [(0 4 5 6 7 8 9) (format "~ath" i)]
(if (pair? other-given) [(1) (format "~ast" i)]
(car other-given) [(2) (format "~and" i)]
given)))) [(3) (format "~ard" i)])))
;; check-arg : sym bool str str TST -> void ;; Symbol (union true String) String X -> void
(define (check-arg pname condition expected arg-posn given) (define (check-list-list pname condition pred given)
(unless condition (when (string? condition)
(tp-error pname "expected <~a> as ~a argument, given: ~e" (tp-error pname (string-append condition (format "~nin ~e" given)))))
expected arg-posn given)))
;; Symbol (_ -> Boolean) String X X *-> X
;; check-arity : sym num (list-of TST) -> void (define (check-result pname pred? expected given . other-given)
(define (check-arity name arg# args) (if (pred? given)
(if (= (length args) arg#) given
(void) (tp-error pname "result of type <~a> expected, given: ~a" expected
(tp-error name "expects ~a arguments, given ~e" arg# (length args)))) (if (pair? other-given)
(car other-given)
;; check-proc : given))))
;; sym (... *->* ...) num (union sym str) (union sym str) -> void
(define (check-proc proc f exp-arity arg# arg-err) ;; check-arg : sym bool str str TST -> void
(unless (procedure? f) (define (check-arg pname condition expected arg-posn given)
(tp-error proc "procedure expected as ~s argument; given ~e" arg# f)) (unless condition
(unless (procedure-arity-includes? f exp-arity) (tp-error pname "expected <~a> as ~a argument, given: ~e"
(let ([arity-of-f (procedure-arity f)]) expected arg-posn given)))
(tp-error proc "procedure of ~a expected as ~a argument; given procedure of ~a "
arg-err arg# ;; check-arity : sym num (list-of TST) -> void
(cond (define (check-arity name arg# args)
[(number? arity-of-f) (if (= (length args) arg#)
(if (= arity-of-f 1) (void)
(format "1 argument") (tp-error name "expects ~a arguments, given ~e" arg# (length args))))
(format "~s arguments" arity-of-f))]
[(arity-at-least? arity-of-f) (format "at least ~s arguments" (arity-at-least-value arity-of-f))] ;; check-proc :
[else (format "multiple arities (~s)" arity-of-f)])))))) ;; sym (... *->* ...) num (union sym str) (union sym str) -> void
(define (check-proc proc f exp-arity arg# arg-err)
(unless (procedure? f)
(tp-error proc "procedure expected as ~s argument; given ~e" arg# f))
(unless (procedure-arity-includes? f exp-arity)
(let ([arity-of-f (procedure-arity f)])
(tp-error proc "procedure of ~a expected as ~a argument; given procedure of ~a "
arg-err arg#
(cond
[(number? arity-of-f)
(if (= arity-of-f 1)
(format "1 argument")
(format "~s arguments" arity-of-f))]
[(arity-at-least? arity-of-f) (format "at least ~s arguments" (arity-at-least-value arity-of-f))]
[else (format "multiple arities (~s)" arity-of-f)])))))