Re: [Factor-talk] Stack Depth

2012-07-08 Thread Jim Mack
How do you know the optimizing compiler does or doesn't perform one
additional read/write when the result goes to 'the' stack instead of going
to a different data structure?  I couldn't build a compiler, but I think
the registers are being used at times, and that the stack is a metaphor.
 Factor certainly is written better than simply treating each word as a
jump, which early forths did, before inlining.  It may be fewer ops to do
it the first way rather than the second, as the second actually contains
the first, plus the temp storage of cut pieces pre-pan.



On Sat, Jul 7, 2012 at 2:03 AM, graham telfer gakouse...@hotmail.comwrote:



 --
 Thanks to everybody who gave me suggestions about the stack. I'd like to
 add a final comment about why I think the stack is useful in this case.

 Imagine you are going to chop up a carrot to cook it. There are 2 ways you
 might go about the job: the first is to cut off a piece and then turn and
 put it into the pan ready to cook. Keep doing this, cutting one piece at a
 time, turning and putting each piece of carrot into the pan until there is
 no carrot left. The second way is to cut the carrot into pieces and then
 put all of the pieces into the pan as a single operation.

 I see the second way as analagous to using the stack. Do all the dividing
 then put all the values into the data structure as a single operation. I do
 not see this as using the stack as a data structure; just a convenient
 holding pen.


 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk


--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-07-07 Thread graham telfer



Thanks to everybody who gave me suggestions about the stack. I'd like to add a 
final comment about why I think the stack is useful in this case.

Imagine you are going to chop up a carrot to cook it. There are 2 ways you 
might go about the job: the first is to cut off a piece and then turn and put 
it into the pan ready to cook. Keep doing this, cutting one piece at a time, 
turning and putting each piece of carrot into the pan until there is no carrot 
left. The second way is to cut the carrot into pieces and then put all of the 
pieces into the pan as a single operation.

I see the second way as analagous to using the stack. Do all the dividing then 
put all the values into the data structure as a single operation. I do not see 
this as using the stack as a data structure; just a convenient holding pen.
  --
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-07-06 Thread Adam
Expressed in Factor:

: depth ( -- n ) datastack length ;

: package-stack ( -- seq )
datastack [ depth narray ] with-datastack ;

...but just the datastack word itself will result in the same sequence ;-)

On Tue, Jun 19, 2012 at 8:52 PM, Graham Telfer gakouse...@hotmail.com wrote:
 Doug Coleman doug.coleman@... writes:


 How would you do this in C? Would you take the return address off the
 stack frame, then start calculating the division-by-2s and pushing
 them to the local stack frame in a loop, and then walk back up
 accumulating them in an array until you hit the return address, at
 which point you return the array?

 That's kind of what it sounds like you're suggesting, which would be
 kind of fun but totally impractical. In C, you'd collect them
 immediately after you calculate them, too, just like you should in
 Factor.

 The data stack is not for storing all your data, just for threading
 arguments and return values between word calls in a predictable way
 (known stack height.)

 Is that helpful? :)

 Doug

 I know nothing about C. In Prolog an accumulator is exactly what I'd use.

 Forth has a Depth word which is partly why I expected one in Factor. When 
 the
 job has finished the stack depth is known and

 As an alternative data structure to I tried constructing a list and adding 
 each
 new result to it; but the list data structure in Factor is so messy and 
 unwieldy
 I quickly decided not to use it. The Lisp words cons cadr are dreadful.
 Prolog and Haskell are much more elegant in their use of lists.




 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Doug Coleman doug.coleman@... writes:

 
 In general, you shouldn't want to do this, as all Factor words
 (besides the one I'm about to show you) need a fixed number of
 parameters at compile-time. Macros expand at compile-time, so that's
 one way to get around the restriction.
 
 The other way it to use a slow word called with-datastack:
 
 { 1 2 } [ + ] with-datastack
 
 You can also use smart combinators:
 
 [ 2 1 - 3 ] sum-outputs
 
 This works because the quotation infers at compile-time, and
 sum-outputs is a smart combinator that expands based on the number
 of outputs inferred.
 
 You are trying to do a C varargs function, basically, and just as in
 C, most Factor words are not varags.
 
 If you have more details about what you want to program, we could help you!
 
 Doug

Hi Doug,

I want to divide an integer by 2 until it gets to 1.

I have this snippet of code that goes into a loop.

dup 1  [ dup 2 /i ] [  ] if

When it gets to 1 I want to push the elements into a sequence.


--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread John Benediktsson

 I want to divide an integer by 2 until it gets to 1.

 I have this snippet of code that goes into a loop.

 dup 1  [ dup 2 /i ] [  ] if

 When it gets to 1 I want to push the elements into a sequence.


Take a look at the produce word:

: divide-until-2 ( n -- seq )
[ dup 1  ] [ 2 /i dup ] produce nip ;
--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Marshall Lochbaum
I figured there would be a word designed for that already... Here's my take
on this anyway.

I would append the items to a sequence as the algorithm goes along:

{ } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffix

accomplishes the task.

Marshall

On Tue, Jun 19, 2012 at 10:50 PM, Graham Telfer gakouse...@hotmail.comwrote:

 Doug Coleman doug.coleman@... writes:

 
  In general, you shouldn't want to do this, as all Factor words
  (besides the one I'm about to show you) need a fixed number of
  parameters at compile-time. Macros expand at compile-time, so that's
  one way to get around the restriction.
 
  The other way it to use a slow word called with-datastack:
 
  { 1 2 } [ + ] with-datastack
 
  You can also use smart combinators:
 
  [ 2 1 - 3 ] sum-outputs
 
  This works because the quotation infers at compile-time, and
  sum-outputs is a smart combinator that expands based on the number
  of outputs inferred.
 
  You are trying to do a C varargs function, basically, and just as in
  C, most Factor words are not varags.
 
  If you have more details about what you want to program, we could help
 you!
 
  Doug

 Hi Doug,

 I want to divide an integer by 2 until it gets to 1.

 I have this snippet of code that goes into a loop.

 dup 1  [ dup 2 /i ] [  ] if

 When it gets to 1 I want to push the elements into a sequence.



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
The example for the produce word is basically your solution already.

\ produce help

click the example

It should look like this:

USING: kernel math prettyprint sequences ;
1337 [ dup 0  ] [ 2/ dup ] produce nip .
{ 668 334 167 83 41 20 10 5 2 1 0 }

This is basically John's solution, too.

Cheers,
Doug

On Tue, Jun 19, 2012 at 8:09 PM, Marshall Lochbaum mwlochb...@gmail.com wrote:
 I figured there would be a word designed for that already... Here's my take
 on this anyway.

 I would append the items to a sequence as the algorithm goes along:

 { } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffix

 accomplishes the task.

 Marshall

 On Tue, Jun 19, 2012 at 10:50 PM, Graham Telfer gakouse...@hotmail.com
 wrote:

 Doug Coleman doug.coleman@... writes:

 
  In general, you shouldn't want to do this, as all Factor words
  (besides the one I'm about to show you) need a fixed number of
  parameters at compile-time. Macros expand at compile-time, so that's
  one way to get around the restriction.
 
  The other way it to use a slow word called with-datastack:
 
  { 1 2 } [ + ] with-datastack
 
  You can also use smart combinators:
 
  [ 2 1 - 3 ] sum-outputs
 
  This works because the quotation infers at compile-time, and
  sum-outputs is a smart combinator that expands based on the number
  of outputs inferred.
 
  You are trying to do a C varargs function, basically, and just as in
  C, most Factor words are not varags.
 
  If you have more details about what you want to program, we could help
  you!
 
  Doug

 Hi Doug,

 I want to divide an integer by 2 until it gets to 1.

 I have this snippet of code that goes into a loop.

 dup 1  [ dup 2 /i ] [  ] if

 When it gets to 1 I want to push the elements into a sequence.



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk


--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread John Benediktsson
If you are concerned about performance (suffix makes a new array each
time), then you can use vectors and push onto them, or use the make
vocabulary which provides some convenience mechanisms for that.  Or look
into collector-for which the produce word uses...


On Tue, Jun 19, 2012 at 8:09 PM, Marshall Lochbaum mwlochb...@gmail.comwrote:

 I figured there would be a word designed for that already... Here's my
 take on this anyway.

 I would append the items to a sequence as the algorithm goes along:

 { } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffix

 accomplishes the task.

 Marshall

 On Tue, Jun 19, 2012 at 10:50 PM, Graham Telfer gakouse...@hotmail.comwrote:

 Doug Coleman doug.coleman@... writes:

 
  In general, you shouldn't want to do this, as all Factor words
  (besides the one I'm about to show you) need a fixed number of
  parameters at compile-time. Macros expand at compile-time, so that's
  one way to get around the restriction.
 
  The other way it to use a slow word called with-datastack:
 
  { 1 2 } [ + ] with-datastack
 
  You can also use smart combinators:
 
  [ 2 1 - 3 ] sum-outputs
 
  This works because the quotation infers at compile-time, and
  sum-outputs is a smart combinator that expands based on the number
  of outputs inferred.
 
  You are trying to do a C varargs function, basically, and just as in
  C, most Factor words are not varags.
 
  If you have more details about what you want to program, we could help
 you!
 
  Doug

 Hi Doug,

 I want to divide an integer by 2 until it gets to 1.

 I have this snippet of code that goes into a loop.

 dup 1  [ dup 2 /i ] [  ] if

 When it gets to 1 I want to push the elements into a sequence.



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk




 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk


--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Marshall Lochbaum mwlochbaum@... writes:

 
 
 I figured there would be a word designed for that already... Here's my take on
this anyway.
 I would append the items to a sequence as the algorithm goes along:
 { } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffixaccomplishes the
task.
 
 Marshall

Hi Marshall,

Yes, this works but I feel the two tasks are conceptually different and I didn't
want to interleave them. I wanted to do all the division as 1 job and then just
push the results as the 2nd job. This way the code stays simple and properly
factored.

Graham



--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
John Benediktsson mrjbq7@... writes:

 
 
 I want to divide an integer by 2 until it gets to 1.
 I have this snippet of code that goes into a loop.
 dup 1  [ dup 2 /i ] [  ] if
 When it gets to 1 I want to push the elements into a sequence.
 
 
 Take a look at the produce word:
 
 : divide-until-2 ( n -- seq )
     [ dup 1  ] [ 2 /i dup ] produce nip ;


Hi John,

produce is exactly what I was looking for. This needs to go into the cook-book
as a useful 1 liner.

Graham


--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
How would you do this in C? Would you take the return address off the
stack frame, then start calculating the division-by-2s and pushing
them to the local stack frame in a loop, and then walk back up
accumulating them in an array until you hit the return address, at
which point you return the array?

That's kind of what it sounds like you're suggesting, which would be
kind of fun but totally impractical. In C, you'd collect them
immediately after you calculate them, too, just like you should in
Factor.

The data stack is not for storing all your data, just for threading
arguments and return values between word calls in a predictable way
(known stack height.)

Is that helpful? :)

Doug

On Tue, Jun 19, 2012 at 8:26 PM, Graham Telfer gakouse...@hotmail.com wrote:
 Marshall Lochbaum mwlochbaum@... writes:



 I figured there would be a word designed for that already... Here's my take 
 on
 this anyway.
 I would append the items to a sequence as the algorithm goes along:
 { } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffixaccomplishes the
 task.

 Marshall

 Hi Marshall,

 Yes, this works but I feel the two tasks are conceptually different and I 
 didn't
 want to interleave them. I wanted to do all the division as 1 job and then 
 just
 push the results as the 2nd job. This way the code stays simple and properly
 factored.

 Graham



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Doug Coleman
Actually, here's how you can do this in Factor using the with-datastack word:

USING: kernel math prettyprint sequences ;
{ 1337 } [ [ dup 1  ] [ 2/ dup ] while ] with-datastack

It's clean looking, but it's pretty slow.

Doug

On Tue, Jun 19, 2012 at 8:33 PM, Doug Coleman doug.cole...@gmail.com wrote:
 How would you do this in C? Would you take the return address off the
 stack frame, then start calculating the division-by-2s and pushing
 them to the local stack frame in a loop, and then walk back up
 accumulating them in an array until you hit the return address, at
 which point you return the array?

 That's kind of what it sounds like you're suggesting, which would be
 kind of fun but totally impractical. In C, you'd collect them
 immediately after you calculate them, too, just like you should in
 Factor.

 The data stack is not for storing all your data, just for threading
 arguments and return values between word calls in a predictable way
 (known stack height.)

 Is that helpful? :)

 Doug

 On Tue, Jun 19, 2012 at 8:26 PM, Graham Telfer gakouse...@hotmail.com wrote:
 Marshall Lochbaum mwlochbaum@... writes:



 I figured there would be a word designed for that already... Here's my take 
 on
 this anyway.
 I would append the items to a sequence as the algorithm goes along:
 { } swap [ dup 1  ] [ dup 2 /i [ suffix ] dip ] while suffixaccomplishes 
 the
 task.

 Marshall

 Hi Marshall,

 Yes, this works but I feel the two tasks are conceptually different and I 
 didn't
 want to interleave them. I wanted to do all the division as 1 job and then 
 just
 push the results as the 2nd job. This way the code stays simple and properly
 factored.

 Graham



 --
 Live Security Virtual Conference
 Exclusive live event will cover all the ways today's security and
 threat landscape has changed and how IT managers can respond. Discussions
 will include endpoint security, mobile security and the latest in malware
 threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
 ___
 Factor-talk mailing list
 Factor-talk@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Stack Depth

2012-06-19 Thread Graham Telfer
Doug Coleman doug.coleman@... writes:

 
 How would you do this in C? Would you take the return address off the
 stack frame, then start calculating the division-by-2s and pushing
 them to the local stack frame in a loop, and then walk back up
 accumulating them in an array until you hit the return address, at
 which point you return the array?
 
 That's kind of what it sounds like you're suggesting, which would be
 kind of fun but totally impractical. In C, you'd collect them
 immediately after you calculate them, too, just like you should in
 Factor.
 
 The data stack is not for storing all your data, just for threading
 arguments and return values between word calls in a predictable way
 (known stack height.)
 
 Is that helpful? :)
 
 Doug

I know nothing about C. In Prolog an accumulator is exactly what I'd use.

Forth has a Depth word which is partly why I expected one in Factor. When the
job has finished the stack depth is known and 

As an alternative data structure to I tried constructing a list and adding each
new result to it; but the list data structure in Factor is so messy and unwieldy
I quickly decided not to use it. The Lisp words cons cadr are dreadful.
Prolog and Haskell are much more elegant in their use of lists.




--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk