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
-- }
-- ]
-- }
-- ]