Skip to content(if available)orjump to list(if available)

Functors: Identity, Composition, and fmap

munchler

Here's a question to test your understanding of functors: Is the Haskell type `Set e` a functor? (Where `Set e` is a set of elements of type `e`.)

Does the answer vary depending on the programming language? E.g. Is the `Set<'e>` type in F# a functor?

ossopite

ROT13

Fb V guvax gur nafjre eribyirf nebhaq gur pbzcbfvgvba ynj. Va bgure jbeqf, vf sznc (sznc frg s) t rdhny gb sznc frg (t . s) sbe nal s naq t?

V guvax guvf ubyqf bayl vs s naq t ner cher (be znlor whfg vs t vf cher?). fb znlor vs lbh vtaber hafnsr bcrengvbaf va unfxryy vg'f gehr? vg'f pregnvayl abg gehr va s#/bpnzy. vzntvar gung s vf sha _ -> 0 naq t vf sha _ -> arkg_vag_sebz_zhgnoyr_pbhagre (). gura sznc frg (t . s) qbrfa'g punatr gur fvmr bs gur frg juvyr sznc (sznc frg s) t cebqhprf n fvatyrgba frg.

munchler

Let's assume all the functions are pure. Otherwise, even Maybe would not be a functor.

ossopite

Gung'f snve. Gura V guvax, abg pbafvqrevat nal cnegvphyne cebtenzzvat ynathntr, gur nafjre qrcraqf ba ubj Frg qrgrezvarf rdhnyvgl bs ryrzragf. Vs vg hfrf fbzr vzcyrzragngvba bs rdhnyvgl/pbzcnevfba sbe glcr r gung pna fnl gung fbzr inyhrf bs glcr r ner rdhny gung ner arireguryrff qvfgvathvfunoyr, gura Frg vfa'g n shapgbe

TheMatten

Another fun one in case someone's interested: the post shows an example of a type that may sometimes lack the inner value of a given type (`Maybe a`), but what about type that never contains such inner value? Would it be useful? And could you define some interface in style of `Functor` class that would prove this property?

chrisoverzero

Is the answer (ROT13 for spoilers):

V vzntvar gung vg pbhyq abg or orpnhfr Frg erdhverf gur rkgen fngvfsnpgvba bs Rd, evtug?

After looking it up: Nu, lrf, ohg Beq. Vagrerfgvat ubj gung vzcyrzragngvba qrgnvy yrnxf.

Even later: Unat ba, gurer'f ab jnl vg pbhyq or rira vs gung fngvfsnpgvba pbhyq or birepbzr! Vzntvar n frg pbagnvavat sbhe naq artngvir sbhe naq lbh znc `fdhner` bire vg… Gur fvmr bs gur pbyyrpgvba jbhyq punatr!

munchler

> Even later: ...

Yes, now you've hit the crux of the problem. Does that violate the functor laws?

nasso_dev

> Gur fvmr bs gur pbyyrpgvba jbhyq punatr!

Ubj vf guvf n ceboyrz gubhtu? Vg qbrfa'g frrz gb ivbyngr nal bs gur shapgbe ynjf!

branperr

Caesar cypher +7

Zv tf buklyzahukpun, ha slhza pu QZ dolyl P't mhtpsphy, pz aoha pa dvbsk il. Ylhzvu ilpun pz aoha Zla jhu lzzluaphssf il hu Hyyhf, dopjo pz h mbujavy: jvuza zlaThw = (zla) => (mu) => uld Zla(zla.rlfz().thw(mu))

Mvy vaoly shunbhnlz, P'k nblzz aopz klwlukz vu pm fvb jhu palyhal vu paz lsltluaz.

munchler

I think there are some differences between those two types that are potentially relevant.

jackweirdy

What a delightful little question!

munchler

Thanks. I ran into it in a real life situation, and was surprised that the answer is so nuanced. It had me questioning my sanity for a while. :)

tmoertel

Do you mean to ask whether `Set` (no `e`) is a functor?

munchler

Yes, I was just trying to be descriptive by including the full signature.

tmoertel

Ok. It's just that, as asked, the answer is obviously no because if F = `Set e`, F is not a type constructor, so F cannot be a functor.

null

[deleted]

ryandv

ROT13

Gur nafjre vf lrf, ohg jvgu n yvggyr ovg zber ahnapr guna rkcrpgrq. Jr arrq bayl fubj gur gjb shapgbe ynjf, `sznc vq == vq` naq `sznc (s . t) = sznc s . sznc t`. Gubhtu gur dhrfgvba jnf (cerfhznoyl) cbfrq sbe Qngn.Frg (sebz gur pbagnvaref cnpxntr), pbafvqre vafgrnq gur pnfr bs Qngn.UnfuFrg sebz gur habeqrerq-pbagnvaref cnpxntr sbe vgf fvzcyre vzcyrzragngvba bs `Qngn.UnfuFrg.znc` juvpu jvyy nffvfg jvgu gur sbyybjvat qrzbafgengvba [0]:

                      UnfuFrg.znc vq = sebzYvfg . Yvfg.znc vq . gbYvfg
                      UnfuFrg.znc vq = sebzYvfg .          vq . gbYvfg -- fvapr Yvfg vf n shapgbe haqre Yvfg.znc, Yvfg.znc borlf gur svefg shapgbe ynj
                      UnfuFrg.znc vq = sebzYvfg . gbYvfg
                      UnfuFrg.znc vq = vq -- pbzcbfgvba bs vairefrf gbYvfg naq sebzYvfg (zbqhyb beqre...)

                      UnfuFrg.znc s = sebzYvfg . Yvfg.znc s . gbYvfg
      UnfuFrg.znc s . UnfuFrg.znc t = sebzYvfg . Yvfg.znc s . gbYvfg . sebzYvfg . Yvfg.znc t . gbYvfg
      UnfuFrg.znc s . UnfuFrg.znc t = sebzYvfg . Yvfg.znc s . vq . Yvfg.znc t . gbYvfg                 -- pbzcbfgvba bs vairefrf gbYvfg naq sebzYvfg (zbqhyb beqre...)
      UnfuFrg.znc s . UnfuFrg.znc t = sebzYvfg . Yvfg.znc s . Yvfg.znc t . gbYvfg
      UnfuFrg.znc s . UnfuFrg.znc t = sebzYvfg . Yvfg.znc (s . t) . gbYvfg                             -- fvapr Yvfg vf n shapgbe haqre Yvfg.znc, Yvfg.znc borlf gur frpbaq shapgbe ynj
      UnfuFrg.znc s . UnfuFrg.znc t = UnfuFrg.znc (s . t)
Gurer vf n ovg bs unaqjnivat urer nf, fgevpgyl fcrnxvat, `UnfuFrg.gbYvfg . UnfuFrg.sebzYvfg` qbrf abg arprffnevyl pbzcevfr gur vqragvgl shapgvba - gur yvfg pbhyq va snpg or erghearq va n qvssrerag beqre, fhpu gung, sbe vafgnapr, `(UnfuFrg.gbYvfg . UnfuFrg.sebzYvfg $ [2,1]) == [1,2]` juvpu vf boivbhfyl abg gur bevtvany vachg `[2,1]`; ohg fvapr jr'er cnpxvat rirelguvat vagb na habeqrerq UnfuFrg va gur raq naljnl, V qba'g guvax vg znggref naq jr pna punyx vg hc gb "vzcyrzragngvba qrgnvyf" bs gbYvfg naq sebzYvfg.

Fb jul qbrfa'g Qngn.UnfuFrg vafgnapr gur Shapgbe glcrpynff va Unfxryy? Vs lbh gel vg bhg lbh'yy trg lbhe nafjre:

    Ceryhqr> vzcbeg dhnyvsvrq Qngn.UnfuFrg nf UnfuFrg
    Ceryhqr UnfuFrg> vafgnapr Shapgbe UnfuFrg.UnfuFrg jurer sznc = UnfuFrg.znc

    <vagrenpgvir>:2:47: reebe:
        • Ab vafgnapr sbe (unfunoyr-1.3.0.0:Qngn.Unfunoyr.Pynff.Unfunoyr o)

    Ceryhqr UnfuFrg> :g UnfuFrg.znc
    UnfuFrg.znc
      :: (unfunoyr-1.3.0.0:Qngn.Unfunoyr.Pynff.Unfunoyr o, Rd o) =>
         (n -> o) -> UnfuFrg.UnfuFrg n -> UnfuFrg.UnfuFrg o
Fb, juvyr UnfuFrg znl or n "shapgbe" va fcvevg, vg pna'g or gur cnegvphyne pncvgny-S Shapgbe va Unfxryy qhr gb gur grpuavpnyvgl bs gur nqqrq Unfunoyr glcrpynff pbafgenvag.

Jung qbrf guvf zrna sbe Qngn.Frg? UnfuFrg vf shapgvbanyyl gur fnzr nf Qngn.Frg sebz pbagnvaref; gurl obgu fhccyl n "frg" nofgenpg qngn glcr, ohg qvssre va gur qngn fgehpgher hfrq gb vzcyrzrag vg (unfuzncf if. ovanel gerrf), naq lbh raq hc abg orvat n Shapgbe qhr gb na rkgen Beq glcrpynff pbafgenvag vafgrnq bs gur Unfunoyr bar vzcbfrq ol UnfuFrg. Guhf, vs UnfuFrg pna or n "shapgbe", V frr abguvat ceriragvat Frg sebz orvat bar nf jryy nf gurl ner zber be yrff gur fnzr qngn glcr, ybbxvat cnfg gurve vzcyrzragngvba qrgnvyf.

Abgr gung gur pnirng tvira va gur qbphzragngvba:

> Vg'f jbegu abgvat gung gur fvmr bs gur erfhyg znl or fznyyre vs, sbe fbzr (k,l), k /= l && s k == s l

... vf n erq ureevat, nf guvf unf ab ornevat ba gur shapgbe ynjf.

[0] uggcf://unpxntr.unfxryy.bet/cnpxntr/habeqrerq-pbagnvaref-0.2.20/qbpf/fep/Qngn.UnfuFrg.Vagreany.ugzy#znc