<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Petr Pudlak wrote:
<blockquote cite="mid:20091106180809.GA20150@pudlak.name" type="cite">
  <pre wrap="">The problem is the (Ord b) condition, which is required for the Map
functions.  When I try to define the monad instance as</pre>
  <blockquote type="cite">
    <pre wrap="">instance Monad Distrib where
    return = dreturn
    (&gt;&gt;=)  = dcompose
    </pre>
  </blockquote>
  <pre wrap=""><!---->obviously, I get an error at (&gt;&gt;=):
    Could not deduce (Ord b) from the context.

Is there some way around? Either to somehow define the monad, or to
achieve the same functionality without using Map, which requires Ord
instances?
  </pre>
</blockquote>
Not being allowed constraints on the variables for class methods is
probably the problem I have most frequently run into recently in
Haskell (is there any way to fix this, or does it open up a whole can
of worms?).<br>
<br>
There is no easy way around it, but for your problem, do you require
that the items be kept unique as you go along?  Could you not use a
list of items with probabilities -- that can potentially contain
duplicate items (but all the summed probabilities should add to one at
every stage, presumably), and then combine them at the end? i.e. your
code would look like:<br>
<br>
<pre wrap="">newtype Distrib a = Distrib { undistrib :: [(a, Float)] }

runDistrib :: Ord a =&gt; Distrib a -&gt; Map.Map a Float
runDistrib = Map.fromListWith (+) . undistrib
</pre>
<br>
This would push the Ord constraint to runDistrib, and allow you to
leave it off (&gt;&gt;=).<br>
<br>
Neil.<br>
<br>
<br>
</body>
</html>