One way may be to define global shorthand notations for pattern matching.
For example, I get the result
f[a_?i]:=2 a;
f//Information
(* Global`f *)
(* f[a_?IntegerQ]:=2 a *)
with the code
ClearAll[shortHand, shortHandReplace, shortHandReplacements, patternTest];
shortHand = {{i, IntegerQ}, {nric, NumericQ}, {num, NumberQ}};
shortHandReplacements = Block[{temp}, Table[patternTest[a_, term[[1]]]:> patternTest[a, temp] /. temp -> term[[2]], {term,shortHand}]];
SetAttributes[shortHandReplace, HoldAllComplete]
shortHandReplace[input_] := ReleaseHold[HoldForm[input] /. PatternTest-> patternTest /. shortHandReplacements /. patternTest ->PatternTest];
$Pre = Function[input, shortHandReplace[input], HoldAllComplete];
Basically, shorthand
is a list such that any encounter of the first item in a PatternTest
is to be replaced with second item before evaluation. For example, {i,IntegerQ}
in the list ensures that even though we type f[a_?i]
, it is evaluated as f[a_?IntegerQ]
.
The function shortHandReplacements
create these replacements, and shortHandReplace
apply those to given input. I use $Pre
so that this is applied to all input.
This shorthand works in the rules as well because it only relies on the presence of PatternTest
. For example,
g[3]/.a_?i:>2 a
(* g[6] *)
One advantage of the code is that the shorthand notation does not affect the code outside so using these shortcuts is no-means a limitation on these variables:
ClearAll[f];
f[i_?i]:=Cos[i]
?f
(* Global`f *)
(* f[i_?IntegerQ]:=Cos[i] *)
Another advantage of the code is that we can arbitrarily define tests and shorthand notations for them easily. For example:
ClearAll[divisibleByThreeQ,divisibleBySevenQ];
divisibleByThreeQ=(Divisible[#,3]&);
divisibleBySevenQ=(Divisible[#,7]&);
shortHand={{i,IntegerQ},{nric,NumericQ},{num,NumberQ},{d3,divisibleBySevenQ},{d7,divisibleBySevenQ}};
allows us to define
ClearAll[j];
j[a_?d3,b_?d7]:=a+b;
?j
(* Global`j *)
(* j[a_?(Divisible[#1,3]&),b_?(Divisible[#1,7]&)]:=a+b *)
num = NumericQ
. Shorter but not necessarily easier to follow for other users. $\endgroup$f[numlist : {_?NumericQ ..}] := Module[{x, y, z, k, l}, {x, y, z, k, l} = numlist; ...]
. $\endgroup$