Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

continuations

Name: Anonymous 2013-03-02 2:18

In this thread, you may post code that makes use of continuations, and I will attempt to produce code that does the same and looks reasonably similar, without using continuations.

Name: Anonymous 2013-03-02 14:43

>>31

I would translate that, but there are too many undefined symbols...


make-list-of-n-resources
make-io-port
start-io-event
...


I would fill in the gaps with my own implementations but I could be oversimplifying the problem. So instead, here's the start of a thread manager...


;; A thread is a stack of functions, waiting to be called with a return value.
;; Tail calls are used to pass control between threads. The native function call stack
;; remains one layer deep.

(define (thread-return thread return-value)
  (if (null? thread)
    (error "nothing to return to!")
    ((car thread) (cdr thread) return-value)))

(define (thread-call thread return-handler callee . callee-args)
  (apply callee (cons (cons return-handler thread) callee-args)))

(define (thread-tail-call thread callee . callee-args)
  (apply callee (cons thread callee-args)))

(define (fact thread n acc)
  (if (= n 0)
    (thread-return thread acc)
    (thread-tail-call thread fact (- n 1) (* n acc))))

(define (fact2 thread n)
  (if (= n 0)
    (thread-return thread 1)
    (thread-call thread
                 (lambda (thread ret-val)
                   (thread-return thread (* n ret-val)))
                 fact2 (- n 1))))

(define (print t v)
  (display v)
  (newline))

(fact (list print) 20 1)
(fact2 (list print) 20)

Name: Anonymous 2013-03-02 15:31

making fun of names people had no say in choosing, classy

Name: Anonymous 2013-03-02 16:10

nevermind that one.


(define (fact return n acc)
  (if (= n 0)
    (return acc)
    (fact return (- n 1) (* n acc))))

(define (fact2 return n)
  (if (= n 0)
    (return 1)
    (fact2 (lambda (ret-val)
             (return (* n ret-val)))
           (- n 1))))

(fact display 20 1)
(fact2 display 20)

Name: Anonymous 2013-03-02 16:52

import Control.Monad.Cont

dubz x = return (x ++ " 'em ")
em x = return (x ++ "dubz")

main = runCont (dubz "check" >>= em) putStrLn

Name: Anonymous 2013-03-02 19:04

>>42
It's okay if we do it to Nikita "Delicate Flower" Sadkov.

Name: Anonymous 2013-03-02 20:04

>>45
It's okay if we do it to Shlomo "Rothsberg" Goldstein.

Name: Anonymous 2013-03-02 21:24

>>46
Still nobody wants to sleep with you.

Name: Anonymous 2013-03-02 22:33


(define (producer return)
  (return 1 (lambda (return)
    (return 2 (lambda (return)
      (return 3 '()))))))

(define (consumer produsa)
  (if (not (null? produsa))
    (produsa (lambda (return-value produsa)
      (display return-value)
      (newline)
      (consumer produsa)))))

(consumer producer)


maybe something....

Name: Anonymous 2013-03-02 23:41

>>47
Oh! Oh! Eye-Do! Eye-Do! And >>47-Too!

Name: Anonymous 2013-03-03 3:28

Continuations are like violence, if they doesn't solve your problem, you're not using enough of them.

Name: Anonymous 2013-03-03 4:13

>>50
my program segfaults, how much violence should i use to solve it?

Name: Anonymous 2013-03-03 4:37

>>51
More.

Name: Anonymous 2013-03-03 6:54

>>1
Implement amb without continuations. I fucking dare you.

Name: Anonymous 2013-03-03 7:02

>>53
Can it be implemented with delimited continuations? I wonder.

Name: Anonymous 2013-03-03 7:04

>>49
Slut.

Name: Anonymous 2013-03-03 7:07

>>53,12
We're waiting.

Name: Anonymous 2013-03-03 10:42


?BREAK  IN 10
READY.
CONT
?CAN'T CONTINUE  ERROR
READY.
CUNT
?SYNTAX  ERROR
READY.
SYS64738

Name: Anonymous 2013-03-03 11:14

>>41
Sorry, it doesn't run on anything. I just wrote it to show "blocking" calls on a single threaded event loop without having to resort to CPS.

While you can do much without first class continuations, trying to emulate them with closures most often forces you to write code in CPS. If you prefer that, then fine.

Name: Anonymous 2013-03-03 15:47

>>56
All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.

Anything that can be done with continuations, can be done with tail recursion in programs that don't use the native stack.

Translating....


(define (fact-normal n)
  (if (= n 0)
    1
    (* n (fact-normal (- n 1)))))


To.....................


(define (fact2 return n)
  (if (= n 0)
    (return 1)
    (fact2 (lambda (ret-val)
             (return (* n ret-val)))
           (- n 1))))

Name: Anonymous 2013-03-03 16:02

>>59
All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.
Right. Such is how amb is implemented on deterministic machines.
Your code example is fibs, not amb. We're still waiting.

Name: Anonymous 2013-03-03 21:43

>>60
After further inspection of amb, it has come to my attention that the interface is dependant on mutations. Hence, my religious convictions prevent me from implementing it, as it's interface is inherently flawed and harmful. Thus rather that using amb, I will explicitly use lazy lists that call functions which emulate continuations. As shown in this example...................................................................................................................................................................

http://mitpress.mit.edu/sicp/full-text/sicp/book/node91.html

(if-fail (let ((x (an-element-of '(1 3 5 8))))
           (require (even? x))
           x)
         'all-odd)



(let ((success (lambda (x) x))
      (fail (lambda () 'all-odd)))
  (an-element-of '(1 3 5 8) (lambda (x fail)
                              (if (even? x)
                                (success x)
                                (fail)))
                            fail))

Name: Anonymous 2013-03-03 21:44

missing definition.......very sorry............................


(define (an-element-of lis success fail)
  (if (null? lis)
    (fail)
    (success (car lis) (lambda () (an-element-of (cdr lis) success fail)))))

Name: Anonymous 2013-03-03 22:27

I support amb because combining mutable state with continuations is awesome. Haskell's continuation monad is boring.

Name: Anonymous 2013-03-04 1:47

>>63
I support amb because combining mutable state with continuations is awesome.
How do you deal with race conditions and so? Not trolling, I really want to know.

Name: Anonymous 2013-03-04 1:49

>>59
All non deterministic programming can be expressed as deterministic operations on lazy lists that invoke continuations upon evaluation.
Does the converse hold? Can continuations be implemented in terms of non-deterministic operations?

Name: Anonymous 2013-03-04 3:56

>>65
I'm sure they can, since continuations are implemented on deterministic turning machines, but what do you have in mind?

Name: Anonymous 2013-03-04 4:18

>>66
Like almost everyone else on this board four years ago, I am trying to achieve computational enlightenment. I wish to see the true essence of computation, and I wonder whether it is purely functional, how powerful macros are, what kind of continuations there are, and so on. So far I am certain that evaluation must be strict, and that it must look and behave mostly like Lisp.

So more to the point, I am wondering whether continuations can be elegantly expressed in terms of some non-deterministic operations which would then generalize them.

Name: Anonymous 2013-03-04 5:16

>>67
Master brainfuck and unlambda.

Name: Anonymous 2013-03-04 5:19

Holy shit guy............................................... Are you a child?......................................................................................................................................................................................................................................

Name: Anonymous 2013-03-04 5:34

>>61
False. The only mutations in the linked example implementation are in amb-collect-thunk which is not touched by amb itself. Could you stop holding down your period key too? It makes you look like a fucking idiot (though was that the intention and HIBT?)
http://bugs.call-cc.org/browser/release/4/amb/trunk/amb.scm
It is quite easy to replicate the functionality of amb without continuations, but with a load of cruft as seen in your example. I want to see amb alone.

Name: Anonymous 2013-03-04 23:23

>>70
Thanks...................................I was thrown off a little by the (require (even x))...................................................don't ask me why..........................................................................Forgot continuations.................................are..like.....................................................................................don't need....to return from....where they are...................................................................I don't like them.

Ok............I will do it.................just..give me some time....please..........

Name: Anonymous 2013-03-05 2:05

I'm.....really..not looking forward....to this. It sounds hard....I have to translate....


XS
(require (even x))
YS


to..............................................something..


XS
(require (even x) fail (lambda ()
                         YS)


how do I do......it?????

Name: Anonymous 2013-03-05 4:50

>>63
you can use Cont in StateT

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List