categories package

Submodules

categories.applicative module

class categories.applicative.Applicative(pure, apply)[source]

Bases: object

categories.applicative.composition_law(u, v, w, type_form=None)[source]
pure (.) <*> u <*> v <*> w == u <*> (v <*> w)
where (<*>) = apply
categories.applicative.homomorphism_law(f, x, _type, type_form=None)[source]
pure f <*> pure x == pure (f x)
where (<*>) = apply
categories.applicative.identity_law(v, type_form=None)[source]
pure id <*> v == v
where (<*>) = apply
categories.applicative.instance(type, pure, apply)[source]
categories.applicative.interchange_law(u, y, type_form=None)[source]
u <*> pure y == pure ($ y) <*> u
where (<*>) = apply
categories.applicative.pure(x, _type, type_form=None)[source]

The optional type_form arg is an attempt to provide some more flexibility with the type that pure is required to cast to. For example, pure for the tuple Applicative instance needs to know the types of its elements so it can return the correct object:

pure(7, tuple, type_form=(str, int)) == (“”, 7)

categories.builtins module

categories.either module

class categories.either.Either(type: str, value: Union[E, A])[source]

Bases: typing.Generic

classmethod left(value: E) → Etr[source]
match(constructor) → bool[source]
classmethod right(value: A) → Etr[source]
categories.either.either(f: Callable, g: Callable, x: categories.either.Either) → categories.either.Either[source]

Given two functions and an Either object, call the first function on the value in the Either if it’s a Left value, otherwise call the second function on the value in the Either. Return the result of the function that’s called.

Parameters:
  • f – a function that accepts the type packed in x
  • g – a function that accepts the type packed in x
  • x – an Either object
Returns:

Whatever the functions f or g return

categories.functor module

class categories.functor.Functor(fmap)[source]

Bases: object

categories.functor.composition_law(f, g, x)[source]

fmap (g . f) == fmap g . fmap f

categories.functor.identity_law(x)[source]

fmap id == id

categories.functor.instance(type, fmap)[source]

categories.instances module

class categories.instances.Typeclass[source]

Bases: object

categories.instances.make_adder(instances: Dict[Type[CT_co], categories.instances.Typeclass]) → Callable[[Type[CT_co], categories.instances.Typeclass], None][source]
categories.instances.make_getter(instances: Dict[Type[CT_co], categories.instances.Typeclass], name: str) → Callable[[Type[CT_co]], categories.instances.Typeclass][source]
categories.instances.make_undefiner(instances: Dict[Type[CT_co], categories.instances.Typeclass]) → Callable[[Type[CT_co]], None][source]

categories.maybe module

class categories.maybe.Maybe(type: str, value: A = None)[source]

Bases: typing.Generic

classmethod just(value: A) → M[source]
match(constructor) → bool[source]
classmethod nothing() → M[source]
categories.maybe.maybe(default: B, f: Callable, x: categories.maybe.Maybe[~A][A]) → B[source]

Given a default value, a function, and a Maybe object, return the default if the Maybe object is Nothing, otherwise call the function with the value in the Maybe object, call the function on it, and return the result.

Parameters:
  • default – This is the value that gets returned if x is Nothing
  • f – When x matches Just, this function is called on the value in x, and the result is returned
  • x – a Maybe object
Returns:

Whatever type default or the return type of f is

categories.monad module

class categories.monad.Monad(bind, mreturn)[source]

Bases: object

categories.monad.associativity_law(m, f, g, type_form=None)[source]

(m >>= f) >>= g == m >>= (x -> f x >>= g)

categories.monad.instance(type, mreturn, bind)[source]
categories.monad.left_identity_law(a, f, _type, type_form=None)[source]

mreturn a >>= f == f a

categories.monad.mreturn(x, _type, type_form=None)[source]
categories.monad.right_identity_law(m, type_form=None)[source]

m >>= return == m

Ex:
Just 7 >>= return == Just 7

categories.monoid module

class categories.monoid.Monoid(mempty, mappend)[source]

Bases: object

categories.monoid.associativity_law(x, y, z)[source]
(x <> y) <> z = x <> (y <> z)
where (<>) = mappend
categories.monoid.identity_law(x)[source]

Assert left and right identity laws:

mempty <> x = x x <> mempty = x

where (<>) = mappend
categories.monoid.instance(type, mempty, mappend)[source]
categories.monoid.mempty(type)[source]

categories.utils module

categories.utils.flip(f: Callable[[A, B], Any]) → Callable[[B, A], Any][source]

Return a function that reverses the arguments it’s called with.

Parameters:

f – A function that takes exactly two arguments

Example:
>>> exp = lambda x, y: x ** y
>>> flip_exp = flip(exp)
>>> exp(2, 3)
8
>>> flip_exp(2, 3)
9
categories.utils.funcall(f: Callable, *args) → Any[source]
categories.utils.id_(x: Any) → Any[source]

The identity function. Returns whatever argument it’s called with.

categories.utils.unit(x: Any) → Any

The identity function. Returns whatever argument it’s called with.

Module contents