Re: [stack] Syntax for combinators
Manfred Von Thun <[email protected]>
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <C26128C5.929%[email protected]> |
On 2/5/07 1:38 AM, "John Cowan" <[email protected]> wrote: > > > > > James Hague scripsit: > >> > I would like to see [if-part] replaced with a simple Boolean flag. >> > Is there a reason it needs to be a quotation? I believe it's a Boolean >> > in Factor. > > There are two separate two-armed conditional words in Joy, "ifte" and > "choice". The latter behaves as you suggest: it takes a boolean > value and two quotations on the stack. However, "ifte" works differently: > after executing the [if-part], it gets the boolean value from the > top of the stack and then restores the stack to its former state. > > Thus the program > > 20 [pop true] ["yes"] ["no"] ifte > > is equivalent to > > 20 "yes" > > because although the pop is executed, its effect is undone by the > stack restoration. > > There are many other Joy combinators with similar behavior. For example, > "nullary" pops a combinator from the stack, preserves the stack, executes > the combinator, saves the top value, restores the stack, and pushes the > top value. Thus it does not matter how many items are popped from the > stack by the combinator. > > In the Joy interpreter, the stack is a linked list rather than a vector, > and garbage collection is used to reclaim stack fragments. Consequently, > restoring the stack is a cheap operation. The two combinators ifte and choice are interdefinable: [thenpart] [elsepart] choice == [] [thenpart] [elsepart] ifte == [thenpart] [elsepart] [] rollup ifte and hence: choice == [] rollup ifte [ifpart] [thenpart] [elsepart] ifte == [ifpart] nullary [thenpart][elepart] choice == [ifpart] [nullary] i [thenpart] [elsepart] choice == [ifpart] thenpart] [nullary] dip [elsepart] choice == [ifpart] [thenpart] [elsepart] [[nullary]dip] dip choice And hence: ifte == [[nullary] dip] dip choice. So the ifte combinator can be defined in tems of nullary and dip. Presumably the nullary combinator will evoke the same wide variety of reactions from programmers as the ifte combinator. By way of contrast, the dip combinator has never evoked such a variety. But what is surprising is that nullary need not be a primitive combinator, it can be defined in terms of dip or in terms of infra. I cannot remember the two definitions right now. But I¹ll look them up at home and post them next week. - Manfred [Non-text portions of this message have been removed]