4

According to this article on denotational semantics in haskell All types have bottom, and a function f:A->B is strict if it maps the bottom of type A to the bottom of type B, it is called non-strict other-wise.

(This is reminiscent of a pointed category where morphisms preserve the basepoint).

Why does Haskell have non-strict functions, whereas Standard ML doesn't?

Mozibur Ullah
  • 593
  • 3
  • 13
  • Are you asking about why laziness is part of Haskell in particular, or why it is part of programming languages in general? – Heatsink Jan 10 '13 at 16:12
  • 2
    If you're asking about Haskell in particular, it has non-strict functions because other languages have them. Haskell was created to unify the features of extant lazy languages. See section 2.1 of http://research.microsoft.com/en-us/um/people/simonpj/papers/history-of-haskell/index.htm . – Heatsink Jan 10 '13 at 16:17
  • @heatsink:With programming languages in general. I've gathered that non-strictness is something to with laziness but don't understand why. – Mozibur Ullah Jan 10 '13 at 16:18
  • 1
    I don't think that “why does X feature Y” or “why does X not feature Y” are good questions for this site. Why is Java named after an island, why did the go language team feel that exceptions were not necessary, and what's the problem with firefox not being a multi process browser? These are no programming problems actually that stackoverflow is supposed to cover. – scravy Jan 10 '13 at 18:25
  • foldl and foldr are good candidate for you question. "foldleft can be defined in terms of foldright but not vice-versa, since foldleft is strict in the tail of the list argument but foldright is not (i.e. fold_left acting on bottom gives bottom)" – zurgl Jan 10 '13 at 18:52

4 Answers4

12

Every programming language with recursion has at least one non-strict function, often in the form of a conditional (if-then-else). Otherwise all recursions would denote bottom (non-termination). Essential as non-strict functions are, however, most of these languages do not let you define your own! Some languages make up for this limitation by offering macros--a somewhat function-like mechanism that transforms syntax instead of values.

Conal
  • 18,517
  • 2
  • 37
  • 40
7

Why does Haskell have non-strict functions, whereas Standard ML doesn't?

Haskell has non-strict functions -- typically lazy ones -- because they are a useful programming feature to have.

They improve equational reasoning, make it easier to compose code, and make it possible to write more kinds of programs.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • I agree that lazy functions are useful. But how does strictness defined in the question allow that? – Mozibur Ullah Jan 10 '13 at 16:35
  • 1
    Your question needs to be clarified -- are you asking why strictness maps a bottom value to in an argument, to a bottom value for the result? (i.e. passing `undefined` as an argument to a function results in `undefined` in a strict function). – Don Stewart Jan 10 '13 at 17:31
  • it maybe that the word 'strict' is being used for more that one concept here. In denotational semantics its exactly equivalent to preserving bottom. Whereas I think you're using 'strict' to refer to non-lazy functions. I'll change the question to reflect that. – Mozibur Ullah Jan 10 '13 at 17:35
  • 3
    Ah, I see. Laziness is one way to implement non-strict semantics. – Don Stewart Jan 10 '13 at 17:44
6

Simon Peyton-Jones gave some good responses to this in his set of slides, Wearing the Hair Shirt.

Laziness is jolly convenient

Recursive values are jolly useful

Laziness keeps you honest [regarding purity]

The last reason is the most important to me. Haskell's computational purity and tight control of effects are due in large part to its non-strictness.

Every call-by-value language has given into the siren call of side effects

Programmers want to write C-like code, which I think is the "siren call" that lures in most languages. In Haskell, interleaving effects willy-nilly just doesn't make sense, because non-strictness means that you wouldn't be sure when an effect will be performed.

Community
  • 1
  • 1
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
1

Why does Haskell have non-strict functions, whereas Standard ML doesn't?

Because, expression in haskell are evaluated in Weak Head Normal Form, whereas in Standar ML expression are evaluated in Normal Form.

Then, in Haskell you can reason using unevaluated thunk in Standard ML you can't.
But, you should know that laziness can be added to Standard ML. (you can do that in ocaml for example)

Laziness by default in haskell is a design choice, may be, reflecting the belief that creating a compiler which deal with lazy by default could improve understanding of Functional Programming, allowing the research community to go one step forward.

zurgl
  • 1,930
  • 1
  • 14
  • 20
  • 2
    "Because, expression in haskell are evaluated in Weak Head Normal Form ...". I'd say the other way around: WHNF evaluation is in order to support non-strict functions. – Conal Jan 10 '13 at 18:01