Re:There's a mathematician in your compiler
by philzook on Tuesday March 31, 2020 @12:20PM
(#22739138)
You can achieve a certain amount of type disequality in Haskell. It isn't necessarily easy or sufficient for all purposes. There is a type family that returns a type level Bool for equality here. https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-...
There are some older techniques here http://okmij.org/ftp/Haskell/typeEQ.html
But there's no equivalent for non-unification - `a /~ b`. You can sort of hack it by asking the compiler "do these types unify right now", and then negating the response you get, but that doesn't work for types which eventually unify, but just fail to at the moment.
A good example is with non-injective type functions. To show `Maybe a ~ Maybe b`, you can exploit the fact that `Maybe` is an injective type constructor to simplify the problem to deciding if `a ~ b`. But if you have an ad-hoc type function which is not injective, say `Foo`, then it's possible that `Foo a ~ Foo b` even if `a /~ b`. It's worse if you have two different type functions - `Foo a ~ Bar b`. Those types fail to unify immediately because `Foo` is definitely different to `Bar` - but depending on the final values of `a` and `b`, the types they result in may unify.