[Haskell-cafe] is closing a class this easy?

Conor McBride conor at strictlypositive.org
Fri Jul 17 11:38:36 EDT 2009


Friends

Is closing a class this easy?

--------------------------------------

module Moo
   (  Public(..)
   )  where

class Private x => Public x where
   blah :: ...

class Private x where

instance Private A where
instance Public A where
   blah = ...

instance Private B where
instance Public B where
   blah = ...

--------------------------------------

Modules importing Moo get Public and its instances,
but cannot add new ones: any such instances must be
accompanied by Private instances, and Private is
out of scope.

Does this work? If not, why not? If so, is this well
known?

It seems to be just what I need for a job I have in
mind. I want a class with nothing but hypothetical
instances. It seems like I could write

--------------------------------------

module Noo
   (  Public(..)
   ,  public
   )  where

class Private x => Public x where
   blah :: ...
   blah = ...

class Private x where

public :: (forall x. Public x => x -> y) -> y
public f = f Pike

data Pike = Pike
instance Private Pike
instance Public Pike

--------------------------------------

But if I don't tell 'em Pike, I've ensured that
blah can only be used in the argument to public.

Or is there a hole?

Cures youriously

Conor



More information about the Haskell-Cafe mailing list