Return to Snippet

Revision: 11422
at February 4, 2009 16:38 by narkisr


Initial Code
-- Enabling the extension
{-# OPTIONS -fglasgow-exts #-} 

--  Equal like class
class  MEEq a  where
   (=-=)   :: a -> a -> Bool

-- Instances with no implementation
instance MEEq ()
instance MEEq Bool
instance MEEq Int

-- The rewrite rule
{-# RULES
    "eq/Bool"   (=-=) = eq_Bool :: Bool -> Bool -> Bool
    "eq/Unit"   (=-=) = eq_Unit :: ()   -> ()   -> Bool
    #-}

main = do
       print $ True =-= False
       print $ ()   =-= ()
       -- This fails (at runtime!) since we didn't define a rule for it
       print $ 7    =-= (8 :: Int)

-- Makes sure to fail any instance that doesn't have an implementation on compile time
{-# RULES
    "Unable to resolve instance resolution" (=-=) = (=-=)
      #-}

Initial URL

                                

Initial Description
Haskell uses static dispatch for overloaded methods, in addition it has an extension to write rules that optimize Haskell dispatching code.

Initial Title
Haskell static dispatch rewrite rules

Initial Tags

                                

Initial Language
Java