Skip to content

Instantly share code, notes, and snippets.

@takoeight0821

takoeight0821/Main.hs

Last active Nov 3, 2019
Embed
What would you like to do?
エキゾチックなNumインスタンス
module Main where
newtype IODouble = IODouble { runIODouble :: IO Double }
instance Num IODouble where
fromInteger x = IODouble $ do
print x
pure $ fromInteger x
(IODouble x) + (IODouble y) = IODouble $ do
x' <- x
y' <- y
putStrLn $ "(+ " <> show x' <> " " <> show y' <>")"
pure (x' + y')
(IODouble x) * (IODouble y) = IODouble $ do
x' <- x
y' <- y
putStrLn $ "(* " <> show x' <> " " <> show y' <>")"
pure (x' * y')
abs (IODouble x) = IODouble $ do
x' <- x
putStrLn $ "(abs" <> show x' <> ")"
pure (abs x')
signum (IODouble x) = IODouble $ do
x' <- x
putStrLn $ "(signum " <> show x' <> ")"
pure (signum x')
negate (IODouble x) = IODouble $ do
x' <- x
putStrLn $ "(negate " <> show x' <> ")"
pure (negate x')
instance Fractional IODouble where
fromRational x = IODouble $ do
print x
pure $ fromRational x
recip (IODouble x) = IODouble $ do
x' <- x
putStrLn $ "(recip " <> show x' <> ")"
pure (recip x')
main = do
x <- runIODouble $ 10 / 3
print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.