Wanted: local data, class, instance declarations
Johannes Waldmann
waldmann at imn.htwk-leipzig.de
Fri Jan 27 05:00:42 EST 2006
in today's Haskell, all data and instance declarations are global,
which sometimes leads to violations of the information hiding principle.
E. g. if there is a function sort :: Ord a => [a] -> [a],
and I want to sort a list of items w.r.t. a specified ordering,
then I would want to write
let instance Ord Item where ...
xs :: [ Item ] ; xs = ...
in sort xs
but I don't want to make the instance Ord Item global
because I might need a different instance in another place,
or there already is a global instance but I don't want it.
So, the local instance should override an outer or global one.
Shouldn't be too difficult, the compiler just has to insert the proper
dictionary (the "most local" one that's visible at the call site).
With current all-global instances, a sort function of the given type is
not very flexible, leading to supplementary functions (sortBy),
see "Generalized functions" e. g. in
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#22
This is exactly a work-around for not being able
to make local dictionaries, right?
Best regards,
--
-- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 --
---- http://www.imn.htwk-leipzig.de/~waldmann/ -------
More information about the Haskell-prime
mailing list