ashinn / chibi-scheme

Official chibi-scheme repository
Other
1.2k stars 142 forks source link

cond-expand does not isolate code at expansion time #949

Closed Marie-Joseph closed 5 months ago

Marie-Joseph commented 5 months ago

Hello,

I want to make a library initially written in Guile portable across R7RS-compliant Schemes. I've figured out a basic prototype for library/module declarations that seems to work with Guile and Gambit, but fails with Chibi because of a reader error when encountering a Guile-style module declaration inside a Guile-specific branch of a cond-expand. Here's the actual code in polyglot.scm:

(cond-expand
 (guile (define-module (polyglot)
          #:export (test)))
 (else #t)) ; necessary for at least Gambit

(define (test)
  (display "Worked")
  (newline))

and the associated polyglot.sld:

(define-library (polyglot)
  (import (scheme base)
          (scheme write))
  (include "polyglot.scm")
  (export test))

Here's a session attempting to import this in the Chibi REPL:

$ chibi-scheme
> (import (polyglot))
ERROR on line 3 of file ./polyglot.scm: invalid char following '#': #\:

And here's what happens when one uses -m:

chibi-scheme -m polyglot
ERROR on line 3 of file ./polyglot.scm: invalid char following '#': #\:
  called from <anonymous> on line 206 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/meta-7.scm
  called from <anonymous> on line 1210 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from <anonymous> on line 821 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from lp on line 280 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from <anonymous> on line 821 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from for1 on line 75 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from for1 on line 75 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from <anonymous> on line 821 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from call-with-current-continuation on line 848 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from <anonymous> on line 280 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/init-7.scm
  called from <anonymous> on line 245 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/meta-7.scm
  called from <anonymous> on line 432 of file /gnu/store/...-chibi-scheme-0.10/share/chibi/meta-7.scm

I've tested this against the version of Chibi currently in Guix (0.10) and with a custom package using code from commit 29dd1a3b81e297033f687cbbf2b51319856647f4; both have the same output.

Marie-Joseph commented 5 months ago

fwiw the workaround to this issue is to use R7RS-style library definitions even within the Guile branch. Guile accepts this just fine. However, this still feels like it shouldn't be necessary.

(and from the Guile side, Guile should accept .sld files by default but doesn't without the right flags, so you have to have a library/module definition at the top of the .scm file anyway. just so y'all know I'm not picking on Chibi ;) )

mnieper commented 5 months ago

cond-expand is evaluated during expansion time, which happens after reading. Any R7RS implementation is free to reject the code posted in the beginning. The correct way is to use include in the cond-expand branch. The file which include references won't be read if that cond-expand branch is not evaluated.

ashinn commented 5 months ago

Yes, this is working as expected. To be able to process the cond-expand form we need to be able to read it first, and the #: syntax is not valid R7RS. include allows you to conditionally load code with non-portable read syntax.

Alternatively, use a separate module file for Guile.

(define-library (polyglot)
  (import (scheme base)
          (scheme write))
  (include "polyglot-impl.scm")
  (export test))
(define-module (polyglot)
          #:export (test))
(include-from-path "polyglot-impl.scm")
(define (test)
  (display "Worked")
  (newline))
Marie-Joseph commented 5 months ago

Thanks for the guidance!