[Haskell-cafe] vector-space and standard API for vectors

Alexey Khudyakov alexey.skladnoy at gmail.com
Tue Oct 26 08:51:16 EDT 2010

On 24.10.2010 03:38, wren ng thornton wrote:
> On 10/23/10 4:53 PM, Alexey Khudyakov wrote:
>> On 23.10.2010 05:11, wren ng thornton wrote:
>>> I'd rather see,
>>> class Additive v where -- or AdditiveMonoid, if preferred
>>> zeroV :: v
>>> (^+^) :: v -> v -> v
>>> class Additive v => AdditiveGroup v where
>>> negateV :: v -> v
>> Seems good for me. One more instance declaration to write and no changes
>> in usage.
>> However when written this way it becomes obvious that
>> `zeroV' == `mempty' and ^+^ = mappend. Is Additive really needed then?
> It depends on the usage, since we don't have a nice way of having
> multiple Monoid instances in scope with different identifiers for their
> respective mzero/mappend. For example, in Edward Kmett's monoids[1]
> library he reuses Monoid for additive monoids and adds a new
> Multiplicative class for multiplicative monoids; that way you can use
> operators for a semiring without needing newtype wrappers everywhere in
> order to distinguish the two structures on the same type.
> When dealing with modules and vector spaces we have three or four
> different monoids in play: the additive and multiplicative monoids of
> the underlying semiring/ring/field, and the additive and multiplicative
> monoids of the module/vectorspace. Lacking the aforementioned feature,
> that means there are good reasons to have duplicate classes (i.e.,
> they're all monoids) so long as they are documented as capturing
> different notions (e.g., distinguishing "scalar" and "vectorial" uses).
> I don't care much about the name of the class, I'd just like support for
> monoids, semirings,... when they lack a group, ring,... structure.
Then what about following type class hierarchy? I think it supports 
those structures. Only restriction is that it forces one to have both 
left and right modules. It's possible to split them but I think it will 
be to painful for vector spaces over R and C.

class AdditiveMonoid v where
   (^+^) :: v → v → v
   zeroV :: v

class AdditiveMonoid ⇒ AdditiveGroup v where
   negateV :: v → v
   -- For performance sake
   (^-^) :: v → v → v
   v ^-^ u = v ^+^ negateV u

class Module v where
   type Scalar v :: *
   (*^) :: Scalar v → v → v
   (^*) :: v → Scalar v → v
   (^*) = flip (*^)

class (AdditiveGroup v, Module v) ⇒ VectorSpace v

class VectorSpace v ⇒ InnerSpace v where
   (<.>) :: v → v → Scalar v

More information about the Haskell-Cafe mailing list