emacs-devel
[Top][All Lists]
Advanced

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

Re: continuation passing in Emacs vs. JUST-THIS-ONE


From: Stefan Monnier
Subject: Re: continuation passing in Emacs vs. JUST-THIS-ONE
Date: Thu, 16 Mar 2023 23:08:25 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

>> ;; (futur-let*
>> ;;   (exitcode <- (futur-process-make :command cmd :buffer t))
>> ;;   (out (buffer-string)) ;; Get the process's output.
>> ;;   (cmd2 (build-second-arg-list exitcode out))
>> ;;   (otherexit <- (futur-process-make :command cmd2 :buffer t)))
>> ;;  (futur-pure (buffer-string)))
>
> Seems like beautiful lisp code has no futur. :-)

BTW the above code can't work right now.  Part of the issue is the
management of `current-buffer`: should the composition of futures with
`futur-let*` save&restore `current-buffer` to mimic more closely the
behavior one would get with plain old sequential execution?  If so,
should we do the same with `point`?  What about other such state?

> There is something very ugly about this code.
> It looks like assembly, 1 dimensional vertical code.
> It is hard to see the structure of the code and what it actually does.
> I do not think it is practical to write non-trivial code in this style.

:-)

> Nice lisp code is usually 2 dimensional,
> with indentation and top-left to bottom-right direction.
> It is usually much clearer to see what is an argument to what
> based on the position in the syntax tree.
>
> Is it possible to make the syntax more structured (lispy)?
> Meaning tree-like, not list-like?
> Something in the spirit of:
>
> (futur-progn
>  (futur-process-make
>   :command (futur-let ((exitcode (futur-process-make
>                                   :command (build-arg-list)
>                                   :buffer t)))
>              (build-second-arg-list exitcode (buffer-string)))
>   :buffer t)
>  (buffer-string))

The `futur-progn` is just:

    (defmacro futur-progn (form &rest forms)
      (if (null forms) form
        `(futur-let* ((_ ,form)) (futur-progn ,@forms))))

As for passing the result of `futur-let` to `:command` it just requires
writing `futur-process-make` in a way that is tolerant of this
`:command` arg being a future rather than a string, which should be
fairly easy (it's basically always easy when done within a function
which itself returns a future).

> or would it need some fancy syntax rewriting like other async/cps
> syntax rewriting libraries?

I don't think so, no.  But you would need fancy rewriting if you wanted
to allow

    (concat foo (futur-let* (...) ...))

But as you point out at the beginning, as a general rule, if you want to
avoid rewritings in the style of `generator.el`, then the code will tend
to feel less like a tree and more "linear/imperative/sequential",
because you fundamentally have to compose your operations "manually"
with a monadic "bind" operation that forces you to *name* the
intermediate value.

> Second question: I see that futur-wait blocks the whole Emacs due to
> the while loop.  How can one use futur without blocking Emacs?

Don't use `futur-wait` and instead use `futur-let*`.
IOW: instead of waiting, return immediately a future.

> Last question: How would similar functionality be implemented
> using futur?

Good question.
To a large extent I guess it could be implemented in basically the same
way: you'd use futures only for the timer part of the code, and leave
the process's output to fill the buffer just like you do.

I think the difference would be very small and cosmetic like replacing

    (defun stream-pull-in-background (stream &optional secs repeat)
      (let (timer)
        (setq timer (run-with-timer
                     (or secs 1)
                     (or repeat 1)
                     (lambda ()
                       ;;(message "@@@ polling!")
                       (unless (funcall stream)
                         (cancel-timer timer)))))))

with something like:

    (defun stream-pull-in-background (stream &optional secs repeat)
      (futur-run-with-timer
       (or secs 1)
       (lambda ()
         ;;(message "@@@ polling!")
         (when (and (funcall stream) repeat)
           (stream-pull-in-background stream secs repeat)))))

The only benefit I could see is that it returns a future, i.e. a kind of
standardized representation of that async computation so the caller can
use things like `futur-wait` or `futur-let*` without having to care
about whether the function is using timers or something else.
And, there's also the benefit of standardized error-signaling.


        Stefan




reply via email to

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