2 Answers
2

In general I'd say no, it's not possible. However, if you are trying to solve the practical problem of unwrapping and wrapping all over the place (especially common with newtypes), I often define a mapf f (Type val) = Type (f val) function, analogous to fmap, and then don't export it. You can do the same for a n-ary data type just by passing more functions. If the implementation isn't supposed to be secret, you can export it too (as fmap for unary). I recommend either this kind of map function or views for complicated types because pattern matching will tie you to the implementation.

The basic types already have such functions defined, e.g. maybe and either.

Why do you say that it is not possible in general? You can always define constructors and deconstructor functions to replace explicit pattern matching; and you can always translate a pointed Haskell function to an (arbitrarily complicated) pointfree version. So in general, it is possible.
–
Don StewartMay 6 '11 at 23:10

Just a terminology thing, I thought he was looking for a solution that didn't require a helper function. I.e. point-free pattern matching. If you define a function then it's point-free functions. Destructing a constructor always boils down to a case eventually, right? I suppose you could say practically speaking the generic programming stuff means you don't have to write a case, but only because the compiler put one in the automatic Data instance.
–
Evan LaforgeMay 7 '11 at 4:52

1

maybe and either are actually a fold over the type (even if the datatype is not recursive, that's probably why they are not called folds), because it converts an object of that type into its Church encoding: you supply it a function corresponding to each constructor. To build the return value, calls to those functions replace calls to the constructors of the data type. Your mapf is very similar to fmap, but only for a type with 1 constructor having 1 parameter; otherwise the analogy to folds is more useful.
–
BlaisorbladeJul 23 '11 at 16:19

@mathepic it's much clearer with the points, imho. I once heard it said of Lisp (in reference to Lisp macros): you only have to write the same kind of ugly code once. If you consider pointful code as "ugly", then just write one "ugly" lifting function and then make use of it repeatedly thereafter.
–
Dan BurtonMay 6 '11 at 20:38