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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:16:13+00:00 2026-05-24T09:16:13+00:00

Is it possible to place an inequality constraint on the typevariables of a function,

  • 0

Is it possible to place an inequality constraint on the typevariables of a function, à la foo :: (a ~ b) => a -> b as in GHC type family docs, except inequality rather than equality?

I realise that there is possibly no direct way to do this (as the ghc docs doesn’t list any to my knowledge), but I would be almost puzzled if this weren’t in some way possible in light of all the exotic type-fu I have been exposed to.

  • 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-24T09:16:13+00:00Added an answer on May 24, 2026 at 9:16 am

    First, keep in mind that distinct type variables are already non-unifiable within their scope–e.g., if you have \x y -> x, giving it the type signature a -> b -> c will produce an error about not being able to match rigid type variables. So this would only apply to anything calling the function, preventing it from using an otherwise simple polymorphic function in a way that would make two types equal. It would work something like this, I assume:

    const' :: (a ~/~ b) => a -> b -> a
    const' x _ = x
    
    foo :: Bool
    foo = const' True False -- this would be a type error
    

    Personally I doubt this would really be useful–the independence of type variables already prevents generic functions from collapsing to something trivial, knowing two types are unequal doesn’t actually let you do anything interesting (unlike equality, which lets you coerce between the two types), and such things being declarative rather than conditional means that you couldn’t use it to distinguish between equal/unequal as part of some sort of specialization technique.

    So, if you have some specific use in mind where you want this, I’d suggest trying a different approach.

    On the other hand, if you just want to play with ridiculous type-hackery…

    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE TypeOperators #-}
    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE FunctionalDependencies #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE UndecidableInstances #-}
    {-# LANGUAGE OverlappingInstances #-}
    
    -- The following code is my own hacked modifications to Oleg's original TypeEq. Note
    -- that his TypeCast class is no longer needed, being basically equivalent to ~.
    
    data Yes = Yes deriving (Show)
    data No = No deriving (Show)
    
    class (TypeEq x y No) => (:/~) x y
    instance (TypeEq x y No) => (:/~) x y
    
    class (TypeEq' () x y b) => TypeEq x y b where
        typeEq :: x -> y -> b
        maybeCast :: x -> Maybe y
    
    instance (TypeEq' () x y b) => TypeEq x y b where
        typeEq x y = typeEq' () x y
        maybeCast x = maybeCast' () x
    
    class TypeEq' q x y b | q x y -> b where
        typeEq' :: q -> x -> y -> b
        maybeCast' :: q -> x -> Maybe y
    
    instance (b ~ Yes) => TypeEq' () x x b where
        typeEq' () _ _ = Yes
        maybeCast' _ x = Just x
    
    instance (b ~ No) => TypeEq' q x y b where
        typeEq' _ _ _ = No
        maybeCast' _ _ = Nothing
    
    const' :: (a :/~ b) => a -> b -> a
    const' x _ = x
    

    Well, that was incredibly silly. Works, though:

    > const' True ()
    True
    > const' True False
    
    <interactive>:0:1:
        Couldn't match type `No' with `Yes'
        (...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to place more than one object (which represent TreeViewer selections) into
Is it possible to place Facebook's request form inside a popup? Google is failing
Hi is it possible to place an icon in the title of a titledBorder
I know it's possible to place a PyCairo surface inside a Gtk Drawing Area.
When using jQuery to submit a form is it possible to place the resulting
Is it possible to programmatically place the contents of a web page into a
Is there a place to find all possible uses of the syscmd method in
Is it possible to have multiple folders where I can place applications to be
Is it possible to place clientaccesspolicy.xml file into site root for the SharePoint Online,
Is it possible to place a grid of buttons in Tkinter inside another frame?

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.