Skip to content

Commit cec04c4

Browse files
committed
Expose more type facilities
1 parent 64f84e3 commit cec04c4

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

packages/core/src/impl.ts

+33-33
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,19 @@ export const Real = Symbol("Real");
251251
export const Tan = Symbol("Tan");
252252

253253
/** Representation of the null type. */
254-
type Nulls = typeof Null;
254+
export type Nulls = typeof Null;
255255

256256
/** Representation of the boolean type. */
257-
type Bools = typeof Bool;
257+
export type Bools = typeof Bool;
258258

259259
/** Representation of the 64-bit floating point type. */
260-
type Reals = typeof Real;
260+
export type Reals = typeof Real;
261261

262262
/** Representation of the 64-bit floating point tangent type. */
263-
type Tans = typeof Tan;
263+
export type Tans = typeof Tan;
264264

265265
/** Representation of a bounded index type (it's just the upper bound). */
266-
type Nats = number;
266+
export type Nats = number;
267267

268268
/** Property key for the index type ID of a vector type. */
269269
const ind = Symbol("index");
@@ -432,11 +432,11 @@ const call = (f: Fn, generics: Uint32Array, args: unknown[]): unknown => {
432432
/**
433433
* Map from a type of a type to the type of the symbolic values it represents.
434434
*
435-
* The result should be `Symbolic`, so this should be used in any situation
436-
* where the abstract value is being synthesized (e.g. the parameters of the
437-
* body of `fn` or `vec`, or the returned value from a call or `select`).
435+
* This should be used in any situation where the abstract value is being
436+
* synthesized (e.g. the parameters of the body of `fn` or `vec`, or the
437+
* returned value from a call or `select`).
438438
*/
439-
type ToSymbolic<T> = T extends Nulls
439+
export type Symbolic<T> = T extends Nulls
440440
? Null
441441
: T extends Bools
442442
? Bool
@@ -447,17 +447,17 @@ type ToSymbolic<T> = T extends Nulls
447447
: T extends Nats
448448
? Nat
449449
: T extends Vecs<any, infer V>
450-
? Vec<ToSymbolic<V>>
451-
: { [K in keyof T]: ToSymbolic<T[K]> };
450+
? Vec<Symbolic<V>>
451+
: { [K in keyof T]: Symbolic<T[K]> };
452452

453453
/**
454454
* Map from a type of a type to the type of the abstract values it represents.
455455
*
456-
* The result should be `Value`, so this should be used in any situation where
457-
* the abstract value is provided by the user (e.g. the returned value in the
458-
* body of `fn` or `vec`, or the inputs to a call or `select).
456+
* This should be used in any situation where the abstract value is provided by
457+
* the user (e.g. the returned value in the body of `fn` or `vec`, or the inputs
458+
* to a call or `select).
459459
*/
460-
type ToValue<T> = T extends Nulls
460+
export type Value<T> = T extends Nulls
461461
? Null
462462
: T extends Bools
463463
? Bool
@@ -468,25 +468,25 @@ type ToValue<T> = T extends Nulls
468468
: T extends Nats
469469
? Nat
470470
: T extends Vecs<any, infer V>
471-
? Vec<ToValue<V>> | ToValue<V>[]
472-
: { [K in keyof T]: ToValue<T[K]> };
471+
? Vec<Value<V>> | Value<V>[]
472+
: { [K in keyof T]: Value<T[K]> };
473473

474-
/** Map from a parameter type array to a symbolic parameter value type array. */
474+
/** Map from parameter type array to symbolic parameter value type array. */
475475
type SymbolicParams<T> = {
476-
[K in keyof T]: ToSymbolic<T[K]>;
476+
[K in keyof T]: Symbolic<T[K]>;
477477
};
478478

479-
/** Map from a parameter type array to an abstract parameter value type array. */
479+
/** Map from parameter type array to abstract parameter value type array. */
480480
type ValueParams<T> = {
481-
[K in keyof T]: ToValue<T[K]>;
481+
[K in keyof T]: Value<T[K]>;
482482
};
483483

484484
/** Construct an abstract function by abstractly interpreting `f` once. */
485485
export const fn = <const P extends readonly any[], const R>(
486486
params: P,
487487
ret: R,
488-
f: (...args: SymbolicParams<P>) => ToValue<R>,
489-
): Fn & ((...args: ValueParams<P>) => ToSymbolic<R>) => {
488+
f: (...args: SymbolicParams<P>) => Value<R>,
489+
): Fn & ((...args: ValueParams<P>) => Symbolic<R>) => {
490490
// TODO: support closures
491491
if (context !== undefined)
492492
throw Error("can't define a function while defining another function");
@@ -535,8 +535,8 @@ export const fn = <const P extends readonly any[], const R>(
535535
export const opaque = <const P extends readonly Reals[], const R extends Reals>(
536536
params: P,
537537
ret: R,
538-
f: (...args: JsArgs<SymbolicParams<P>>) => ToJs<ToValue<R>>,
539-
): Fn & ((...args: ValueParams<P>) => ToSymbolic<R>) => {
538+
f: (...args: JsArgs<SymbolicParams<P>>) => ToJs<Value<R>>,
539+
): Fn & ((...args: ValueParams<P>) => Symbolic<R>) => {
540540
// TODO: support more complicated signatures for opaque functions
541541
const func = new wasm.Func(params.length, f);
542542
const g: any = (...args: any): any =>
@@ -958,15 +958,15 @@ export const xor = (p: Bool, q: Bool): Bool => {
958958
export const select = <const T>(
959959
cond: Bool,
960960
ty: T,
961-
then: ToValue<T>,
962-
els: ToValue<T>,
963-
): ToSymbolic<T> => {
961+
then: Value<T>,
962+
els: Value<T>,
963+
): Symbolic<T> => {
964964
const ctx = getCtx();
965965
const t = tyId(ctx, ty);
966966
const p = boolId(ctx, cond);
967967
const a = valId(ctx, t, then);
968968
const b = valId(ctx, t, els);
969-
return idVal(ctx, t, ctx.block.select(ctx.func, p, t, a, b)) as ToSymbolic<T>;
969+
return idVal(ctx, t, ctx.block.select(ctx.func, p, t, a, b)) as Symbolic<T>;
970970
};
971971

972972
/** Return the variable ID for the abstract floating point number `x`. */
@@ -1049,8 +1049,8 @@ export const geq = (x: Real, y: Real): Bool => {
10491049
export const vec = <const I, const T>(
10501050
index: I,
10511051
elem: T,
1052-
f: (i: ToSymbolic<I>) => ToValue<T>,
1053-
): Vec<ToSymbolic<T>> => {
1052+
f: (i: Symbolic<I>) => Value<T>,
1053+
): Vec<Symbolic<T>> => {
10541054
const ctx = getCtx();
10551055
const i = tyId(ctx, index);
10561056
const e = tyId(ctx, elem);
@@ -1061,13 +1061,13 @@ export const vec = <const I, const T>(
10611061
const body = new wasm.Block();
10621062
try {
10631063
ctx.block = body;
1064-
out = valId(ctx, e, f(idVal(ctx, i, arg) as ToSymbolic<I>));
1064+
out = valId(ctx, e, f(idVal(ctx, i, arg) as Symbolic<I>));
10651065
} finally {
10661066
if (out === undefined) body.free();
10671067
ctx.block = block;
10681068
}
10691069
const id = block.vec(ctx.func, t, arg, body, out);
1070-
return idVal(ctx, t, id) as Vec<ToSymbolic<T>>;
1070+
return idVal(ctx, t, id) as Vec<Symbolic<T>>;
10711071
};
10721072

10731073
/** Return the variable ID for the abstract number or tangent `x`. */

packages/core/src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
export {
22
Bool,
3+
Bools,
34
Dual,
45
Fn,
6+
Nats,
57
Null,
8+
Nulls,
69
Real,
10+
Reals,
11+
Symbolic,
712
Tan,
13+
Tans,
14+
Value,
815
Vec,
916
Vecs,
1017
abs,

0 commit comments

Comments
 (0)