diff --git a/.gitignore b/.gitignore index 0dc139f..4788ab4 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,9 @@ dist # Stores VSCode versions used for testing VSCode extensions .vscode-test +# WebStorm +.idea + # yarn v2 .yarn/cache .yarn/unplugged diff --git a/src/Complex/all.ts b/src/Complex/all.ts index 9d417b8..b0ec237 100644 --- a/src/Complex/all.ts +++ b/src/Complex/all.ts @@ -1,8 +1,3 @@ -import {ForType} from '../core/Dispatcher.js' import * as Complex from './native.js' export {Complex} - -declare module "../core/Dispatcher" { - interface ImplementationTypes extends ForType<'Complex', typeof Complex> {} -} diff --git a/src/Complex/arithmetic.ts b/src/Complex/arithmetic.ts new file mode 100644 index 0000000..1919d81 --- /dev/null +++ b/src/Complex/arithmetic.ts @@ -0,0 +1,127 @@ +import { Complex, complex_binary } from './type.js' + +export const add = + (dep: { + add: (a: T, b: T) => T + }) => + (w: Complex, z: Complex): Complex => + complex_binary(dep.add(w.re, z.re), dep.add(w.im, z.im)) + +export const addReal = + (dep: { + addReal: (a: T, b: T) => T + }) => + (z: Complex, r: T): Complex => + complex_binary(dep.addReal(z.re, r), z.im) + +export const unaryMinus = + (dep: { + unaryMinus: (z: T) => T + }) => + (z: Complex): Complex => + complex_binary(dep.unaryMinus(z.re), dep.unaryMinus(z.im)) + +export const conj = + (dep: { + unaryMinus: (z: T) => T, + conj: (z: T) => T + }) => + (z: Complex): Complex => + complex_binary(dep.conj(z.re), dep.unaryMinus(z.im)) + +export const subtract = + (dep: { + subtract: (a: T, b: T) => T + }) => + (w: Complex, z: Complex): Complex => + complex_binary(dep.subtract(w.re, z.re), dep.subtract(w.im, z.im)) + +export const multiply = + (dep: { + add: (a: T, b: T) => T, + subtract: (a: T, b: T) => T, + multiply: (a: T, b: T) => T, + conj: (z: T) => T + }) => + (w: Complex, z: Complex): Complex => { + const mult = dep.multiply + const realpart = dep.subtract(mult(w.re, z.re), mult(dep.conj(w.im), z.im)) + const imagpart = dep.add(mult(dep.conj(w.re), z.im), mult(w.im, z.re)) + return complex_binary(realpart, imagpart) + } + +export const absquare = + (dep: { + add: (a: U, b: U) => U, + absquare: (z: T) => U + }) => + (z: Complex): U => dep.add(dep.absquare(z.re), dep.absquare(z.im)) + +export const divideByReal = + (dep: { + divideByReal: (a: T, b: T) => T + }) => + (z: Complex, r: T) => + complex_binary(dep.divideByReal(z.re, r), dep.divideByReal(z.im, r)) + +export const reciprocal = + (dep: { + conj: (z: Complex) => Complex, + absquare: (z: Complex) => T, + divideByReal: (a: Complex, b: T) => Complex, + zero: (z: T) => T, + }) => + (z: Complex): Complex => dep.divideByReal(dep.conj(z), dep.absquare(z)) + +export const divide = + (dep: { + multiply: (a: Complex, b: Complex) => Complex, + reciprocal: (z: Complex) => Complex, + }) => + (w: Complex, z: Complex) => dep.multiply(w, dep.reciprocal(z)) + +export const complexSqrt = + (dep: { + conservativeSqrt: (a: T) => T, + isSquare: (a: T) => boolean, + complex: (a: T) => Complex, + unaryMinus: (a: T) => T, + zero: (a: T) => T, + nan: (a: Complex) => Complex + }) => + (r: T): Complex => { + if (dep.isSquare(r)) return dep.complex(dep.conservativeSqrt(r)) + const negative = dep.unaryMinus(r) + if (dep.isSquare(negative)) { + return complex_binary( + dep.zero(r), dep.conservativeSqrt(negative)) + } + // neither the real number or its negative is a square; could happen + // for example with bigint. So there is no square root. So we have to + // return the NaN of the type. + return dep.nan(dep.complex(r)) + } + +export const sqrt = + (dep: { + isReal: (z: Complex) => boolean, + complexSqrt: (a: T) => Complex, + conservativeSqrt: (a: T) => T, + absquare: (a: Complex) => T, + addReal: (a: Complex, b: T) => Complex, + divideByReal: (a: Complex, b: T) => Complex, + add: (a: T, b: T) => T, + re: (a: Complex) => T, + + }) => + (z: Complex) => { + if (dep.isReal(z)) return dep.complexSqrt(z.re) + const myabs = dep.conservativeSqrt(dep.absquare(z)) + const num = dep.addReal(z, myabs) + const r = dep.re(z) + const denomsq = dep.add(dep.add(myabs, myabs), dep.add(r, r)) + const denom = dep.conservativeSqrt(denomsq) + return dep.divideByReal(num, denom) + } + +export const conservativeSqrt = sqrt diff --git a/src/Complex/predicate.ts b/src/Complex/predicate.ts new file mode 100644 index 0000000..47365fb --- /dev/null +++ b/src/Complex/predicate.ts @@ -0,0 +1,12 @@ +import { Complex } from './type.js' + +export const isReal = + (dep: { + equal: (a: T, b: T) => boolean, + add: (a: T, b: T) => T, + isReal: (z: T) => boolean + }) => + (z: Complex) => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im)) + +export const isSquare = + (z: Complex) => true // FIXME: not correct for Complex once we get there diff --git a/src/Complex/relational.ts b/src/Complex/relational.ts new file mode 100644 index 0000000..2621972 --- /dev/null +++ b/src/Complex/relational.ts @@ -0,0 +1,7 @@ +import { Complex } from './type.js' + +export const equal = + (dep: { + equal: (a: T, b: T) => boolean + }) => + (w: Complex, z: Complex): boolean => dep.equal(w.re, z.re) && dep.equal(w.im, z.im) diff --git a/src/Complex/type.ts b/src/Complex/type.ts index affbedc..50a231f 100644 --- a/src/Complex/type.ts +++ b/src/Complex/type.ts @@ -1,22 +1,54 @@ -import {joinTypes, typeOfDependency, Dependency} from '../core/Dispatcher.js' +import { + joinTypes, typeOfDependency, Dependency, +} from '../core/Dispatcher.js' -export type Complex = {re: T; im: T;} +export type Complex = { re: T; im: T; } export const Complex_type = { - test: (dep: {testT: (z: unknown) => z is T}) => + test: (dep: { testT: (z: unknown) => z is T }) => (z: unknown): z is Complex => - typeof z === 'object' && 're' in z && 'im' in z - && dep.testT(z.re) && dep.testT(z.im), + typeof z === 'object' && z != null && 're' in z && 'im' in z + && dep.testT(z.re) && dep.testT(z.im), infer: (dep: typeOfDependency) => (z: Complex) => joinTypes(dep.typeOf(z.re), dep.typeOf(z.im)), from: { T: (dep: Dependency<'zero', [T]>) => (t: T) => - ({re: t, im: dep.zero(t)}), - Complex: (dep: {convert: (from: U) => T}) => - (z: Complex) => ({re: dep.convert(z.re), im: dep.convert(z.im)}) + ({ re: t, im: dep.zero(t) }), + Complex: (dep: { convert: (from: U) => T }) => + (z: Complex) => ({ re: dep.convert(z.re), im: dep.convert(z.im) }) } } -export const complex_unary = (dep: Dependency<'zero', [T]>) => - (t: T) => ({re: t, im: dep.zero(t)}) -export const complex_binary = (t: T, u: T) => ({re: t, im: u}) +export const complex_unary = + (dep: { + zero: (z: T) => Complex + }) => + (t: T) => ({ re: t, im: dep.zero(t) }) + +export const complex_binary = + (t: T, u: T): Complex => ({ re: t, im: u }) + +export const zero = + (dep: { + zero: (z: T) => T + }) => + (z: Complex): Complex => complex_binary(dep.zero(z.re), dep.zero(z.im)) + +export const one = + (dep: { + zero: (z: T) => T, + one: (z: T) => T + }) => + (z: Complex): Complex => complex_binary(dep.one(z.re), dep.zero(z.im)) + +export const nan = + (dep: { + nan: (z: T) => T + }) => + (z: Complex): Complex => complex_binary(dep.nan(z.re), dep.nan(z.im)) + +export const re = + (dep: { + re: (z: T) => T + }) => + (z: Complex): T => dep.re(z.re) 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/core/Config.ts b/src/core/Config.ts index 3765328..c1eb24c 100644 --- a/src/core/Config.ts +++ b/src/core/Config.ts @@ -1,4 +1,5 @@ export type Config = { + epsilon: number predictable: boolean } diff --git a/src/core/Dispatcher.ts b/src/core/Dispatcher.ts index bed8f0d..9b005d9 100644 --- a/src/core/Dispatcher.ts +++ b/src/core/Dispatcher.ts @@ -9,16 +9,62 @@ type TypeName = string type Parameter = TypeName -type Signature = Parameter[] +type InputSignature = Parameter[] +type DependenciesType = Record -export interface ImplementationTypes {} export type typeOfDependency = {typeOf: (x: unknown) => TypeName} -// Helper for collecting implementations -// (Really just suffixes the type name onto the keys of exports) -export type ForType = keyof Exports extends string - ? {[K in keyof Exports as `${K}_${T}`]: Exports[K]} - : never +// All of the implementations must publish descriptions of their +// return types into the following interface, using the format +// described just below: +export interface ReturnTypes {} + +/***** + To describe one implementation for a hypothetical operation `foo`, there + should be a property of the interface whose name starts with `foo` and whose + next character, if any, is an underscore. The type of this property + must be the return type of that implementation when Params matches the + parameter types of the implementation, and `never` otherwise. + Thus to describe an implementation that takes a number and a string and + returns a boolean, for example, you could write + ``` + declare module "Dispatcher" { + interface ReturnTypes { + foo_example: Params extends [number, string] ? boolean : never + } + } + ``` + If there is another, generic implementation that takes one argument + of any type and returns a Vector of that type, you can say + ``` + ... + foo_generic: Params extends [infer T] ? Vector : never + ... + ``` + In practice, each subdirectory corresponding to a type, like Complex, + defines an interface, like `ComplexReturn` for the implementations + in that subdirectory, which can mostly be defined without suffixes because + there's typically just a single implementation within that domain. + Then the module responsible for collating all of the implementations for + that type inserts all of the properties of that interface into `ReturnTypes` + suitably suffixed to avoid collisions. + + One might think that simply defining an implementation for `foo` + of type `(n: number, s: string) => boolean` would provide all of the same + information as the type of the key `foo_example` in the ReturnTypes + interface above, but in practice TypeScript has challenges in extracting + types relating to functions. (In particular, there is no + way to get the specialized return type of a generic function when it is + called on aguments whose specific types match the generic parameters.) + Hence the need for this additional mechanism to specify return types, in + a way readily suited for TypeScript type computations. +*****/ + +// Helpers for specifying signatures + +// A basic signature with concrete types +export type Signature = + CandidateParams extends ActualParams ? Returns : never //dummy implementation for now export function joinTypes(a: TypeName, b: TypeName) { @@ -26,27 +72,27 @@ export function joinTypes(a: TypeName, b: TypeName) { return 'any' } -/** - * Build up to Dependency type lookup - */ -type DependenciesType = Record +// Used to filter keys that match a given operation name +type BeginsWith = Name | `${Name}_${string}` -type FinalShape = - FuncType extends (arg: DependenciesType) => Function - ? ReturnType : FuncType +// Look up the return type of an implementation based on its name +// and the parameters it takes +export type ImpReturns = + {[K in keyof ReturnTypes]: K extends BeginsWith + ? ReturnTypes[K] : never}[keyof ReturnTypes] -type BeginsWith = `${Name}${string}` - -type DependencyTypes = - {[K in keyof Ob]: K extends BeginsWith - ? FinalShape extends (...args: Params) => any - ? FinalShape - : never - : never} +// The type of an implementation (with dependencies satisfied, +// based on its name and the parameters it takes +export type ImpType = + (...args: Params) => ImpReturns +// The type of a dependency on an implementation based on its name +// and the parameters it takes (just a simple object with one property +// named the same as the operation, of value type equal to the type of +// that implementation. These can be `&`ed together in case of multiple +// dependencies: export type Dependency = - {[N in Name]: - DependencyTypes[keyof ImplementationTypes]} + {[N in Name]: ImpType} // Now types used in the Dispatcher class itself @@ -64,9 +110,9 @@ type SpecificationsGroup = Record export class Dispatcher { installSpecification( name: string, - signature: Signature, + signature: InputSignature, returns: TypeName, - dependencies: Record, + dependencies: Record, behavior: Function // possible todo: constrain this type based // on the signature, return type, and dependencies. Not sure if // that's really possible, though. diff --git a/src/generic/all.ts b/src/generic/all.ts new file mode 100644 index 0000000..1b1b8a4 --- /dev/null +++ b/src/generic/all.ts @@ -0,0 +1,3 @@ +import * as generic from './arithmetic.js' + +export { generic } diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts new file mode 100644 index 0000000..99a7aab --- /dev/null +++ b/src/generic/arithmetic.ts @@ -0,0 +1,5 @@ +export const square = + (dep: { + multiply: (x: T, y: T) => T + }) => + (z: T): T => dep.multiply(z, z) diff --git a/src/index.ts b/src/index.ts index bb83486..7508578 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,3 +2,22 @@ import {Dispatcher} from './core/Dispatcher.js' import * as Specifications from './all.js' export default new Dispatcher(Specifications) + + +// Test https://github.com/josdejong/pocomath/issues/1#issuecomment-1364056151 + +import {Complex} from './Complex/type.js' +import {absquare as absquare_complex} from './Complex/arithmetic.js' + +const mockRealAdd = (a: number, b: number) => a+b +const mockComplexAbsquare = (z: Complex) => z.re*z.re + z.im*z.im + +const quatAbsquare = absquare_complex({ + add: mockRealAdd, + absquare: mockComplexAbsquare +}) + +const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}}) +const typeTest: typeof myabs = 7 // check myabs is just a number + +console.log('Result is', myabs) diff --git a/src/numbers/all.ts b/src/numbers/all.ts index 5aea220..deb4a8e 100644 --- a/src/numbers/all.ts +++ b/src/numbers/all.ts @@ -1,8 +1,3 @@ -import {ForType} from '../core/Dispatcher.js' import * as numbers from './native.js' export {numbers} - -declare module "../core/Dispatcher" { - interface ImplementationTypes extends ForType<'numbers', typeof numbers> {} -} diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index e78d9ec..499a942 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,20 +1,28 @@ -import {configDependency} from '../core/Config.js' -import {Dependency} from '../core/Dispatcher.js' +import { Config } from '../core/Config.js' +import type { Complex } from '../Complex/type.js' + +export const add = (a: number, b: number): number => a + b +export const addReal = add +export const unaryMinus = (a: number): number => -a +export const conj = (a: number): number => a +export const subtract = (a: number, b: number): number => a - b +export const multiply = (a: number, b: number): number => a * b +export const absquare = (a: number): number => a * a +export const reciprocal = (a: number): number => 1 / a +export const divide = (a: number, b: number): number => a / b +export const divideByReal = divide + +export const conservativeSqrt = (a: number): number => isNaN(a) ? NaN : Math.sqrt(a) -export const add = (a: number, b: number) => a + b -export const unaryMinus = (a: number) => -a -export const subtract = (a: number, b: number) => a - b -export const multiply = (a: number, b: number) => a * b -export const divide = (a: number, b: number) => a / b export const sqrt = - (dep: configDependency - & Dependency<'complex', [number, number]>) => { - if (dep.config.predictable || !dep.complex) { - return (a: number) => isNaN(a) ? NaN : Math.sqrt(a) - } - return (a: number) => { - if (isNaN(a)) return NaN - if (a >= 0) return Math.sqrt(a) - return dep.complex(0, Math.sqrt(unaryMinus(a))) - } - } + (dep: { + config: Config, + complex: (re: number, im: number) => Complex + }): (a: number) => number | Complex => { + if (dep.config.predictable || !dep.complex) return conservativeSqrt + return a => { + if (isNaN(a)) return NaN + if (a >= 0) return Math.sqrt(a) + return dep.complex(0, Math.sqrt(unaryMinus(a))) + } + } diff --git a/src/numbers/predicate.ts b/src/numbers/predicate.ts new file mode 100644 index 0000000..483b103 --- /dev/null +++ b/src/numbers/predicate.ts @@ -0,0 +1,2 @@ +export const isReal = (a: number) : boolean => true +export const isSquare = (a: number) : boolean => a >= 0 diff --git a/src/numbers/relational.ts b/src/numbers/relational.ts new file mode 100644 index 0000000..dac949a --- /dev/null +++ b/src/numbers/relational.ts @@ -0,0 +1,26 @@ +import { Config } from '../core/Config.js' + +const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16 + +export const equal = + (dep: { + config: Config + }) => (x: number, y: number): boolean => { + 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 + } + + return false + } + +export const unequal = (dep: { + equal: (x: number, y: number) => boolean +}) => + (x: number, y: number): boolean => !dep.equal(x, y) diff --git a/src/numbers/type.ts b/src/numbers/type.ts index 67dbd29..46fbf6f 100644 --- a/src/numbers/type.ts +++ b/src/numbers/type.ts @@ -1,7 +1,10 @@ export const number_type = { before: ['Complex'], test: (n: unknown): n is number => typeof n === 'number', - from: {string: s => +s} + from: { string: (s: string) => +s } } -export const zero = (a: number) => 0 +export const zero = (a: number): number => 0 +export const one = (a: number): number => 1 +export const nan = (a: number): number => NaN +export const re = (a: number): number => a