Skip to content

Commit

Permalink
Update to contrast more directly with #281
Browse files Browse the repository at this point in the history
  • Loading branch information
gabejohnson committed Jan 17, 2018
1 parent 6687033 commit 010c84f
Showing 1 changed file with 76 additions and 14 deletions.
90 changes: 76 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,21 +802,67 @@ In Fantasy Land, types are specified by a capitalized name and a catamorphic
method of the same name, without capitalization. The method allows access to
the values contained within the structure or the ability to provide a default
value when the structure is nullary. When defined on a value, a method name
contains the same 'fantasy-land/' prefix.
contains the same 'fantasy-land/' as other methods described in the spec prefix.

For example
```hs
identity :: Identity i => i a ~> (a -> b) -> b
```

Additionally, data constructors may be referenced in the specification by name
and arity. Conforming data structures are not required to provide these
constructors nor are any provided constructors required to share these names.
For example, instead of `Left` and `Right`, the constructors could be named
`Failure` and `Success`.

### Either
For example
```js
function Id(x) {
this.x = x;
}

The `Either` type encodes the concept of binary possibility (Left a and Right b).
Id.prototype['fantasy-land/identity'] = function identity(f) {
return f(this.x);
};

Array.prototype['fantasy-land/identity'] = function identity(f) {
return f(this[0]);
};

var identity = 'fantasy-land/identity';

(new Id(42))[identity](x => x) === [42][identity](x => x);
```

### Maybe

The `Maybe` type encodes the concept of optionality (Nothing and Just a).

#### `maybe` method

```hs
maybe :: Maybe m => m a ~> (b, (a -> b)) -> b
```

A value which conforms to the Maybe specification must provide a `maybe` method.

The `maybe` method takes two arguments:

m.maybe(x, f)

If e has constructors unary constructors `Left` and `Right`
1. `x` is the default value in the `Nothing` case

e.either(Left, Right) === e (identity)
1. If `x` does not match the return value of `maybe`, the behaviour of
`maybe` is unspecified.

2. `f` must be a unary function. It is called in the `Just` case

1. If `f` is not a unary function or the return value of `f` does not match
the return value of `maybe`, the behaviour of `maybe` is unspecified.

### Either

The `Either` type encodes the concept of binary possibility (Left a and Right b).

#### `either` method

Expand All @@ -830,19 +876,35 @@ The `either` method takes two arguments:

e.either(f, g)

1. `f` must be a function which returns a value
1. `f` must be a unary function. It is called in the `Left` case

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

2. `g` must be a function which returns a value
2. `g` must be a unary function. It is called in the `Right` case

1. If `g` is not a function, the behaviour of `either` is unspecified.
2. `g` can return any value.
3. No parts of `g`'s return value should be checked.
1. If `g` is not a unary function or the return value of `g` does not match
the return value of `either`, the behaviour of `either` is unspecified.

### Pair

`Pair` is the canonical product type and represents a structure containing two
values (Pair a b).

```hs
pair :: Pair p => p a b ~> ((a, b) -> c) -> c
```

A value which conforms to the Pair specification must provide a `pair` method.

The `pair` method takes a single argument:

p.pair(f)

1. `f` must be a binary function

3. `f`, `g`, and `either` must return a value of the same type
1. If `f` is not a binary function or the return value of `f` does not match
the return value of `pair`, the behaviour of `pair` is unspecified.

## Derivations

Expand Down

0 comments on commit 010c84f

Please sign in to comment.