Re: Web development without connections to external repl (in geiser)

2022-10-16 Thread Dr. Arne Babenhauserheide

Dmitry Polyakov  writes:

>> Using fibers, I thought I could run the web server in seperate thread
>> that dont block current one where, for example, I could rebind the
>> handler (via ice-9 atomics or something). But it's not, after eval
>> (run-server handler), repl get stuck. May be I misunderstood something?
>> This is code:
>
> May this is because of main thread is blocked by REPL?

Have a look at the cooperative REPL in chickadee:
https://www.gnu.org/software/guile/manual/html_node/Cooperative-REPL-Servers.html
https://dthompson.us/manuals/chickadee/Live-Coding.html

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de


signature.asc
Description: PGP signature


Re: fibers doc: fix typo

2022-10-16 Thread Maxime Devos

> [...]

Applied the two patches.  Now in master.

Greetings,
Maxime.


OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: defining macros within eval

2022-10-16 Thread Jean Abou Samra

Le 16/10/2022 à 11:39, Paul Jarc a écrit :

Hi.  I'm updating some old code to work with newer versions of Guile.
This example used to work with 1.8, but gives an error with 2.2 and
later:

(begin
   (eval '(define-syntax-rule (rule x) x) (current-module))
   (display (rule "ok\n")))

ERROR: Wrong type to apply: #

The error happens for define-syntax-rule and define-macro, but not
plain define.




In Guile 2 and 3, the main way to run code is to byte-compile it.
This is what happens by default (Guile will print a note the first
time: "auto-compilation is enabled, ..."). In this mode, Guile will
first compile the .scm file into a .go bytecode file. This requires
doing all the macro expansion. Since the code run by eval is not
necessarily known at compile-time, it can't define macros used by
the compiled code.

What happens here is that the eval call just adds a syntax transformer
in the current module, and it would be used if it had been available
at the time of compilation, but it is too late, and it is just looked
up in the module and applied as a normal function, which fails.




It happens when eval is within begin or let, but not at
the top level.



Kind of. Something like

(eval '(define-syntax-rule (rule x) x) (current-module))
(display (rule "ok\n"))

will work in the REPL but not in a script, because in the REPL the
expansion is done step-by-step (since the result for an S-expr is
printed as soon as you enter it), where as in a file, it is done
in batch.



Is there some way to make this work?  In my real code,
the expression is read from a file, where it might be a macro
definition or anything else, and it's evaluated in a different module
from the current one.



You cannot byte-compile code in advance if it uses macros that
are only known dynamically. What you can do is using the evaluator
to run your code instead of the compiler. For example, if you set
GUILE_AUTO_COMPILE=0 and clear the bytecode cache (for me it's under
~/.cache/guile/ccache), running a script will no longer used compiled
bytecode but go through the evaluator, and in this case it works.

Be aware, though, that debugging evaluated code is a bit of a hell
because you won't get source locations for error messages, and
backtraces won't be in terms of the source code being run but in
terms of the source code of Guile's evaluator, the file
module/ice-9/eval.scm in the Guile source code.

Regards,
Jean




Re: defining macros within eval

2022-10-16 Thread Maxime Devos

On 16-10-2022 11:39, Paul Jarc wrote:

Hi.  I'm updating some old code to work with newer versions of Guile.
This example used to work with 1.8, but gives an error with 2.2 and
later:

(begin
   (eval '(define-syntax-rule (rule x) x) (current-module))
   (display (rule "ok\n")))

ERROR: Wrong type to apply: #

The error happens for define-syntax-rule and define-macro, but not
plain define.  It happens when eval is within begin or let, but not at
the top level.  Is there some way to make this work?  In my real code,
the expression is read from a file, where it might be a macro
definition or anything else, and it's evaluated in a different module
from the current one.


Surround (eval ...) by (eval-when (expand) ...).  Section 
'(guile)Eval-when' explains the 'why'.  Depending on where you are using 
'rule' and what the real 'rule' is, you might need the other 'load' and 
'eval' as well.


IIUC, the previous 'lazy macros' (?) system of 1.8 (which didn't need 
the eval-when thing (?)) was rather impractical to do optimisation with, 
hence the more conventional 'eval-when' as found in other Schemes.


Greetings,
Maxime.


OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: defining macros within eval

2022-10-16 Thread Maxime Devos



On 16-10-2022 11:39, Paul Jarc wrote:

Hi.  I'm updating some old code to work with newer versions of Guile.
This example used to work with 1.8, but gives an error with 2.2 and
later:

(begin
   (eval '(define-syntax-rule (rule x) x) (current-module))
   (display (rule "ok\n")))

ERROR: Wrong type to apply: # [...]


See my previous reply, and also are you sure that 'eval' is appropriate 
here?  Would datum->syntax + read tricks work instead?


For an example in the wild, see e.g. 
.


(the (include-from-path "gnu/.../protocols.scmgen" is not relevant here, 
you could inline protocols.scmgen in that example -- I just found 
separating it in a separate file a nicer structure).


Greetings,
Maxime.


OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: defining macros within eval

2022-10-16 Thread Maxime Devos



On 16-10-2022 16:07, Jean Abou Samra wrote:



Is there some way to make this work?  In my real code,
the expression is read from a file, where it might be a macro
definition or anything else, and it's evaluated in a different module
from the current one.



You cannot byte-compile code in advance if it uses macros that
are only known dynamically. [...]


Possibly in Paul Jarc's case, while they the macros might be computed, 
they might also be the same between runs of "guile -l do-something.scm".


If that's the case (i.e., the expression read from the file remains 
unchanged), byte compiling is possible, just use eval-when, see my reply.


Greetings,
Maxime.


OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature