[Haskell-cafe] (Feeling that) GHCi is trying to protect me from myself with overlapping instances and existential types

Viktor Dukhovni ietf-dane at dukhovni.org
Sun Feb 16 22:59:36 UTC 2020


> On Feb 16, 2020, at 2:37 PM, Juan Casanova <juan.casanova at ed.ac.uk> wrote:
> 
> I'm not that familiar with Typeable. If I understand correctly, what is
> going on here is that by pattern-matching on the FromB constructor and
> using Typeable, you are bypassing the instance checking and comparing
> two things that should not be compared. Is this a more or less correct
> understanding?

Somewhat, but "should not be compared" is not how I see it.
I prefer types in which structural equivalence is equivalence.
Types that violate this cannot be handled generically.

Typeable allows one to ask whether two terms of a-priori distinct
(polymorphic) types are in fact of the same actual type, and if so,
for example, "cast" one to the other:
	
    cast :: forall a b. (Typeable a, Typeable b) => a -> Maybe b

	The type-safe cast operation

In your example (jazzed up with Typeable and some Eq constraints)
however, it is possible to construct two terms, both of the *same*
type: Bar (Either String String), constructor "FromB" and data
"abc", which are not in fact equivalent because they're
bound to different instances of Class1.

The different instance bindings are not apparent in the types
of the terms or their data, so the reflection machinery fails
tell them apart, and yet they're not the same.  This is best
avoided IMHO.

-- 
-- 
	Viktor.



More information about the Haskell-Cafe mailing list