ASTM updates

| 1 Comment | No TrackBacks

Today, I've been talking with some people in #haskell about this ASTM thing, and I've had some great input, mainly from Stefan Ljungstrand (ski on #haskell), along with quicksilver and Simon Marlow, and I thought I'd share the changes I've put in with their help. I'm still not sure how useful this stuff is, but it's fun and I'm learning. If you're wondering what i'm on about, see this post.

So, the new features I've added are mainly to do with exception handling, so that variables don't 'die' if you call tail on [], they report the error, and don't change the 'state'. I've also deleted the Swap constructor, and replaced it with a more general Mod' constructor:

data Transaction a =
      Put a
    | Get (MVar a)
    | Mod (a -> a) (MVar (Maybe E.SomeException))
    | forall b. Mod' (a -> (a,b)) (MVar (Either E.SomeException b))
    | Atom (a -> Bool) (a -> a) (a -> a) (MVar Bool)

This you provide a function which takes the current 'state', possibly modifies it, and returns something else, and return that something else back to you. This works a lot like the State monad, but allows concurrency.

I've changed handler as well to look like this:

handler :: Chan (Transaction a) -> a -> IO b 
handler chan !x = do
  req <- readChan chan
  case req of
    Put a         -> handler chan a
    Get mvar      -> do
        putMVar mvar x
        handler chan x

    Mod f mvar    -> do
        let x' = f x
        p <- E.catch (E.evaluate x' >> return Nothing)
                     (\e -> return (Just e))
        putMVar mvar p
        case p of
          Nothing -> handler chan x'
          _       -> handler chan x

    Mod' f mvar    -> do
          let y@(a,b) = f x
          p <- E.try (E.evaluate a >> E.evaluate b)
          case p of
              Right _  -> do
                  putMVar mvar (Right b)
                  handler chan a
              (Left e) -> do
                  putMVar mvar (Left e)
                  handler chan x

    Atom test y n res ->
      if test x
        then do
              putMVar res True
              handler chan (y x)
        else do
              putMVar res False
              handler chan (n x)
    

As you can see, I've added some exception handling, but I still need to add some more to the Atom/condModAVar case. What I want is something that can keep running even after there's been an exception. I mean, who wants a mutable variable to just stop working huh?. I think once I get that done, along with haddock docs, I'll stick it on hackage.

No TrackBacks

TrackBack URL: http://axman6.homeip.net/cgi-bin/mt/mt-tb.cgi/27

1 Comment

This looks rather fun and I hope you solve the problems.

Leave a comment

February 2009

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.23-en

About this Entry

This page contains a single entry by Alex Mason published on February 18, 2009 12:15 AM.

ASTM: redundant STMish fun was the previous entry in this blog.

AVar released (three times) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.