<div dir="ltr"><div><div>Rigid just means the compiler knows the type without having to infer it.  And if the type is inferred to be different than what you said it should be, you get an error that says 's' is  a rigid type variable bound by ... presumably the type signature.<br><br></div>I'm not sure what free means.  My best guess is that a local variable is free if it is not referred to in the type signature of the encompassing (? surrounding?) scope.<br><br></div>forall a. is just what what ScopedTypeVariables uses to ensure that local variables in the function with the same type variables are no longer free, and that they must adhere to the type signature of the encompassing scope.<br><div><br></div><div>My terminology is terrible, and I've probably made a mistake, but hopefully someone will correct me.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 7, 2017 at 10:31 AM, Baa <span dir="ltr"><<a href="mailto:aquagnu@gmail.com" target="_blank">aquagnu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello, David!<br>
<span class=""><br>
> But remember that in x, 'a' is not the same as the 'a' in f, so you<br>
<br>
</span>Exactly, and I often hit errors like "Can't match rigid type 'M a' with<br>
rigid type 'M a1'"! So, the question comes down to "forall" and<br>
"ScopedTypeVariables". Did I understand you correctly:<br>
<br>
this extension expands type-variable scope to whole function body. But<br>
what sort of type vairables? Only under "forall ...", right ?<br>
<br>
And another question: type-variables under "forall" - they are rigid?<br>
Or free? I can't understand the terminology. I thought early that they<br>
are called "rigid", but in documentation of "ScopedTypeVariables"<br>
extension they are called "free". IMHO symbol ∀ means "unbound", so<br>
"free". But I often hit errors when GHC says something about rigid, so<br>
I'm not sure - what is rigid? Bound?<br>
<br>
And last: why I need "forall a." in beginning of signature while<br>
Haskell assumes "forall" for all type variables by default? Or I'm not<br>
right here...<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
> As to your original code, the only actual bug is that someA :: a is<br>
> wrong. The actual type of someA is someA :: C a => a, so you could<br>
> have written<br>
><br>
> class C a where<br>
>   someA :: a<br>
><br>
> f :: C a => a -> Int<br>
> f a =<br>
>   let x :: C a => a<br>
>       x = someA -- absolutely works<br>
>   in 0<br>
><br>
><br>
> But remember that in x, 'a' is not the same as the 'a' in f, so you<br>
> might as well have written this<br>
><br>
>   f :: C a => a -> Int<br>
>   f a =<br>
>     let x :: C b => b<br>
>         x = someA -- also works<br>
>     in 0<br>
><br>
> This is how it works.  When you write<br>
><br>
> f :: C a => a -> ()<br>
> f =<br>
>   let x = someA  -- without ':: a'<br>
>   ()<br>
><br>
> The problem is that by not giving an explicit type to x, ghc infers a<br>
> type and for all intents and purposes it is as though you wrote<br>
><br>
> f :: C a => a -> ()<br>
> f =<br>
>   let x :: C a1 => a1<br>
>       x = someA<br>
>       -- also  if you had written x = some :: a, it just means you<br>
> effectively wrote<br>
>       -- let x :: C a1 => a1<br>
>       --     x = someA :: a<br>
>       -- but 'a1' is not equivalent to 'a', so it errors.<br>
>   ()<br>
><br>
> And so it says hey wait, a and a1 could be different types.  That is<br>
> why you often see ghc referring to 'a1' or 'a0' in error messages,<br>
> because when it infers a type it has to give a name to it in order to<br>
> report on it.<br>
><br>
> What ScopedTypeVariables does is allow you to specify that for the<br>
> entire scope of this forall, 'a' in any type signature always refers<br>
> to the same type.<br>
><br>
> f :: forall a. C a => a -> ()<br>
> f = undefined<br>
> where<br>
>   some :: a -- this is guaranteed to be the a from above.<br>
>   some = undefined<br>
><br>
>   somethingelse :: forall a. a -- this is a different a<br>
>   somethingelse = undefined<br>
><br>
> some :: a -- this toplevel a is of course also different.<br>
> some = undefined<br>
><br>
> So you could just go<br>
> {-# LANGUAGE ScopedTypeVariables #-}<br>
><br>
> f :: forall a. C a => a -> Int<br>
> f a =<br>
>   let<br>
>      x :: a  -- now the a is the same a as above, so the constraint C<br>
> a is understood.<br>
>      x = someA<br>
>   in 0<br>
><br>
> I really love the ScopedTypeVariables extension, and so do a lot of<br>
> other people.  I honestly think it should be on by default because<br>
> the pitfalls of not having it on are a lot worse than the pitfalls of<br>
> having it on.  The only pitfall I know of to having it on is that if<br>
> you have a function with tons of lets and / or wheres, you might not<br>
> notice you had used the type variable 'a' in two different<br>
> incompatible places.<br>
><br>
> Hopefully all this makes sense.<br>
><br>
><br>
><br>
> On Thu, Dec 7, 2017 at 6:41 AM, Baa <<a href="mailto:aquagnu@gmail.com">aquagnu@gmail.com</a>> wrote:<br>
><br>
> > Hello everyone!<br>
> ><br>
> > Suppose I have:<br>
> ><br>
> >   class C a where<br>
> >     someA :: a<br>
> ><br>
> >   f :: C a => a -> Int<br>
> >   f a =<br>
> >     let x = someA :: a in -- BUG!!<br>
> >       0<br>
> ><br>
> > BUG as I understand I due to `:: a` - this is another `a`, not the<br>
> > same as in `f` singature. But seems that it's the same `a` if `f`<br>
> > is the "method" of some instance. And such bug does not happen if I<br>
> > return it, so my question<br>
> > is how to create such `x` of type `a` in the body of the `f`? How<br>
> > to use `a` anywhere in the body of `f`? Is it possible?<br>
> ><br>
> > I found a way if I change signature to `forall a. C a => a -> Int`.<br>
> > But there<br>
> > are another questions in the case:<br>
> ><br>
> >  - as I know all such params are under "forall" in Haskell<br>
> > by-default. Seems<br>
> >    it's not true?<br>
> >  - in method body seems they are under "forall" but in simple<br>
> > functions - not?<br>
> >  - i'm not sure what exactly does this "forall", IMHO it unbounds<br>
> > early bound<br>
> >    type's variables (parameters) by typechecker, but how `a` can be<br>
> > bound in the<br>
> >    just begining of signature (where it occurs first time) ?!<br>
> ><br>
> > As for me, looks that w/o "forall" all `a`, `b`, `c`, etc in the<br>
> > body of simple functions always are different types. But w/<br>
> > "forall" Haskell typechecker<br>
> > makes something like (on type-level):<br>
> ><br>
> >    let a = ANY-TYPE<br>
> >    in<br>
> >       ... here `a` can occur and will be bound to ANY-TYPE<br>
> ><br>
> > Where am I right and where am I wrong? :)<br>
> ><br>
> > ===<br>
> > Best regards, Paul<br>
> > ______________________________<wbr>_________________<br>
> > Beginners mailing list<br>
> > <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> > <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
> ><br>
<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>