Sunday, January 19, 2014

Using Monads in Haskell

Haskell is a bit of a quirky language, to say the least. One of the most talked about features of Haskell is the monad, which is a minimal context that essentially "wraps" a value. Monads are not a part of Haskell itself, but rather a ubiquitous use of the type system. All monads must obey the three Monadic laws of Left Identity, Right Identity, and Associativity. Academics can argue these laws all day, and with good reason, but what about a real programmer more interested in getting real work done than writing proofs for their code? This is a quick run-down to teach those types how they can use monads in their code.

Do Notation
The main way to handle monads is do-notation. It can "unwrap" monads easily, with do and arrows. Here is an example with the IO monad, inside of the main function.

main = do
  fileHandle <- openFile "test.txt" ReadMode
  x <- hGetContents fileHandle
  let x-lines = lines x
  return ()

In this block of code, the Handle is "extracted" from the IO Monad in the second line of code. <- will take a Monad, and bind its wrapped value to a named value. Note that this is still lazily evaluated. Afterwards, a new String is taken from an IO (String) and put into x. The next important line, "return ()", uses one of monad's core methods. This is because main must return an IO monad. However, you can see that it is inefficient to bind so many variables. So, here is how to use another one of Monad's methods, >>=, to chain actions together.

main = do
  xlines <- openFile "test.txt" ReadMode >>= hGetContents
  return ()

This is such an essential usage of Monads that >>= is the unofficial symbol representing Haskell. As you can see, the result of the openFile command is "pushed" into hGetContents. It's important to note that a monad is going into hGetContents, and a monad is coming out. <- is what unbinds it.

Functors and Monads
So, you still have to use the <- to "de-monad" the monad you get before it can be used, right? Wrong. There is a typeclass called fmap, which is generally applicable to everything. One of the monad laws dictates the behavior of fmap, so it damn well better be able to manipulate our monads. Here is the program once again rewritten:

main = fmap putStrLn $ openFile "test.txt" ReadMode >>= hGetContents

Here, fmap is a function with the type signature of "String -> IO ()", applied to the result of hGetContents on the monad pushed into it by the openFile. fmap maps putStrLn over the results, and the IO is returned as a result. Nifty? Nifty.

Applicatives
So, the last piece of the puzzle is this: You have multiple monads, and you want to add the result of them without binding temporary variables. This can be done with Applicative Functors. They are like Functors, but with some added goodies. To use Applicative functors, include Control.Applicative and test out this chunk of code:

main = fmap putStrLn $ pure (++) <*> (openFile "test.txt" ReadMode >>= hGetContents) <*> (openFile "test2.txt" ReadMode >>= hGetContents)

This will take the ++ function, turn it into one which can work with Applicatives, and then apply it partially over the <*> elements. If the function can already handle Applicatives, then <$> can be used instead.

In Conclusion
Monads were a tricky subject for me for a long time. Most of my programs look like my first chunk of code, where I did one monadic operation per line. However, learning to master Monads in Haskell to control side-effects and efficienty keep track of your state is of utmost importance in commanding the language. So go on, don't pick up that monad. Wear that >>= with pride.

2 comments:

  1. The user should open the Hotmail official website on the browser after that the user should click the sign-up button given in the left side of the page further enter a username of choice then enter a password twice, also further the user should enter a phone number and an alternate email ID for recovery purposes. If needed then the user can connect with the certified team of Hotmail technicians as and when needed.
    Hotmail Support Number UK

    ReplyDelete
  2. Do You Need A Loan To Consolidate Your Debt At 0.5% ? ValidusCapitals@gmail.com Or A Personal Loans * Business Loans etc. Interested Applicants Should Contact Us For More Information At Via Email: ( ValidusCapital@execs.com ) ValidusCapital@techie.com

    Do You Seek Funds To Pay Off Credits and Debts ? IFSCapitalloan@gmail.com Is Here To Put A Stop To Your Financial Problems. We Offer All Kinds Of Loan ( Personal Loan, Commercial Loan, etc. ) We Give Out Loan With An Interest Rate Of 0.5%. Interested Applicants Should Contact Us Via Email: IFSCapital@post.com

    We Offers Financial Consulting To Clients, Companies Seeking Debt / Loan Financing And Seeking For Working Capital To Start A New Business Or To Expand Existing Business. Interested Parties Should Contact Us For More Information Through Via E-mail: SuiteCapitals@gmail.com
    ( SuiteCapitals@post.com )

    Are You A Business Man Or Woman ? Are You In Any Financial Mess ? Do You Need Funds To Start Up Your Own Business ? Do You Need A Loan To Start A Nice Small Scale And Medium Business ? Do You Have A Low Credit Score And You Are Finding It Hard To Obtain Capital Loan From Local Banks And Other Financial Institutes ? etc. Interested Applicants Should Contact Us For More Information At Via Email: CYSCredits@Asia.com ( CYSCredits@Post.com )

    RTSFinancialService@gmail.com Is Offering Its Global Fund Program, Where Loans Are Offered To Interested Clients Globally To Fund Profitable Business And Projects. Interested Applicants Should Contact Us For More Information Through Via Email: RTSFinancialService@gmail.com

    Do You Need A Loan To Consolidate Your Debt At 0.5% ? Or A Personal Loans * Business Loans etc. Interested Applicants Should Contact Us For More Information Through Via Email: RMPCapitals@gmail.com ( RMPCapitals@usa.com )

    Do You Need A Loan To Consolidate Your Debt At 0.5% ? ValidusCapitals@gmail.com Or A Personal Loans * Business Loans etc. Interested Applicants Should Contact Us For More Information At Via Email: ( ValidusCapital@execs.com ) ValidusCapital@techie.com

    Do You Seek Funds To Pay Off Credits and Debts ? IFSCapitalloan@gmail.com Is Here To Put A Stop To Your Financial Problems. We Offer All Kinds Of Loan ( Personal Loan, Commercial Loan, etc. ) We Give Out Loan With An Interest Rate Of 0.5%. Interested Applicants Should Contact Us Via Email: IFSCapital@post.com

    We Offers Financial Consulting To Clients, Companies Seeking Debt / Loan Financing And Seeking For Working Capital To Start A New Business Or To Expand Existing Business. Interested Parties Should Contact Us For More Information Through Via E-mail: SuiteCapitals@gmail.com
    ( SuiteCapitals@post.com )

    Are You A Business Man Or Woman ? Are You In Any Financial Mess ? Do You Need Funds To Start Up Your Own Business ? Do You Need A Loan To Start A Nice Small Scale And Medium Business ? Do You Have A Low Credit Score And You Are Finding It Hard To Obtain Capital Loan From Local Banks And Other Financial Institutes ? etc. Interested Applicants Should Contact Us For More Information At Via Email: CYSCredits@Asia.com ( CYSCredits@Post.com )

    RTSFinancialService@gmail.com Is Offering Its Global Fund Program, Where Loans Are Offered To Interested Clients Globally To Fund Profitable Business And Projects. Interested Applicants Should Contact Us For More Information Through Via Email: RTSFinancialService@gmail.com

    Do You Need A Loan To Consolidate Your Debt At 0.5% ? Or A Personal Loans * Business Loans etc. Interested Applicants Should Contact Us For More Information Through Via Email: RMPCapitals@gmail.com ( RMPCapitals@usa.com )

    ReplyDelete