Skip to content

Haskell Language

Understand Monad Transformers(2)

This is a reading notes from the great book named book of monads. It helps me a lot and I quite recommend you to read it if you have any plan to read books about haskell monads.

This is the following article after understand monad transformers as there are some confusions and missing points in the previous blog.

Previous blog focused on when to use monad transformers, and how we understand the monad transformer creates a new powerful monad which supports both monadT and underlying monad features.

After several months, I would rather to say it didn't point out the core part of monad transformer, which is how the monad transformer supports the underlying monad functionalities via type class.

  • what's the problem of stacking monads(the reason why we need monad transformer)
  • why we can use features from both monad and monad transformer under the scope of monad transformer.

Onboarding Parsec Library from Self-Defined Parser

Previously, I used my own parser to parse the solidity language syntax and finished the expression part. It's a combination of ExceptT and State to maintain the states and report error.

type ErrMsg = Text

type Parser a = ExceptT ErrMsg (State Text) a

The original parser doesn't have error report at all, and it definitely is not the correct way. Moreover, I believe the open source parser library works better than mine as i'm still newbie to haskell. Hence, I choose to replace my custom parser by the parsec parser.

This blog introduces the investigation of parsec and the problems I encountered during migration. Beside them, I will compare some pattern differences between parsec and mine.

Foldr and Foldl in Haskell

This blog introduces how foldr and foldl work for a better usage them. Understanding the accumulation and association support from them is crucial.

foldr: Right-associative fold of a structure, lazy in the accumulator.

foldl: Left-associative fold of a structure, lazy in the accumulator.

In short, it helps you to construct a left/right associative structure. One of the real cases is you want to construct a left associative structure inside a parser , but you parse it in a right recursive way.

Note that this is a simple note, which has been already covered by documentation, and I wrote it because at the beginning I misunderstand its feature, so I wrote some code to try.

Why Traversable Requires Applicative at Least

At the beginning, the blog name is why traversable requires applicative. However, later I feel like it is nonsense and just repeating the sentence to describe the traversable. It requires applicative of course because it uses that. As a result, I changed blog name because in this blog, we will learn about how the traversable uses applicative and why it requires applicative at least.

You should be familiar with Foldable before reading this blog, and I recommend you to read my previous blog why foldable requires monoid at least

In short, applicative is needed as we need to lift the chosen operator, pure the default value and keep applying by Foldable function foldr.