edwisdom / chords

Convert Chords to Pitches
BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link

Abstractions Galore #14

Closed ChrisEPhifer closed 4 years ago

ChrisEPhifer commented 4 years ago

I think there are a few places where we might benefit from writing a type class. I have some comments on this in #12, in particular about a proposed Invertible type class for objects admitting a sensible notion of inversion (which is... a lot of stuff.)

In particular, I would propose that the following type class be defined among our abstractions:

class Invertible a where
  -- invert must be involutive. That is, `(invert . invert) x == x` for every `x :: a`.
  invert :: a -> a

We can get a whole bunch of leverage out of this by defining some instances over data structures we care about, too, eg:

instance (Functor f, Invertible a) => Invertible (f a) where
  invert = fmap invert

instance Invertible a => Invertible (Set a) where
  invert = Data.Set.map invert

It would be worth thinking about whether or not there are other opportunities for ad hoc polymorphism; this ticket can be used for discussion about that.

ChrisEPhifer commented 4 years ago

Per the discussion in #19, I'm going to work on this (plus a couple of other refactors/abstraction efforts) in a new branch, typeclass-refactors.

edwisdom commented 4 years ago

Sounds good! Let me know if there's any instances we're missing of functions that you'd like me to write, e.g. invert for chords or transpose for intervals, etc.

ChrisEPhifer commented 4 years ago

Closing since we've merged the other work / are aware of some of the other opportunities for ad hoc abstraction.