concept HINQ in category haskell

appears as: HINQ
Get Programming with Haskell

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

This allows you to easily combine two data sets, the teacher table and course table, to extract relevant information. You’ll build a set of tools you’ll call HINQ (borrowing its name from the similar .NET tool LINQ). HINQ will allow you to query your data relationally. You’ll make extensive use of the Monad type class to make this possible. In the end, you’ll have a query tool that

In this section, you’ll create a HINQ type that represents a query. You know that a query can be a select clause, join/data clause, and a where clause, or just the first two. This will allow you to run queries with and without _where statements. Before moving on, though, you need to make one more improvement in your _select, _where, and _join functions. Currently, these all operate on lists, but you can generalize this so they work on other monads. To fix this, you don’t need to change your code at all, only make your type signatures less restrictive. But you’ll have to add a type class constraint. The guard function works on types of the Alternative type class. Alternative is a subtype of Applicative, and includes defining an empty element for the type (a lot like Monoid). Both List and Maybe are members of Alternative, but IO isn’t. To use the Alternative type class, you need to import Control.Applicative. Here are your refactored type signatures that will extend the power of your HINQ queries.

Listing 33.16. _select, _where, and _join functions can work for all monads
_select :: Monad m => (a -> b) -> m a -> m b _where :: (Monad m, Alternative m) => (a -> Bool) -> m a -> m a _join :: (Monad m, Alternative m, Eq c) => m a -> m b -> (a -> c) -> (b -> c) -> m (a,b)

This is a great example of why writing monadic code is so useful. You start by solving your problem for just the List type. But you can make your code dramatically more generalized by changing the type signatures! If you had used list-specific functions, such as map and filter, this would require much more work to refactor. Now that your types are refactored, you can make a generic HINQ type to represent the queries you’re interested in running, as shown in figure 33.6.

Figure 33.6. Understanding the HINQ data type

This constructor uses the types of your _selector, _join, and possibly _where functions. With this data type, you can write a runHINQ function that takes a HINQ type and runs the query.

Listing 33.17. runHINQ function allows you to execute HINQ queries
runHINQ :: (Monad m, Alternative m) => HINQ m a b -> m b runHINQ (HINQ sClause jClause wClause) = _hinq sClause jClause wClause runHINQ (HINQ_ sClause jClause) = _hinq sClause jClause (_where (\_ -> True))

The other benefit of having the HINQ type is that it clarifies the originally long type you were working with. Let’s run a few queries to see how it does!

You’ll end this capstone by using HINQ to solve a more complicated problem involving multiple joins.