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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:48:09+00:00 2026-05-13T15:48:09+00:00

Basically I have defined a Tree data type which is defined as follows: data

  • 0

Basically I have defined a Tree data type which is defined as follows:

data Tree a = Empty
| Leaf a
| Node (Tree a) a (Tree a)
deriving (Eq, Ord, Show)

Now I have to create a function to insert a value into an ordered tree (it doesn’t have to sort the tree, just add the value). This is what I’ve come up with so far:

insert :: a -> Tree a -> Tree a
insert x Empty      = Leaf x
insert x (Leaf m)   | m < x     = Node (Leaf x) m Empty
                    | otherwise = Node Empty m (Leaf x)
insert x (Node l m r)   | x > m     = Node (Leaf l) m (insert x r)
                        | otherwise = Node (insert x l) m (Leaf r)

However, when I run this I get the following error message:

Couldn’t match expected type ‘a’ (a rigid variable)
against inferred type ‘Tree a’
‘a’ is bound by the type signature for ‘insert’ at Main.hs:11:10
In the first argument of ‘Leaf’, namely ‘l’
In the first argument of ‘Node’, namely ‘(Leaf l)’
In the expression: Node (Leaf l) m (insert x r)

I assume it’s something to do with types but I can’t see where I’ve put any types that shouldn’t be there.

  • 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-13T15:48:09+00:00Added an answer on May 13, 2026 at 3:48 pm

    Translating roughly from type-checker-ese into English:

    Couldn’t match expected type ‘a’ (a
    rigid variable)

    This means that it was expecting an arbitrary type a, that’s also used elsewhere (hence “rigid”) so it can’t just take any old type.

    against inferred type ‘Tree a’

    This means that instead it found a Tree containing elements of the expected rigid polymorphic type. This obviously doesn’t make sense, so it complains.

    ‘a’ is bound by the type signature for ‘insert’ at Main.hs:11:10

    This just says that the type is being restricted because you specified it in that type signature.

    In the first argument of ‘Leaf’, namely ‘l’ In the first
    argument of ‘Node’, namely ‘(Leaf l)’ In the expression: Node (Leaf l) m (insert x r)

    This is just telling you which specific term it’s complaining about, with some context.

    So, to sort things out: The variable l is a Tree a being used in a context that needs just a. In this case l obviously has the correct type, so the mistake is in how it’s used. Why was the type checker looking for type a? Because you applied a Tree data constructor to it. But wait, l is already a Tree a! et voila, the scales fall from our eyes and the truth is beheld.

    …which was all just a lengthy way of explaining why Edward Kmett‘s quick-draw answer is correct, and what kind of reasoning one might use to arrive at such an answer.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at the following outline for the DX10… May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer If you run: make this program will look for a… May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Nope, they won't, so update will only be called once… May 14, 2026 at 6:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.