What's Function, What's Operator?
Perm url with updates: http://xahlee.org/math/function_and_operators.html
What's Function, What's Operator?
Xah Lee, 2010-12-14
Typically, we understand what “function” and “operator” mean, but average programer or mathematician may have a hard time explaining them. Here, we clarify a bit on the meaning of the word “function” and “operator”, their context, their relation.
Function Is a Mathematical Concept
Function you probably understand. A function takes inputs, and produces a unique value/output for the given inputs. The inputs are called “parameters”. A specific set of input are called “arguments”. The number of parameters of a function is called the function's “arity”. So, for example, the function “sin” has arity 1. A constant, such as 35, π, can be considered as functions of no parameter, so they are functions of arity 0. A function of 2 parameters, such as 「define f(x,y) := x+y」, are arity 2.
Function is a mathematical concept. Viewed in another way, it's a map from one space (aka set) to another. You can read Wikipedia for detail here: Function (mathematics).
Operator is About Notation
A operator, is less of a mathematical concept, but more of a concept of notation, i.e.syntax. A “operator” is a symbol (or symbols) that are written to indicate operations. For example, we write 「1+2」, the “+” is a operator, and the “1” and “2” are its “operands”. Mathematically, operator is a function that acts on its operands. The operands are the arguments of the function.
Typically, operators takes 2 arguments, the left and right of the symbol. e.g. 「2×3」, 「3/4」, 「2^3」, union ∩, intersection ∪.
But there are also 1-argument operators. For example the minus sign 「-3」, and the logical not 「¬」 sign, the factorial 「3!」, square root 「√3」.
Operators can also involve other forms with more symbols. For example, the absolute sign 「|-3|」, floor 「⌊3.8⌋」 uses a bracket, summation ∑ takes 4 arguments, a expression, a variable, and start and end values. The anti-derivative (aka indefinite integral) ∫ takes 2 arguments, a expression and a variable. In traditional notation, integration as operand involves 2 symbols, ∫ and ⅆ. For example, we have 「∫ sin(x) ⅆx 」.
Operator can be a bit complicated. For example 「-3」 can be interpreted in several ways. We can think of the minus sign as unary operator acting on the argument 「3」. So, mathematically, it is a function of 1 arity that returns the addictive inverse of the element 3. Or, we can interprete it as one single entity, a element denoted 「-3」. When we write 「3-2」, the ways to interprete it gets a bit more complex. One way to think of it as a notation shorthand for 「3 + (-2)」. The 「-2」 part can be thought of as before. Another way is to think of 「-」 as a binary operator on 3 and 2, but this seemingly simple interpretation is a bit complex. Because, what is math definition of the minus binary function? I'm not sure how it can be defined in terms of algebra without ultimately thinking of it as additon of 2 elements, one being a addictive inverse. The other way is to think of it as a real line, moving the first argument to the left by a distance of the second argument. Of course ultimately these are equivalent, but i can't think of a simple, direct, interpretation that can serve as a math foundation. Also, this is directly related to how does one interprete division, such as 「3/2」.
The multiplication operator also gets complicated. For example, when we write 「3 x」, it usually means 「3*x」. The space acts as implicit multiplication sign, but otherwise the space char has no significance and only make things easier to read.
There's also the concept of “operator stickiness” (aka “operator precendence”) at work that makes expressions with operators more concise. When we write「3△4▲5」, how do you know it's 「(3△4)▲5」 or 「3△(4▲5)」? The concept of operator stickiness is needed to resolve that. Otherwise, you'll need to always write 「3+(4*5)」 instead of the simpler 「3+4*5」. But this again, also introduced more complexity. When you have a expression of tens of different operators, it becomes a problem of remembering the stickiness grammar for each operator. (in practice, you just resort to use explicit priority indicator by parens. Forgetting Operator Precedence is a frequent source of code error in programing. And in written math for human communication, it is prone to miscommunication.)
Operator is tied to Notation
Because the concept of “operator” more has to do with syntax and notation, and when considering traditional math notation of writing in “2-dimensions”, also the fact that traditional math notation has a lot ambiguities, it gets a bit complicated and not as imprecise as we like. (See: The Problems of Traditional Math Notation)
Mathematically, operator and function are the same thing.
Math function, when written down, usually takes the form e.g. 「sin(x)」, 「f(x,y)」, where the things inside the paren are its parameter/arguments.
Operators are useful because writing everything out in full function notation gets very cumbersome and hard to read. For example, we write 「3+4*5」 instead of 「plus(3,times(4,5))」 or 「+(3,*(4,5))」.
Here's a example of traditional notation using operators, versus traditional full functional notation.
-b+√(b^2-4 a c)/(2 a)
If you don't allow space as implicit multiplication sign, then you have:
If you don't allow the implicit operator precedence, then you have:
Finally, writing it out in the traditional function 「f(x)」 form, you have:
If you prefer words than symbols, as traditionally practiced when writing out functions, you have:
The notation using operators is much concise, readible, but at the cost of relatively complex lexical grammar. The full functional notation is precise, grammatically simple, but difficult to read.
In math context, it's best to think of functions instead of operator, and sometimes also use a uniform function notation, where all arguments are explicitly indicated in one uniform way.
Here's what Wikipedia has to say about operators: Operation (mathematics). Quote:
An operation ω is a function of the form ω : X1 × … × Xk → Y. The sets Xj are called the domains of the operation, the set Y is called the codomain of the operation, and the fixed non-negative integer k (the number of arguments) is called the type or arity of the operation.
Note that it doesn't really speaks of “operators”, but speaks of “operations”, and flatly defines operation as a function.
Traditional Function Notation 「sin(x)」 Isn't Perfect
Note that, even the functional notation such as 「sin(x)」, isn't perfect.
Problem of Functions Returning Functions
Normally, with full function notation, you'd expect the operation clearly corresponds to the nesting structure. For example, our example before:
But there's a problem when a function returns a function. For example, the derivative takes a function and returns a function. We might write:
Now, if we want to evaluate the result at 3, then we might write:
You can see that the notation no longer nests. The operator precedence issue is back. Now, you need a symbol precedence to make it clear.
One solution to this is the lisp language's syntax. In lisp syntax, everything is written inside a paren. The first element is the function name, the rest is its arguments. So, 「sin(x)」 would be written as 「(sin,x)」. (comma is used for separator) Our derivative example would then be:
In this way, the syntax remains a pure nested form, and provides the utmost precision.
Our formula example in fully nested syntax be:
We could change the comma separator to space. So, it would look like this:
(/ (+ (- b) (√ (+ (^ b 2) (- (* 4 a c))))) (* 2 a))
(Note: lisp language syntax is not regular. Many of its syntax does not have the form 「(a b c ...)」. See: Fundamental Problems of Lisp.)
Syntax Design for Computer Languages
Mixing Operator Syntax with Full Function Notation Syntax
In most computer language, they allow both the operator and full function syntax. For example, you can write 「(sin(x))^2+3」. (almost all languages do this; e.g. C, C++, C#, Java, Pascal, Perl, Python, Ruby, Bash, PowerShell, Haskell, OCmal. The only exception is lisps.) This way is normal, and most flexible. Because, not all functions have a associated operator symbol. And, writing everything in nested brackets is not readible and hard to write too. The question is, is it possible to design a syntax, that has very simple lexical grammar, is regular, and easy to read and write?
Though, almost all computer languages does not have a regular syntax, in fact, non of any major computer lang has a syntax specification. The closest one that has a somewhat regular and simple syntax grammar is Mathematica. See: The Concepts and Confusions of Prefix, Infix, Postfix and Fully Nested Notations ◇ Math Notations, Computer Languages, and the “Form” in Formalism.
So, if we were to design a computer language, with a syntax that has a simple grammar, what would it be like?
The following are half written, still work in progress
Here's a explanation of one approach. (much based on syntax of Mathematica)
Start with a fully nested syntax. So, everything is in a fully nested functional notation, like this: 「(a b c ...)」.
For some function names or builtin names, we give them a operator symbol. For example, say we have a function named plus 「(plus,2,3)」. Now, suppose we assign the s
Unique Semantics for Each Symbol
Note first that the char paren is used for 2 purposes. (1) as a operation priority grouping indicator. (2) as delimiter for function's arguments.
So, if the context is designing a consistent math notaton or computer language syntax, this we need to fix. One way, is by insisting that each symbol used only has one unique purpose, and not be dependent on adjacent symbols. So, we might insist that, function parameter delimiters should use the square bracket. Like this 「sin[x]」. (this is what Mathematica does)