What evidence is there that Clojure Lightning will benefit from expression as a comonade?

In this presentation [2005] we read on slide 32:

The zipper data type hides the command. This is exactly who needs to structure attribute scores.

So, you can express Lightning in terms of Comonads . It even seems possible in Scala .

Looking at the source of the lightning , we see lightnings designated as Clojure metadata.

My question is: What is the evidence that Clojure Lightning will benefit from expression as a comonade?

Eric offers an advantage

so we need to get all the possible lightning compared to the original group!

+5
source share
1 answer

There is some structural mistake to what you ask. It is not that lightnings can be expressed as Komonads, but instead they are by nature.

Similarly, integers are simply monoids (in two ways!), Regardless of whether you decide to accept this fact.

So, instead of asking what benefits you should ask, “can I improve clarity by recognizing the accompanying structure?”

The answer is yes!


The comonadic structure means that on any zipper there are two interesting methods. The first is obvious and obviously useful - the here function. To make this more specific, I will create a zipper list

data Zipper a = Zipper { before :: [a], here :: a, after :: [a] } 

and now here :: Zipper a -> a is a comonadic function, commonly known as extract .

 extract = here 

Thus, it may be fair to say that every time you examine the thing pointed to by lightning, you use the comonadic interface.

However, extract is the boring side of the interface. More interesting is extend .

 extend :: (Zipper a -> b) -> Zipper a -> Zipper b 

What extend captures is the idea of ​​applying a “contextualized transformation” to each zippered element. The comonadic structure notes that there is a standard and well-structured method for this, which arises from the " extend ing" conversion to the whole comonad.

As an example, you can apply convolution to the list, for example, a small blur function:

 blurKernel :: Fractional a => Zipper a -> a blurKernel (Zipper prior current future) = (a + current + c) / 3 where a = case prior of [] -> 0 (p:ps) -> p c = case future of [] -> 0 (p:ps) -> p blur :: Fractional a => Zipper a -> Zipper a blur = extend blurKernel 

So why write blur in these terms? Is there a natural, recursive, or iterative formulation that works the same and is more obvious?

Well, recognizing that blur based on a comonad extension, we discovered a common structure in our operations with Zipper. This may be useful for maintaining DRY.

We also began to understand something deep about lightning. Each zipper has comonadic extend , so perhaps we can generalize blur to all Fractional type zippers, somehow generalizing only blurKernel and extend ing it in every zipper that we care about.


In any case, I hope that my example demonstrates that lightning is comonads, whether you notice it or not.

This is usually with good Haskell abstractions - they are natural properties regarding how certain types of code work. Type classes just grab them for convenience. Maybe / State / List / etc would be monads, even if they were not Monad s. And Zipper / Store / Trace would be Komonad, even if they were not Comonad s.

+8
source

Source: https://habr.com/ru/post/1200648/


All Articles