Re: In a Pascal-like language, would being able to declare a subroutine as "purely functional" be of any value?

glen herrmannsfeldt <gah@ugcs.caltech.edu>
Fri, 11 Mar 2011 19:55:22 +0000 (UTC)

          From comp.compilers

Related articles
In a Pascal-like language, would being able to declare a subroutine as noitalmost@cox.net (noitalmost) (2011-03-11)
Re: In a Pascal-like language, would being able to declare a subroutin gah@ugcs.caltech.edu (glen herrmannsfeldt) (2011-03-11)
Re: In a Pascal-like language, would being able to declare a subroutin bobduff@shell01.TheWorld.com (Robert A Duff) (2011-03-11)
Re: In a Pascal-like language, would being able to declare a subroutin pdjpurchase@googlemail.com (1Z) (2011-03-11)
Re: In a Pascal-like language, would being able to declare a subroutin gah@ugcs.caltech.edu (glen herrmannsfeldt) (2011-03-12)
Re: In a Pascal-like language, would being able to declare a subroutin comp.lang.misc@inglorion.net (Robbert Haarman) (2011-03-12)
Re: In a Pascal-like language, would being able to declare a subroutin mcr@wildcard.demon.co.uk (Martin Rodgers) (2011-03-12)
Re: In a Pascal-like language, would being able to declare a subroutin noitalmost@cox.net (noitalmost) (2011-03-15)
[6 later articles]
| List of all articles for this month |
From: glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups: comp.compilers
Date: Fri, 11 Mar 2011 19:55:22 +0000 (UTC)
Organization: A noiseless patient Spider
References: 11-03-032
Keywords: design, optimize
Posted-Date: 11 Mar 2011 19:53:42 EST

noitalmost <noitalmost@cox.net> wrote:


> I was thinking of something like:
> pure function foo(x,y : int) int
> begin
> ...
> end;


Fortran has PURE. You might look at how it was done.


In addition, variables aren't allowed to have the SAVE attribute
(static in some other languages).


Also, I/O isn't allowed. That seems to make sense. Then you try to
debug a program, even a fatal error, by printing something out and
find that you can't. Maybe not so bad, but the STOP statement (like
exit() in C) is also disallowed. Again it makes sense until you try
to debug something.


> I put "purely functional" in quotes because I'm using the term
> pretty loosely, since assignments and such would be allowed inside
> foo. But they all have to be to local variables (i.e. not to an
> alias whose data is in some outer scope).


I believe PURE allows compilers to optimize two calls with the
same arguments into one. That is:


            X=FUN(1)+FUN(1)


into


            X=2*FUN(1)


(so don't make random number routines PURE) Though some say
that Fortran can do that even if a function isn't pure.


In addition, Fortran has ELEMENTAL, which is more restrictive than
PURE. ELEMENTAL allows a function (or subroutine) with only scalar
dummy arguments to be called with array actual arguments, such that it
is called for each array element. Similar to the way you can say:


            Y=SQRT(X)


with X and Y arrays, for element by element operations.


> Can a compiler do this sort of checking, or would the complexity get
> out of hand? I'm thinking that at least a limited form must be
> possible in order to do any sort of inter-procedural register
> allocation.


I believe that most of the checks are pretty easy. Though the general
rule with language restrictions is that they are restrictions on the
programmer, not the compiler. If you don't follow the rules, the
compiler is not required to diagnose them, and in addition you can't
rely on the results.


In the case of ELEMENTAL, the compiler could evaluate more than one
array element at the same time. If you did manage to modify something
outside, or local static, the results may be surprising. That could
also be true of PURE.


-- glen


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.