home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.scheme
- Path: sparky!uunet!wupost!darwin.sura.net!mlb.semi.harris.com!travis.csd.harris.com!grouper!grouper!brent
- From: brent@ssd.csd.harris.com (Brent Benson)
- Subject: Re: How do I implement trace/untrace
- Organization: Harris Computer Systems
- Date: Wed, 6 Jan 1993 14:32:17 GMT
- Message-ID: <BRENT.93Jan6093217@rcx1.ssd.csd.harris.com>
- In-Reply-To: mtoy@mycool.asd.sgi.com's message of 6 Jan 93 01:18:06 GMT
- References: <1idc0eINNak2@fido.asd.sgi.com>
- Sender: news@grouper.mkt.csd.harris.com (Network News)
- Lines: 53
-
- mtoy@mycool.asd.sgi.com (Michael Toy) writes:
-
- > I am new to scheme and I am trying various small projects to explore
- > what can and can not be done. One idea was to add a factility for
- > strict type checking, which would allow you to declare what the
- > legal types for a function were, and the type check would be
- > "inserted" into the procedure, similar to the way "trace" and
- > "untrace" work in some schemes.
-
- Here's a simple version of what you're looking for. (You need to use
- a macro if you don't like having to set! the result.) As an exercise,
- write a similar procedure that also checks the types of the arguments.
-
- ;;;
- ;;; Return a version of FUN that calls TYPE-PRED? on
- ;;; its return value and signals an error if the return
- ;;; type is incorrect.
- ;;;
- (define (type-check fun fun-name type-pred?)
- (lambda args
- (if (and (not (null? args))
- (eq? (car args) 'un-type-check)
- (null? (cdr args)))
- fun
- (let ((result (apply fun args)))
- (if (type-pred? result)
- result
- (error "incorrect return type:" fun-name))))))
-
- ;;;
- ;;; Return the original FUN.
- ;;;
- (define (un-type-check fun)
- (fun 'un-type-check))
-
- An example:
-
- > (define (number-car l) (car l))
- > (set! number-car (type-check number-car 'number-car number?))
- > (number-car '(1 2 3))
- 1
- > (number-car '(a b c))
-
- Error: incorrect return type:
- number-car
- 1>
- > (set! number-car (un-type-check number-car))
- > (number-car '(a b c))
- 'a
-
- --
- Brent Benson
- Harris Computer Systems
-