[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: statements as zero-value forms (expressions vs. statements)



Howard Stearns <stearns@curl.com> asks:
> SUMMARY:
>   Is a statement just a form that produces zero values?
>   What kinds of values mismatches do you want the compiler to tell you
> about?
>   How do statements help with that?

I think the core distinction is between forms that have side effects
(STATEments, that affect state), and forms that don't.  Some languages
grammaticize this as a syntactic difference; some don't.

Whether a form has values emerges from this distinction.  Since statements
are composed by sequencing them, statement values would in general be
ignored.  (The implicit value of a statement is the new global state, which
is an implicit argument to the next statement.)  Since expressions are
composed by, well, composition, they need values, and since the canonical
expression is purely applicative, an expression without a value does
nothing.

Imperative languages start from this distinction and dilute it, by allowing
expressions that have side effects.  Imperative languages therefore creatie
problems about argument evaluation order, and makie the distinction between
statements and expressions somewhat pointless.  They still take it as a
starting point.  That's why assignment is the canonical statement: it
affects the state.

(For example, arguments about why Python doesn't allow statements in lambda
call on this distinction, even though its designers then turn around and
explain that [f(x) for x in list] is slower than map(f, list) because f(x)
might change the value of f...)

There's an analogy here, to sentences and phrases in natural language.  Paul
Graham suggested here that functions were verbs, because they take
arguments.  Actually both functions and nouns take arguments, and both can
be saturated to produce phrases, which act as the arguments to embedding
phrases, and so forth up to the maximally spanning phrase, which is the
sentence.  Phrases are (arguably) referential: like expressions, they
produce or name values.  Sentences, on the other hand, are sequenced, not
(in general) composed.  And modern treatments of sentence semantics, such as
Discourse Representation Theory and Dynamic Binding, take the meanings of
sentences to be update functions on the discourse state, just like
statements.

I don't think the syntactic distinction works very well in programming
languages.  The parallel constructs for conditionals in C should be evidence
of this, and a langauge like Haskell (with State and IO monads) that
maintains a distinction between the applicative core and state transitions,
doesn't relate this distinction to anything at the syntactic level.
Historically, I suspect they're an intermediate point on the way from
assembly and von Neuman machines, which just use statements, to something
more declarative and applicative.  But I wouldn't be completely surprised if
it made the languages easier for some people to learn and reason about, by
drawing on their familiarity with natural language.  (It's a wild claim, and
I wouldn't be surprised if it didn't help at all, either.)

Backus's Turing Award lecture is worth looking at, before you dismiss the
distinction entirely.