<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Mar 10, 2014 at 8:59 AM, Derek McLoughlin <span dir="ltr"><<a href="mailto:derek.mcloughlin@gmail.com" target="_blank">derek.mcloughlin@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">ghci> distance (x, x) $ toVector (y, y)<br>
12.727922061357855<br>
<br>
I don't understand why this doesn't work:<br>
<br>
ghci> let z = toVector (y, y)<br>
interactive>:32:9:<br>
    No instance for (Vectorizable (Double, Double) v0)<br>
      arising from a use of `toVector'<br>
    The type variable `v0' is ambiguous<br>
    Possible fix: add a type signature that fixes these type variable(s)<br>
    Note: there is a potential instance available:<br>
      instance Vectorizable (Double, Double) (Double, Double)<br>
        -- Defined at vector.hs:13:10<br>
    Possible fix:<br>
      add an instance declaration for (Vectorizable (Double, Double) v0)<br>
    In the expression: toVector (y, y)<br>
    In an equation for `z': z = toVector (y, y)<br>
<br>
It seems odd that "toVector" works when used as an argument to<br>
"distance" but not when used in a let expression.<br>
<br>
Can anyone explain?<br></blockquote></div><div class="gmail_extra"><br></div>When you use them together, Haskell can infer the correct type for `v0` from the inferred type of `distance` by applying defaulting: the type of `sqrt` introduces a constraint that is subject to defaulting. If you separate them, it can no longer determine a concrete type for your `let`, as multiparameter type classes are not subject to defaulting (and can't be). Also, Haskell cannot conclude from the existence of a single possible instance that it should use that instance: the meaning of your expression would then change if you imported a module which defined a new instance.<br clear="all">
<div><br></div><div>The requirement for a concrete type comes from the monomorphism restriction, which is applied to your `let` because `z` has no parameters.</div><div><br></div><div><a href="http://www.haskell.org/haskellwiki/Monomorphism_restriction">http://www.haskell.org/haskellwiki/Monomorphism_restriction</a><br>
</div><div><br></div><div>Many people turn off the monomorphism restriction in ghci to avoid this kind of issue.</div><div><br></div><div>    :set -XNoMonomoprhismRestriction</div><div><br></div><div>which you can put in ~/.ghci so that it is the default for new ghci sessions.</div>
<div><br></div>-- <br><div dir="ltr"><div>brandon s allbery kf8nh                               sine nomine associates</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>                                  <a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div>
<div>unix, openafs, kerberos, infrastructure, xmonad        <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div></div>
</div></div>