Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Monad laws
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== The monad laws in practice == If we re-write the laws using Haskell's <code>do</code>-notation: {| |- | ''Left identity:'' | <haskell> do xβ² <- return x f xβ² </haskell> | β‘ | <haskell> do f x </haskell> |- | ''Right identity:'' | <haskell> do x <- m return x </haskell> | β‘ | <haskell> do m </haskell> |- | ''Associativity:'' | <haskell> do y <- do x <- m f x g y </haskell> | β‘ | <haskell> do x <- m do y <- f x g y </haskell> | β‘ | <haskell> do x <- m y <- f x g y </haskell> |} we can see that the laws represent plain, ordinary common-sense transformations of imperative programs. === But why should monadic types satisfy these laws? === ---- When we see a program written in a form on the left-hand side, we expect it to do the same thing as the corresponding right-hand side; and vice versa. And in practice, people do write like the lengthier left-hand side once in a while. * First example: beginners tend to write :<haskell> skip_and_get = do unused <- getLine line <- getLine return line </haskell> :and it would really throw off both beginners and veterans if that did not act like (by ''right identity''): :<haskell> skip_and_get = do unused <- getLine getLine </haskell> * Second example: Next, you go ahead and use <code>skip_and_get</code>: :<haskell> main = do answer <- skip_and_get putStrLn answer </haskell> :The most popular way of comprehending this program is by ''inlining'' (whether the compiler does or not is an orthogonal issue): :<haskell> main = do answer <- do unused <- getLine getLine putStrLn answer </haskell> :and applying associativity so you can pretend it is: :<haskell> main = do unused <- getLine answer <- getLine putStrLn answer </haskell> The associativity law is amazingly pervasive: you have always assumed it, and you have never noticed it. The associativity of a ''binary'' operator allows for any number of operands to be combined by applying the binary operator with any arbitrary grouping to get the same well-defined result, just like the result of summing up a list of numbers is fully defined by the binary <code>(+)</code> operator no matter which parenthesization is used (yes, just like in folding a list of any type of monoidal values). Whether compilers make use of them or not, you still want the laws for your own sake, just so you can avoid pulling your hair out over counter-intuitive program behaviour, which depends (in brittle fashion!) on e.g. how many redundant <code>return</code>s you insert or how you nest your <code>do</code>-blocks... [[Category:Standard_classes]] [[Category:Monad]]
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width