6
$\begingroup$

A WL (Mma) program is a sequence of expressions to be evaluated, which generally involves applying commands and functions to actual arguments. What is the accepted terminology for what I think of as a function call? (Is this somehow misusing concepts from other languages?) What is the name for the square brackets used to make such a call, which I would be inclined to call call brackets?

The documents seem most often to say we use a command. But I hate this, since we also "use" a command if we pass it as an argument (e.g., to map it over a list). Is the documentation terminology just being folksy, or is it meant to be technical? If the latter, what is the terminology for distinguishing these different kinds of uses?

And btw, does the word "command" always imply a WL builtin, while "function" implies it is a user created callable? (Is there a WL term for "callable"? And finally, what is the test for callability, since CallableQ does not exist?)

$\endgroup$
  • 1
    $\begingroup$ For any expression expr, expr[args] is also an expression that will evaluate to something (possibly to itself). It's true even for basic atomic expressions e.g. 5[a, b] is a valid expression, I don't think I've seen any practical use for such expressions, but there's nothing "incorrect" about them. In that sense everything is "callable". On the practical side, I can see a need to answer following question. Given expression expr and arguments args of certain "type" will result of evaluation of expr[args] be of some other "type"? $\endgroup$ – jkuczm Jun 9 '17 at 19:56
  • $\begingroup$ Depending on answer to this question one could, choose different branch of some algorithm, perform some code optimizations, or check code validity i.e. all benefits of static type checking. Mathematica, in general, does not provide such functionality out of the box, but for certain specific use cases one can introduce some forms of specialized type systems. Compilation functionality has a type system, datasets introduced type system for queries. $\endgroup$ – jkuczm Jun 9 '17 at 19:56
  • 1
    $\begingroup$ Here is a discussion of M as a term-rewriting system by one of the developers (R. Maeder): library.wolfram.com/infocenter/Conferences/183. Related (SO/4430998), (119933). Another article by Maeder, which I can't find a copy of: library.wolfram.com/infocenter/Articles/3255 $\endgroup$ – Michael E2 Jun 10 '17 at 4:38
7
$\begingroup$

I think that in Mathematica, symbols, expressions, evaluation and substitution rules are fundamental terms.

One writes an expression, sends it to the Mathematica kernel, kernel evaluates the expression (i.e. applies all relevant substitution rules) and returns the resulting expression.

For example, one may write f1[x,y]. This is just an expression with head f1 and two parts x and y, where f1, x, and y are all symbols. Square brackets are just a notation to distinguish parts from the head. If there is only one part one may use f1@x instead of the brackets.

One may define a substitution rule for symbol f1. For example:

f1[_Real, _Real]:=0

This instructs kernel to replace any sub-expression with head f1 and two real parts with 0 (another expression). However, if kernel encounters f1[1.0, 1.0, 1.0] or f1[a,b], no substitution will be made.

Very formally f1 is a symbol, which might induce a substitution when encountered as a head of subexpression during evaluation.

For simplicity, one calls f1 a function.

When f1[1.0, 1.0] gets substituted by 0 during evaluation, one may say that this is a function call.

In my experience, the term "function" is usually used for symbols which evaluate to numeric expression (i.e. Sin), while the term "command" is used for symbols which evaluate to some other expressions (i.e. Plot is substituted by Graphics)

There is no Callable in Mathematica, but there is an internal (undocumented) function to check if given expression might cause a substitution when used as a head:

System`Private`MightEvaluateWhenAppliedQ[f1]

True

This function can correctly identify pure functions as well:

System`Private`MightEvaluateWhenAppliedQ[(#) &]

True

| improve this answer | |
$\endgroup$
  • $\begingroup$ This is pretty close to the kind of answer I was looking for. Thanks; I'm going to chew on it a bit. $\endgroup$ – Alan Jun 9 '17 at 23:10
7
$\begingroup$

A WL (Mma) program is a sequence of expressions to be evaluated, which generally involves applying commands and functions to actual arguments.

A program in Mathematica is a sequence of expression evaluations, period. A "function" or "command" is a replacement rule:

f[x_] := x
DownValues[f]

{HoldPattern[f[x_]] :> x}

Instead of f[x_] := x; f[5] we might just as well write f[5] /. f[x_] :> x. Both user-defined "functions" and "built-in functions" work in this way. Replacement rules, functions, and commands are all the same thing.

What is the distinction between DownValues, UpValues, SubValues, and OwnValues? shows that there are several different ways to attach replacement rules to symbols. "Functions" and "commands" is what we would say about symbols with DownValues (and more rarely up values and SubValues; symbols with SubValues would more commonly be referred to as operators), whereas symbols with OwnValues would be called variables.

I think it is justified to call f a function (or command) whether it appears in the context f[5] or Map[f, {1,2,3]. In both cases, f represents a symbol with an entry in DownValues[f].

Perhaps the distinction that you are looking for is that f[5] evaluates to something else, whereas f by itself does not. ValueQ exists to check if this is the case. Note that ValueQ will also return True if the expression will be transformed by OwnValues, UpValues, or SubValues, though. Not just DownValues.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I don't quarrel with what you wrote, but I don't think it answers my question. One exception: you suggest that "command" and "function" are synonyms; but that does not seem to match usage in the documentation. (I get your point about ValueQ, but it still diverges from my query.) My question is primarily about terminology, not about how WL works. Are you saying that since this is all a matter of replacement rules, to use the notion of a function call is misguided? Yet surely some simple term is used whether f is defined with Function (own values) or by pattern matching (down values). $\endgroup$ – Alan Jun 9 '17 at 2:56
  • $\begingroup$ @Alan Function is a special case. A symbol defined as a Function object is always called an "anonymous function". There are other special cases as well, a symbol defined as an Association is called an "association". These symbols take their names from the objects they evaluate to. But as for the questions, I think I did answer those: It makes no sense to separate built-in from user defined functions because they are the same. It makes no sense to distinguish between functions and commands because there aren't two separate concepts. I explained when the word "operator" is used instead. $\endgroup$ – C. E. Jun 9 '17 at 3:37
  • $\begingroup$ @Alan My comment about ValueQ was in response to your question about whether there is a CallableQ – now, it is not exactly clear what you mean by "callable," but by my interpretation ValueQ is the most related test in Mathematica. If you could quote specific examples in the documentation where you think "function" (not in the context of "anonymous function") and "command" are not used interchangeably, that would help. $\endgroup$ – C. E. Jun 9 '17 at 3:40
  • $\begingroup$ @Alan As for the question about square brackets, I did not understand it. What do you call parentheses in languages that use those instead, "call parentheses"? $\endgroup$ – C. E. Jun 9 '17 at 3:54
  • 1
    $\begingroup$ @Alan I think it is ok so say "f2 calls f1 with arguments x and y", but does that mean that f1 is necessarily callable? In Wolfram Language, I would just take that to mean that "f2 evaluates f1[x,y]". f1[x,y] may evaluate to something different, or it may not: either way, it's valid Mathematica code. Also, note that a symbol is not inherently "callable". It is callable and it isn't. It depends on the parameters. I'm guessing a callable in Python throws an error if it is supplied with arguments for which it has not been defined. $\endgroup$ – C. E. Jun 9 '17 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.