Test of understanding: "Variable" vs "Value" and "function" against "abstraction",

(This question is a continuation of this when studying Haskell.)

I used the concepts of "variable" and "value", which are confusing. So I read about the lambda calculus wiki page, as well as the previous answer above. I come out with lower interpretations.

Can I confirm whether they are correct? I just want to double the confirmation, because this concept is quite simple, but important for functional programming. Any advice is appreciated.

Premises from the wiki :

Lambda calculus syntax
exp → ID
| (Exp)
| λ ID.exp // abstraction
| exp exp // application

(Designation: "<=>" is equivalent)

Interpretations:

  • "value": This is actual data or instructions stored on the computer.
    “variable”: this is a way to determine the location of the data, a link to replace the value, but not the data set or instructions stored on the computer.
    "abstraction" <=> "function" ∈ syntactic form. ( https://stackoverflow.com/questions/816789/... )
    "application": it accepts the input of "abstraction", and the input of the "lambda expression" leads to the "lambda expression".
    “Abstraction” is called abstraction because in the usual definition of a function we shorten the body of a function (usually a longer one) into a much shorter form, that is, the identifier of the function, followed by a list of formal parameters. (Although lambda abstractions are anonymous functions, other functions usually have a name.)

  • "variable" <=> "symbol" <=> "reference"
    a "variable" is associated with a "value" through a process called "binding."

  • "constant" ∈ "variable"
    "literal" ∈ "value"
    "formal parameter" ∈ "variable"
    "actual parameter" (argument) ∈ "value"

  • a "variable" may have a "value" for "data", => for example. the variable "a" has a value of 3

  • a "variable" can also have a "value" of a "set of instructions", => for example. the + operator is a variable
+5
function variables haskell lambda-calculus
source share
2 answers

"value": This is actual data or instructions stored on the computer.

You are trying to think about it very specifically from the point of view of a machine that, I am afraid, may confuse you. It’s better to think about it from the point of view of mathematics: value is simply a thing that never changes, for example, the number 42, the letter "H" or the sequence of letters that make up the "Hello World".

Another way to think about it is in terms of mental models. We come up with mental models to indirectly talk about the world; talking about mental models, we make predictions about things in the real world. We write computer programs that help us work with these mental models reliably and in large volumes.

Values ​​are things in the mental model. Bits and bytes are just a coding model in computer architecture.

"variable": this is a way to determine the location of the data, a link to replace the value, but not the data set or instructions stored on the computer.

A variable is simply a name that denotes a value in a specific area of ​​the program. Each time a variable is evaluated, its value must be sought in the environment. There are several implementations of this concept in computer terms:

  • The stack frame in an impatient language is an implementation of the environment for finding the values ​​of a local variable each time the subroutine is called.
  • The compiler provides environments for finding global scope names when a program is compiled or loaded into memory.

"abstraction" <=> "function" ∈ syntactic form.

Abstraction and function are not equivalent. In the lambda calculus, "abstraction" is a type of syntactic expression, but a function is a value.

One analogy that is not too shabby is names and descriptions versus things. Names and descriptions are part of the language, and things are part of the world. You could say that the meaning of a name or description is what it calls or describes.

Languages ​​contain both simple names for things (for example, 12 is a name for the number twelve), as well as more complex descriptions of things ( 5 + 7 - a description of the number twelve). Lambda abstraction - a description of the function; for example, the expression \x -> x + 7 is a description of a function that adds seven to its argument.

The trick is that when descriptions become very complex, it is not easy to understand what they describe. If I give you 12345 + 67890 , you need to do some work to figure out which number I just described. Computers are machines that are faster and more reliable than we can do.

"application": it accepts the input of "abstraction", and the input of the "lambda expression" leads to the "lambda expression".

An application is simply an expression with two subexpressions that describes the meaning as follows:

  • The first subexpression means function.
  • The second subexpression means some meaning.
  • The application as a whole means a value that leads to the application of the function in (1) to the value from (2).

In formal semantics (and don't be afraid of the word), we often use double brackets ⟦∙⟧ to mean "meaning"; for example ⟦dog⟧ = "meaning of the dog." Using these notation:

 ⟦e1 e2⟧ = ⟦e1⟧(⟦e2⟧) 

where e1 and e2 are any two expressions or terms (any variable, abstraction, or application).

"abstraction" is called abstraction because in the usual definition of a function, we shorten the body of the function (usually longer) into a much shorter form, that is, the identifier of the function, followed by a list of formal parameters. (Although lambda abstractions are anonymous functions, other functions usually have a name.)

Honestly, I never thought about whether the term "abstraction" is suitable for this or why it was chosen. As a rule, with mathematics, he does not pay to ask such questions, unless the terms were very poorly chosen and mislead people.

"constant" ∈ "variable"

"literal" ∈ "value"

The lambda calculus itself does not have the concepts of "permanent" and "literal." But one way to define them is:

  • A literal is an expression that, due to the rules of the language, always has the same meaning no matter where it occurs.
  • A constant, in a purely functional language, is a variable in the very top area of ​​a program. Each (without shadow) use of this variable will always have the same value in the program.

"formal parameter" ∈ "variable"

"actual parameter" (argument) ∈ "value"

A formal parameter is one of the uses of a variable. In any expression of the form λv.e (where v is a variable and e is an expression), v is a formal variable.

An argument is any expression (not a value!) That occurs as the second subexpression of the application.

a "variable" may have a "value" of "data" => for example. the variable "a" has a value of 3

All expressions have values, not just variables. For example, 5 + 7 is an application, and it has a value of 12.

a "variable" may also have a "value" of a "set of instructions" => for example. the + operator is a variable

The value + is a function - it is a function that adds its arguments. A set of instructions is an implementation of this function.

Think of a function as an abstract table that says, for each combination of argument values, what is the result. The way to enter instructions is as follows:

  • For many functions, we cannot literally implement them as a table. If added, this is because the table will be infinitely large.
  • Even for functions where we can list cases, we want to implement them much more concisely and efficiently.

But the way you check that the implementation of the function is correct, in a sense, checks that in each case it does the same thing as the "endless table". The two sets of instructions that both verify in this way are indeed two different implementations of the same function.

+6
source share
  • The word "abstraction" is used because we cannot "look inside" the function and see what happens for the most part, therefore it is "abstract" (contrasts with the "concrete"). An application is the process of applying a function to an argument. This means that his body is running, but with what is applied to it, replacing the name of the argument (avoiding any capture). I hope this example explains better than I can (in Haskell. \ Syntax represents lambda): (\x -> x + x) 5 <=> 5 + 5 Here we apply the lambda expression on the left to the value 5 on the right. We get 5 + 5 as our result (which can then be further reduced to 10 ). A “link” may refer to something else in the Haskell context ( IORef and STRef s), but internally all the bindings (“variables”) in Haskell have a layer of indirect links, like links in other languages ​​(in fact, they have an even larger indirect nature than it is in some way due to a lax assessment).

  • This basically looks fine, except for the reference issue I mentioned above.

    • In Haskell, there is no difference between a variable and a constant.

    • A literal is usually a constructor for a value. For example, 20 builds the number 20 , but the application (\x -> 2 * x) 10 will not be considered a literal for 20 because it has an extra step before you get the value.

    • That's right, not all variables are parameters. A parameter is what is passed to the function. x in the above lambda expressions are examples of parameters. A non-example would be similar to let a = 15 in a * a . a is a "variable" but not a parameter. In fact, I would call a “binding” here, because it can never change or take on a different value (vary).

    • The formal parameter vs the actual part of the parameter looks correct.

  • It looks good.

  • I would say that instead a variable can be a function. Usually in functional programming, we usually think of functions and functional applications instead of lists of instructions.

I would also point out that you might run into problems thinking of functions as just syntactic forms. You can create new functions by applying some kinds of functions of a higher order, without using one of the syntactic forms for directly building the function. A simple example of this is functional composition, (.) In Haskell

 (f . g) x = f (gx) -- Definition of (.) (* 10) . (+ 1) <=> \x -> ((* 10) ((+ 1) x)) <=> \x -> 10 * (x + 1) 

Record as (* 10) . (+ 1) (* 10) . (+ 1) does not directly use lambda syntax or function definition syntax to create a new function.

+2
source share

All Articles