Ah, I did not know this asTypeOf function.<div><br></div><div>But ScopedTypeVariables also allows you to give inner functions type signatures that reuse polymorphic type parameters of the parent scope, which makes code clearer I think.</div>
<div><br><div class="gmail_quote">On Thu, Apr 2, 2009 at 8:54 PM, Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div dir="ltr"><br><br><div class="gmail_quote"><div class="im">On Thu, Apr 2, 2009 at 9:51 PM, Felipe Lessa <span dir="ltr">&lt;<a href="mailto:felipe.lessa@gmail.com" target="_blank">felipe.lessa@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
<div>On Thu, Apr 02, 2009 at 08:18:27PM +0200, Peter Verswyvelen wrote:<br>
&gt; The type inferer seems to struggle to find the type of minBound and<br>
&gt; maxBound, and GHC asks to use a type annotation.<br>
&gt; To only way I see how to add a type annotation here is to use a GHC<br>
&gt; extension:<br>
&gt;<br>
&gt; {-# LANGUAGE ScopedTypeVariables #-}<br>
<br>
</div>Just use &#39;asTypeOf&#39;. It is defined as<br>
<br>
&gt; asTypeOf :: a -&gt; a -&gt; a<br>
&gt; asTypeOf = const<br>
<br>
so that @asTypeOf x y == x@ but both types are constrained to be<br>
equal.  The above function would become<br>
<div><br>
&gt; randomEnum :: (Enum a, Bounded a, RandomGen g) =&gt; Rand g a<br>
&gt; randomEnum = do<br>
</div>&gt;     let min = minBound; max = maxBound<br>
&gt;     randVal &lt;- getRandomR (fromEnum min, fromEnum max)<br>
&gt;     return $ toEnum randVal `asTypeOf` min `asTypeOf` max<br>
<br>
Note that I use the fact that &#39;return&#39; is constrained to the type<br>
variable &#39;a&#39; we want to constrain its argument, and the<br>
&#39;asTypeOf&#39; constrains everything to be of the same type.<br>
<br>
HTH,<br>
<br>
--<br>
<font color="#888888">Felipe.<br>
</font><div><div></div><div></div></div></blockquote></div><div><br>Interesting alternative. However, I think the ScopedTypeVariables looks a little bit cleaner. I&#39;ll keep the asTypeOf in mind for the future though.<br>

<br>Michael <br></div></div><br></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>