Can anyone explain (better with an example in plain English) what a list monad can do to model non-deterministic calculations? Namely what the problem is and what solution a list monad can offer.
相关问题
- F#: Storing and mapping a list of functions
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Is there something like the threading macro from C
- Top-level expression evaluation at compile time
- FSharp.Data.JsonProvider - Getting json from types
- Stuck in the State Monad
- Signing an F# Assembly (Strong name component)
- Learning F#: What books using other programming la
So, it's important to clearly define what 'non-determinism' means here, since it's not quite the same as how it might be perceived in, say, a non-deterministic algorithm. The sense being captured here is that the computation branches - there may be multiple states that the system can move to at any particular point.
Lists model this because, simply, they contain multiple elements. What's more, monadic comprehensions give us a way to compose non-deterministic results - that is, to model exploring all branches at once.
Here's an example based on coin tossing. The problem is as follows:
We can model this in Haskell as follows. First, you need the types of coin and their faces
We know that tossing a fair coin can come up either
Head
orTail
whereas the biased coin always comes upHead
. We model this with a list of possible alternatives (where implicitly, each possibility is equally likely).We also need a function that picks the fair or biased coin at random
Then we put it all together like this
Notice that although the code reads like we're just running the experiment once, but the list monad is modelling nondeterminism, and actually following out all possible paths. Therefore the result is
Because all the possibilities are equally likely, we can conclude that there is a 2/3 chance that we have the biased coin, and only a 1/3 chance that we have the fair coin.
When we say that it is non-determinism, it means that it has more than one values.
The Learn You A Haskell book nicely explains this:
List monad models non-determinism nicely. Its instance is like this:
So, when you feed a non-deterministic value it will produce another set of non-deterministic value:
The list monad can be though of representing "all possible results from a non-deterministic computation". For example, the function
can be interpreted as a non-deterministic computation that takes
x
and returns one ofx
,x+1
andx+2
.The function
can be interpreted as a non-deterministic computation that takes
x
and returns either2 * x
or3 * x
. The "composition" of these two non-deterministic computations should be another non-deterministic computation which takesx
, transforms it to one ofx
,x + 1
orx + 2
, and then then either doubles it or triples it. Thus in terms of lists the result should be a list of all six possibilitiesNow
so indeed this models non-determinism as we expected.
(There is some awkwardness to using lists for non-determinism, since they also have a ordering of elements. A "set monad" would probably be a more natural way to model non-determinism. Lists certainly contain enough information to model non-determinism, but the ordering means that we have more information than necessary.)
EDIT: in fact what I wrote only really goes as far as using the list applicative instance. To get something that fully takes advantage of the monadic interface you want a computation that returns a number of results that depends on its input, for example
although admittedly this is a completely arbitrary and unmotivated example!