calc: Declaration Basics
7.6.1 Declaration Basics
------------------------
The ‘s d’ (‘calc-declare-variable’) command is the easiest way to make a
declaration for a variable. This command prompts for the variable name,
then prompts for the declaration. The default at the declaration prompt
is the previous declaration, if any. You can edit this declaration, or
press ‘C-k’ to erase it and type a new declaration. (Or, erase it and
press <RET> to clear the declaration, effectively “undeclaring” the
variable.)
A declaration is in general a vector of “type symbols” and “range”
values. If there is only one type symbol or range value, you can write
it directly rather than enclosing it in a vector. For example, ‘s d foo
<RET> real <RET>’ declares ‘foo’ to be a real number, and ‘s d bar <RET>
[int, const, [1..6]] <RET>’ declares ‘bar’ to be a constant integer
between 1 and 6. (Actually, you can omit the outermost brackets and
Calc will provide them for you: ‘s d bar <RET> int, const, [1..6]
<RET>’.)
Declarations in Calc are kept in a special variable called ‘Decls’.
This variable encodes the set of all outstanding declarations in the
form of a matrix. Each row has two elements: A variable or vector of
variables declared by that row, and the declaration specifier as
described above. You can use the ‘s D’ command to edit this variable if
you wish to see all the declarations at once. Operations on
Variables, for a description of this command and the ‘s p’ command
that allows you to save your declarations permanently if you wish.
Items being declared can also be function calls. The arguments in
the call are ignored; the effect is to say that this function returns
values of the declared type for any valid arguments. The ‘s d’ command
declares only variables, so if you wish to make a function declaration
you will have to edit the ‘Decls’ matrix yourself.
For example, the declaration matrix
[ [ foo, real ]
[ [j, k, n], int ]
[ f(1,2,3), [0 .. inf) ] ]
declares that ‘foo’ represents a real number, ‘j’, ‘k’ and ‘n’ represent
integers, and the function ‘f’ always returns a real number in the
interval shown.
If there is a declaration for the variable ‘All’, then that
declaration applies to all variables that are not otherwise declared.
It does not apply to function names. For example, using the row ‘[All,
real]’ says that all your variables are real unless they are explicitly
declared without ‘real’ in some other row. The ‘s d’ command declares
‘All’ if you give a blank response to the variable-name prompt.