guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does declaration order matter in guile?


From: Taylan Kammer
Subject: Re: Does declaration order matter in guile?
Date: Sun, 12 Feb 2023 20:52:16 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.7.2

On 12.02.2023 19:46, wolf wrote:

> So, I have few questions I would like to ask:
> 
> 1. When does order matter? What is going on here?

Heya.

The order matters in this case because the SRFI-9 implementation in Guile 
defines
syntax (macros) rather than just variables bound to procedures.

If you use an undefined variable in a lambda body like '(define (blah) <HERE>)'
then it will be compiled into a variable lookup in the global environment, so 
you
can make it work by later defining that variable to something appropriate.

However, the "macro expansion" needs to happen immediately, so what happens 
here is:

1. (foo y) is compiled into code that will:
   - Look up the global variable 'foo'
   - Try to "apply" its value to the value of 'y'

2. You then bind 'foo' to a "syntax transformer" (macro) in the global 
environment.

3. When the code is executed, it tries to apply the syntax transformer as if it 
were
   a procedure or another type that can be "applied," which fails, because 
syntax
   transformers are not a type that can be "applied" like procedures and such.

(The error message would be clearer if it said "is not a procedure" instead of 
"wrong
type to apply" but there's other types in Guile that can be "applied" and not 
just
procedures, hence that slightly less clear error message.)

What *needs* to happen for it to work instead, is:

1. You bind 'foo' to the syntax transformer.

2. During the compilation of (foo y), the compiler calls the syntax transformer 
to
   affect the generation of code, so it will do the right thing.

> 2. Why does guile recommend SRFI-9 over the R6RS records? They seem less
>    verbose and more robust. At least to novice like me.

SRFI-9 is smaller and more widespread.  Ultimately, it's a matter of taste, 
since
both have advantages and disadvantages, and some things that are either an 
advantage
or a disadvantage depending on who you ask. :-)

> 3. What does guile implement by default? There are --r6rs and --r7rs 
> arguments,
>    what scheme is used when neither is supplied? R5RS? Sorry if this is stupid
>    question, the scheme landscape seems... complicated.

No worries, not a stupid question at all.  What Guile uses by default could be 
called
"Guile Scheme."  It neither completely fulfills the requirements of R7RS or 
R6RS, nor
is it limited to either.  I think that's what most Scheme implementations do... 
 The
standardization of Scheme is in a sad state.

> 4. Is the (install-r6rs!) global and affecting all reading from that point on
>    or is it scoped to the file being currently read? I ask because I am 
> curious
>    if I can mix files using R6RS and R7RS in one program.

I'm not 100% sure on this one, but I don't think it can affect the reading of 
the file
it appears in, because the entirety of reading happens before anything is 
executed.

It should affect *explicit* reading you do from that point on, i.e., what the 
'read'
procedure will do with its input.

> Thank you very much,
> W.
> 

You're welcome, and have fun with Guile!

-- 
Taylan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]