instances

class List(*xs, **ys)

Class Methods

  • new : ... -> Type

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

  • unit : A -> List A

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

  • join : List List A -> List A

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

class Object(xs)
classmethod fmap(f)

Map a function on lists.

Parameters:

f (Hom('A', 'B'))

Return type:

Hom(cls(‘A’), cls(‘B’))

classmethod join(xx)

Flatten a list of lists.

Parameters:

xx (cls(cls('A')))

Return type:

cls(‘A’)

src

alias of Type

tgt

alias of Type

classmethod unit(a)

Singleton list.

class Bool(x)

Boolean values.

show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

class Float(x=0, /)
show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

class Int
show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

class Wrap(*xs, **ys)

Type wrapper lifting selected methods to the container type.

Class Methods

  • new : ... -> Type

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

The class method lift will iterate over the class attribute _lifted_methods_ to assign lifted methods on the wrapped type.

Attributes:

_lifted_methods_ (list):
A list of of triples (name, signature, lift_args) where
  • name (str): name of the method to be lifted,

  • signature (Callable[type, Type]) yielding lifted method type signature(cls),

  • lift_args (int | tuple[int] | type(...)) index of arguments to be unwrapped.

Example

class StrWrapper(Wrap):

    _lifted_methods_ = [
        ('upper', lambda A: Hom(A, A), ...)
        ('isalnum', lambda A: Hom(A, Bool), ...)
        ('join', lambda A: Hom((A, List[A]), Bool), 0)
    ]
Object

alias of WrapObject

classmethod lift(method, homtype, lift_args=Ellipsis)

Lift a method to the wrapped type.

Parameters:
  • method (MethodType)

  • homtype (Type)

  • lift_args (Iterable[int] | ellipsis)

Return type:

HomObject

class WrapObject(data)

Base type for wrapped values.

show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

class State(*xs, **ys)

State bifunctor and monad.

Class Methods

  • new : ... -> Type

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

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

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

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

The type State(S, A) describes stateful computations yielding a return value of type A while acting on the state type S. The isomorphism:

S -> (S, A) ~= State(S, A)

may be used as a decorator do define stateful computations.:

>>> @State(Str, Str)
... def popchar(s):
...     return s[1:], s[0]
...
>>> popchar.run("cat")
(Str, Str) : ('at', 'c')

Calling State with only one argument S will return a StateMonad subclass with a class attribute _state_. Calling this monad on a type A will also return the type State(S, A), i.e.:

>>> isinstance(State(Str), Monad)
True
>>> State(Str, Int) is State(Str)(Int)
True
# monadic types `State S A` are of type `State`
>>> type(State(Str)(Int) is State)
True

Note that this is an important difference with the Stateful implementation, which puts the emphasis on state-type specific implementations. Using the State bifunctor may be considered safer at a little convenience cost (the global class state of Stateful(S, s0) types).

class Object(*args, **kwargs)

Stateful computation.

bind(mf)
exec(s=None)
gets(f, tgt=None)
map(f)

Bound map method, equivalent to Functor.fmap(f)(x).

put(s)
puts(f)
run(s=None)
show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

then(f, tgt=None)
unit(a)
use(state)
classmethod eval(s, f)

Evaluate f on input x.

classmethod fmap(f)

Pushforward : post-composition on the target.

classmethod gets(f)
classmethod new(S, A=Ellipsis)
classmethod put(s)
classmethod puts(f)
class StateMonad(*xs, **ys)

State monad.

Class Methods

  • new : ... -> Type

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

  • unit : A -> StateMonad A

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

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

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

The monad State S maps any type A to the stateful computation type State S A.

classmethod chain(*sts)
classmethod fmap(f)

Map pure callables A -> B by post-transform of the return value.

>>> State.fmap(f)(st_a)(s0) == st_a.exec(s0), f(st_a.eval(s0))
True
classmethod gets(f)

Get and return an image of the state by f : S -> A.

classmethod join(ffa)

Evaluate the returned stateful subprocess.

classmethod new(A)
classmethod put(s)

Update the state and return ().

classmethod puts(f)

Update the state by an image of f : A -> S.

src

alias of Type

tgt

alias of Type

classmethod unit(a=())

Return a value a : A.

class Stateful(S, initial=None, dct=None)
class StatefulMonad(*xs, **ys)

Stateful Monads on a pointed state type.

Fully stateful monads are defined by both a state type S and an initial state _initial_ : S.

>>> MyString = Stateful(Str, "Hello World!")
class Object(*args, **kwargs)
arity: int = 1
run(s=None)
show()

Print x, prefixed by its name if any, and return x.

Parameters:

x (Any)

Return type:

Any

shows(m)

Print a value x and set its name, returning x.

Parameters:
  • x (Any)

  • m (str)

Return type:

Any

property state
property value
classmethod new(A)
classmethod use(s0)

Context manager for the _initial_ class attribute.

Within a managed block, evaluation of any stateful instance will be computed from s0.

Yields:

put_s0 (cls(Type.Unit)) – a stateful instance with initial state s0 and returning ().

Parameters:

s0 (S)