Typeclass Laws¶
check_functor_laws and check_monad_laws test the
identity and associativity laws of a type class instance on
chosen objects (Moggi, 1991; Wadler, 1992). A passing check covers the values tested,
and a proof that a law holds must cover the implementation's whole domain.
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 equations of the theories in
quivers.monadic.theories, stated there
first-order over a universe of combinators, so a law is stated once as
a Python predicate on tensors and once as a theory equation that
panproto composes, transports along views, and checks in finite models.
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
LawCheck
didactic-model
¶
Bases: Model
How one law of a typeclass's theory fared on an instance.
| ATTRIBUTE | DESCRIPTION |
|---|---|
law |
The law's name in the class's theory (
TYPE:
|
status |
TYPE:
|
checked_at |
The objects a checked law was evaluated at.
TYPE:
|
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. |
Source code in src/quivers/monadic/laws.py
check_typeclass_laws
¶
Check an instance against every law of its class's theory, by name.
The laws are those of the class's theory and every theory it extends
(signature_of(typeclass).laws). A law whose counterpart on tensors
the instance's operations evaluate is checked at A: the functor
identity and composition laws, and the monad unit and associativity
laws in their join form. Every other law (the combinator and
coherence equations of the theory) is recorded as assumed.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The instance.
TYPE:
|
typeclass
|
The class it is an instance of.
TYPE:
|
A
|
A small object the laws are checked at.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[LawCheck, ...]
|
One record per law, in the theory's order. |
| RAISES | DESCRIPTION |
|---|---|
InstanceError
|
With code |
Source code in src/quivers/monadic/laws.py
Cited sources¶
Eugenio Moggi. Notions of Computation and Monads. Information and Computation, 93(1):55–92, 1991. URL: https://www.sciencedirect.com/science/article/pii/0890540191900524, doi:10.1016/0890-5401(91)90052-4.
Philip Wadler. The Essence of Functional Programming. In Proceedings of the 19th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 1–14. ACM, 1992. URL: https://doi.org/10.1145/143165.143169, doi:10.1145/143165.143169.