You need to enable ScopedTypeVariables, and add a forall to introduce the type variable at the top level. The local variable will then be the *same* 'a' instead of a fresh one:<br><br> {-# LANGUAGE ScopedTypeVariables #-}<br>
<br> data D a = D1 a | D2 a (a -> a)<br>
<br>
f :: forall a. Eq a => D a -> a<br>
f (D1 x) = x<br>
f (D2 x g) = let y :: Eq a => a<br>
y = g x<br>
in if x == y then x else g y<br>
<br>
main = putStr $ shows (f (D2 (1 :: Int) succ)) "\n"<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 14, 2012 at 1:03 PM, Serge D. Mechveliani <span dir="ltr"><<a href="mailto:mechvel@botik.ru" target="_blank">mechvel@botik.ru</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Please,<br>
how to correctly set an explicit type for a local value in the body of<br>
a polymorphic function?<br>
<br>
Example (tested under ghc-7.6.1):<br>
<br>
data D a = D1 a | D2 a (a -> a)<br>
<br>
f :: Eq a => D a -> a<br>
f (D1 x) = x<br>
f (D2 x g) = let -- y :: Eq a => a<br>
y = g x<br>
in if x == y then x else g y<br>
<br>
main = putStr $ shows (f (D2 (1 :: Int) succ)) "\n"<br>
<br>
<br>
This is compiled by ghc --make Main<br>
<br>
Now I need, for a certain reason, to explicitly set the type for y in<br>
`let', with the meaning:<br>
"this very `a' which is in the signature for f"<br>
(and I think that this type Haskell assignes to y in "y = g x").<br>
<br>
I need to declare this type in a separate line: y :: <what ever it is>.<br>
<br>
Both `y :: a' and `y :: Eq a => a' are not compiled.<br>
<br>
Please, copy the answer to <a href="mailto:mechvel@botik.ru">mechvel@botik.ru</a><br>
<br>
Thanks,<br>
<br>
------<br>
Sergei<br>
<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>
</blockquote></div><br></div>