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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:50:13+00:00 2026-06-04T07:50:13+00:00

When I compile the following code with GHC (using the -Wall flag): module Main

  • 0

When I compile the following code with GHC (using the -Wall flag):

module Main where

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)

insert :: (Ord a) => a -> Tree a -> Tree a
insert x EmptyTree = Node x EmptyTree EmptyTree
insert x (Node a left right)
    | x == a = Node a left right
    | x < a = Node a (insert x left) right
    | x > a = Node a left (insert x right)

main :: IO()
main = do
    let nums = [1..10]::[Int]
    print . foldr insert EmptyTree $ nums

GHC complains that pattern matching in insert is non-exhaustive:

test.hs|6| 1:
||     Warning: Pattern match(es) are non-exhaustive
||              In an equation for `insert': Patterns not matched: _ (Node _ _ _)

Why is GHC issuing this warning? It is pretty obvious that the pattern GHC complains about is handled in insert x (Node a left right).

  • 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-04T07:50:14+00:00Added an answer on June 4, 2026 at 7:50 am

    Riccardo is correct, GHC doesn’t infer that your guards can’t possibly all be false. So accept his answer please.

    I’m going to digress and talk about coding style.

    Your motivation for not using otherwise may have been that it looks unsightly:

    insert :: (Ord a) => a -> Tree a -> Tree a
    insert x EmptyTree = Node x EmptyTree EmptyTree
    insert x (Node a left right)
        | x == a    = Node a left right
        | x < a     = Node a (insert x left) right
        | otherwise = Node a left (insert x right)
    

    Looking at this code, a human reader must confirm to themselves that the final guard accepts precisely those cases where x > a.

    We could instead write it like this:

    insert :: (Ord a) => a -> Tree a -> Tree a
    insert x EmptyTree = Node x EmptyTree EmptyTree
    insert x (Node a left right) = case x `compare` a of
        EQ -> Node a left right
        LT -> Node a (insert x left) right
        GT -> Node a left (insert x right)
    

    The Ordering type returned by compare has only the three values EQ, LT, and GT, so GHC can confirm that you’ve covered all possibilities, and a human reader can easily see that you’ve covered them correctly.

    This is also more efficient code: we call compare once, instead of calling == and then probably calling < as well.

    Now I’m going to digress some more and talk about laziness.

    You’ve probably also written a function similar to this:

    contains :: (Ord a) => a -> Tree a -> Bool
    contains _ EmptyTree = False
    contains x (Node a left right) = case x `compare` a of
        EQ -> True
        ...
    

    When x == a, you need to know that the tree uses the Node constructor, and that its first argument is equal to x. You don’t need to know what either of the subtrees are.

    But now look back at my definition of insert above. When the tree it’s given is a Node, it always returns a Node whose first argument is always a. But it doesn’t state that up front: instead it evaluates x `compare` a.

    We can rewrite insert to perform the comparison as late as possible:

    insert :: (Ord a) => a -> Tree a -> Tree a
    insert x EmptyTree = Node x EmptyTree EmptyTree
    insert x (Node a left right) = Node a newLeft newRight
      where comparison = x `compare` a
            newLeft  = if comparison == LT then insert x left  else left
            newRight = if comparison == GT then insert x right else right
    

    Now we return the Node a bit as soon as possible — even if the comparison throws an error! — and we still perform the comparison once at most.

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

Sidebar

Related Questions

I'm trying to compile the following code with GHC: module Test where import Maybe
I'm trying to get the following code to compile import IO data MyInt =
When trying to compile the following code in LINQPad : void Main() { DriveInfo.GetDrives().Select(GetProviderName).Dump();
I can't compile the following code using g++ 4.1.2: #include <memory> class A {
I cannot compile the following code: main = do line <- getLine putStrLn (work
When I compile the following code using g++ class A {}; void foo(A&) {}
I am trying to compile the following code on Linux using gcc 4.2: #include
When I try to compile the following code using System; using System.Collections.Generic; using System.Reflection;
When i try to compile the following code: #include <string.h> using namespace std; typedef
When I compile the following code with g++ #include <CoreServices/CoreServices.h> int main(int argc, char

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.