├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── docs ├── _config.yml ├── index.md └── modules │ ├── Either.ts.md │ ├── Option.ts.md │ ├── index.md │ ├── index.ts.md │ └── laws.ts.md ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── Either.ts ├── Option.ts ├── index.ts └── laws.ts ├── test ├── index.ts └── tsconfig.json ├── tsconfig.json └── tslint.json /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Do you want to request a _feature_ or report a _bug_?** 2 | 3 | **What is the current behavior?** 4 | 5 | **If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://codesandbox.io/ or similar.** 6 | 7 | **What is the expected behavior?** 8 | 9 | **Which versions of fp-ts-laws, and which browser and OS are affected by this issue? Did this work in previous versions of fp-ts-laws?** 10 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Before submitting a pull request,** please make sure the following is done: 2 | 3 | - Fork [the repository](https://github.com/gcanti/fp-ts-laws) and create your branch from `master`. 4 | - Run `npm install` in the repository root. 5 | - If you've fixed a bug or added code that should be tested, add tests! 6 | - Ensure the test suite passes (`npm test`). 7 | 8 | **Note**. If you find a typo in the **documentation**, make sure to modify the corresponding source (docs are generated). 9 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | # Allows you to run this workflow manually from the Actions tab 10 | workflow_dispatch: 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | 16 | strategy: 17 | matrix: 18 | node-version: [16.17.1] 19 | 20 | steps: 21 | - uses: actions/checkout@v2 22 | - name: Use Node.js ${{ matrix.node-version }} 23 | uses: actions/setup-node@v1 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | - run: npm install 27 | - run: npm run build --if-present 28 | - run: npm test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | lib 4 | dev 5 | coverage 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "./node_modules/typescript/lib", 3 | "prettier.printWidth": 120, 4 | "prettier.semi": false, 5 | "prettier.singleQuote": true, 6 | "tslint.enable": true 7 | } 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | > **Tags:** 4 | > 5 | > - [New Feature] 6 | > - [Bug Fix] 7 | > - [Breaking Change] 8 | > - [Documentation] 9 | > - [Internal] 10 | > - [Polish] 11 | > - [Experimental] 12 | 13 | **Note**: Gaps between patch versions are faulty/broken releases. 14 | **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. 15 | 16 | # 0.3.0 17 | 18 | - **Breaking Change** 19 | - upgrade `fast-check` dependency to v2 and move to `peerDependencies`, closes #9 (@gcanti) 20 | 21 | # 0.2.1 22 | 23 | - **Polish** 24 | - `Functor` 25 | - better _composition law_ test (@gcanti) 26 | 27 | # 0.2.0 28 | 29 | - **Breaking Change** 30 | - upgrade to `fp-ts@2.x` (@gcanti) 31 | - remove `lib/Validation` module (@gcanti) 32 | - rename `setoid` law to `eq` (@gcanti) 33 | 34 | # 0.1.0 35 | 36 | - **Breaking Change** 37 | - `Functor`, `Apply`, `Applicative` and `Monad` laws are now curried (@gcanti) 38 | 39 | # 0.0.3 40 | 41 | - **New Feature** 42 | - add missing derivedAp test to monad (@giogonzo) 43 | - extract setoid, ord, semigroup, monoid, semiring, ring, field laws (@giogonzo) 44 | 45 | # 0.0.2 46 | 47 | - **New Feature** 48 | - add `Functor` laws (@gcanti) 49 | - add `Apply`, `Applicative`, `Chain`, `Monad` laws (@giogonzo) 50 | 51 | # 0.0.1 52 | 53 | Initial release 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-present Giulio Canti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | build status 4 | 5 |

6 | 7 | [fp-ts](https://github.com/gcanti/fp-ts) type class laws for property based testing 8 | 9 | Usage of [fast-check](https://github.com/dubzzz/fast-check) is required. 10 | 11 | # Example 12 | 13 | ```ts 14 | import * as laws from 'fp-ts-laws' 15 | import * as fc from 'fast-check' 16 | 17 | import { Semigroup } from 'fp-ts/Semigroup' 18 | import { eqString } from 'fp-ts/Eq' 19 | 20 | describe('my semigroup instance', () => { 21 | it('should test Semigroup laws', () => { 22 | const semigroupSpace: Semigroup = { 23 | concat: (x, y) => x + ' ' + y 24 | } 25 | laws.semigroup(semigroupSpace, eqString, fc.string()) 26 | }) 27 | }) 28 | ``` 29 | 30 | For other examples check out the [tests](test/index.ts) 31 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | remote_theme: pmarsceill/just-the-docs 2 | 3 | # Enable or disable the site search 4 | search_enabled: true 5 | 6 | # Aux links for the upper right navigation 7 | aux_links: 8 | 'fp-ts-laws on GitHub': 9 | - 'https://github.com/gcanti/fp-ts-laws' 10 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Home 3 | nav_order: 1 4 | --- 5 | 6 | [fp-ts](https://github.com/gcanti/fp-ts) type class laws for property based testing 7 | 8 | Usage of [fast-check](https://github.com/dubzzz/fast-check) is required. 9 | 10 | # Example 11 | 12 | ```ts 13 | import * as laws from 'fp-ts-laws' 14 | import * as fc from 'fast-check' 15 | 16 | import { Semigroup } from 'fp-ts/Semigroup' 17 | import { setoidString } from 'fp-ts/Setoid' 18 | 19 | describe('my semigroup instance', () => { 20 | it('should test Semigroup laws', () => { 21 | const semigroupSpace: Semigroup = { 22 | concat: (x, y) => x + ' ' + y 23 | } 24 | laws.semigroup(semigroupSpace, setoidString, fc.string()) 25 | }) 26 | }) 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/modules/Either.ts.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Either.ts 3 | nav_order: 1 4 | parent: Modules 5 | --- 6 | 7 | ## Either overview 8 | 9 | Added in v0.0.2 10 | 11 | --- 12 | 13 |

Table of contents

14 | 15 | - [utils](#utils) 16 | - [getEither](#geteither) 17 | - [getLeft](#getleft) 18 | - [getRight](#getright) 19 | 20 | --- 21 | 22 | # utils 23 | 24 | ## getEither 25 | 26 | Returns an `Arbitrary` that yelds both `left`s and `right`s 27 | 28 | **Signature** 29 | 30 | ```ts 31 | export declare function getEither(leftArb: fc.Arbitrary, rightArb: fc.Arbitrary): fc.Arbitrary> 32 | ``` 33 | 34 | Added in v0.0.2 35 | 36 | ## getLeft 37 | 38 | Returns an `Arbitrary` that yelds only `left`s 39 | 40 | **Signature** 41 | 42 | ```ts 43 | export declare function getLeft(arb: fc.Arbitrary): fc.Arbitrary> 44 | ``` 45 | 46 | Added in v0.0.2 47 | 48 | ## getRight 49 | 50 | Returns an `Arbitrary` that yelds only `right`s 51 | 52 | **Signature** 53 | 54 | ```ts 55 | export declare function getRight(arb: fc.Arbitrary): fc.Arbitrary> 56 | ``` 57 | 58 | Added in v0.0.2 59 | -------------------------------------------------------------------------------- /docs/modules/Option.ts.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Option.ts 3 | nav_order: 4 4 | parent: Modules 5 | --- 6 | 7 | ## Option overview 8 | 9 | Added in v0.0.2 10 | 11 | --- 12 | 13 |

Table of contents

14 | 15 | - [utils](#utils) 16 | - [getNone](#getnone) 17 | - [getOption](#getoption) 18 | - [getSome](#getsome) 19 | 20 | --- 21 | 22 | # utils 23 | 24 | ## getNone 25 | 26 | Returns an `Arbitrary` that yelds only `none`s 27 | 28 | **Signature** 29 | 30 | ```ts 31 | export declare function getNone
(): fc.Arbitrary> 32 | ``` 33 | 34 | Added in v0.0.2 35 | 36 | ## getOption 37 | 38 | Returns an `Arbitrary` that yelds both `none`s and `some`s 39 | 40 | **Signature** 41 | 42 | ```ts 43 | export declare function getOption(arb: fc.Arbitrary): fc.Arbitrary> 44 | ``` 45 | 46 | Added in v0.0.2 47 | 48 | ## getSome 49 | 50 | Returns an `Arbitrary` that yelds only `some`s 51 | 52 | **Signature** 53 | 54 | ```ts 55 | export declare function getSome(arb: fc.Arbitrary): fc.Arbitrary> 56 | ``` 57 | 58 | Added in v0.0.2 59 | -------------------------------------------------------------------------------- /docs/modules/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Modules 3 | has_children: true 4 | permalink: /docs/modules 5 | nav_order: 2 6 | --- 7 | -------------------------------------------------------------------------------- /docs/modules/index.ts.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: index.ts 3 | nav_order: 2 4 | parent: Modules 5 | --- 6 | 7 | ## index overview 8 | 9 | Added in v0.0.1 10 | 11 | --- 12 | 13 |

Table of contents

14 | 15 | - [utils](#utils) 16 | - [applicative](#applicative) 17 | - [apply](#apply) 18 | - [eq](#eq) 19 | - [field](#field) 20 | - [functor](#functor) 21 | - [monad](#monad) 22 | - [monoid](#monoid) 23 | - [ord](#ord) 24 | - [ring](#ring) 25 | - [semigroup](#semigroup) 26 | - [semiring](#semiring) 27 | 28 | --- 29 | 30 | # utils 31 | 32 | ## applicative 33 | 34 | Tests the `Applicative` laws 35 | 36 | **Signature** 37 | 38 | ```ts 39 | export declare function applicative( 40 | F: Applicative3 41 | ): ( 42 | lift:
(a: fc.Arbitrary) => fc.Arbitrary>, 43 | liftEq: (Sa: Eq) => Eq> 44 | ) => void 45 | export declare function applicative( 46 | F: Applicative2 47 | ): ( 48 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 49 | liftEq: (Sa: Eq) => Eq> 50 | ) => void 51 | export declare function applicative( 52 | F: Applicative2C 53 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 54 | export declare function applicative( 55 | F: Applicative1 56 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 57 | export declare function applicative( 58 | F: Applicative 59 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 60 | ``` 61 | 62 | Added in v0.1.0 63 | 64 | ## apply 65 | 66 | Tests the `Apply` laws 67 | 68 | **Signature** 69 | 70 | ```ts 71 | export declare function apply( 72 | F: Apply3 73 | ): ( 74 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 75 | liftEq: (Sa: Eq) => Eq> 76 | ) => void 77 | export declare function apply( 78 | F: Apply2 79 | ): ( 80 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 81 | liftEq: (Sa: Eq) => Eq> 82 | ) => void 83 | export declare function apply( 84 | F: Apply2C 85 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 86 | export declare function apply( 87 | F: Apply1 88 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 89 | export declare function apply( 90 | F: Apply 91 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 92 | ``` 93 | 94 | Added in v0.1.0 95 | 96 | ## eq 97 | 98 | Tests the `Eq` laws 99 | 100 | **Signature** 101 | 102 | ```ts 103 | export declare const eq: (E: Eq, arb: fc.Arbitrary) => void 104 | ``` 105 | 106 | Added in v0.0.1 107 | 108 | ## field 109 | 110 | Tests the `Field` laws 111 | 112 | **Signature** 113 | 114 | ```ts 115 | export declare const field: (F: Field, S: Eq, arb: fc.Arbitrary, seed?: number) => void 116 | ``` 117 | 118 | Added in v0.0.1 119 | 120 | ## functor 121 | 122 | Tests the `Functor` laws 123 | 124 | **Signature** 125 | 126 | ```ts 127 | export declare function functor( 128 | F: Functor3 129 | ): ( 130 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 131 | liftEq: (Sa: Eq) => Eq> 132 | ) => void 133 | export declare function functor( 134 | F: Functor2 135 | ): ( 136 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 137 | liftEq: (Sa: Eq) => Eq> 138 | ) => void 139 | export declare function functor( 140 | F: Functor2C 141 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 142 | export declare function functor( 143 | F: Functor1 144 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 145 | export declare function functor( 146 | F: Functor 147 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 148 | ``` 149 | 150 | Added in v0.1.0 151 | 152 | ## monad 153 | 154 | Tests the `Monad` laws 155 | 156 | **Signature** 157 | 158 | ```ts 159 | export declare function monad( 160 | M: Monad3 161 | ): (liftEq: (Sa: Eq) => Eq>) => void 162 | export declare function monad(M: Monad2): (liftEq: (Sa: Eq) => Eq>) => void 163 | export declare function monad( 164 | M: Monad2C 165 | ): (liftEq: (Sa: Eq) => Eq>) => void 166 | export declare function monad(M: Monad1): (liftEq: (Sa: Eq) => Eq>) => void 167 | export declare function monad(M: Monad): (liftEq: (Sa: Eq) => Eq>) => void 168 | ``` 169 | 170 | Added in v0.1.0 171 | 172 | ## monoid 173 | 174 | Tests the `Monoid` laws 175 | 176 | **Signature** 177 | 178 | ```ts 179 | export declare const monoid: (M: Monoid, E: Eq, arb: fc.Arbitrary) => void 180 | ``` 181 | 182 | Added in v0.0.1 183 | 184 | ## ord 185 | 186 | Tests the `Ord` laws 187 | 188 | **Signature** 189 | 190 | ```ts 191 | export declare const ord: (O: Ord, arb: fc.Arbitrary) => void 192 | ``` 193 | 194 | Added in v0.0.1 195 | 196 | ## ring 197 | 198 | Tests the `Ring` laws 199 | 200 | **Signature** 201 | 202 | ```ts 203 | export declare const ring: (R: Ring, S: Eq, arb: fc.Arbitrary, seed?: number) => void 204 | ``` 205 | 206 | Added in v0.0.1 207 | 208 | ## semigroup 209 | 210 | Tests the `Semigroup` laws 211 | 212 | **Signature** 213 | 214 | ```ts 215 | export declare const semigroup: (S: Semigroup, E: Eq, arb: fc.Arbitrary) => void 216 | ``` 217 | 218 | **Example** 219 | 220 | ```ts 221 | import * as laws from 'fp-ts-laws' 222 | import * as fc from 'fast-check' 223 | import { Semigroup } from 'fp-ts/Semigroup' 224 | import { eqString } from 'fp-ts/Eq' 225 | 226 | const semigroupSpace: Semigroup = { 227 | concat: (x, y) => x + ' ' + y 228 | } 229 | laws.semigroup(semigroupSpace, eqString, fc.string()) 230 | ``` 231 | 232 | Added in v0.0.1 233 | 234 | ## semiring 235 | 236 | Tests the `Semiring` laws 237 | 238 | **Signature** 239 | 240 | ```ts 241 | export declare const semiring: (S: Semiring, E: Eq, arb: fc.Arbitrary, seed?: number) => void 242 | ``` 243 | 244 | Added in v0.0.1 245 | -------------------------------------------------------------------------------- /docs/modules/laws.ts.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: laws.ts 3 | nav_order: 3 4 | parent: Modules 5 | --- 6 | 7 | ## laws overview 8 | 9 | Added in v0.1.0 10 | 11 | --- 12 | 13 |

Table of contents

14 | 15 | - [utils](#utils) 16 | - [applicative](#applicative) 17 | - [apply](#apply) 18 | - [chain](#chain) 19 | - [eq](#eq) 20 | - [field](#field) 21 | - [functor](#functor) 22 | - [monad](#monad) 23 | - [monoid](#monoid) 24 | - [ord](#ord) 25 | - [ring](#ring) 26 | - [semigroup](#semigroup) 27 | - [semiring](#semiring) 28 | 29 | --- 30 | 31 | # utils 32 | 33 | ## applicative 34 | 35 | **Signature** 36 | 37 | ```ts 38 | export declare const applicative: { 39 | identity: (F: Applicative, S: Eq>) => (fa: HKT) => boolean 40 | homomorphism: (F: Applicative, S: Eq>, ab: FunctionN<[A], B>) => (a: A) => boolean 41 | interchange: (F: Applicative, S: Eq>) => (a: A, fab: HKT>) => boolean 42 | derivedMap: (F: Applicative, S: Eq>, ab: FunctionN<[A], B>) => (fa: HKT) => boolean 43 | } 44 | ``` 45 | 46 | Added in v0.1.0 47 | 48 | ## apply 49 | 50 | **Signature** 51 | 52 | ```ts 53 | export declare const apply: { 54 | associativeComposition: ( 55 | F: Apply, 56 | S: Eq> 57 | ) => (fa: HKT, fab: HKT>, fbc: HKT>) => boolean 58 | } 59 | ``` 60 | 61 | Added in v0.1.0 62 | 63 | ## chain 64 | 65 | **Signature** 66 | 67 | ```ts 68 | export declare const chain: { 69 | associativity: ( 70 | F: Chain, 71 | S: Eq>, 72 | afb: FunctionN<[A], HKT>, 73 | bfc: FunctionN<[B], HKT> 74 | ) => (fa: HKT) => boolean 75 | derivedAp: (F: Chain, S: Eq>, fab: HKT>) => (fa: HKT) => boolean 76 | } 77 | ``` 78 | 79 | Added in v0.1.0 80 | 81 | ## eq 82 | 83 | **Signature** 84 | 85 | ```ts 86 | export declare const eq: { 87 | reflexivity:
(E: Eq) => (a: A) => boolean 88 | simmetry: (E: Eq) => (a: A, b: A) => boolean 89 | transitivity: (E: Eq) => (a: A, b: A, c: A) => boolean 90 | } 91 | ``` 92 | 93 | Added in v0.1.0 94 | 95 | ## field 96 | 97 | **Signature** 98 | 99 | ```ts 100 | export declare const field: { 101 | commutativity: (F: Field, S: Eq) => (a: A, b: A) => boolean 102 | integralDomain: (F: Field, S: Eq) => (a: A, b: A) => boolean 103 | nonNegativity: (F: Field, S: Eq) => (a: A) => boolean 104 | quotient: (F: Field, S: Eq) => (a: A, b: A) => boolean 105 | reminder: (F: Field, S: Eq) => (a: A, b: A) => boolean 106 | submultiplicative: (F: Field, S: Eq) => (a: A, b: A) => boolean 107 | inverse: (F: Field, S: Eq) => (a: A) => boolean 108 | } 109 | ``` 110 | 111 | Added in v0.1.0 112 | 113 | ## functor 114 | 115 | **Signature** 116 | 117 | ```ts 118 | export declare const functor: { 119 | identity: (F: Functor, S: Eq>) => (fa: HKT) => boolean 120 | composition: ( 121 | F: Functor, 122 | S: Eq>, 123 | ab: FunctionN<[A], B>, 124 | bc: FunctionN<[B], C> 125 | ) => (fa: HKT) => boolean 126 | } 127 | ``` 128 | 129 | Added in v0.1.0 130 | 131 | ## monad 132 | 133 | **Signature** 134 | 135 | ```ts 136 | export declare const monad: { 137 | leftIdentity: (M: Monad, S: Eq>, afb: FunctionN<[A], HKT>) => (a: A) => boolean 138 | rightIdentity: (M: Monad, S: Eq>) => (fa: HKT) => boolean 139 | derivedMap: (M: Monad, S: Eq>, ab: FunctionN<[A], B>) => (fa: HKT) => boolean 140 | } 141 | ``` 142 | 143 | Added in v0.1.0 144 | 145 | ## monoid 146 | 147 | **Signature** 148 | 149 | ```ts 150 | export declare const monoid: { 151 | rightIdentity: (M: Monoid, E: Eq) => (a: A) => boolean 152 | leftIdentity: (M: Monoid, E: Eq) => (a: A) => boolean 153 | } 154 | ``` 155 | 156 | Added in v0.1.0 157 | 158 | ## ord 159 | 160 | **Signature** 161 | 162 | ```ts 163 | export declare const ord: { 164 | totality: (O: Ord) => (a: A, b: A) => boolean 165 | reflexivity: (O: Ord) => (a: A) => boolean 166 | antisimmetry: (O: Ord) => (a: A, b: A) => boolean 167 | transitivity: (O: Ord) => (a: A, b: A, c: A) => boolean 168 | } 169 | ``` 170 | 171 | Added in v0.1.0 172 | 173 | ## ring 174 | 175 | **Signature** 176 | 177 | ```ts 178 | export declare const ring: { additiveInverse: (R: Ring, E: Eq) => (a: A) => boolean } 179 | ``` 180 | 181 | Added in v0.1.0 182 | 183 | ## semigroup 184 | 185 | **Signature** 186 | 187 | ```ts 188 | export declare const semigroup: { associativity: (S: Semigroup, E: Eq) => (a: A, b: A, c: A) => boolean } 189 | ``` 190 | 191 | Added in v0.1.0 192 | 193 | ## semiring 194 | 195 | **Signature** 196 | 197 | ```ts 198 | export declare const semiring: { 199 | addAssociativity: (S: Semiring, E: Eq) => (a: A, b: A, c: A) => boolean 200 | addIdentity: (S: Semiring, E: Eq) => (a: A) => boolean 201 | commutativity: (S: Semiring, E: Eq) => (a: A, b: A) => boolean 202 | mulAssociativity: (S: Semiring, E: Eq) => (a: A, b: A, c: A) => boolean 203 | mulIdentity: (S: Semiring, E: Eq) => (a: A) => boolean 204 | leftDistributivity: (S: Semiring, E: Eq) => (a: A, b: A, c: A) => boolean 205 | rightDistributivity: (S: Semiring, E: Eq) => (a: A, b: A, c: A) => boolean 206 | annihilation: (S: Semiring, E: Eq) => (a: A) => boolean 207 | } 208 | ``` 209 | 210 | Added in v0.1.0 211 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | transform: { 4 | '^.+\\.tsx?$': 'ts-jest' 5 | }, 6 | testRegex: 'test', 7 | moduleFileExtensions: ['ts', 'js'], 8 | testPathIgnorePatterns: [] 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fp-ts-laws", 3 | "version": "0.3.0", 4 | "description": "fp-ts type class laws for property based testing", 5 | "files": [ 6 | "lib" 7 | ], 8 | "main": "lib/index.js", 9 | "typings": "lib/index.d.ts", 10 | "scripts": { 11 | "lint": "tslint -p tsconfig.json src/**/*.ts test/**/*.ts", 12 | "jest": "jest", 13 | "prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test}/**/*.ts\"", 14 | "fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test}/**/*.ts\"", 15 | "test": "npm run prettier && npm run jest && npm run docs", 16 | "clean": "rimraf rm -rf lib/*", 17 | "build": "npm run clean && tsc", 18 | "prepublish": "npm run build", 19 | "mocha": "mocha -r ts-node/register test/*.ts", 20 | "docs": "docs-ts" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/gcanti/fp-ts-laws.git" 25 | }, 26 | "author": "Giulio Canti ", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/gcanti/fp-ts-laws/issues" 30 | }, 31 | "homepage": "https://github.com/gcanti/fp-ts-laws", 32 | "peerDependencies": { 33 | "fast-check": "^2.6.0", 34 | "fp-ts": "^2.0.0" 35 | }, 36 | "devDependencies": { 37 | "@types/jest": "^23.3.13", 38 | "@types/node": "^10.12.18", 39 | "docs-ts": "^0.5.2", 40 | "fast-check": "^2.6.0", 41 | "fp-ts": "^2.16.1", 42 | "jest": "^24.8.0", 43 | "mocha": "^5.2.0", 44 | "prettier": "^1.16.4", 45 | "rimraf": "^2.6.2", 46 | "ts-jest": "^24.0.2", 47 | "ts-node": "^8.0.2", 48 | "tslint": "^5.12.1", 49 | "tslint-config-standard": "^8.0.1", 50 | "typescript": "^3.5.2" 51 | }, 52 | "tags": [ 53 | "fp-ts", 54 | "property-based-testing" 55 | ], 56 | "keywords": [ 57 | "fp-ts", 58 | "property-based-testing" 59 | ] 60 | } 61 | -------------------------------------------------------------------------------- /src/Either.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @since 0.0.2 3 | */ 4 | import * as fc from 'fast-check' 5 | import { Either, left, right } from 'fp-ts/lib/Either' 6 | 7 | /** 8 | * Returns an `Arbitrary` that yelds only `right`s 9 | * 10 | * @since 0.0.2 11 | */ 12 | export function getRight(arb: fc.Arbitrary): fc.Arbitrary> { 13 | return arb.map(a => right(a)) 14 | } 15 | 16 | /** 17 | * Returns an `Arbitrary` that yelds only `left`s 18 | * 19 | * @since 0.0.2 20 | */ 21 | export function getLeft(arb: fc.Arbitrary): fc.Arbitrary> { 22 | return arb.map(l => left(l)) 23 | } 24 | 25 | /** 26 | * Returns an `Arbitrary` that yelds both `left`s and `right`s 27 | * 28 | * @since 0.0.2 29 | */ 30 | export function getEither(leftArb: fc.Arbitrary, rightArb: fc.Arbitrary): fc.Arbitrary> { 31 | return fc.oneof(getLeft(leftArb), getRight(rightArb)) 32 | } 33 | -------------------------------------------------------------------------------- /src/Option.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @since 0.0.2 3 | */ 4 | import * as fc from 'fast-check' 5 | import { none, Option, some } from 'fp-ts/lib/Option' 6 | 7 | /** 8 | * Returns an `Arbitrary` that yelds only `some`s 9 | * 10 | * @since 0.0.2 11 | */ 12 | export function getSome(arb: fc.Arbitrary): fc.Arbitrary> { 13 | return arb.map(some) 14 | } 15 | 16 | /** 17 | * Returns an `Arbitrary` that yelds only `none`s 18 | * 19 | * @since 0.0.2 20 | */ 21 | export function getNone(): fc.Arbitrary> { 22 | return fc.constant(none) 23 | } 24 | 25 | /** 26 | * Returns an `Arbitrary` that yelds both `none`s and `some`s 27 | * 28 | * @since 0.0.2 29 | */ 30 | export function getOption(arb: fc.Arbitrary): fc.Arbitrary> { 31 | return fc.oneof(getNone(), getSome(arb)) 32 | } 33 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @since 0.0.1 3 | */ 4 | import * as fc from 'fast-check' 5 | import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3 } from 'fp-ts/lib/Applicative' 6 | import { Apply, Apply1, Apply2, Apply2C, Apply3 } from 'fp-ts/lib/Apply' 7 | import { Eq, eqBoolean, eqNumber, eqString } from 'fp-ts/lib/Eq' 8 | import { Field } from 'fp-ts/lib/Field' 9 | import { Functor, Functor1, Functor2, Functor2C, Functor3 } from 'fp-ts/lib/Functor' 10 | import { HKT, Kind, Kind2, Kind3, URIS, URIS2, URIS3 } from 'fp-ts/lib/HKT' 11 | import { Monad, Monad1, Monad2, Monad2C, Monad3 } from 'fp-ts/lib/Monad' 12 | import { Monoid } from 'fp-ts/lib/Monoid' 13 | import { Ord } from 'fp-ts/lib/Ord' 14 | import { Ring } from 'fp-ts/lib/Ring' 15 | import { Semigroup } from 'fp-ts/lib/Semigroup' 16 | import { Semiring } from 'fp-ts/lib/Semiring' 17 | import * as laws from './laws' 18 | 19 | /** 20 | * Tests the `Eq` laws 21 | * 22 | * @since 0.0.1 23 | */ 24 | export const eq = (E: Eq, arb: fc.Arbitrary): void => { 25 | const reflexivity = fc.property(arb, laws.eq.reflexivity(E)) 26 | const symmetry = fc.property(arb, arb, laws.eq.simmetry(E)) 27 | const transitivity = fc.property(arb, arb, arb, laws.eq.transitivity(E)) 28 | fc.assert(reflexivity) 29 | fc.assert(symmetry) 30 | fc.assert(transitivity) 31 | } 32 | 33 | /** 34 | * Tests the `Ord` laws 35 | * 36 | * @since 0.0.1 37 | */ 38 | export const ord = (O: Ord, arb: fc.Arbitrary): void => { 39 | eq(O, arb) 40 | const totality = fc.property(arb, arb, laws.ord.totality(O)) 41 | const reflexivity = fc.property(arb, laws.ord.reflexivity(O)) 42 | const antisymmetry = fc.property(arb, arb, laws.ord.antisimmetry(O)) 43 | const transitivity = fc.property(arb, arb, arb, laws.ord.transitivity(O)) 44 | fc.assert(totality) 45 | fc.assert(reflexivity) 46 | fc.assert(antisymmetry) 47 | fc.assert(transitivity) 48 | } 49 | 50 | /** 51 | * Tests the `Semigroup` laws 52 | * 53 | * @example 54 | * import * as laws from 'fp-ts-laws' 55 | * import * as fc from 'fast-check' 56 | * import { Semigroup } from 'fp-ts/Semigroup' 57 | * import { eqString } from 'fp-ts/Eq' 58 | * 59 | * const semigroupSpace: Semigroup = { 60 | * concat: (x, y) => x + ' ' + y 61 | * } 62 | * laws.semigroup(semigroupSpace, eqString, fc.string()) 63 | * 64 | * @since 0.0.1 65 | */ 66 | export const semigroup = (S: Semigroup, E: Eq, arb: fc.Arbitrary): void => { 67 | const associativity = fc.property(arb, arb, arb, laws.semigroup.associativity(S, E)) 68 | fc.assert(associativity) 69 | } 70 | 71 | /** 72 | * Tests the `Monoid` laws 73 | * 74 | * @since 0.0.1 75 | */ 76 | export const monoid = (M: Monoid, E: Eq, arb: fc.Arbitrary): void => { 77 | semigroup(M, E, arb) 78 | const rightIdentity = fc.property(arb, laws.monoid.rightIdentity(M, E)) 79 | const leftIdentity = fc.property(arb, laws.monoid.leftIdentity(M, E)) 80 | fc.assert(rightIdentity) 81 | fc.assert(leftIdentity) 82 | } 83 | 84 | /** 85 | * Tests the `Semiring` laws 86 | * 87 | * @since 0.0.1 88 | */ 89 | export const semiring = (S: Semiring, E: Eq, arb: fc.Arbitrary, seed?: number): void => { 90 | const addAssociativity = fc.property(arb, arb, arb, laws.semiring.addAssociativity(S, E)) 91 | const addIdentity = fc.property(arb, laws.semiring.addIdentity(S, E)) 92 | const commutativity = fc.property(arb, arb, laws.semiring.commutativity(S, E)) 93 | const mulAssociativity = fc.property(arb, arb, arb, laws.semiring.mulAssociativity(S, E)) 94 | const mulIdentity = fc.property(arb, laws.semiring.mulIdentity(S, E)) 95 | const leftDistributivity = fc.property(arb, arb, arb, laws.semiring.leftDistributivity(S, E)) 96 | const rightDistributivity = fc.property(arb, arb, arb, laws.semiring.rightDistributivity(S, E)) 97 | const annihilation = fc.property(arb, laws.semiring.annihilation(S, E)) 98 | fc.assert(addAssociativity, { seed }) 99 | fc.assert(addIdentity, { seed }) 100 | fc.assert(commutativity, { seed }) 101 | fc.assert(mulAssociativity, { seed }) 102 | fc.assert(mulIdentity, { seed }) 103 | fc.assert(leftDistributivity, { seed }) 104 | fc.assert(rightDistributivity, { seed }) 105 | fc.assert(annihilation, { seed }) 106 | } 107 | 108 | /** 109 | * Tests the `Ring` laws 110 | * 111 | * @since 0.0.1 112 | */ 113 | export const ring = (R: Ring, S: Eq, arb: fc.Arbitrary, seed?: number): void => { 114 | semiring(R, S, arb, seed) 115 | const additiveInverse = fc.property(arb, laws.ring.additiveInverse(R, S)) 116 | fc.assert(additiveInverse) 117 | } 118 | 119 | /** 120 | * Tests the `Field` laws 121 | * 122 | * @since 0.0.1 123 | */ 124 | export const field = (F: Field, S: Eq, arb: fc.Arbitrary, seed?: number): void => { 125 | ring(F, S, arb, seed) 126 | if (S.equals(F.zero, F.one)) { 127 | throw new Error(`one should not be equal to zero`) 128 | } 129 | const commutativity = fc.property(arb, arb, laws.field.commutativity(F, S)) 130 | const integralDomain = fc.property(arb, arb, laws.field.integralDomain(F, S)) 131 | const nonNegativity = fc.property(arb, laws.field.nonNegativity(F, S)) 132 | const quotient = fc.property(arb, arb, laws.field.quotient(F, S)) 133 | const reminder = fc.property(arb, arb, laws.field.reminder(F, S)) 134 | const submultiplicative = fc.property(arb, arb, laws.field.submultiplicative(F, S)) 135 | const inverse = fc.property(arb, laws.field.inverse(F, S)) 136 | fc.assert(commutativity, { seed }) 137 | fc.assert(integralDomain, { seed }) 138 | fc.assert(nonNegativity, { seed }) 139 | fc.assert(quotient, { seed }) 140 | fc.assert(reminder, { seed }) 141 | fc.assert(submultiplicative, { seed }) 142 | fc.assert(inverse, { seed }) 143 | } 144 | 145 | /** 146 | * Tests the `Functor` laws 147 | * 148 | * @since 0.1.0 149 | */ 150 | export function functor( 151 | F: Functor3 152 | ): ( 153 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 154 | liftEq: (Sa: Eq) => Eq> 155 | ) => void 156 | export function functor( 157 | F: Functor2 158 | ): ( 159 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 160 | liftEq: (Sa: Eq) => Eq> 161 | ) => void 162 | export function functor( 163 | F: Functor2C 164 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 165 | export function functor( 166 | F: Functor1 167 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 168 | export function functor( 169 | F: Functor 170 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 171 | export function functor( 172 | F: Functor 173 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void { 174 | return (lift, liftEq) => { 175 | const arb = lift(fc.string()) 176 | const Sa = liftEq(eqString) 177 | const Sc = liftEq(eqNumber) 178 | const identity = fc.property(arb, laws.functor.identity(F, Sa)) 179 | const ab = (s: string): number | undefined | null => (s.length === 1 ? undefined : s.length === 2 ? null : s.length) 180 | const bc = (n: number | undefined | null): number => (n === undefined ? 1 : n === null ? 2 : n * 2) 181 | 182 | const composition = fc.property(arb, laws.functor.composition(F, Sc, ab, bc)) 183 | 184 | fc.assert(identity) 185 | fc.assert(composition) 186 | } 187 | } 188 | 189 | /** 190 | * Tests the `Apply` laws 191 | * 192 | * @since 0.1.0 193 | */ 194 | export function apply( 195 | F: Apply3 196 | ): ( 197 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 198 | liftEq: (Sa: Eq) => Eq> 199 | ) => void 200 | export function apply( 201 | F: Apply2 202 | ): ( 203 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 204 | liftEq: (Sa: Eq) => Eq> 205 | ) => void 206 | export function apply( 207 | F: Apply2C 208 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 209 | export function apply( 210 | F: Apply1 211 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 212 | export function apply( 213 | F: Apply 214 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 215 | export function apply( 216 | F: Apply 217 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void { 218 | const functorF = functor(F) 219 | return (lift, liftEq) => { 220 | functorF(lift, liftEq) 221 | 222 | const Sc = liftEq(eqBoolean) 223 | const arbFa = lift(fc.string()) 224 | const arbFab = lift(fc.constant((a: string) => a.length)) 225 | const arbFbc = lift(fc.constant((b: number) => b > 2)) 226 | const associativeComposition = fc.property(arbFa, arbFab, arbFbc, laws.apply.associativeComposition(F, Sc)) 227 | 228 | fc.assert(associativeComposition) 229 | } 230 | } 231 | 232 | /** 233 | * Tests the `Applicative` laws 234 | * 235 | * @since 0.1.0 236 | */ 237 | export function applicative( 238 | F: Applicative3 239 | ): ( 240 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 241 | liftEq: (Sa: Eq) => Eq> 242 | ) => void 243 | export function applicative( 244 | F: Applicative2 245 | ): ( 246 | lift: (a: fc.Arbitrary) => fc.Arbitrary>, 247 | liftEq: (Sa: Eq) => Eq> 248 | ) => void 249 | export function applicative( 250 | F: Applicative2C 251 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 252 | export function applicative( 253 | F: Applicative1 254 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 255 | export function applicative( 256 | F: Applicative 257 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void 258 | export function applicative( 259 | F: Applicative 260 | ): (lift: (a: fc.Arbitrary) => fc.Arbitrary>, liftEq: (Sa: Eq) => Eq>) => void { 261 | const applyF = apply(F) 262 | return (lift, liftEq) => { 263 | applyF(lift, liftEq) 264 | 265 | const arbFa = lift(fc.string()) 266 | const Sa = liftEq(eqString) 267 | const Sb = liftEq(eqNumber) 268 | const identity = fc.property(arbFa, laws.applicative.identity(F, Sa)) 269 | const ab = (s: string) => s.length 270 | const homomorphism = fc.property(fc.string(), laws.applicative.homomorphism(F, Sb, ab)) 271 | const arbFab = lift(fc.constant(ab)) 272 | const interchange = fc.property(fc.string(), arbFab, laws.applicative.interchange(F, Sb)) 273 | const derivedMap = fc.property(arbFa, laws.applicative.derivedMap(F, Sb, ab)) 274 | 275 | fc.assert(identity) 276 | fc.assert(homomorphism) 277 | fc.assert(interchange) 278 | fc.assert(derivedMap) 279 | } 280 | } 281 | 282 | /** 283 | * Tests the `Monad` laws 284 | * 285 | * @since 0.1.0 286 | */ 287 | export function monad(M: Monad3): (liftEq: (Sa: Eq) => Eq>) => void 288 | export function monad(M: Monad2): (liftEq: (Sa: Eq) => Eq>) => void 289 | export function monad(M: Monad2C): (liftEq: (Sa: Eq) => Eq>) => void 290 | export function monad(M: Monad1): (liftEq: (Sa: Eq) => Eq>) => void 291 | export function monad(M: Monad): (liftEq: (Sa: Eq) => Eq>) => void 292 | export function monad(M: Monad): (liftEq: (Sa: Eq) => Eq>) => void { 293 | const applicativeM = applicative(M) 294 | return liftEq => { 295 | applicativeM(arb => arb.map(M.of), liftEq) 296 | 297 | const Sc = liftEq(eqBoolean) 298 | const arbFa = fc.string().map(M.of) 299 | const afb = (s: string) => M.of(s.length) 300 | const bfc = (n: number) => M.of(n > 2) 301 | const associativity = fc.property(arbFa, laws.chain.associativity(M, Sc, afb, bfc)) 302 | const Sb = liftEq(eqNumber) 303 | const fab = M.of((a: string) => a.length) 304 | const derivedAp = fc.property(arbFa, laws.chain.derivedAp(M, Sb, fab)) 305 | 306 | fc.assert(associativity) 307 | fc.assert(derivedAp) 308 | 309 | const arb = fc.string().map(M.of) 310 | const Sa = liftEq(eqString) 311 | const leftIdentity = fc.property(fc.string(), laws.monad.leftIdentity(M, Sb, afb)) 312 | const rightIdentity = fc.property(arb, laws.monad.rightIdentity(M, Sa)) 313 | const ab = (s: string) => s.length 314 | const derivedMap = fc.property(arb, laws.monad.derivedMap(M, Sb, ab)) 315 | 316 | fc.assert(leftIdentity) 317 | fc.assert(rightIdentity) 318 | fc.assert(derivedMap) 319 | } 320 | } 321 | -------------------------------------------------------------------------------- /src/laws.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @since 0.1.0 3 | */ 4 | import { Eq } from 'fp-ts/lib/Eq' 5 | import { Functor } from 'fp-ts/lib/Functor' 6 | import { HKT } from 'fp-ts/lib/HKT' 7 | import { Apply } from 'fp-ts/lib/Apply' 8 | import { Chain } from 'fp-ts/lib/Chain' 9 | import { Applicative } from 'fp-ts/lib/Applicative' 10 | import { Monad } from 'fp-ts/lib/Monad' 11 | import { Ord } from 'fp-ts/lib/Ord' 12 | import { Semigroup } from 'fp-ts/lib/Semigroup' 13 | import { Monoid } from 'fp-ts/lib/Monoid' 14 | import { Semiring } from 'fp-ts/lib/Semiring' 15 | import { Ring } from 'fp-ts/lib/Ring' 16 | import { Field } from 'fp-ts/lib/Field' 17 | import { FunctionN } from 'fp-ts/lib/function' 18 | 19 | /** 20 | * @since 0.1.0 21 | */ 22 | export const eq = { 23 | reflexivity: (E: Eq) => (a: A): boolean => { 24 | return E.equals(a, a) 25 | }, 26 | simmetry: (E: Eq) => (a: A, b: A): boolean => { 27 | return E.equals(a, b) === E.equals(b, a) 28 | }, 29 | transitivity: (E: Eq) => (a: A, b: A, c: A): boolean => { 30 | return (E.equals(a, b) && E.equals(b, c)) === (E.equals(a, b) && E.equals(a, c)) 31 | } 32 | } 33 | 34 | /** 35 | * @since 0.1.0 36 | */ 37 | export const ord = { 38 | totality: (O: Ord) => (a: A, b: A): boolean => { 39 | return O.compare(a, b) <= 0 || O.compare(b, a) <= 0 40 | }, 41 | reflexivity: (O: Ord) => (a: A): boolean => { 42 | return O.compare(a, a) <= 0 43 | }, 44 | antisimmetry: (O: Ord) => (a: A, b: A): boolean => { 45 | return (O.compare(a, b) <= 0 && O.compare(b, a) <= 0) === O.equals(a, b) 46 | }, 47 | transitivity: (O: Ord) => (a: A, b: A, c: A): boolean => { 48 | return !(O.compare(a, b) <= 0 && O.compare(b, c) <= 0) || O.compare(a, c) <= 0 49 | } 50 | } 51 | 52 | /** 53 | * @since 0.1.0 54 | */ 55 | export const semigroup = { 56 | associativity: (S: Semigroup, E: Eq) => (a: A, b: A, c: A): boolean => { 57 | return E.equals(S.concat(S.concat(a, b), c), S.concat(a, S.concat(b, c))) 58 | } 59 | } 60 | 61 | /** 62 | * @since 0.1.0 63 | */ 64 | export const monoid = { 65 | rightIdentity: (M: Monoid, E: Eq) => (a: A): boolean => { 66 | return E.equals(M.concat(a, M.empty), a) 67 | }, 68 | leftIdentity: (M: Monoid, E: Eq) => (a: A): boolean => { 69 | return E.equals(M.concat(M.empty, a), a) 70 | } 71 | } 72 | 73 | const allEquals = (E: Eq) => (a: A, ...as: Array): boolean => { 74 | return as.every(item => E.equals(item, a)) 75 | } 76 | 77 | /** 78 | * @since 0.1.0 79 | */ 80 | export const semiring = { 81 | addAssociativity: (S: Semiring, E: Eq) => (a: A, b: A, c: A): boolean => { 82 | return E.equals(S.add(S.add(a, b), c), S.add(a, S.add(b, c))) 83 | }, 84 | addIdentity: (S: Semiring, E: Eq) => (a: A): boolean => { 85 | return allEquals(E)(a, S.add(a, S.zero), S.add(S.zero, a)) 86 | }, 87 | commutativity: (S: Semiring, E: Eq) => (a: A, b: A): boolean => { 88 | return E.equals(S.add(a, b), S.add(b, a)) 89 | }, 90 | mulAssociativity: (S: Semiring, E: Eq) => (a: A, b: A, c: A): boolean => { 91 | return E.equals(S.mul(S.mul(a, b), c), S.mul(a, S.mul(b, c))) 92 | }, 93 | mulIdentity: (S: Semiring, E: Eq) => (a: A): boolean => { 94 | return allEquals(E)(a, S.mul(a, S.one), S.mul(S.one, a)) 95 | }, 96 | leftDistributivity: (S: Semiring, E: Eq) => (a: A, b: A, c: A): boolean => { 97 | return E.equals(S.mul(a, S.add(b, c)), S.add(S.mul(a, b), S.mul(a, c))) 98 | }, 99 | rightDistributivity: (S: Semiring, E: Eq) => (a: A, b: A, c: A): boolean => { 100 | return E.equals(S.mul(S.add(a, b), c), S.add(S.mul(a, c), S.mul(b, c))) 101 | }, 102 | annihilation: (S: Semiring, E: Eq) => (a: A): boolean => { 103 | return allEquals(E)(S.zero, S.mul(a, S.zero), S.mul(S.zero, a)) 104 | } 105 | } 106 | 107 | /** 108 | * @since 0.1.0 109 | */ 110 | export const ring = { 111 | additiveInverse: (R: Ring, E: Eq) => (a: A): boolean => { 112 | return allEquals(E)(R.sub(a, a), R.add(R.sub(R.zero, a), a), R.zero) 113 | } 114 | } 115 | 116 | /** 117 | * @since 0.1.0 118 | */ 119 | export const field = { 120 | commutativity: (F: Field, S: Eq) => (a: A, b: A): boolean => { 121 | return S.equals(F.mul(a, b), F.mul(b, a)) 122 | }, 123 | integralDomain: (F: Field, S: Eq) => (a: A, b: A): boolean => { 124 | return S.equals(a, F.zero) || S.equals(b, F.zero) || !S.equals(F.mul(a, b), F.zero) 125 | }, 126 | nonNegativity: (F: Field, S: Eq) => (a: A): boolean => { 127 | return S.equals(a, F.zero) || F.degree(a) >= 0 128 | }, 129 | quotient: (F: Field, S: Eq) => (a: A, b: A): boolean => { 130 | const q = F.div(a, b) 131 | const r = F.mod(a, b) 132 | return S.equals(b, F.zero) || S.equals(a, F.add(F.mul(q, b), r)) 133 | }, 134 | reminder: (F: Field, S: Eq) => (a: A, b: A): boolean => { 135 | const r = F.mod(a, b) 136 | return S.equals(b, F.zero) || S.equals(r, F.zero) || F.degree(a) <= F.degree(b) 137 | }, 138 | submultiplicative: (F: Field, S: Eq) => (a: A, b: A): boolean => { 139 | return S.equals(a, F.zero) || S.equals(b, F.zero) || F.degree(a) <= F.degree(F.mul(a, b)) 140 | }, 141 | inverse: (F: Field, S: Eq) => (a: A): boolean => { 142 | const i = F.div(F.one, a) 143 | return S.equals(a, F.zero) || allEquals(S)(F.one, F.mul(i, a), F.mul(a, i)) 144 | } 145 | } 146 | 147 | /** 148 | * @since 0.1.0 149 | */ 150 | export const functor = { 151 | identity: (F: Functor, S: Eq>) => (fa: HKT): boolean => { 152 | return S.equals(F.map(fa, a => a), fa) 153 | }, 154 | composition: (F: Functor, S: Eq>, ab: FunctionN<[A], B>, bc: FunctionN<[B], C>) => ( 155 | fa: HKT 156 | ): boolean => { 157 | return S.equals(F.map(fa, a => bc(ab(a))), F.map(F.map(fa, ab), bc)) 158 | } 159 | } 160 | 161 | /** 162 | * @since 0.1.0 163 | */ 164 | export const apply = { 165 | associativeComposition: (F: Apply, S: Eq>) => ( 166 | fa: HKT, 167 | fab: HKT>, 168 | fbc: HKT> 169 | ): boolean => { 170 | return S.equals( 171 | F.ap(F.ap(F.map(fbc, bc => (ab: FunctionN<[A], B>) => (a: A) => bc(ab(a))), fab), fa), 172 | F.ap(fbc, F.ap(fab, fa)) 173 | ) 174 | } 175 | } 176 | 177 | /** 178 | * @since 0.1.0 179 | */ 180 | export const applicative = { 181 | identity: (F: Applicative, S: Eq>) => (fa: HKT): boolean => { 182 | return S.equals(F.ap(F.of((a: A) => a), fa), fa) 183 | }, 184 | homomorphism: (F: Applicative, S: Eq>, ab: FunctionN<[A], B>) => (a: A): boolean => { 185 | return S.equals(F.ap(F.of(ab), F.of(a)), F.of(ab(a))) 186 | }, 187 | interchange: (F: Applicative, S: Eq>) => (a: A, fab: HKT>): boolean => { 188 | return S.equals(F.ap(fab, F.of(a)), F.ap(F.of((ab: FunctionN<[A], B>) => ab(a)), fab)) 189 | }, 190 | derivedMap: (F: Applicative, S: Eq>, ab: FunctionN<[A], B>) => (fa: HKT): boolean => { 191 | return S.equals(F.map(fa, ab), F.ap(F.of(ab), fa)) 192 | } 193 | } 194 | 195 | /** 196 | * @since 0.1.0 197 | */ 198 | export const chain = { 199 | associativity: ( 200 | F: Chain, 201 | S: Eq>, 202 | afb: FunctionN<[A], HKT>, 203 | bfc: FunctionN<[B], HKT> 204 | ) => (fa: HKT): boolean => { 205 | return S.equals(F.chain(F.chain(fa, afb), bfc), F.chain(fa, a => F.chain(afb(a), bfc))) 206 | }, 207 | derivedAp: (F: Chain, S: Eq>, fab: HKT>) => (fa: HKT): boolean => { 208 | return S.equals(F.ap(fab, fa), F.chain(fab, f => F.map(fa, f))) 209 | } 210 | } 211 | 212 | /** 213 | * @since 0.1.0 214 | */ 215 | export const monad = { 216 | leftIdentity: (M: Monad, S: Eq>, afb: FunctionN<[A], HKT>) => (a: A): boolean => { 217 | return S.equals(M.chain(M.of(a), afb), afb(a)) 218 | }, 219 | rightIdentity: (M: Monad, S: Eq>) => (fa: HKT): boolean => { 220 | return S.equals(M.chain(fa, M.of), fa) 221 | }, 222 | derivedMap: (M: Monad, S: Eq>, ab: FunctionN<[A], B>) => (fa: HKT): boolean => { 223 | return S.equals(M.map(fa, ab), M.chain(fa, a => M.of(ab(a)))) 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /test/index.ts: -------------------------------------------------------------------------------- 1 | import * as fc from 'fast-check' 2 | import * as E from 'fp-ts/lib/Either' 3 | import { fieldNumber } from 'fp-ts/lib/Field' 4 | import { monoidString, monoidSum } from 'fp-ts/lib/Monoid' 5 | import * as O from 'fp-ts/lib/Option' 6 | import { ordNumber } from 'fp-ts/lib/Ord' 7 | import { Semigroup } from 'fp-ts/lib/Semigroup' 8 | import { eqNumber, eqString } from 'fp-ts/lib/Eq' 9 | import * as laws from '../src' 10 | import { getEither } from '../src/Either' 11 | import { getOption } from '../src/Option' 12 | 13 | describe('eq', () => { 14 | it('should test Eq laws', () => { 15 | laws.eq(eqNumber, fc.float()) 16 | }) 17 | }) 18 | 19 | describe('ord', () => { 20 | it('should test Ord laws', () => { 21 | laws.ord(ordNumber, fc.float()) 22 | }) 23 | }) 24 | 25 | describe('my semigroup instance', () => { 26 | it('should test Semigroup laws', () => { 27 | const semigroupSpace: Semigroup = { 28 | concat: (x, y) => x + ' ' + y 29 | } 30 | laws.semigroup(semigroupSpace, eqString, fc.string()) 31 | }) 32 | }) 33 | 34 | describe('monoid', () => { 35 | it('should test Monoid laws', () => { 36 | laws.monoid(monoidSum, eqNumber, fc.float()) 37 | }) 38 | }) 39 | 40 | describe('semiring', () => { 41 | it('should test Semiring laws', () => { 42 | const seed = 1552808164540 43 | laws.semiring(fieldNumber, eqNumber, fc.float(), seed) 44 | }) 45 | }) 46 | 47 | describe('ring', () => { 48 | it('should test Ring laws', () => { 49 | const seed = 1552808164540 50 | laws.ring(fieldNumber, eqNumber, fc.float(), seed) 51 | }) 52 | }) 53 | 54 | // describe('field', () => { 55 | // it('should test Field laws', () => { 56 | // const seed = Date.now() 57 | // // tslint:disable-next-line: no-console 58 | // console.log(seed) 59 | // laws.field(fieldNumber, eqNumber, fc.float(), seed) 60 | // }) 61 | // }) 62 | 63 | describe('functor', () => { 64 | it('should test Functor laws', () => { 65 | laws.functor(O.option)(getOption, O.getEq) 66 | laws.functor(E.either)(arb => getEither(fc.string(), arb), S => E.getEq(eqString, S)) 67 | }) 68 | }) 69 | 70 | describe('apply', () => { 71 | it('should test Apply laws', () => { 72 | laws.apply(O.option)(getOption, O.getEq) 73 | laws.apply(E.either)(arb => getEither(fc.string(), arb), S => E.getEq(eqString, S)) 74 | laws.apply(E.getValidation(monoidString))(arb => getEither(fc.string(), arb), S => E.getEq(eqString, S)) 75 | }) 76 | }) 77 | 78 | describe('applicative', () => { 79 | it('should test Applicative laws', () => { 80 | laws.applicative(O.option)(getOption, O.getEq) 81 | laws.applicative(E.either)(arb => getEither(fc.string(), arb), S => E.getEq(eqString, S)) 82 | laws.applicative(E.getValidation(monoidString))(arb => getEither(fc.string(), arb), S => E.getEq(eqString, S)) 83 | }) 84 | }) 85 | 86 | describe('monad', () => { 87 | it('should test Monad laws', () => { 88 | laws.monad(O.option)(O.getEq) 89 | laws.monad(E.either)(S => E.getEq(eqString, S)) 90 | laws.monad(E.getValidation(monoidString))(S => E.getEq(eqString, S)) 91 | }) 92 | }) 93 | -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "noEmit": true, 4 | "sourceMap": true, 5 | "declaration": true, 6 | "module": "commonjs", 7 | "noImplicitReturns": false, 8 | "noUnusedLocals": true, 9 | "noUnusedParameters": true, 10 | "noFallthroughCasesInSwitch": true, 11 | "noEmitOnError": false, 12 | "strict": true, 13 | "target": "es5", 14 | "moduleResolution": "node", 15 | "forceConsistentCasingInFileNames": true, 16 | "lib": ["es6"] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "./lib", 4 | "declaration": true, 5 | "module": "commonjs", 6 | "noImplicitReturns": false, 7 | "noUnusedLocals": true, 8 | "noUnusedParameters": true, 9 | "noFallthroughCasesInSwitch": true, 10 | "noEmitOnError": false, 11 | "strict": true, 12 | "target": "es5", 13 | "moduleResolution": "node", 14 | "forceConsistentCasingInFileNames": true, 15 | "lib": ["es6"] 16 | }, 17 | "include": ["./src/**/*"] 18 | } 19 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint-config-standard", 3 | "rules": { 4 | "space-before-function-paren": false, 5 | "no-use-before-declare": false, 6 | "variable-name": false, 7 | "quotemark": [true, "single", "jsx-double"], 8 | "ter-indent": false, 9 | "strict-boolean-expressions": true, 10 | "forin": true, 11 | "no-console": true, 12 | "array-type": [true, "generic"], 13 | "deprecation": true, 14 | "no-inferred-empty-object-type": true 15 | } 16 | } 17 | --------------------------------------------------------------------------------