Challenge 2: A Nested Division-by-Zero Error Propagating Out — Possible Solution ==================================================================== Main.hs: main :: IO () main = do let expr = Add (IntLit 5) (Div (IntLit 10) (IntLit 0)) print (evalM expr) -- Why the error propagates out of the WHOLE Add expression: -- -- evalM (Add e1 e2) is written as: -- v1 <- evalM e1 -- v2 <- evalM e2 -- Right (v1 + v2) -- -- This is do-notation over Either, which desugars to >>= (per -- haskell2-3). evalM e1 (the IntLit 5) succeeds, producing Right 5. -- But evalM e2 -- the nested Div (IntLit 10) (IntLit 0) -- evaluates -- to Left "division by zero", since the divisor is 0. Because -- >>= on Either SHORT-CIRCUITS the instant it encounters a Left, -- the `v2 <- evalM e2` line never successfully binds a value at -- all -- execution never reaches `Right (v1 + v2)`. Instead, the -- Left "division by zero" from the INNER Div expression is passed -- straight through as the result of the OUTER Add expression, -- completely unchanged. The 5 that WAS successfully computed for e1 -- is simply discarded, since there's no way to produce a meaningful -- combined Int once one half of the addition failed. Output: Left "division by zero" WHY THIS WORKS AS AN ANSWER ------------------------------ This constructs a genuine nested failure (a Div error inside an Add), confirms the error propagates correctly out of the entire expression rather than being silently swallowed, and the comment correctly traces the propagation through >>= 's own short-circuit-on-Left behavior from haskell2-3.