* "define is only allowed in a <body>"
@ 2022-01-27 22:58 Panicz Maciej Godek
2022-01-27 23:57 ` Per Bothner
0 siblings, 1 reply; 2+ messages in thread
From: Panicz Maciej Godek @ 2022-01-27 22:58 UTC (permalink / raw)
To: kawa
Hi,
I observed that when I write macros that expand to 'define' form, I
sometimes get this error:
define is only allowed in a <body>
The error seems very fragile, and I didn't manage to find a minimal setup
to recreate it.
But I have a fairly elaborate set of modules where the error appears.
So in particular, in this folder and in this commit:
https://github.com/panicz/grasp-android/tree/d7146355a3862cf3e2aef168bc10f741915b8828/stages/retreat/GRASP/src
I have two entry points to my module.
The first one is a small test suite, namely -- the file "test-screen.scm"
When I run it,
kawa -f test-screen.scm
I get a few warnings:
primitive.scm:454:18: warning - void-valued expression where value is needed
primitive.scm:473:18: warning - void-valued expression where value is needed
primitive.scm:525:3: warning - unreachable code
primitive.scm:550:3: warning - unreachable code
test-screen.scm:110:5: warning - type Screen is incompatible with required
type Screen
test-screen.scm:139:21: warning - type Finger is incompatible with required
type Tile
But the tests run allright.
On the other hand, when I try to run the file editor.scm
CLASSPATH=./lanterna-3.1.1.jar kawa -f editor.scm
then -- in addition to those warnings -- I get an error in the module
primitive.scm:
primitive.scm:305:1: define is only allowed in a <body>
primitive.scm:454:18: warning - void-valued expression where value is needed
primitive.scm:473:18: warning - void-valued expression where value is needed
primitive.scm:525:3: warning - unreachable code
primitive.scm:550:3: warning - unreachable code
primitive.scm:310:5: warning - no declaration seen for heads
Now, the expression beginning in the line 305 of primitive.scm looks like
this:
(define-cache (heads tail)
(cache (head)
(cons head tail)))
where the form "define-cache" is defined in the "define-cache.scm" file as
(define-syntax-rule (define-cache (property-name object) default)
(define property-name
(cache (object) default)))
and define-syntax-rule is defined in the define-syntax-rule.scm file as
(define-syntax define-syntax-rule
(syntax-rules ()
((define-syntax-rule (name . args) substitution)
(define-syntax name
(syntax-rules ()
((name . args)
substitution))))
((define-syntax-rule (name . args) . substitution)
(define-syntax name
(syntax-rules ()
((name . args)
(begin . substitution)))))
))
Now, if I substitute the usage of "define-cache" with its expansion, i.e.
(define heads
(cache (tail)
(cache (head)
(cons head tail))))
the problem disappears.
Moreover, I have noticed one thing. The test-screen.scm imports a number of
modules:
(import
(cell-display-properties)
(define-interface)
(define-type)
(conversions)
(primitive)
(text-screen)
(combinators)
(parse)
(examples)
(assert)
(cursor)
(infix))
The editor.scm file imports almost the same modules and in the same order
(plus two other low-dependency modules), but using one import expression
per module, rather than one import expression containing all modules:
(import (cell-display-properties))
(import (define-interface))
(import (define-type))
(import (conversions))
(import (primitive))
(import (text-screen))
(import (combinators))
(import (parse))
(import (examples))
(import (assert))
(import (cursor))
(import (infix))
(import (match))
(import (term))
If I replace this series of single-module imports with a single import of
all those modules, then the problem disappears as well.
I have experienced the same error ("define is only allowed in a <body>")
when I had some circular dependencies between modules.
Since then, I did my best to remove any circular dependencies. I'm not 100%
sure they are all gone, but I didn't manage to find any.
How could this error (and its solutions) be explained?
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: "define is only allowed in a <body>"
2022-01-27 22:58 "define is only allowed in a <body>" Panicz Maciej Godek
@ 2022-01-27 23:57 ` Per Bothner
0 siblings, 0 replies; 2+ messages in thread
From: Per Bothner @ 2022-01-27 23:57 UTC (permalink / raw)
To: Panicz Maciej Godek, kawa
I don't know the actual problem, but the following explains the error message.
Syntax expansion (translating of S-expressions to Expression objects)
is done in two interleaved phases:
(1) The "scan" phase is primarily about collecting the set of names
bound in a <body>. So given something like:
(define name expr)
it will make a note of 'name' but it will leave 'expr' for the next phase.
(2) The rewrite phase expands a single expression in the context of existing
definitions.
Macro expansion may happen in either phase, as needed.
For example 'define' is actually a macro that expands into the more primitive '%define'.
Forms at the the top-level of a <body> have to be expanded at scan time -
thus 'define' gets expanded at scan time to '%define' which then gets further processed.
The 'let' form does not define new non-local identifiers, so 'let' is processed at
rewrite time - and does not need any processing at scan time.
'define' (or rather '%define') needs processing at both scan and rewrite times:
scan makes a note of the defined name, and converts it to a simpler form
that is later processed by rewrite.
It is an error for 'define' to appear in expression context, such as the following:
(list (define x 10) x)
That is what the error message means. Kawa will complain if rewrite is asked to
process a 'define' (or '%define') form that has not been previously processed by scan.
Basically, one of your macros expands to a 'define' in expression context.
--
--Per Bothner
per@bothner.com http://per.bothner.com/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-01-27 23:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27 22:58 "define is only allowed in a <body>" Panicz Maciej Godek
2022-01-27 23:57 ` Per Bothner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).