typematch - Maple Programming Help

Home : Support : Online Help : Programming : Logic : Boolean : typematch

typematch

type based pattern matching

 Calling Sequence typematch(e, t) typematch(e, t, 's')

Parameters

 e - any expression t - any type, possibly augmented by :: operators s - (optional) a name

Description

 • The typematch command is a boolean valued function. It returns true if the input expression e matches the type t. Normally the type t will contain pattern bindings, that is, expressions of the form ${v}_{i}::{t}_{i}$ where v_i is a variable For example in the command typematch(e,v::name=range), the purpose of the v variable is to bind v with the corresponding name in the expression e . The typematch command has two different ways of returning the bound values, which are as follows.
 • In command typematch(e, t), all pattern variables v_i in t are assigned the values as they are successfully matched. Thus the result of typematch(x=1..infinity, v::name=range) is true and v is assigned the name x.
 • In command typematch(e, t, 's') if the match is successful, a list of equations of the form ${v}_{i}={r}_{i}$ is assigned the parameter s which represents the variables and their matched values. Thus the result of typematch(x=1..infinity, v::name=range, 's') is true and s is assigned the list $\left[v=x\right]$.
 • Note that the arguments to the pattern binding operator :: are evaluated. Thus if the variable x may have been assigned a value, for example, in a previous typematch command, then it is necessary to use quotes. That is typematch(e,'x'::name=range).
 • For more information about the precedence of the pattern binding "::" operator in relation to other operators, see operators/precedence.

 • The typematch command is thread safe as of Maple 15, provided that s, if used, is not shared between threads.

Examples

 > $\mathrm{typematch}\left({x}^{2},{b::\mathrm{anything}}^{n::'\mathrm{integer}'},'s'\right)$
 ${\mathrm{true}}$ (1)
 > $s$
 $\left[{b}{=}{x}{,}{n}{=}{2}\right]$ (2)
 > $\mathrm{subs}\left(s,n{b}^{n-1}\right)$
 ${2}{}{x}$ (3)
 > $\mathrm{typematch}\left({x}^{2},{b::\mathrm{anything}}^{n::'\mathrm{integer}'}\right)$
 ${\mathrm{true}}$ (4)
 > $b$
 ${x}$ (5)
 > $n$
 ${2}$ (6)
 > $n{b}^{n-1}$
 ${2}{}{x}$ (7)
 > $b≔'b':$$n≔'n':$
 > $\mathrm{typematch}\left({x}^{2},p::\left({b::\mathrm{anything}}^{n::'\mathrm{integer}'}\right),'s'\right)$
 ${\mathrm{true}}$ (8)
 > $s$
 $\left[{b}{=}{x}{,}{n}{=}{2}{,}{p}{=}{{x}}^{{2}}\right]$ (9)
 > $\mathrm{typematch}\left(x=1..2,\left\{v::\mathrm{name},v::\mathrm{name}=\mathrm{algebraic}..\mathrm{algebraic}\right\},'s'\right)$
 ${\mathrm{true}}$ (10)
 > $s$
 $\left[{v}{=}{x}\right]$ (11)
 > $\mathrm{typematch}\left(\left[1,2,3,4,5\right],\mathrm{list}\left(v::'\mathrm{integer}'\right),'s'\right)$
 ${\mathrm{true}}$ (12)
 > $s$
 $\left[{v}{=}{1}{,}{v}{=}{2}{,}{v}{=}{3}{,}{v}{=}{4}{,}{v}{=}{5}\right]$ (13)