X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Freport.git;a=blobdiff_plain;f=Chapters%2FFuture.tex;h=38d618ff89906090885a9f359ee97e36dfbfc193;hp=4eb297d7e9c3d2b357942bfec82359017a263699;hb=19c17205efa182b80916caa31afeadad9d2dd5b5;hpb=6b45e9cc13a588fe45096dfa9e8b54c606ed3c32 diff --git a/Chapters/Future.tex b/Chapters/Future.tex index 4eb297d..38d618f 100644 --- a/Chapters/Future.tex +++ b/Chapters/Future.tex @@ -47,13 +47,40 @@ will be desugared into: (somefunc a) >> (otherfunc b) \stophaskell -\todo{Properly introduce >>=} -There is also the \hs{>>=} operator, which allows for passing variables from -one expression to the next. If we could use this notation to compose a -stateful computation from a number of other stateful functions, this could -move all the boilerplate code into the \hs{>>} operator. Perhaps the compiler -should be taught to always inline the \hs{>>} operator, but after that there -should be no further changes required to the compiler. +The main reason to have the monadic notation, is to be able to wrap +results of functions in a datatype (the \emph{monad}) that can contain +extra information, and hide extra behaviour in the binding operators. + +The \hs{>>=} operator allows extracting the actual result of a function +and passing it to another function. Let's try to illustrate this from an +example. The following snippet: + +\starthaskell +do + x <- somefunc a + otherfunc x +\stophaskell + +will be desugared into: + +\starthaskell +(somefunc a) >>= (\\x -> otherfunc x) +\stophaskell + +The \hs{\\x -> ...} notation creates a lambda abstraction in Haskell, +that binds the \hs{x} variable. Here, the \hs{>>=} operator is supposed +to extract whatever result somefunc has and pass it to the lambda +expression created. This will probably not make the monadic notation +completely clear to a reader without prior experience with Haskell, but +it should serve to understand the following discussion. + +The monadic notation could perhaps be used to compose a number of +stateful functions into another stateful computation. Perhaps this could +move all the boilerplate code into the \hs{>>} and \hs{>>=} operators. +Because the boilerplate is still there (it's not magically disappeared, +just moved into these functions), the compiler should still be able to compile +these descriptions without any special magic (though perhaps it should +always inline the binding operators to reveal the flow of values). This is highlights an important aspect of using a functional language for our descriptions: We can use the language itself to provide abstractions of common