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

  • Home
  • SEARCH
  • 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 9274311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:19:29+00:00 2026-06-18T16:19:29+00:00

I am implementing an Expression solver, but I am having some problems with pattern

  • 0

I am implementing an Expression solver, but I am having some problems with pattern matching.
I have the following code

data Expression a where
                Const   ∷  Int → Expression Int
                Add ∷  Expression Int → Expression Int → Expression Int
                Sub ∷  Expression Int → Expression Int → Expression Int


eval ∷  Expression a → a
eval (Const a) = a

eval (Add exp1 exp2) = (val1 + val2)
  where
    val1 = eval exp1
    val2 = eval exp2


eval (Sub exp1 exp2) = (val1 - val2)
  where
    val1 = eval exp1
    val2 = eval exp2

But since eval Add and eval Sub are very similar and I could want another operations I though of doing a more generic implementation, but I am having some problems.
I though of doing like

data Op = Add | Sub

data Expression a where
                Const   ∷  Int → Expression Int
                Op ∷  Expression Int → Expression Int → Expression Int

eval (Op exp1 exp2) = case Op of
                           Add → (val1 + val2)
                           Sub → (val1 - val2)
                      where
                        val1 = eval exp1
                        val2 = eval exp2 

But it doesn’t work. Is it possible to do something like this?
Thanks in advance

  • 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-06-18T16:19:30+00:00Added an answer on June 18, 2026 at 4:19 pm

    This does not work because you are defining Op as both a data constructor
    and a type. The type Op has two constructors Add and Sub, but the
    Expression type has an Op constructor. This code is confusing the two.

    The case statement of your eval function attempts to match of the value
    Op, but Op is a constructor that takes two arguments in this context,
    so you can’t pattern match on it. I suspect you are going for something like
    this

    data Op = Add | Sub
    
    data Expression a where
                    Const ::  Int -> Expression Int
                    Op ::  Op -> Expression Int -> Expression Int -> Expression Int
    
    eval (Const c)         = c
    eval (Op op exp1 exp2) = case op of
                               Add -> (val1 + val2)
                               Sub -> (val1 - val2)
                          where
                            val1 = eval exp1
                            val2 = eval exp2
    

    You will have to include a field in the Op constructor that denotes what
    operation is to be performed. Since you have to match on the that operation
    anyway, it would probably be nicer to stick with the original definition of
    Expression.

    Another possibility that is simpler and easier to extend might be something like
    the following

    data Expression a where
        Const ::  Int -> Expression Int
        Op    ::  (a -> b -> c) -> Expression a -> Expression b -> Expression c
    
    eval :: Expression a -> a
    eval (Const c)        = c
    eval (Op f exp1 exp2) = f (eval exp1) (eval exp2)
    

    where an Op wraps the actual function up with it. You would not be able to
    do nice things like print out the expression and know what function it
    corresponds to though.

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

Sidebar

Related Questions

I am implementing arithmetic calculator, but I am getting errors: error: expected primary-expression before
I'm implementing a linux shell for my weekend assignment and I am having some
I am following sample code on implementing MVVM in Silverlight (see: http://msdn.microsoft.com/en-us/magazine/dd458800.aspx ). On
I am implementing IQueryable, and so far have only implemented an expression visitor for
I'm implementing some basic ranking functionality in my MVC application, but I'm running across
I'm writing a computation expression that is essentially implementing a State monad and I'm
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I am implementing a tree which is a Binary Expression Tree. The leaf nodes
I want a regular expression pattern that will match with the end of a

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.