annotate 7-Haskell/cointoss.hs @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
103
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 module CoinToss where
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 -- Based on https://stackoverflow.com/questions/20638893/how-can-non-determinism-be-modeled-with-a-list-monad
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 -- Because it only throws "*** Exception: <interactive>:57:1-26: Non-exhaustive patterns in function toss" in gchi
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 import Control.Monad -- Required for guard, but not mentioned in the example
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 data CoinType = Fair | Biased deriving (Show)
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 data Coin = Head | Tail deriving (Eq,Show)
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 toss Fair = [Head, Tail]
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 toss Biased = [Head, Head]
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 pick = [Fair, Biased]
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 experiment = do
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 coin <- pick -- Pick a coin at random (Non-determinism takes both coins)
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 result <- toss coin -- Toss it, to get a result (Non-determinism joins all possible toss results in a single list)
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 guard (result == Head) -- We only care about results that come up Heads (Non-determinism does something weird and inexplicable)
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 return coin -- Return which coin was used in this case (Magic has happened here and we've suddenly gone from "Head, Tail, Head, Head" to "Fair, Biased, Biased")
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 part_experiment = do
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 coin <- pick -- Pick a coin at random
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 result <- toss coin -- Toss it, to get a result
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 return result -- Return the intermediary results
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 part_experiment_2 = do
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 coin <- pick -- Pick a coin at random
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 result <- toss coin -- Toss it, to get a result
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 return guard (result == Head) -- Return the intermediary results
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29
98be775c533c An odd "non-determinism" example from StackOverflow
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 -- Load the module and then run "experiment" and you get [Fair,Biased,Biased], which apparently shows that there's a 2/3 chance it's a biased coin