The Haskell Unfolder Episode 35: distributive and representable functors

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 พ.ย. 2024

ความคิดเห็น • 4

  • @DrBartosz
    @DrBartosz 3 วันที่ผ่านมา +2

    Cool to see Representable used on a non-trivial example. One way of thinking about Representable is that the index type is like a logarithm of your data type (if you visualize the function type i->a as an exponential a^i). Three and Grid are both product types, and you can always take a logarithm of a product--you get a sum. As you mentioned, list is not representable because it's a sum type: List a = 1 + a * List a. An infinite list, on the other hand, is an infinite product, so it's representable.

  • @pedrovasconcelos8260
    @pedrovasconcelos8260 4 วันที่ผ่านมา

    Great video as usual! I have a question regarding performance overhead: can GHC optimize away the Distributive/Representable for Grid and obtain something similar to the initial hand written code? I initially thought yes because all the functions involved are non-recursive, but then realized that the indices IxThree exist at run-time representations and GHC will not be clever enought to optimize it way.

  • @larsbrunjes1798
    @larsbrunjes1798 2 วันที่ผ่านมา

    Why don't you use tabulate to define positions, so you don't need that Applicative instance?
    positions grid = tabulate $ \i -> (index grid i, setter i grid)

    • @andresloeh
      @andresloeh วันที่ผ่านมา +1

      Yes, that would have been simpler.