Challenge 1: Adding a Mul Constructor — Possible Solution ==================================================================== Main.hs (extended): data Expr a where IntLit :: Int -> Expr Int BoolLit :: Bool -> Expr Bool Add :: Expr Int -> Expr Int -> Expr Int Mul :: Expr Int -> Expr Int -> Expr Int -- new Div :: Expr Int -> Expr Int -> Expr Int If :: Expr Bool -> Expr a -> Expr a -> Expr a evalM :: Expr a -> Either String a evalM (IntLit n) = Right n evalM (BoolLit b) = Right b evalM (Add e1 e2) = do v1 <- evalM e1 v2 <- evalM e2 Right (v1 + v2) evalM (Mul e1 e2) = do -- new v1 <- evalM e1 v2 <- evalM e2 Right (v1 * v2) evalM (Div e1 e2) = do v1 <- evalM e1 v2 <- evalM e2 if v2 == 0 then Left "division by zero" else Right (v1 `div` v2) evalM (If cond t f) = do c <- evalM cond if c then evalM t else evalM f class Pretty a where pretty :: a -> String instance Pretty (Expr a) where pretty (IntLit n) = show n pretty (BoolLit b) = show b pretty (Add e1 e2) = "(" ++ pretty e1 ++ " + " ++ pretty e2 ++ ")" pretty (Mul e1 e2) = "(" ++ pretty e1 ++ " * " ++ pretty e2 ++ ")" -- new pretty (Div e1 e2) = "(" ++ pretty e1 ++ " / " ++ pretty e2 ++ ")" pretty (If c t f) = "if " ++ pretty c ++ " then " ++ pretty t ++ " else " ++ pretty f main :: IO () main = do let expr = Add (IntLit 2) (Mul (IntLit 3) (IntLit 4)) putStrLn (pretty expr) print (evalM expr) Output: (2 + (3 * 4)) Right 14 Explanation: Mul is added following the exact same shape as Add's own constructor and evaluation case -- same argument types, same "evaluate both sides then combine" pattern, just using (*) instead of (+). Both evalM and pretty needed one new equation each, matching the GADT's own new constructor. The nested expression (2 + (3 * 4)) correctly computes to 14, confirming Mul composes correctly with the pre-existing Add. WHY THIS WORKS AS AN ANSWER ------------------------------ This extends the GADT, evalM, and pretty consistently with a genuine new constructor following the established pattern, and tests it in a nested expression combining the new Mul with the existing Add.