feat: Allow nonrecursive whole-function dependencies #2

Merged
glen merged 2 commits from allow_full_dependency into main 2022-07-19 03:10:56 +00:00
6 changed files with 43 additions and 4 deletions

View File

@ -74,17 +74,38 @@ export default class PocomathInstance {
throw new SyntaxError(`No implementations for ${name}`) throw new SyntaxError(`No implementations for ${name}`)
} }
const tf_imps = {} const tf_imps = {}
// TODO: handle nonempty dependencies
for (const signature in imps) { for (const signature in imps) {
const [deps, imp] = imps[signature] const [deps, imp] = imps[signature]
if (deps.length === 0) { if (deps.length === 0) {
tf_imps[signature] = imp tf_imps[signature] = imp
} else { } else {
throw new Error('Unimplemented') const refs = {}
for (const dep of deps) {
// TODO: handle self dependencies
if (dep.slice(0,4) === 'self') {
throw new Error('self-reference unimplemented')
}
// TODO: handle signature-specific dependencies
if (dep.includes('(')) {
throw new Error('signature specific reference unimplemented')
}
refs[dep] = this._ensureBundle(dep) // just assume acyclic for now
}
tf_imps[signature] = imp(refs)
} }
} }
const tf = typed(name, tf_imps) const tf = typed(name, tf_imps)
this[name] = tf this[name] = tf
return tf return tf
} }
/**
* Ensure that the generated typed function is assigned to the given
* name and return it
*/
_ensureBundle(name) {
const maybe = this[name]
if (typed.isTypedFunction(maybe)) return maybe
return this._bundle(name)
}
} }

3
generic/subtract.mjs Normal file
View File

@ -0,0 +1,3 @@
export const subtract = {
'any,any': [['add', 'negate'], ref => (x,y) => ref.add(x, ref.negate(y))]
}

3
number/all.mjs Normal file
View File

@ -0,0 +1,3 @@
export {add} from './add.mjs'
export {negate} from './negate.mjs'
export {subtract} from '../generic/subtract.mjs'

3
number/negate.mjs Normal file
View File

@ -0,0 +1,3 @@
export const negate = {
number: [[], n => -n]
}

View File

@ -1,8 +1,8 @@
/* Core of pocomath: generates the default instance */ /* Core of pocomath: generates the default instance */
import PocomathInstance from './PocomathInstance.mjs' import PocomathInstance from './PocomathInstance.mjs'
import * as numberAdd from './number/add.mjs' import * as numbers from './number/all.mjs'
const math = new PocomathInstance('math') const math = new PocomathInstance('math')
math.install(numberAdd) math.install(numbers)
export default math export default math

View File

@ -2,9 +2,18 @@ import assert from 'assert'
import math from '../pocomath.mjs' import math from '../pocomath.mjs'
describe('The default full pocomath instance "math"', () => { describe('The default full pocomath instance "math"', () => {
it('can subtract numbers', () => {
assert.strictEqual(math.subtract(12, 5), 7)
})
it('can add numbers', () => { it('can add numbers', () => {
assert.strictEqual(math.add(3, 4), 7) assert.strictEqual(math.add(3, 4), 7)
assert.strictEqual(math.add(1.5, 2.5, 3.5), 7.5) assert.strictEqual(math.add(1.5, 2.5, 3.5), 7.5)
assert.strictEqual(math.add(Infinity), Infinity) assert.strictEqual(math.add(Infinity), Infinity)
}) })
it('can negate numbers', () => {
assert.strictEqual(math.negate(-1), 1)
assert.strictEqual(math.add(10, math.negate(3)), 7)
})
}) })