type-checking function - Maple Help

type - type-checking function

 Calling Sequence type(e, t)

Parameters

 e - expression t - valid type expression

Description

 • In many contexts, it is not necessary to know the exact value of an expression; it suffices to know that an expression belongs to some broad class, or group, of expressions that share some common properties. These classes or groups are known as types. If T represents a type, then an expression is of type T if it belongs to the class that T represents. For example, an expression is said to be of type posint if it belongs to the class of expressions denoted by the type name posint, which is the set of positive integers.
 • Many Maple procedures use types to direct the flow of control in algorithms or to decide whether an expression is a valid input. The behavior of the non-commutative multiplication operator . depends on the type of its arguments.
 • Many of these types (classes) have names, such as integer, numeric, list, and radalgnum. Other types are described by forming more complicated type expressions, built up via a grammar (a set of rules for their valid combination) from more primitive type expressions. For more information, see type/structure. The class of expressions that represent valid types is itself described by a type called type. (That is, type(T, type) returns true only when $T$ is a valid type expression. In particular, type(type, type) returns true.)
 • Types can represent expressions that share a common underlying data structure, such as the types list and table, or can represent mathematical properties, such as type prime.
 • The type(e, t) calling sequence allows you to test whether a Maple expression is of a given type. The call type(e, t) returns the value true if the expression e is of type t, and returns the value false otherwise.
 Note: The type function does not check assumptions, it only considers the object itself. If you are using the assume facility, it is recommended that you use the is function.
 • Two arguments must be passed to type. The first argument can be any Maple expression. The second argument must be a valid type expression. A type expression may be as simple as a name that is known to type, but it may also be an arbitrarily complicated structured type (see type/structure). It is assumed that the first argument is NULL in a flattened expression when only one argument is received by a call to type.
 • An exception is raised only in the case that the second argument t is not a valid type.
 • Expressions of the form $e::t$ are also used to perform type tests in certain contexts. When evaluated in a Boolean context (such as the test expression in an if statement), the expression $e::t$ evaluates to true if $e$ is of type $t$, and evaluates to false otherwise. It is used, primarily, for automatically checking the types of arguments passed to procedures. For more information, see typematch.

Details

 • Types (or type expressions) can be categorized according to the way in which they are defined in Maple.
 The simplest types are the surface types. These are generally defined in the Maple kernel, and represent superficial or top-level properties of expressions. For example, type list is a surface type. Testing against a surface type is very fast; surface type tests can be completed in constant time, regardless of the size of the expression being tested. For more information, see type/surface.
 Arbitrarily complicated structured types can be formed by combining simpler, more primitive types. They allow you to classify expressions based on their deep structure. For example, the type expression $\mathrm{list}\left(\mathrm{posint}\right)$ represents the class of lists whose members are positive integers. Testing against this type requires not only that the top-level data type (list) of the expression be tested, but also that each list member be examined to determine whether it has the correct type (posint). Testing an expression against a structured type may require a full, recursive traversal of the expression, so the time required to perform a structured type test often increases with the size of the expression. For more information on structured type expressions, see type/structure.
 Defined types are named types that may perform arbitrary computation. A type named T is defined by assigning to the global name type/T a structured type expression or a procedure. There is no rule of thumb for the performance characteristics of defined types, since they may be very simple or arbitrarily complex. For more information on defining types, see type/definition.

Defined Types

 • The following type names, which include surface and structured types, are defined in Maple:

Notes:

 If a type name is an operator or keyword, it must be enclosed in left single quotes to prevent a syntax error.  See the examples below.
 Some types become active only after a specific package is loaded. For example, the type/const is active after the difforms package is loaded.
 • For extending the list above by embedding arbitrary predicates in the Maple type system, see type/satisfies. For more information on specific types, see type/datatype, where datatype is one of the names in the list above.
 • Arguments passed to the type command are evaluated normally as for other Maple commands. While it is rarely necessary to do so in interactive use, Maple programs that seek to be robust should enclose names that appear in type expressions with unevaluation quotes (') to prevent evaluation. For example, if the name T is assigned the value $2$ and is also used as a type name, then type(e, 'T') will work correctly, while type(e, T) will likely fail. (In this case, it would fail silently, because $2$ is a valid type expression.) See the examples below.
 • In Maple, type expressions are first class expressions. Types can be passed as arguments to, and returned from, procedures and can be stored in data structures. You can manipulate complicated type expressions just as you would any other Maple expression. Type expressions are, in fact, ordinary Maple expressions. It is only in certain contexts that these expressions are recognized as types. (Not all expressions are valid types, however.)
 Note: There is no $\mathrm{exprseq}$ type in Maple. However, the whattype function returns $\mathrm{exprseq}$ for objects that are expression sequences. (There is no Maple object $o$ for which type( o, exprseq ) returns true. In fact, it is not possible to construct a call to type with an expression sequence as its first argument, because expression sequences are flattened.)
 • You can make a data type known to the type function in the following way. If you have defined the procedure type/mytype, then a call to type of the form: type(a, mytype(b, c, ...)) evaluates the function call type/mytype(a, b, c,...).
 • A number of Maple commands other than type are designed to work with type expressions. See, for example, hastype, anames, and indets. Other commands, such as select, work well with the type command, though they are designed to work with more general predicates.

Examples

 > $\mathrm{type}\left(6.777800,'\mathrm{float}'\right)$
 ${\mathrm{true}}$ (1)
 > $\mathrm{type}\left(6.777800,'\mathrm{rational}'\right)$
 ${\mathrm{false}}$ (2)
 > $\mathrm{whattype}\left(\mathrm{solve}\left(3x+5=14,x\right)\right)$
 ${\mathrm{integer}}$ (3)
 > $\mathrm{type}\left(\mathrm{sin}\left(x\right),'\mathrm{trig}'\right)$
 ${\mathrm{true}}$ (4)
 > $\mathrm{type}\left(a+b,'\mathrm{polynom}'\right)$
 ${\mathrm{true}}$ (5)

For symbols and reserved names, use name quotes.

 > $\mathrm{type}\left(a+b,\mathrm{+}\right)$
 ${\mathrm{true}}$ (6)
 > $\mathrm{type}\left(ab,'\mathrm{+}'\right)$
 ${\mathrm{false}}$ (7)
 > $\mathrm{type}\left(a\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}b,'\mathrm{and}'\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{type}\left(\mathbf{module}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{export}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}e;\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module},'\mathrm{module}'\right)$
 ${\mathrm{true}}$ (9)
 > $a,b,5$
 ${a}{,}{b}{,}{5}$ (10)
 > $\mathrm{whattype}\left(\right)$
 ${\mathrm{exprseq}}$ (11)
 > $\mathrm{whattype}\left(\mathrm{NULL}\right)$
 ${\mathrm{exprseq}}$ (12)
 > $\mathrm{type}\left(\mathrm{NULL},\mathrm{exprseq}\right)$
 ${\mathrm{false}}$ (13)
 > $\mathrm{type}\left(\mathrm{exprseq},\mathrm{NULL}\right)$
 ${\mathrm{false}}$ (14)
 > $\mathrm{type}\left(\left[a,b,c\right],'\mathrm{list}'\right)$
 ${\mathrm{true}}$ (15)

False negative test.

 > $\mathrm{type/T}:='\mathrm{list}':$
 > $T:=2:$
 > $\mathrm{type}\left(\left[a,b,c\right],T\right)$
 ${\mathrm{false}}$ (16)

The next two commands are correct.

 > $\mathrm{type}\left(\left[a,b,c\right],'T'\right)$
 ${\mathrm{true}}$ (17)
 > $\mathrm{type}\left(2,'T'\right)$
 ${\mathrm{false}}$ (18)

False positive test.

 > $\mathrm{type}\left(2,T\right)$
 ${\mathrm{true}}$ (19)