From 8c06c8f36ea0216892868864da4ebf91c6dc96c7 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 22 Dec 2022 16:12:36 +0000 Subject: [PATCH] feat: Add generic operation `square` and numeric `unequal` (#4) Co-authored-by: Jos de Jong Co-authored-by: Glen Whitney Reviewed-on: https://code.studioinfinity.org/glen/typocomath/pulls/4 --- src/Complex/type.ts | 13 +++++++++++++ src/all.ts | 1 + src/generic/all.ts | 10 ++++++++++ src/generic/arithmetic.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/generic/type.ts | 3 +++ src/numbers/relational.ts | 29 ++++++++++++++++++----------- 6 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 src/generic/all.ts create mode 100644 src/generic/arithmetic.ts create mode 100644 src/generic/type.ts diff --git a/src/Complex/type.ts b/src/Complex/type.ts index a150700..36c040a 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -30,6 +30,19 @@ export interface ComplexReturn { : Params extends BBinary ? Complex // binary case : never + // alternatively if it seems better; each definition is simpler, but at + // the cost of having two keys here: + // complex_unary: Params extends [infer R] ? Complex : never + // complex_binary: Params extends BBinary ? Complex : never + + // There is actually a subtlety here that complex_unary really only works + // on real types that include their own zero value, so it should really be + // complex_unary: Params extends [infer R] + // ? ImpReturns<'zero', [R]> extends R ? Complex : never + // : never + // and that might actually simplify some of the typings of other operations, + // but we'll leave such fine tuning til later, if we adopt this scheme + zero: Params extends [infer Z] // unary ? Z extends Complex // of a Complex parameter ? ImpReturns<'zero', T> extends T ? Z : never // that has its real 0 diff --git a/src/all.ts b/src/all.ts index 192c7be..e2e83f1 100644 --- a/src/all.ts +++ b/src/all.ts @@ -1,2 +1,3 @@ export * from './numbers/all.js' export * from './Complex/all.js' +export * from './generic/all.js' diff --git a/src/generic/all.ts b/src/generic/all.ts new file mode 100644 index 0000000..1a008ac --- /dev/null +++ b/src/generic/all.ts @@ -0,0 +1,10 @@ +import { ForType } from '../core/Dispatcher.js' +import { GenericReturn } from './type.js' +import * as generic from './arithmetic.js' + +export { generic } + +declare module "../core/Dispatcher" { + interface ReturnTypes + extends ForType<'generic', GenericReturn> { } +} diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts new file mode 100644 index 0000000..46d6922 --- /dev/null +++ b/src/generic/arithmetic.ts @@ -0,0 +1,39 @@ +import {Dependency, ImpType, ImpReturns} from "../core/Dispatcher"; + +declare module "./type" { + interface GenericReturn { + // Jos: not sure how to define this or why it is needed + // square: Signature + // square: ConservativeUnary + // square: Params extends [infer R] + // ? R extends number ? UnderlyingReal : never + // : never + + // The type of `square` in this interface, instantiated with the type + // Params of a parameter list, needs to be the return type of the + // operation `square` on those parameters. In other words, `square` gives + // a type transformer from the tuple type of its parameters to its return + // type. + // That's how Dispatcher knows what the return type will be in + // `Dependency<'square', [bigint]>`, for example: it instantiates + // GenericReturn with Params equal to [bigint] and then grabs the + // type of the `square` property. Hence we write: + + square: Params extends [infer T] // square only takes 1 arbitrary parameter + ? ImpReturns<'multiply', [T, T]> // and returns whatever multiply does + : never; // otherwise if not a single argument, this implementation + // doesn't handle it + + // If square had more than one implementation in this collection, we could + // either add more conditional clauses to the above type transformer + // as I did in Complex/type.ts for `complex`, or we could have two + // different keys that both start with `square_` and Dispatcher will + // check both (as I have now done in comments in Complex/type.ts and + // verified that also works). + } +} + +export const square = + (dep: Dependency<'multiply', [T, T]>): + ImpType<'square', [T]> => + z => dep.multiply(z, z) diff --git a/src/generic/type.ts b/src/generic/type.ts new file mode 100644 index 0000000..8589417 --- /dev/null +++ b/src/generic/type.ts @@ -0,0 +1,3 @@ +export interface GenericReturn { + +} \ No newline at end of file diff --git a/src/numbers/relational.ts b/src/numbers/relational.ts index cd0a418..51d7e07 100644 --- a/src/numbers/relational.ts +++ b/src/numbers/relational.ts @@ -1,27 +1,34 @@ import {configDependency} from '../core/Config.js' -import {Signature, ImpType} from '../core/Dispatcher.js' +import {Signature, ImpType, Dependency} from '../core/Dispatcher.js' const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16 declare module "./type" { interface NumbersReturn { equal: Signature + unequal: Signature } } export const equal = (dep: configDependency): ImpType<'equal', [number, number]> => - (x, y) => { - const eps = dep.config.epsilon - if (eps === null || eps === undefined) return x === y - if (x === y) return true - if (isNaN(x) || isNaN(y)) return false + (x, y) => { + const eps = dep.config.epsilon + if (eps === null || eps === undefined) return x === y + if (x === y) return true + if (isNaN(x) || isNaN(y)) return false - if (isFinite(x) && isFinite(y)) { - const diff = Math.abs(x - y) - if (diff < DBL_EPSILON) return true - return diff <= Math.max(Math.abs(x), Math.abs(y)) * eps + if (isFinite(x) && isFinite(y)) { + const diff = Math.abs(x - y) + if (diff < DBL_EPSILON) return true + return diff <= Math.max(Math.abs(x), Math.abs(y)) * eps + } + + return false } - return false +export const unequal = (dep: Dependency<'equal', [number, number]>): + ImpType<'unequal', [number, number]> => + (x, y) => { + return !dep.equal(x, y) }