Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Alt, Plus and Alternative specs #197

Merged
merged 12 commits into from
Nov 4, 2016
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ structures:
* [Functor](#functor)
* [Apply](#apply)
* [Applicative](#applicative)
* [Alt](#alt)
* [Plus](#plus)
* [Alternative](#alternative)
* [Foldable](#foldable)
* [Traversable](#traversable)
* [Chain](#chain)
* [ChainRec](#chainrec)
* [Monad](#monad)
* [MonadZero](#monadzero)
* [MonadPlus](#monadplus)
* [MonadOr](#monador)
* [Extend](#extend)
* [Comonad](#comonad)
* [Bifunctor](#bifunctor)
Expand Down Expand Up @@ -243,6 +249,69 @@ Given a value `f`, one can access its type representative via the

1. No parts of `a` should be checked

### Alt

A value that implements the Alt specification must also implement
the [Functor](#functor) specification.

1. `a.alt(b).alt(c)` is equivalent to `a.alt(b.alt(c))` (associativity)
2. `a.alt(b).map(f)` is equivalent to `a.map(f).alt(b.map(f))` (distributivity)

#### `alt` method

```hs
alt :: Alt f => f a ~> f a -> f a
```

A value which has a Alt must provide a `alt` method. The
`alt` method takes one argument:

a.alt(b)

1. `b` must be a value of the same Alt

1. If `b` is not the same Alt, behaviour of `alt` is
unspecified.
2. `a` and `b` can contain any value of same type.
3. No parts of `a`'s and `b`'s containing value should be checked.

2. `alt` must return a value of the same Alt.

### Plus

A value that implements the Plus specification must also implement
the [Alt](#alt) specification.

1. `x.alt(A.zero())` is equivalent to `x` (right identity)
2. `A.zero().alt(x)` is equivalent to `x` (left identity)
2. `A.zero().map(f)` is equivalent to `A.zero()` (annihilation)

#### `zero` method

```hs
zero :: Plus x => () -> x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be Plus x => () -> x a? Also maybe use p instead of x?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we should also add forall a I think, but i'm not familiar with that. maybe @joneshf could help here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think forall is kinda implicit in our loosely specified type system. What I was trying to say is that x is not a type here, it's a type constructor, so it cannot be in that position in signature. x a is a type on the other hand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use f a. f to be consistent with other types

```

A value which has a Plus must provide an `zero` function on its
[type representative](#type-representatives):

M.zero()

Given a value `x`, one can access its type representative via the
`constructor` property:

x.constructor.zero()

1. `zero` must return a value of the same Plus

### Alternative

A value that implements the Alternative specification must also implement
the [Applicative](#applicative) and [Plus](#plus) specifications.

1. `x.ap(f.alt(g))` is equivalent to `x.ap(f).alt(x.ap(g))` (distributivity)
1. `x.ap(M.zero())` is equivalent to `M.zero()` (annihilation)

### Foldable

1. `u.reduce` is equivalent to `u.reduce((acc, x) => acc.concat([x]), []).reduce`
Expand Down Expand Up @@ -389,6 +458,27 @@ the [Applicative](#applicative) and [Chain](#chain) specifications.
1. `M.of(a).chain(f)` is equivalent to `f(a)` (left identity)
2. `m.chain(M.of)` is equivalent to `m` (right identity)

### MonadZero

A value that implements the MonadZero specification must also implement
the [Monad](#monad) and [Alternative](#alternative) specifications.

1. `M.zero().chain(f)` is equivalent to `M.zero()` (annihilation)

### MonadPlus

A value that implements the MonadPlus specification must also implement
the [MonadZero](#monadzero) specification.

1. `x.alt(y).chain(f)` is equivalent to `x.chain(f).alt(y.chain(f))` (distributivity)

### MonadOr

A value that implements the MonadOr specification must also implement
the [MonadZero](#monadzero) specification.

1. `M.of(a).alt(b)` is equivalent to `M.of(a)` (left catch)

### Extend

1. `w.extend(g).extend(f)` is equivalent to `w.extend(_w => f(_w.extend(g)))`
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"/node_modules/",
"/.*",
"/id.js",
"/id_test.js",
"/test.js",
"/implementations.md",
"/logo.png",
"/package.json"
Expand Down
14 changes: 14 additions & 0 deletions figures/dependencies.dot
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ digraph {
node [shape=plaintext]

# Algebras
Alt;
Alternative;
Applicative;
Apply;
Bifunctor;
Expand All @@ -12,24 +14,36 @@ digraph {
Foldable;
Functor;
Monad;
MonadOr;
MonadPlus;
MonadZero;
Monoid;
Plus;
Profunctor;
Semigroup;
Setoid;
Traversable;

# Dependencies
Alt -> Plus;
Applicative -> Alternative;
Applicative -> Monad;
Alternative -> MonadZero;
Apply -> Applicative;
Apply -> Chain;
Chain -> ChainRec;
Chain -> Monad;
Extend -> Comonad;
Foldable -> Traversable;
Functor -> Alt;
Functor -> Apply;
Functor -> Bifunctor;
Functor -> Extend;
Functor -> Profunctor;
Functor -> Traversable;
Monad -> MonadZero;
MonadZero -> MonadOr;
MonadZero -> MonadPlus;
Plus -> Alternative;
Semigroup -> Monoid;
}
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.
113 changes: 0 additions & 113 deletions id_test.js

This file was deleted.

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
map: 'fantasy-land/map',
ap: 'fantasy-land/ap',
of: 'fantasy-land/of',
alt: 'fantasy-land/alt',
zero: 'fantasy-land/zero',
reduce: 'fantasy-land/reduce',
traverse: 'fantasy-land/traverse',
chain: 'fantasy-land/chain',
Expand Down
18 changes: 18 additions & 0 deletions internal/compose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const {tagged} = require('daggy');

const fl = require('..');
const {equality} = require('./func');

const Compose = module.exports = tagged('c');
Compose[fl.of] = Compose;
Compose.prototype[fl.ap] = function(f) {
return Compose(this.c[fl.ap](f.c[fl.map](u => y => y[fl.ap](u))));
};
Compose.prototype[fl.map] = function(f) {
return Compose(this.c[fl.map](y => y[fl.map](f)));
};
Compose.prototype[fl.equals] = function(x) {
return equality(this.c, x.c);
};
8 changes: 8 additions & 0 deletions internal/func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const fl = require('../');
const equality = (x, y) => typeof x[fl.equals] === 'function' ? x[fl.equals](y) : x === y;

module.exports = {
equality,
};
13 changes: 6 additions & 7 deletions id.js → internal/id.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const fl = require('./');
const fl = require('../');
const {equality} = require('./func');

const Id = function Id(a) {
this.value = a;
};
const {tagged} = require('daggy');

const Id = module.exports = tagged('value');

// Setoid
Id.prototype[fl.equals] = function(b) {
return typeof this.value[fl.equals] === 'function' ? this.value[fl.equals](b.value) : this.value === b.value;
return equality(this.value, b.value);
};

// Semigroup (value must also be a Semigroup)
Expand Down Expand Up @@ -73,5 +74,3 @@ Id[fl.of] = function(a) {
Id.prototype[fl.extract] = function() {
return this.value;
};

module.exports = Id;
17 changes: 17 additions & 0 deletions internal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const patch = require('./patch');
const Maybe = require('./maybe');
const Id = require('./id');
const Sum = require('./string_sum');
const Compose = require('./compose');
const {equality} = require('./func');

module.exports = {
Maybe,
Id,
Sum,
Compose,
equality,
patch,
};
Loading