-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmb.ts
282 lines (278 loc) · 8.66 KB
/
cmb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2022-2023 Joshua Martinez-Maes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Functionality for associative combination.
*
* @remarks
*
* ## Importing from this module
*
* This module exposes utilities for working with semigroups. It is recommended
* to import them as they are named:
*
* ```ts
* import { Semigroup, cmb } from "@neotype/prelude/cmb.js";
* ```
*
* ## Implementing semigroups
*
* - The {@link Semigroup:interface | `Semigroup<T>`} interface provides a
* contract for implementing [semigroups]. See the documentation for
* implementation patterns.
* - The {@linkcode Semigroup:namespace} companion namespace provides the
* unique symbol required to implement its associated interface.
*
* [semigroups]: https://mathworld.wolfram.com/Semigroup.html
*
* @module
*/
/**
* An interface that provides evidence of a [semigroup].
*
* [semigroup]: https://mathworld.wolfram.com/Semigroup.html
*
* @remarks
*
* ## Properties
*
* Implementors of `Semigroup` must implement an operation that satisfies the
* [associative property], such that:
*
* - `cmb(x, cmb(y, z))` is equivalent to `cmb(cmb(x, y), z)`
*
* for all values `x`, `y`, and `z`.
*
* ## Implementing `Semigroup`
*
* `Semigroup` requires an implementation for `[Semigroup.cmb]`.
*
* The most common implementation strategies are writing classes and patching
* existing prototypes. Implementation is implicit and does not require an
* `implements` clause. TypeScript uses [structural subtyping] to determine
* whether a value implements `Semigroup`.
*
* ### Conditional implementation
*
* Working with generic types requires additional consideration: in some cases,
* a generic type implements `Semigroup` **only** when one or more of its
* generic parameters implement `Semigroup`; in these cases, we must require a
* `Semigroup` implementation from the parameter(s). In other cases, there are
* no such requirements.
*
* ### Writing classes
*
* Classes and objects can implement `Semigroup`. This strategy works best for
* types that:
*
* - are already modeled using classes or objects.
* - provide direct access to their implementation.
* - have a single, specific behavior as a semigroup.
*
* Additionally, classes can easily wrap existing types to provide a variety of
* `Semigroup` implementations. These "helper" classes are useful for types
* that:
*
* - have more than one behavior as a semigroup, or already have a default
* implementation for `Semigroup` but can have alternative implementations.
* - do not provide access to their implementation, and where patching the
* implementation is undesireable.
*
* ### Patching existing prototypes
*
* Existing types can be patched to implement `Semigroup`. This strategy works
* well for types that:
*
* - are built-in or imported from external modules.
* - do not provide access to their implementation.
* - have a single, specific behavior as a semigroup, or where the programmer
* wishes to implement a default behavior.
*
* Patching a type in TypeScript requires two steps:
*
* 1. an [augmentation] for a module or the global scope that patches the
* type-level representation; and
* 2. a concrete implementation for `[Semigroup.cmb]`.
*
* The concrete implementation logic is similar to writing a method body for a
* class or object, and the same practices apply when requiring generic type
* parameters to implement `Semigroup`.
*
* [associative property]: https://mathworld.wolfram.com/Associative.html
* [structural subtyping]:
* https://www.typescriptlang.org/docs/handbook/type-compatibility.html#site-content
* [augmentation]:
* https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
*
* @example Non-generic implementation
*
* Consider a type that combines strings using concatenation:
*
* ```ts
* import { Semigroup } from "@neotype/prelude/cmb.js";
*
* class Str {
* constructor(readonly val: string) {}
*
* [Semigroup.cmb](that: Str): Str {
* return new Str(this.val + that.val);
* }
* }
* ```
*
* @example Generic implementation with no `Semigroup` requirements
*
* Consider a type that combines arrays using concatenation:
*
* ```ts
* import { Semigroup } from "@neotype/prelude/cmb.js";
*
* class Concat<out T> {
* constructor(readonly val: T[]) {}
*
* [Semigroup.cmb](that: Concat<T>): Concat<T> {
* return new Concat([...this.val, ...that.val]);
* }
* }
* ```
*
* Notice how `Concat` is generic, but there are no special requirements for
* implementing `[Semigroup.cmb]`.
*
* @example Generic implementation with a `Semigroup` requirement
*
* Consider a type that combines promises by combining their results, which
* requires that the results also implement `Semigroup`:
*
* ```ts
* import { Semigroup, cmb } from "@neotype/prelude/cmb.js";
*
* class Async<out T> {
* constructor(readonly val: Promise<T>) {}
*
* [Semigroup.cmb]<T extends Semigroup<T>>(
* this: Async<T>,
* that: Async<T>,
* ): Async<T> {
* return new Async(
* this.val.then((lhs) => that.val.then((rhs) => cmb(lhs, rhs))),
* );
* }
* }
* ```
*
* Notice the extra syntax when implementing `[Semigroup.cmb]`. We introduce
* a *method-scoped* generic parameter `T` and require that it has a `Semigroup`
* implementation by writing `T extends Semigroup<T>` (the name `T` is
* arbitrary).
*
* Then, we require that `this` and `that` are `Async<T>` where `T extends
* Semigroup<T>`. This allows us to use `cmb` to implement our desired behavior.
*
* @example Generic implementation with multiple `Semigroup` requirements
*
* Consider a type that combines two values pairwise, which requires that each
* value implement `Semigroup`:
*
* ```ts
* import { Semigroup, cmb } from "@neotype/prelude/cmb.js";
*
* class Pair<out A, out B> {
* constructor(readonly fst: A, readonly snd: B) {}
*
* [Semigroup.cmb]<A extends Semigroup<A>, B extends Semigroup<B>>(
* this: Pair<A, B>,
* that: Pair<A, B>,
* ): Pair<A, B> {
* return new Pair(cmb(this.fst, that.fst), cmb(this.snd, that.snd));
* }
* }
* ```
*
* The syntax is similar to the `Async` implementation above. Notice there are
* now two method-scoped generic parameters that are each required to implement
* `Semigroup`.
*
* @example Non-generic augmentation
*
* Consider a global augmentation for the `String` prototype:
*
* ```ts
* import { Semigroup } from "@neotype/prelude/cmb.js";
*
* declare global {
* interface String {
* [Semigroup.cmb](that: string): string
* }
* }
*
* String.prototype[Semigroup.cmb] = function (that: string): string {
* return this + that;
* };
* ```
*
* @example Generic augmentation
*
* Consider a module augmentation for an externally defined `Pair` type:
*
* ```ts
* import { Semigroup, cmb } from "@neotype/prelude/cmb.js";
* import { Pair } from "path_to/pair.js";
*
* declare module "path_to/pair.js" {
* interface Pair<A, B> {
* [Semigroup.cmb]<A extends Semigroup<A>, B extends Semigroup<B>>(
* this: Pair<A, B>,
* that: Pair<A, B>,
* ): Pair<A, B>
* }
* }
*
* Pair.prototype[Semigroup.cmb] = function <
* A extends Semigroup<A>,
* B extends Semigroup<B>,
* >(this: Pair<A, B>, that: Pair<A, B>): Pair<A, B> {
* return new Pair(cmb(this.fst, that.fst), cmb(this.snd, that.snd));
* };
* ```
*/
export interface Semigroup<in out T> {
/**
* Combine this and that semigroup using an associative operation.
*/
[Semigroup.cmb](that: T): T;
}
/**
* The companion namespace for the {@link Semigroup:interface `Semigroup<T>`}
* interface.
*
* @remarks
*
* This namespace provides the unique symbol required to implement `Semigroup`.
*/
export namespace Semigroup {
/** The unique symbol used by implementors of `Semigroup`. */
export const cmb = Symbol();
}
/**
* Combine two of the same semigroup.
*
* @remarks
*
* `cmb(lhs, rhs)` is equivalent to `lhs[Semigroup.cmb](rhs)`.
*/
export function cmb<T extends Semigroup<T>>(lhs: T, rhs: T): T {
return lhs[Semigroup.cmb](rhs);
}