unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Delimited continuations
@ 2017-12-09  9:06 John Wiegley
  2017-12-10 16:53 ` Michael Heerdegen
  2017-12-10 17:39 ` Real continuations (was: Delimited continuations) Michael Heerdegen
  0 siblings, 2 replies; 13+ messages in thread
From: John Wiegley @ 2017-12-09  9:06 UTC (permalink / raw)
  To: emacs-devel

It just occurred to me this evening that lexical-binding makes delimited
continuations (https://en.wikipedia.org/wiki/Delimited_continuation) trivial
to implement in Emacs Lisp:

  ;; -*- lexical-binding: t -*-
  
  (defun shift (k entry)
    (if (eq (nth 0 k) 'outer)
        (throw (nth 1 k)
               (funcall entry #'(lambda (val)
                                  (funcall (nth 2 k)
                                           (list 'inner val)))))
      (nth 1 k)))
  
  (defun reset (thunk)
    (let ((bound (make-symbol "reset--bound")))
      (catch bound
        (funcall thunk (list 'outer bound thunk)))))
  
  (reset
   #'(lambda (_)
       (+ 4 (reset
             #'(lambda (p)
                 (* 2 (shift p #'(lambda (k)
                                   (funcall k (funcall k 4))))))))))
  ;; (+ 4 (* 2 (* 2 4)))
  ;; => 20
  
  (reset
   #'(lambda (q)
       (+ 4 (reset
             #'(lambda (_)
                 (* 2 (shift q #'(lambda (k)
                                   (funcall k (funcall k 4))))))))))
  ;; (+ 4 (* 2 (+ 4 (* 2 4))))
  ;; => 28

This can handy when coding something that naturally lends itself to "inversion
of control", when it's easier to code the inner structure of something up
front, and then refer to that structure -- possibly repeating it and
transforming it -- to build up the resulting data:

  (let ((data '(1 2 3)))
    (reset
     #'(lambda (p)
         (list 'a 'b (shift p #'(lambda (k)
                                  (append (funcall k 0)
                                          (mapcar (apply-partially k)
                                                  data))))))))
  ;; => (a b 0
  ;;     (a b 1)
  ;;     (a b 2)
  ;;     (a b 3))

-- 
John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-01-03 20:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-09  9:06 Delimited continuations John Wiegley
2017-12-10 16:53 ` Michael Heerdegen
2017-12-10 19:59   ` John Wiegley
2017-12-12 14:27     ` Michael Heerdegen
2017-12-11 16:47   ` Stefan Monnier
2017-12-12 14:17     ` Michael Heerdegen
2017-12-12 22:24       ` John Wiegley
2018-01-02 18:39         ` Christopher Lemmer Webber
2018-01-02 22:29           ` John Wiegley
2018-01-03 16:01           ` Michael Heerdegen
2018-01-03 20:12             ` Stefan Monnier
2017-12-10 17:39 ` Real continuations (was: Delimited continuations) Michael Heerdegen
2017-12-11 16:16   ` Real continuations Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).