-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
49 lines (44 loc) · 2.57 KB
/
core.ts
File metadata and controls
49 lines (44 loc) · 2.57 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
/* eslint-disable no-use-before-define */
// Lambda Abs
type TypeBindingIdx = number
type TypeFinalBody = (TypeAbs | TypeBindingIdx | TypeFinalBody)[]
type TypeMappedFinalBody = (TypeAbs | TypeMappedFinalBody)[]
export type TypeBody = TypeAbs | TypeFinalBody
export type TypeAbs = { _body: TypeBody, _bindings: TypeAbs[] }
export type TermAbs<Body extends TypeBody, Bindings extends TypeAbs[] = []> = {
_body: Body,
_bindings: Bindings
}
// Lambda App
export type UtilMapFrom<Body extends TypeFinalBody, SrcBindings extends TypeAbs[], Flatten extends boolean = false, SentenceHead extends boolean = true> =
Body extends [infer FirstIdx extends TypeBindingIdx, ...infer Rest extends TypeFinalBody] ?
(SrcBindings[FirstIdx] extends undefined ? true : false) & (`${FirstIdx}` extends `-${string}` ? true : false) extends true ?
never
: [SrcBindings[FirstIdx], ...UtilMapFrom<Rest, SrcBindings, Flatten, false>]
: Body extends [infer FirstAbs extends TypeAbs, ...infer Rest extends TypeFinalBody] ?
[FirstAbs, ...UtilMapFrom<Rest, SrcBindings, Flatten, false>]
: Body extends [infer FirstBody extends TypeFinalBody, ...infer Rest extends TypeFinalBody] ?
UtilMapFrom<FirstBody, SrcBindings> extends never ?
never
: Flatten | (SentenceHead & (FirstBody extends { length: 1 } ? true : false)) extends true ? // Flatten && (SentenceHead || len(FirstBody)=1)
[...UtilMapFrom<FirstBody, SrcBindings, Flatten, SentenceHead>, ...UtilMapFrom<Rest, SrcBindings, Flatten, false>]
: [UtilMapFrom<FirstBody, SrcBindings, Flatten, SentenceHead>, ...UtilMapFrom<Rest, SrcBindings, Flatten, false>]
: []
export type TermApp<M extends TypeAbs, N extends TypeAbs> =
M['_body'] extends TypeAbs ?
TermAbs<M['_body']['_body'], [...M['_bindings'], N]>
: M['_body'] extends TypeFinalBody ?
TermAppRecursive<UtilMapFrom<M['_body'], [...M['_bindings'], N], true, true>>
: never
export type TermAppRecursive<MNList extends TypeMappedFinalBody> =
MNList extends [infer M extends TypeAbs] ?
M
: MNList extends [infer M extends TypeAbs, infer N extends TypeAbs] ?
TermApp<M, N>
: MNList extends [infer M extends TypeAbs, infer N extends TypeMappedFinalBody] ?
TermApp<M, TermAppRecursive<N>>
: MNList extends [infer M extends TypeAbs, infer N extends TypeAbs, ...infer RestNList extends TypeMappedFinalBody] ?
TermAppRecursive<[TermApp<M, N>, ...RestNList]>
: MNList extends [infer M extends TypeAbs, infer N extends TypeMappedFinalBody, ...infer RestNList extends TypeMappedFinalBody] ?
TermAppRecursive<[TermApp<M, TermAppRecursive<N>>, ...RestNList]>
: never