Thursday, February 14, 2013

HASKELL BINDING OPERATORS

HASKELL BINDING OPERATORS

REVISED: Wednesday, January 24, 2024




Haskell Binding Operators.

We will be using the following import:

Prelude> import Control.Monad
Prelude Control.Monad>

As shown below the join, (>>=), and return binding operators all produce the same results.

Prelude Control.Monad> :t join
join :: Monad m => m (m a) -> m a

Prelude Control.Monad>

Prelude Control.Monad> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
Prelude Control.Monad>


Prelude Control.Monad> :t return
return :: Monad m => a -> m a
Prelude Control.Monad>

I. BINDING OPERATORS


There are no variables in Haskell. In Haskell you never assign a variable, instead you bind a name to a value. However, you will often see the term variable used in describing Haskell programs as a result of its common usage.

In a pure functional language, a function can only read what is supplied to it in its arguments and the only way it can have an effect on the world is through the values it returns. Non-functional "IO actions," which have side effects, are hidden from the functional world of Haskell by the IO monad.

IO actions are defined within the IO monadIO actions resemble functions. IO actions do nothing when they are defined, but IO actions perform some task when they are invoked.

Monads allow us to express sequential execution of actions. We define our sequence of operations by placing IO operations inside of a monad.

Three Monad Laws
1. Left identity: return x >>= f is the same as f x
2. Right identity: m >>= return is no different than just m
3. Associativity: (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)

The “glue” combinator >>= function is pronounced "bind."

Technically each Monad in Haskell is not a type, but a "type constructor." monads are in the category theory branch of mathematics. IO is a monad, so IO actions can be combined using either the do notation or the >> chevron function which is pronounced then and the >>= bind function operations from the Monad class.

Any IO procedure has one more parameter compared to what you see in its type signature. This parameter is hidden inside the definition of the type alias IO.

Binding operators combine two IO actions, executing them sequentially. Bind f x is normally written as x  >>=  fx >>= f is the action that first performs the action x, and captures its result, passing it to f, which then computes a second action to be performed. That action is then carried out, and its result is the result of the overall computation.

The <- kleisli arrow symbol desugars to >>= bind; the =<< bind function is exactly the same as the >>= bind function except it takes its arguments in the opposite order.

Prelude>  import Control.Monad
Prelude>

Prelude> :type (=<<)
(=<<) :: Monad m => (a -> m b) -> m a -> m b
Prelude>

The >>= bind function binding operator implements sequential composition allowing us to use the result of the first action it gets passed as an additional parameter to the second one.

Prelude> :type (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
Prelude>

The >>= bind operator takes as its input a monadic value also known as an action, unpacks a regular non-monadic value from it, the details of which are specific to the particular monad, and passes that value as the input to the monadic function, which produces a monadic value action as its final result.

There are two fundamental monadic operations, one named "bind" the >>= operator and one named return.

The bind (>>=) operator is a monadic apply operator. It can be used to define a monadic composition operator, which is written (>=>) and read as fish

Prelude>  :type (>=>)
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
Prelude>

The return operator transforms regular values into monadic values. It can be used to define a function to convert regular functions into monadic functions.

Bind takes two arguments, the left and right operands, and returns a compound action. The meaning of this new action is to first perform the action to the left. The left action produces a value of type a. Then the function to the right is applied with this value, which returns a second action. Finally the second action is performed. The value it produces becomes the result of the whole compound action. It is common to use the letter k for the second argument of (>>=) bind because k stands for “continuation”.

Prelude> :type (>>)
(>>) :: Monad m => m a -> m b -> m b
Prelude>

The >> function read as chevron is a binding operator that ignores the value of its first action and returns as an overall result the result of its second action only.

Prelude> :type return
return :: Monad m => a -> m a
Prelude>

Prelude> :type fail
fail :: Monad m => String -> m a
Prelude>

The kleisli arrow function <- pronounced "drawn from", indicates that the result of an action is being bound. The way the kleisli arrow  function <- binding operator is processed is that the name on the left-hand side of the kleisli arrow  function <- becomes a parameter of subsequent operations, represented as one large IO action.

Any IO action that you write in a do statement, or use as a parameter for either the >> chevron function or the >>= bind function operators, is an expression returning a result of type IO a for some type a.

The unit type () is an empty tuple which has both a value and type of (). The unit type is similar to a null type or void in other languages.

IO is a monad, which is a type class. An instance of the monad type class, must include the bind functions (>>=) and return. The monad serves as the glue which binds together the actions in a program.

The use of the name main is important. main is defined to be the entry point of a Haskell program. main always has a type signature of main :: IO something, where something is some concrete type. By convention, we do not usually specify a type declaration for main.

If we are taking data out of an IO action, we can only take it out when we are inside another IO action. In other words, to get the value out of an IO action, you have to perform it inside another IO action by binding it to a name with the <- kleisli arrow function. Variables bound by the <- kleisli arrow function are lambda bound and are thus monomorphic. IO actions will only be performed when they are given a name of main or when they are inside a bigger IO action that was composed with a do block. Use the <- kleisli arrow function when you want to bind results of IO actions to names. Use let bindings to bind pure expressions, normal non-IO expressions, to names. Within do blocks or list comprehensions let without in introduce local bindings.

Pure function application should use let instead of the binding kleisli arrow <-.

You are always writing code in the IO monad when you are using GHCi.  GHCi evaluates the IO action, then calls show on the IO action, and prints the string to the terminal using putStrLn implicitly. 

Prelude>  :type when
when :: Monad m => Bool -> m () -> m ()
Prelude>  

when takes a Boolean value and an IO action, if that Boolean value is True, it returns the same IO action that we supplied to it. However, if it's False, it returns the return () action, an IO action that does not do anything.

Prelude> :type sequence
sequence :: Monad m => [m a] -> m [a]
Prelude>

sequence takes a list of IO actions and returns an IO action that will perform those actions one after the other. The result contained in that IO action will be a list of the results of all the IO actions that were performed.

Prelude> :type mapM
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
Prelude>

mapM takes a function and a list, and maps the function over the list, and then sequences it.

Prelude> :type mapM_
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
Prelude>

mapM_ does the same as mapM; however, mapM_ throws away the result later.

Prelude> :type forever
forever :: Monad m => m a -> m b
Prelude>

forever takes an IO action and returns an IO action that just repeats the IO action it received forever.

Prelude> :type forM
forM :: Monad m => [a] -> (a -> m b) -> m [b]
Prelude>

forM  is like mapM, only it has its parameters switched around. The first forM parameter is the list and the second one is the function to map over that list, which is then sequenced.

To get a list of the bindings currently in scope, use the :show bindings command.

A. LOCAL VARIABLES

Haskell also allows for local bindings; which means we can create values inside of a function that only that function can see. Local bindings are created by using a let in declaration. You can provide multiple declarations inside a let; however, make sure they are indented the same amount, or you will have layout problems.

The keywords to look out for here are let, which starts a block of variable declarations, and in, which ends it. Each line introduces a new local variable. The name is on the left of the =, and the expression to which it is bound is on the right.

In general, we will refer to the places within our code where we can use a name as the name's scope. If we can use a name, it is in scope, otherwise it is out of scope. If a name is visible throughout a source file, we say it is at the top level, it is global.

We can use another mechanism to introduce local variables: the where clause. The definitions in a where clause apply to the code that precedes it. It lets us direct our reader's focus to the important details of an expression, with the supporting definitions following afterwards. For example, you can use where to define a local variable scoped over multiple guarded branches.

Haskell's syntax for defining a variable looks very similar to its syntax for defining a function. This symmetry is preserved in let and where blocks; we can define local functions just as easily as local variables. 

We can also define global variables, as well as global functions, at the top level of a source file.

II. CONCLUSION


In this tutorial, you have received an introduction to the Haskell binding operators.

III. REFERENCES


Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.