Take this simple data type (from the Uniplate documentation ):
data Expr = Val Int
| Neg Expr
| Add Expr Expr
I want to check if the expression tree contains a certain operation (in our case, Negand Add).
If we get Uniplatefor Expr, we can use universeto write these two simple functions:
hasNeg :: Expr -> Bool
hasNeg e = not $ null [() | Neg{} <- universe e]
hasAdd :: Expr -> Bool
hasAdd e = not $ null [() | Add{} <- universe e]
I would like to extract the general code and write some “general” function that will take some information about the constructor, but I can’t even think of a signature of the appropriate type, which is usually a bad sign. Does this function make any sense and what is its correct way?
Thank!
source
share