justinethier / cyclone

:cyclone: A brand-new compiler that allows practical application development using R7RS Scheme. We provide modern features and a stable system capable of generating fast native binaries.
http://justinethier.github.io/cyclone/
MIT License
823 stars 42 forks source link

Apply in icyc #537

Closed srgx closed 2 months ago

srgx commented 4 months ago

Function 'apply' is not working properly in icyc

(import (scheme base) (scheme write))

(define (exfun a b . rst)
  (list a b rst))

(display (apply exfun 1 2 '(3 4 5))) (newline)
; (1 3 (4 5))

; (display (apply cons '(5 (1 2 3)))) (newline)
; error

(display (apply cons '(5 '(1 2 3)))) (newline)
; (5 1 2 3)
yorickhardy commented 4 months ago

I don't know if this helps much, but for a bit more context, the second issue seems to be limited to primitives:

cyclone> (define (kons a b) (cons a b))
ok
cyclone> (apply kons '(5 (1 2 3)))
(5 1 2 3)
justinethier commented 4 months ago

Good find, thanks for the report @srgx!

justinethier commented 4 months ago

Added debug output and see the following.

Why is analyze called again after analyze-quoted? Need to investigate further...

cyclone> (display (apply cons '(5 (1 2 3)))) (newline)

/* (analyze (display (apply cons (quote (5 (1 2 3)))))) */
/* (analyze display) */
/* (analyze (apply cons (quote (5 (1 2 3))))) */
/* (analyze apply) */
/* (analyze cons) */
/* (analyze (quote (5 (1 2 3)))) */
/* (analyze-quoted (quote (5 (1 2 3)))) */
/* (analyze (<primitive cons> 5 (1 2 3))) */
/* (analyze <primitive cons>) */
/* (analyze 5) */
/* (analyze (1 2 3)) */
/* (analyze 1) */
/* (analyze 2) */
/* (analyze 3) */Error: Unknown procedure type -- EXECUTE-APPLICATION: 1
cyclone>
/* (analyze (newline)) */
/* (analyze newline) */

and alternatively:

cyclone> (define (kons a b) (cons a b))
cyclone> (apply kons '(5 (1 2)))

/* (analyze (apply kons (quote (5 (1 2))))) */
/* (analyze apply) */
/* (analyze kons) */
/* (analyze (quote (5 (1 2)))) */
/* (analyze-quoted (quote (5 (1 2)))) */
/* (analyze ((procedure (a b) ...) 5 (quote (1 2)))) */
/* (analyze (procedure (a b) ...)) */
/* (analyze 5) */
/* (analyze (quote (1 2))) */
/* (analyze-quoted (quote (1 2))) */
justinethier commented 3 months ago

Possible fix above, need to vet more and possibly clean up associated code. Also need to add some unit tests for this case before wrapping everything up.

justinethier commented 2 months ago

We are good to go on this, closing.