-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.js
231 lines (202 loc) · 6.71 KB
/
decorators.js
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
/* eslint-disable no-param-reassign, func-names */
import { List } from 'immutable';
import { Inline } from 'slate';
import { expand, hashObj, set as setAddr } from './Address';
import { mkStructuralKey, mkAddrKey, mkText } from './slateHelpers';
import type { Address } from './Address';
import type {
ChildUpdate,
EditorEvent,
Open,
Event,
} from './events';
import type { Term, Unif, UnificationResult } from './types';
import type { SlatePath, SlateVal } from './slateHelpers';
export function mkUpdate(
from: Address,
to: Address,
provenance: EditorEvent
): ChildUpdate {
return { type: 'ChildUpdate', from, to, provenance };
}
// => Class<T & Hashable>
export function hashable<T>(target: T): void {
// $FlowFixMe: https://github.com/facebook/flow/issues/285
Object.defineProperty(target.prototype, 'hash', {
get() {
const obj = {
constructor: target.name,
obj: this.toJS(),
};
return hashObj(obj).hash;
},
});
target.prototype.mkUpdate = function (
to: Term<Address>,
evt: Event
): ChildUpdate {
// TODO: find alternative to the setAddr hack here?
return mkUpdate(this.hash, setAddr(to), evt);
};
}
export function singletonType(target) {
target.value = new target();
target.addr = setAddr(target.value);
}
export function noPullType(target) {
target.prototype.pullType = function (): null { return null; };
}
export function noPullImpl(target) {
target.prototype.pullImpl = function (): null { return null; };
}
// XXX this needs to be revamped
export function fixedType(ty: Term<Address>) {
return function (target) {
/*
target.prototype.pushType = function (
pushedTy: Term<Unif>
): UnificationResult {
return unify(ty, pushedTy);
};
target.prototype.pullType = function (): Term<Unif> {
return ty.hash;
};
*/
};
}
export function noFixedImpl(target) {
target.prototype.pullImpl = function (): ?Term<Unif> { return null; };
}
// Must be a singleton!
export function trivialUnification(target) {
// XXX understand if unifyChildren is necessary given pushType / pullType
// eslint-disable-next-line no-unused-vars
target.unifyChildren = function (l: Term<Address>, r: Term<Address>): Term<Unif> {
return target.value;
};
}
// In other words, stub out this definition that should never be exercised
export function noAddressableChildren(target) {
// eslint-disable-next-line no-unused-vars
target.prototype.acceptChildUpdate = function (args) { return this; };
target.prototype.mapSubterms = function(f: (a: A) => B): Term<B> {
return this;
};
}
// This thing's representation is fixed -- it accepts no typing. Its children
// can still accept typing, though.
export function fixedRepresentation(target) {
target.prototype.handleTypingAtEnd = function (evt) {
return evt;
};
}
export function dispatchEvents(target) {
target.prototype.handle = function (evt: EditorEvent): EditorEvent {
if (evt.type === 'TypingAtEnd') {
return this.handleTypingAtEnd(evt);
} else if (evt.type === 'Backspace') {
return this.handleBackspace(evt);
} else if (evt.type === 'ChildUpdate') {
return this.acceptChildUpdate(evt);
} else if (evt.type === 'ResolveConflict') {
// TODO figure out how to deal with only `Conflict` handling this
return this.resolveConflict(evt);
} else if (evt.type === 'Close') {
return this.mkUpdate(this.close(), evt);
} else if (evt.type === 'Open') {
return this.mkUpdate(this.open(), evt);
} else if (evt.type === 'ConflictToLisp') {
return this.conflictToLisp(evt);
} else if (evt.type === 'QueryMeta') {
return typeof this.queryMeta !== 'undefined'
? this.queryMeta(evt)
: evt;
} else if (evt.type === 'Substitute') {
return {
type: 'ChildUpdate',
from: evt.from,
to: evt.to,
provenance: evt,
};
} else {
throw new Error('Unhandled event!');
}
};
}
// Normalize can never make progress on this class alone.
//
// In other words, this is an introduction form. It's stuck without a
// corresponding elimination. TODO: use this intuition, introduce notion of
// introduction / elimination.
export function irreducible(target) {
target.prototype.normalize = function (): Address {
return this.hash;
};
}
export function noMeta(target) {
target.prototype.meta = function (): Array<Meta> { return []; };
}
export function mapSubtermsIsMap(target) {
target.prototype.mapSubterms = function(f: (a: A) => B): Term<B> {
return this.map(f);
};
}
// * Both arguments must have the same type -- a singleton
// * Result type must be fixed
// * Fun takes and returns boxed values
// * Children named `l` and `r` on the target
// HACK: takes Lisp as an argument since `decorators` is prior to `bootstrap`
export function binaryFunction(target, appearance, nameAddr, fun, childrenTy, Lisp) {
target.unifyChildren = function (
l: Term<Address>,
// eslint-disable-next-line no-unused-vars
r: Term<Address>
): Term<Unif> {
// normalize if possible -- otherwise just unify children
throw new Error('TODO binaryFunction.unifyChildren');
};
target.prototype.slate = function (path: SlatePath): SlateVal {
const l = expand(this.l);
const r = expand(this.r);
return new Inline({
type: 'span',
nodes: List([
l.slate(path.concat(0)),
mkText(appearance, mkStructuralKey(path, 0)),
r.slate(path.concat(1)),
]),
key: mkAddrKey(this.hash, path),
});
};
target.prototype.open = function (evt: Open): ChildUpdate {
const { l, r } = this;
return this.mkUpdate(new Lisp([nameAddr, l, r]), evt);
};
target.prototype.handleBackspace = target.prototype.open;
target.prototype.normalize = function () {
const [l, r] = [this.l, this.r]
.map(expand)
.map(tm => expand(tm.normalize()));
// TODO is this kosher, this instanceof check? Should we only be able to
// ask the terms for their type? That's problematic because their type
// doesn't tell if they're fully reduced or not!
return setAddr(
l instanceof childrenTy && r instanceof childrenTy
? fun(l, r) // new Bool({ b: l_.b || r_.b })
: new target({ l: l.hash, r: r.hash })
);
};
target.prototype.acceptChildUpdate = function (evt: ChildUpdate): ChildUpdate {
const { from, to } = evt;
// XXX Assuming a ChildUpdate can't change the type of the child. We need
// some way to account for that. Attach to ChildUpdate if types are flowing
// up?
return this.mkUpdate(
this.mapSubterms(addr => (addr === from ? to : addr)),
evt
);
};
target.prototype.mapSubterms = function(f: (a: A) => B): Term<B> {
return this.map(f);
};
}