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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:11:49+00:00 2026-06-18T06:11:49+00:00

I am new to Haskell, and I am struggling with debugging my code. Fixing

  • 0

I am new to Haskell, and I am struggling with debugging my code.
Fixing an error leads to other errors…

Here is my code.

import Data.Maybe

data Op = Add | Sub | Mul | Div | And | Or | Not | Eq | Less | Great
    deriving (Eq, Show)

data Exp = Literal Value
     | Primitive Op [Exp]
     | Variable String
     | If Exp Exp Exp
     | Let [(String, Exp)] Exp
    deriving (Show, Eq)

data Value = Number Int
       | Bool Bool
       | String String
    deriving (Eq, Show)

type Env = [(String, Value)]

eval :: Env -> Exp -> Value
eval e (Literal v) = v
eval e (Variable x) = fromJust (lookup x e)   --22

prim :: Op -> [Value] -> Value
prim Add [Number a, Number b] = Number (a + b)
prim And [Bool a, Bool b] = Bool (a && b)
prim Sub [Number a, Nuamber b] = Number (a - b)   -- No instance for (Fractional Int) arising from a use of `/'
prim Mul [Number a, Number b] = Number (a * b)
prim Div [Number a, Number b] = Number (a / b)
prim Or [Bool a, Bool b] = Bool (a || b)
prim Not [Bool a] = Bool (not a)
prim Eq [Number a, Number b] = Bool (a == b)
prim Eq [String a, String b] = Bool (a == b) 
prim Less [Number a, Number b] = Bool (a < b)
prim Less [String a, String b] = Bool (a < b)
prim Great [Number a, Number b] = Bool (a > b)
prim Great [String a, String b] = Bool (a > b) --37

main = do
    eval [("y", (Number 40))] (Let [("x", (Literal (Number 2)))] (prim Add [(Variable "x"), (Variable "y")])) -- Couldn't match expected type `Exp' with actual type `Value'

I am now getting two errors which I wrote in comments.
If you know what’s wrong with my code, please share your idea and save my time…

Thank you very much.

  • 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-18T06:11:50+00:00Added an answer on June 18, 2026 at 6:11 am
    -- No instance for (Fractional Int) arising from a use of `/'
    

    Presumably that’s coming from this line rather than the one with your comment:

    prim Div [Number a, Number b] = Number (a / b)
    

    a and b are Ints. The division operator is (/) :: Fractional a => a -> a -> a (you can find that out by firing up ghci and entering :t (/), or by looking it up on Hoogle).

    If you haven’t seen types like Fractional a => a -> a -> a, you should read this in two parts:

    1. The context Fractional a
    2. The type a -> a -> a

    This is just like a regular a -> a -> a type, so it takes two arguments of some type and gives you back a result of the same type. The only difference in adding the Fractional a context is that type used for a must be an instance of the Fractional type class; it isn’t free to range over any type you like.

    If you haven’t learned about type classes yet don’t worry. They’re pretty easy to grasp, but not something you should be looking at in depth when you’re just starting; you’ll get to them later.

    Int isn’t a member of the Fractional type class, so the / operator doesn’t work on Ints.

    The reason is that regular mathematical division doesn’t work on integers with this type; 3 / 2 would have to either give 1.5, in which case it doesn’t fit the type Int -> Int -> Int, or give 1 or 2, in which case it wouldn’t be correct mathematical division. There is a function div for implementing integer division, usable like a `div` b in infix notation.

    -- Couldn't match expected type `Exp' with actual type `Value'
    

    This message is about your own types, in a single expression you wrote. And the actual full error message would have given you more context about which part of the expression contains the error. Just follow it through from the top down, checking the types of things yourself and the error very quickly leaps out at you.

    In this case, you get to here:

    Let [("x", (Literal (Number 2)))] (prim Add [(Variable "x"), (Variable "y")])
    

    Let needs two arguments, a [(String, Exp)] and an Exp. The list is fine, but the second argument is (prim Add [(Variable "x"), (Variable "y")]). Without even digging into the substructure of that to see if it’s correct, prim has type Op -> [Value] -> Value, so there’s no way it’s going to give you an Exp.

    How to fix that is up to you; it looks like you need a bit of a refactor across the whole expression/value distinction. prim gives you an Value, which you could simply apply wrap in a Literal to get you past the type error you’re getting, but then you run into the problem that prim should be taking an Op and a [Value], but you appear to have given it an Op and a [Exp] (containing variables). I think you need to think about the difference between using prim to compute the results of a primitive application, using the Primitive constructor of Exp to represent a primitive application, and using eval to evaluate (in an environment) an arbitrary expression (which may contain several primitive applications) to a value.

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

Sidebar

Related Questions

Im new to Haskell!! I wrote this code: import Data.List inputIndex :: [String] ->
I'm new to Haskell and I'm getting an annoying behaviour when debugging. I add
I am new to Haskell and I wonder how/if I can make this code
I am very new in haskell, i wrote the code for item details and
I am new to Haskell and facing a "cannot construct infinite type" error that
I'm new to Haskell and struggling with some subtleties of syntax. Why is this
I'm new with Haskell and have trouble with its package. I want to import
I'm fairly new to Haskell and have a question about pattern-matching. Here is a
I'm new to haskell, and so I'm trying to recreate the following C++ code
I am new to Haskell and am trying to use bitwise operations from Data.Bits.

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.