On Fri, 26 Sep 2025, "Thompson, David" <[email protected]> wrote:

[...]

> (define-syntax max
>   (lambda (stx)
>     (syntax-case stx ()
>       (id
>        (identifier? #'id)
>        #'%max)
>       ((_ x) #'x)
>       ((_ x y)
>        #'(let ((x* x)
>                (y* y))
>            (if (> x* y*) x* y*)))
>       ((_ x y ...)
>        #'(let ((x* x)
>                (m (max y ...)))
>            (if (> x* m) x* m))))))

Why not just:

       ((_ x y ...)
        #'(max x (max y ...)))

for the last case?

Also, I'm surprise that constant folding only seems to work up to 5
parameters here:

  scheme@(guile-user)> ,optimize (max 1 2 3 4 5)
  $13 = 5
  scheme@(guile-user)> ,optimize (max 1 2 3 4 5 6)
  $14 = (let ((m 6)) (if (> 1 m) 1 m))

There seems to be a hardcoded value in Guile preventing too much
recursion for this optimization pas.  We can actually call ,optimize on
the result to get down to a constant ...

  scheme@(guile-user)> ,optimize (let ((m 6)) (if (> 1 m) 1 m))
  $15 = 6

Fortunatelly, it seems that compiling actually yield a constant:

  scheme@(guile-user)> ,compile (max 1 2 3 4 5 6 7 8)
  Disassembly of <unnamed function> at #xe8:

     0    (instrument-entry 26)                                 at (unknown 
file):453:9
     2    (assert-nargs-ee/locals 1 0)    ;; 1 slot (0 args)
     3    (make-immediate 0 34)           ;; 8                  at (unknown 
file):453:28
     4    (handle-interrupts)             
     5    (return-values)                 

I guess the previous optimization is ran until a fix point is found.

[...]

Thanks,
Olivier

-- 
Olivier Dion
oldiob.ca

Reply via email to