1

I'm trying to write a program compatible with both Chez and Chicken Scheme. Starting with something maximally simple:

(c1) R:\>type  hello.ss
(display "hello, world\n")

(c1) R:\>csc hello.ss

(c1) R:\>hello
hello, world

(c1) R:\>"C:\Program Files\Chez Scheme 9.5.8\bin\a6nt\scheme.exe" --program hello.ss
Exception: invalid top-level program import subform (display "hello, world\n") at line 1, char 1 of hello.ss

Chicken is happy with the simplest version, but Chez isn't. Okay, I know a way to make Chez happy:

(c1) R:\>type  hello2.ss
(import (rnrs))
(display "hello, world\n")

(c1) R:\>"C:\Program Files\Chez Scheme 9.5.8\bin\a6nt\scheme.exe" --program hello2.ss
hello, world

But does Chicken like that version?

(c1) R:\>csc hello2.ss

Syntax error (import): cannot import from undefined module

        rnrs

        Expansion history:

        <syntax>          (##core#begin (import (rnrs)))
        <syntax>          (import (rnrs))       <--

Error: shell command terminated with non-zero exit status 70: ""c:/chicken/bin/chicken.exe" "hello2.ss" -output-file "hello2.c""

Sadly, no.

How can I write a program that both implementations are happy with?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • Have you tried with `#!r6rs` as the first line before `import`? – Sylwester Jul 22 '22 at 15:27
  • @Sylwester That gives an error: Error: (line 1) invalid `#!' token: "r6rs" – rwallace Jul 22 '22 at 15:28
  • 2
    Chicken is R5RS, and there's a R7RS egg available to support that version of the standard. It doesn't claim to support R6RS. – Shawn Jul 22 '22 at 16:01
  • 1
    `(import (scheme))` works in both, but since Chez is R6RS and Chicken is R5RS/R7RS, it'll be up to you to stick to a compatible language subset. – Peter Winton Jul 22 '22 at 16:05
  • 2
    If [this is to be believed](https://docs.scheme.org/surveys/cond-expand/), Chez doesn't support SRFI-0 `cond-expand`, so you can't use it to conditionally import the appropriate modules depending on scheme implementation... I thought I was on to something, too. – Shawn Jul 22 '22 at 16:08
  • 2
    Better to write something compatible with one of the official revisions and require a scheme that supports it. It's the same with Python 2 vs 3 and Perl5 vs Raku. You simply cannot expect random code that works in one to also work in the other since there are not compatible. What Peter suggests is basically like making a polygot, code that happens to run in two different runtimes. – Sylwester Jul 22 '22 at 20:16
  • 1
    On linux there is https://github.com/scheme-live/live that implements a `json2scm` program that runs on several Scheme implementations. Also see https://github.com/amirouche/scheme-united – amirouche Jul 23 '22 at 11:15
  • 1
    @amirouche How does it work around the scheme version differences? Eg R5RS does not support `import`. Why json2scm. Sounds a little farfetched to write code in json – Sylwester Jul 23 '22 at 22:08
  • json2scm will translate json to scheme expression according to SRFI-180 mapping. CHICKEN 5 support imports. Look for chicken cond-expand at https://github.com/scheme-live/live/blob/hello-schemer/live/unstable.sld – amirouche Jul 24 '22 at 23:12
  • JSON is JavaScript Object Notation, SCM is referring to in this case to Scheme expressions. – amirouche Jul 24 '22 at 23:12

0 Answers0