6.6 Declared vs. Undeclared Functions¶
If you declare the type of a function, you can apply it to any data that can be converted to the source type of the function.
Define f with type {\sf Integer → Integer}.
f(x: Integer): Integer == x + 1
Function declaration f : Integer -> Integer has been added to
workspace.
Type: Void
The function f can be applied to integers, ...
f 9
Compiling function f with type Integer -> Integer
10 |
Type: PositiveInteger
and to values that convert to integers, ...
f(-2.0)
-1 |
Type: Integer
but not to values that cannot be converted to integers.
f(2/3)
Conversion failed in the compiled user function f .
Cannot convert from type Fraction Integer to Integer for value
2
-
3
To make the function over a wide range of types, do not declare its type. Give the same definition with no declaration.
g x == x + 1
Type: Void
If x+1 makes sense, you can apply g to x.
g 9
Compiling function g with type PositiveInteger -> PositiveInteger
10 |
Type: PositiveInteger
A version of g with different argument types get compiled for each new kind of argument used.
g(2/3)
Compiling function g with type Fraction Integer -> Fraction Integer
53 |
Type: Fraction Integer
Here x+1 for x=”axiom” makes no sense.
g("axiom")
There are 11 exposed and 5 unexposed library operations named +
having 2 argument(s) but none was determined to be applicable.
Use HyperDoc Browse, or issue
)display op +
to learn more about the available operations. Perhaps
package-calling the operation or using coercions on the arguments
will allow you to apply the operation.
Cannot find a definition or applicable library operation named +
with argument type(s)
String
PositiveInteger
Perhaps you should use "@" to indicate the required return type,
or "$" to specify which version of the function you need.
AXIOM will attempt to step through and interpret the code.
There are 11 exposed and 5 unexposed library operations named +
having 2 argument(s) but none was determined to be applicable.
Use HyperDoc Browse, or issue
)display op +
to learn more about the available operations. Perhaps
package-calling the operation or using coercions on the arguments
will allow you to apply the operation.
Cannot find a definition or applicable library operation named +
with argument type(s)
String
PositiveInteger
Perhaps you should use "@" to indicate the required return type,
or "$" to specify which version of the function you need.
As you will see in Chapter ugCategories FriCAS has a formal idea of categories for what makes sense.