From 9aec1bca17065dba8eb68b9d192803246565e633 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Thu, 22 Dec 2022 14:59:48 +0100 Subject: [PATCH 1/4] feat: implement function unequal --- src/numbers/relational.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/numbers/relational.ts b/src/numbers/relational.ts index cd0a418..b5285c6 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 { configDependency } from '../core/Config.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) } -- 2.34.1 From 22f114d7f939018328bc13d42e3aa5e37ea0581b Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Thu, 22 Dec 2022 15:04:12 +0100 Subject: [PATCH 2/4] feat: implement generic square (WIP) --- src/generic/all.ts | 10 ++++++++++ src/generic/arithmetic.ts | 17 +++++++++++++++++ src/generic/type.ts | 3 +++ 3 files changed, 30 insertions(+) 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/generic/all.ts b/src/generic/all.ts new file mode 100644 index 0000000..40a35e4 --- /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<'numbers', GenericReturn> { } +} diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts new file mode 100644 index 0000000..5cb4801 --- /dev/null +++ b/src/generic/arithmetic.ts @@ -0,0 +1,17 @@ +import { ConservativeUnary, Dependency, ImpType, Signature } 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 + } +} + +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 -- 2.34.1 From fa63022656819b602d1013bc66897c7dd19b02d1 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 22 Dec 2022 11:05:39 -0500 Subject: [PATCH 3/4] feat: add generic `square` operation and numeric `unequal` --- src/Complex/type.ts | 13 +++++++++++++ src/all.ts | 1 + src/generic/all.ts | 2 +- src/generic/arithmetic.ts | 26 ++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) 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 index 40a35e4..1a008ac 100644 --- a/src/generic/all.ts +++ b/src/generic/all.ts @@ -6,5 +6,5 @@ export { generic } declare module "../core/Dispatcher" { interface ReturnTypes - extends ForType<'numbers', GenericReturn> { } + extends ForType<'generic', GenericReturn> { } } diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts index 5cb4801..770c766 100644 --- a/src/generic/arithmetic.ts +++ b/src/generic/arithmetic.ts @@ -1,13 +1,35 @@ -import { ConservativeUnary, Dependency, ImpType, Signature } from "../core/Dispatcher"; +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: 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). } } -- 2.34.1 From b5f30b7c58e3fa04465afd8a4453c955206b327d Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Thu, 22 Dec 2022 11:10:23 -0500 Subject: [PATCH 4/4] style: avoid whitespace-only diffs --- src/generic/arithmetic.ts | 2 +- src/numbers/relational.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts index 770c766..46d6922 100644 --- a/src/generic/arithmetic.ts +++ b/src/generic/arithmetic.ts @@ -1,4 +1,4 @@ -import { Dependency, ImpType, ImpReturns } from "../core/Dispatcher"; +import {Dependency, ImpType, ImpReturns} from "../core/Dispatcher"; declare module "./type" { interface GenericReturn { diff --git a/src/numbers/relational.ts b/src/numbers/relational.ts index b5285c6..51d7e07 100644 --- a/src/numbers/relational.ts +++ b/src/numbers/relational.ts @@ -1,5 +1,5 @@ -import { configDependency } from '../core/Config.js' -import { Signature, ImpType, Dependency } from '../core/Dispatcher.js' +import {configDependency} from '../core/Config.js' +import {Signature, ImpType, Dependency} from '../core/Dispatcher.js' const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16 -- 2.34.1