# Re: [stack] Joy in Haskell [was: Re: Parsing in concatenative languages]

## Greg Buchholz — 2005-04-05 16:05:07

    --- "William Tanksley, Jr" <wtanksleyjr@...> wrote:

     > Ivan Tomac <e1_t@...> wrote:

    > > Maybe it is possible to rewrite every Haskell expression to not use

    > variables afterall?

    >

    > It is -- I don't know whether you can do that without using monads,

    > but you can do it with them. (Hint: a stack will work, in which case

    > Haskell will become concatenative, although with an explicit

    > concatenation operator.)

         I think you can do it without monads.  I recently looked at writing

    Joy's combinators in Haskell (see the thread...

    http://www.haskell.org/pipermail/haskell-cafe/2005-March/009312.html )

    The type of Joy's recursion combinators was giving me problems, but I

    think the end conclusion was that you could mechanically transform your

    Joy programs into Haskell...

    http://www.haskell.org/pipermail/haskell-cafe/2005-March/009342.html

    ...Of course, that conversion ends of being horrendously inefficient

    (unary numbers and all).  But I'm sure there are recursion combinators

    out there that are more amenable to static typing, so that you could have

    an efficient implementation.  Then you'd have a functional, lazy and

    statically-typed concatenative language.

    Greg Buchholz

    __________________________________

    Do you Yahoo!?

    Yahoo! Personals - Better first dates. More second dates.

    http://personals.yahoo.com

## William Tanksley, Jr — 2005-04-05 17:36:56

    Greg Buchholz <sleepingsquirrel@...> wrote:

     > an efficient implementation.  Then you'd have a functional, lazy and

    > statically-typed concatenative language.

     I'm curious: how could a concatenative language be lazy? What does

    that look like, in practice?

     > Greg Buchholz

     -Billy

## Greg Buchholz — 2005-04-05 18:06:58

    --- "William Tanksley, Jr" <wtanksleyjr@...> wrote:

     > Greg Buchholz <sleepingsquirrel@...> wrote:

    > > an efficient implementation.  Then you'd have a functional, lazy and

    > > statically-typed concatenative language.

    >

    > I'm curious: how could a concatenative language be lazy?

         Change call-by-value into call-by-need ;-)

     >What does that look like, in practice?

    >

         In Joy, the following little snippet dies because of a division by

    zero error...

    1 0 / "abc" swap pop

    ...but the result of the division isn't used in the rest of the

    computation (i.e. the "swap pop"), so it really doesn't need to be

    computed in the first place.  A lazy language wouldn't bother performing

    the division.  Try out the Joy-in-Haskell program below.  The first print

    statement succeeds and leaves "abc" on the stack, because the

    division-by-zero never happens.  The second print fails because the

    results are needed (i.e. we are printing out the values left on the

    stack).

    -- Joy combinators in Haskell

    -- and lazy evaluation

    module Joy where

    main =

       do

         --In Joy: 1 0 / "abc" swap pop

         --lazy evaluation saves us from division by zero...

         print $ ((lit 1) ! (lit 0) ! divide ! (lit "abc") ! swap ! pop) bot

         --not so lucky this time...

         print $ ((lit 1) ! (lit 0) ! divide ! (lit "abc")) bot

    bot = "EOS" -- end of stack

    (!) f g = g.f

    lit val stack    = (val, stack)

    quote = lit

    i    (a, b)      = (a b)

    add  (a, (b, c)) = (b+a, c)

    sub  (a, (b, c)) = (b-a, c)

    mult (a, (b, c)) = (b*a, c)

    divide (a, (b, c)) = (b `div` a, c)

    swap (a, (b, c)) = (b, (a, c))

    pop  (a, b)      =  b

    dup  (a, b)      = (a, (a, b))

    dip  (a, (b, c)) = (b, (a  c))

    eq   (a, (b, c)) | a == b    = (True, c)

                     | otherwise = (False,c)

    ifte (f, (t, (b, stack))) | fst (b stack) == True = (t stack)

                              | otherwise             = (f stack)

    times (p, (n, stack)) | n>0 = times (p, (n-1, (p stack)))

                          | otherwise = stack

    --end of program

    __________________________________

    Do you Yahoo!?

    Yahoo! Personals - Better first dates. More second dates.

    http://personals.yahoo.com

## Ivan Tomac — 2005-04-06 02:11:19

    --- In concatenative@yahoogroups.com, Greg Buchholz

    <sleepingsquirrel@y...> wrote:

     >

    > --- "William Tanksley, Jr" <wtanksleyjr@g...> wrote:

    > > Greg Buchholz <sleepingsquirrel@y...> wrote:

    > > > an efficient implementation.  Then you'd have a functional,

     lazy and

     > > > statically-typed concatenative language.

    > >

    > > I'm curious: how could a concatenative language be lazy?

    >

    >     Change call-by-value into call-by-need ;-)

    >

    > >What does that look like, in practice?

    > >

    >

    >     In Joy, the following little snippet dies because of a division

     by

     > zero error...

    >

    > 1 0 / "abc" swap pop

    >

    > ...but the result of the division isn't used in the rest of the

    > computation (i.e. the "swap pop"), so it really doesn't need to be

    > computed in the first place.  A lazy language wouldn't bother

     performing

     > the division.  Try out the Joy-in-Haskell program below.  The first

     print

     > statement succeeds and leaves "abc" on the stack, because the

    > division-by-zero never happens.  The second print fails because the

    > results are needed (i.e. we are printing out the values left on the

    > stack).

    >

     Wouldn't that break concatenative properties of the language?

    Ivan