feat: Narrow tsc typing of operation dependencies/implementations

This commit is contained in:
Glen Whitney 2024-09-29 13:48:06 -07:00
parent 90b66dc863
commit f575582879
19 changed files with 912 additions and 111 deletions

2
src/numbers/all.ts Normal file
View file

@ -0,0 +1,2 @@
export * as type_data from './type'
export * as arithmetic_functions from './arithmetic'

26
src/numbers/arithmetic.ts Normal file
View file

@ -0,0 +1,26 @@
import {implementations} from '@/core/Dispatcher'
import type {CommonSignature} from '@/interfaces/type'
const conservativeSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
export default implementations<CommonSignature<number>>()
.independent({
add: (a, b) => a + b,
unaryMinus: a => -a,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
absquare: a => a * a,
reciprocal: a => 1 / a,
divide: (a, b) => a / b,
conj: a => a,
conservativeSqrt })
.dependent({config: {}, complex: {}}, {
sqrt: dep => {
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(-a))
}
}})
.ship()

31
src/numbers/type.ts Normal file
View file

@ -0,0 +1,31 @@
import type {Complex} from '@/Complex/type'
import {implementations} from '@/core/Dispatcher'
import type {CommonSignature} from '@/interfaces/type'
export const number_type = {
name: 'number',
before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number',
from: {string: (s: string) => +s }
}
declare module "@/interfaces/type" {
interface AssociatedTypes<T> {
number: {
type: number
zero: 0
one: 1
nan: typeof NaN
real: number
closure: Complex<number>
}
}
}
export default implementations<CommonSignature<number>>()
.independent({
zero: a => 0,
one: a => 1,
nan: a => NaN,
re: a => a
}).ship()