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"><<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>></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"><<a href="mailto:felipe.lessa@gmail.com" target="_blank">felipe.lessa@gmail.com</a>></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>
> The type inferer seems to struggle to find the type of minBound and<br>
> maxBound, and GHC asks to use a type annotation.<br>
> To only way I see how to add a type annotation here is to use a GHC<br>
> extension:<br>
><br>
> {-# LANGUAGE ScopedTypeVariables #-}<br>
<br>
</div>Just use 'asTypeOf'. It is defined as<br>
<br>
> asTypeOf :: a -> a -> a<br>
> 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>
> randomEnum :: (Enum a, Bounded a, RandomGen g) => Rand g a<br>
> randomEnum = do<br>
</div>> let min = minBound; max = maxBound<br>
> randVal <- getRandomR (fromEnum min, fromEnum max)<br>
> return $ toEnum randVal `asTypeOf` min `asTypeOf` max<br>
<br>
Note that I use the fact that 'return' is constrained to the type<br>
variable 'a' we want to constrain its argument, and the<br>
'asTypeOf' 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'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>