chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] fixnum-specific math operators patch


From: felix winkelmann
Subject: Re: [Chicken-users] fixnum-specific math operators patch
Date: Fri, 1 Sep 2006 16:29:05 +0200

On 9/1/06, Will M Farr <address@hidden> wrote:

3. My opinion is that The Right Thing (TM) is to code the macro as
follows:

(define-syntax do-range
   (syntax-rules ()
     ((_ (i aa bb) expr ...)
      (let ((a aa)
           (b bb))
        (cond-expand
        ((not unsafe)
         (if (or (not (fixnum? a))
                 (not (fixnum? b)))
             (error 'do-range "non-fixnum limits" a b))))
        (do ((i a (fx+ i 1)))
           ((fx= i b))
         expr ...)))))

This works, but it's pretty yucky.  It is the simplest way, given the
present behavior of the fxXXX operators, to achieve safety in safe
mode (with useful, and immediate, error reporting) and speed in
unsafe mode.  I submitted the fixnum patch because it would implement
the same behavior, but without requiring people using the fxXXX
operators to think so much about these issues (and type (cond-
expand ...) every time).


Using something like "ensure" can help here:

% cat x.scm
(define x 33)
(print (ensure fixnum? x))
% csc x.scm -debug 2 && ./x
[canonicalized]
(set! c2 '"argument has incorrect type")
(##core#callunit "library")
(##core#callunit "eval")
(##core#callunit "extras")
(##core#undefined)
(##core#undefined)
(set! x '33)
(print (let ((g01 x))
        (if (fixnum? g01)
          g01
          (##sys#signal-hook type-error: c2 g01 'fixnum?))))
((##sys#implicit-exit-handler))
(##core#undefined)
33
% csc x.scm -debug 2 -unsafe && ./x
[canonicalized]
(set! c2 '"argument has incorrect type")
(##core#callunit "library")
(##core#callunit "eval")
(##core#callunit "extras")
(##core#undefined)
(##core#undefined)
(set! x '33)
(print (let ((g01 x))
        (if '#t g01 (##sys#signal-hook type-error: c2 g01 'fixnum?))))
((##sys#implicit-exit-handler))
(##core#undefined)
33

(Note that the "if" expression will be folded to "g01")


cheers,
felix

--
http://galinha.ucpel.tche.br:8081/blog/blog.ssp




reply via email to

[Prev in Thread] Current Thread [Next in Thread]