[Haskell-cafe] Re: working Edison, a couple of collection modules,
and typing troubles
oleg at pobox.com
oleg at pobox.com
Thu May 26 22:42:28 EDT 2005
> This also doesn't seem like it would work very well with making an
> instance for IntMap.
> I guess I can't have everything.
It is not that difficult to make instances for IntMap:
> data TypeCast k IM.Key => WIM k e = WIM (IM.IntMap e) deriving Show
> instance Collection (WIM IM.Key e) (IM.Key,e) where
> empty = WIM $ IM.empty
> fromList = WIM . IM.fromList
> size (WIM x) = IM.size x
> instance Mapping WIM IM.Key e where
> lookup k (WIM im) = maybe (fail "not found") return (IM.lookup k im)
> -- verbatim from the HList paper
> class TypeCast a b | a -> b, b->a where typeCast :: a -> b
> class TypeCast' t a b | t a -> b, t b -> a where typeCast' :: t->a->b
> class TypeCast'' t a b | t a -> b, t b -> a where typeCast'' :: t->a->b
> instance TypeCast' () a b => TypeCast a b where typeCast x = typeCast' () x
> instance TypeCast'' t a b => TypeCast' t a b where typeCast' = typeCast''
> instance TypeCast'' () a a where typeCast'' _ x = x
It seems that restricted data type WIM k e is better than GADT
approach shown yesterday. We define `WIM k e' in such a way that the type
parameter 'k' is restricted to satisfy a particular constraint, being
equal to IM.Key in our case. If the compiler cannot see that this is
the case, the compiler will raise an error. For example, one may try
instance Collection (WIM k e) (k,e) where ...
and see what happens. So, the constraint enforcement in our case
happens statically and quite early.
> What do you suggest that I do for map? What sort of class should it be
Functor. The similarity of the names `map' and `Mapping' is confusing;
the two describe different concepts in the framework at hand.
More information about the Haskell-Cafe