define in cond-expand

36 views
Skip to first unread message

ceving

unread,
Jan 8, 2023, 2:23:22 PM1/8/23
to chibi-scheme
I have a problem with the following code:

(cond-expand
 (gambit
  (define a 1)
  (define b 2))
 (else))

(cond-expand
 (chibi
  (define a 1)
  (define b 2))
 (else))

(cond-expand
 (guile
  (define a 1)
  (define b 2))
 (else))

(display (cons a b))
(newline)

It works in Guile and Gambit, but not in Chibi:

$ guile cond-expand-define.scm
(1 . 2)
$ gsi cond-expand-define.scm
(1 . 2)
$ chibi-scheme cond-expand-define.scm
ERROR on line 10 of file cond-expand-define.scm: undefined variable: a
  called from <anonymous> on line 1268 of file /usr/local/share/chibi/init-7.scm
  called from <anonymous> on line 800 of file /usr/local/share/chibi/init-7.scm
Searching for modules exporting a ...
... none found.
$ chibi-scheme -V
chibi-scheme 0.10.0 "neon" (chibi r7rs ratios complex uvector threads full-unicode modules dynamic-loading linux little-endian)

Who is right?

Alex Shinn

unread,
Jan 9, 2023, 7:03:02 PM1/9/23
to chibi-...@googlegroups.com
You don't have any imports, so neither cond-expand nor define are even defined.

Start the file with `(import (scheme base) (scheme write))` and it works fine.

--
Alex

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chibi-scheme/b6a4b258-fcca-4570-b52f-c59b039e67e3n%40googlegroups.com.

ceving

unread,
Jan 11, 2023, 3:59:55 AM1/11/23
to chibi-scheme
Alex Shinn schrieb am Dienstag, 10. Januar 2023 um 01:03:02 UTC+1:
You don't have any imports, so neither cond-expand nor define are even defined.

Start the file with `(import (scheme base) (scheme write))` and it works fine.

This does not work, because Schemes had modules long before R7RS specified "import".
 
(cond-expand (r7rs ...))

Makes only sense if I can use "cond-expand" before I use anything from R7RS. And
this implies "import", because "import" is a R7RS-thing. And I can not use R7RS-things,
before I have checked if I can use them.

Alex Shinn

unread,
Jan 11, 2023, 5:13:54 AM1/11/23
to chibi-...@googlegroups.com
You're asking how to make a Scheme program run portably on R7RS and non-R7RS systems.
This is tricky in general.  R7RS section 5.1 clearly states that a program must start with one or
more import declaration.  There is a cond-expand form in library declarations, and a cond-expand
syntax exported by (scheme base), but that must be imported first.

Chibi actually does (non-portably) allow a top-level cond-expand, but you need to import `define`
first and separately before using it due to interactions between define and the expander:

(cond-expand
 (chibi
  (import (scheme base) (scheme write)))

 (else))

(cond-expand
 (chibi
  (define a 1)
  (define b 2))
 (else))

Note this is for running programs.  You could also just pipe the program into the repl
with `chibi-scheme < cond-expand-define.scm` and it would work because the repl
has a default environment.

--
Alex

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages