| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Continuations and State Monads 

Page history last edited by Ke Zhu 15 years ago

Video: http://www.kyte.tv/ch/274758-legendz3/399290-houston-altnet

Having no monadic experts, we had a fairly high-level and theoretical discussion regarding the usefulness of monads in general and the continuation and state monads in particular. We first covered an overview of monads by first looking at monoids and their relationship to monads. We then identified that many people using LINQ in C# are already using the list monad via the SelectMany extension method.

 

From there, we moved into continuations, and in particular, using continuation passing style (CPS) in .NET. Continuations are a means of saving the state of the entire application at a point in time so that the state can be retrieved and processing continued from that state after having advanced to some future state. The concept is similar to closures, except that the entire application state is preserved and not just the local variable access. Several potential uses we discussed were:

  • AsyncWebRequest - building up a list of request processes and executing them asynchronously with an explicitly stated callback (which I'm using in a tool for backing up items on Twine called Stealth)
  • Wizards - returning to a previous state when the user moves back
  • Web development with the back button (e. g. Seaside and Wee)
  • Using function composition to build up the action you want to execute on an item retrieved from the database, then retrieving, executing, and committing at the last possible second to avoid concurrency issues and race conditions.
  • Testing algorithms instead of expected data values, i.e. compose the final algorithm, then ensure it was composed correctly.

 

After covering continuations, we briefly discussed the state monad. This one eludes me a bit, and we didn't really get into much of a helpful discussion due to our lack of real understanding as to its usefulness. I highly recommend anyone wanting to learn more watch Brian Beckman's The Zen of Stateless State - The State Monad - Part 1 and Part 2.

 

We finished our main discussion by briefly looking at the Stealth app running the monadic AsyncWebRequest and then looking at the F# syntax for the maybe monad as described by Matthew Podwysocki.

 

Overall, we concluded that monads in C# and F# is certainly looking into and may provide some very nice benefits for concurrency, parallelism, and testing but that we have to be careful how we use them because of the required paradigm shift when thinking about and reading the code. Given that most developers using these tools will work with the syntactic-sugared api, many problems might be avoided. However, the additional complexity must be carefully considered before diving down into the world of monads.

Comments (0)

You don't have permission to comment on this page.