fork download
  1. data DataOrError a b = Error a | Data b deriving Show
  2.  
  3. apply f (Data x) (Data y) = Data (f x y)
  4. apply f (Data x) y@(Error _) = y
  5. apply f x@(Error _) _ = x
  6.  
  7. main = do
  8. print (apply (+) x1 x2) -- (1)
  9. print (apply (+) x1 e2) -- (2)
  10. print (apply (+) e1 x2) -- (3)
  11. print (apply (+) e1 e2) -- (4)
  12. where
  13. x1 = Data (2 :: Int)
  14. x2 = Data (3 :: Int)
  15. e1 = Error ("First thing failed")
  16. e2 = Error ("Second thing failed")
  17.  
Compilation error #stdin compilation error #stdout 0s 6284KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:8:3:
    No instance for (Show a0) arising from a use of `print'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance (Show a, Show b) => Show (DataOrError a b)
        -- Defined at prog.hs:1:50
      instance Show Double -- Defined in `GHC.Float'
      instance Show Float -- Defined in `GHC.Float'
      ...plus 24 others
    In a stmt of a 'do' block: print (apply (+) x1 x2)
    In the expression:
      do { print (apply (+) x1 x2);
           print (apply (+) x1 e2);
           print (apply (+) e1 x2);
           print (apply (+) e1 e2) }
    In an equation for `main':
        main
          = do { print (apply (+) x1 x2);
                 print (apply (+) x1 e2);
                 print (apply (+) e1 x2);
                 .... }
          where
              x1 = Data (2 :: Int)
              x2 = Data (3 :: Int)
              e1 = Error ("First thing failed")
              e2 = Error ("Second thing failed")
stdout
Standard output is empty