Skip to content

Commit

Permalink
docs for semigroup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosh committed Feb 7, 2022
1 parent b2ad8f2 commit eaf4e40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/typeclasses/Semigroup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Semigroup

A `Semigroup` instance provides a way to combine two things of the same type to create another instance of the same
type.

## API

The `Semigroup` typeclass exposes a method `append`, which requires two elements of the same type `A` to produce another
element of type `A`.

```php
interface Semigroup
{
/**
* @param A $a
* @param A $b
* @return A
*
* @psalm-pure
*/
public function append($a, $b);
}
```

It simplified type is

```
append :: (A, A) -> A
```

## Laws

The only law associated to the `Semigroup` typeclass states that it should be associative.

### Associativity

```php
$semigroup->append($a, $semigroup->append($b, $c)) == $semigroup->append($semigroup->append($a, $b), $c)
```
2 changes: 2 additions & 0 deletions src/Typeclass/Semigroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Marcosh\LamPHPda\Typeclass;

/**
* @see https://github.com/marcosh/lamphpda/tree/master/docs/typeclasses/Semigroup.md
*
* @template-covariant A
*
* @psalm-immutable
Expand Down

0 comments on commit eaf4e40

Please sign in to comment.