r/haskell • • 6h ago

In this approach, `StateT` modules are treated as independent functional units, which are then composed to build business applications. This method utilizes `liftIO` to vertically integrate `IO` action functions, serving as an alternative to the commonly used `ReaderT` pattern.

0 Upvotes

I watched the following video, which discusses the issue of "Transformer Hell" (the complexity arising from nested monad transformers) and proposes the use of "Algebraic Effects" as a solution:

https://www.youtube.com/watch?v=P3FmKp4HtSo

Intrigued, I experimented with the Haskell library `Polysemy`, but it did not provide the level of flexibility I had hoped for.

Since the core of functional programming lies in function composition, I began to wonder if the practice of composing monad transformers might actually undermine modularity.

Consequently, I propose a design in which `StateT` modules—built upon `IO`—are layered atop a `main` function that is also rooted in `IO`. This approach should help prevent structural complexity, even in large-scale programs. Furthermore, by standardizing the implementation patterns across modules, one can expect improvements in productivity, safety, and reusability.

With the assistance of Google Gemini and ChatGPT (for tasks such as debugging compilation errors), I developed and executed test code. When I asked both AIs to evaluate the design philosophy and the actual code, they concluded that "this design approach could serve as a highly viable architectural option for building enterprise-grade Haskell applications." I am sharing this in the hope that it might be useful to others.

As the full codebase is quite extensive, I have included only the parts related to `StateT` here. Please note that my previous post was removed for violating the terms of service due to the inclusion of AI-generated content. I would like to take this opportunity to offer my sincere apologies to Axman6 and 1rs, who left comments on that post.

Since 1rs expressed interest in the code, I have attached the section where `StateT` is implemented. Please note that this is an excerpt, as the full text is too long to include here.

■ IOEitherTest.hs

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DuplicateRecordFields      #-}
{-# LANGUAGE OverloadedRecordDot        #-}

import Control.Monad
import Control.Monad.State.Strict
import Control.Monad.Except
import Data.Either
import Data.Decimal
import Data.Time
import Types.MessageType
import Types.MoneyType
import Types.CustomerType
import Types.ProductType
import Types.SellType
import Text.Show.Unicode

-- ==========   売上情報生成関数   ========== --

type MainPara = (CustId,[(ProdId, Quantity)])
type SellPara =          (ProdId, Quantity)
type SldtPara = (SldtNo,  ProdId, Quantity)
type RsSell   = Either ErrMsg Sell
type RsSldt   = Either ErrMsg Selldtl
type StSell   = StateT [ Sell ] IO ErrMsg
type StSldt   = StateT [RsSldt] IO ErrMsg

-- 売上明細の作成と明細一覧への追加
makeSells :: [MainPara] -> StateT [Sell] IO ErrMsg
makeSells sellparas = do
    sellList <- get
    (emsg, newList) <- liftIO $ flip runStateT sellList $ 
        forM sellparas $ \(cid, dtls) -> addSell cid dtls
    put  newList
    pure $ fmtErrMsg $ foldr (<>) (ErrMsg "") emsg

-- 売上伝票の作成と売上一覧への追加
addSell :: CustId -> [SellPara] -> StSell
addSell cid dtls = do
    let addSldtNo :: SldtNo -> SellPara -> SldtPara
        addSldtNo sldtno (prodid, quantity) =
                 (sldtno, prodid, quantity)
        dtails  = zipWith addSldtNo [firstSldtNo ..] dtls
    sellList    <- get
    slno        <- pure   $ getSellNo sellList
    now         <- liftIO $ getCurrentTime
    customer    <- liftIO $ getCustomer cid
    (emsg, dts) <- liftIO $ (`runStateT` [])
                            (addSelldtl slno dtails)
    let sell = mkSell slno  (utctDay now) (rights dts) customer
    case sell of
        Left  ermsg -> pure (ermsg <> emsg)
        Right rsell -> do
            put (sellList <> [rsell])
            pure emsg

-- 売上明細の作成と明細一覧への追加
addSelldtl :: SellNo -> [SldtPara] -> StSldt
addSelldtl slno dtls = do
    detail <- liftIO $ forM dtls $
        \(sldtno, prodid, quantity) -> do
            product <- getProduct prodid
            pure $ mkSelldtl slno sldtno quantity product
    put  detail
    pure $ foldr (<>) (ErrMsg "") (lefts detail)

-- ==========   試験用アクション   ========== --

main :: IO ()
main = do
    let sellList = [] :: [Sell]
        keyEnt = [((CustId 1),[((ProdId 1),(Quantity  5))
                              ,((ProdId 2),(Quantity 10))
                              ])
                 ,((CustId 2),[((ProdId 2),(Quantity 10))
                              ,((ProdId 3),(Quantity  1))
                              ])
                 ,((CustId 3),[((ProdId 1),(Quantity  5))
                              ,((ProdId 3),(Quantity  1))
                              ])
                 ,((CustId 1),[])
                 ,((CustId 4),[((ProdId 2),(Quantity 10))
                              ,((ProdId 3),(Quantity  1))
                              ])
                 ]
    (errs, slist) <- (`runStateT` sellList) (makeSells keyEnt)
    uprint errs
    uprint slist

-- λ>main
-- ErrMsg "CustNm \"IBM\" Selldtl's cont is 0. ||
--         Customer not found: CustId 4"
-- [ Sell { sellNo = SellNo 1
--        , sellDt = SellDt 2026-11-16
--        , custId = CustId 1
--        , custNm = CustNm "IBM"
--        , ttlAmt = TtlAmount 170
--        , taxRt = TaxRate 10
--        , taxAmt = TaxAmount 17
--        , allAmt = AllAmount 187
--        , selDtl = [ Selldtl { sellNo = SellNo 1
--                             , sldtNo = SldtNo 1
--                             , prodId = ProdId 1
--                             , prodNm = ProdNm "pencil"
--                             , prodPr = Price 30
--                             , sldtQt = Quantity 5
--                             , amount = Amount 150
--                             }
--                   , Selldtl { sellNo = SellNo 1
--                             , sldtNo = SldtNo 2
--                             , prodId = ProdId 2
--                             , prodNm = ProdNm "Paper"
--                             , prodPr = Price 2
--                             , sldtQt = Quantity 10
--                             , amount = Amount 20
--                             }
--                    ]
--        }
-- , Sell { sellNo = SellNo 2
--        , sellDt = SellDt 2026-11-16
--        , custId = CustId 2
--        , custNm = CustNm "Apple"
--        , ttlAmt = TtlAmount 120
--        , taxRt = TaxRate 10
--        , taxAmt = TaxAmount 12
--        , allAmt = AllAmount 132
--        , selDtl = [ Selldtl { sellNo = SellNo 2
--                             , sldtNo = SldtNo 1
--                             , prodId = ProdId 2
--                             , prodNm = ProdNm "Paper"
--                             , prodPr = Price 2
--                             , sldtQt = Quantity 10
--                             , amount = Amount 20
--                             }
--                   , Selldtl { sellNo = SellNo 2
--                             , sldtNo = SldtNo 2
--                             , prodId = ProdId 3
--                             , prodNm = ProdNm "eraser"
--                              , prodPr = Price 100
--                             , sldtQt = Quantity 1
--                             , amount = Amount 100
--                             }
--                   ]
--        }
-- , Sell { sellNo = SellNo 3
--        , sellDt = SellDt 2026-11-16
--        , custId = CustId 3
--        , custNm = CustNm "Fujitsu"
--        , ttlAmt = TtlAmount 250
--        , taxRt = TaxRate 10
--        , taxAmt = TaxAmount 25
--        , allAmt = AllAmount 275
--        , selDtl = [ Selldtl { sellNo = SellNo 3
--                             , sldtNo = SldtNo 1
--                             , prodId = ProdId 1
--                             , prodNm = ProdNm "pencil"
--                             , prodPr = Price 30
--                             , sldtQt = Quantity 5
--                             , amount = Amount 150
--                             }
--                   , Selldtl { sellNo = SellNo 3
--                             , sldtNo = SldtNo 2
--                             , prodId = ProdId 3
--                             , prodNm = ProdNm "eraser"
--                             , prodPr = Price 100
--                             , sldtQt = Quantity 1
--                             , amount = Amount 100
--                             }
--                   ]
--        }
-- ]

r/haskell • • 4h ago

datomlite - A zero-dep, Datomic-style "DB-as-a-value" you can drop into your Haskell apps... (Looking for feedback!)

10 Upvotes

Hello! Been tinkering on datomlite for a while, and it should be about ready for trials ... would love your sharp eyes on how the "typed datalog-style" API feels and if you see any glaring performance weak seams.


r/haskell • • 6h ago

Community suggestion on Prelude replacement

11 Upvotes

I am trying to weigh options on if a Prelude replacement is needed and if so, which one.?

I see a lot prelude replacements are naintained by small companies or one or two person shops. And some preludes like classy prleude are dead and obsolete?

What is preventing the GHC maintainers from adopting the learnings from the prelude replacements? I understand backward compatibility is a big concern, but new improved functions and types cn be added to new modules and re exported?​​​


r/haskell • • 14h ago

A B compiler written in Haskell. Looking for code reviews.

15 Upvotes

This is my first ever compiler (Maybe second if I wanna consider that semi-serious assignment in Uni on a C compiler) and also my first major project with Haskell. Before this, I mainly did Hackerrank and a couple of advent of code with Haskell.

The Lexer, Parser and semantic analyser/IRGen is written in Haskell, and it targets the QBE backend.

Repo - https://github.com/SBaori/B

Looking for feedback and code reviews on good practices missed, some idiomatic patterns in compiler development and general Haskell coding standards advice.

Comment on LLMs - Every single character in the repo is thought, reasoned and written by me. LLMs were only used for summarising the original B specification, learning some compiler theory and trying to extract/query some built-in functions from Haskell.