modeller
modeller

Reputation: 3850

Is function a sort of variable?

I used to think variable as data and function as data mappings, and they are distinct things in general sense. However, when reading some language standard (namely, Haskell), I found that the standard appears to group them together (see 2.4, and 10.5, and this question ).

So, I have a few questions regarding variable and function (in addition to the question title):

  1. Does their language-specific meaning (e.g. in Haskell) also conform to their meaning in general sense?
    (i.e. The former is a subset of the latter.) Or one meaning has some complements set of the other?
    (By "in general sense" I mean in mathematics.)
  2. What is the definition of "variable"?
    Does "having a type" define one thing to "be a variable"?
  3. I thought the concept "function" has below (structured) aspects.
    One of its sub-aspects does have a type, so, does it makes "function" a variable?
    Function (has aspects of:)
    • Function specifier / identifier: specifies which function to refer
    • Functionality / mapping : specifies what the function does, i.e. it maps what data to what. (has aspects of:)
      • Mapping type: e.g. in Haskell, a function may has the type of (a -> b)

Edits:

Summary

After reading all the answers (they are really great), I realized that my doubts was due to confusion between "operator" and "function": I wrongly mixed the two things up. The correct understanding should be: an "operator" is just a symbol that refers to a "function".

Allow me to borrow the explanation from E-Lisp Intro that I found very helpful:

We can articulate another characteristic of Lisp based on what we have discussed so far—an important characteristic: a symbol, like +, is not itself the set of instructions for the computer to carry out. Instead, the symbol is used, perhaps temporarily, as a way of locating the definition or set of instructions. What we see is the name through which the instructions can be found. Names of people work the same way. I can be referred to as ‘Bob’; however, I am not the letters ‘B’, ‘o’, ‘b’ but am, or was, the consciousness consistently associated with a particular life-form. The name is not me, but it can be used to refer to me.

Upvotes: 7

Views: 434

Answers (3)

Matt Fenwick
Matt Fenwick

Reputation: 49085

No, a function is not a variable. A function is a value (in Haskell, Python, JavaScript, Clojure, and many other languages, but not in all languages). A variable (or symbol, identifier) can refer or be bound to a value. Therefore, a variable/symbol/identifier can refer to a function.

Your sub-questions:

  1. IMHO, yes. There are higher-order logics in mathematics, for example. There are also common higher-order functions used in mathematics such as derivatives, integrals, and summation.

  2. IMHO, variables aren't defined by "having a type" -- values also have types, albeit possibly in a different sense -- but rather by their ability to represent (stand in for, refer to, point to) something else.

    "What is a variable?" should probably be asked as a separate question, because a precise answer is surprisingly complicated and rooted in the history of mathematics, from what I understand.

  3. No, the fact that functions have types does not mean that they are variables.

Reading your question more carefully, I think there are two separate issues here: 1) what "is" something? and 2) similarities between different things. So while you've identified similarities between variables and functions, that doesn't mean that functions "are" variables or vice versa. I'll try to illustrate this with a Java example (bear with me here!): suppose you have a Java program with two classes, Variable and Function, and you want to know how to create an inheritance relationship between them. My answer is that I don't think there is one -- neither one inherits from the other -- but you don't need one because you can instead create interfaces capturing the aspects that you're interested in, and then implement those interfaces for both classes.

Upvotes: 11

J. Abrahamson
J. Abrahamson

Reputation: 74344

A function is a syntactic form that looks like

\ x -> E

along with a few variations. If the expression E has type b and all references to x in E have type a then the type of the whole expression is a -> b.

Conversely, something like take or (.) is a variable. It turns out that in each of those cases the type of that variable is of the form a -> b so that variable represents a function.

Since Haskell is pure we're guaranteed that replacing a variable with the value it represents will not change the meaning of the program.

(I'll ignore for the moment the difference between syntactic forms and values—you can blur that distinction for a long time without too much trouble.)

This notion of value freely replacing reference is the typical meaning of a variable in mathematics—though not the most core way of reflecting on it.

Generally, a variable is an unknown which may be constrained by any number of things. For instance, it could participate in a system of equations and be constrained to be a solution to that system

a = (True, a)  -- invalid Haskell, but a valid concept! What is "a" here?

Haskell variables cannot be so directly defined: there are restrictions on the kinds of recursive equations a variable can participate in and these are given by the typing judgements of Haskell.

But despite me trying to drive a distinction between "typical" mathematical variables and Haskell variables, the two are FAR more similar than "variables" in other computer languages. Those might be better named "mutables" or "assignables".

Finally, what is the essence of a function? If you "have" a function either via variable reference or syntactic form then you can apply it to an argument. This causes a form of variable substitution to occur. We already stated that variables can be freely substituted when they reference somethjng though—why do we need functions?

Because the construction of a function produces a context which contained bound variables. These variables are ones that only make sense within the body of the function and fail to take any actual reference until the function is applied and the local bubble context melts back into the program.

Generally functions and variables have a complex interplay. Studying (typed or untyped) lambda calculus is all about this.

Upvotes: 6

Adi
Adi

Reputation: 1

If the idea of a function is such that it returns a variable, it can well be assumed to be a variable for the caller. But I doubt the specification would treat a function as a subset of variables since that doesn't not capture the total essence of a function.

Upvotes: -2

Related Questions