[lisp-interface-library-devel] Curating libraries

Drew Crampsie me at drewc.ca
Mon Oct 29 21:32:32 UTC 2012


cl-cont .. heh ...

This is the call/cc that will/should be included in :interface/monad :

(define-interface <monad> (<type>) ()
  (:singleton)
  (:generic result (<monad> value))
  (:generic bind (<monad>  monadic-value monadic-function)))

(defmacro mlet* (monad bindings &body body)
  (destructuring-bind (monad &optional (functions-spec '<monad>))
      (or (when (listp monad) monad) (list monad))
    `(macrolet ((%mlet* (((symbol monadic-value) &rest rest) &body body)
  `(bind ,monadic-value
 (lambda (,symbol)
   ,@(when (string-equal (symbol-name symbol) "_")
   `((declare (ignorable ,symbol))))
   ,@(if rest
 `((%mlet* ,rest
   , at body))
 body)))))
       (interface:with-interface (,monad ,functions-spec)
 ,@(if bindings
      `((%mlet* ,bindings , at body))
      body)))))

(interface:define-interface <continuation> (<monad>)
  ()
  (:singleton)
  (:generic call/cc (<continuation> function)))

(defmethod result ((m <continuation>) value)
  (lambda (k) (funcall k value)))

;m >>= f  = Cont (\k -> runCont m (\a -> runCont (f a) k))

(defmethod bind ((m <continuation>) mv mf)
  (lambda (k)
    (funcall mv
     (lambda (a)
       (funcall (funcall mf a)
k)))))

; callCC f = Cont $ \k -> runCont (f (\a -> Cont $ \_ -> k a)) k

(defmethod call/cc ((m <continuation>) fn)
  (lambda (k)
    (funcall (funcall fn (lambda (a)
   (lambda (_)
     (declare (ignore _))
     (funcall k a))))
     k)))


;;; Then:

ORG/IPS/MONAD/CONTINUATION> (let (cc)
      (mlet* (<continuation>)
  ((a (result 1))
   (a.5 (lambda (k) (setf cc k) (funcall k 5)))
   (b (result 2))
   (c (result (+ a a.5 b))))
(result (list c cc))))

=>#<CLOSURE (LAMBDA (K) :IN BIND) {CBBCE15}>

ORG/IPS/MONAD/CONTINUATION> (funcall *  #'identity)
=> (8 #<CLOSURE (LAMBDA # :IN BIND) {B88785D}>)

ORG/IPS/MONAD/CONTINUATION> (funcall (second *) 6)
=> (9 #<CLOSURE (LAMBDA # :IN BIND) {B88785D}>)

ORG/IPS/MONAD/CONTINUATION> (funcall (second *) 10)
=> (13 #<CLOSURE (LAMBDA # :IN BIND) {B88785D}>)

So, that is call/cc based on the cont monad, in IPS. w00t.

any questions/comments/etc are most welcome.

Cheers,
drewc

On Mon, Oct 29, 2012 at 1:50 PM, Dan Lentz <danlentz at gmail.com> wrote:

> Fare
>
> I've got ctries, redblack trees, and weight-balanced trees I've been
> thinking about trying to integrate with/into LIL.  I would be proud if
> any/all of it were to be rolled into the LIL project proper.
>
> Regarding non-concurrent structures with concurrent interface --  I'm
> probably not the most qualified to answer but if you we're to ask me I
> think that new futures library by deliciousrobot looks interesting.
> It implements  cooperative multitasking based on green threads built
> using cl-cont.
>
> My access to email is sporadic over the next day or so.  Everyone here
> in South Jersey is hunkered down in anticipation of the hurricane.
>
> best regards,
> Dan Lentz
>
> _______________________________________________
> lisp-interface-library-devel mailing list
> lisp-interface-library-devel at common-lisp.net
>
> http://lists.common-lisp.net/cgi-bin/mailman/listinfo/lisp-interface-library-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/lisp-interface-library-devel/attachments/20121029/1e72a149/attachment.html>


More information about the lisp-interface-library-devel mailing list