6

Is SF already defined anywhere, or does it at least have a name?

data SF a f x = SF a (f x)

instance Functor f => Functor (SF a f) where
  fmap g (SF a fx) = SF a (fmap g fx)
Emily
  • 2,577
  • 18
  • 38
  • It looks like you are declaring a functor which contains another functor. – Code-Apprentice Oct 02 '14 at 19:22
  • AFAIK there isn't one already out there, but that doesn't mean that it doesn't exist in the corner of some random package I've never used. – bheklilr Oct 02 '14 at 19:28
  • @Code-Apprentice, I'm defining a functor transformer. `SF a f` is the same as functor `f`, but it carries an additional value of type `a` attached to it. – Emily Oct 02 '14 at 19:37
  • (It's isomorphic to a product of `Const a` and `Identity`.) – AndrewC Oct 02 '14 at 20:18
  • 3
    @ArtyomKazak Since functors (and applicative functors, [unlike monads](http://stackoverflow.com/q/13034229/1333025)) compose, there is no need for something like (applicative) functor transformer. See `Data.Functor.*` modules in [transformers](http://hackage.haskell.org/package/transformers-0.4.1.0). – Petr Oct 02 '14 at 20:24

2 Answers2

4

Your functor looks like

type SF a f = (,) a :. f

using functor-combo notation.

(I somehow prefer to look at it using composition, rather than using product and Const.)

chi
  • 111,837
  • 3
  • 133
  • 218
1

You could just define functor products

data (f :* g) a = P (f a) (g a) deriving Functor

and then write it directly

type SF a f = Const a :* f
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180