Re: [Factor-talk] when-empty

2017-03-01 Thread Björn Lindqvist
It is because when-zero preserves the value on the stack if it isn't
zero. I think it is easier to see the mistake if you forego using the
when-* combinators:

: check0 ( n -- ) 0 = [ call-fail ] [ ] if ;

: check0 ( n -- ) [ call-fail ] [ ] if-zero ;

Both quotations supplied to if-zero has effect ( -- ), but that is not
right. The second quotation should lift one extra item from the stack.
As in:

: check0 ( n -- ) [ call-fail ] [ drop ] if-zero ;

When I write Factor code, I always start with the "easy" branch
combinators like `if` and `?`. Then after I have ensured that the code
does what I want and that the stack effects balance, I look for
opportunities to refactor using "fancier" combinators like `when`,
`?if`, `unless-empty` and so on.


2017-03-01 20:18 GMT+01:00 Alexander Ilin :
> Continuing with this example, here's what I stumbled upon (again), and found
> confusing (once more):
>
> This works as expected:
>
> ```
> : check0 ( n -- ) 0 = [ call-fail ] when ;
> ```
>
> This fails to compile:
>
> ```
> : check0 ( n -- ) [ call-fail ] when-zero ;
> ```
>
> But according to the documented stack effect (at least the way I read it,
> please correct me if I'm wrong) it should compile: n and quotation should be
> dropped from the stack by when-zero.
>
> 01.03.2017, 22:13, "Alexander Ilin" :
>
> Here's a slightly better stack effect documentation:
>
> `when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b )`
>
> But IIUC the actual stack effect is one of these:
>
> `when-zero ( ..a 0 quot: ( ..a -- ..b ) -- ..b )`
> `when-zero ( ..a n#0 quot: ( ..a -- ..b ) -- ..a n )`
>
> where # denotes inequality.
>
> Am I right? How does the compiler handle such ambiguity in the stack effect?
> 01.03.2017, 19:28, "John Benediktsson" :
>
> A stack effect of ( seq quot -- ) doesn't say what the quot is expected to
> do...
>
> You can play around with it and see how it works:
>
> IN: scratchpad "abc" [ "empty" ] when-empty .
> "abc"
>
> IN: scratchpad "" [ "empty" ] when-empty .
> "empty"
>
> We should improve the docs here because they are a little hard to follow,
> since it refers to ``when-empty`` taking the "first quot" of an
> ``if-empty``, with an empty second quot.
>
>
>
>
> On Wed, Mar 1, 2017 at 8:18 AM, Alexander Ilin  wrote:
>
> Hello!
>
>   I'm struggling with some of the Factor's words. Mostly with `when-empty`,
> `if-zero` and similar. It's difficult for me to understand why they don't
> work sometimes, while their simpler counterparts (while, if) do work exactly
> as expected.
>
>   Here's the latest installment of my struggle:
>
> IN: scratchpad USE: crypto.xor [ [ empty-xor-key ] when-empty ]  infer.
> ( x -- x )
>
>   Why is the stack effect as shown here? The documentation for `when-empty`
> says it takes `( seq quot -- )`. Therefore, I expect `[ [ empty-xor-key ]
> when-empty ]  infer.` to output `( x -- )`.
>
>   When I modify the quotation passed to `when-empty` to be a no-op, I get a
> compilation error:
> IN: scratchpad [ [ 1 drop ] when-empty ]  infer.
> Error
> The input quotations to “if-empty” don't match their expected effects
> Input  Expected   Got
> [ 1 drop ] ( ..a -- ..b ) ( -- )
> [ ]( ..a seq -- ..b ) ( -- )
>
>   That's completely weird to me. Is the stack effect in the documentation
> incorrect? Should it be `( seq quot -- seq )`?
>   And why does it refuse to compile the second example with `1 drop`?
>
> ---=---
>  Александр
>
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
> ,
>
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>
> ,
>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
>
> ---=---
> Александр
>
> ,
>
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>
> ,
>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
>
> ---=---
> Александр
>
>
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> ___
> Factor-talk mailing list
> Factor-talk@lists

Re: [Factor-talk] bi idiom?

2017-03-01 Thread John Benediktsson
I like dup , personally, but this pattern (used sometimes with tri 
also) is an experiment in readability avoiding stack shuffle words. As you say, 
the code is identical, but often fewer tokens is preferred. 

> On Mar 1, 2017, at 11:46 AM, Alexander Ilin  wrote:
> 
> Hello!
> 
>  Noticed this code:
> 
> ```
> M: windows-crypto-context random-bytes* ( n windows-crypto-context -- bytes )
>handle>> swap [ ] [  ] bi
>[ CryptGenRandom win32-error=0/f ] keep ; 
> ```
> 
>  Is the construction `[ ] [  ] bi` somehow idiomatic?
>  How is it better than `dup `? Is it more readable?
>  Or should it be replaced with the shorter version?
> 
> ---=--- 
> Александр
> 
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] bi idiom?

2017-03-01 Thread Alexander Ilin
Hello!

  Noticed this code:

```
M: windows-crypto-context random-bytes* ( n windows-crypto-context -- bytes )
handle>> swap [ ] [  ] bi
[ CryptGenRandom win32-error=0/f ] keep ; 
```

  Is the construction `[ ] [  ] bi` somehow idiomatic?
  How is it better than `dup `? Is it more readable?
  Or should it be replaced with the shorter version?

---=--- 
 Александр

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] when-empty

2017-03-01 Thread Alexander Ilin
Continuing with this example, here's what I stumbled upon (again), and found confusing (once more): This works as expected: ```: check0 ( n -- ) 0 = [ call-fail ] when ;``` This fails to compile: ```: check0 ( n -- ) [ call-fail ] when-zero ;``` But according to the documented stack effect (at least the way I read it, please correct me if I'm wrong) it should compile: n and quotation should be dropped from the stack by when-zero. 01.03.2017, 22:13, "Alexander Ilin" :Here's a slightly better stack effect documentation: `when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b )` But IIUC the actual stack effect is one of these: `when-zero ( ..a 0 quot: ( ..a -- ..b ) -- ..b )``when-zero ( ..a n#0 quot: ( ..a -- ..b ) -- ..a n )` where # denotes inequality. Am I right? How does the compiler handle such ambiguity in the stack effect?01.03.2017, 19:28, "John Benediktsson" :A stack effect of ( seq quot -- ) doesn't say what the quot is expected to do... You can play around with it and see how it works:     IN: scratchpad "abc" [ "empty" ] when-empty .    "abc"     IN: scratchpad "" [ "empty" ] when-empty .    "empty" We should improve the docs here because they are a little hard to follow, since it refers to ``when-empty`` taking the "first quot" of an ``if-empty``, with an empty second quot.On Wed, Mar 1, 2017 at 8:18 AM, Alexander Ilin  wrote:Hello!  I'm struggling with some of the Factor's words. Mostly with `when-empty`, `if-zero` and similar. It's difficult for me to understand why they don't work sometimes, while their simpler counterparts (while, if) do work exactly as expected.  Here's the latest installment of my struggle:IN: scratchpad USE: crypto.xor [ [ empty-xor-key ] when-empty ]  infer.( x -- x )  Why is the stack effect as shown here? The documentation for `when-empty` says it takes `( seq quot -- )`. Therefore, I expect `[ [ empty-xor-key ] when-empty ]  infer.` to output `( x -- )`.  When I modify the quotation passed to `when-empty` to be a no-op, I get a compilation error:IN: scratchpad [ [ 1 drop ] when-empty ]  infer.ErrorThe input quotations to “if-empty” don't match their expected effectsInput      Expected           Got[ 1 drop ] ( ..a -- ..b )     ( -- )[ ]        ( ..a seq -- ..b ) ( -- )  That's completely weird to me. Is the stack effect in the documentation incorrect? Should it be `( seq quot -- seq )`?  And why does it refuse to compile the second example with `1 drop`?---=--- Александр--Check out the vibrant tech community on one of the world's mostengaging tech sites, SlashDot.org! http://sdm.link/slashdot___Factor-talk mailing listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk,--Check out the vibrant tech community on one of the world's mostengaging tech sites, SlashDot.org! http://sdm.link/slashdot,___Factor-talk mailing listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk  ---=---Александр ,--Check out the vibrant tech community on one of the world's mostengaging tech sites, SlashDot.org! http://sdm.link/slashdot,___Factor-talk mailing listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk  ---=---Александр --
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] when-empty

2017-03-01 Thread Alexander Ilin
Here's a slightly better stack effect documentation: `when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b )` But IIUC the actual stack effect is one of these: `when-zero ( ..a 0 quot: ( ..a -- ..b ) -- ..b )``when-zero ( ..a n#0 quot: ( ..a -- ..b ) -- ..a n )` where # denotes inequality. Am I right? How does the compiler handle such ambiguity in the stack effect?01.03.2017, 19:28, "John Benediktsson" :A stack effect of ( seq quot -- ) doesn't say what the quot is expected to do... You can play around with it and see how it works:     IN: scratchpad "abc" [ "empty" ] when-empty .    "abc"     IN: scratchpad "" [ "empty" ] when-empty .    "empty" We should improve the docs here because they are a little hard to follow, since it refers to ``when-empty`` taking the "first quot" of an ``if-empty``, with an empty second quot.On Wed, Mar 1, 2017 at 8:18 AM, Alexander Ilin  wrote:Hello!  I'm struggling with some of the Factor's words. Mostly with `when-empty`, `if-zero` and similar. It's difficult for me to understand why they don't work sometimes, while their simpler counterparts (while, if) do work exactly as expected.  Here's the latest installment of my struggle:IN: scratchpad USE: crypto.xor [ [ empty-xor-key ] when-empty ]  infer.( x -- x )  Why is the stack effect as shown here? The documentation for `when-empty` says it takes `( seq quot -- )`. Therefore, I expect `[ [ empty-xor-key ] when-empty ]  infer.` to output `( x -- )`.  When I modify the quotation passed to `when-empty` to be a no-op, I get a compilation error:IN: scratchpad [ [ 1 drop ] when-empty ]  infer.ErrorThe input quotations to “if-empty” don't match their expected effectsInput      Expected           Got[ 1 drop ] ( ..a -- ..b )     ( -- )[ ]        ( ..a seq -- ..b ) ( -- )  That's completely weird to me. Is the stack effect in the documentation incorrect? Should it be `( seq quot -- seq )`?  And why does it refuse to compile the second example with `1 drop`?---=--- Александр--Check out the vibrant tech community on one of the world's mostengaging tech sites, SlashDot.org! http://sdm.link/slashdot___Factor-talk mailing listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk,--Check out the vibrant tech community on one of the world's mostengaging tech sites, SlashDot.org! http://sdm.link/slashdot,___Factor-talk mailing listFactor-talk@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/factor-talk  ---=---Александр --
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] External Library Init

2017-03-01 Thread John Benediktsson
Use a startup hook if you want code run every time Factor starts up. Probably 
necessary for most init calls. 

Top level code like your example would only run the first time the vocabulary 
is USE'd. You could also have every word call something like a `maybe-init` 
before doing what they would do with the library.



> On Mar 1, 2017, at 9:13 AM, Alexander Ilin  wrote:
> 
> Hello!
> 
>  What's the proper way of making sure an external library is initialized?
> 
>  I'm making bindings for libsodium, and it wants `int sodium_init()` to be 
> called before any other functions are used.
> 
>  I'm thinking about simply adding the following code at the end of my FFI 
> vocab:
> 
> ```
> ERROR: sodium-init-fail ;
> 
> ! Call this on library load, may be called multiple times.
> sodium_init 0 < [ sodium-init-fail ] when
> ```
> 
>  There is no penalty in calling it multiple times, it'll simply return 1 in 
> those cases. So, that means we can skip adding a variable preventing multiple 
> calls, and simply do the call when we think we may need one (the variable is 
> already implemented by the library itself).
> 
>  Is that the best way to do it? Do I have to do anything additional with 
> `add-startup-hook` and such? What if Factor loads an image with the vocab 
> already loaded? Won't that skip the `sodium_init` call?
> 
>  Is there anything else I should take care of?
> 
> ---=--- 
> Александр
> 
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] External Library Init

2017-03-01 Thread Alexander Ilin
Hello!

  What's the proper way of making sure an external library is initialized?

  I'm making bindings for libsodium, and it wants `int sodium_init()` to be 
called before any other functions are used.

  I'm thinking about simply adding the following code at the end of my FFI 
vocab:

```
ERROR: sodium-init-fail ;

! Call this on library load, may be called multiple times.
sodium_init 0 < [ sodium-init-fail ] when
```

  There is no penalty in calling it multiple times, it'll simply return 1 in 
those cases. So, that means we can skip adding a variable preventing multiple 
calls, and simply do the call when we think we may need one (the variable is 
already implemented by the library itself).

  Is that the best way to do it? Do I have to do anything additional with 
`add-startup-hook` and such? What if Factor loads an image with the vocab 
already loaded? Won't that skip the `sodium_init` call?

  Is there anything else I should take care of?

---=--- 
 Александр

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] when-empty

2017-03-01 Thread John Benediktsson
A stack effect of ( seq quot -- ) doesn't say what the quot is expected to
do...

You can play around with it and see how it works:

IN: scratchpad "abc" [ "empty" ] when-empty .
"abc"

IN: scratchpad "" [ "empty" ] when-empty .
"empty"

We should improve the docs here because they are a little hard to follow,
since it refers to ``when-empty`` taking the "first quot" of an
``if-empty``, with an empty second quot.




On Wed, Mar 1, 2017 at 8:18 AM, Alexander Ilin  wrote:

> Hello!
>
>   I'm struggling with some of the Factor's words. Mostly with
> `when-empty`, `if-zero` and similar. It's difficult for me to understand
> why they don't work sometimes, while their simpler counterparts (while, if)
> do work exactly as expected.
>
>   Here's the latest installment of my struggle:
>
> IN: scratchpad USE: crypto.xor [ [ empty-xor-key ] when-empty ]  infer.
> ( x -- x )
>
>   Why is the stack effect as shown here? The documentation for
> `when-empty` says it takes `( seq quot -- )`. Therefore, I expect `[ [
> empty-xor-key ] when-empty ]  infer.` to output `( x -- )`.
>
>   When I modify the quotation passed to `when-empty` to be a no-op, I get
> a compilation error:
> IN: scratchpad [ [ 1 drop ] when-empty ]  infer.
> Error
> The input quotations to “if-empty” don't match their expected effects
> Input  Expected   Got
> [ 1 drop ] ( ..a -- ..b ) ( -- )
> [ ]( ..a seq -- ..b ) ( -- )
>
>   That's completely weird to me. Is the stack effect in the documentation
> incorrect? Should it be `( seq quot -- seq )`?
>   And why does it refuse to compile the second example with `1 drop`?
>
> ---=---
>  Александр
>
> 
> --
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] when-empty

2017-03-01 Thread Alexander Ilin
Hello!

  I'm struggling with some of the Factor's words. Mostly with `when-empty`, 
`if-zero` and similar. It's difficult for me to understand why they don't work 
sometimes, while their simpler counterparts (while, if) do work exactly as 
expected.

  Here's the latest installment of my struggle:

IN: scratchpad USE: crypto.xor [ [ empty-xor-key ] when-empty ]  infer.
( x -- x )

  Why is the stack effect as shown here? The documentation for `when-empty` 
says it takes `( seq quot -- )`. Therefore, I expect `[ [ empty-xor-key ] 
when-empty ]  infer.` to output `( x -- )`.

  When I modify the quotation passed to `when-empty` to be a no-op, I get a 
compilation error:
IN: scratchpad [ [ 1 drop ] when-empty ]  infer.
Error
The input quotations to “if-empty” don't match their expected effects
Input  Expected   Got
[ 1 drop ] ( ..a -- ..b ) ( -- )
[ ]( ..a seq -- ..b ) ( -- )

  That's completely weird to me. Is the stack effect in the documentation 
incorrect? Should it be `( seq quot -- seq )`?
  And why does it refuse to compile the second example with `1 drop`?

---=--- 
 Александр

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk