feat(return types): Add more return types for complex functions

These changes greatly increased the need for precision in generating
  implementations for signatures of operations whenever possible. So this
  commit also includes a refactor that basically caches all of the
  conversions of Pocomath implementations to typed-function implementatios so
  that they are more often externally available (i.e., not disrupted so much
  after invalidation).
This commit is contained in:
Glen Whitney 2022-08-26 23:54:32 -04:00
parent bc434c7163
commit a2f76a55b8
13 changed files with 273 additions and 98 deletions

View file

@ -1,10 +1,25 @@
import {Returns, returnTypeOf} from '../core/Returns.mjs'
export * from './Types/Complex.mjs'
export const abs = {
'Complex<T>': ({
sqrt, // Calculation of the type needed in the square root (the
// underlying numeric type of T, whatever T is, is beyond Pocomath's
// (current) template abilities, so punt and just do full resolution
sqrt, // Unfortunately no notation yet for the needed signature
'absquare(T)': baseabsq,
'absquare(Complex<T>)': absq
}) => z => sqrt(absq(z))
}) => {
const pm = sqrt.fromInstance
if (typeof pm === 'undefined') {
// Just checking for the dependencies, return value is irrelevant
return undefined
}
const midType = returnTypeOf(baseabsq)
const sqrtImp = pm.resolve('sqrt', midType, sqrt)
let retType = returnTypeOf(sqrtImp)
if (retType.includes('|')) {
// This is a bit of a hack, as it relies on all implementations of
// sqrt returning the "typical" return type as the first option
retType = retType.split('|',1)[0]
}
return Returns(retType, z => sqrtImp(absq(z)))
}
}