Testing type equality concisely - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

TheMatten

I was unhappy with having to tinker with Refls when trying multiple matches on TypeRep, and I've ended up with these helpers:

-- introduces result in scope
pattern Is :: () => a ~~ b => Maybe (a :~~: b)
pattern Is = Just HRefl

-- let's me pass expected type as type argument
as :: forall a b. Typeable a => TypeRep b -> Maybe (a :~~: b)
as = eqTypeRep typeRep

which can be used as

case typeOf x of
  (as @Int -> Is) -> _ -- x ~~ Int in scope

Am I reinventing the wheel and there's already something similar or better?

Georgi Lyubenov // googleson78

the () => is some hack for pattern synonyms?

TheMatten

PatternSynonyms are sort of interesting because the can both want and provide constraints - to make it possible to declare both situations, GHC recognizes two separate constraints in their signatures as corresponding options

TheMatten

So Is wants () (nothing) and provides a ~~ b :big_smile:

TheMatten

Once we have type applications in patterns, we'll be able to do

As @Int -> _

(I guess)

Georgi Lyubenov // googleson78

so does that mean you can't write (a, b) => ... as a => b => ... instead? :o

TheMatten

Not in signatures of patterns, but you can otherwise

TheMatten

It's a hack, but not a terrible one

Georgi Lyubenov // googleson78

I would snoop around singletons, to see if there is something like what you've defined