feat: Template implementations
This commit is contained in:
parent
1076c3c727
commit
fd3d6b2eb3
9 changed files with 67 additions and 23 deletions
|
@ -1,5 +1,3 @@
|
|||
export * from './Types/bigint.mjs'
|
||||
|
||||
export const add = {
|
||||
'...bigint': () => addends => addends.reduce((x,y) => x+y, 0n)
|
||||
}
|
||||
export const add = {'bigint,bigint': () => (a,b) => a+b}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
export * from './Types/Complex.mjs'
|
||||
|
||||
export const add = {
|
||||
'...Complex': ({self}) => addends => {
|
||||
if (addends.length === 0) return {re:0, im:0}
|
||||
const seed = addends.shift()
|
||||
return addends.reduce(
|
||||
(w,z) => ({re: self(w.re, z.re), im: self(w.im, z.im)}), seed)
|
||||
}
|
||||
'Complex,number': ({
|
||||
'self(number,number)': addNum,
|
||||
'complex(any,any)': cplx
|
||||
}) => (z,x) => cplx(addNum(z.re, x), z.im),
|
||||
'Complex,Complex': ({
|
||||
self,
|
||||
'complex(any,any)': cplx
|
||||
}) => (w,z) => cplx(self(w.re, z.re), self(w.im, z.im))
|
||||
}
|
||||
|
|
|
@ -443,7 +443,26 @@ export default class PocomathInstance {
|
|||
for (const instType of behavior.instantiations) {
|
||||
const signature =
|
||||
substituteInSig(rawSignature, theTemplateParam, instType)
|
||||
this._addTFimplementation(tf_imps, signature, behavior, instType)
|
||||
const uses = new Set()
|
||||
for (const dep of behavior.uses) {
|
||||
if (this._templateParam(dep)) continue
|
||||
uses.add(substituteInSig(dep, theTemplateParam, instType))
|
||||
}
|
||||
const patch = (refs) => {
|
||||
const innerRefs = {}
|
||||
for (const dep of behavior.uses) {
|
||||
if (this._templateParam(dep)) {
|
||||
innerRefs[dep] = instType
|
||||
} else {
|
||||
const outerName = substituteInSig(
|
||||
dep, theTemplateParam, instType)
|
||||
innerRefs[dep] = refs[outerName]
|
||||
}
|
||||
}
|
||||
const original = behavior.does(innerRefs)
|
||||
return behavior.does(innerRefs)
|
||||
}
|
||||
this._addTFimplementation(tf_imps, signature, {uses, does: patch})
|
||||
}
|
||||
/* Now add the catchall signature */
|
||||
const signature = substituteInSig(rawSignature, theTemplateParam, 'any')
|
||||
|
@ -464,13 +483,21 @@ export default class PocomathInstance {
|
|||
`Cannot find template parameter in ${rawSignature}`)
|
||||
}
|
||||
const self = this
|
||||
const patch = (refs) => {
|
||||
const original = behavior.does(refs)
|
||||
return (...args) => {
|
||||
const example = args[exemplar]
|
||||
console.log('Have to match template to', example)
|
||||
return original(...args)
|
||||
const patch = (refs) => (...args) => {
|
||||
const example = args[exemplar]
|
||||
const instantiateFor = self.typeOf(example)
|
||||
refs[theTemplateParam] = instantiateFor
|
||||
const instCount = behavior.instantiations.size
|
||||
for (const earlier of self._priorTypes[instantiateFor]) {
|
||||
behavior.instantiations.add(earlier)
|
||||
}
|
||||
behavior.instantiations.add(instantiateFor)
|
||||
if (behavior.instantiations.size > instCount) {
|
||||
self._invalidate(name)
|
||||
}
|
||||
// And for now, we have to rely on the "any" implementation. Hope
|
||||
// it matches the instantiated one!
|
||||
return behavior.does(refs)(...args)
|
||||
}
|
||||
this._addTFimplementation(
|
||||
tf_imps, signature, {uses: behavior.uses, does: patch})
|
||||
|
@ -484,7 +511,7 @@ export default class PocomathInstance {
|
|||
* to typed-function implementations and inserts the result into plain object
|
||||
* imps
|
||||
*/
|
||||
_addTFimplementation(imps, signature, behavior, instType) {
|
||||
_addTFimplementation(imps, signature, behavior) {
|
||||
const {uses, does} = behavior
|
||||
if (uses.length === 0) {
|
||||
imps[signature] = does()
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import {reducingOperation} from './reducingOperation.mjs'
|
||||
|
||||
export * from './Types/generic.mjs'
|
||||
|
||||
export const add = reducingOperation
|
||||
export {lcm} from './lcm.mjs'
|
||||
export {mod} from './mod.mjs'
|
||||
export {multiply} from './multiply.mjs'
|
||||
export const multiply = reducingOperation
|
||||
export {divide} from './divide.mjs'
|
||||
export {sign} from './sign.mjs'
|
||||
export {sqrt} from './sqrt.mjs'
|
||||
|
|
12
src/generic/reducingOperation.mjs
Normal file
12
src/generic/reducingOperation.mjs
Normal file
|
@ -0,0 +1,12 @@
|
|||
export * from './Types/generic.mjs'
|
||||
|
||||
export const reducingOperation = {
|
||||
'undefined': () => u => u,
|
||||
'undefined,...any': () => (u, rest) => u,
|
||||
'any,undefined': () => (x, u) => u,
|
||||
any: () => x => x,
|
||||
'any,any,...any': ({
|
||||
self
|
||||
}) => (a,b,rest) => [b, ...rest].reduce((x,y) => self(x,y), a)
|
||||
}
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
export const subtract = {
|
||||
'T,T': ({'add(T)': addT, 'negate(T)': negT}) => (x,y) => addT(x, negT(y))
|
||||
'T,T': ({'add(T,T)': addT, 'negate(T)': negT}) => (x,y) => addT(x, negT(y))
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
export * from './Types/number.mjs'
|
||||
|
||||
export const add = {
|
||||
'...number': () => addends => addends.reduce((x,y) => x+y, 0),
|
||||
}
|
||||
export const add = {'number,number': () => (m,n) => m+n}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue