Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking (a, a) a Functor
    text
    copied!<p>How can I make <code>(a, a)</code> a <code>Functor</code> without resorting to a <code>newtype</code>?</p> <p>Basically I want it to work like this:</p> <pre><code>instance Functor (a, a) where fmap f (x, y) = (f x, f y) </code></pre> <p>But of course that's not a legal way to express it:</p> <pre><code>Kind mis-match The first argument of `Functor' should have kind `* -&gt; *', but `(a, a)' has kind `*' In the instance declaration for `Functor (a, a)' </code></pre> <p>What I really want is a type-level function like this: <code>\a -&gt; (a, a)</code> (invalid syntax). So a type alias, perhaps?</p> <pre><code>type V2 a = (a, a) instance Functor V2 where fmap f (x, y) = (f x, f y) </code></pre> <p>I would think this would work, but it doesn't. First I get this complaint:</p> <pre><code>Illegal instance declaration for `Functor V2' (All instance types must be of the form (T t1 ... tn) where T is not a synonym. Use -XTypeSynonymInstances if you want to disable this.) In the instance declaration for `Functor V2' </code></pre> <p>If I follow the advice and add the <code>TypeSynonymInstances</code> extension, I get a new error:</p> <pre><code>Type synonym `V2' should have 1 argument, but has been given 0 In the instance declaration for `Functor V2' </code></pre> <p>Well, duh, that's the point! <code>V2</code> has kind <code>* -&gt; *</code> which is what is required of a <code>Functor</code> instance. Well, ok, I can use a <code>newtype</code> like this:</p> <pre><code>newtype V2 a = V2 (a, a) instance Functor V2 where fmap f (V2 (x, y)) = V2 (f x, f y) </code></pre> <p>But now I've got to sprinkle <code>V2</code>s liberally throughout my code instead of just being able to deal with simple tuples, which kind of defeats the point of making it a <code>Functor</code>; at that point I might as well make my own function <code>vmap :: (a -&gt; b) -&gt; (a, a) -&gt; (b, b)</code>.</p> <p>So is there any way to do this nicely, i.e. without a <code>newtype</code>?</p>
 

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