concept context in category haskell

appears as: context, context, The context
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

The only thing the extension OverloadedStrings is responsible for is replacing every string literal in the source code with a call to the fromString method on that literal. Then it’s time for instance resolution algorithms to find the right instance and convert a String to some other type. Note, that it should be unambiguous from the context which type is expected at the position of a String literal.

ghci> runST comp1

<interactive>:4:7: error:
    • Couldn't match type ‘a’ with ‘STRef s Int’
        because type variable ‘s’ would escape its scope
      This (rigid, skolem) type variable is bound by
        a type expected by the context:
          forall s. ST s a
        at <interactive>:4:7-11
      Expected type: ST s a
        Actual type: ST s (STRef s Int)
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

Figure 14.2 shows the type classes that are defined in Haskell’s standard library. Arrows from one class to another indicate a superclass relationship. This unit has covered most of the basic type classes. In unit 3, you’ll start exploring the more abstract type classes Semigroup and Monoid, and you’ll start to see how different type classes can be from interfaces. In unit 5, you’ll look at a family of type classes—Functor, Applicative, and Monad—that provide a way to model the context of a computation. Although this last group is particularly challenging to learn, it also allows for some of Haskell’s most powerful abstractions.

In the preceding lesson, you learned how the Functor type class allows you to perform computation inside a container such as List or a context such as Maybe and IO. The key method behind Functor is fmap (more commonly, the <$> operator), which works just like map on a list. In this lesson, you’ll work with a more powerful type class called Applicative. The Applicative type class extends the power of Functor by allowing you to use functions that are themselves in a context.

Although this may not seem useful, it allows you to chain together long sequences of computation in a context such as IO or Maybe.

In your first example, you’ll see the limitations of Functor by building a command-line tool that allows the user to calculate the distance between two cities. The issue is that you need to pass two Maybe values to a function, which surprisingly Functor can’t do. You’ll then see how Applicative resolves this issue. After you learn about Applicative, you’ll see how this can help you create data in the context of either IO or Maybe, while allowing you to reuse the majority of your code.

28.2. Using <*> for partial application in a context

The problem you need to solve now is generalizing Functor’s fmap to work with multiple arguments. In lesson 5, you learned that partial application means that calling an argument with fewer arguments than it requires results in a function waiting for the remaining arguments. Then in section 10.2.2, you saw that all functions are functions of one argument. Multi-argument functions are just a chain of single-argument functions. The key to solving your problem lies in being able to perform partial application in a context such as Maybe or IO.

The real limitation of Functor’s <$> is that if you end up with a function in a context, through partial application, you have no way of using that function. For example, you can use <$>, (+), and the number 1 in a context to create a maybeInc function.

Listing 28.5. Using Functor’s <$> operator for partial application in a context
maybeInc = (+) <$> Just 1

The (+) operator is a function that takes two values; by using <$> on a Maybe value, you created a function waiting for a missing value, but it’s inside a Maybe. You now have a Maybe function, but there’s no way to apply this function! Recalling the visual language of circles and squares in context, you’ve arrived at a problem of finding an adapter for the situation illustrated in figure 28.3.

Figure 28.3. You need a new type of adapter for connecting types in a context with functions in a context.

In this lesson, our objective was to introduce you to the Applicative type class. The Applicative’s <*> operator allows you to use functions that are themselves in a context. For example, if you have a function that might not exist, Maybe (Int -> Double), you can apply it to a value in the same context, Maybe Int, and get a result still in that context, Maybe Double. This may seem like a seldom-used operator, but it’s essential to being able to extend Functor to multi-argument functions. Because of the prevalence of partial application in Haskell programs, it’s fairly common to wind up with a function in a context. Without Applicative, it’d be impossible to use these functions in many cases. Let’s see if you got this.