cartesian

class Hom(*xs, **ys)

Hom functor: maps type pairs to their callable types.

Class Methods

  • new : ... -> Type

  • fmap : (A -> B) -> (X -> A) -> X -> B

  • cofmap : (X -> Y) -> (Y -> A) -> X -> A

  • compose : (A -> B) -> (B -> C) -> A -> C

  • eval : A -> (A -> B) -> B

The type Hom(A, B) describes callables with input in A and output in B, it can be used as a decorator to type a function definition.

Example:

>>> @Hom(Int, Str):
... def bar(n):
...     return n * "|"
...
>>> bar
Int -> Str : bar
>>> bar(8)
Str : '||||||||'
Object

alias of HomObject

classmethod compose(f, *fs)

Pipe a collection of functions.

The usual composition of two functions f @ g is obtained as Hom.compose(g, f). Applied on an input x, this returned pipe satisfies:

>>> Hom.compose(f, *fs)(x) == Hom.compose(*fs)(f(x))
True

Note: Composition is made associative by storing the sequence of functions in a flat tuple. This also avoids nesting function closures.

classmethod curry(f, xs)

Curried function applied to n-ary input xs for n < arity.

Example:

>>> Int.add
Int -> Int -> Int: add
>>> Int.add(2, 3)
Int: 5
>>> Int.add(2):
Int -> Int: add 2
>>> Int.add(2)(3)
Int: 3
classmethod eval(x, f)

Evaluate f on input x.

Parameters:
  • x (A)

  • f (callable[A, B])

Return type:

B

src

alias of Type

tgt

alias of Type

class Prod(*xs, **ys)

Class Methods

  • new : ... -> Type

  • fmap : ((A -> B), ...) -> (A, ...:src) -> (B, ...:tgt)

class Object(*xs)

Product base type: tuple alias.

Unit

alias of ()

classmethod branch(f, *fs)

Universal property of categorical products.

Given a collection of arrows with the same source X, return an arrow from X to the product of targets.

(X -> A, X -> B, …) -> X -> (A, B, …)

The terminal arrow to (A, B, ...) has the input maps as projections.

Parameters:
  • f (Callable)

  • fs (Callable)

Return type:

Callable

classmethod fmap(*fs)

Map a collection of functions to their joint action.

Given maps f : X -> A, g : Y -> B, … return the product map

(f, g, …) : (X, Y, …) -> (A, B, …)

Parameters:

fs (Callable)

Return type:

Callable

class Writer(W, bases=(), dct=None)

Constructor of Writer functors.

Given a writing type W, the functor Writer W maps any type A to the pair type (W, A).

When W is a monoid, the functor Writer W is also a monad:

unitA -> (W, A)
a -> (1, a)
join(W, (W, A)) -> (W, A)
(v, (w, a)) -> (v op w, a)

A particular example is Str, which is useful for writing logs or error messages.

Parameters:

W (type)

class WriterFunctor(*xs, **ys)

Base class for Writer functors.

class Object(*xs)
classmethod fmap(*fs)

Map a collection of functions to their joint action.

Given maps f : X -> A, g : Y -> B, … return the product map

(f, g, …) : (X, Y, …) -> (A, B, …)

class Either(*xs, **ys)

Direct sum of types (union).

Class Methods

  • new : ... -> Type

  • fmap : (A -> B) -> (A) -> (B)

  • unit : A -> (A)

  • lift2 : (A -> B -> C) -> (A) -> (B) -> (C)

  • join : ((A)) -> (A)

  • bind : (A) -> (A -> (B)) -> (B)

Example:

>>> Either(Int, Str)
Either : (Int | Str)
>>> (Int | Str) is Either(Int, Str)
True
>>> x = List(int | str)(["Hello", 12, "World!", 8])
>>> foo = Hom(str, int)(len)
>>> bar = Hom(int, str)(lambda x: "|" * x)
### functorial map: type-wise application
>>> Either.fmap(bar, foo)
(int | str | ...) -> (str | int | ...) : (λ | len | ...)
>>> List.fmap(Either.fmap(bar, foo))(x)
List (str | int | ...) : [1 : 5, 0 : ||||||||||||, 1 : 6, 0 : ||||||||]
Unit

alias of :py:class:`~fp.meta.type.∅ `

classmethod fmap(*fs)

Apply a mapping on each case.

classmethod gather(f, *fs)

Universal property of categorical sums.

Given a collection of arrows with the same target Y, return an arrow from the sum of sources to Y.

(A -> Y, B -> Y, …) -> (A | B | …) -> Y

The initial arrow from (A | B | ...) has the input maps as as restrictions.

Parameters:
  • f (Callable)

  • fs (Callable)

Return type:

Callable

classmethod join(EEx)

Access unwrapped either value.

Parameters:

EEx (cls(cls('A', ...), ...))

Return type:

cls(‘A’, …)

classmethod new(*As)

Type union.