egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
69 stars 17 forks source link

R7RS define-record-type does not work in a library #555

Closed lassik closed 1 year ago

lassik commented 1 year ago
(define-library (testlib)
  (import (scheme base))
  (begin

    (define-record-type <test-record>
      (make-test-record name)
      test-record?
      (name test-record-name))))
$ stklos testlib.sld
**** Error while executing command ("testlib.sld")
     Where: in %execute
    Reason: symbol `<test-record>' unbound in library (testlib)
jpellegrini commented 1 year ago

Hi @lassik You need SRFI-9 for that...

But then there's the real problem:

(define-library testlib
(import (scheme base) (srfi 9))
  (begin

    (define-record-type <test-record>
      (make-test-record name)
      test-record?
      (name test-record-name))))
**** Error:
%execute: symbol `make-struct-type' unbound in library (testlib)

Ah, that is a problem indeed!

The primitive make-struct-type exists, but it's not being exported to a library that imports SRFI-9:

stklos> (define-library y (import (srfi 9)))
;; y
stklos> (in-module y make-struct-type)
**** Error:
symbol-value*: symbol `make-struct-type' unbound in library (y)
jpellegrini commented 1 year ago

@egallesio -- SRFI 9 naturally inherits make-struct-type and everything else from the STklos module, since it is itself a module, but it exports define-record-type, which is syntax, and which depends on a primitive (which SRFI-9 should not export).

When a library imports (srfi 9), it brings the macro, but not the symbols referenced by the macro (this is one of the pending issues for hygienic macros... They should remember the environment when they were defined).

What would a feasible workaround for now?

jpellegrini commented 1 year ago

I mean... @lassik - this would work, but it brings all Scheme symbols into the library:

(define-library testlib
(import SCHEME
        (srfi 9))
  (begin

    (define-record-type <test-record>
      (make-test-record name)
      test-record?
      (name test-record-name))))
lassik commented 1 year ago

Thanks for looking into it!

You need SRFI-9 for that...

The R7RS standard does not provide a (srfi 9) library. It should be possible to import (scheme base) and get a working define-record-type. (IIRC the R7RS semantics are the same as in SRFI 9, but I'm not 100% sure about that.)

it brings the macro, but not the symbols referenced by the macro (this is one of the pending issues for hygienic macros... They should remember the environment when they were defined).

Indeed.

jpellegrini commented 1 year ago

The R7RS standard does not provide a (srfi 9) library

Oh, sorry! I forgot that define-record-type was in R7RS! :)

jpellegrini commented 1 year ago

So one would just need to add define-recrord-type and its dependencies to (scheme base) to fix this... Seems easy, actually.

lassik commented 1 year ago

Yes, but neither (scheme base) nor (srfi 9) should export internal procedures that are called by the define-record-type macro.

egallesio commented 1 year ago

I'm fixing this issue. Thanks for signaling it.

egallesio commented 1 year ago

The last commits correct this issue.