SHARE
TWEET

Untitled

a guest Aug 2nd, 2012 11 Never
  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 #-}
RAW Paste Data
Top