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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:05:34+00:00 2026-05-30T01:05:34+00:00

I thought it’d be neat to allow arbitrary chained comparison in Haskell, so you

  • 0

I thought it’d be neat to allow arbitrary chained comparison in Haskell, so you could do simple range checks like:

x <= y < z

And more complex stuff like

x /= y < z == a

Where the above two are semantically equivalent to

x <= y && y < z
x /= y && y < z && z == a

Just seeing if I could get the syntax to work.

So I got most of the way there using a couple of type classes:

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
module ChainedOrd where

import Prelude hiding ((<), (<=), (>), (>=), (==), (/=))

class Booly v a where
  truthy :: v -> a
  falsy :: v -> a

instance Booly a Bool where
  truthy = const True
  falsy = const False

instance Booly a (Maybe a) where
  truthy = Just
  falsy = const Nothing

class ChainedOrd a b where
  (<),(>),(<=),(>=),(==),(/=) :: (Booly b c) => a -> b -> c

infixl 4 <
infixl 4 >
infixl 4 <=
infixl 4 >=
infixl 4 ==
infixl 4 /=

instance Ord a => ChainedOrd a a where
  x < y     = case compare x y of LT -> truthy y ; _ -> falsy y
  x > y     = case compare x y of GT -> truthy y ; _ -> falsy y
  x <= y    = case compare x y of GT -> falsy y  ; _ -> truthy y
  x >= y    = case compare x y of LT -> falsy y  ; _ -> truthy y
  x == y    = case compare x y of EQ -> truthy y ; _ -> falsy y
  x /= y    = case compare x y of EQ -> falsy y  ; _ -> truthy y

instance Ord a => ChainedOrd (Maybe a) a where
  Just x < y     = case compare x y of LT -> truthy y ; _ -> falsy y
  Nothing < y    = falsy y
  Just x > y     = case compare x y of GT -> truthy y ; _ -> falsy y
  Nothing > y    = falsy y
  Just x <= y    = case compare x y of GT -> falsy y  ; _ -> truthy y
  Nothing <= y   = falsy y
  Just x >= y    = case compare x y of LT -> falsy y  ; _ -> truthy y
  Nothing >= y   = falsy y
  Just x == y    = case compare x y of EQ -> truthy y ; _ -> falsy y
  Nothing == y   = falsy y
  Just x /= y    = case compare x y of EQ -> falsy y  ; _ -> truthy y
  Nothing /= y   = falsy y

Which compiles fine, but doesn’t quite seem to allow chaining, due to the problem of intermediate types.

-- works
checkRange1 :: Ord a => a -> a -> a -> Bool
checkRange1 x y z = x `lem` y <= z
  where lem :: Ord a => a -> a -> Maybe a
        lem = (<=)

-- works
checkRange2 :: Ord a => a -> a -> a -> Bool
checkRange2 x y z = (x <= y) `leb` z
  where leb :: Ord a => Maybe a -> a -> Bool
        leb = (<=)

checkRange1 and checkRange2 work fine, since they both put a constraint on the intermediate type (either
as a result of the first comparison, or as an argument to the second).

-- error
checkRange3 :: Ord a => a -> a -> a -> Bool
checkRange3 x y z = (x <= y) <= z

When I try to let the compiler infer the intermediate type, though, it barks at me.

ChainedOrd.hs:64:30:
    Ambiguous type variable `a0' in the constraints:
      (ChainedOrd a0 a) arising from a use of `<='
                        at ChainedOrd.hs:64:30-31
      (Booly a a0) arising from a use of `<=' at ChainedOrd.hs:64:24-25
    Probable fix: add a type signature that fixes these type variable(s)
    In the expression: (x <= y) <= z
    In an equation for `checkRange3': checkRange3 x y z = (x <= y) <= z

Is there any way I can convince the compiler that it should use Maybe a as
the intermediate type a0 satisifying Booly a a0, ChainedOrd a0 a, since that’s the only instance it knows about?

Failing that, is there another way I can make arbitrary comparison chaining work?

  • 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-30T01:05:35+00:00Added an answer on May 30, 2026 at 1:05 am
    infixl 4 ==?
    
    class ChainedEq a b where
      (==?) :: a -> b -> Maybe b
    
    instance (Eq a) => ChainedEq (Maybe a) a where
      x ==? y = if x == Just y
        then x
        else Nothing
    
    instance (Eq a) => ChainedEq a a where
      x ==? y = if x == y
        then Just x
        else Nothing
    
    unChain :: Maybe a -> Bool
    unChain Nothing = False
    unChain (Just _) = True
    
    test :: Int -> Int -> Int -> Bool
    test x y z = unChain $ x ==? y ==? z
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought expressions like this would cause Haskell to evaluate forever. But the behaviors
I thought this would be a relatively simple task with something like FMOD, but
I thought I'd offer this softball to whomever would like to hit it out
Thought I could use the BinaryWriter but haven't had any luck. Suggestions?
I thought I was pretty knowledgeable about CSS but this simple problem baffles me.
I thought this would be pretty simple; basically, it's a 5-star rating system. When
Thought I'd try to implement SHA1 in Haskell myself. I came up with an
I thought of the possibility of writing a Java program that could use the
I thought this would be really easy but I can't find a simple solution,
I thought something like this <ajaxToolkit:CalendarExtender ID=TextBox1_CalendarExtender runat=server TargetControlID=txtDatumPoaganje Format=MM/dd/yyyy StartDate=<%=DateTime.Now%>> But it doesnt

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.