Compare commits
1 Commits
main
...
json_schem
Author | SHA1 | Date | |
---|---|---|---|
23c05ca24c |
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,7 +2,7 @@
|
|||||||
*~
|
*~
|
||||||
# Typescript
|
# Typescript
|
||||||
# emitted code
|
# emitted code
|
||||||
build
|
obj
|
||||||
|
|
||||||
# ---> Node
|
# ---> Node
|
||||||
# Logs
|
# Logs
|
||||||
|
14
README.md
14
README.md
@ -2,13 +2,9 @@
|
|||||||
|
|
||||||
A final (?) prototype for a refactor of mathjs, culminating the picomath, pocomath, typomath series. Provides an extensible core with "fuzzy" types for its operations, that can at any time generate exact .d.ts file for its current state.
|
A final (?) prototype for a refactor of mathjs, culminating the picomath, pocomath, typomath series. Provides an extensible core with "fuzzy" types for its operations, that can at any time generate exact .d.ts file for its current state.
|
||||||
|
|
||||||
|
To build and run the prototype, run:
|
||||||
|
|
||||||
Convenience scripts:
|
```
|
||||||
|
npx tsc
|
||||||
* `pnpm build` -- compile the package
|
node obj
|
||||||
* `pnpm exec` -- run the compiled code produced by `pnpm build`
|
```
|
||||||
* `pnpm go` -- both of the above in sequence.
|
|
||||||
|
|
||||||
Important installation note:
|
|
||||||
|
|
||||||
after `pnpm install`, you must execute `npx ts-patch install` to activate the ts-macros compiler plugin.
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
{"type": "module"}
|
|
@ -1,16 +1,11 @@
|
|||||||
{
|
{
|
||||||
name: 'typocomath',
|
name: 'typocomath',
|
||||||
version: '0.0.2',
|
version: '0.0.1',
|
||||||
description: 'A hopeful final typescipt-pragmatic mathjs proof-of-concept',
|
description: 'A hopeful final typescipt-pragmatic mathjs proof-of-concept',
|
||||||
main: 'index.ts',
|
main: 'index.ts',
|
||||||
scripts: {
|
scripts: {
|
||||||
test: 'echo "Error: no test specified" && exit 1',
|
test: 'echo "Error: no test specified" && exit 1',
|
||||||
build: 'mkdirp build && cpy etc/package.json build --flat && tsc',
|
|
||||||
start: 'node build',
|
|
||||||
go: 'pnpm clean && pnpm build && pnpm start',
|
|
||||||
clean: 'del-cli build',
|
|
||||||
},
|
},
|
||||||
packageManager: 'pnpm',
|
|
||||||
keywords: [
|
keywords: [
|
||||||
'math',
|
'math',
|
||||||
'algebra',
|
'algebra',
|
||||||
@ -23,13 +18,10 @@
|
|||||||
url: 'https://code.studioinfinity.org/glen/typocomath.git',
|
url: 'https://code.studioinfinity.org/glen/typocomath.git',
|
||||||
},
|
},
|
||||||
devDependencies: {
|
devDependencies: {
|
||||||
'@types/node': '20.8.4',
|
typescript: '^5.1.6',
|
||||||
'cpy-cli': '5.0.0',
|
},
|
||||||
'del-cli': '5.1.0',
|
dependencies: {
|
||||||
mkdirp: '3.0.1',
|
'source-map': '^0.7.4',
|
||||||
'source-map': '0.7.4',
|
'typescript-json-schema': '^0.59.0',
|
||||||
'ts-macros': '2.6.0',
|
|
||||||
'ts-patch': '3.0.2',
|
|
||||||
typescript: '5.1.6',
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
990
pnpm-lock.yaml
990
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,6 @@ 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'
|
|
||||||
|
|
||||||
declare module "../interfaces/type" {
|
declare module "../interfaces/type" {
|
||||||
interface Signatures<T> {
|
interface Signatures<T> {
|
||||||
@ -82,7 +81,8 @@ export const sqrt =
|
|||||||
addTR: Signature<'addReal', T>,
|
addTR: Signature<'addReal', T>,
|
||||||
addRR: Signature<'add', RealType<T>>,
|
addRR: Signature<'add', RealType<T>>,
|
||||||
addCR: Signature<'addReal', Complex<T>>
|
addCR: Signature<'addReal', Complex<T>>
|
||||||
}): Signature<'sqrt', Complex<T>> =>
|
}):
|
||||||
|
Signature<'sqrt', Complex<T>> =>
|
||||||
z => {
|
z => {
|
||||||
const myabs = dep.conservativeSqrt(dep.absquare(z))
|
const myabs = dep.conservativeSqrt(dep.absquare(z))
|
||||||
const r = dep.re(z)
|
const r = dep.re(z)
|
||||||
@ -98,7 +98,4 @@ export const sqrt =
|
|||||||
return dep.divideReal(num, denom)
|
return dep.divideReal(num, denom)
|
||||||
}
|
}
|
||||||
|
|
||||||
$reflect!([
|
export const conservativeSqrt = sqrt
|
||||||
add, addReal, unaryMinus, conj, subtract, multiply, absquare, divideReal,
|
|
||||||
reciprocal, divide, sqrt
|
|
||||||
])
|
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
import {Complex} from './type.js'
|
import {Complex} from './type.js'
|
||||||
import type {Dependencies, Signature} from '../interfaces/type.js'
|
import type {Dependencies, Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const isReal =
|
export const isReal =
|
||||||
<T>(dep: Dependencies<'add' | 'equal' | 'isReal', T>):
|
<T>(dep: Dependencies<'add' | 'equal' | 'isReal', T>):
|
||||||
Signature<'isReal', Complex<T>> =>
|
Signature<'isReal', Complex<T>> =>
|
||||||
z => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
|
z => dep.isReal(z.re) && dep.equal(z.re, dep.add(z.re, z.im))
|
||||||
|
|
||||||
export const isSquare = (): Signature<'isSquare', Complex<unknown>> =>
|
export const isSquare: Signature<'isSquare', Complex<any>> = z => true // FIXME: not correct for Complex<bigint> once we get there
|
||||||
z => true // FIXME: not correct for Complex<bigint> once we get there
|
|
||||||
|
|
||||||
$reflect!([isReal, isSquare])
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import {Complex} from './type.js'
|
import {Complex} from './type.js'
|
||||||
import {Dependencies, Signature} from '../interfaces/type.js'
|
import {Dependencies, Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const equal =
|
export const equal =
|
||||||
<T>(dep: Dependencies<'equal', T>): Signature<'equal', Complex<T>> =>
|
<T>(dep: Dependencies<'equal', T>): Signature<'equal', Complex<T>> =>
|
||||||
(w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)
|
(w, z) => dep.equal(w.re, z.re) && dep.equal(w.im, z.im)
|
||||||
$reflect!([equal])
|
|
||||||
|
@ -2,12 +2,10 @@ import {joinTypes, typeOfDependency} from '../core/Dispatcher.js'
|
|||||||
import type {
|
import type {
|
||||||
ZeroType, OneType, NaNType, Dependencies, Signature, Returns
|
ZeroType, OneType, NaNType, Dependencies, Signature, Returns
|
||||||
} from '../interfaces/type.js'
|
} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export type Complex<T> = { re: T; im: T; }
|
export type Complex<T> = { re: T; im: T; }
|
||||||
|
|
||||||
export const Complex_type = {
|
export const Complex_type = {
|
||||||
name: 'Complex', // just until we have reflection to tell us
|
|
||||||
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
|
test: <T>(dep: { testT: (z: unknown) => z is T }) =>
|
||||||
(z: unknown): z is Complex<T> =>
|
(z: unknown): z is Complex<T> =>
|
||||||
typeof z === 'object' && z != null && 're' in z && 'im' in z
|
typeof z === 'object' && z != null && 're' in z && 'im' in z
|
||||||
@ -63,5 +61,3 @@ export const nan =
|
|||||||
export const re =
|
export const re =
|
||||||
<T>(dep: Dependencies<'re', T>): Signature<'re', Complex<T>> =>
|
<T>(dep: Dependencies<'re', T>): Signature<'re', Complex<T>> =>
|
||||||
z => dep.re(z.re)
|
z => dep.re(z.re)
|
||||||
|
|
||||||
$reflect!([complex, zero, one, nan, re])
|
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
* for specific types (including their own).
|
* for specific types (including their own).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {parseReflectedType, ImplementationDef} from './parseReflectedType.js'
|
|
||||||
|
|
||||||
// First helper types and functions for the Dispatcher
|
// First helper types and functions for the Dispatcher
|
||||||
|
|
||||||
type TypeName = string
|
type TypeName = string
|
||||||
@ -27,7 +25,6 @@ export function joinTypes(a: TypeName, b: TypeName) {
|
|||||||
// Now types used in the Dispatcher class itself
|
// Now types used in the Dispatcher class itself
|
||||||
|
|
||||||
type TypeSpecification = {
|
type TypeSpecification = {
|
||||||
name: string, // just until we get reflection, then we can remove this property
|
|
||||||
before?: TypeName[],
|
before?: TypeName[],
|
||||||
test: ((x: unknown) => boolean)
|
test: ((x: unknown) => boolean)
|
||||||
| ((d: DependenciesType) => (x: unknown) => boolean),
|
| ((d: DependenciesType) => (x: unknown) => boolean),
|
||||||
@ -41,18 +38,14 @@ type SpecificationsGroup = Record<string, SpecObject>
|
|||||||
export class Dispatcher {
|
export class Dispatcher {
|
||||||
installSpecification(
|
installSpecification(
|
||||||
name: string,
|
name: string,
|
||||||
defn: ImplementationDef,
|
signature: Signature,
|
||||||
|
returns: TypeName,
|
||||||
|
dependencies: Record<string, Signature>,
|
||||||
behavior: Function // possible todo: constrain this type based
|
behavior: Function // possible todo: constrain this type based
|
||||||
// on the signature, return type, and dependencies. Not sure if
|
// on the signature, return type, and dependencies. Not sure if
|
||||||
// that's really possible, though.
|
// that's really possible, though.
|
||||||
) {
|
) {
|
||||||
console.log('Pretending to install', name, 'with signatures')
|
console.log('Pretending to install', name, signature, '=>', returns)
|
||||||
for (const signature of defn.fn.signatures) {
|
|
||||||
console.log(' ', signature.args, '=>', signature.returns)
|
|
||||||
}
|
|
||||||
if (defn.fn.aliasOf) {
|
|
||||||
console.log(' As an alias of', defn.fn.aliasOf)
|
|
||||||
}
|
|
||||||
//TODO: implement me
|
//TODO: implement me
|
||||||
}
|
}
|
||||||
installType(name: TypeName, typespec: TypeSpecification) {
|
installType(name: TypeName, typespec: TypeSpecification) {
|
||||||
@ -60,25 +53,23 @@ export class Dispatcher {
|
|||||||
//TODO: implement me
|
//TODO: implement me
|
||||||
}
|
}
|
||||||
constructor(collection: SpecificationsGroup) {
|
constructor(collection: SpecificationsGroup) {
|
||||||
const implementations = []
|
|
||||||
for (const key in collection) {
|
for (const key in collection) {
|
||||||
console.log('Working on', key)
|
console.log('Working on', key)
|
||||||
for (const identifier in collection[key]) {
|
for (const identifier in collection[key]) {
|
||||||
const item = collection[key][identifier]
|
console.log('Handling', key, ':', identifier)
|
||||||
if (typeof item === 'function') {
|
const parts = identifier.split('_')
|
||||||
implementations.push([key, identifier, item])
|
if (parts[parts.length - 1] === 'type') {
|
||||||
} else {
|
parts.pop()
|
||||||
console.log('Handling type', key, ':', identifier)
|
const name = parts.join('_')
|
||||||
this.installType(
|
this.installType(
|
||||||
item.name, collection[key][identifier] as TypeSpecification)
|
name, collection[key][identifier] as TypeSpecification)
|
||||||
|
} else {
|
||||||
|
const name = parts[0]
|
||||||
|
this.installSpecification(
|
||||||
|
name, ['dunno'], 'unsure', {},
|
||||||
|
collection[key][identifier] as Function)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const trio of implementations) {
|
|
||||||
const [k, id, imp] = trio
|
|
||||||
console.log('Handling implementation', id, 'from', k)
|
|
||||||
this.installSpecification(
|
|
||||||
id, parseReflectedType(id, imp.reflectedType), imp)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,279 +0,0 @@
|
|||||||
export type FunctionDef = {
|
|
||||||
name: string,
|
|
||||||
aliasOf?: string,
|
|
||||||
signatures: Array<{
|
|
||||||
args: Array<{ name: string, type: string }>
|
|
||||||
returns: string
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type ImplementationDef = {
|
|
||||||
fn: FunctionDef,
|
|
||||||
dependencies: Record<string, FunctionDef>
|
|
||||||
genericParameter: string | null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a reflected type coming out of TypeScript into a structured object, for example:
|
|
||||||
*
|
|
||||||
* '<T>(dep: configDependency & { complex: ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>); }) => (a: number) => number | Complex<number>'
|
|
||||||
*/
|
|
||||||
export function parseReflectedType(name: string, reflectedType: string): ImplementationDef {
|
|
||||||
console.log('For', name, 'parsing', reflectedType)
|
|
||||||
const [factoryArgs, fnsClause] = split(reflectedType, '=>', 2).map(trim)
|
|
||||||
const fn = parseAlias(name, fnsClause)
|
|
||||||
|
|
||||||
// extract the generic parameter like '<T>' at the start of the type
|
|
||||||
const genericParameter = factoryArgs.trim().startsWith('<')
|
|
||||||
? findBlockContents(factoryArgs, '<', '>')?.innerText || null
|
|
||||||
: null
|
|
||||||
|
|
||||||
const factoryArgsInner = findBlockContents(factoryArgs, '(', ')')
|
|
||||||
const depArg = split(factoryArgsInner.innerText, ':').map(trim)[1]
|
|
||||||
const depArgBlocks: string[] = depArg ? split(depArg, '&').map(trim) : []
|
|
||||||
|
|
||||||
const deps = depArgBlocks
|
|
||||||
.filter(depArgBlock => {
|
|
||||||
if (depArgBlock.startsWith('{') || depArgBlock === 'configDependency') {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
throw new SyntaxError(`Cannot parse dependency "${depArgBlock}"`)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.flatMap(parseDependencies)
|
|
||||||
|
|
||||||
const dependencies: Record<string, FunctionDef> = groupBy(deps, 'name')
|
|
||||||
|
|
||||||
return {fn, dependencies, genericParameter }
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseDependencies(deps: string): FunctionDef[] {
|
|
||||||
if (deps === 'configDependency') {
|
|
||||||
return [{name: 'config', signatures: [{args: [], returns: 'Config'}]}]
|
|
||||||
}
|
|
||||||
const inner = findBlockContents(deps, '{', '}').innerText
|
|
||||||
return split(inner, ';')
|
|
||||||
.map(trim)
|
|
||||||
.filter(notEmpty)
|
|
||||||
.map(parseDependency)
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse a dependency like "complex: ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>)"
|
|
||||||
function parseDependency(dep: string): FunctionDef {
|
|
||||||
const [name, def] = split(dep, ':').map(trim)
|
|
||||||
|
|
||||||
return parseAlias(name, def)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse a possibly aliased function
|
|
||||||
function parseAlias(name: string, alias: string): FunctionDef {
|
|
||||||
const { aliasOf, innerSignature } = parseAliasOf(alias)
|
|
||||||
|
|
||||||
return { name, signatures: parseSignatures(innerSignature), aliasOf }
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse function signatures like ((re: number) => Complex<number>) | ((re: number, im: number) => Complex<number>)
|
|
||||||
// But also have to succeed on (a: number) => number | Complex<number>
|
|
||||||
// That's why we only split on an alternation bar `|` that's followed by
|
|
||||||
// a parenthesis; that way we avoid splitting a union return type. Note
|
|
||||||
// this is not necessarily foolproof, as there could be a return type that
|
|
||||||
// is a union with a complicated piece that has to be enclosed in parens;
|
|
||||||
// but so far it seems to work in practice.
|
|
||||||
function parseSignatures(sigs: string) {
|
|
||||||
return split(sigs, /[|]\s*(?=[(])/)
|
|
||||||
.map(trim)
|
|
||||||
.map(stripParenthesis)
|
|
||||||
.map(signature => {
|
|
||||||
const [argsBlock, returns] = split(signature, '=>').map(trim)
|
|
||||||
|
|
||||||
if (!returns) {
|
|
||||||
throw new SyntaxError(`Failed to find return type in '${signature}'`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
args: parseArgs(argsBlock),
|
|
||||||
returns
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse args like "(re: number, im: number)"
|
|
||||||
function parseArgs(argsBlock: string) : Array<{name: string, type: string}> {
|
|
||||||
const args = findBlockContents(argsBlock, '(', ')').innerText
|
|
||||||
|
|
||||||
return split(args, ',')
|
|
||||||
.map(trim)
|
|
||||||
.map(arg => {
|
|
||||||
const [name, type] = split(arg, ':').map(trim)
|
|
||||||
return { name, type}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse "AliasOf<"divide", (a: Complex<T>, b: RealType<Complex<T>>) => Complex<T>>"
|
|
||||||
function parseAliasOf(signature: string) : { innerSignature: string, aliasOf: string | undefined } {
|
|
||||||
if (!signature.startsWith('AliasOf')) {
|
|
||||||
return {
|
|
||||||
innerSignature: signature,
|
|
||||||
aliasOf: undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const inner = findBlockContents(signature, '<', '>').innerText.trim()
|
|
||||||
const [aliasOfWithQuotes, innerSignature] = split(inner, ',').map(trim)
|
|
||||||
return {
|
|
||||||
innerSignature,
|
|
||||||
aliasOf: aliasOfWithQuotes.substring(1, aliasOfWithQuotes.length - 1) // remove double quotes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove the outer parenthesis, for example "((re: number) => Complex<number>)" returns "(re: number) => Complex<number>"
|
|
||||||
function stripParenthesis(text: string) : string {
|
|
||||||
return text.startsWith('(') && text.endsWith(')')
|
|
||||||
? text.substring(1, text.length - 1)
|
|
||||||
: text
|
|
||||||
}
|
|
||||||
|
|
||||||
function findBlockContents(text: string, blockStart: string, blockEnd: string, startIndex = 0) : { start: number, end: number, innerText: string } | undefined {
|
|
||||||
let i = startIndex
|
|
||||||
|
|
||||||
while (!matchSubString(text, blockStart, i) && i < text.length) {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= text.length) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
i++
|
|
||||||
const start = i
|
|
||||||
|
|
||||||
while (!matchSubString(text, blockEnd, i) || matchSubString(text, '=>', i - 1)) {
|
|
||||||
i = skipBrackets(text, i)
|
|
||||||
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= text.length) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
const end = i
|
|
||||||
|
|
||||||
return {
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
innerText: text.substring(start, end)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Given a string, generate a string source for a regexp that will match
|
|
||||||
* exactly the given string.
|
|
||||||
* Uses the fact that the only characters that need to be escaped in
|
|
||||||
* a character class are \, ], and ^
|
|
||||||
*/
|
|
||||||
function regexpQuote(s: string) {
|
|
||||||
const special = '\\]^'
|
|
||||||
let re = ''
|
|
||||||
for (const char of s) {
|
|
||||||
if (special.includes(char)) re += `\\${char}`
|
|
||||||
else re += `[${char}]`
|
|
||||||
}
|
|
||||||
return re
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Split a string by a delimiter, but ignore all occurrences of the delimiter
|
|
||||||
* that are inside bracket pairs <> () [] {}
|
|
||||||
*/
|
|
||||||
export function split(
|
|
||||||
text: string, delimiter: string | RegExp, pieces = 0): string[] {
|
|
||||||
const delim: RegExp = typeof delimiter === 'string'
|
|
||||||
? new RegExp(regexpQuote(delimiter), 'y')
|
|
||||||
: new RegExp(delimiter.source, 'y')
|
|
||||||
const parts: string[] = []
|
|
||||||
|
|
||||||
let i = 0
|
|
||||||
let n = 1
|
|
||||||
let start = 0
|
|
||||||
while (i < text.length && (pieces === 0 || n < pieces)) {
|
|
||||||
i = skipBrackets(text, i)
|
|
||||||
|
|
||||||
delim.lastIndex = i
|
|
||||||
const result = delim.exec(text)
|
|
||||||
if (result) {
|
|
||||||
parts.push(text.substring(start, i))
|
|
||||||
n += 1
|
|
||||||
i += result[0].length
|
|
||||||
start = i
|
|
||||||
}
|
|
||||||
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
parts.push(text.substring(start))
|
|
||||||
|
|
||||||
return parts
|
|
||||||
}
|
|
||||||
|
|
||||||
function skipBrackets(text: string, startIndex: number) : number {
|
|
||||||
let level = 0
|
|
||||||
let i = startIndex
|
|
||||||
|
|
||||||
do {
|
|
||||||
if (isBracketOpen(text, i)) {
|
|
||||||
level++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBracketClose(text, i) && level > 0) {
|
|
||||||
level--
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level === 0) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
i++
|
|
||||||
} while(i < text.length)
|
|
||||||
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
function isBracketOpen(text: string, index: number) {
|
|
||||||
const char = text[index]
|
|
||||||
return char === '(' || char === '<' || char === '[' || char === '{'
|
|
||||||
}
|
|
||||||
|
|
||||||
function isBracketClose(text: string, index: number) {
|
|
||||||
const char = text[index]
|
|
||||||
// we need to take care of not matching the ">" of the operator "=>"
|
|
||||||
return char === ')' || (char === '>' && text[index - 1] !== '=') || char === ']' || char === '}'
|
|
||||||
}
|
|
||||||
|
|
||||||
function matchSubString(text: string, search: string, index: number) : boolean {
|
|
||||||
for (let i = 0; i < search.length; i++) {
|
|
||||||
if (text[i + index] !== search[i]) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
function trim(text: string) : string {
|
|
||||||
return text.trim()
|
|
||||||
}
|
|
||||||
|
|
||||||
function notEmpty(text: string) : boolean {
|
|
||||||
return text.length > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
function groupBy<T>(items: T[], key: string) : Record<string, T> {
|
|
||||||
const obj: Record<string, T> = {}
|
|
||||||
|
|
||||||
items.forEach((item) => {
|
|
||||||
obj[item[key]] = item
|
|
||||||
})
|
|
||||||
|
|
||||||
return obj
|
|
||||||
}
|
|
@ -1,9 +1,5 @@
|
|||||||
import type {Dependencies, Signature} from '../interfaces/type.js'
|
import type {Dependencies, Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const square =
|
export const square =
|
||||||
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
|
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
|
||||||
z => dep.multiply(z, z)
|
z => dep.multiply(z, z)
|
||||||
// z => dep.fooBar(z, z) // fails as desired
|
|
||||||
// z => dep.multiply(z, 'foo') // still fails as desired
|
|
||||||
$reflect!([square])
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import {Dependencies, Signature} from '../interfaces/type.js'
|
import {Dependencies, Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const unequal =
|
export const unequal =
|
||||||
<T>(dep: Dependencies<'equal', T>): Signature<'unequal', T> =>
|
<T>(dep: Dependencies<'equal', T>): Signature<'unequal', T> =>
|
||||||
(x, y) => !dep.equal(x, y)
|
(x, y) => !dep.equal(x, y)
|
||||||
$reflect!([unequal])
|
|
||||||
|
39
src/index.ts
39
src/index.ts
@ -1,4 +1,3 @@
|
|||||||
import {inspect} from 'node:util'
|
|
||||||
import {Dispatcher} from './core/Dispatcher.js'
|
import {Dispatcher} from './core/Dispatcher.js'
|
||||||
import * as Specifications from './all.js'
|
import * as Specifications from './all.js'
|
||||||
|
|
||||||
@ -6,48 +5,16 @@ export default new Dispatcher(Specifications)
|
|||||||
|
|
||||||
import {Complex} from './Complex/type.js'
|
import {Complex} from './Complex/type.js'
|
||||||
import {absquare as absquare_complex} from './Complex/arithmetic.js'
|
import {absquare as absquare_complex} from './Complex/arithmetic.js'
|
||||||
import {parseReflectedType} from './core/parseReflectedType.js'
|
|
||||||
|
|
||||||
const mockRealAdd = (a: number, b: number) => a+b
|
const mockRealAdd = (a: number, b: number) => a+b
|
||||||
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
|
const mockComplexAbsquare = (z: Complex<number>) => z.re*z.re + z.im*z.im
|
||||||
const mockComplex = (re: number, im: number) => ({ re, im })
|
|
||||||
|
|
||||||
const config = {
|
|
||||||
predictable: false,
|
|
||||||
epsilon: 1e-14
|
|
||||||
}
|
|
||||||
|
|
||||||
const quatAbsquare = absquare_complex({
|
const quatAbsquare = absquare_complex({
|
||||||
add: mockRealAdd,
|
add: mockRealAdd,
|
||||||
absquare: mockComplexAbsquare
|
absquare: mockComplexAbsquare
|
||||||
})
|
})
|
||||||
|
|
||||||
const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}})
|
const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}})
|
||||||
const typeTest: typeof myabs = 7 // check myabs is just a number
|
const typeTest: typeof myabs = 7 // check myabs is just a number
|
||||||
console.log('Result is myabs=', myabs)
|
|
||||||
|
|
||||||
const sqrt = Specifications.numbers.sqrt({
|
console.log('Result is', myabs)
|
||||||
config,
|
|
||||||
complex: mockComplex
|
|
||||||
})
|
|
||||||
console.log('Result of sqrt(16)=', sqrt(16))
|
|
||||||
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()
|
|
||||||
console.log('2) GENERIC SQUARE')
|
|
||||||
console.log(`1.1) REFLECTED TYPE: "${Specifications.generic.square.reflectedType}"`)
|
|
||||||
console.log('2.2) PARSED TYPE:', inspect(parseReflectedType('square', Specifications.generic.square.reflectedType), { depth: null, colors: true }))
|
|
||||||
|
|
||||||
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 }))
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import {$$typeToString} from 'ts-macros'
|
|
||||||
|
|
||||||
/*****
|
/*****
|
||||||
* Every typocomath type has some associated types; they need
|
* Every typocomath type has some associated types; they need
|
||||||
* to be published in the following interface. The key is the
|
* to be published in the following interface. The key is the
|
||||||
@ -15,10 +13,6 @@ import {$$typeToString} from 'ts-macros'
|
|||||||
* but that's OK, the generic parameter doesn't hurt in those cases.
|
* but that's OK, the generic parameter doesn't hurt in those cases.
|
||||||
****/
|
****/
|
||||||
|
|
||||||
type ValueIntersectionByKeyUnion<T, TKey extends keyof T> = {
|
|
||||||
[P in TKey]: (k: T[P])=>void
|
|
||||||
} [TKey] extends ((k: infer I)=>void) ? I : never
|
|
||||||
|
|
||||||
export interface AssociatedTypes<T> {
|
export interface AssociatedTypes<T> {
|
||||||
undefined: {
|
undefined: {
|
||||||
type: undefined
|
type: undefined
|
||||||
@ -30,10 +24,10 @@ export interface AssociatedTypes<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AssociatedTypeNames = keyof AssociatedTypes<unknown>['undefined']
|
type AssociatedTypeNames = keyof AssociatedTypes<unknown>['undefined']
|
||||||
type ALookup<T, Name extends AssociatedTypeNames> = ValueIntersectionByKeyUnion<{
|
type ALookup<T, Name extends AssociatedTypeNames> = {
|
||||||
[K in keyof AssociatedTypes<T>]:
|
[K in keyof AssociatedTypes<T>]:
|
||||||
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : unknown},
|
T extends AssociatedTypes<T>[K]['type'] ? AssociatedTypes<T>[K][Name] : never
|
||||||
keyof AssociatedTypes<T>>
|
}[keyof AssociatedTypes<T>]
|
||||||
|
|
||||||
// For everything to compile, zero and one must be subtypes of T:
|
// For everything to compile, zero and one must be subtypes of T:
|
||||||
export type ZeroType<T> = ALookup<T, 'zero'> & T
|
export type ZeroType<T> = ALookup<T, 'zero'> & T
|
||||||
@ -75,16 +69,7 @@ export interface Signatures<T> {
|
|||||||
type SignatureKey<T> = keyof Signatures<T>
|
type SignatureKey<T> = keyof Signatures<T>
|
||||||
|
|
||||||
export type Signature<Name extends SignatureKey<T>, T> = Signatures<T>[Name]
|
export type Signature<Name extends SignatureKey<T>, T> = Signatures<T>[Name]
|
||||||
export type Returns<Name extends SignatureKey<T>, T> =
|
export type Returns<Name extends SignatureKey<T>, T> = ReturnType<Signatures<T>[Name]>
|
||||||
ReturnType<Signatures<T>[Name]>
|
export type Dependencies<Name extends SignatureKey<T>, T> = {[K in Name]: Signature<K, T>}
|
||||||
type Deps<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
|
|
||||||
export type Dependencies<Name extends SignatureKey<T>, T> =
|
|
||||||
Deps<{[K in Name]: Signature<K, T>}>
|
|
||||||
|
|
||||||
export type AliasOf<Name extends string, T> = T & {aliasOf?: Name}
|
export type AliasOf<Name extends string, T> = T & {aliasOf?: Name}
|
||||||
|
|
||||||
// For defining implementations with type reflection
|
|
||||||
export function $reflect<ImplTuple>(tup: ImplTuple) {
|
|
||||||
+[[tup], <T>(elt: T) =>
|
|
||||||
elt.reflectedType = $$typeToString!<T>(true, false, true)]
|
|
||||||
}
|
|
||||||
|
@ -1,35 +1,25 @@
|
|||||||
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'
|
|
||||||
|
|
||||||
export const add = (): Signature<'add', number> => (a, b) => a + b
|
export const add: Signature<'add', number> = (a, b) => a + b
|
||||||
const unaMinus = (a: number) => -a
|
export const unaryMinus: Signature<'unaryMinus', number> = a => -a
|
||||||
export const unaryMinus = (): Signature<'unaryMinus', number> => unaMinus
|
export const conj: Signature<'conj', number> = a => a
|
||||||
export const conj = (): Signature<'conj', number> => a => a
|
export const subtract: Signature<'subtract', number> = (a, b) => a - b
|
||||||
export const subtract = (): Signature<'subtract', number> => (a, b) => a - b
|
export const multiply: Signature<'multiply', 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 absquare = (): Signature<'absquare', number> => a => a * a
|
export const reciprocal: Signature<'reciprocal', number> = a => 1 / a
|
||||||
export const reciprocal = (): Signature<'reciprocal', number> => a => 1 / a
|
export const divide: Signature<'divide', number> = (a, b) => a / b
|
||||||
export const divide = (): Signature<'divide', number> => (a, b) => a / b
|
|
||||||
|
|
||||||
const basicSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
|
const basicSqrt = a => isNaN(a) ? NaN : Math.sqrt(a)
|
||||||
export const conservativeSqrt = (): Signature<'conservativeSqrt', number> =>
|
export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt
|
||||||
basicSqrt
|
|
||||||
|
|
||||||
export const sqrt =
|
export const sqrt =
|
||||||
(dep: configDependency
|
(dep: configDependency & Dependencies<'complex', number>):
|
||||||
& Dependencies<'complex', number>): Signature<'sqrt', number> => {
|
Signature<'sqrt', number> => {
|
||||||
if (dep.config.predictable || !dep.complex) {
|
if (dep.config.predictable || !dep.complex) return basicSqrt
|
||||||
return basicSqrt
|
return a => {
|
||||||
}
|
if (isNaN(a)) return NaN
|
||||||
return a => {
|
if (a >= 0) return Math.sqrt(a)
|
||||||
if (isNaN(a)) return NaN
|
return dep.complex(0, Math.sqrt(unaryMinus(a)))
|
||||||
if (a >= 0) return Math.sqrt(a)
|
}
|
||||||
return dep.complex(0, Math.sqrt(unaMinus(a)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$reflect!([
|
|
||||||
add, unaryMinus, conj, subtract, multiply, absquare, reciprocal, divide,
|
|
||||||
conservativeSqrt, sqrt
|
|
||||||
])
|
|
||||||
|
@ -1,4 +1,2 @@
|
|||||||
export * from './type.js'
|
export * from './type.js'
|
||||||
export * from './arithmetic.js'
|
export * from './arithmetic.js'
|
||||||
export * from './predicate.js'
|
|
||||||
export * from './relational.js'
|
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
import type {Signature} from '../interfaces/type.js'
|
import type {Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const isReal = (): Signature<'isReal', number> => (a) => true
|
export const isReal: Signature<'isReal', number> = (a) => true
|
||||||
export const isSquare = (): Signature<'isSquare', number> => (a) => a >= 0
|
export const isSquare: Signature<'isSquare', number> = (a) => a >= 0
|
||||||
|
|
||||||
$reflect!([isReal, isSquare])
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import {configDependency} from '../core/Config.js'
|
import {configDependency} from '../core/Config.js'
|
||||||
import {Signature} from '../interfaces/type.js'
|
import {Signature} from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
|
const DBL_EPSILON = Number.EPSILON || 2.2204460492503130808472633361816E-16
|
||||||
|
|
||||||
@ -20,4 +19,3 @@ export const equal =
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
$reflect!([equal])
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import type { Signature } from '../interfaces/type.js'
|
import type { Signature } from '../interfaces/type.js'
|
||||||
import {$reflect} from '../interfaces/type.js'
|
|
||||||
|
|
||||||
export const number_type = {
|
export const number_type = {
|
||||||
name: 'number', // just until we have reflection to tell us
|
|
||||||
before: ['Complex'],
|
before: ['Complex'],
|
||||||
test: (n: unknown): n is number => typeof n === 'number',
|
test: (n: unknown): n is number => typeof n === 'number',
|
||||||
from: { string: (s: string) => +s }
|
from: { string: (s: string) => +s }
|
||||||
@ -10,7 +8,7 @@ export const number_type = {
|
|||||||
|
|
||||||
declare module "../interfaces/type" {
|
declare module "../interfaces/type" {
|
||||||
interface AssociatedTypes<T> {
|
interface AssociatedTypes<T> {
|
||||||
number: {
|
numbers: {
|
||||||
type: number
|
type: number
|
||||||
zero: 0
|
zero: 0
|
||||||
one: 1
|
one: 1
|
||||||
@ -21,9 +19,7 @@ declare module "../interfaces/type" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// I don't like the redundancy of repeating 'zero'; any way to eliminate that?
|
// I don't like the redundancy of repeating 'zero'; any way to eliminate that?
|
||||||
export const zero = (): Signature<'zero', number> => (a) => 0
|
export const zero: Signature<'zero', number> = (a) => 0
|
||||||
export const one = (): Signature<'one', number> => (a) => 1
|
export const one: Signature<'one', number> = (a) => 1
|
||||||
export const nan = (): Signature<'nan', number> => (a) => NaN
|
export const nan: Signature<'nan', number> = (a) => NaN
|
||||||
export const re = (): Signature<'re', number> => (a) => a
|
export const re: Signature<'re', number> = (a) => a
|
||||||
|
|
||||||
$reflect!([zero, one, nan, re])
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "ES2022",
|
||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"outDir": "./build",
|
"outDir": "./obj",
|
||||||
"moduleResolution": "nodenext",
|
"moduleResolution": "nodenext"
|
||||||
"plugins": [ {
|
|
||||||
"transform": "ts-macros/dist/type-resolve",
|
|
||||||
"transformProgram": true
|
|
||||||
} ]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user