ForAllType

ForAllType[x,type]

represents a type parameterized by x.

ForAllType[x,cond,type]

represents a type satisfying cond.

ForAllType[{x1,x1,},cond,type]

represents a type with multiple parameters.

Details

  • ForAllType does not evaluate on its own but can be used inside functions that accept types including Typed, FunctionDeclaration, etc.
  • ForAllType is used to specify the type of polymorphic functions.
  • Possible forms for cond include:
  • Element[x,"absType"]assert that x is a member of an abstract type
    TypeEvaluate[]run an arbitrary function
    Element[x,{ty1,ty2,}]assert that x is one of the given types
  • cond can take the form Element[xi,"absType"], where "absType" is an abstract type. »
  • cond can take the form Element[xi,{ty1,ty2,}], where tyi are concrete types. »
  • TypeEvaluate can be used in cond. »
  • cond can be given as a list of conditions.

Examples

open allclose all

Basic Examples  (1)

Represent the type of a function that accepts a list of objects with type t and returns a single object with type t:

Represent a declaration of a polymorphic function with this type:

Compile a program using the polymorphic function:

Compile another program using the polymorphic function with a different concrete type:

Scope  (7)

Represent the type of a function that takes any packed array and returns an array of higher rank:

Declare a function with that type and call that function on a concrete example:

Represent the type of a function that is polymorphic in its output:

Compile a function with the type:

Represent the type of a function that only accepts integers:

Represent a function with the type:

Compile a program that calls the function on an integer type:

Compilation fails when the function is given a non-integer type:

Represent the type of a function that only accepts packed arrays with a rank less than 3:

Represent a function with the type:

Compile a program that applies the function to a packed array with rank 2:

Compilation fails when the inputted packed array has rank 3:

The constraint in a declaration can be used to distinguish between versions of a function.

Here are two declarations of the same function: one is for "Integers" and one is for "UnsignedIntegers":

The only constraint that matches is that for "Integers":

Here both constraints match and the narrowest declaration (that for "UnsignedMachineIntegers") is used:

CompilerInformation can be useful to see how types such as "Integer64" work with abstract types such as "Integers".

Constraints can also be given with a list of alternatives.

This declaration can apply to both "Integer64" and "Real64":

The declaration works for "Integer64":

The declaration also works for "Real64":

Constraints with a list of alternatives can use types with variables:

The declaration works for "Integer64":

It also works for a "PackedArray" type:

it is quite useful to give a declaration without having to go to the trouble of declaring an abstract type. These declarations can be helpful to give the shape of the types being used even if they are not completely known.

Wolfram Research (2022), ForAllType, Wolfram Language function, https://reference.wolfram.com/language/ref/ForAllType.html.

Text

Wolfram Research (2022), ForAllType, Wolfram Language function, https://reference.wolfram.com/language/ref/ForAllType.html.

CMS

Wolfram Language. 2022. "ForAllType." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ForAllType.html.

APA

Wolfram Language. (2022). ForAllType. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ForAllType.html

BibTeX

@misc{reference.wolfram_2024_foralltype, author="Wolfram Research", title="{ForAllType}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/ForAllType.html}", note=[Accessed: 10-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_foralltype, organization={Wolfram Research}, title={ForAllType}, year={2022}, url={https://reference.wolfram.com/language/ref/ForAllType.html}, note=[Accessed: 10-December-2024 ]}