Difference between revisions of "MetaVar..Do"
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Evaluation Functions]] | [[Category:Evaluation Functions]] | ||
− | + | ||
== MetaVar ''x'' := ''expr'' Do ''body'' == | == MetaVar ''x'' := ''expr'' Do ''body'' == | ||
Line 9: | Line 9: | ||
:<code>MetaVar root := isin of self;</code> | :<code>MetaVar root := isin of self;</code> | ||
− | :<code>While (isin of root<> null) Do root:= isin of root;</code> | + | :<code>While (isin of root <> null) Do root := isin of root;</code> |
:<code>root</code> | :<code>root</code> | ||
Line 15: | Line 15: | ||
== Interpretation of «x» within «body» == | == Interpretation of «x» within «body» == | ||
− | |||
Whenever the identifier for «x» appears within «body», it consistently represents the value held by «x». The important distinction between a local variable declared using [[MetaVar..Do]], compared to a local declared by [[Var..Do]] or [[LocalAlias..Do]] occurs when «x» is set to a [[handle]]. In this case, «x» represents the [[handle]], which is a type of atomic value, just like a number or text value is a type of atomic entity. This is contrasted to locals declaring via [[Var..Do]] or [[LocalAlias..Do]] which serve as an alias to the variable pointed to by the [[handle]]. Examples: | Whenever the identifier for «x» appears within «body», it consistently represents the value held by «x». The important distinction between a local variable declared using [[MetaVar..Do]], compared to a local declared by [[Var..Do]] or [[LocalAlias..Do]] occurs when «x» is set to a [[handle]]. In this case, «x» represents the [[handle]], which is a type of atomic value, just like a number or text value is a type of atomic entity. This is contrasted to locals declaring via [[Var..Do]] or [[LocalAlias..Do]] which serve as an alias to the variable pointed to by the [[handle]]. Examples: | ||
Line 30: | Line 29: | ||
If you assign a list value to the meta-variable, you cannot use the local identifier where an index is expected. So, for example, this would not be allowed: | If you assign a list value to the meta-variable, you cannot use the local identifier where an index is expected. So, for example, this would not be allowed: | ||
− | :<code>MetaVar x:=1..5 Do Sum(x^2, x) { not allowed }</code> | + | :<code>MetaVar x:= 1..5 Do Sum(x^2, x) { not allowed }</code> |
So, unlike [[Var..Do]], even though [[MetaVar..Do]] may create an implicit dimension, you can't refer to that dimension using the name of the local variable. If you intend to do that, you probably want to be using [[MetaIndex..Do]]. On the other hand, if you assign the meta-local a [[handle]] to an index, then you can use the identifier in an index context. | So, unlike [[Var..Do]], even though [[MetaVar..Do]] may create an implicit dimension, you can't refer to that dimension using the name of the local variable. If you intend to do that, you probably want to be using [[MetaIndex..Do]]. On the other hand, if you assign the meta-local a [[handle]] to an index, then you can use the identifier in an index context. | ||
Line 36: | Line 35: | ||
== Dimensionality Declaration == | == Dimensionality Declaration == | ||
Like [[Var..Do]], [[MetaVar..Do]] supports the same dimensionality declarations, i.e.: | Like [[Var..Do]], [[MetaVar..Do]] supports the same dimensionality declarations, i.e.: | ||
− | :[[MetaVar..Do|MetaVar]] «x»[«indexList»] := «expr» Do «body» | + | :[[MetaVar..Do|MetaVar]] «x»[«indexList»] := «expr» '''Do''' «body» |
To declare «x» to be atomic (zero-dimensional) use | To declare «x» to be atomic (zero-dimensional) use | ||
− | :[[MetaVar..Do|MetaVar]] «x»[] := «expr» Do «body» | + | :[[MetaVar..Do|MetaVar]] «x»[] := «expr» '''Do''' «body» |
or the equivalent shortcut: | or the equivalent shortcut: | ||
:[[Atomic..Do|Atomic]] «x» := «expr» Do «body» | :[[Atomic..Do|Atomic]] «x» := «expr» Do «body» | ||
Line 47: | Line 46: | ||
== See Also == | == See Also == | ||
+ | * [[Atomic..Do]] | ||
* [[LocalAlias..Do]] | * [[LocalAlias..Do]] | ||
+ | * [[Var..Do]] | ||
* [[MetaIndex..Do]] | * [[MetaIndex..Do]] | ||
− | * [[ | + | * [[Meta-Inference]] |
* [[Handle]] | * [[Handle]] | ||
− | |||
* [[Evaluate]] | * [[Evaluate]] |
Latest revision as of 20:41, 27 January 2016
MetaVar x := expr Do body
Declares a local variable with identifier «x», and assigns it an initial value obtained by evaluating «expr». The identifier «x» can then be referred to within the «body» expression. The expression «body» is said to be the lexical context of «x», since outside of the lexical context, the identifier «x» is not recognized. MetaVar..Do is identical to Var..Do when the value assigned to «x» contain no handles and is not a 1-D null-indexed list. When implementing Meta-Inference algorithms that reason about or manipulate Analytica objects, MetaVar..Do is usually preferred to Var..Do in that its treatment of handles is more consistent and easier to understand than that of Var..Do.
MetaVar..Do is often used in a procedural syntax, where the declaration is followed by a semi-colon and the Do keyword is omitted, such as:
MetaVar root := isin of self;
While (isin of root <> null) Do root := isin of root;
root
The identifier, in this case root, exists from the expression immediately following the semi-colon until the end of the immediate lexical context that MetaVar..Do appears in.
Interpretation of «x» within «body»
Whenever the identifier for «x» appears within «body», it consistently represents the value held by «x». The important distinction between a local variable declared using MetaVar..Do, compared to a local declared by Var..Do or LocalAlias..Do occurs when «x» is set to a handle. In this case, «x» represents the handle, which is a type of atomic value, just like a number or text value is a type of atomic entity. This is contrasted to locals declaring via Var..Do or LocalAlias..Do which serve as an alias to the variable pointed to by the handle. Examples:
Suppose
Variable Va1 := 3
Then:
MetaVar x:= Handle(Va1) Do [x] → [Va1]
MetaVar x:= Handle(Va1) Do [Evaluate(x)] → [3]
Var x:= Handle(Va1) Do [x] → [3]
LocalAlias x := Handle(Va1) Do [x] → [3]
To access the value of the variable pointed to by a MetaVar local, use Evaluate(«x»), as in the second example.
If you assign a list value to the meta-variable, you cannot use the local identifier where an index is expected. So, for example, this would not be allowed:
MetaVar x:= 1..5 Do Sum(x^2, x) { not allowed }
So, unlike Var..Do, even though MetaVar..Do may create an implicit dimension, you can't refer to that dimension using the name of the local variable. If you intend to do that, you probably want to be using MetaIndex..Do. On the other hand, if you assign the meta-local a handle to an index, then you can use the identifier in an index context.
Dimensionality Declaration
Like Var..Do, MetaVar..Do supports the same dimensionality declarations, i.e.:
- MetaVar «x»[«indexList»] := «expr» Do «body»
To declare «x» to be atomic (zero-dimensional) use
- MetaVar «x»[] := «expr» Do «body»
or the equivalent shortcut:
- Atomic «x» := «expr» Do «body»
History
As of Analytica 4.2, a parameter of a User-Defined Function declared as (x : Handle) behaves like a local variable declared via MetaVar..Do.
Enable comment auto-refresher