[spire] mapping on spire types

34 views
Skip to first unread message

Marco Kuper

unread,
Feb 9, 2016, 5:20:27 PM2/9/16
to Typelevel Users & Development List
Is it possible to map on the spire types, as long as the inner type that is mapped to is in the list of specialized types? E.g on a Field[A] the signature would look sth like
def map[B](f: A => B): Field[B]

I am asking because I would like to be able use operators combining types with different realized specialization, e.g. multiply a Field[Int] with a Field[Double]

Cheers 

Erik Osheim

unread,
Feb 9, 2016, 7:28:57 PM2/9/16
to Marco Kuper, Typelevel Users & Development List
Hi Marco,

Most of Spire's type classes can't correctly implement map. Let's look
at a simple example, Monoid[A], and see if we can do it.

trait Monoid[A] { self =>
def id: A // abstract
def op(x: A, y: A): A // abstract
def map[B](f: A => B): Monoid[B] =
new Monoid[B] {
def id: B = f(self.id)
def op(x: B, y: B): B =
f(self.op(x??, x??))
}
}

This would work except for the x?? and y?? parts. In order to use the
underlying Monoid to implement combine we also need a way to go from
(B => A), so we can convert the mapped combine's arguments into A
values so we can use them with the original Monoid.

We *could* give these types a method like imap:

trait Monoid[A] { self =>
def id: A // abstract
def op(x: A, y: A): A // abstract
def imap[B](f: A => B)(g: B => A): Monoid[B] =
new Monoid[B] {
def id: B = f(self.id)
def op(x: B, y: B): B =
f(self.op(g(x), g(y)))
}
}

Does this make sense?

To generalize this a bit (and move from Spire to Cats), we can
characterize any type that supports a map method as having a
cats.Functor (specificallly, a covariant functor). Types like
spire.algebra.Monoid do not have covariant functors, but they do have
a cats.functor.Invariant (an invariant functor) which has the more
restrictive imap method.

Spire itself does not currently put an imap method on these type
classes, but I would be open to adding it. Also, if you are using
Cats, you could define an instance of Invariant[Field] (for example)
which would give you access to .imap via enrichment.

-- Erik
> --
> You received this message because you are subscribed to the Google Groups "Typelevel Users & Development List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to typelevel+...@googlegroups.com.
> To post to this group, send email to type...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/typelevel/f56d753d-230d-4e7d-b6df-e240f05dff4d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marco Kuper

unread,
Feb 10, 2016, 2:36:10 PM2/10/16
to Typelevel Users & Development List, marcok...@googlemail.com, er...@plastic-idolatry.com
Thanks Erik, your explanation does make a lot of sense. I'll look into Cats to see if .imap is what I'm looking for.

Cheers,
Marco
Reply all
Reply to author
Forward
0 new messages