Typeclass Laws¶
Runtime-checkable typeclass-law scaffolding.
laws
¶
Runtime checks of the functor and monad laws.
Each function takes a typeclass instance and a small set of representative objects or morphisms, then asserts that the relevant laws hold to within numerical tolerance by comparing the tensors of the two sides of each equation.
The same laws are recorded as equations in the theories of
quivers.monadic.theories, so a law is
stated once as a Python predicate and once as a theory equation.
References
- Hughes, J. (2000). Generalising monads to arrows. Science of Computer Programming, 37(1-3), 67-111. doi:10.1016/S0167-6423(99)00023-4
check_functor_laws
¶
check_functor_laws(inst: Functor, A: SetObject, B: SetObject, C: SetObject, f: Morphism, g: Morphism) -> None
Assert the two functor laws hold for inst on the given data.
Laws:
- identity:
F(id_A) = id_{F(A)} - composition:
F(g ∘ f) = F(g) ∘ F(f)
| PARAMETER | DESCRIPTION |
|---|---|
inst
|
The functor under test.
TYPE:
|
A
|
The objects
TYPE:
|
B
|
The objects
TYPE:
|
C
|
The objects
TYPE:
|
f
|
A morphism
TYPE:
|
g
|
A morphism
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If either law fails. |
Source code in src/quivers/monadic/laws.py
check_monad_laws
¶
Assert the three monad laws hold for inst at A.
Laws, in terms of pure and join:
- left unit:
join_A ∘ pure_{F(A)} = id_{F(A)} - right unit:
join_A ∘ F(pure_A) = id_{F(A)} - associativity:
join_A ∘ F(join_A) = join_A ∘ join_{F(A)}
The check materializes F(F(F(A))), so A should be small for
instances whose carrier grows quickly, such as function spaces.
| PARAMETER | DESCRIPTION |
|---|---|
inst
|
The monad under test.
TYPE:
|
A
|
The object at which the laws are checked.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If any law fails. |