Skip to content

Commit

Permalink
adds contravariant functor to spec (#150)
Browse files Browse the repository at this point in the history
* adds contravariant functor to spec

* adds to list of structures

* Adds type signature; removes contravariant dependency in profunctor

* Fix linting errors

* Update figures

* Update height of img in README

* Remove 'functor' from 'Contravariant' section name

* Update algebra name in dependencies graph

* Should be the last few fixes necessary for merge

* Update contramap laws
  • Loading branch information
rjmk authored and joneshf committed Jan 30, 2017
1 parent 847da46 commit 5ac5a9f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ structures:
* [Semigroup](#semigroup)
* [Monoid](#monoid)
* [Functor](#functor)
* [Contravariant](#contravariant)
* [Apply](#apply)
* [Applicative](#applicative)
* [Alt](#alt)
Expand All @@ -28,7 +29,7 @@ structures:
* [Bifunctor](#bifunctor)
* [Profunctor](#profunctor)

<img src="figures/dependencies.png" width="888" height="347" />
<img src="figures/dependencies.png" width="888" height="289" />

## General

Expand Down Expand Up @@ -186,6 +187,32 @@ method takes one argument:

2. `map` must return a value of the same Functor

### Contravariant

1. `u.contramap(a => a)` is equivalent to `u` (identity)
2. `u.contramap(x => f(g(x)))` is equivalent to `u.contramap(f).contramap(g)`
(composition)

#### `contramap` method

```hs
contramap :: Contravariant f => f a ~> (b -> a) -> f b
```

A value which has a Contravariant must provide a `contramap` method. The
`contramap` method takes one argument:

u.contramap(f)

1. `f` must be a function,

1. If `f` is not a function, the behaviour of `contramap` is
unspecified.
2. `f` can return any value.
3. No parts of `f`'s return value should be checked.

2. `contramap` must return a value of the same Contravariant

### Apply

A value that implements the Apply specification must also
Expand Down
1 change: 1 addition & 0 deletions figures/dependencies.dot
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ digraph {
Extend;
Foldable;
Functor;
Contravariant;
Monad;
Monoid;
Plus;
Expand Down
Binary file modified figures/dependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
concat: 'fantasy-land/concat',
empty: 'fantasy-land/empty',
map: 'fantasy-land/map',
contramap: 'fantasy-land/contramap',
ap: 'fantasy-land/ap',
of: 'fantasy-land/of',
alt: 'fantasy-land/alt',
Expand Down
27 changes: 27 additions & 0 deletions laws/contravariant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const {identity, compose} = require('fantasy-combinators');
const {contramap} = require('..');

/*
### Contravariant Functor
1. `u.contramap(a => a)` is equivalent to `u` (identity)
2. `u.contramap(x => f(g(x)))` is equivalent to `u.contramap(f).contramap(g)` (composition)
*/

const identityʹ = t => eq => x => {
const a = t(x)[contramap](identity);
const b = t(x);
return eq(a, b);
};

const composition = t => eq => x => {
const a = t(x)[contramap](compose(identity)(identity));
const b = t(x)[contramap](identity)[contramap](identity);
return eq(a, b);
};

module.exports = {identity: identityʹ, composition};

0 comments on commit 5ac5a9f

Please sign in to comment.