From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel Subject: Re: ELisp futures and continuations/coroutines Date: Fri, 20 May 2011 10:29:39 -0500 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <8739k9s84c.fsf@lifelogs.com> References: <87fwogaxzb.fsf@stupidchicken.com> <87mxilezg8.fsf@lifelogs.com> <87boz0eov8.fsf@lifelogs.com> <87mxikrulm.fsf@lifelogs.com> <871uzw5asv.fsf@lifelogs.com> <878vu2ztua.fsf_-_@lifelogs.com> <87oc2ywwuy.fsf@ambire.localdomain> <87r57uyaeu.fsf@lifelogs.com> <87hb8qwsmo.fsf@ambire.localdomain> <87oc2yuu8u.fsf@lifelogs.com> <87d3jew4eq.fsf@ambire.localdomain> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1305905412 2141 80.91.229.12 (20 May 2011 15:30:12 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 20 May 2011 15:30:12 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri May 20 17:30:08 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QNReP-0002v1-DH for ged-emacs-devel@m.gmane.org; Fri, 20 May 2011 17:30:05 +0200 Original-Received: from localhost ([::1]:48464 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNReO-0007JV-LW for ged-emacs-devel@m.gmane.org; Fri, 20 May 2011 11:30:04 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:46376) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNReL-0007IP-UC for emacs-devel@gnu.org; Fri, 20 May 2011 11:30:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QNReK-0006XX-Jy for emacs-devel@gnu.org; Fri, 20 May 2011 11:30:01 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:34766) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNReK-0006XR-9m for emacs-devel@gnu.org; Fri, 20 May 2011 11:30:00 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QNReI-0002qB-H8 for emacs-devel@gnu.org; Fri, 20 May 2011 17:29:58 +0200 Original-Received: from 38.98.147.130 ([38.98.147.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 May 2011 17:29:58 +0200 Original-Received: from tzz by 38.98.147.130 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 May 2011 17:29:58 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 88 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 38.98.147.130 X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:8lK268sQ+5/U3jquINLoO41rlC0= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:139552 Archived-At: On Fri, 20 May 2011 03:23:57 +0200 Thien-Thi Nguyen wrote: TN> () Ted Zlatanov TN> () Thu, 19 May 2011 18:48:49 -0500 TN> Why tie futures to fsm.el or deferred.el specifically? That seems like TN> an implementation detail. TN> Well, one important detail, whether you consider it implementation TN> or design, is who kicks the evaluation (and when). In the case of TN> fsm.el, this is done by either a timer or from process input. Yes, but deferred.el does it with a timer-triggered queue drain, storing the next deferred item in the current one, and the concurrency branch might do it with a separate thread. These implementation details obscure the purpose of the "future" type: to hold a potential value. TN> A "future type" w/o this detail well-defined is no better than TN> a simple closure (IMHO) because it has no concept of time / timing. ... TN> Minimalism is nice, but i suppose i like a more full-featured "future type", TN> one that can self-terminate, for example. Conniving computers, grumble... TN> (i'll have to dig up and post some old fsm toys -- get a dozen of them going TN> at once and you'll see what i mean :-D). Why should a future type encapsulate any time knowledge? The whole point is that you don't know when and if it will be available. You could derive a expected-future which will have some knowledge of when it might have the data. A self-managed-future could run its own functions. And so on. But I really want a clean, simple API that can be used by any library. Let's get this done, simple though it is, and then see how it will integrate with fsm.el and deferred.el. I really think it will benefit everyone to start simple. TN> Yes, [defstruct is] much cleaner than fsm's plist munging, but (as i TN> tried to express above), that is just the shape of the sail and not TN> the motion of the boat. Heh heh. I guess we're close to land then. TN> Keep going! The below adds a way to cancel a future. I think error handling is all that's left unsaid. Ted #+begin_src lisp (defstruct future callback errorback status value) (defun future-done-p (future) (future-status future)) (defun future-errored-p (future) (eq (future-status future) 'error)) (defun future-cancelled-p (future) (eq (future-status future) 'cancel)) (defun future-finish (future &optional status) (unless (future-done-p future) (setf (future-status future) (or status t)) (when (future-callback future) (funcall (future-callback future) future)))) (defun future-errored (future error) (unless (future-done-p future) (setf (future-status future) 'error) (when (future-errorback future) (funcall (future-errorback future) future error)))) (defun future-call (future) (unless (future-done-p future) (let ((ff (future-value future))) (when (functionp ff) ;; TODO: needs error handling (setf (future-value future) (funcall ff)))) (future-finish future))) (defun future-cancel (future) (unless (future-done-p future) (future-finish future 'cancel))) #+end_src