9

In eager languages like Scheme and Python, you can use a lambda expression without parameters to delay evaluation, e.g. in Scheme (Chicken Scheme):

#;1> (define (make-thunk x) (lambda () (+ x 1)))
#;2> (define t (make-thunk 1))
#;3> (t)
2

In line 2, t is bound to the unevaluated expression (lambda () (+ 1 1)), which is then evaluated to 2 in line 3.

Similarly, in Python:

>>> def make_thunk(x): return lambda: x + 1
... 
>>> t = make_thunk(1)
>>> t()
2

Using this technique one can implement lazy evaluation in an eager language.

So, I was expecting that Haskell would not have lambda expressions without parameters because the language is already lazy and there is no need to build delayed expressions. To my surprise, I found out that in Haskell it is possible to write the lambda expression

\() -> "s"

which can only be applied to the () value like so:

(\() -> "s") ()

giving the result

"s"

Applying this function to any argument other than () throws an exception (at least as far as I could see during my tests). This seems different from delayed evaluation in Scheme and Python, because the expression still needs an argument to be evaluated. So what does a lambda expression without variables (like \() -> "s") mean in Haskell and what can it be useful for?

Also, I would be curious to know if similar parameterless lambda expressions exist in (some variety of) lambda-calculus.

1
  • "Applying this function to any argument other than () throws an exception..." Does it cause the program to throw an exception, or cause the compiler to complain that the code doesn't type check? Mar 8, 2020 at 1:02

3 Answers 3

14

Well, the other answers cover what \() -> "something" means in Haskell: an unary function that takes () as argument.

  • What is a function without arguments? – A value. Actually, it can occasionally be useful to think of variables as nullary functions that evaluate to their value. The let-syntax for a function without arguments (which doesn't actually exist) ends up giving you a variable binding: let x = 42 in ...

  • Does lambda calculus have nullary functions? – No. Every function takes exactly one argument. However, this argument may be a list, or the function may return another function that takes the next argument. Haskell prefers the latter solution, so that a b c is actually two function calls ((a b) c). To simulate nullary functions, you have to pass some unused placeholder value.

4
  • 1
    +1 for "What is a function without arguments? - A value", and by extension the nullary function view as well. I think it's useful to be able to see how functions and values are related, and a function can be seen as a generalization of a value.
    – KChaloux
    Sep 23, 2014 at 21:35
  • @amon: So a nullary function only makes sense in a language like Scheme in which you can explicitly trigger evaluation and in which functions (procedures) can be useful both for their value and for their side-effects.
    – Giorgio
    Sep 24, 2014 at 7:19
  • @Giorgio Lisp is a direct encoding of lambda calculus with a lot of syntactic sugar, but if it's lambda calculus, it can't have nullary functions. Everything is a list, so the function application expression (f) is conceptually a single-element list, with a head value of 'f and a nil tail – so using cons we can write it as (cons 'f '()). This tail is the list that is (conceptually) used as an argument, and it doesn't matter that nil represents the empty list. The difference between Lisp and Haskell is that the latter has implicit currying, so the expression (f) means different things
    – amon
    Sep 24, 2014 at 13:08
  • 2
    But yes, functions without arguments are not generally useful in Haskell, because it's lazy and pure. Strict languages can use argument-less functions to simulate laziness, or for general-purpose callbacks. Whether you have to provide a dummy argument such as () (as is the case in MLs) or whether you don't knowingly do so (as is the case in Lisps or C-like languages) doesn't really matter.
    – amon
    Sep 24, 2014 at 13:13
9

You're misinterpreting what () means in Haskell. It isn't the lack of a value, it is rather the only value of the Unit type (the type itself being referred to by an empty set of parentheses ()).

Since lambdas can be constructed to use pattern matching, the lambda expression \() -> "s" is explicitly saying "create an anonymous function, expecting an input that matches the () pattern". There isn't much point to doing it, but it's certainly allowed.

You can use pattern matching with lambdas in other ways as well, for example:

map (\(a, b) -> a + b) [(1,2), (3,4), (5,6)] -- uses pattern matching to destructured tuples

map (\(Name first _) -> first) [Name "John" "Smith", Name "Jane" "Doe"] -- matches a "Name" data type and its first field

map (\(x:_) -> x) [[1,2,3], [4,5,6]] -- matches the head of a list
9
  • Except that Unit is also spelt () in Haskell.
    – user7043
    Sep 23, 2014 at 21:15
  • @delnan I'm more proficient in Scala =P I'll update the answer to be a little more specific on that point.
    – KChaloux
    Sep 23, 2014 at 21:15
  • So I imagine this pattern-matching mechanism is a Haskell feature which is not present in lambda-calculus (?)
    – Giorgio
    Sep 23, 2014 at 21:16
  • @Giorgio I'm not super up on my basic lambda calculus, but my understanding is that you are correct; pattern matching is a language feature, and not innate to lambda calculus.
    – KChaloux
    Sep 23, 2014 at 21:21
  • 1
    @Giorgio Sounds about right. I think you can pin this on how each language treats side effects. A parameterless function is either A) an unchanging value, or B) something that has a side effect. Haskell tries to avoid side effects more than Python (or Scheme to a lesser extent), so it assumes that a parameterless function is a value. It doesn't make much sense to provide an anonymous function that takes no input and returns the same value (unlike const, which takes any input and transforms it all into the same value).
    – KChaloux
    Sep 23, 2014 at 21:32
2

() -> "s" is a lambda function which takes one argument:

ghci> :t (\() -> "s")
(\() -> "s") :: () -> [Char]

(), the empty tuple (sometimes known as Unit), is a fully-fledged type in Haskell. It only has one member (ignoring _|_*), which is also written as (). Look at the definition of ():

data () = ()

This means the form on the left-hand side of your lambda expression is syntactically a pattern-match which matches (), the only member of the type (). There's only one valid way to call it (which is to supply () as an argument) because there's only one valid member of the type ().

Such a function is not very useful in Haskell, because by default terms are lazily evaluated anyway, as you observed in your question.

For a much more detailed explanation, see this answer.

*_|_ is called "bottom" or "undefined". It always type-checks, but it crashes your program. The classic example of how to get a _|_ is let x = x in x.

1
  • "Such a function is not very useful in Haskell, because by default terms are lazily evaluated anyway, as you observed in your question.": Not only that: I need to provide an argument, which I do not need to do e.g. in Scheme.
    – Giorgio
    Sep 23, 2014 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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