touch up locals documentation, unify terminology, and put a link to "limitations of locals" in MACRO:: docs
parent
2f90b13160
commit
91d12ddc22
|
@ -9,65 +9,66 @@ HELP: [|
|
|||
|
||||
HELP: [let
|
||||
{ $syntax "[let code :> var code :> var code... ]" }
|
||||
{ $description "Establishes a new lexical scope for local variable bindings. Variables bound with " { $link POSTPONE: :> } " within the body of the " { $snippet "[let" } " will be lexically scoped to the body of the " { $snippet "[let" } " form." }
|
||||
{ $description "Establishes a new scope for lexical variable bindings. Variables bound with " { $link POSTPONE: :> } " within the body of the " { $snippet "[let" } " will be lexically scoped to the body of the " { $snippet "[let" } " form." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
HELP: :>
|
||||
{ $syntax ":> var" ":> var!" ":> ( var-1 var-2 ... )" }
|
||||
{ $description "Binds one or more new local variables. In the " { $snippet ":> var" } " form, the value on the top of the datastack to a new local variable named " { $snippet "var" } ", lexically scoped to the enclosing quotation, " { $link POSTPONE: [let } " form, or " { $link POSTPONE: :: } " definition."
|
||||
{ $description "Binds one or more new lexical variables. In the " { $snippet ":> var" } " form, the value on the top of the datastack to a new lexical variable named " { $snippet "var" } " and scoped to the enclosing quotation, " { $link POSTPONE: [let } " form, or " { $link POSTPONE: :: } " definition."
|
||||
$nl
|
||||
"The " { $snippet ":> ( var-1 ... )" } " form binds multiple local variables from the top of the datastack in left to right order. These two snippets would have the same effect:"
|
||||
"The " { $snippet ":> ( var-1 ... )" } " form binds multiple variables to the top values off the datastack in left to right order. These two snippets have the same effect:"
|
||||
{ $code ":> c :> b :> a" }
|
||||
{ $code ":> ( a b c )" }
|
||||
$nl
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), that new variable will be mutable. See " { $link "locals-mutable" } " for more information on mutable local bindings." }
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), that new variable is mutable. See " { $link "locals-mutable" } " for more information on mutable lexical variables." }
|
||||
{ $notes
|
||||
"This syntax can only be used inside a lexical scope established by a " { $link POSTPONE: :: } " definition, " { $link POSTPONE: [let } " form, or " { $link POSTPONE: [| } " quotation. Definition forms such as " { $link POSTPONE: : } " do not establish a lexical scope by themselves, nor is there a lexical scope available at the top level of source files or in the listener. To use local variable bindings in these situations, use " { $link POSTPONE: [let } " to provide a scope for them." }
|
||||
"This syntax can only be used inside a lexical scope established by a " { $link POSTPONE: :: } " definition, " { $link POSTPONE: [let } " form, or " { $link POSTPONE: [| } " quotation. Normal quotations have their own lexical scope only if they are inside an outer scope. Definition forms such as " { $link POSTPONE: : } " do not establish a lexical scope by themselves unless documented otherwise, nor is there a lexical scope available at the top level of source files or in the listener. " { $link POSTPONE: [let } " can be used to create a lexical scope where one is not otherwise available." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
{ POSTPONE: [let POSTPONE: :> } related-words
|
||||
|
||||
HELP: ::
|
||||
{ $syntax ":: word ( vars... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a word with named inputs; it reads datastack values into local variable bindings from left to right, then executes the body with those bindings in lexical scope."
|
||||
{ $description "Defines a word with named inputs. The word binds its input values to lexical variables from left to right, then executes the body with those bindings in scope."
|
||||
$nl
|
||||
"If any of the " { $snippet "vars" } "' names is followed by an exclamation point (" { $snippet "!" } "), that variable will be mutable. See " { $link "locals-mutable" } " for more information on mutable local bindings." }
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), the corresponding new variable is made mutable. See " { $link "locals-mutable" } " for more information on mutable lexical variables." }
|
||||
{ $notes "The names of the " { $snippet "outputs" } " do not affect the word's behavior. However, the compiler verifies that the stack effect accurately represents the number of outputs as with " { $link POSTPONE: : } " definitions." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
{ POSTPONE: : POSTPONE: :: } related-words
|
||||
|
||||
HELP: MACRO::
|
||||
{ $syntax "MACRO:: word ( bindings... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a macro with named inputs; it reads datastack values into local variable bindings from left to right, then executes the body with those bindings in lexical scope."
|
||||
{ $syntax "MACRO:: word ( vars... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a macro with named inputs. The macro binds its input variables to lexical variables from left to right, then executes the body with those bindings in scope."
|
||||
$nl
|
||||
"If any of the " { $snippet "vars" } "' names is followed by an exclamation point (" { $snippet "!" } "), that variable will be mutable. See " { $link "locals-mutable" } " for more information on mutable local bindings." }
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), the corresponding new variable is made mutable. See " { $link "locals-mutable" } " for more information on mutable lexical variables." }
|
||||
{ $notes "The expansion of a macro cannot reference lexical variables bound in the outer scope. There are also limitations on passing arguments involving lexical variables into macros. See " { $link "locals-limitations" } " for details." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
{ POSTPONE: MACRO: POSTPONE: MACRO:: } related-words
|
||||
|
||||
HELP: MEMO::
|
||||
{ $syntax "MEMO:: word ( bindings... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a memoized word with named inputs; it reads datastack values into local variable bindings from left to right, then executes the body with those bindings in lexical scope."
|
||||
{ $syntax "MEMO:: word ( vars... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a memoized word with named inputs. The word binds its input values to lexical variables from left to right, then executes the body with those bindings in scope."
|
||||
$nl
|
||||
"If any of the " { $snippet "vars" } "' names is followed by an exclamation point (" { $snippet "!" } "), that variable will be mutable. See " { $link "locals-mutable" } " for more information on mutable local bindings." }
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), the corresponding new variable is made mutable. See " { $link "locals-mutable" } " for more information on mutable lexical variables." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
{ POSTPONE: MEMO: POSTPONE: MEMO:: } related-words
|
||||
|
||||
HELP: M::
|
||||
{ $syntax "M:: class generic ( bindings... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a new method on " { $snippet "generic" } " for " { $snippet "class" } " with named inputs; it reads datastack values into local variable bindings from left to right, then executes the body with those bindings in lexical scope."
|
||||
{ $syntax "M:: class generic ( vars... -- outputs... ) body... ;" }
|
||||
{ $description "Defines a new method on " { $snippet "generic" } " for " { $snippet "class" } " with named inputs. The method binds its input values to lexical variables from left to right, then executes the body with those bindings in scope."
|
||||
$nl
|
||||
"If any of the " { $snippet "vars" } "' names is followed by an exclamation point (" { $snippet "!" } "), that variable will be mutable. See " { $link "locals-mutable" } " for more information on mutable local bindings." }
|
||||
"If any " { $snippet "var" } " name is followed by an exclamation point (" { $snippet "!" } "), the corresponding new variable is made mutable. See " { $link "locals-mutable" } " for more information on mutable lexical variables." }
|
||||
{ $notes "The names of the " { $snippet "outputs" } " do not affect the word's behavior. However, the compiler verifies that the stack effect accurately represents the number of outputs as with " { $link POSTPONE: M: } " definitions." }
|
||||
{ $examples "See " { $link "locals-examples" } "." } ;
|
||||
|
||||
{ POSTPONE: M: POSTPONE: M:: } related-words
|
||||
|
||||
ARTICLE: "locals-examples" "Examples of locals"
|
||||
{ $heading "Definitions with locals" }
|
||||
"The following example demonstrates local variable bindings in word definitions. The " { $snippet "quadratic-roots" } " word is defined with " { $link POSTPONE: :: } ", so it takes its inputs from the top three elements of the datastack and binds them to the variables " { $snippet "a" } ", " { $snippet "b" } ", and " { $snippet "c" } ". In the body, the " { $snippet "disc" } " variable is bound using " { $link POSTPONE: :> } " and then used in the following line of code."
|
||||
ARTICLE: "locals-examples" "Examples of lexical variables"
|
||||
{ $heading "Definitions with lexical variables" }
|
||||
"The following example demonstrates lexical variable bindings in word definitions. The " { $snippet "quadratic-roots" } " word is defined with " { $link POSTPONE: :: } ", so it takes its inputs from the top three elements of the datastack and binds them to the variables " { $snippet "a" } ", " { $snippet "b" } ", and " { $snippet "c" } ". In the body, the " { $snippet "disc" } " variable is bound using " { $link POSTPONE: :> } " and then used in the following line of code."
|
||||
{ $example """USING: locals math math.functions kernel ;
|
||||
IN: scratchpad
|
||||
:: quadratic-roots ( a b c -- x y )
|
||||
|
@ -77,7 +78,7 @@ IN: scratchpad
|
|||
"""2.0
|
||||
-3.0"""
|
||||
}
|
||||
"If you wanted to perform the quadratic formula interactively from the listener, you could use " { $link POSTPONE: [let } " to provide a scope for the local variables:"
|
||||
"If you wanted to perform the quadratic formula interactively from the listener, you could use " { $link POSTPONE: [let } " to provide a scope for the variables:"
|
||||
{ $example """USING: locals math math.functions kernel ;
|
||||
IN: scratchpad
|
||||
[let 1.0 :> a 1.0 :> b -6.0 :> c
|
||||
|
@ -90,8 +91,8 @@ IN: scratchpad
|
|||
|
||||
$nl
|
||||
|
||||
{ $heading "Quotations with locals, and closures" }
|
||||
"These next two examples demonstrate local variable bindings in quotations defined with " { $link POSTPONE: [| } ". In this example, the values " { $snippet "5" } " and " { $snippet "3" } " are put on the datastack. When the quotation is called, those values are bound to " { $snippet "m" } " and " { $snippet "n" } " respectively in the lexical scope of the quotation:"
|
||||
{ $heading "Quotations with lexical variables, and closures" }
|
||||
"These next two examples demonstrate lexical variable bindings in quotations defined with " { $link POSTPONE: [| } ". In this example, the values " { $snippet "5" } " and " { $snippet "3" } " are put on the datastack. When the quotation is called, it takes those values as inputs and binds them respectively to " { $snippet "m" } " and " { $snippet "n" } " before executing the quotation:"
|
||||
{ $example
|
||||
"USING: kernel locals math prettyprint ;"
|
||||
"IN: scratchpad"
|
||||
|
@ -100,7 +101,7 @@ $nl
|
|||
}
|
||||
$nl
|
||||
|
||||
"In this example, the " { $snippet "adder" } " word creates a quotation that closes over its argument " { $snippet "n" } ". When called, the result of " { $snippet "5 adder" } " pulls " { $snippet "3" } " off the datastack and binds it to " { $snippet "m" } ":"
|
||||
"In this example, the " { $snippet "adder" } " word creates a quotation that closes over its argument " { $snippet "n" } ". When called, the result quotation of " { $snippet "5 adder" } " pulls " { $snippet "3" } " off the datastack and binds it to " { $snippet "m" } ", which is added to the value " { $snippet "5" } " bound to " { $snippet "n" } " in the outer scope of " { $snippet "adder" } ":"
|
||||
{ $example
|
||||
"USING: kernel locals math prettyprint ;"
|
||||
"IN: scratchpad"
|
||||
|
@ -150,8 +151,8 @@ mutable-example [ call . ] bi@"""
|
|||
6"""
|
||||
}
|
||||
"In " { $snippet "rebinding-example" } ", the binding of " { $snippet "a" } " to " { $snippet "5" } " is closed over in the first quotation, and the binding of " { $snippet "a" } " to " { $snippet "6" } " is closed over in the second, so calling both quotations results in " { $snippet "5" } " and " { $snippet "6" } " respectively. By contrast, in " { $snippet "mutable-example" } ", both quotations close over a single binding of " { $snippet "a" } ". Even though " { $snippet "a" } " is assigned to " { $snippet "6" } " after the first quotation is made, calling either quotation will output the new value of " { $snippet "a" } "."
|
||||
{ $heading "Locals in literals" }
|
||||
"Some kinds of literals can include references to local variables as described in " { $link "locals-literals" } ". For example, the " { $link 3array } " word could be implemented as follows:"
|
||||
{ $heading "Lexical variables in literals" }
|
||||
"Some kinds of literals can include references to lexical variables as described in " { $link "locals-literals" } ". For example, the " { $link 3array } " word could be implemented as follows:"
|
||||
{ $example
|
||||
"""USING: locals prettyprint ;
|
||||
IN: scratchpad
|
||||
|
@ -161,8 +162,8 @@ IN: scratchpad
|
|||
"""{ 1 "two" 3.0 }"""
|
||||
} ;
|
||||
|
||||
ARTICLE: "locals-literals" "Locals in literals"
|
||||
"Certain data type literals are permitted to contain local variables. Any such literals are rewritten into code which constructs an instance of the type with the values of the variables spliced in. Conceptually, this is similar to the transformation applied to quotations containing free variables."
|
||||
ARTICLE: "locals-literals" "Lexical variables in literals"
|
||||
"Certain data type literals are permitted to contain lexical variables. Any such literals are rewritten into code which constructs an instance of the type with the values of the variables spliced in. Conceptually, this is similar to the transformation applied to quotations containing free variables."
|
||||
$nl
|
||||
"The data types which receive this special handling are the following:"
|
||||
{ $list
|
||||
|
@ -182,7 +183,7 @@ $nl
|
|||
"ordinary-word-test ordinary-word-test eq? ."
|
||||
"t"
|
||||
}
|
||||
"In a word with locals, literals which do not contain locals still behave in the same way:"
|
||||
"Inside a lexical scope, literals which do not contain lexical variables still behave in the same way:"
|
||||
{ $example
|
||||
"USE: locals"
|
||||
"IN: scratchpad"
|
||||
|
@ -192,7 +193,7 @@ $nl
|
|||
"locals-word-test locals-word-test eq? ."
|
||||
"t"
|
||||
}
|
||||
"However, literals with locals in them actually expand into code for constructing a new object:"
|
||||
"However, literals with lexical variables in them actually construct a new object:"
|
||||
{ $example
|
||||
"USING: locals splitting ;"
|
||||
"IN: scratchpad"
|
||||
|
@ -203,17 +204,17 @@ $nl
|
|||
"constructor-test constructor-test eq? ."
|
||||
"f"
|
||||
}
|
||||
"One exception to the above rule is that array instances containing free local variables (that is, immutable local variables not referenced in a closure) do retain identity. This allows macros such as " { $link cond } " to recognize that the array is constant and expand at compile time." ;
|
||||
"One exception to the above rule is that array instances containing free lexical variables (that is, immutable lexical variables not referenced in a closure) do retain identity. This allows macros such as " { $link cond } " to expand at compile time even when their arguments reference variables." ;
|
||||
|
||||
ARTICLE: "locals-mutable" "Mutable locals"
|
||||
"Whenever a local variable is bound using " { $link POSTPONE: :> } ", " { $link POSTPONE: :: } ", or " { $link POSTPONE: [| } ", the variable may be made mutable by suffixing its name with an exclamation point (" { $snippet "!" } ") when it is bound. The variable's value can be read by giving its name without the exclamation point as usual. To write to the variable, use its name with the " { $snippet "!" } " suffix."
|
||||
ARTICLE: "locals-mutable" "Mutable lexical variables"
|
||||
"When a lexical variable is bound using " { $link POSTPONE: :> } ", " { $link POSTPONE: :: } ", or " { $link POSTPONE: [| } ", the variable may be made mutable by suffixing its name with an exclamation point (" { $snippet "!" } "). A mutable variable's value is read by giving its name without the exclamation point as usual. To write to the variable, use its name with the " { $snippet "!" } " suffix."
|
||||
$nl
|
||||
"Mutable bindings are implemented in a manner similar to the ML language; each mutable binding is actually an immutable binding of a mutable cell (in Factor's case, a 1-element array); reading the binding automatically dereferences the array, and writing to the binding stores into the array."
|
||||
"Mutable bindings are implemented in a manner similar to the ML language; each mutable binding is actually an immutable binding of a mutable cell. Reading the binding automatically unboxes the value from the cell, and writing to the binding stores into it."
|
||||
$nl
|
||||
"Writing to mutable locals in outer scopes is fully supported and has full closure semantics. See " { $link "locals-examples" } " for examples of mutable local variables in action." ;
|
||||
"Writing to mutable variables from outer lexical scopes is fully supported and has full closure semantics. See " { $link "locals-examples" } " for examples of mutable lexical variables in action." ;
|
||||
|
||||
ARTICLE: "locals-fry" "Locals and fry"
|
||||
"Locals integrate with " { $link "fry" } " so that mixing locals with fried quotations gives intuitive results."
|
||||
ARTICLE: "locals-fry" "Lexical variables and fry"
|
||||
"Lexical variables integrate with " { $link "fry" } " so that mixing variables with fried quotations gives intuitive results."
|
||||
$nl
|
||||
"The following two code snippets are equivalent:"
|
||||
{ $code "'[ sq _ + ]" }
|
||||
|
@ -226,7 +227,7 @@ $nl
|
|||
"When quotations take named parameters using " { $link POSTPONE: [| } ", " { $link curry } " fills in the variable bindings from right to left. The following two snippets are equivalent:"
|
||||
{ $code "3 [| a b | a b - ] curry" }
|
||||
{ $code "[| a | a 3 - ]" }
|
||||
"Because of this, the behavior of " { $snippet "fry" } " changes when applied to such a quotation to ensure that fry conceptually behaves the same as with normal quotations, placing the fried values “underneath” the local variable bindings. Thus, the following snippets are no longer equivalent:"
|
||||
"Because of this, the behavior of " { $snippet "fry" } " changes when applied to such a quotation to ensure that fry conceptually behaves the same as with normal quotations, placing the fried values “underneath” the variable bindings. Thus, the following snippets are no longer equivalent:"
|
||||
{ $code "'[ [| a | _ a - ] ]" }
|
||||
{ $code "'[ [| a | a - ] curry ] call" }
|
||||
"Instead, the first line above expands into something like the following:"
|
||||
|
@ -234,15 +235,15 @@ $nl
|
|||
$nl
|
||||
"The precise behavior is as follows. When frying a " { $link POSTPONE: [| } " quotation, a stack shuffle (" { $link mnswap } ") is prepended so that the " { $snippet "m" } " curried values, which start off at the top of the stack, are transposed with the quotation's " { $snippet "n" } " named input bindings." ;
|
||||
|
||||
ARTICLE: "locals-limitations" "Limitations of locals"
|
||||
"There are two main limitations of the current locals implementation, and both concern macros."
|
||||
ARTICLE: "locals-limitations" "Limitations of lexical variables"
|
||||
"There are two main limitations of the current implementation, and both concern macros."
|
||||
{ $heading "Macro expansions with free variables" }
|
||||
"The expansion of a macro cannot reference local variables bound in the outer scope. For example, the following macro is invalid:"
|
||||
"The expansion of a macro cannot reference lexical variables bound in the outer scope. For example, the following macro is invalid:"
|
||||
{ $code "MACRO:: twice ( quot -- ) [ quot call quot call ] ;" }
|
||||
"The following is fine, though:"
|
||||
{ $code "MACRO:: twice ( quot -- ) quot quot '[ @ @ ] ;" }
|
||||
{ $heading "Static stack effect inference and macros" }
|
||||
"A macro will only expand at compile-time if all inputs to the macro are literal. Likewise, the word containing the macro will only get a static stack effect and compile successfully if the macro's inputs are literal. When locals are used in a macro's literal arguments, there is an additional restriction: The literals must immediately precede the macro call lexically."
|
||||
"A macro will only expand at compile-time if all of its inputs are literal. Likewise, the word containing the macro will only have a static stack effect and compile successfully if the macro's inputs are literal. When lexical variables are used in a macro's literal arguments, there is an additional restriction: The literals must immediately precede the macro call lexically."
|
||||
$nl
|
||||
"For example, all of the following three code snippets are superficially equivalent, but only the first will compile:"
|
||||
{ $code
|
||||
|
@ -253,7 +254,7 @@ $nl
|
|||
" { [ a 0 = ] [ ... ] }"
|
||||
" } cond ;"
|
||||
}
|
||||
"The next two snippets will not compile, because the argument to " { $link cond } " does not immediately precede the call to " { $link cond } ":"
|
||||
"The next two snippets will not compile because the argument to " { $link cond } " does not immediately precede the call:"
|
||||
{ $code
|
||||
": my-cond ( alist -- ) cond ; inline"
|
||||
""
|
||||
|
@ -272,14 +273,14 @@ $nl
|
|||
" { [ a 0 = ] [ ... ] }"
|
||||
" } swap swap cond ;"
|
||||
}
|
||||
"The reason is that locals are rewritten into stack code at parse time, whereas macro expansion is performed later during compile time. To circumvent this problem, the " { $vocab-link "macros.expander" } " vocabulary is used to rewrite simple macro usages prior to local transformation. However, " { $vocab-link "macros.expander" } " cannot deal with more complicated cases where the literal inputs to the macro do not immediately precede the macro call in the source." ;
|
||||
"The reason is that lexical variable references are rewritten into stack code at parse time, whereas macro expansion is performed later during compile time. To circumvent this problem, the " { $vocab-link "macros.expander" } " vocabulary is used to rewrite simple macro usages prior to lexical variable transformation. However, " { $vocab-link "macros.expander" } " cannot deal with more complicated cases where the literal inputs to the macro do not immediately precede the macro call in the source." ;
|
||||
|
||||
ARTICLE: "locals" "Lexical variables"
|
||||
"The " { $vocab-link "locals" } " vocabulary provides lexically scoped local variables. Full closure semantics, both downward and upward, are supported. Mutable variable bindings are also provided, supporting assignment to bindings in the current scope or in outer scopes."
|
||||
{ $subsections
|
||||
"locals-examples"
|
||||
}
|
||||
"Word definitions where the inputs are bound to named local variables:"
|
||||
"Word definitions where the inputs are bound to lexical variables:"
|
||||
{ $subsections
|
||||
POSTPONE: ::
|
||||
POSTPONE: M::
|
||||
|
@ -291,7 +292,7 @@ ARTICLE: "locals" "Lexical variables"
|
|||
POSTPONE: [let
|
||||
POSTPONE: :>
|
||||
}
|
||||
"Quotation literals where the inputs are named local variables:"
|
||||
"Quotation literals where the inputs are bound to lexical variables:"
|
||||
{ $subsections POSTPONE: [| }
|
||||
"Additional topics:"
|
||||
{ $subsections
|
||||
|
@ -300,6 +301,6 @@ ARTICLE: "locals" "Lexical variables"
|
|||
"locals-fry"
|
||||
"locals-limitations"
|
||||
}
|
||||
"Local variables complement " { $link "namespaces" } "." ;
|
||||
"Lexical variables complement " { $link "namespaces" } "." ;
|
||||
|
||||
ABOUT: "locals"
|
||||
|
|
Loading…
Reference in New Issue