comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@adaos.worldonline.co.uk>
Subject: Side-Effects in Functions [Rosen Trick]
Date: Sun, 4 Nov 2001 17:17:08 -0000
Date: 2001-11-04T17:17:08+00:00	[thread overview]
Message-ID: <9s3tl3$111hco$1@ID-25716.news.dfncis.de> (raw)
In-Reply-To: 5ee5b646.0111040507.5ca7ea23@posting.google.com

"Robert Dewar" <dewar@gnat.com> wrote in message
news:5ee5b646.0111040507.5ca7ea23@posting.google.com...
> "Nick Roberts" <nickroberts@adaos.worldonline.co.uk> wrote in message
news:<9s230d$107b5a$2@ID-25716.news.dfncis.de>...
>
> > PS: Why? Because functions should not have side-effects (in a language
like
> > Ada).
>
> You present this as a kind of obvious ab initio fact, but
> it is nothing of the kind.

I was hoping that it would be obvious that I was putting this point in a
deliberately polemical style. I am merely stating my own strongly held
belief on this subject, based on perfectly good reasoning, as well as the
consensus of programming wisdom.

> Here are the facts:
> ...
> There was some attept early on to consider trying to stop
> functions from having side effects, and indeed I think one
> published early version of Ada distinguished functions and
> value returning procedures, or at least that was discussed.

I'm glad this didn't happen, because, as mentioned in my previous post,
there are occasions -- such as debugging, profiling, auditing -- when it is
useful for a function to have a side-effect, and forcing the programmer to
convert it to a procedure -- and thus all calls on it to procedure calls --
would be wholly unjustified.

> But such attempts were firmly rejected, and the decision
> was that there are situations where it makes perfectly good
> sense for functions to have side effects, and an absolute
> rule prohibiting them is in the same not-well-thought-out
> category as absolute rules against use clauses, goto statements,
> unchecked conversion, and various other very
> useful features of Ada. Useful does not mean that they should be used
> all the time, or misused, just that they have some legitimate use.

I agree there.

> Another important factor is that other languages agree with
> Ada that functions should be allowed to have side effects.
> In particular C takes this viewpoint, and side effects in
> functions are common in C, including side effects in calls
> to standard functions in the C library.

The fact that the C language permits something, or that C programmers do a
particular thing, is surely no recommendation that Ada programmers should do
the same!

> Now in Ada, the rule for functions is a bit odd:
>
>   Side effects from access to global variables - permitted
>   Side effects from modifying access type args - permitted
>   Side effects from in out parameters - prohibited

These rules may seem a little odd, but they pertain to what Ada allows
programmers to do, not what Ada programmers should do. My point was entirely
about what Ada programmers should do.

> I have never understood the rationale for this inconsistent
> design.

The rationale is simple and well-known Robert! Good grief, every Ada
textbook on the planet has an example of how side-effects in functions can
introduce subtle and pernicious bugs (especially on porting). Must I really
repeat these?

> Most annoyingly, the natural translation of a * arg in a
> C function, e.g. *int, is to translate it to an IN OUT
> parameter, but that is arbitrarily not allowed.

This isn't the most natural translation, and the prevention of the use of
in-out parameters in functions is not arbitrary, it was, as you said
earlier, carefully debated.

> It is of
> course allowed to translate it to an access-to-int parameter, since
> there is no restriction on the use of
> such parameters for functions, even though they are obviously
> logically equivalent from an expressive point of view.

They are not logically equivalent. The use of the access parameter implies
the (actual) object is aliased (kept in memory essentially all the time).
This may be something that C assumes for all its variables (clever compiler
deductions notwithstanding), but Ada does not, and rightly so.

> The trouble is that although in-out parameters and access-type-to-foo
> parameters are equivalent from a logical
> point of view, they are very different syntactically, and
> we are left with the following unpleasant alternatives in
> interfacing to C functions:
>
>   1. Interface as procedures. This either requires a
>      wrapper or something like the Valued_Procedure
>      interface available in GNAT. The trouble with
>      this is that the calls get really ugly. Yes, in
>      the case where the result is just an error code
>      it's acceptable, but that's by no means always
>      the case.
>
>   2. Interface using named access types. The calls are
>      somewhat better here, but you get a plague of aliased
>      variables appearing, and the access types need
>      defining which is messy.
>
>   3. Use a wrapper with global variables for the missing
>      parameters. But this is nasty anyway, and gets too
>      far from the C form.

To me, it is obvious that option 2 should be used. To describe the required
aliasing of variables as a 'plague' is rather an overstatement: it simply
means adding the word 'aliased' to the appropriate objects; that's not a
plague! It is absolutely right, since they must (at least in effect) be
aliased for the called C routine(s) to access them via a pointer.

> To me, the arguments for allowing IN OUT parameters, at
> least for interfaced functions, are very strong, but
> unfortunately, this is an argument that cannot be won.
> There seem to be three kinds of people:
>
> 1. Those who are adamantly opposed to IN OUT parameters
> on basically religeous (i.e. belief based) grounds without
> very strong technical justification in many cases, or with
> the technical justification being inconsistent with the
> general permission for functions to have side effects in
> Ada. This is a surprisingly large group.

Well, as I say above, there is indeed a very strong, very well documented,
and generally accepted technical justification for avoiding side-effects in
functions (which is the point I was originally making), and disallowing
in-out parameters in Ada functions seems to be be perfectly in tune with
this principle.

> ...

To sum up, I am amazed at Robert's opposition to what I have always
understood to be a very well established programming principle (in
procedural languages such as Ada). That principle is taught for very good
software engineering reasons. I said "Must I really repeat these?". If the
answer is simply "Yes.", then please just answer "Yes" and I will.

--
Nick Roberts






  reply	other threads:[~2001-11-04 17:17 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                     ` Nick Roberts [this message]
2001-11-05  2:46                       ` Side-Effects in Functions [Rosen Trick] 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
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