Re: define-datatype and scope
- From: Jens Axel Søgaard <usenet@xxxxxxxxxxxx>
- Date: Thu, 12 Apr 2007 19:33:17 +0200
Marlene Miller skrev:
Is it an error because they decided we shouldn't refer to a datatype before
it is declared, or is it an error because the datatype name is not a
top-level variable (which can be referred to before it is defined)?
Because they (Flatt?) decided it should be that way.
The error is good, because I didn't realize the dependencies of the two
macros in separate files.
Agree.
But all macros have to be defined at the top. I
haven't figured out how to package things in Scheme.
I am not sure what you are thinking of here.
Are you talking about your own macros or the macros
definining define-datatype ?
If the latter, you can use define-datatype in a non-EOPL
langauge like this:
(require (lib "datatype.ss" "eopl"))
> ; - - - - - - - - - -
PLT Scheme, Language Standard (R5RS)
(define-syntax define-datatype
(syntax-rules ()
target
;((direct-target indirect-target) (direct-target v) (indirect-target v))
It looks like the identifier target is a top-level variable.
Yes, that's the way the book authors defined it. [They needed R5RS
compability, so they stricter requirements than Flatt].
;- - - - - - - - - -
PLT Scheme, Language: EOPL
(define-datatype target target?
(direct-target (v number?))
(indirect-target (v target?)))
target
;target: illegal use of syntax in: target
The identifier target is not a top-level variable? It is something else?
Yes.
(define-syntax define-datatype
(lambda (stx)
(syntax-case stx ()
[(_ name pred-name
(variant-name (field-name field-pred) ...)
...)
<a lot deleted>
(define-syntax name
(let ([cert (syntax-local-certifier)])
(make-dt (cert (syntax pred-name))
(list
(make-vt (cert (syntax variant-name))
(cert (syntax variant?))
(cert (syntax variant-accessor))
variant-field-count)
...))))
<more deleted>
This means that name (i.e. target in the example) is bound in the
transfomer environment (that is, as syntax). It isn't bound to
a macro transformer, but to a value, a dt-structure, that represents
the datatype to be defined.
This value is used by the cases macro. As an example, it starts
with
(let ([dt (and (identifier? (syntax datatype))
(syntax-local-value (syntax datatype)
(lambda () #f)))])
(unless (dt? dt)
(raise-syntax-error
'cases
"not a datatype name"
stx
(syntax datatype)))
which checks that the datatype has been defined previously.
Later we see check the check that a variant mentioned in
the use of cases actually is a variant of the datatype.
(let* ([variant (syntax variant)]
[vt
(ormap (lambda (dtv)
(let ([vt-name (vt-name-stx dtv)])
(and (module-identifier=? variant vt-name)
dtv)))
(dt-variants dt))]
[orig-variant (and vt (vt-name-stx vt))])
(unless orig-variant
(raise-syntax-error #f
(format "not a variant of `~a'"
(syntax-object->datum (syntax datatype)))
stx variant))
The source is at:
<http://ja.soegaard.net/planet/html/collects/eopl/datatype.ss>
--
Jens Axel Søgaard
.
- References:
- define-datatype and scope
- From: Marlene Miller
- Re: define-datatype and scope
- From: Jens Axel Søgaard
- Re: define-datatype and scope
- From: Marlene Miller
- define-datatype and scope
- Prev by Date: Re: Scheme & Data Structures
- Next by Date: Re: Scheme & Data Structures
- Previous by thread: Re: define-datatype and scope
- Next by thread: Withdrawn SRFI 92: ALAMBDA and ALAMBDA*
- Index(es):
Relevant Pages
|