Hi Elan,

You wrote:
"During recursive calls, REBOL is using a dynamic binding for 'x,
which means that each instance of recfunc, called from within
recfunc recursively, has its own context in which 'x is bound to
the value that was passed to the recfunc instance."

I would like to divide the paragraph into two parts:

"During recursive calls, REBOL is using a dynamic binding for 'x."

That looks allright, but the next part makes a problem:

"...each instance of recfunc, called from within recfunc
recursively, has its own context in which 'x is bound to the value
that was passed to the recfunc instance"

This is true for non-Rebol notion of "contexts". Once we
compare this to Rebol Contexts, we see:

blk: copy []
probeblk: func [] [
    prin mold blk
    prin ": "
    print mold reduce blk
]
recfun: func [x] [
    append blk 'x
    either x <= 1 [
        probeblk
        probe same? first blk second blk
    ] [
        recfun x - 1
    ]
]
recfun 2

The results:

>> recfun 2
[x x]: [1 1]
true
== true

While:

blk: copy []
probeblk: func [] [
    prin mold blk
    prin ": "
    print mold reduce blk
]
fun1: func [x] [
    append blk 'x
    either x <= 1 [
        probeblk
        probe same? first blk second blk
    ] [
        fun2: func [x] [
            append blk 'x
            probeblk
            probe same? first blk second blk
        ]
        fun2 x - 1
    ]
]
fun1 2

And the results:

>> fun1 2
[x x]: [2 1]
false
== false

>From that is clear, that in the case of Recfun, there is only one
'x. Words 'x inserted to Blk as the first and the second  element
are in fact just one word 'x that was bound to just one Rebol
Context - the (dynamic) Rebol Context of Recfun, while two Rebol
Contexts in the
case of Fun1 and Fun2 mean two words 'x in Blk, each bound to a
different Rebol Context (Fun1's context's 'x and Fun2's context's
'x).

Similarly the next statement's first part:
"Once the recursion expires the context of 'x is again bound to
the value it last had before recfunc called itself recursively..."

Is true, if we consider a different kind of binding - "Value
Binding", because "Context Binding" remains the same, the only
thing changing between Recfun calls is the value of 'x.

While the second part:
"Once the recursion expires (...), it ('x) is bound in the static
context of the top-level instance of the recfunc function."

Can not be interpreted as Rebol Context change, because Rebol
Context remains the same - namely that of Recfun and no Rebol
Context change is necessary.

As you pointed out, there is a conflict of two things:
"Static Context Binding" vs. "Dynamic Value Binding". As a
consequence, Use may misbehave during recursive calls.

Regards
    Ladislav

> Hi Ladislav, Brett,
>
> Ladislav wrote:
>
> >> blk: copy []
> >> probeblk: func [] [
> >>     prin mold blk
> >>     prin ": "
> >>     print mold reduce blk
> >> ]
> >> recfun: func [x] [
> >>     append blk 'x
> >>     either x <= 1 [
> >>         probeblk
> >>     ] [
> >>         recfun x - 1
> >>     ]
> >> ]
> >> recfun 3
> >> probeblk
>
> To which Brett replied
>
> >I would have expected [3 2 1]
> >not [1 1 1] nor [3 3 3].
>
> What you are demonstrating, Ladislav, is IMHO a conflict that
results from
> the fact that REBOL is using two different binding mechanisms.
>
> During recursive calls, REBOL is using a dynamic binding for 'x,
which
> means that each instance of recfunc, called from within recfunc
> recursively, has its own context in which 'x is bound to the
value that was
> passed to the recfunc instance.
>
> Once the recursion expires the context of 'x is again bound to
the value it
> last had before recfunc called itself recursively, it is bound
in the
> static context of the top-level instance of the recfunc
function.
>
> So 'x exists in multiple contexts: the static context of the
function
> recfunc, and the temporary dynamic contexts, that are created
for each
> recfunc instance, when recfunc calls itself recursively.
>
> When you reduce blk from within a recursively called instance of
recfunc,
> 'x is bound in the temporary dynamic context of the recursively
called
> function's instance. Once recfunc's loop expires and there are
no more
> recursively called instances of recfunc effective, then 'x is
once again
> bound in the context of the top-level recfunc function, its
static context,
> and the reduced block evaluates to the value that 'x is bound to
in that
> context.
>
>
> At 11:44 PM 7/19/00 +1000, you wrote:
> >I would have expected [3 2 1]
> >not [1 1 1] nor [3 3 3].
> >
> >But I find it difficult to answer what I want, because the
function argument
> >x seems like it's in a bit of a no-mans land (I'm thinking of
:x )
> >
> >My brain hurts now. :)
> >Brett.
> >
> >----- Original Message -----
> >From: <[EMAIL PROTECTED]>
> >To: <[EMAIL PROTECTED]>
> >Sent: Wednesday, July 19, 2000 5:03 PM
> >Subject: [REBOL] Bug in 'use? Re:(3)
> >
> >
> >> Hi,
> >>
> >> > Hi Ladislav, 15-Jul-2000 you wrote:
> >> >
> >> > >you are right, the problem is caused by a context
> >> manipulation -
> >> > >Use unsets your Middle every time it gets executed. My
> >> suggestion
> >> > >is to not use Use in recursive functions, while this
problem
> >> > >doesn't get corrected.
> >> >
> >> > Judging from the nature of recursiveness, that's a little
hard,
> >> isn't it? ;-)
> >> >
> >> > Do you know if this problem has already been reported to
> >> feedback?
> >> >
> >> > Kind regards,
> >> > --
> >> > Ole Friis <[EMAIL PROTECTED]>
> >> >
> >> > Amiga is a trademark of Amiga Inc.
> >> >
> >>
> >> You should probably report it to feedback. BTW, did you
succeed to
> >> sort the permutations correctly?
> >>
> >> One more question for everybody. What do you want to see
after
> >> executing:
> >>
> >> blk: copy []
> >> probeblk: func [] [
> >>     prin mold blk
> >>     prin ": "
> >>     print mold reduce blk
> >> ]
> >> recfun: func [x] [
> >>     append blk 'x
> >>     either x <= 1 [
> >>         probeblk
> >>     ] [
> >>         recfun x - 1
> >>     ]
> >> ]
> >> recfun 3
> >> probeblk
> >>
> >> Regards
> >>     Ladislav
> >>
> >
> >
> >
>
> ;- Elan [ : - ) ]








Reply via email to