comp.lang.ada
 help / color / mirror / Atom feed
From: dmitry@elros.cbb-automation.de (Dmitry A. Kazakov)
Subject: Re: Side-Effects in Functions [Rosen Trick]
Date: Tue, 06 Nov 2001 09:30:12 GMT
Date: 2001-11-06T09:30:12+00:00	[thread overview]
Message-ID: <3be7a31d.1736453@News.CIS.DFN.DE> (raw)
In-Reply-To: 9s7bfb$11boa1$1@ID-25716.news.dfncis.de

On Tue, 6 Nov 2001 00:10:31 -0000, "Nick Roberts"
<nickroberts@adaos.worldonline.co.uk> wrote:

>"Dmitry A. Kazakov" <dmitry@elros.cbb-automation.de> wrote in message
>news:3be666fe.6426140@News.CIS.DFN.DE...
>> On 4 Nov 2001 23:26:03 -0800, pete@nospam <pete_member@newsguy.com>
>> wrote:
>>
>> >functions, in the mathemtical sense, do not accept an IN OUT paramters,
>> >and do not have side effects.
>> >
>> >y=f(x), x here can never be modified by the function f().
>> >
>> >so, Ada function is more like a math function.
>
>From a pedagogical point of view, care needs to be taken not to give the
>impression that a function in a procedural programming language (most
>programming languages, really) is the same thing as a mathematical function.
>Clearly it isn't: a mathematical function is a kind of relation called a
>'bijection' (and is nowadays specifically defined as such, I think); a
>programming function is really just an encapsulated computation.
>
>Nevertheless, because a great many (practically useful) computations are, in
>fact, essentially to work out the mapping represented by a mathematical
>function from one value to another, it has become idiomatic to call these
>computations functions. Classic examples include such functions as sin, cos,
>and tan. Since the computations involve no side-effects, they neatly fit the
>'no side-effects' model for programming functions.
>
>> No function in a programming language may have no side effects. One
>> could say that there are side effects which are of no interest, but no
>> more than that.
>
>I think, perhaps, that depends on precisely how you define a side-effect. If
>you include modification of registers or memory as side-effects, then it's
>true. However, we are only interested in side-effects in which we are
>interested!

It is a great problem to give a careful definition of side-effects.
Ada follows rather a pragmatic view, side-effects are ones of
arguments. Unfortunately this definition is too weak for correctness
checks. Though I doubt that a correct definition exists. Just consider
an assertion that includes absolute time!

>> The discussion about in out parameters is proved to be evergreen
>> (:-)). But what I presonally cannot understand is why not to allow
>> PROCEDUREs having a result, i.e.
>>
>> procedure Find (Folder : in out Table) return Table_Token;
>>
>> procedure GetNext  (Item_List : in out Iterator) return Element;
>
>It would be pointless to have value-returning procedures in Ada. The reason
>why is because procedures of this kind would suffer from precisely the same
>dangers as functions (and would therefore be of no benefit over functions).
>
>I'm going to spell out the problems with side-effects in functions, so that
>(hopefully) everyone will be aware of them.
>
>Ada (like many procedural languages) permits compilers to evaluate the
>arguments of a function call in any order it chooses. (Obviously we are
>talking about a functions with more than one parameter here.) The arguments
>to this function call could be other function calls (nested within). For
>example:
>
>   A := B(C(E),D(E));
>
>B, C, and D are all functions. First, the compiler will compile code to
>evaluate E, then it will compile code to evaluate (call) C and D, and then
>finally code that evaluates (calls) B, passing in the results from C and D.
>The code calling C may come before or after the code calling D: it is the
>choice of the compiler.
>
>Now, suppose B, C, and D all have a side-effect when called: they print
>their name onto a log file. The result on the log file of compiling and
>executing this line of code with one compiler could be "C D B", but with
>another compiler it could be "D C B". You have the self-same program,
>compiled on two perfectly correct and compliant compilers, doing two
>different things.
>
>The two different behaviours may seem innocuous in the above example, but
>consider this example, adapted from a real-life situation:
>
>   Header := Decode(Data(1..4)) & Decode(Data(5..8));
>
>The Decode function was really a pseudo-random number generator, but it used
>the values it generated to decode an encrypted message. It used a global
>variable for its state, which had to be seeded with the correct encryption
>key; its side-effect was to update this state. Each 'chunk' that it decoded
>had to be 4 bytes long.
>
>The header of a certain message format was 8 bytes long, so we passed it
>into Decode in two 4-byte chunks, as shown. This code worked fine for us,
>but half-way through the project we changed compilers, and suddenly it
>stopped working. Nobody had altered the code. Recriminations flew. Can you
>see what had happened?
>
>Answer: the first compiler evaluated Decode(Data(1..4)) first, and
>Decode(Data(5..8)) second. So the first decryption value generated (at that
>point in the program) by Decode was used on Data(1..4) first, and the second
>on Data(5..8); the second compiler did it the other way around (as it was
>perfectly entitled to do, since the two calls to Decode are the two
>arguments of a call to "&").
>
>If we had declared Decode as a procedure instead, and then written:
>
>   Decode(Data(1..4),Header(1..4));
>   Decode(Data(5..8),Header(5..8));
>
>this bizarre and mystifying problem would never had occurred (and a certain
>blameless manager -- not myself, I was a junior at the time -- would
>probably not had been sacked).
>
>Side-effects can be programmed into functions quite safely, if you know what
>you're doing. As a rule of thumb, either: (a) diligently ensure that the
>side-effect can't possibly cause a (significant) problem due to an
>'order-of-evaluation dependency', or (b) remove the side-effect.
>
>Typically, the easiest way to remove a side-effect from a function is to
>turn the function into a procedure.
>
>I hope I've convinced any doubters, at this point. If you're still not sure,
>just trust your uncle Nicky, eh?

The above is of course correct. Perhaps everybody enjoyed things like
that at least once [lucky ones did it several times (:-))] 

Yet it proves nothing more than if you have side-effects then, well,
there are side-effects.

Side-effects are bad, dangerous, nasty,... but sometimes necessary.
Then, can Ada's functions prevent users from writting things like
above? The answer is no.

P.S. A comment about computation order. I am not sure, but maybe it
would be worth to think about introducing lazy parameters (Algol's by
name). Ada already has them hard-wired in "and then" and "or else".
The order lazy parameters are evaluated is obviously defined by the
implementation of the subroutine, thus the above problems will
disappear, or better to say, become the responsibility of the
implementation. In your example "&" should have the second parameter
lazy, so 

    Decode(Data(1..4)) & Decode(Data(5..8));

would  mean: evaluate Decode(Data(1..4)); call "&", which internally
calls Decode(Data(5..8)).

Regards,
Dmitry Kazakov



  reply	other threads:[~2001-11-06  9:30 UTC|newest]

Thread overview: 166+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-02  3:56 List container strawman Ted Dennison
2001-11-02  4:20 ` James Rogers
2001-11-02 14:23   ` Ted Dennison
2001-11-02 14:38     ` Preben Randhol
2001-11-02 15:15     ` Larry Kilgallen
2001-11-02  4:35 ` Eric Merritt
2001-11-02 15:46   ` Ted Dennison
2001-11-02  7:28 ` Ehud Lamm
2001-11-02 14:57   ` Marin David Condic
2001-11-02 15:57     ` Francisco Javier Loma Daza
2001-11-02 16:28       ` Marin David Condic
2001-11-02 17:08         ` Ted Dennison
2001-11-02 15:06   ` Ted Dennison
2001-11-02 15:32     ` Marin David Condic
2001-11-02 16:33       ` Ted Dennison
2001-11-02 16:43         ` Marin David Condic
2001-11-02 22:51           ` Jeffrey Carter
2001-11-03  0:24             ` Matthew Heaney
2001-11-03  2:21               ` Jeffrey Carter
2001-11-03 22:51                 ` Rosen Trick [List container strawman] Nick Roberts
2001-11-04 13:07                   ` Robert Dewar
2001-11-04 17:17                     ` Side-Effects in Functions [Rosen Trick] Nick Roberts
2001-11-05  2:46                       ` Robert Dewar
2001-11-05  7:26                         ` pete
2001-11-05 10:29                           ` Dmitry A. Kazakov
2001-11-05 11:19                             ` pete
2001-11-05 14:59                               ` Dmitry A. Kazakov
2001-11-05 15:21                                 ` Preben Randhol
2001-11-05 16:04                                   ` Ted Dennison
2001-11-05 16:33                                   ` Dmitry A. Kazakov
2001-11-05 17:42                                     ` Warren W. Gay VE3WWG
2001-11-05 18:11                                       ` Preben Randhol
2001-11-06  8:38                                       ` Dmitry A. Kazakov
2001-11-06  9:31                                         ` tgingold
2001-11-06  0:10                             ` Nick Roberts
2001-11-06  9:30                               ` Dmitry A. Kazakov [this message]
2001-11-06 16:18                                 ` Lazy Evaluation [Side-Effects in Functions] Nick Roberts
2001-11-07  3:42                                   ` Robert Dewar
2001-11-07  4:42                                     ` Darren New
2001-11-07 11:54                                       ` Robert Dewar
2001-11-07 13:32                                         ` Florian Weimer
2001-11-07 13:24                                           ` Jean-Marc Bourguet
2001-11-09 18:06                                         ` Ted Dennison
2001-11-09 18:27                                           ` M. A. Alves
2001-11-11 20:13                                           ` Georg Bauhaus
2001-12-06 17:47                                             ` Harri J Haataja
2001-11-07  9:28                                   ` Dmitry A. Kazakov
2001-11-06 20:08                               ` Side-Effects in Functions [Rosen Trick] Florian Weimer
2001-11-06 22:48                                 ` Nick Roberts
2001-11-07 10:46                                   ` Florian Weimer
2001-11-05 13:56                           ` Robert Dewar
2001-11-05 16:08                             ` Ted Dennison
2001-11-05 17:44                               ` Warren W. Gay VE3WWG
2001-11-05 15:56                         ` Ted Dennison
2001-11-05 18:46                         ` Nick Roberts
2001-11-08 11:51                           ` Georg Bauhaus
2001-11-16  0:31                 ` List container strawman Vincent Marciante
2001-11-05 15:10             ` Marin David Condic
2001-11-05 18:31               ` Ted Dennison
2001-11-05 19:09                 ` Marin David Condic
2001-11-05 21:23                   ` Ted Dennison
2001-11-07 19:27                   ` Stephen Leake
2001-11-02 18:11         ` Mark Johnson
2001-11-02 18:46           ` Marin David Condic
2001-11-02 19:21           ` Larry Kilgallen
2001-11-03 22:30         ` Nick Roberts
2001-11-02 16:26   ` Ted Dennison
2001-11-02 16:36     ` Marin David Condic
2001-11-02 19:31       ` Ted Dennison
2001-11-02 17:49     ` Jeffrey Carter
2001-11-08 10:34     ` Ehud Lamm
2001-11-08 18:53       ` Better Finalisation [List container strawman] Nick Roberts
2001-11-09 13:36         ` Robert Dewar
2001-11-09 15:04           ` Florian Weimer
2001-11-10  0:36           ` Nick Roberts
2001-11-09 15:16         ` Ted Dennison
2001-11-09 17:30         ` Better control of assignment Jeffrey Carter
2001-11-10  0:32           ` Nick Roberts
2001-11-10 22:27             ` Jeffrey Carter
2001-11-13  6:36               ` Craig Carey
2001-11-13  6:39               ` Craig Carey
2001-11-13  8:53               ` Craig Carey
2001-11-14  9:42                 ` Craig Carey
2001-11-09 14:49       ` List container strawman Ted Dennison
2001-11-09 16:12         ` Ehud Lamm
2001-11-09 17:12         ` Marin David Condic
2001-11-09 18:11           ` Ted Dennison
2001-11-09 18:42           ` Matthew Heaney
2001-11-10 17:54             ` Simon Wright
     [not found] ` <3BE29AF4.80804@telepath.com>
2001-11-02 13:14   ` Ted Dennison
2001-11-02 13:31     ` Larry Kilgallen
2001-11-02 15:09       ` Ted Dennison
2001-11-02 15:13         ` Preben Randhol
2001-11-02 20:48       ` David Starner
2001-11-02 22:49         ` Larry Kilgallen
2001-11-02 17:44     ` Jeffrey Carter
2001-11-02 20:07       ` Ted Dennison
2001-11-02 23:19         ` Jeffrey Carter
2001-11-03  6:56           ` Ted Dennison
2001-11-03 19:22             ` Jeffrey Carter
2001-11-04 18:58               ` Darren New
2001-11-04 19:40                 ` Larry Kilgallen
2001-11-04 20:49                   ` Darren New
2001-11-07 19:07                   ` ramatthews
2001-11-08  0:04                     ` Darren New
2001-11-08  4:50                     ` Jeffrey Carter
2001-11-08 23:26                       ` ramatthews
2001-11-09 18:00                     ` Ted Dennison
2001-11-09 18:13                       ` Jean-Marc Bourguet
2001-11-09 18:55                         ` Ted Dennison
2001-11-10  1:48                           ` Nick Roberts
2001-11-10 17:04                             ` Ted Dennison
2001-11-10 20:59                               ` Nick Roberts
2001-11-10 23:17                                 ` Larry Hazel
2001-11-11  3:27                                   ` Nick Roberts
2001-11-12 18:39                                     ` Darren New
2001-11-13  0:35                                       ` Nick Roberts
2001-11-10 19:36                             ` Ehud Lamm
2001-11-10 20:15                               ` Nick Roberts
2001-11-09 19:27                       ` Larry Kilgallen
2001-11-09 20:03                       ` Stephen Leake
2001-11-09 21:05                         ` Ted Dennison
2001-11-09 22:42                         ` Larry Kilgallen
2001-11-10  4:52                           ` Nick Roberts
2001-11-10 20:24                       ` ramatthews
2001-11-05 19:28                 ` Ted Dennison
2001-11-05 19:42                   ` Jean-Marc Bourguet
2001-11-05 20:40                     ` Ted Dennison
2001-11-05 20:24                   ` Darren New
2001-11-05 20:45                     ` Ted Dennison
2001-11-05 17:21         ` List container strawman; Construct alternatives Stephen Leake
2001-11-03  7:42       ` List container strawman Simon Wright
2001-11-05 14:00   ` Stephen Leake
2001-11-08 11:17     ` Simon Wright
2001-11-13 16:29       ` Stephen Leake
2001-11-13 22:43         ` Jeffrey Carter
2001-11-13 22:48         ` Jeffrey Carter
2001-11-14  3:46           ` Nick Roberts
2001-11-15 10:23             ` Ehud Lamm
2001-11-14 14:50           ` Marin David Condic
2001-11-14 16:53             ` Jeffrey Carter
2001-11-14 17:59               ` Marin David Condic
2001-11-15  3:33                 ` Nick Roberts
2001-11-15 15:10                   ` Marin David Condic
2001-11-16  1:29                     ` Nick Roberts
2001-11-16 16:03                       ` Marin David Condic
2001-11-16 20:19                         ` Nick Roberts
2001-11-15 18:08                   ` Matthew Heaney
2001-11-02 14:49 ` Marin David Condic
2001-11-02 15:15   ` Ted Dennison
2001-11-02 15:37     ` Marin David Condic
2001-11-02 16:49       ` Ted Dennison
2001-11-02 17:09         ` Marin David Condic
2001-11-04  0:10           ` Nick Roberts
2001-11-03 23:41         ` Nick Roberts
2001-11-02 17:02 ` David Botton
2001-11-02 17:55   ` David Botton
2001-11-03 19:22 ` Nick Roberts
2001-11-08 14:57 ` M. A. Alves
2001-11-09  2:00   ` Jeffrey Carter
2001-11-09 18:31   ` Ted Dennison
2001-11-10 19:56     ` Ehud Lamm
  -- strict thread matches above, loose matches on Subject: below --
2001-11-05 21:10 Side-Effects in Functions [Rosen Trick] Alexandre E. Kopilovitch
2001-11-05 21:58 ` Brian Rogoff
2001-11-22  6:22   ` David Thompson
2001-11-23  7:18     ` Brian Rogoff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox