Thomas Conway wrote:
> I'm having some difficulty with typeclasses.
> 
> What I'm trying to do should be obvious, but it's still giving me
> trouble. I want to take a packaged item, and strengthen the
> constraints on its type. Rather than being just any type that is an
> instance of A, I want to do a runtime check and do something different
> if it is a type that is also an instance of B (which implies that it
> is an instance of A also).

  It is not possible to check at run time, whether an instance of some
class is available for a given value. If a constraint is not mentioned
at compile time, the dictionary for that constraint will not be included
at run time, so there will be nowhere to get it from (unlike in Java,
where every object "carries" along all the dictionaries that it implements).

  Having said that there is a hack/technique that I am using myself -
maybe it will help you. It simulates something similar to what Java
does. I rewrote your program to use it - result attached here.
  The idea is to have an existential box for each class so that this box
carries the dictionary at run time. We call such a box an "interface"
and provide for our values a runtime list of interfaces that they
implement. This list is available as a function of the Interfaced class.

  You might also look at the paper "Scrap your boilerplate with class:
extensible generic functions" by Ralf Lämmel and Simon Peyton-Jones.
Among other things they describe existential boxes that can be
parametrized with the context that they carry.

Cheers,
  Misha
{-# OPTIONS_GHC -fglasgow-exts #-}

module Interface (

  Interfaced(..),
  Interface(..),
  getInterface

) where

import Data.Typeable

import Control.Monad (msum)

class Typeable a => Interfaced a where
  interfaces :: a -> [Interface]
  interfaces = const []

data Interface = forall box . Typeable box => Interface box

getInterface :: (Interfaced a, Typeable interface) => a -> Maybe interface
getInterface = msum . (map checkInterface) . interfaces where
  checkInterface (Interface interface) = cast interface
{-# OPTIONS_GHC -fglasgow-exts #-}

import Data.Typeable
import Interface

import Control.Monad

class Typeable a => A a where
    baz :: a -> IO ()

data A_box = forall a . A a => A_box a deriving Typeable

class A a => B a where
    qux :: a -> IO ()

data B_box = forall a . B a => B_box a deriving Typeable

{-
  returns @Just qux a@, if B constraint is implemented by a,
  otherwise @Just baz a@, if A constraint is implemented by a,
  otherwise @[EMAIL PROTECTED]
-}
tryit :: Interfaced a => a -> Maybe (IO ())
tryit a = 
  (do
    B_box a' <- getInterface a
    return $ qux a')
  `mplus`
  (do
    A_box a' <- getInterface a
    return $ baz a')

data T = T deriving Typeable

instance A T where
  baz _ = putStrLn "baz"

instance B T where
  qux _ = putStrLn "qux"

instance Interfaced T where
  interfaces a = [Interface (A_box a), Interface (B_box a)]

main = 
  case tryit T of
    Just io -> io
    Nothing -> putStrLn "no interface implemented"
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to