## Refactoring probability distributions, part 1: PerhapsT

Posted by Eric Kidd Wed, 21 Feb 2007 08:06:00 GMT

Martin Erwig and Steve Kollmansberger wrote PFP, a really sweet Haskell library for computing with probabilities. To borrow their example:

``````die :: Dist Int
die = uniform [1..6]
``````

If we roll a die, we get the expected distribution of results:

``````> die
1  16.7%
2  16.7%
3  16.7%
4  16.7%
5  16.7%
6  16.7%``````

If you haven’t seen PFP before, I strongly encourage you to check it out. You can use it to solve all sorts of probability puzzles.

Anyway, I discovered an interesting way to implement PFP using monad transformers. Here’s what it looks like:

``````type Dist = PerhapsT ([])

uniform = weighted . map (\x -> (x, 1))
``````

In other words, `Dist` can be written by adding some semantics to the standard list monad.

### Perhaps: A less specific version of Maybe

First, let’s define a simple probability type:

``````newtype Prob = P Float
deriving (Eq, Ord, Num)

instance Show Prob where
show (P p) = show intPart ++ "." ++ show fracPart ++ "%"
where digits = round (1000 * p)
intPart = digits `div` 10
fracPart = digits `mod` 10
``````

Thanks to the `deriving (Num)` declaration, we can treat `Prob` like any other numeric type.

We can now define `Perhaps`, which represents a value with an associated probability:

``````data Perhaps a = Perhaps a Prob
deriving (Show)
``````

Now, this is just a generalization of Haskell’s built-in `Maybe` type, which treats a value as either present (probability 1) or absent (probability 0). All we’ve added is a range of possibilities in between: `Perhaps x 0.5` represents a 50% chance of having a value.

Note that there’s one small trick here: When the probability of a value is 0, we may not actually know it! But because Haskell is a lazy language, we can write:

``````Perhaps undefined 0
``````

We’ll need a convenient way to test for this case, to make sure we don’t try to use any undefined values:

``````neverHappens (Perhaps _ 0) = True
neverHappens _             = False
``````

So `Perhaps` is just like `Maybe`. As it turns out, they’re both monads, and they both have an associated monad transformer.

## Probabilistic Functional Programming is cool

Posted by Eric Kidd Sat, 10 Feb 2007 16:14:00 GMT

Syntaxfree is hacking on Martin Erwig’s probability monad. This is one of the coolest monads out there—it allows you to trivially solve all kinds of probability problems.

Mikael Johansson has a good example.

I hope to write a bit more about probability monads soon. There’s already a long post sitting on my hard drive, and some more ideas that I’m still trying to puzzle out.

In the meantime, I’d like to recommend The Haskell Road to Logic, Maths and Programming. There’s an excellent review available.

Tags , , ,

## The first Carnival of Mathematics

Posted by Eric Kidd Fri, 09 Feb 2007 09:25:00 GMT

A blog carnival is a summary of recent posts on an interesting topic. A typical carnival appears weekly or monthly, and moves from blog to blog with each edition.

If you’re into math, go check out the first Carnival of Mathematics. There’s something for everyone:

I’ve gotten plenty of submissions that span the entire gamut of math-blogging: education, pure math, applied math, debunking bad math - it’s all there. Only the gender distribution could be made slightly more equal (and that’s an understatement). I’m linking to the posters in roughly increasing order of mathematical difficulty, but don’t let my opinions deter you from reading the posts closer to the bottom.

Why study math? Even the most abstract math has an uncanny tendency to solve unexpected problems:

[T]he enormous usefulness of mathematics in the natural sciences is something bordering on the mysterious and [there] is no rational explanation for it.

And since I’ve started hacking on Haskell, I’ve been equally surprised at how often deep math can solve real-world programming problems.

Tags

## Haskell: What happens when you divide infinity by 2?

Posted by Eric Kidd Fri, 02 Feb 2007 22:00:00 GMT

Sometime back in elementary school, I first asked teachers, “What happens when you divide infinity by 2?” Some teachers couldn’t answer, and others told me, “It’s still infinity!”

More recently, a couple of friends were discussing a similar question at lunch: “What happens when you add 1 to infinity?”

Of course I said, “It’s still infinity!”, but I couldn’t explain it much better than my school teachers (at least not without using the word denumerable, which is a good way to ruin a lunch conversation).

## Step 1: Counting

First, we need to teach Haskell about the natural numbers. (Why not use Haskell’s built-in integers? Just humor the crazy programmer for a moment, OK?)

A number is either zero, or the successor of another number. We can write that in Haskell as:

``````data Nat = Zero | Succ Nat
deriving (Show, Eq, Ord)
``````

Math geeks in the audience will recognize this as the Peano arithmetic. The “deriving” keyword tells Haskell to define `show` and the comparison operators for us.

Using this definition of Nat, we can now define some numbers:

``````one   = Succ Zero
two   = Succ one
three = Succ two
four  = Succ three
``````

These work the way you’d expect:

``````*Main> three
Succ (Succ (Succ Zero))
*Main> two < three
True``````

OK, I threw in that last example just for fun.