Message Boards Message Boards

1
|
4228 Views
|
10 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Arities of function symbols

Posted 8 years ago

Given a symbol F used in function definitions like so :

F[x_,y_,z_] := ... ; F[x_,y_] := ... ; F[x_] := ... ; F := ...

Is it possible to introspect the system and get the various arities of that function symbol, something like :

Arities[F] -> {3,2,1,0}

Where -> denotes the evaluation of the expression.

Thanks for your help.

POSTED BY: Fabien Todescato
10 Replies

I'm not sure if that is really necessary in the Wolfram Language;

if you just have a function definitions like:

f[x_] := x^2

and you try to evaluate f[2,3] or f[1,2,3] or f[], it will just remain unevaluated; i.e. it automatically checks if the number of arguments matches...

POSTED BY: Sander Huisman

Yes... But having a lot of functions to try against numerous lists, an indexing scheme based on the arities of the functions would avoid incurring the cost of pattern matching in case the arity and the length of the list do not match... That was the idea... :)

POSTED BY: Fabien Todescato

As the language is not compiled, you must realize that every time you use a function g or whatever, it is actually going through a big list of functions and look for a definition of g, is nothing matches then it leaves it unevaluated. Mathematica by its very core is a pattern-matching system, even for simple things like 1+2, or 4! or ....

POSTED BY: Sander Huisman

Indeed... :)

POSTED BY: Fabien Todescato

I meant; I'm not sure how you can circumvent this behaviour ;)

POSTED BY: Sander Huisman

Thank you so much Sanders, for your much appreciated help. Being quite new to the language, I am not that familiar with the notion of DownValues... or UpValues for that matter. I will have check the documentations.

Again, thank you.

POSTED BY: Fabien Todescato

A more in-depth version would be something like:

ClearAll[Arities]
Arities[g_]:=Module[{dv,bl,blns,bls,opts,num},
    dv=DownValues[g];
    dv=dv[[All,1]];
    bl=Count[#,Verbatim[Blank[]],\[Infinity]]&/@dv;
    bls=Count[#,Verbatim[BlankSequence[]],\[Infinity]]&/@dv;
    blns=Count[#,Verbatim[BlankNullSequence[]],\[Infinity]]&/@dv;
    opts=Count[#,Verbatim[Optional][_,_],\[Infinity]]&/@dv;
    num=MapThread[#1+If[#2>0,#2 {1,\[Infinity]},0]+If[#3>0, #3{0,\[Infinity]},0]-#4{1,0}&,{bl,bls,blns,opts}];
    {dv,bl,bls,blns,opts,num}
]

ClearAll[F]
F[x_, y_, z_, f_, g_: 2, x2_: 3] := 14
F[x_, y_, z_, f_, g_: 2] := 14
F[x_, z_, f_, y___] := 14
F[x_, z_, y__] := 14
F[x_, y_] := 14
F[x_] := 23
F[] := 12

Arities[F] // Transpose // Grid

returns:

enter image description here

Which is a list of Definition, # of Blanks, # of BlankSequences, # of BlankNullSequenced, # of optional arguments, and range of number of arguments.

May I ask why you need the arities of the function? I can't really think of a use in 'normal' calculations...

POSTED BY: Sander Huisman

Again, you can go much beyond, there can be other cases:

F[x_,{y_,z_}] := ...

should this be counted as 2 or 3 arguments? It is not so clear...

F[x_,g:{y_,z_}] := ...
F[x_,g:{_,_}] := ...
F[x_,{x_,y_}] := ...

These are the same function definition, it is just that it gives names either to each of the list-items or to the full item, or both. Should this be counted as number of arguments? Not so clear!

I'm very curious why you need the arities, I've used Mathematica for 10+ years in a large range of applications and I never felt the need to know the arities...

POSTED BY: Sander Huisman

Thank you Sander for your detailed explanations. I will need some time to study and digest your code, though.

I am interested in function arities in the context of an algorithm that tentatively applies function symbols to lists... Knowing beforehand the arities of the functions would allow me to use some indexing scheme so as to avoid applying functions of arities n to argument lists of length n' != n. In my case, the functions do not rely on sequence patterns, and have arities known beforehand.

POSTED BY: Fabien Todescato

Hi Fabien,

These function 'rules' are stored in its UpValues and DownValues. You can retrieve them symbolically in this way:

F[x_,y_]:=14
F[x_]:=23
F[]:=12
DownValues[F]
Length/@ReplaceAll[%,F->List][[All,1,1]]

Or as a function:

Arities[g_] := Length /@ ReplaceAll[DownValues[g], g -> List][[All, 1, 1]]

Note however that Mathematica has also BlankSequence x__ and BlankNullSequence x___. So for some function the number of arguments can vary, some can accept 0 to infinity number of arguments. Think for example functions like Plus, Times, Join...

O btw, this function does not take into account this BlankSequence or BlankNullSequence nor optional arguments. You have to make a more elaborate function to catch all those cases. But with DownValues you have all the information...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract