background preloader

Monads

Facebook Twitter

Dixin's Blog - Contents tagged with Category Theory - Tags. .net - In C# adding SelectMany extends linq to a new monad type, how do I do the same thing in VB.net? The Marvels of Monads – Yet Another Language Geek. If the word "continuation" causes eyes to glaze over, then the word "monad" induces mental paralysis.

The Marvels of Monads – Yet Another Language Geek

Perhaps, this is why some have begun inventing more benign names for monads. These days, monads are the celebrities of programming language theory. They gloss the cover of blogs and have been compared to everything from boxes of fruit to love affairs. Nerds everywhere are exclaiming that the experience of understanding monads causes a pleasantly painful mental sensation. Like continuations, monads are simpler than they sound and are very useful in many situations. Immutable update via Lenses. While working on the code to manipulate my immutable level game state I found some interesting Haskell talks around the concept of lenses, these are composable computational blocks for updating immutable object graphs.

Immutable update via Lenses

A lens holds two basic functions which abstract a property get/set pair: A getter that given an object returns a property in the object.A setter that given an object and a property value returns an object with the property set. The basic signature in C# being: public class Lens<TType, TValue> { private readonly Func<TType, TValue> _get; private readonly Func<TValue, Func<TType, TType>> _set; internal Lens( Func<TType, TValue> get, Func<TValue, Func<TType, TType>> set) { Debug.Assert(get !

= null); Debug.Assert(set ! Fabulous adventures in coding. Last time on FAIC I set out to explore monads from an object-oriented programmer’s perspective, rather than delving into the functional programmer’s perspective immediately.

Fabulous adventures in coding

The “monad pattern” is a design pattern for types, and a “monad” is a type that uses that pattern. Rather than describing the pattern itself, let’s start by listing some monad-ish types that you are almost certainly very familiar with, and see what they have in common. These five types are the ones that immediately come to my mind; I am probably missing some. If you have an example of a commonly-used C# type that is monadic in nature, please leave a comment. So, what do these types have in common? Aside: This is essentially an accident of history.

Another way to look at these generic types is that they are “amplifiers”. Types13. Brian Beckman: Don't fear the Monad. Functional programming is increasing in popularity these days given the inherent problems with shared mutable state that is rife in the imperative world.

Brian Beckman: Don't fear the Monad

As we march on to a world of multi and many-core chipsets, software engineering must evolve to better equip software engineers with the tools to exploit the vast power of multiple core processors as it won't come for free as it did in the recent past which was predictably based on Moore's law. Of course, learning new ways to think about programming semantics and code patterns are not always straight forward.

For example, most imperative programmers (which include most of us who build software for a living...) are somewhat perplexed by the notion of functions as first class data structures that can be combined to create powerful and composable systems. Dr. This video interview is the result of Brian's idea that he can in fact remove the fear of monads from anybody who pays attention to his explanation. Tune in. Enjoy. Brian Beckman: The Zen of Stateless State - The State Monad - Part 1. Concurrency is a problem that faces all developers as we move to the age of ManyCore processor architectures.

Brian Beckman: The Zen of Stateless State - The State Monad - Part 1

Managing state is an important aspect of programming generally and for parallel programming especially. The great Brian Beckman demonstrates three ways of labeling a binary tree with unique integer node numbers: (1) by hand, (2) non-monadically, but functionally, by threading an updating counter state variable through function arguments, and (3) monadically, by using a partially generalized state-monad implementation to handle the threading via composition.

Of course during this lesson from one of the masters of mathematical programming, we wind through various conversational contexts, but always stay true to the default topic in a stateful monadic way (watch/listen to this piece to understand what this actually means This is another great conversation with astrophysicist and programming master Brian Beckman. Brian Beckman: The Zen of Stateless State - The State Monad - Part 2. Monad (functional programming) Formally, a monad consists of a type constructor M and two operations, bind and return (where return is often also called unit).

Monad (functional programming)

The operations must fulfill several properties to allow the correct composition of monadic functions (i.e. functions that use values from the monad as their arguments or return value). The return operation takes a value from a plain type and puts it into a monadic container using the constructor, creating a monadic value.

The bind operation performs the reverse process, extracting the original value from the container and passing it to the associated next function in the pipeline, possibly with additional checks and transformations. The Marvels of Monads - Yet Another Language Geek. If the word "continuation" causes eyes to glaze over, then the word "monad" induces mental paralysis.

The Marvels of Monads - Yet Another Language Geek

Perhaps, this is why some have begun inventing more benign names for monads. The Marvels of Monads – Yet Another Language Geek. Parallel Programming with .NET. A few years back, Wes Dyer wrote a great post on monads, and more recently, Eric Lippert wrote a terrific blog series exploring monads and C#.

Parallel Programming with .NET

In that series, Eric alluded to Task<TResult> several times, so I thought I’d share a few related thoughts on Task<TResult> and the async/await keywords.