Arrow Group

In functional programming, an Arrow is an abstraction that generalizes the notion of functions and enables working with computations as first-class objects. This concept was introduced by John Hughes for structuring programs where computations can be combined and transformed into more complex constructs. In Scala, this concept is implemented through type classes, which provide the flexibility to build compositional programs, modeling composable relationships between two types — for example, a function A => B, Kleisli (wrapping A => F[B]) or Cokleisli (wrapping F[A] => B). An arrow F[A, B] can be viewed as a representation of a computation from A to B with some context, similar to how a functor/applicative/monad F[A] represents a value A with context.

The Arrow Group includes several related type classes. These type classes enable:

Usage

The Arrow group finds application in various areas of functional programming. These abstractions enable working with computations as first-class objects, providing flexibility and modularity. Here are the main areas where the Arrow group is used:

Diagram

classDiagram
    class Compose~ =>: ~{
        +compose[A, B, C](f: B =>: C, g: A =>: B) A =>: C
    }
    class Category~ =>: ~{
        +id[A]: A =>: A    
    }
    Compose <|-- Category 
    class Split~ =>: ~{
        +split[A, B, C, D](f: A =>: B, g: C =>: D) (A, C) =>: (B, D)   
    }
    Compose <|-- Split 
    class Profunctor~ =>: ~{
        +mapfst[A, B, C](fab: A =>: B)(f: C => A) C =>: B
        +mapsnd[A, B, C](fab: A =>: B)(f: B => C) A =>: C
        +dimap[A, B, C, D](fab: A =>: B)(f: C => A)(g: B => D) C =>: D
    }
    class Strong~ =>: ~{
        +first[A, B, C](fa: A =>: B) (A, C) =>: (B, C)
        +second[A, B, C](fa: A =>: B) (C, A) =>: (C, B)
    }
    Profunctor <|-- Strong 
    class Arrow~ =>: ~{
        +arr[A, B](f: A => B) A =>: B
    }
    Category <|-- Arrow 
    Split <|-- Arrow
    Strong <|-- Arrow
    class Choice~ =>: ~{
        +choice[A, B, C](fac: A =>: C, fbc: B =>: C) Either[A, B] =>: C
    }
    Category <|-- Choice
    class ArrowChoice~ =>: ~{
        +choose[A, B, C, D](f: A =>: C)(g: B =>: D) Either[A, B] =>: Either[C, D]
    }
    Choice <|-- ArrowChoice
    Arrow <|-- ArrowChoice

References: