Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Haskell have non-strict functions (semantics)? [closed]

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?

like image 639
Mozibur Ullah Avatar asked Jan 10 '13 15:01

Mozibur Ullah


4 Answers

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.

like image 140
Conal Avatar answered Sep 22 '22 15:09

Conal


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.

like image 30
Don Stewart Avatar answered Sep 21 '22 15:09

Don Stewart


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.

like image 37
Dan Burton Avatar answered Sep 21 '22 15:09

Dan Burton


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.

like image 40
zurgl Avatar answered Sep 22 '22 15:09

zurgl