diff --git a/src/Complex/arithmetic.ts b/src/Complex/arithmetic.ts index 6cc916d..d2155d1 100644 --- a/src/Complex/arithmetic.ts +++ b/src/Complex/arithmetic.ts @@ -2,7 +2,7 @@ import {Complex} from './type.js' import type { Dependencies, Signature, Returns, RealType, AliasOf } from '../interfaces/type.js' -import {$reflecType} from '../interfaces/type.js' +import {$reflect} from '../interfaces/type.js' declare module "../interfaces/type" { interface Signatures { @@ -82,8 +82,7 @@ export const sqrt = addTR: Signature<'addReal', T>, addRR: Signature<'add', RealType>, addCR: Signature<'addReal', Complex> - }): - Signature<'sqrt', Complex> => + }): Signature<'sqrt', Complex> => z => { const myabs = dep.conservativeSqrt(dep.absquare(z)) const r = dep.re(z) @@ -98,4 +97,4 @@ export const sqrt = const denom = dep.conservativeSqrt(denomsq) return dep.divideReal(num, denom) } -$reflecType!(sqrt) +$reflect!([sqrt]) diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts index de5b608..37ea84a 100644 --- a/src/generic/arithmetic.ts +++ b/src/generic/arithmetic.ts @@ -1,7 +1,9 @@ import type {Dependencies, Signature} from '../interfaces/type.js' import {$reflect} from '../interfaces/type.js' -export const square = $reflect!('square', - (dep: Dependencies<'multiply', T>) => (z:T) => dep.multiply(z, z)) +export const square = + (dep: Dependencies<'multiply', T>): Signature<'square', T> => + z => dep.multiply(z, z) // z => dep.fooBar(z, z) // fails as desired // z => dep.multiply(z, 'foo') // still fails as desired +$reflect!([square]) diff --git a/src/index.ts b/src/index.ts index 759df12..0952d95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,20 @@ -import { inspect} from 'node:util' +import {inspect} from 'node:util' import {Dispatcher} from './core/Dispatcher.js' +import {Signatures, Deps} from './interfaces/type.js' +import {$$typeToString} from 'ts-macros' import * as Specifications from './all.js' +class Placeholder {} +const allSignatures = + $$typeToString!>>(true, false, true) + +console.log('Found signatures', allSignatures) + export default new Dispatcher(Specifications) import {Complex} from './Complex/type.js' import {absquare as absquare_complex} from './Complex/arithmetic.js' -import { parseReflectedType, split } from './core/parseReflectedType.js' +import {parseReflectedType} from './core/parseReflectedType.js' const mockRealAdd = (a: number, b: number) => a+b const mockComplexAbsquare = (z: Complex) => z.re*z.re + z.im*z.im @@ -36,7 +44,11 @@ console.log('Result of sqrt(-4)=', sqrt(-4)) console.log() console.log('1) NUMBER SQRT') console.log(`1.1) REFLECTED TYPE: "${Specifications.numbers.sqrt.reflectedType}"`) -console.log('1.2) PARSED TYPE:', inspect(parseReflectedType('sqrt', Specifications.numbers.sqrt.reflectedType), { depth: null, colors: true })) +console.log( + '1.2) PARSED TYPE:', + inspect( + parseReflectedType('sqrt', Specifications.numbers.sqrt.reflectedType), + { depth: null, colors: true })) console.log() console.log('2) GENERIC SQUARE') @@ -47,9 +59,3 @@ console.log() console.log('3) COMPLEX SQRT') console.log(`1.1) REFLECTED TYPE: "${Specifications.complex.sqrt.reflectedType}"`) console.log('3.2) PARSED TYPE:', inspect(parseReflectedType('sqrt', Specifications.complex.sqrt.reflectedType), { depth: null, colors: true })) - -// FIXME: cleanup -// console.log() -// console.log('split', split('hello**world**how**are**you', '**')) -// console.log('split', split('hello(test**world)**how**are**you', '**')) -// console.log('split', split('(dep: { multiply: (a: T, b: T) => T; }) => (z: T) => T', '=>')) diff --git a/src/interfaces/type.ts b/src/interfaces/type.ts index e56d385..446c761 100644 --- a/src/interfaces/type.ts +++ b/src/interfaces/type.ts @@ -1,4 +1,4 @@ -import {$$typeToString, $$ident, $$define} from 'ts-macros' +import {$$typeToString} from 'ts-macros' /***** * Every typocomath type has some associated types; they need @@ -74,25 +74,23 @@ export interface Signatures { type SignatureKey = keyof Signatures -export type Signature, T> = Signatures[Name] +export type Signature, T> = + Signatures[Name] +export type DSignature, T> = + Signatures[Name] & {reflectedType?: string, actualType?: Signatures[Name]} export type Returns, T> = ReturnType[Name]> -type Deps = T extends unknown ? { [K in keyof T]: T[K] } : never; +export type Deps = T extends unknown ? { [K in keyof T]: T[K] } : never; export type Dependencies, T> = Deps<{[K in Name]: Signature}> export type AliasOf = T & {aliasOf?: Name} // For defining implementations with type reflection -export function $reflect(name: string, expr: Impl) : Impl & { reflectedType: string} { - $$define!(name, expr, false, false); - $$ident!(name).reflectedType = $$typeToString!(true, false, true); - return $$ident!(name) -} - -export function $reflecType(expr: Impl) { - expr.reflectedType = $$typeToString!(true, false, true); -} - -export function $reflecTypes(tup: ImplTuple) { +export function $reflect(tup: ImplTuple) { +[[tup], (elt: T) => - elt.reflectedType = $$typeToString!(true, false, true)] + elt.reflectedType = $$typeToString!(true, false, true)] } +export function $Dreflect(tup: ImplTuple) { + +[[tup], (elt: T) => + elt.reflectedType = $$typeToString!(true, false, true)] +} +export type RTT = {reflectedType?: string} diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index ba75bfe..354667d 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,11 +1,11 @@ import type {configDependency} from '../core/Config.js' -import type {Dependencies, Signature} from '../interfaces/type.js' -import { $reflecType, $reflecTypes } from '../interfaces/type.js' +import type {Dependencies, Signature, RTT, DSignature} from '../interfaces/type.js' +import {$reflect, $Dreflect} from '../interfaces/type.js' -export const add /*: Signature<'add', number>*/ = (a, b) => a + b +export const add: Signature<'add', number> & RTT = (a, b) => a + b export const unaryMinus: Signature<'unaryMinus', number> = a => -a -export const conj /*: Signature<'conj', number>*/ = a => a -export const subtract /*: Signature<'subtract', number>*/ = (a, b) => a - b +export const conj: DSignature<'conj', number> = a => a +export const subtract = (): Signature<'subtract', number> => (a, b) => a - b export const multiply: Signature<'multiply', number> = (a, b) => a * b export const absquare: Signature<'absquare', number> = a => a * a export const reciprocal: Signature<'reciprocal', number> = a => 1 / a @@ -26,5 +26,5 @@ export const sqrt = return dep.complex(0, Math.sqrt(unaryMinus(a))) } } -$reflecType!(sqrt) -$reflecTypes!([add, conj, subtract]) +$reflect!([add, sqrt, subtract]) +$Dreflect!([conj])