Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. return <=< x = x
  2.  
  3. seq (return <=< undefined :: a -> Identity b) () = ()
  4. seq (undefined :: a -> Identity b) () = undefined
  5.  
  6. seq (return <=< undefined :: a -> Maybe b) () = ()
  7. seq (undefined :: a -> Maybe b) () = undefined
  8.  
  9. -- in GHC.Types (ghc-prim)
  10. newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
  11.  
  12. -- in GHC.Base (base)
  13. instance Monad IO where
  14. {-# INLINE return #-}
  15. {-# INLINE (>>) #-}
  16. {-# INLINE (>>=) #-}
  17. m >> k = m >>= _ -> k
  18. return = returnIO
  19. (>>=) = bindIO
  20. fail s = failIO s
  21.  
  22. returnIO :: a -> IO a
  23. returnIO x = IO $ s -> (# s, x #)
  24.  
  25. bindIO :: IO a -> (a -> IO b) -> IO b
  26. bindIO (IO m) k = IO $ s -> case m s of (# new_s, a #) -> unIO (k a) new_s
  27.  
  28. thenIO :: IO a -> IO b -> IO b
  29. thenIO (IO m) k = IO $ s -> case m s of (# new_s, _ #) -> unIO k new_s
  30.  
  31. unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #))
  32. unIO (IO a) = a
  33.  
  34. return x >>= f ≡ f x:
  35. return x >>= f = IO $ s -> case (t -> (# t, x #)) s of
  36. (# new_s, a #) -> unIO (f a) new_s
  37. = IO $ s -> case (# s, x #) of
  38. (# new_s, a #) -> unIO (f a) new_s
  39. = IO $ s -> unIO (f x) s
  40.  
  41. m >>= return ≡ m:
  42. (IO k) >>= return = IO $ s -> case k s of
  43. (# new_s, a #) -> unIO (return a) new_s
  44. = IO $ s -> case k s of
  45. (# new_s, a #) -> (t -> (# t, a #)) new_s
  46. = IO $ s -> case k s of
  47. (# new_s, a #) -> (# new_s, a #)
  48. = IO $ s -> k s
  49.  
  50. m >>= (x -> g x >>= h) ≡ (m >>= g) >>= h:
  51. (IO k) >>= (x -> g x >>= h) = IO $ s -> case k s of
  52. (# new_s, a #) -> unIO ((x -> g x >>= h) a) new_s
  53. = IO $ s -> case k s of
  54. (# new_s, a #) -> unIO (g a >>= h) new_s
  55. = IO $ s -> case k s of
  56. (# new_s, a #) -> (t -> case unIO (g a) t of
  57. (# new_t, b #) -> unIO (h b) new_t) new_s
  58. = IO $ s -> case k s of
  59. (# new_s, a #) -> case unIO (g a) new_s of
  60. (# new_t, b #) -> unIO (h b) new_t
  61. ((IO k) >>= g) >>= h = IO $ s -> case (t -> case k t of
  62. (# new_s, a #) -> unIO (g a) new_s) s of
  63. (# new_t, b #) -> unIO (h b) new_t
  64. = IO $ s -> case (case k s of
  65. (# new_s, a #) -> unIO (g a) new_s) of
  66. (# new_t, b #) -> unIO (h b) new_t
  67.  
  68. case (case e of case e of
  69. pat1 -> ex1) of ≡ pat1 -> case ex1 of
  70. pat2 -> ex2 pat2 -> ex2
  71.  
  72. m >>= return ≡ m
  73.  
  74. Prelude> seq ( undefined >>= return :: IO () ) "hello, world"
  75. "hello, world"
  76.  
  77. Prelude> seq ( undefined :: IO () ) "hello, world"
  78. *** Exception: Prelude.undefined
  79.  
  80. Prelude> seq ( undefined >>= return :: Maybe () ) "hello, world"
  81. *** Exception: Prelude.undefined
  82.  
  83. Prelude> seq ( undefined :: Maybe () ) "hello, world"
  84. *** Exception: Prelude.undefined
Add Comment
Please, Sign In to add comment