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 6222601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:20:34+00:00 2026-05-24T08:20:34+00:00

Haskell newbie here. I wrote an evaluator for a minimal assembly-like language. Now, I

  • 0

Haskell newbie here.

I wrote an evaluator for a minimal assembly-like language.

Now, I want to extend that language to support some syntactic sugar which, I will then compile back to use only the primitive operators. The ideia is that I do not want to touch the evaluator module again.

In the OO way of doing things, I think, one could extend the original module so to support the syntactic sugar operators, providing here the translation rules.

Other than that, I can only think of rewriting the datatype constructors in both modules so that they would not name-collide, and proceed from there, as if they were complete different things, but that implies some redundancy, for I would have to repeat (just with other names) the operators in common. Again, I think the keyword here is extend.

Is there a functional way of accomplishing this?

Thanks for taking the time to read this question.

  • 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-24T08:20:35+00:00Added an answer on May 24, 2026 at 8:20 am

    This problem was named “the expression problem” by Phil Wadler, in his words:

    The goal is to define a data type by cases, where one can add new cases to the data type and new functions over the data type, without recompiling existing code, and while retaining
    static type safety.

    One solution to have extensible data type is to use type classes.

    As an example let’s assume we have a simple language for arithmetics:

    data Expr = Add Expr Expr | Mult Expr Expr | Const Int
    
    run (Const x) = x
    run (Add exp1 exp2)  = run exp1 + run exp2
    run (Mult exp1 exp2) = run exp1 * run exp2
    

    e.g.

    ghci> run (Add (Mult (Const 1) (Const 3)) (Const 2))
    5
    

    If we wanted to implement it in an extensible way, we should switch to type classes:

    class Expr a where
        run :: a -> Int
    
    
    data Const = Const Int
    
    instance Expr Const where
        run (Const x) = x
    
    
    data Add a b = Add a b
    
    instance (Expr a,Expr b) => Expr (Add a b) where
        run (Add expr1 expr2) = run expr1 + run expr2
    
    
    data Mult a b = Mult a b
    
    instance (Expr a, Expr b) => Expr (Mult a b) where
        run (Mult expr1 expr2) = run expr1 * run expr2
    

    Now let’s extend the language adding subtractions:

    data Sub a b = Sub a b
    
    instance (Expr a, Expr b) => Expr (Sub a b) where
        run (Sub expr1 expr2) = run expr1 - run expr2
    

    e.g.

    ghci> run (Add (Sub (Const 1) (Const 4)) (Const 2))
    -1
    

    For more info on this approach, and in general on the expression problem, check Ralf Laemmel’s videos 1 and 2 on Channel 9.

    However, as noticed in the comments, this solution changes the semantics. For example lists of expressions are no longer legal:

    [Add (Const 1) (Const 5), Const 6] -- does not typecheck
    

    A more general solution using coproducts of type signatures is presented in the functional pearl “Data types a la carte”. See also Wadler’s comment on the paper.

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

Sidebar

Related Questions

I'm learning Haskell and I'd like to write some multithreaded programs now to see
I have some Haskell code that does work correctly on an infinite list, but
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors
I am a Haskell newbie, though had a previous Lisp/Scheme experience. Right now I
For haskell practice I want to implement a game where students/pupils should learn some
Haskell newbie here. I'm attempting to use the http-enumerator to query a service via
I am an absolute newbie in Haskell yet trying to understand how it works.
Haskell is givinig me a headache today. I want to handle an exception. When
Haskell provides the feature something like f = f1 . f2 How can I
In Haskell, is there a way to restrict a monad M a so that

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.