Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the lambda calculus have to say about return values?

It is by now a well known theorem of the lambda calculus that any function taking two or more arguments can be written through currying as a chain of functions taking one argument:

# Pseudo-code for currying
f(x,y) -> f_curried(x)(y)

This has proven to be extremely powerful not just in studying the behavior of functions but in practical use (Haskell, etc.).

Functions returning values, however, seem to not be discussed. Programmers typically deal with their inability to return more than one value from a function by returning some meta-object (lists in R, structures in C++, etc.). It has always struck me as a bit of a kludge, but a useful one.

For instance:

# R code for "faking" multiple return values
uselessFunc <- function(dat) {
   model1 <- lm( y ~ x , data=dat )
   return( list( coef=coef(model1), form=formula(model1) ) )
}

Questions

  1. Does the lambda calculus have anything to say about a multiplicity of return values? If so, do any surprising conclusions result?
  2. Similarly, do any languages allow true multiple return values?
like image 325
Ari B. Friedman Avatar asked Nov 22 '11 13:11

Ari B. Friedman


People also ask

What is lambda calculus explain in detail?

Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any Turing machine.

How do you read lambda in calculus?

Lambda calculus is composed of 3 elements: variables, functions, and applications. The most basic function is the identity function: λx. x which is equivalent to f(x) = x . The first “x” is the function's argument, and the second is the body of the function.

What is the main reduction rule of the semantic of the λ-calculus?

Semantics of Lambda Expressions Evaluating a lambda expression is called reduction . The basic reduction rule involves substituting expressions for free variables in a manner similar to the way that the parameters in a function definition are passed as arguments in a function call.

What is the value of lambda in math?

In the system of Greek numerals, lambda has a value of 30. Lambda is derived from the Phoenician Lamed. . Lambda gave rise to the Latin L and the Cyrillic El (Л).


2 Answers

According to the Wikipedia page on lambda calculus:

Lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion

And a function, in the mathematical sense:

Associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output

So answering your first question no, lambda calculus (or any other formalism based on mathematical functions) can not have multiple return values.

For your second question, as far as I know, programming languages that implement multiple return values do so by packing multiple results in some kind of data structure (be it a tuple, an array, or even the stack) and then unpacking it later - and that's where the differences lie, as some programming languages make the packing/unpacking part transparent for the programmer (for instance Python uses tuples under the hood) while other languages make the programmer do the job explicitly, for example Java programmers can simulate multiple return values to some extent by packing multiple results in a returned Object array and then extracting and casting the returned result by hand.

like image 63
Óscar López Avatar answered Nov 15 '22 19:11

Óscar López


A function returns a single value. This is how functions are defined in mathematics. You can return multiple values by packing them into one compound value. But then it is still a single value. I'd call it a vector, because it has components. There are vector functions in mathematics there, so there are also in programming languages. The only difference is the support level from the language itself and does it facilitate it or not.

like image 39
SasQ Avatar answered Nov 15 '22 19:11

SasQ