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

Pages: 1-4041-

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 3:47

(call/cc call/cc)

Name: Anonymous 2013-03-02 4:04

>>2
I need a more complete usage in order to bring the generality down to higher order functions alone...


(lambda (x) x)

Name: Anonymous 2013-03-02 4:17

ehhhhhh maybe this...

(lambda (x) (x x))

Name: THEOREM OF THE DAY 2013-03-02 4:25

Theorem. Let R be a ring and let a ∈
 Z(R). Then then the evaluation map φa : R[x] → R, φa(f(x)) = f(a) is a surjective ring homomorphism.

Name: Anonymous 2013-03-02 4:30

>>5
define "infinite set"

Name: Anonymous 2013-03-02 4:51

>>6
A countably infinite set is a set that can be put in one-to-one correspondence with the natural numbers.

Name: Anonymous 2013-03-02 5:25

>>7
define "the natural numbers"

Name: Anonymous 2013-03-02 5:39

Name: Anonymous 2013-03-02 5:48

>>8

Shalom NIKITA!

Name: Anonymous 2013-03-02 6:02

>>1
I'll give you a better task: write fibs using delimited continuations.

Name: Anonymous 2013-03-02 6:14

amb
You have 2 hours

Name: Anonymous 2013-03-02 6:16

>>9
how do you know, that "for every natural number n, if n is in K, then S(n) is in K"?

Name: Anonymous 2013-03-02 6:22

>>13
From axiom 6, step it up.

Name: Anonymous 2013-03-02 6:27

>>14
How do you know "axiom 6" is correct?

Name: Anonymous 2013-03-02 6:29

>>15

You write gibberish, please don't write random sequences of symbols.

Name: Anonymous 2013-03-02 6:43

>>16
How do you know "i write gibberish"?

Name: Anonymous 2013-03-02 6:57

>>17

Do you even write English? You know, symbols, like letters, have only meaning if you order them correctly.

Name: Anonymous 2013-03-02 8:55

Name: Anonymous 2013-03-02 9:45

>>18
How do you know?

Name: Anonymous 2013-03-02 9:46

>>20

Are you chinese or something?

Name: Anonymous 2013-03-02 9:49

>>21
How do you know?

Name: Anonymous 2013-03-02 9:50

>>22

Cannot parse alien signs.

Name: Anonymous 2013-03-02 9:50

>>23

Cannot parse alien signs.

Name: Anonymous 2013-03-02 9:51

>>24
>>26

Cannot parse alien signs.

Name: Anonymous 2013-03-02 9:51

>>25

Cannot parse alien signs.

Name: Anonymous 2013-03-02 9:54

Define "define", "cannot", "parse", "alien", "signs" and ".", Nikita.

Define Nikita.

Name: Anonymous 2013-03-02 9:55

>>1-28

Cannot parse alien signs.

Name: Anonymous 2013-03-02 9:59

>>27

Nikita:
Female name, literally delicate flower.

Female:
Female

name:
name

literally:
literally

delicate:
delicate

flower:
flower

define:
define

cannot:
cannot

parse:
parse

alien:
alien

sign:
sign, sign ..

.:
.

,:
,

::
:

Name: Anonymous 2013-03-02 10:20

>>29
AAHAHAHA NIKITA IS A FUCKING FAGGOT

Name: Anonymous 2013-03-02 10:52

I suppose there are bugs in this, but you get the idea:

(define yield-to-event-loop (lambda () (error "not set")))

(define (main-loop)
  (dispatch (get-next-event))
  (main-loop))

(define (dispatch event)
  (cond
    ; Other internal and external events
    ; ...

    ((io-event? event)
     (let ((cont (get-io-continuation (get-io-ticket event))))
       (cont (get-io-data event))))

    ((resumable? event)
     ((get-resumable-continuation event)))

    (else (unhandled-event event))))

(define (resource-manager init n)
  ; Limited number of resources, for instance file descriptors
  (define resources (make-list-of-n-resources init n))
  (define wait-queue '())

  ; Return a free resource, and wait until there is one to get
  (define (get-resource)
    (call-with-current-continuation
      (lambda (k)
        (if (null? resources)
            (begin
              ; No free resource, resume caller later
              (set! waiters (cons k wait-queue))
              (yield-to-event-loop))
            (let ((resource (car resources)))
              (set! resources (cdr resources))
              resource)))))

  ; Free a single resource and possibly let a waiting event handler run
  (define (free-resource resource)
    (call-with-current-continuation
      (lambda (k)
        (if (null? wait-queue)
            (set! resources (cons resource resources))
            (let (waiter (car wait-queue))
              (set! waiters (cdr wait-queue))

              ; Suspend the caller for a while
              (push-resumable-event k)

              ; Resume a waiting event handler
              (waiter resource)))))
    #t)

  ; Return dispatcher
  (lambda (message)
    (case message
      ((get-resource) get-resource)
      ((free-resource) free-resource)
      (else (error "unknown message")))))

(define (read-from-io-device device)
  (call-with-current-continuation
    (lambda (k)
      (let (io-ticket (schedule-read device))
        (push-event-queue-io-continuation io-ticket k)
        (start-io-event io-ticket)
        (yield-to-event-loop)))))

(define io-manager (resource-manager make-io-port 4))

; Register some event handlers
(for-each
  (lambda (event)
    (register-event-handler event
      (lambda ()
        ; Here, without continuations, you end up with node.js
        ; get-resource, free-resource and read-from-io-device all
        ; yield to the event loop if necessary
        (let ((device ((io-manager 'get-resource))))
          (display (read-from-io-device device)
          ((io-manager 'free-resource) device))))))
  '(event-1 event-2 event-3))

; Start main loop
(begin
  (call-with-current-continuation (lambda (k) (set! yield-to-event-loop k)))
  (main-loop))

Name: Anonymous 2013-03-02 11:03

>>29
it's a russian male-only name and french female name, french adopted it from russian

http://en.wikipedia.org/wiki/Nikita_(given_name)
>Beginning in the 20th century, it was adopted for female children in some countries such as France

also it has different stressing, russian name is pronounced as nik**i**ta while french one as nikit**a**

Name: Anonymous 2013-03-02 11:08

>>32

It is a Haryana name which means: as beautiful a girl as the moon.

Name: Anonymous 2013-03-02 11:09

>>33
lolol is ur nikita a girl

Name: Anonymous 2013-03-02 11:33

>>34

Yes and it's related to Yadira, which means friend in HEBREW.

Name: Anonymous 2013-03-02 11:34

Nikita Sadkov - JEW

Name: Anonymous 2013-03-02 11:39

Nikita Sadkov - JEW
Sadkov - JEW
- JEW
JEW

Name: Anonymous 2013-03-02 11:40

>>34
Why is that so many language developers are MtF transgender people? Audrey Tang, some chick from PyPy if I recall correctly, now Nikita "Delicate flower" Sadkova joins the ranks.

Name: Anonymous 2013-03-02 11:42

>>38

Audry Tang was from Pugs, Perl6 implementation in Haskell. Maybe because programmers look too much futa?

Name: Anonymous 2013-03-02 11:59

>>39

Audry tang is a faggot.

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

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