functions not in type classes

Ashley Yakeley ashley@semantic.org
Fri, 18 Jan 2002 16:59:29 -0800


At 2002-01-18 03:06, Cagdas Ozgenc wrote:

>Same story, you shouldn't have had a Container of Char only in the first
>place.

Well, what about a file-handle? Not necessarily exactly a Container 
(though it might be), but it's always "of Char", or better yet, "of 
Word8". You could make a generalised file-handle to be of anything, but 
your read and write functions would necessarily have to be restricted.

For instance, given this:

  class Stream1 c where
    read :: c a -> Integer -> IO [a]
    write :: c a -> [a] -> IO ()

  data FileHandle1 a = ...

  fileRead :: FileHandle1 Word8 -> Integer -> IO [Word8]
  fileWrite :: FileHandle1 Word8 -> [Word8] -> IO ()

...you cannot make FileHandle1 an instance of Stream1.

However, you could do this:

  class Stream2 s a | s -> a where
    read :: s -> Integer -> IO [a]
    write :: s -> [a] -> IO ()

  data FileHandle2 = ...

  fileRead :: FileHandle2 -> Integer -> IO [Word8]
  fileWrite :: FileHandle2 -> [Word8] -> IO ()

  instance Stream2 FileHandle2 Word8 where
    read = fileRead
    write = fileWrite


-- 
Ashley Yakeley, Seattle WA