Posted By

narkisr on 02/04/09


Tagged

rules rewrite haskell


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

keigoi


Haskell static dispatch rewrite rules


 / Published in: Java
 

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

  1. -- Enabling the extension
  2. {-# OPTIONS -fglasgow-exts #-}
  3.  
  4. -- Equal like class
  5. class MEEq a where
  6. (=-=) :: a -> a -> Bool
  7.  
  8. -- Instances with no implementation
  9. instance MEEq ()
  10. instance MEEq Bool
  11. instance MEEq Int
  12.  
  13. -- The rewrite rule
  14. {-# RULES
  15. "eq/Bool" (=-=) = eq_Bool :: Bool -> Bool -> Bool
  16. "eq/Unit" (=-=) = eq_Unit :: () -> () -> Bool
  17. #-}
  18.  
  19. main = do
  20. print $ True =-= False
  21. print $ () =-= ()
  22. -- This fails (at runtime!) since we didn't define a rule for it
  23. print $ 7 =-= (8 :: Int)
  24.  
  25. -- Makes sure to fail any instance that doesn't have an implementation on compile time
  26. {-# RULES
  27. "Unable to resolve instance resolution" (=-=) = (=-=)
  28. #-}

Report this snippet  

You need to login to post a comment.