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

Robbert Haarman <comp.lang.misc@inglorion.net>
Sat, 12 Mar 2011 10:39:41 +0100

          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)
Re: In a Pascal-like language, would being able to declare a subroutin wclodius@los-alamos.net (2011-03-15)
Re: In a Pascal-like language, would being able to declare a subroutin mcr@wildcard.demon.co.uk (Martin Rodgers) (2011-03-16)
Re: In a Pascal-like language, would being able to declare a subroutin sinu.nayak2001@gmail.com (Srinivas Nayak) (2011-03-16)
Re: In a Pascal-like language, would being able to declare a subroutin massimo_dentico@hotmail.com (Massimo A. Dentico) (2011-03-18)
[2 later articles]
| List of all articles for this month |
From: Robbert Haarman <comp.lang.misc@inglorion.net>
Newsgroups: comp.compilers
Date: Sat, 12 Mar 2011 10:39:41 +0100
Organization: Wanadoo
References: 11-03-032 11-03-033
Keywords: design, optimize
Posted-Date: 12 Mar 2011 23:21:05 EST

On Fri, Mar 11, 2011 at 07:55:22PM +0000, glen herrmannsfeldt wrote:
> 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). ...


In Haskell, where functions are pure by default, there is an escape
hatch called unsafePerformIO so that impure code can be smuggled into
otherwise pure functions without upsetting the type checker.


Using unsafePerformIO, you can perform logging, e.g. like this:


import System.IO
import System.IO.Unsafe


add a b = unsafePerformIO $ do
        putStrLn $ "add " ++ (show a) ++ " + " ++ (show b)
        return $ a + b


The type of add here is (Num a) => a -> a -> a (i.e. it takes two instances
of Num and returns an instance of Num). This type is the same as if we had
written:


add a b = a + b


which is a pure function.


Regards,


Bob


Post a followup to this message

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