Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three answers to your question, one captious and one unhelpful and one abstract:</p> <h2>The captious answer</h2> <pre><code>instance Monoid (Ftree a) where mempty = Empty mappend = Branch </code></pre> <p>This is an instance of the <code>Monoid</code> type class, but does not satisfy any of the required properties.</p> <h2>The unhelpful answer</h2> <p>What Monoid do you want? Just asking for a monoid instance without further information is like asking for a solution without giving the problem. Sometimes there is a natural monoid instance (e.g. for lists) or there is only one (e.g. for <code>()</code>, disregarding questions of definedness). I don’t think either is the case here.</p> <p><sup>BTW: There would be an interesting monoid instance if your tree would have data at internal nodes that combines two trees recursively...</sup></p> <h2>The abstract answer</h2> <p>Since you gave a <code>Monad (Ftree a)</code> instance, there is a generic way to get a <code>Monoid</code> instance:</p> <pre><code>instance (Monoid a, Monad f) =&gt; Monoid (f a) where mempty = return mempty mappend f g = f &gt;&gt;= (\x -&gt; (mappend x) `fmap` g) </code></pre> <p>Lets check if this is a Monoid. I use <code>&lt;&gt; = mappend</code>. We assume that the <code>Monad</code> laws hold (I did not check that for your definition). At this point, recall the <a href="http://www.haskell.org/haskellwiki/Monad_Laws" rel="nofollow">Monad laws written in do-notation</a>.</p> <p>Our <code>mappend</code>, written in do-Notation, is:</p> <pre><code>mappend f g = do x &lt;- f y &lt;- g return (f &lt;&gt; g) </code></pre> <p>So we can verify the monoid laws now:</p> <p><em>Left identity</em></p> <pre><code>mappend mempty g ≡ -- Definition of mappend do x &lt;- mempty y &lt;- g return (x &lt;&gt; y) ≡ -- Definition of mempty do x &lt;- return mempty y &lt;- g return (x &lt;&gt; y) ≡ -- Monad law do y &lt;- g return (mempty &lt;&gt; y) ≡ -- Underlying monoid laws do y &lt;- g return y ≡ -- Monad law g </code></pre> <p><em>Right identity</em></p> <pre><code>mappend f mempty ≡ -- Definition of mappend do x &lt;- f y &lt;- mempty return (x &lt;&gt; y) ≡ -- Monad law do x &lt;- f return (x &lt;&gt; mempty) ≡ -- Underlying monoid laws do x &lt;- f return x ≡ -- Monad law f </code></pre> <p>And finally the important <em>associativity law</em></p> <pre><code>mappend f (mappend g h) ≡ -- Definition of mappend do x &lt;- f y &lt;- do x' &lt;- g y' &lt;- h return (x' &lt;&gt; y') return (x &lt;&gt; y) ≡ -- Monad law do x &lt;- f x' &lt;- g y' &lt;- h y &lt;- return (x' &lt;&gt; y') return (x &lt;&gt; y) ≡ -- Monad law do x &lt;- f x' &lt;- g y' &lt;- h return (x &lt;&gt; (x' &lt;&gt; y')) ≡ -- Underlying monoid law do x &lt;- f x' &lt;- g y' &lt;- h return ((x &lt;&gt; x') &lt;&gt; y') ≡ -- Monad law do x &lt;- f x' &lt;- g z &lt;- return (x &lt;&gt; x') y' &lt;- h return (z &lt;&gt; y') ≡ -- Monad law do z &lt;- do x &lt;- f x' &lt;- g return (x &lt;&gt; x') y' &lt;- h return (z &lt;&gt; y') ≡ -- Definition of mappend mappend (mappend f g) h </code></pre> <p>So for every (proper) Monad (and even for every applicative functor, as Jake McArthur pointed out on #haskell), there is a Monoid instance. It may or may not be the one that you are looking for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload