Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Simon Peyton-Jones <simonpj-0li6OtcxBFHby3iVrkZq2A <at> public.gmane.org>
Subject: RE: Make it possible to evaluate monadic actions when assigning record fields
Newsgroups: gmane.comp.lang.haskell.prime
Date: Wednesday 11th July 2007 07:38:31 UTC (over 10 years ago)
Another alternative (which I got from Greg Morrisett) that I'm toying with
is this.  It's tiresome to write

        do { x <- 
           ; y <- 
           ; f x y }

In ML I'd write simply

        f  

So Greg's idea (or at least my understanding thereof) is to write it like
this:

        do { f $(stuff1) $(stuff2) }

The idea is that a "splice" $e must be lexically enclosed by a 'do', with
no intervening lambda.  It's desugared to the code above; that is, each
splice it pulled out, in lexically left-right order, and given a name,
which replaces the splice.

Of course it doesn't have to look like the above; the rule applies to any
do:

        do { v <- this; foo $(h v); y <- f $(t v v); ...etc }

The "linearise the splices" rule is quite general.

Don't burn any cycles on concrete syntax; I know the $ notation is used for
Template Haskell; one would need to think of a good syntax.  But the idea
is to make it more convenient to write programs that make effectful calls,
and then use the result exactly once.

Anyway, this'd do what the original proposer wanted, but in a much more
general way.

Just a thought -- I have not implemented this.

Simon

| -----Original Message-----
| From: [email protected]
[mailto:[email protected]] On Behalf Of Adde
| Sent: 10 July 2007 21:40
| To: [email protected]
| Cc: [email protected]
| Subject: Re: Make it possible to evaluate monadic actions when assigning
record fields
|
| On Tue, 2007-07-10 at 17:04 +0000,
[email protected] wrote:
| > Isaac Dupree 
wrote:
| >  >
| >  > Adde wrote:
| >  > >  tmp <- foo
| >  > >  return Bar {
| >  > >    barFoo = tmp
| >  > >  }
| >  >
| >  > There is a feature being worked on in GHC HEAD that would let you do
| >  >
| >  >   do
| >  >    tmp <- foo
| >  >    return Bar{..}
| >  >
| >  > which captures fields from everything of the same name that's in
scope.
| >  >   I think this would also satisfy your desire.
| >  >
| >
| > I guess this means I could write:
| >
| >
| > data D = C {field1 :: Bool, field2 :: Char}
| >
| > f x = do
| >   field1 <- foo1
| >   field2 <- foo2
| >   field3 <- foo3
| >   other stuff
| >   return C{..}
| >
| >
| > instead of
| >
| >
| > f x = do
| >   tmp1 <- foo1
| >   tmp2 <- foo2
| >   field3 <- foo3
| >   other stuff
| >   return $ C { field1 = tmp1, field2 = tmp2 }
| >
| >
| > This has a dangerous feel to it ---
| > extending the definition of D to include a field field3
| > may have quite unintended consequences.
| >
| >
| > What I am missing most in the record arena
| > is a functional notation for record update, for example:
| >
| > {^ field1 }  =  \ f r -> r {field1 = f (field1 r)}
|
| I agree, capturing variables without asking is just scary.
| While I'm pretty biased I still think my suggestion solves the problem
| in a cleaner, more consistent way.
|
| /Adde
|
| _______________________________________________
| Haskell-prime mailing list
| [email protected]
| http://www.haskell.org/mailman/listinfo/haskell-prime
 
CD: 3ms