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
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ structures:
* [Functor](#functor)
* [Apply](#apply)
* [Applicative](#applicative)
* [Alt](#alt)
* [Plus](#plus)
* [Alternative](#alternative)
* [Foldable](#foldable)
* [Traversable](#traversable)
* [Chain](#chain)
Expand All @@ -25,7 +28,7 @@ structures:
* [Bifunctor](#bifunctor)
* [Profunctor](#profunctor)

<img src="figures/dependencies.png" width="863" height="347" />
<img src="figures/dependencies.png" width="100%" />
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 this will reintroduce the problem @rpominov fixed in #119.

Copy link
Member Author

@safareli safareli Nov 2, 2016

Choose a reason for hiding this comment

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

Will fix that. We definitly need CONTRIBUTING.md

Copy link
Member Author

@safareli safareli Nov 2, 2016

Choose a reason for hiding this comment

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

on github the width of README is fixed but for example on npm it looks bad because max width of readme area is 727px and if you are on small screan than it's worth.

screen shot 2016-11-02 at 5 45 06 pm

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, same issue with mobile version of GitHub for example. I wish we could find a fix without this side effect, but in the current situation having links to point to the right locations is more important imo.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok will change it as it was before.

But we could move the image to the end of README and have a link in it's place where it was before (next to list of specs) and also a link back.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for moving to the end.

Copy link
Member Author

Choose a reason for hiding this comment

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

you can check rendered image here


## General

Expand Down Expand Up @@ -243,6 +246,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):

A.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(A.zero())` is equivalent to `A.zero()` (annihilation)

### Foldable

1. `u.reduce` is equivalent to `u.reduce((acc, x) => acc.concat([x]), []).reduce`
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
7 changes: 7 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 @@ -13,23 +15,28 @@ digraph {
Functor;
Monad;
Monoid;
Plus;
Profunctor;
Semigroup;
Setoid;
Traversable;

# Dependencies
Alt -> Plus;
Applicative -> Alternative;
Applicative -> Monad;
Apply -> Applicative;
Apply -> Chain;
Chain -> ChainRec;
Chain -> Monad;
Extend -> Comonad;
Foldable -> Traversable;
Functor -> Alt;
Functor -> Apply;
Functor -> Bifunctor;
Functor -> Extend;
Functor -> Profunctor;
Functor -> Traversable;
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;
15 changes: 15 additions & 0 deletions internal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

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

module.exports = {
Id,
Sum,
Compose,
equality,
patch,
};
29 changes: 29 additions & 0 deletions internal/patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const fl = require('..');

module.exports = () => {
String.prototype[fl.concat] = String.prototype.concat;
Array.prototype[fl.equals] = function(y) {
return this.length === y.length && this.join('') === y.join('');
};
Array.prototype[fl.map] = Array.prototype.map;
Array.prototype[fl.ap] = function(fs) {
return fs[fl.chain](f => this.map(f));
};
Array.prototype[fl.chain] = function(f) {
return [].concat(this.map(f));
};
Array.prototype[fl.reduce] = Array.prototype.reduce;
Array.prototype[fl.concat] = Array.prototype.concat;
Array.prototype[fl.traverse] = function(f, p) {
return this.map(f)[fl.reduce](
(ys, x) => ys[fl.ap](x[fl.map](y => z => z[fl.concat](y))),
p([])
);
};
Array.prototype[fl.alt] = function(b) {
return this.concat(b);
};
Array[fl.zero] = () => [];
};
20 changes: 20 additions & 0 deletions internal/string_sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

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

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

// Special type of sum for the type of string.
const Sum = module.exports = tagged('v');
Sum[fl.of] = Sum;
Sum[fl.empty] = () => Sum('');
Sum.prototype[fl.map] = function(f) {
return Sum(f(this.v));
};
Sum.prototype[fl.concat] = function(x) {
return Sum(this.v + x.v);
};
Sum.prototype[fl.equals] = function(x) {
return equality(this.v, x.v);
};
Loading