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.
How can non-determinism be modeled with a List monad?
5.6k views Asked by Trident D'Gao AtThere are 4 answers
The list monad can be though of representing "all possible results from a non-deterministic computation". For example, the function
f x = [x, x + 1, x + 2]
can be interpreted as a non-deterministic computation that takes x
and returns one of x
, x+1
and x+2
.
The function
g x = [2 * x, 3 * x]
can be interpreted as a non-deterministic computation that takes x
and returns either 2 * x
or 3 * x
. The "composition" of these two non-deterministic computations should be another non-deterministic computation which takes x
, transforms it to one of x
, x + 1
or x + 2
, and then then either doubles it or triples it. Thus in terms of lists the result should be a list of all six possibilities
Now
g =<< f x = [2 * x, 3 * x, 2 * (x + 1), 3 * (x + 1), 2 * (x + 2), 3 * (x + 2)]
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
g 0 = [1000, 1001]
g x = [2 * x, 3 * x, 4 * x]
although admittedly this is a completely arbitrary and unmotivated example!
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:
You have two coins, labeled Biased and Fair. The Biased coin has two heads, and the Fair coin has one head and one tail. Pick one of these coins at random, toss it and observe the result. If the result is a head, what is the probability that you picked the Biased coin?
We can model this in Haskell as follows. First, you need the types of coin and their faces
data CoinType = Fair | Biased deriving (Show)
data Coin = Head | Tail deriving (Eq,Show)
We know that tossing a fair coin can come up either Head
or Tail
whereas the biased coin always comes up Head
. We model this with a list of possible alternatives (where implicitly, each possibility is equally likely).
toss Fair = [Head, Tail]
toss Biased = [Head, Head]
We also need a function that picks the fair or biased coin at random
pick = [Fair, Biased]
Then we put it all together like this
experiment = do
coin <- pick -- Pick a coin at random
result <- toss coin -- Toss it, to get a result
guard (result == Head) -- We only care about results that come up Heads
return coin -- Return which coin was used in this case
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
>> experiment
[Biased, Biased, Fair]
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: