type class VS struct/functor

Mike Gunter m@ryangunter.com
23 Jan 2002 16:41:06 -0800


You can also export the type without exporting the constructors.  That
way "import"ers can use the type in type signatures and instance
declarations while still not being able to use anything but the
exported interface.

E.g. instead of

  Module Set
         ( emptySet
         , makeSet
         , firstOfSet
         ) where

use

  Module Set
         ( Set		-- <===========
	     , emptySet
         , makeSet
         , firstOfSet
         ) where

.  (The non-hiding export is

  Module Set
         ( Set(..)	-- <===========
	     , emptySet
         , makeSet
         , firstOfSet
         ) where
.)

This is covered in the Haskell report; see:
http://www.haskell.org/onlinereport/modules.html#abstract-types
.

	mike

> At 13:15 2002-01-22 -0500, Hongwei Xi wrote:
> ><...>
> >In Haskell, I guess that the one implemented later is always chosen.
> >Why can't I have two different implementations for an interface?
> 
> Actually, I can't think of situations where I would desire this.
> Could you please give an example?
> 
> >Another problem with Haskell classes is that there is currently
> >no way of hiding type information. For instance, suppose that
> >I want to implement a module for operations on sets but I do
> >not want to reveal what data representation I use for sets. Is
> >there a way of doing this in Haskell?
> >
> >--Hongwei
> 
> There is: Use restricted exports. Only export functions on the
> datatype, and don't export the datatype itself. For example:
> 
> -----------------------
> Module Set
>          ( emptySet
>          , makeSet
>          , firstOfSet
>          ) where
> 
> -- Not exported, only locally visible
> data Set a = EmptySet | OneElementSet a
> 
> -- Using these functions, everyone can
> -- make, change and read Set's
> emptySet :: Set a
> emptySet = EmptySet
> 
> makeSet :: a -> Set a
> makeSet x = OneElementSet x
> 
> firstOfSet :: Set a -> a
> firstOfSet EmptySet             = error "Set.firstOfSet: Empty set"
> firstOfSet (OneElementSet x)    = x
> -----------------------
> 
> Regards,
> 
> Rijk-Jan van Haaften
> 
> 
> 
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell