Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:53:31+00:00 2026-05-27T12:53:31+00:00

At least I think that’s what’s going on. Main.hs: module Main ( main )

  • 0

At least I think that’s what’s going on.

Main.hs:

module Main (
    main
) where

import Arithmetic
import Data.Maybe
import Data.Either
import Control.Monad.Error

testExpr :: Expr Float
testExpr = 
        (MultExpr "*"
            (AddExpr "XXX"
                (NumExpr 1) 
                (AddExpr "-"
                    (NumExpr 24)
                    (NumExpr 21)
                )
            ) 
            (NumExpr 5) 
        )

main :: IO ()
main = do 
    putStrLn $ case eval testExpr of
            Left msg -> "Error: " ++ msg
            Right result -> show result

Arithmetic.hs:

{-# LANGUAGE GADTs #-}

module Arithmetic  where

type Op = String

data Expr a where
    NumExpr :: Float -> Expr Float
    AddExpr :: Op -> Expr Float -> Expr Float -> Expr Float
    MultExpr :: Op -> Expr Float -> Expr Float -> Expr Float

eval :: (Monad m) => Expr Float -> m Float
eval (NumExpr n) = return n
eval (AddExpr "+" e1 e2) = evalBin (+) e1 e2
eval (AddExpr "-" e1 e2) = evalBin (-) e1 e2
eval (AddExpr "%" e1 e2) = evalBin (%) e1 e2
eval (AddExpr _ _ _ ) = fail "Invalid operator. Expected +, - or %"
eval (MultExpr "*" e1 e2) = evalBin (*) e1 e2
eval (MultExpr "/" e1 e2) = evalBin (/) e1 e2
eval (MultExpr _ _ _ ) = fail "Invalid operator. Expected * or /"

evalBin :: (Monad m) => (Float -> Float -> Float) -> Expr Float -> Expr Float -> m Float
evalBin op e1 e2 = do 
  v1 <- eval e1
  v2 <- eval e2
  return $ op v1 v2

infixl 6 %
(%) :: Float -> Float -> Float
a % b = a - b * (fromIntegral $ floor (a / b))

But, when eval fails, I get an error in IO, without the “Error: ” string appended.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:53:31+00:00Added an answer on May 27, 2026 at 12:53 pm

    Which version of base are you using? fail is no longer defined to return a Left in the latest version of the Either e monad, instead it uses the default definition (which calls error, which throws an exception that can only be caught in IO).

    I don’t know why this changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At least, I think that's related to the problem. My scenario is this: I've
Given that the Control class implements IDisposable, I would think that ASP.Net is at
I understand the overall meaning of pointers and references(or at least I think i
I've just wrapped my head around monads (at least I'd like to think I
I remember reading somewhere (a long time ago) that sprites - or at least
Theoretically, I think that I can define Brushes and Colors etc. in an xaml
Currently the range slider outputs min&max values into single text field. I think that's
I know many of you will think that my questions been already asked and
Has anyone written either a long or short program in COW that demonstrates what
Or maybe with our implementation of it. I'm new to the team that wants

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.