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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:53:59+00:00 2026-05-23T11:53:59+00:00

Again Haskellers & Haskellettes, I still have the problem, that I don’t really know

  • 0

Again Haskellers & Haskellettes,

I still have the problem, that I don’t really know when to use (Class a) => a, Type or
Type a where Type is a “union type” as for example Val and ExprTree

Now i want to make my datatype ExprTree a bit more versatile by adding a few instances:

import Ratio
data Fun = Add|Sub|Mul|Div|Pow
    deriving (Eq,Ord)
instance Show Fun where
    show Add = "+"
    show Sub = "-"
    show Mul = "*"
    show Div = "/"
    show Pow = "^"
type Label = Rational
type Var = String
class Eval e where
eval :: (Num a) => e -> a -> a
data ExprTree a = Leaf {lab::Label, val::Val a}
          | Node {lab::Label, fun::Fun, lBranch::ExprTree a, rBranch::ExprTree a}
          deriving(Eq,Ord)
data Val a = Num a | Var String deriving (Eq, Ord, Show)

instance (Num a) => Num (ExprTree a) where
    ...
    fromInteger i = Leaf (0%1) i -- <--error

instance Show (ExprTree a) where
    show (Leaf l a) = show a -- <-- error
    show (Node l f lb rb) = (show lb)++"  "++(show l)
                      ++(show f)++"  "++(show rb)++"\n"

instance Eval (Val a) where
    eval (Var v) n = n
    eval a _ = a -- <-- error

I think this problems – are all of the same kind. And the NOT understanding the difference between class and type and so forth in a more than trivial setting – is a hole in the basis of my haskell programming; so i really want to understand this not to solve any homework.

Note: The label is a Rational – which makes inserting and searching much easier, than labelling with natural numbers or integers.

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-05-23T11:53:59+00:00Added an answer on May 23, 2026 at 11:53 am

    The problem with your fromInteger is that i needs to be converted to a Val a.

    instance Num a => Num (ExprTree a) where
        -- ...
        fromInteger i = Leaf (0%1) (Val (fromInteger i))
    

    The Val wrapper makes it a Val; the inner fromInteger creates an a, which is a caller-specified type, which we know can be created with fromInteger because of the Num a constraint) from the Integer. Pay close attention to that last: it’s the reason behind typeclasses. The Num a => constraint insures that, given an a, we know that we can use any of the Num instance methods on it.

    The other problem is the same issue in reverse: you are calling show, but you haven’t done anything to make sure that show makes sense on an a.

    instance Show a => ExprTree a where
        show (Leaf _ a) = show a -- this works now because we told it a is show-able
        -- ...
    

    The problem with Eval is slightly different: a just appears without any source, so the compiler doesn’t know anything about it. In particular, eval a _ = a requires that an e be a valid a — but a is an unknown, so the compiler correctly says “what?”. For this one you need to think about what you’re really trying to do; the simplest solution is to drop the e and use a everywhere, but is that really what you intended?

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

Sidebar

Related Questions

Me again... I have an XML File that contains different categories which I would
Once again a question about extjs. I use EXT 4.0.2 and have a couple
I again have problem with routing :) When I run application if user is
Again a simple thing. I have a stylesheet that parses XML and XSL files.
Again I have problem with checking whether DataReader object has data or not? Dim
Again I am in a serious problem .. please help me out. I have
again have problem with opends now i would like to save unicode in the
(Again, an MVC validation question. I know, I know...) I'd like to use AutoMapper
again I have a question that is not so easily googlable. Let's assume BIOS
again. I have a script that is using the jQuery UI plugin to generate

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.