type/satisfies
embed a predicate as a structured type
Calling Sequence
Parameters
Description
Examples
type(expr, 'satisfies'(P1, P2, ... ))
expr
-
Maple expression
P1, P2, ...
predicates
The type satisfies provides a mechanism for embedding arbitrary predicates in Maple's type system. It enables you to define a structured type corresponding to an arbitrary predicate without having to pollute the type namespace. This is helpful when used in conjunction with the few Maple facilities (such as the two-argument form of indets) that accept types, but not arbitrary predicates.
An expression expr is of type satisfies(P1, P2, ...) if all Pk(expr) evaluate to true, or to And, Or, Xor, Implies, Not constructions that, when converted to their corresponding boolean operators (and, or, xor, implies, not; see convert/boolean_operator), evaluate to true. Also all Pk(expr) are expected to evaluate to either true or false, or to And, Or, Xor, Implies, Not constructions with those meanings.
Type satisfies is always used as a structured type with at least one parameter. The function type(expr, 'satisfies'(P)) evaluates to true if the expression expr satisfies the predicate P. More than one predicate P1, P2, ... can be supplied, in which case the expression expr must satisfy all of the predicates P1, P2, ....
Type satisfies evaluates the Pk(expr) in the order supplied until an expression evaluates to false. Then the type(expr, 'satisfies'(P1, P2, ...) call returns false. If no predicate returns false, then the value true is returned.
Predicates are expected to return normally, without raising exceptions. You can use the ordered evaluation of the predicates to test that any preconditions for error-free evaluations are met. See Example 3.
Example 1
Use this type to filter conditions when computing objects of certain type:
All the functions
indets⁡F⁡G⁡x,y,z⁢H⁡x,function
F⁡G⁡x,y,z,G⁡x,y,H⁡x
Only the functions having y
indets⁡F⁡G⁡x,y,z⁢H⁡x,And⁡function,satisfies⁡f↦has⁡f,y
F⁡G⁡x,y,z,G⁡x,y
Only the functions having y and not having z
indets⁡F⁡G⁡x,y,z⁢H⁡x,And⁡function,satisfies⁡f↦has⁡f,yandnothas⁡f,z
G⁡x,y
Example 2
Use this mechanism to introduce local types within module or procedure bodies. Note that NumberTheory[IsSquareFree] returns true or false when applied to some arguments
m ≔ module_export⁡sqrfree;sqrfree ≔ 'satisfies'⁡x→type⁡x,'odd',NumberTheory['IsSquareFree']end module:
Observe that, unlike global type names, which should always be quoted to protect against unwanted evaluation, a local type defined in this way must not be quoted.
For example, the following example cannot have quotes, that is, 'm:−sqrfree'.
type⁡7,m:-sqrfree
true
The global space of names (including the global name sqrfree) remains distinct and so the definition of type m:-sqrfree does not imply the existence of a sqrfree type
type⁡7,sqrfree
Error, type `sqrfree` does not exist
Example 3
Giving predicates in certain order, they are evaluated in that order
map⁡type,0,1,2,And⁡integer,satisfies⁡s↦evalb⁡s≠0,s↦type⁡1s,integer
false,true,false
While constructing predicates (the procedures testing whether a condition is satisfied), the condition being tester may lead to an error, for example, because the test produces a division by zero.
map⁡type,0,1,2,And⁡integer,satisfies⁡s↦type⁡1s,integer
Error, (in unknown) numeric exception: division by zero
You can prevent these situations adding more predicates, taking care of the exceptional cases before the predicate which returns an error is considered
map⁡type,0,1,2,And⁡integer,satisfies⁡s↦is⁡s≠0,s↦type⁡1s,integer
You can use And, Or, Xor, Implies, Not constructions to make them more readable or to have full control of the evaluation rules when constructing your types - these constructions do not automatically evaluate to true or false.
condition≔f↦And⁡type⁡f,function,Not⁡hastype⁡fexp⁡1,function
condition≔f↦type⁡f,function∧¬hastype⁡fⅇ,function
type⁡exp⁡2,satisfies⁡condition
false
type⁡exp⁡1,satisfies⁡condition
See Also
convert/boolean_operator
evalb
export
has
indets
map
module
NumberTheory[IsSquareFree]
rcurry
sqrfree
type
type/structure
Download Help Document