Where is Haskell category composition used regardless of instance?

I think I almost understood what a category class represents. However, at this level of abstraction, it makes me wonder where I could find universal use for it.

What code using .or idfrom Control.Categoryfound practical application for different instances?

+3
source share
1 answer

Miles has recently been common. Consider infinite flow

data Stream a = Stream a (Stream a)

right now we can write endless streams that can be used, for example [1..]

oneUpTo :: Stream Int
oneUpTo = go 1 where go n = Stream n (go (n+1))

but sometimes it’s useful to record streams that input may influence. To do this, we will hide the next "step" of the stream after the function

data Mealy b a = Mealy (b -> (a, Mealy b a))

, echo

echo :: a -> Mealy (Maybe a) a
echo a = Mealy go where
  go Nothing   = (a,  echo a )
  go (Just a') = (a', echo a')

switch "" . , . , () .

, Mealy a Category.

instance Category Mealy where
  id = Mealy (\a -> (a, id))
  Mealy bc . Mealy ab = Mealy $ \a -> case ab a of
    (b, nab) -> case bc b of
      (c, nbc) -> (c, nbc . nab)

Mealy, , , .

, machines Hackage.

+6

All Articles