Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 2nd, 2012  |  syntax: None  |  size: 0.67 KB  |  views: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is it possible to force a type to a class in Haskell?
  2. module T where
  3. import Debug.Trace
  4.  
  5. dosum :: (Num a) => [a] -> a
  6. dosum xs = dosum' 0 xs
  7.     where
  8.         dosum' n [] = n
  9.         dosum' n (x:xs) = trace (show n) $ dosum' (n+x) xs
  10.        
  11. Could not deduce (Show a) arising from a use of dosum'
  12. from the context (Num a)
  13.        
  14. unsafeShow :: a -> String
  15.        
  16. class (Num a) => Number a
  17. instance (Num a) => Number a
  18.        
  19. -- original function, should add NOINLINE to make sure your rule gets a chance to fire.
  20. {-# NOINLINE dosum #-}
  21. dosum :: (Num a) => [a] -> a
  22.  
  23. -- a version with added debugging
  24. dosumShow :: (Num a, Show a) => [a] -> a
  25.  
  26. {-# RULES "sub/dosum" forall x. dosum x = dosumShow x #-}
clone this paste RAW Paste Data