“Pattern matching” of algebraic type data constructors

后端 未结 5 1777
天涯浪人
天涯浪人 2021-02-07 15:07

Let\'s consider a data type with many constructors:

data T = Alpha Int | Beta Int | Gamma Int Int | Delta Int

I want to write a function to che

5条回答
  •  温柔的废话
    2021-02-07 15:41

    Look at the Data.Data module, the toConstr function in particular. Along with {-# LANGUAGE DeriveDataTypeable #-} that will get you a 1-line solution which works for any type which is an instance of Data.Data. You don't need to figure out all of SYB!

    If, for some reason (stuck with Hugs?), that is not an option, then here is a very ugly and very slow hack. It works only if your datatype is Showable (e.g. by using deriving (Show) - which means no function types inside, for example).

    constrT :: T -> String
    constrT = head . words . show
    sameK x y = constrT x == constrT y
    

    constrT gets the string representation of the outermost constructor of a T value by showing it, chopping it up into words and then getting the first. I give an explicit type signature so you're not tempted to use it on other types (and to evade the monomorphism restriction).

    Some notable disadvantages:

    • This breaks horribly when your type has infix constructors (such as data T2 = Eta Int | T2 :^: T2)
    • If some of your constructors have a shared prefix, this is going to get slower, as a larger part of the strings has to be compared.
    • Doesn't work on types with a custom show, such as many library types.

    That said, it is Haskell 98... but that's about the only nice thing I can say about it!

提交回复
热议问题