I would like to write a function that checks that two values were constructed using the same head constructor. This function:
eg. this is not satisfactory (it is linear, and catchall will invalidate the function if I add an extra constructor):
data E = A Int | B String | C
sameCons :: E -> E -> Bool
sameCons t u = case (t, u) of
(A{}, A{}) -> True
(B{}, B{}) -> True
(C{}, C{}) -> True
_ -> False
In OCaml, you can use unsafe functions from a module Obj
to do just that. Can we do something similar in Haskell (a ghc specific solution also works)?
source
share