Using Set: Hypergraph type

Tom Moertel tom-list-haskell-cafe@moertel.com
Tue, 04 Sep 2001 14:00:25 -0400


"Eray Ozkural (exa)" wrote:
> 
> Okay, I guess I got it now. The (types of) elements of a Set ought to be
> instances of Ord.

Yes, that's right.  It's fairly easy to provide the required instances:

    module Hypergraph where
    import Set

    type Hypergraph a = Set (Set a)

    instance Ord a => Ord (Set a) where
        compare x y = compare (setToList x) (setToList y)

    hgraph :: Ord a => [[a]] -> Hypergraph a
    hgraph = mkSet . map mkSet

Now it works as expected:

$ ghci -package data
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 5.00.2, For Haskell
98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package std ... linking ... done.
Loading package lang ... linking ... done.
Loading package concurrent ... linking ... done.
Loading package posix ... linking ... done.
Loading package util ... linking ... done.
Loading package data ... linking ... done.

Prelude> :load Hypergraph
Compiling Hypergraph       ( Hypergraph.hs, interpreted )
Ok, modules loaded: Hypergraph.

Hypergraph> elementOf (mkSet [1]) (hgraph [ [1,2], [1] ])
True


Cheers,
Tom

P.S.  Nice to see a fellow K5'er on the Haskell lists.  (I go my
"tmoertel" on K5.)