Factor/FAQ/Develop

I've found a bug. What should I do?

First, check Bug reports to see if its a known issue. Then, report the bug on that page, in the Concatenative IRC channel, or on the Mailing list.

How do I store my code outside of the Factor source tree?

See http://docs.factorcode.org/content/article-add-vocab-roots.html.

What are compiler errors/warnings?

See the following two help articles:

Compiler errors and warnings indicate problems in your code and should be fixed. If a combinator compiles with a warning, declare it inline; this will suppress the warning, and its callers will then compile without warnings. See the above two links for full details.

I tried entering some code in the listener that came from the docs or from a Factor programmer, however I get a "no word found" error. Is the code wrong?

If you invoke a word that does not exist, you get an error like the following,

( scratchpad ) fadf
1: fadf
       ^
Word not found in current vocabulary search path
"name" "fadf"

The following restarts are available:

:1    Defer word in current vocabulary

Type :help for debugging help.

However, note that the message says "Word not found in current vocabulary search path". Sometimes the word might exist, but its vocabulary won't be in the listener's search path (not all vocabularies are added by default). In that case, the error looks similar, but you get restarts:

( scratchpad ) "Hello world" <label>
1: "Hello world" <label>
                        ^
Word not found in current vocabulary search path
"name" "<label>"

The following restarts are available:

:1    Use the cpu.architecture vocabulary
:2    Use the ui.gadgets.labels vocabulary
:3    Defer word in current vocabulary

Type :help for debugging help.

In the above case, you can invoke :1 or :2 to add the cpu.architecture or ui.gadgets.labels vocabularies to the search path, as if you had typed USE: cpu.architecture or USE: ui.gadgets.labels. See http://docs.factorcode.org/content/article-errors-restartable.html for more information.

What is the difference between USE: foo and USING: foo ; ?

There is none. USING: is equivalent to multiple USE: in a row.

If I do 1 foo set at the toplevel of a file, then why does foo get in the listener give me f after loading that file?

When parsing a file and executing toplevel code, a new dynamic scope is created. If you want to have that value of foo be accessable elsewhere, use 1 foo set-global or : foo 1 ; depending on whether foo's value needs to change. You may, instead, want to make a word like : init-foo 1 foo set ; to initialize that value.

How can hashtables and arrays be implemented efficiently within a language where the principal data structure is a stack?

This is actually a common misconception. Factor's stack is not a data structure -- it is a means of passing values between words. Factor supports hashtables easily enough; you create a new hashtable by calling <hashtable>, which pushes an object on the stack, then you manipulate it with words such as at and set-at. The important thing is that the hashtable sits on the stack as a single value; values on the stack are not limited to scalars such as numbers and booleans, they can be complex types too (arrays, hashtables, tuples, ...). Furthermore, if you really want a stack data structure, don't use the data stack; use a vector and call push and pop on it.

See the following for details:

I have a word pushing a vector on the stack, and it's written as an empty vector, V{ }, in the source, but when the word runs the vector has random objects in it. What's going on?

Literals are a source of confusion for some beginners. Literals are pushed to the stack in the place they are used. A literal in Factor refers to one specific object in memory, and is not automatically cloned. If you modify a literal without cloning it, that modification will be global. For more information, see the help document about literals.

One stack is not enough. Is it possible to define new stacks?

If you find it hard to express your dataflow using the data stack alone, consider either refactoring your code, or using locals, before you start thinking about adding new stacks.

However, if you really need an additional stack (for example, you're writing a parser and need to track nested structure), you can make one easily enough, in one of several ways. The easiest way is to store the stack in a dynamic variable:

SYMBOL: my-stack

: >my-stack ( value -- ) my-stack get push ;
: my-stack> ( -- value ) my-stack get pop ;

: with-my-stack ( quot -- ) [ V{ } clone my-stack ] dip with-variable ; inline

Do stack shuffle words like dup copy my objects?

No. The stack is a stack of pointers, so dup and other words like it are very fast. You do not have to worry about dup slowing down your program if you are working with large arrays and other data structures.

My code is too slow. How do I make it faster?

First, make sure your algorithm is right. Once you've eliminated any time and space complexity issues, you can start looking at low-level optimization. See Optimization.

This revision created on Fri, 17 Apr 2009 04:25:39 by slava