2

I'm giving my tensor operations a notion of sharing, using a monadic context Shared (implemented as State Nat), so

(+) : Tensor F64 -> Tensor F64 -> Tensor F64

becomes

(+) : Tensor F64 -> Tensor F64 -> Shared $ Tensor F64

If I do this, (+) can't be used in my semigroup. Is there a more general notion of a semigroup (and monoid) that allows for context such as this, so

Semigroup (Tensor F64) where
    (<+>) = (+)

becomes e.g.

SemigroupM Shared (Tensor F64) where
    (<+>) = (+)

and is it implemented in the Idris stdlib?

Tagged Haskell because, and correct me if I'm wrong, the question is essentially the same there.

joel
  • 6,359
  • 2
  • 30
  • 55
  • If you have `(+)` lifted to the `Shared` space, then `Shared $ Tensor F64` is a semigroup/monoid w.r.t. that lifted operation, shouldn't that be enough? How would you use that monadic semigroup thing otherwise? – n. m. could be an AI Feb 12 '23 at 22:27
  • @n.m. How would I use it? I'm not sure tbh, I'm asking to get an understanding of how such a problem is normally treated. I need to accept `Tensor F64`s not `Shared $ Tensor F64`s for [these reasons](https://stackoverflow.com/q/74347734/5986907) – joel Feb 12 '23 at 22:43
  • @n.m. No, it is not enough. The lifted `(+)` has type `(+) :: Shared Tensor -> Shared Tensor -> Shared Tensor`, meaning that, e.g., `mx + mx` will re-execute any side effects of `mx`, whereas `do { x <- mx; x <+> x }` would do the side effects just once. If the side-effects are observable sharing, then ironically accepting `Shared Tensor` as an argument eliminates sharing! – Daniel Wagner Feb 13 '23 at 18:58

0 Answers0