Last active
November 3, 2019 12:37
-
-
Save takoeight0821/ef39222a2f7e18a3aeddc4ec238121d6 to your computer and use it in GitHub Desktop.
エキゾチックなNumインスタンス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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