Re: More on Function Purity
- From: Joachim Durchholz <jo@xxxxxxxxxxxxx>
- Date: Fri, 18 May 2007 23:21:50 +0200
Ville Oikarinen schrieb:
On Wed, 16 May 2007, Al wrote:
variable int V = GetTimeOfDay(); // runtime
... So, I would assume that a function *cannot* be pure if it:
* Reads from V; and/or
The code as a whole is not pure, because it uses a mutable variable. But
it's the variable itself (and the code that writes to it) that's impure,
not the function. The function just reads the variable, ignoring the fact
that it *could* be written, too. (Actually, writing to V only once in the
code could justify the whole code to be called pure, but at least the
function is pure.)
If you make V immutable, the code becomes pure functional *without changes
to the function*; therefore the function cannot be impure.
Actually, the function still is impure.
If fn() accesses V, and V is updated asynchronously, then
2*fn()
is semantically different from
fn()+fn()
and this at least breaks the "equational reasoning" kind of purity (and probably other definitions of purity, too).
In my book, purity is not a local property. It's a property of a function and the context it's executing in: guarantees about (im)mutability come into play, for example.
In this concrete case, V and any functions accessing it are impure, simply because V may be updated at any time.
If the updates are synchronous, anything that makes use of purity arguments (equational reasoning, compiler transforms, whatever) would have to be confined so that it does not span an evaluation step that updates V.
If the language syntactically or lexically identifies "islands of purity" between updates, then you can do equational reasoning within these islands, and the function is pure within them. If the language does not, then you'll have to do some kind of whole-system analysis to make sure that V isn't updated in the function or any functions called from it. (This can be a challenge, e.g. if the function in question is a HOF: executing a submitted function may update V, so you need dataflow analysis to determine that none of the functions submitted as parameters will ever update V - note that this kind of analysis is undecidable, so assuming that a function is impure if it accesses an impure variable is just a reasonable conservative approximation.)
(Although I find it somehow inconvenient to talk about pure functionality
using imperative code.)
In non-imperative code, it's difficult to have impure code in the first place, so you don't get to write impure examples.
I think the compile time/runtime evaluation question is not relevant here: a program that contains only expressions evaluatable at compile time is
not useful: you don't need to publish the program; the result it evaluates
to is enough :)
Compile-time evaluation cannot continue as soon as inputs are involved.
If the functions are pure, what you get after compile-time evaluation is a function that needs an input for the next reduction step.
Regards,
Jo
.
- Follow-Ups:
- Re: More on Function Purity
- From: henryware
- Re: More on Function Purity
- From: Ville Oikarinen
- Re: More on Function Purity
- References:
- More on Function Purity
- From: Al
- Re: More on Function Purity
- From: Ville Oikarinen
- More on Function Purity
- Prev by Date: Re: More on Function Purity
- Next by Date: Re: Lisp for the C21
- Previous by thread: Re: More on Function Purity
- Next by thread: Re: More on Function Purity
- Index(es):
Relevant Pages
|
Loading