lawful: Assert the lawfulness of your typeclass instances.

[ bsd3, library, safe ] [ Propose Tags ] [ Report a vulnerability ]

Assert the lawfulness of your typeclass instances.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4.7 && <5) [details]
License BSD-3-Clause
Copyright (c) 2018 Matt Noonan
Author Matt Noonan
Maintainer matt.noonan@gmail.com
Category Safe
Home page https://github.com/matt-noonan/lawful#readme
Source repo head: git clone https://github.com/matt-noonan/lawful
Uploaded by mnoonan at 2018-06-15T15:35:53Z
Distributions LTSHaskell:0.1.0.0, NixOS:0.1.0.0, Stackage:0.1.0.0
Reverse Dependencies 1 direct, 11 indirect [details]
Downloads 1353 total (17 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-06-15 [all 1 reports]

Readme for lawful-0.1.0.0

[back to package description]

lawful: Assert that your typeclass instances are lawful

What is this package for?

This small library provides a single two-parameter typeclass Lawful c t, where c is a typeclass and t is a type. Declaring an instance of Lawful C T is an assertion that "the instance for C T obeys the laws of the class C (whatever that means!)"

For example, a lawful instance of Eq T should satisfy the reflexive law x == x for all x :: T. This is certainly true for most types, such as Int or [a] when Eq a is lawful, so we can define

Lawful Eq Int
Lawful Eq a => Lawful Eq [a]

But it isn't true for Double:

λ> nan = 0 / 0 :: Double
λ> nan == nan
False

Why is there a c t constraint on Lawful c t?

This constraint lets you use a Lawful c t wherever a c t would be expected, as in:

same :: Lawful Eq a => a -> a -> Bool
same x y = x == y

How do I know what laws are expected from a typeclass?

If everybody more-or-less agrees on what the right laws are, hopefully they bothered to write them down somewhere. If they didn't, then sorry! You're on your own!

Shouldn't all typeclass instances be lawful anyway, making this package useless?

Wouldn't that be nice?