refactor: Possible alternate syntax for reflection

This commit is contained in:
Glen Whitney 2023-09-30 15:52:14 -07:00
parent 20236355c1
commit 457b9cdc91
5 changed files with 19 additions and 12 deletions

View File

@ -23,7 +23,7 @@
url: 'https://code.studioinfinity.org/glen/typocomath.git', url: 'https://code.studioinfinity.org/glen/typocomath.git',
}, },
devDependencies: { devDependencies: {
'@types/node': '20.6.2', '@types/node': '20.8.0',
'cpy-cli': '5.0.0', 'cpy-cli': '5.0.0',
'del-cli': '5.1.0', 'del-cli': '5.1.0',
mkdirp: '3.0.1', mkdirp: '3.0.1',

View File

@ -6,8 +6,8 @@ settings:
devDependencies: devDependencies:
'@types/node': '@types/node':
specifier: 20.6.2 specifier: 20.8.0
version: 20.6.2 version: 20.8.0
cpy-cli: cpy-cli:
specifier: 5.0.0 specifier: 5.0.0
version: 5.0.0 version: 5.0.0
@ -79,8 +79,8 @@ packages:
resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==}
dev: true dev: true
/@types/node@20.6.2: /@types/node@20.8.0:
resolution: {integrity: sha512-Y+/1vGBHV/cYk6OI1Na/LHzwnlNCAfU3ZNGrc1LdRe/LAIbdDPTTv/HU3M7yXN448aTVDq3eKRm2cg7iKLb8gw==} resolution: {integrity: sha512-LzcWltT83s1bthcvjBmiBvGJiiUe84NWRHkw+ZV6Fr41z2FbIzvc815dk2nQ3RAKMuN2fkenM/z3Xv2QzEpYxQ==}
dev: true dev: true
/@types/normalize-package-data@2.4.1: /@types/normalize-package-data@2.4.1:

View File

@ -2,7 +2,7 @@ import {Complex} from './type.js'
import type { import type {
Dependencies, Signature, Returns, RealType, AliasOf Dependencies, Signature, Returns, RealType, AliasOf
} from '../interfaces/type.js' } from '../interfaces/type.js'
import {$reflect} from '../interfaces/type.js' import {$reflecType} from '../interfaces/type.js'
declare module "../interfaces/type" { declare module "../interfaces/type" {
interface Signatures<T> { interface Signatures<T> {
@ -74,7 +74,7 @@ export const divide =
// and we have to get it straight which operations we need on each type, and // and we have to get it straight which operations we need on each type, and
// in fact, we need `addReal` on both T and Complex<T>, hence the dependency // in fact, we need `addReal` on both T and Complex<T>, hence the dependency
// with a custom name, not generated via Dependencies<...> // with a custom name, not generated via Dependencies<...>
export const sqrt = $reflect!('sqrt', export const sqrt =
<T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>> <T>(dep: Dependencies<'equal' | 'conservativeSqrt' | 'unaryMinus', RealType<T>>
& Dependencies<'zero' | 'complex', T> & Dependencies<'zero' | 'complex', T>
& Dependencies<'absquare' | 're' | 'divideReal', Complex<T>> & Dependencies<'absquare' | 're' | 'divideReal', Complex<T>>
@ -97,4 +97,5 @@ export const sqrt = $reflect!('sqrt',
const denomsq = dep.addRR(dep.addRR(myabs, myabs), dep.addRR(r, r)) const denomsq = dep.addRR(dep.addRR(myabs, myabs), dep.addRR(r, r))
const denom = dep.conservativeSqrt(denomsq) const denom = dep.conservativeSqrt(denomsq)
return dep.divideReal(num, denom) return dep.divideReal(num, denom)
}) }
$reflecType!(sqrt)

View File

@ -87,3 +87,7 @@ export function $reflect<Impl>(name: string, expr: Impl) : Impl & { reflectedTyp
$$ident!(name).reflectedType = $$typeToString!<Impl>(true, false, true); $$ident!(name).reflectedType = $$typeToString!<Impl>(true, false, true);
return $$ident!(name) return $$ident!(name)
} }
export function $reflecType<Impl>(expr: Impl) {
expr.reflectedType = $$typeToString!<Impl>(true, false, true);
}

View File

@ -1,6 +1,6 @@
import type {configDependency} from '../core/Config.js' import type {configDependency} from '../core/Config.js'
import type {Dependencies, Signature} from '../interfaces/type.js' import type {Dependencies, Signature} from '../interfaces/type.js'
import { $reflect } from '../interfaces/type.js' import { $reflecType } from '../interfaces/type.js'
export const add: Signature<'add', number> = (a, b) => a + b export const add: Signature<'add', number> = (a, b) => a + b
export const unaryMinus: Signature<'unaryMinus', number> = a => -a export const unaryMinus: Signature<'unaryMinus', number> = a => -a
@ -14,8 +14,9 @@ export const divide: Signature<'divide', number> = (a, b) => a / b
const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a) const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt
export const sqrt = $reflect!('sqrt', export const sqrt =
(dep: configDependency & Dependencies<'complex', number>): Signature<'sqrt', number> => { (dep: configDependency
& Dependencies<'complex', number>): Signature<'sqrt', number> => {
if (dep.config.predictable || !dep.complex) { if (dep.config.predictable || !dep.complex) {
return basicSqrt return basicSqrt
} }
@ -24,4 +25,5 @@ export const sqrt = $reflect!('sqrt',
if (a >= 0) return Math.sqrt(a) if (a >= 0) return Math.sqrt(a)
return dep.complex(0, Math.sqrt(unaryMinus(a))) return dep.complex(0, Math.sqrt(unaryMinus(a)))
} }
}) }
$reflecType!(sqrt)