-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathproposal.js
More file actions
294 lines (246 loc) · 9.26 KB
/
Copy pathproposal.js
File metadata and controls
294 lines (246 loc) · 9.26 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
const assert = require ('assert');
const show = require ('sanctuary-show');
// # Proposal: Fully-qualified type representatives
//
// This document describes a proposal for a fundamental change to the
// Fantasy Land specification. It also serves as a proof of concept.
//
// ## Motivation
//
// Thanks to type inference, Haskell expressions are able to evaluate
// differently in different contexts:
//
// Prelude> :t mempty
// mempty :: Monoid a => a
//
// Prelude> mempty :: String
// ""
//
// Prelude> mempty :: [Float]
// []
//
// Prelude> mempty :: (String, [Float])
// ("",[])
//
// Without type inference, explicit type information is required in order
// to achieve this in JavaScript. [Type representatives][1] exist for this
// purpose, giving us the ability to provide a polymorphic `empty` function:
//
// > S.empty
// empty :: Monoid a => TypeRep a -> a
//
// > S.empty (String)
// ''
//
// > S.empty (Array)
// []
//
// This function, though, cannot be used to create an empty value of type
// `Pair String (Array Number)`, because type representatives as currently
// defined provide no information about inner types.
//
// This proposal makes type representatives as expressive as their (implicit)
// Haskell counterparts.
//
// [1]: https://github.com/fantasyland/fantasy-land#type-representatives
//
// ## Changes
//
// 1. The `constructor` property would no longer be used to access a
// value's type representative. A new property name would be chosen.
//
// 2. Fantasy Land methods would only ever be defined on type representatives
// (the current specifications requires certain methods to be defined
// directly on values rather than on their type representatives).
// Consequently, Fantasy Land method names would no longer be prefixed.
//
// 3. Fantasy Land methods would be regular functions. They would take
// all their arguments explicitly. (Farewell squiggly arrow!)
//
// 4. Fantasy Land methods would be curried. This change is optional,
// but should be considered given that ADT library authors would need
// to make significant changes to their libraries regardless.
//
// 5. The fantasy-land package would export a type representative
// for each of JavaScript's built-in nullary types (e.g. `Number`),
// and a type representative constructor for each of JavaScript's
// built-in parameterized types (e.g. `Array a`). The Fantasy Land
// implementations for built-in types would be moved to these type
// representatives from sanctuary-type-classes.
//
// 6. The fantasy-land package would export a function for accessing a
// value's type representative.
//
// 7. The fantasy-land package would export a predicate for each type class,
// enabling type representative constructors to include conditional logic
// (see `Maybe._` and `Pair._` below).
//
// 8. The fantasy-land package would export a function for each method
// defined in the specification.
const FL = {};
//# FL.Unknown :: TypeRep a
//.
//. The type representative of `Nothing` is `Maybe (Unknown)`. In order for
//. `FL.concat (Nothing) (Nothing)` to work, `Maybe (Unknown)` must satisfy
//. the Semigroup constraint. Thus it is necessary to provide dummy methods
//. (which are never invoked). Without this embryonic behaviour it would be
//. necessary to use `concat (Nothing_ (FL.String)) (Nothing_ (FL.String))`.
FL.Unknown = {
'@@show': () => 'Unknown',
'concat': u1 => u2 => { throw new TypeError ('Not implemented'); },
'empty': () => { throw new TypeError ('Not implemented'); },
};
//# FL.Number :: TypeRep Number
FL.Number = {
'@@show': () => 'Number',
};
//# FL.String :: TypeRep String
FL.String = {
'@@show': () => 'String',
'concat': s1 => s2 => s1 + s2,
'empty': () => '',
};
//# FL.Array :: TypeRep a -> TypeRep (Array a)
FL.Array = $1 => Object.assign (
{'@@show': () => 'Array (' + show ($1) + ')',
'concat': a1 => a2 => a1.concat (a2)},
FL.isMonoid ($1) ?
{'empty': () => []} :
{}
);
//# FL.typeRep :: a -> TypeRep a
//.
//. Returns the given value's type representative.
FL.typeRep = function typeRep(x) {
switch (Object.prototype.toString.call (x)) {
case '[object Array]':
return FL.Array (x.length > 0 ? typeRep (x[0]) : FL.Unknown);
case '[object Number]':
return FL.Number;
case '[object String]':
return FL.String;
default:
if (x != null && 'fantasy-land' in x) return x['fantasy-land'];
throw new Error ('Not implemented');
}
};
//# FL.isSemigroup :: TypeRep a -> Boolean
FL.isSemigroup = tr => typeof tr.concat === 'function';
//# FL.isMonoid :: TypeRep a -> Boolean
FL.isMonoid = tr => FL.isSemigroup (tr) && typeof tr.empty === 'function';
// Implementations of a given Fantasy Land type-class method often
// dispatch to the implementation of that method for an inner type.
// To define `equals` for `Identity a`, for example, one requires a
// function of type `Setoid a => a -> a -> a` with which to compare
// the two inner values.
//# FL.concat :: Semigroup a => a -> a -> a
FL.concat = x => {
const tr = FL.typeRep (x);
if (FL.isSemigroup (tr)) return tr.concat (x);
throw new TypeError (show (tr) + ' does not satisfy Semigroup constraint');
};
//# FL.empty :: Monoid a => TypeRep a -> a
FL.empty = tr => {
if (FL.isMonoid (tr)) return tr.empty ();
throw new TypeError (show (tr) + ' does not satisfy Monoid constraint');
};
// This section demonstrates the implications for sanctuary-maybe.
const Maybe = {};
//# Maybe._ :: TypeRep a -> TypeRep (Maybe a)
Maybe._ = $1 => Object.assign (
{'@@show': () => 'Maybe (' + show ($1) + ')'},
// Semigroup a => Monoid (Maybe a)
FL.isSemigroup ($1) ?
{'concat': m1 => m2 =>
m1.isJust ? m2.isJust ? Maybe.Just (FL.concat (m1.value) (m2.value)) : m1 : m2,
'empty': () =>
Maybe.Nothing} :
{}
);
//# Maybe.Nothing_ :: TypeRep a -> Maybe a
Maybe.Nothing_ = $1 => ({
'fantasy-land': Maybe._ ($1),
'@@show': () => 'Nothing',
'isNothing': true,
'isJust': false,
});
//# Maybe.Nothing :: Maybe Unknown
Maybe.Nothing = Maybe.Nothing_ (FL.Unknown);
//# Maybe.Just :: a -> Maybe a
Maybe.Just = value => ({
'fantasy-land': Maybe._ (FL.typeRep (value)),
'@@show': () => 'Just (' + show (value) + ')',
'isNothing': false,
'isJust': true,
'value': value,
});
// This section demonstrates the implications for sanctuary-pair.
//# Pair :: a -> b -> Pair a b
const Pair = fst => snd => ({
'fantasy-land': Pair._ (FL.typeRep (fst)) (FL.typeRep (snd)),
'@@show': () => 'Pair (' + show (fst) + ') (' + show (snd) + ')',
'fst': fst,
'snd': snd,
});
//# Pair._ :: TypeRep a -> TypeRep b -> TypeRep (Pair a b)
Pair._ = $1 => $2 => Object.assign (
{'@@show': () => 'Pair (' + show ($1) + ') (' + show ($2) + ')'},
// (Semigroup a, Semigroup b) => Semigroup (Pair a b)
FL.isSemigroup ($1) && FL.isSemigroup ($2) ?
{'concat': p1 => p2 =>
Pair (FL.concat (p1.fst) (p2.fst))
(FL.concat (p1.snd) (p2.snd))} :
{},
// (Monoid a, Monoid b) => Monoid (Pair a b)
FL.isMonoid ($1) && FL.isMonoid ($2) ?
{'empty': () => Pair (FL.empty ($1)) (FL.empty ($2))} :
{}
);
// eq :: a -> a -> Undefined !
const eq = actual => expected => {
assert.strictEqual (show (actual), show (expected));
};
// throws :: (() -> Undefined !) -> Error -> Undefined !
const throws = thunk => expected => {
assert.throws (
thunk,
err => err.name === expected.name && err.message === expected.message
);
};
eq (FL.concat ('') ('')) ('');
eq (FL.concat ('') ('x')) ('x');
eq (FL.concat ('x') ('')) ('x');
eq (FL.concat ('x') ('y')) ('xy');
throws (() => { FL.concat (0) (0); })
(new TypeError ('Number does not satisfy Semigroup constraint'));
eq (FL.concat ([]) ([])) ([]);
eq (FL.concat ([]) ([0])) ([0]);
eq (FL.concat ([0]) ([])) ([0]);
eq (FL.concat ([0]) ([1])) ([0, 1]);
eq (FL.concat (Maybe.Nothing) (Maybe.Nothing)) (Maybe.Nothing);
eq (FL.concat (Maybe.Nothing) (Maybe.Just ('x'))) (Maybe.Just ('x'));
eq (FL.concat (Maybe.Just ('x')) (Maybe.Nothing)) (Maybe.Just ('x'));
eq (FL.concat (Maybe.Just ('x')) (Maybe.Just ('y'))) (Maybe.Just ('xy'));
throws (() => { FL.concat (Maybe.Just (0)) (Maybe.Just (0)); })
(new TypeError ('Maybe (Number) does not satisfy Semigroup constraint'));
eq (FL.concat (Pair ('x') ([0])) (Pair ('y') ([1]))) (Pair ('xy') ([0, 1]));
throws (() => { FL.concat (Pair ('') (0)) (Pair ('') (0)); })
(new TypeError ('Pair (String) (Number) does not satisfy Semigroup constraint'));
// These aliases make the following examples much clearer.
const NUMBER = FL.Number;
const STRING = FL.String;
const ARRAY = FL.Array;
const MAYBE = Maybe._;
const PAIR = Pair._;
eq (FL.empty (STRING)) ('');
eq (FL.empty (ARRAY (STRING))) ([]);
eq (FL.empty (MAYBE (STRING))) (Maybe.Nothing);
throws (() => { FL.empty (MAYBE (NUMBER)); })
(new TypeError ('Maybe (Number) does not satisfy Monoid constraint'));
eq (FL.empty (PAIR (STRING) (ARRAY (STRING))))
(Pair ('') ([]));
eq (FL.empty (PAIR (STRING) (MAYBE (ARRAY (NUMBER)))))
(Pair ('') (Maybe.Nothing));
throws (() => { FL.empty (PAIR (STRING) (ARRAY (NUMBER))); })
(new TypeError ('Pair (String) (Array (Number)) does not satisfy Monoid constraint'));