conts

stevan apter — 2003-06-02 12:13:20

----- Original Message -----
From: <phimvt@...>
To: <concatenative@yahoogroups.com>
Sent: Monday, June 02, 2003 12:00 AM
Subject: Re: [stack] Re: K for Joy Programmers (paper), continuations


>
> On Sat, 31 May 2003, stevan apter wrote:
[..]

> Apart from explicit continuations like that, Joy still has an
> operator 'conts' which pushes the current implicit continuations
> on the stack - but the manual currently warns about its use.
> It is harmless, and perhaps instructive, to put
> conts putln
> inside a program to see what outstanding implicit continuations there are.
> I had hoped that I would find a use for it that could not be achieved by
> explicit continuations. I have not thought about the topic for quite a
> while, and for the time being I'll remain openminded. Any ideas?
>

an explanation of the behavior of conts would be appreciated.

if i understand correctly, 'conts' pushes the future of the
current computation onto the stack. so i understand

2 3 conts putln + 4 5 * - .
[[+ 4 5 * -]]
-15

(but why the extra level of quotation?)

but what's going on here?

[2 3 conts putln + 4 5 * -] i .
[[+ 4 5 * -] []]
-15

how do you calculate "the future" here? perhaps a wider range of
examples will render it obvious.

John Cowan — 2003-06-02 12:19:10

stevan apter scripsit:

> if i understand correctly, 'conts' pushes the future of the
> current computation onto the stack. so i understand
>
> 2 3 conts putln + 4 5 * - .
> [[+ 4 5 * -]]
> -15
>
> (but why the extra level of quotation?)

Because not only the rest of the current quotation, but the rest of
all quotations in which it is nested, is the value of conts, which is
why it is conts, not cont.

--
Long-short-short, long-short-short / Dactyls in dimeter,
Verse form with choriambs / (Masculine rhyme): jcowan@...
One sentence (two stanzas) / Hexasyllabically http://www.reutershealth.com
Challenges poets who / Don't have the time. --robison who's at texas dot net

stevan apter — 2003-06-03 00:05:23

2 3 conts putln + .
[[+]]
5

why isn't conts

[[putln +]]

is 'putln' filtered as an assist to debugging when programming
with continuations? i.e. as a convenience?

John Cowan — 2003-06-03 18:44:03

stevan apter scripsit:
> 2 3 conts putln + .
> [[+]]
> 5
>
> why isn't conts
>
> [[putln +]]
>
> is 'putln' filtered as an assist to debugging when programming
> with continuations? i.e. as a convenience?

I think because when putln executes the continuation has been changed; i.e.
conts does not really *save* the continuation. My naive Scheme implementation
had this bug.

--
Do NOT stray from the path! John Cowan <jcowan@...>
--Gandalf http://www.ccil.org/~cowan

stevan apter — 2003-06-03 23:46:01

----- Original Message -----
From: "John Cowan" <cowan@...>
To: <concatenative@yahoogroups.com>
Sent: Tuesday, June 03, 2003 2:44 PM
Subject: Re: [stack] conts


> stevan apter scripsit:
> > 2 3 conts putln + .
> > [[+]]
> > 5
> >
> > why isn't conts
> >
> > [[putln +]]
> >
> > is 'putln' filtered as an assist to debugging when programming
> > with continuations? i.e. as a convenience?
>
> I think because when putln executes the continuation has been changed; i.e.
> conts does not really *save* the continuation. My naive Scheme implementation
> had this bug.

ck works as i would expect:

2 3 conts print +
[[print +]]
5

in my implementation, 'eval' is called with two arguments: x, the stack,
and y, a list of things to evaluate. before i enter the evaluation loop,
i prepend y to the continuation stack C, and when i'm done evaluating, i
drop that list (which should be empty) from C. within the evaluation loop
the logic is:

drop first item of the current continuation (i.e. next item in y)
evaluate next item in y

so when the next item in y is 'conts', i drop 'conts' from the head of
C[0], leaving (in our example) C[0] = [print +], then evaluate 'conts'
(which pushes C onto the stack.

now here's a very strange thing. a few weeks ago, when we were discussing
paul chapman's accumulator-generator problem, i was worried about the
fact that the joy solution contained an explicit self-reference, and i
thought that it would be possible to implement something like 'self', to
get the context of the call. i wrestled with this for an hour or so,
inadvertently implementing the continuation logic i described above.
i mean, it was inadvertent since i hadn't even thought about continuations
at that point (i had to read up on them to remember what they were).
continuations took care of the future context, but the *past* had
already been eaten up in the evaluation. billy saw where i was heading
before i did, and said so, but i didn't understand his comment at the
time.



>
> --
> Do NOT stray from the path! John Cowan <jcowan@...>
> --Gandalf http://www.ccil.org/~cowan
>
>
> To unsubscribe from this group, send an email to:
> concatenative-unsubscribe@egroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>

Nick Forde — 2003-06-04 09:13:34

John Cowan writes:
> stevan apter scripsit:
> > 2 3 conts putln + .
> > [[+]]
> > 5
> >
> > why isn't conts
> >
> > [[putln +]]
> >
> > is 'putln' filtered as an assist to debugging when programming
> > with continuations? i.e. as a convenience?
>
> I think because when putln executes the continuation has been changed; i.e.
> conts does not really *save* the continuation. My naive Scheme implementation
> had this bug.

Looking at interp.c:198 the current implementation of conts_() is:

PUSH(conts_,LIST_NEWNODE,LIST_NEWNODE(conts->u.lis->next,conts->next))

Changing this to:

PUSH(conts_,LIST_NEWNODE,LIST_NEWNODE(conts->u.lis,conts->next))

Fixes the problem above:

2 3 conts putln + .
[[putln +]]
5

The variable conts is set in exeterm(). Manfred will have to comment
on where the bugs are as I'm not sure of the desired behaviour and
haven't studied this function in any detail. e.g. with the above
code change:

1 2 3 [conts 4 5] i 6 7 _.
[1 2 3 [[4 5] []] 4 5 6 7]
7

[[conts [conts 1 2] 3] i] i _.
[[[[conts 1 2] 3] [] []] [conts 1 2] 3]
3

I too have wondered about conts for some time. It seems like it could
be pretty powerful but I've not yet found a good application. The
explicit continuations mentioned by Manfred seem easier to use and
more natural as conts forces an extra level of evaluation. I'm sure
there must be some scenarios where this isn't the case. I've probably
just not done enough Joy programming to see them.

Regards,

Nick.

stevan apter — 2003-06-04 12:09:40

>
> I too have wondered about conts for some time. It seems like it could
> be pretty powerful but I've not yet found a good application. The
> explicit continuations mentioned by Manfred seem easier to use and
> more natural as conts forces an extra level of evaluation. I'm sure
> there must be some scenarios where this isn't the case. I've probably
> just not done enough Joy programming to see them.

coroutines?

i've seen the following problem posed in this context.

you've got a program which recurses through a tree and
prints each leaf prefixed by depth-many spaces:

[[[dup @:] dip swap]
[swap $ ,: sysout]
[1 + [pt] left]
ifte] `pt def;

[[1 2 3]4 5] 0 pt;
1
2
3
4
5

(@: is the leaf predicate, 3 5 $: yields " 5", and
,: sysout enlists a string and prints it with a new
line. left takes a b [p] and applies p to a[0] b,
a[1] b, ... so pt reads: if atomic, print, else
apply pt to each subtree with incremented depth.)

the problem is: you're given two isomorphic trees.
write ??? which uses pt to print corresponding leaves:

[[1 2 3]4 5] [[10 20 30]40 50] 0 ???
1
10
2
20
3
30
4
40
5
50

i don't *know* that this can be done with conts, but
it seems promising.

any ideas?


>
> Regards,
>
> Nick.
>

phimvt@lurac.latrobe.edu.au — 2003-06-17 03:33:09

Please ignore this post,
unless you sent me a private email with the above subject.
This is just a courtesy reply.

I responded to your email only today, sorry. But it bounced with
"illegal host/domain name" (your spam protection ?)
There is no point in repeating it here, since if you can read
this, then you can obviously read the pages.

- Manfred

phimvt@lurac.latrobe.edu.au — 2003-06-17 03:57:52

On Wed, 4 Jun 2003, Nick Forde wrote:

> John Cowan writes:

[..]

> Looking at interp.c:198 the current implementation of conts_() is:
>
> PUSH(conts_,LIST_NEWNODE,LIST_NEWNODE(conts->u.lis->next,conts->next))
>
> Changing this to:
>
> PUSH(conts_,LIST_NEWNODE,LIST_NEWNODE(conts->u.lis,conts->next))
>
> Fixes the problem above:

You are right, of course. I have a very vague recollection (5 years
ago) that your proposal was actually my first solution, but that
I changed it for some reason. It had something to do with actually
executing the continuation, rather than printing it, as in

... conts execonts ....

where 'execonts' was something like '[i] step' or so.
After executing 'conts', the 'execonts' is part of the continuation, and
what gets executed by the actual call to 'execonts' is the whole
continuation, starting with 'execonts' (which does NOT find
an appropriate structure on top of the stack. So it was a sort
of chicken/egg problem in reverse. But my memory is very vague.

> The variable conts is set in exeterm(). Manfred will have to comment
> on where the bugs are as I'm not sure of the desired behaviour and
^^^^^^^
I can't give the sort of specific answer you need. The general
answer "something useful" is far too vague.

[..]

> be pretty powerful but I've not yet found a good application. The
> explicit continuations mentioned by Manfred seem easier to use and
> more natural as conts forces an extra level of evaluation.

It's not as bad as it might seem. It is not that we have an interpreter
interpreting an interpreter. It is a top-level stepping through
the conts.

Thanks for the thoughts, Nick.

For the time being I won't change the implementation of conts
until someone finds an application using the old or the new version.

- Manfred

phimvt@lurac.latrobe.edu.au — 2003-06-17 04:12:13

On Wed, 4 Jun 2003, stevan apter wrote:

> i've seen the following problem posed in this context.
>
> you've got a program which recurses through a tree and
> prints each leaf prefixed by depth-many spaces:
[..]
> the problem is: you're given two isomorphic trees.
> write ??? which uses pt to print corresponding leaves:
[..]
> i don't *know* that this can be done with conts, but
> it seems promising.

Yes, maybe. The kind of explicit continuation that I have used
for backtracking would probably not be useful here.

Another example, this time for trees that need not be isomorphic,
the "Same-Fringe" problem (Henderson, the Lispkit book):
given two trees, determine whether corresponding leaves
are identical - jump out and return "false" when a different
pair is encountered. Without the 'jump out' it is easy:
flatten both trees to form two lists, determine whether
the two lists are identical. Henderson gives a continuation
solution.

- Manfred

stevan apter — 2003-06-17 13:10:36

thinking about continuations gives me a headache. the
received wisdom seems to be that most of the time you
don't need them, but every so often you do, and then you
*really* need them (your leaf-equality example seems
to qualify.)

in the simple case of coroutines, it seems like there
is a more direct route in joy: fork the stack. e.g.
you have p, which prints a tree by recursing through
it, keeping track of depth. you have ... u t [p] where
u and t are isomorphic trees. so you have a combinator
'fork' which takes an int n and a program p and splits
the stack, then runs each instruction in p sequentially
across the stacks:

... u t 2 [p] fork

->

... u
... t

when all forks terminate, return something that looks
like this:

... x y

where x and y are the results left on the forked stacks.

?

----- Original Message -----
From: <phimvt@...>
To: <concatenative@yahoogroups.com>
Sent: Tuesday, June 17, 2003 12:12 AM
Subject: Re: [stack] conts


>
> On Wed, 4 Jun 2003, stevan apter wrote:
>
> > i've seen the following problem posed in this context.
> >
> > you've got a program which recurses through a tree and
> > prints each leaf prefixed by depth-many spaces:
> [..]
> > the problem is: you're given two isomorphic trees.
> > write ??? which uses pt to print corresponding leaves:
> [..]
> > i don't *know* that this can be done with conts, but
> > it seems promising.
>
> Yes, maybe. The kind of explicit continuation that I have used
> for backtracking would probably not be useful here.
>
> Another example, this time for trees that need not be isomorphic,
> the "Same-Fringe" problem (Henderson, the Lispkit book):
> given two trees, determine whether corresponding leaves
> are identical - jump out and return "false" when a different
> pair is encountered. Without the 'jump out' it is easy:
> flatten both trees to form two lists, determine whether
> the two lists are identical. Henderson gives a continuation
> solution.
>
> - Manfred
>
>
>
>
>
>
> To unsubscribe from this group, send an email to:
> concatenative-unsubscribe@egroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>