factor/library/kernel.facts

368 lines
19 KiB
Plaintext
Raw Normal View History

2006-08-15 21:23:05 -04:00
IN: kernel
2006-08-17 23:08:04 -04:00
USING: generic help kernel math memory namespaces sequences kernel-internals ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: eq? ( obj1 obj2 -- ? )
2006-01-06 22:42:07 -05:00
{ $values { "obj1" "an object" } { "obj2" "an object" } }
{ $description "Tests if two references point at the same object." } ;
2006-08-15 21:23:05 -04:00
HELP: drop ( x -- ) $shuffle ;
HELP: 2drop ( x y -- ) $shuffle ;
HELP: 3drop ( x y z -- ) $shuffle ;
HELP: dup ( x -- x x ) $shuffle ;
HELP: 2dup ( x y -- x y x y ) $shuffle ;
HELP: 3dup ( x y z -- x y z x y z ) $shuffle ;
HELP: rot ( x y z -- y z x ) $shuffle ;
HELP: -rot ( x y z -- z x y ) $shuffle ;
HELP: dupd ( x y -- x x y ) $shuffle ;
HELP: swapd ( x y z -- y x z ) $shuffle ;
HELP: nip ( x y -- y ) $shuffle ;
HELP: 2nip ( x y z -- z ) $shuffle ;
HELP: tuck ( x y -- y x y ) $shuffle ;
HELP: over ( x y -- x y x ) $shuffle ;
HELP: pick ( x y z -- x y z x ) $shuffle ;
HELP: swap ( x y -- y x ) $shuffle ;
HELP: 2swap ( x y z t -- z t x y ) $shuffle ;
HELP: >r ( x -- )
{ $values { "x" "an object" } } { $description "Moves the top of the data stack to the retain stack." } ;
HELP: r> ( -- x )
{ $values { "x" "an object" } } { $description "Moves the top of the retain stack to the data stack." } ;
HELP: datastack ( -- ds )
2006-01-06 22:42:07 -05:00
{ $values { "ds" "a vector" } }
2006-06-04 16:20:40 -04:00
{ $description "Outputs the a vector containing a copy of the data stack contents right before the call to this word, with the top of the stack at the end of the vector." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: set-datastack ( ds -- )
2006-01-06 22:42:07 -05:00
{ $values { "ds" "a vector" } }
2006-06-04 16:20:40 -04:00
{ $description "Replaces the data stack contents with a copy of a vector. The end of the vector becomes the top of the stack." } ;
2006-08-15 21:23:05 -04:00
HELP: retainstack ( -- rs )
2006-06-04 16:20:40 -04:00
{ $values { "rs" "a vector" } }
{ $description "Outputs the a vector containing a copy of the retain stack contents right before the call to this word, with the top of the stack at the end of the vector." } ;
2006-08-15 21:23:05 -04:00
HELP: set-retainstack ( rs -- )
2006-06-04 16:20:40 -04:00
{ $values { "rs" "a vector" } }
{ $description "Replaces the retain stack contents with a copy of a vector. The end of the vector becomes the top of the stack." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: callstack ( -- cs )
2006-01-06 22:42:07 -05:00
{ $values { "cs" "a vector" } }
2006-06-04 16:20:40 -04:00
{ $description "Outputs the a vector containing a copy of the call stack contents right before the call to this word, with the top of the stack at the end of the vector. The call frame of the caller word is " { $emphasis "not" } " included." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: set-callstack ( cs -- )
2006-01-06 22:42:07 -05:00
{ $values { "cs" "a vector" } }
2006-06-04 16:20:40 -04:00
{ $description "Replaces the call stack contents with a copy of a vector. The end of the vector becomes the top of the stack. The current quotation continues executing. The new callstack takes effect when the current quotation returns, resulting in a callframe being popped." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: clear
2006-06-04 16:20:40 -04:00
{ $description "Clears the data stack." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: hashcode
2006-01-06 22:42:07 -05:00
{ $values { "obj" "an object" } { "n" "a fixnum" } }
{ $contract "Outputs the hashcode of the object. The hashcode operation must satisfy the following properties:"
{ $list
2006-06-04 15:35:00 -04:00
"the hashcode should be a fixnum, however returning a bignum will not cause any problems other than potential performance degradation."
2006-01-06 22:42:07 -05:00
{ "if two objects are equal under " { $link = } ", they must have equal hashcodes" }
"the hashcode is only permitted to change if the object is mutated in some way"
}
"If mutable objects are used as hashtable keys, they must not be mutated in such a way that their hashcode changes. Doing so will violate bucket sorting invariants and result in undefined behavior." } ;
2006-08-15 21:23:05 -04:00
HELP: =
2006-06-16 23:12:40 -04:00
{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } }
2006-08-17 23:08:04 -04:00
{ $description
"Tests if two objects are equal. If " { $snippet "obj1" } " and " { $snippet "obj2" } " point to the same object, outputs " { $link t } ". Otherwise, calls the " { $link equal? } " generic word."
} ;
HELP: equal?
{ $values { "obj1" "an object" } { "obj2" "an object" } { "?" "a boolean" } }
2006-01-06 22:42:07 -05:00
{ $contract
2006-08-17 23:08:04 -04:00
"Tests if two objects are equal."
2006-01-06 22:42:07 -05:00
$terpri
2006-08-17 23:08:04 -04:00
"Method definitions should ensure that this is an equality relation:"
2006-01-06 22:42:07 -05:00
{ $list
{ $snippet "a = a" }
{ { $snippet "a = b" } " implies " { $snippet "b = a" } }
{ { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
}
2006-08-17 23:08:04 -04:00
"While user code can define methods for this generic word, it should not call it directly, since it is slightly less efficient than " { $link = } " in the case the two references point to the same object."
}
{ $examples
"The most common reason for defining a method for this generic word to ensure that instances of a specific tuple class are only ever equal to themselves, overriding the default implementation which checks slot values for equality."
{ $code "TUPLE: foo ;" "M: foo equal? eq? ;" }
2006-01-06 22:42:07 -05:00
} ;
2006-08-15 21:23:05 -04:00
HELP: <=>
2006-01-09 01:34:23 -05:00
{ $values { "obj1" "an object" } { "obj2" "an object" } { "n" "an integer" } }
{ $contract
"Compares two objects using an intrinsic partial order, for example, the natural order for real numbers and lexicographic order for strings."
$terpri
"The output value is one of the following:"
{ $list
{ "positive - indicating that " { $snippet "str1" } " follows " { $snippet "str2" } }
{ "zero - indicating that " { $snippet "str1" } " is equal to " { $snippet "str2" } }
{ "negative - indicating that " { $snippet "str1" } " precedes " { $snippet "str2" } }
}
"The default implementation treats the two objects as sequences, and recursively compares their elements. So no extra work is required to compare sequences lexicographically."
}
{ $see-also natural-sort } ;
2006-08-15 21:23:05 -04:00
HELP: clone
2006-01-06 22:42:07 -05:00
{ $values { "obj" "an object" } { "cloned" "a new object" } }
2006-06-05 23:28:59 -04:00
{ $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: set-boot
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation" } }
{ $description "Sets the initial quotation called by the runtime as the last stage of startup. The image must be saved for changes to the boot quotation to take effect. Usually the boot quotation should not be changed." } ;
2006-08-15 21:23:05 -04:00
HELP: num-types
2006-01-06 22:42:07 -05:00
{ $values { "n" "a postiive integer" } }
{ $description "Outputs one more than the maximum value from the " { $link type } " primitive." } ;
2006-08-15 21:23:05 -04:00
DEFER: type ( object -- n )
HELP: type
{ $values { "object" "an object" } { "n" "a type number" } }
{ $description "Outputs an object's type number, between zero and one less than " { $link num-types } ". This is implementation detail and user code should call " { $link class } " instead." }
{ $see-also type>class tag } ;
2006-08-15 21:23:05 -04:00
HELP: ?
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "true" "an object" } { "false" "an object" } { "true/false" "one two input objects" } }
{ $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
2006-08-15 21:23:05 -04:00
HELP: >boolean
2006-01-06 22:42:07 -05:00
{ $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
{ $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
2006-08-15 21:23:05 -04:00
HELP: not ( obj -- ? )
2006-01-06 22:42:07 -05:00
{ $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
{ $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." } ;
2006-08-15 21:23:05 -04:00
HELP: and
2006-01-06 22:42:07 -05:00
{ $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
{ $description "Tests if neither object is " { $link f } "." } ;
2006-08-15 21:23:05 -04:00
HELP: or
2006-01-06 22:42:07 -05:00
{ $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
{ $description "Tests if at least one object is not " { $link f } "." } ;
2006-08-15 21:23:05 -04:00
HELP: xor
{ $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "obj" "a generalized boolean" } }
{ $description "Tests if at exactly one object is not " { $link f } "." } ;
HELP: cpu
2006-01-06 22:42:07 -05:00
{ $values { "cpu" "a string" } }
{ $description
"Outputs a string descriptor of the current CPU architecture. Currently, this set of descriptors is:"
{ $code "amd64" "ppc" "x86" }
} ;
2006-08-15 21:23:05 -04:00
HELP: os
2006-01-06 22:42:07 -05:00
{ $values { "os" "a string" } }
{ $description
"Outputs a string descriptor of the current operating system family. Currently, this set of descriptors is:"
2006-01-31 14:31:31 -05:00
{ $code "freebsd" "linux" "macosx" "solaris" "win32" "unix" }
2006-01-06 22:42:07 -05:00
} ;
2006-08-15 21:23:05 -04:00
HELP: windows?
2006-06-04 15:35:00 -04:00
{ $values { "?" "a boolean" } }
{ $description "Tests if Factor is running on Windows." } ;
2006-08-15 21:23:05 -04:00
HELP: macosx?
2006-06-04 15:35:00 -04:00
{ $values { "?" "a boolean" } }
{ $description "Tests if Factor is running on Mac OS X." } ;
2006-08-15 21:23:05 -04:00
HELP: call ( quot -- )
{ $values { "quot" "a quotation" } }
{ $description "Push the current callframe on the callstack, and set the callframe to the given quotation. Conceptually, calls the quotation, as if its definition was substituted at the location of the call." }
{ $examples
"The following two lines are equivalent:"
{ $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
} ;
2006-08-15 21:23:05 -04:00
HELP: slip
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation" } { "x" "an object" } }
{ $description "Calls a quotation while hiding the top of the stack." } ;
2006-08-15 21:23:05 -04:00
HELP: 2slip
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation" } { "x" "an object" } { "y" "an object" } }
{ $description "Calls a quotation while hiding the top two stack elements." } ;
2006-08-15 21:23:05 -04:00
HELP: keep
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation with stack effect " { $snippet "( x -- )" } } { "x" "an object" } }
{ $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." } ;
2006-08-15 21:23:05 -04:00
HELP: 2keep
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation with stack effect " { $snippet "( x y -- )" } } { "x" "an object" } { "y" "an object" } }
{ $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
2006-08-15 21:23:05 -04:00
HELP: 3keep
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation with stack effect " { $snippet "( x y -- )" } } { "x" "an object" } { "y" "an object" } { "z" "an object" } }
{ $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
2006-08-15 21:23:05 -04:00
HELP: 2apply
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation with stack effect " { $snippet "( obj -- )" } } { "x" "an object" } { "y" "an object" } }
{ $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." } ;
2006-08-15 21:23:05 -04:00
HELP: if ( cond true false -- )
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "true" "a quotation" } { "false" "a quotation" } }
{ $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
$terpri
"The " { $snippet "cond" } " value is removed from the stack before either quotation is called." } ;
2006-08-15 21:23:05 -04:00
HELP: when
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "true" "a quotation" } }
{ $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
$terpri
"The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
2006-08-15 21:23:05 -04:00
HELP: unless
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "false" "a quotation" } }
{ $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
$terpri
"The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
2006-08-15 21:23:05 -04:00
HELP: if*
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } { "false" "a quotation" } }
{ $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
$terpri
"If the condition is true, it is retained on the stack before the " { $snippet "true" } " quotation is called. Otherwise, the condition is removed from the stack and the " { $snippet "false" } " quotation is called."
$terpri
"The following two lines are equivalent:"
{ $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } } ;
2006-08-15 21:23:05 -04:00
HELP: when*
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } }
{ $description "Variant of " { $link if* } " with no false quotation."
$terpri
"The following two lines are equivalent:"
{ $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
2006-08-15 21:23:05 -04:00
HELP: unless*
2006-01-06 22:42:07 -05:00
{ $values { "cond" "a generalized boolean" } { "false" "a quotation " } }
{ $description "Variant of " { $link if* } " with no true quotation."
$terpri
"The following two lines are equivalent:"
{ $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
2006-08-15 21:23:05 -04:00
HELP: ?if
{ $values { "default" "an object" } { "cond" "a generalized boolean" } { "true" "a quotation with stack effect " { $snippet "( cond -- )" } } { "false" "a quotation with stack effect " { $snippet "( default -- )" } } }
2006-01-06 22:42:07 -05:00
{ $description "If the condition is " { $link f } ", the " { $snippet "false" } " quotation is called with the " { $snippet "default" } " value on the stack. Otherwise, the " { $snippet "true" } " quotation is called with the condition on the stack."
$terpri
"The following two lines are equivalent:"
{ $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" } } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: with
{ $values { "obj" "an object" } { "quot" "a quotation with stack effect " { $snippet "( obj elt -- )" } } { "elt" "an object" } }
{ $description "Utility word used to implement curried combinators such as " { $link each-with } " and " { $link map-with } "." } ;
HELP: keep-datastack
2006-01-06 22:42:07 -05:00
{ $values { "quot" "a quotation" } }
{ $description "Calls a quotation, saving the datastack before calling it and restoring it after it returns." } ;
2006-08-15 21:23:05 -04:00
HELP: die
{ $description "Starts the front-end processor (FEP), which is a low-level debugger which can inspect memory addresses and the like. The FEP is also entered when a critical error occurs." } ;
2006-06-04 15:35:00 -04:00
2006-08-15 21:23:05 -04:00
HELP: exit ( n -- )
{ $values { "n" "an integer exit code" } }
{ $description "Exits the Factor process." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: millis ( -- n )
{ $values { "n" "an integer" } }
{ $description "Outputs the number of milliseconds ellapsed since midnight January 1, 1970." } ;
2006-01-06 22:42:07 -05:00
2006-08-15 21:23:05 -04:00
HELP: os-env ( key -- value )
{ $values { "key" "a string" } { "value" "a string" } }
{ $description "Looks up the value of a shell environment variable." }
{ $examples
"This is an operating system-specific feature. On Unix, you can do:"
{ $example "\"USER\" os-env print" "slava" }
} ;
2006-03-27 23:03:25 -05:00
2006-08-15 21:23:05 -04:00
HELP: cell
2006-06-04 15:35:00 -04:00
{ $values { "n" "a positive integer" } }
{ $description "Outputs the pointer size in bytes of the current CPU architecture." } ;
2006-08-15 21:23:05 -04:00
HELP: win32?
2006-06-04 15:35:00 -04:00
{ $values { "?" "a boolean" } }
{ $description "Tests if Factor is running on 32-bit Windows." } ;
2006-08-15 21:23:05 -04:00
HELP: win64?
2006-06-04 15:35:00 -04:00
{ $values { "?" "a boolean" } }
{ $description "Tests if Factor is running on 64-bit Windows." } ;
2006-08-15 21:23:05 -04:00
IN: memory
HELP: generations
2006-03-27 23:03:25 -05:00
{ $values { "n" "a positive integer" } }
{ $description "Outputs the number of generations partitioning the heap." } ;
2006-08-15 21:23:05 -04:00
HELP: image
2006-03-27 23:03:25 -05:00
{ $values { "path" "a path name string" } }
{ $description "Outputs the path name of the currently running Factor image." } ;
2006-08-15 21:23:05 -04:00
HELP: save-image ( path -- )
2006-03-27 23:03:25 -05:00
{ $values { "path" "a path name string" } }
{ $description "Saves a snapshot of the heap to the given file, overwriting the file if it already exists." } ;
2006-08-15 21:23:05 -04:00
HELP: save
2006-03-27 23:03:25 -05:00
{ $description "Saves a snapshot of the heap to the current image file." } ;
2006-08-15 21:23:05 -04:00
IN: kernel-internals
2006-08-17 23:08:04 -04:00
HELP: (clone) ( obj -- newobj )
{ $values { "obj" "an object" } { "newobj" "a shallow copy" } }
{ $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
2006-08-15 21:23:05 -04:00
HELP: declare
{ $values { "spec" "an array of class words" } }
{ $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
{ $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
{ $examples
"The optimizer cannot do anything with the below quotation:"
{ $example "[ 2 + 10 * ] f dataflow." "[ 2 + 10 * ]" }
"However, if we declare that the top of the stack is a " { $link float } ", then type checks and generic dispatch are eliminated, and the compiler can use unsafe intrinsics:"
{ $example "[ { float } declare 2 + 10 * ] f dataflow." "[ 2.0 float+ 10.0 float* ]" }
} ;
HELP: array-capacity
{ $values { "array" "an array" } { "n" "a non-negative fixnum" } }
{ $description "Low-level array length accessor." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types, so improper use can corrupt memory." } ;
2006-08-15 21:23:05 -04:00
HELP: array-nth
{ $values { "n" "a non-negative fixnum" } { "array" "an array" } { "elt" "an object" } }
{ $description "Low-level array element accessor." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory." } ;
HELP: set-array-nth
{ $values { "elt" "an object" } { "n" "a non-negative fixnum" } { "array" "an array" } }
{ $description "Low-level array element mutator." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is unsafe. It does not check types or array bounds, and improper use can corrupt memory." } ;
HELP: tag ( object -- n )
{ $values { "object" "an object" } { "n" "a tag number" } }
{ $description "Outputs an object's tag number, between zero and one less than " { $link num-tags } ". This is implementation detail and user code should call " { $link class } " instead." }
{ $see-also type } ;
HELP: dispatch ( n array -- )
{ $values { "n" "a fixnum" } { "array" "an array of quotations" } }
{ $description "Calls the " { $snippet "n" } "th quotation in the array." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it is an implementation detail used by the generic word system to accelerate method dispatch. It does not perform type or bounds checks, and user code should not need to call it directly." } ;
HELP: getenv ( n -- obj )
{ $values { "n" "a non-negative integer" } { "obj" "an object" } }
{ $description "Reads an object from the Factor runtime's environment table. User code never has to read the environment table directly; instead, use one of the callers of this word." } ;
2006-08-15 21:23:05 -04:00
HELP: setenv ( obj n -- )
{ $values { "n" "a non-negative integer" } { "obj" "an object" } }
{ $description "Writes an object to the Factor runtime's environment table. User code never has to write to the environment table directly; instead, use one of the callers of this word." } ;
2006-08-15 21:23:05 -04:00
HELP: slot ( obj m -- value )
{ $values { "obj" "an object" } { "m" "a non-negative fixnum" } { "value" "an object" } }
{ $description "Reads the object stored at the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;
2006-08-15 21:23:05 -04:00
HELP: set-slot ( value obj n -- )
{ $values { "value" "an object" } { "obj" "an object" } { "m" "a non-negative fixnum" } }
{ $description "Writes " { $snippet "value" } " to the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
{ $warning "This word is in the " { $vocab-link "kernel-internals" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;