Excuse me for my extremely limited Haskell-fu.
I have a series of data types defined in different modules that are structured the same way:
-- in module Foo data Foo = Foo [Param] -- in module Bar data Bar = Bar [Param] -- * many more elsewhere
I would like to have a set of functions that work in the parameter list, for example, to add and remove elements from the list (returning a new Foo or Bar with a different parameter list, depending on the situation).
As far as I can tell, even if I create a typeclass and create instances for each type, I will need to define all these functions each time, that is:
-- in some imported module class Parameterized a where addParam :: a -> Param -> a -- ... other functions -- in module Foo instance Parameterization Foo where addParam (Foo params) param = Foo (param:params) -- ... other functions -- in module Bar instance Parameterization Bar where -- this looks familiar... addParam (Bar params) param = Bar (param:params) -- ... other functions
It feels tiring - far beyond the extent that I begin to think that I am doing something wrong. If you cannot match the template, regardless of the constructor (?), To extract the value, how can you reduce the template?
To refute a possible line of argument: yes, I know that I could just have one set of functions ( addParam , etc.) that would explicitly display each constructor and pattern matching with parameters - but since I built it pretty modularly (modules Foo and Bar pretty self-sufficient), and I am prototyping a system where there will be dozens of these types, a detailed centralized list of type constructors seems ... wrong.
Is it possible that my approach is simply wrong and that it is not possible to structure the type hierarchy as best as possible, but since I cannot have one data type somewhere and add a new constructor for the type in each of these modules (?). I am puzzled by how to get a nice "plugin" without reviewing the simple functions of the utility every time. Any and all good offers were accepted with pleasure.