Skip to content

Instantly share code, notes, and snippets.

@takoeight0821
Last active November 3, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takoeight0821/ef39222a2f7e18a3aeddc4ec238121d6 to your computer and use it in GitHub Desktop.
Save takoeight0821/ef39222a2f7e18a3aeddc4ec238121d6 to your computer and use it in GitHub Desktop.
エキゾチックな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