feat: respect return typing strategy for all generic functions

This commit is contained in:
Glen Whitney 2025-04-26 23:11:19 -07:00
parent c42249e561
commit 793dd361a6
5 changed files with 20 additions and 11 deletions

View file

@ -1,5 +1,5 @@
import {Complex} from './Complex.js' import {Complex} from './Complex.js'
import {OneOf, Returns, ReturnTyping} from '#core/Type.js' import {ReturnTyping} from '#core/Type.js'
import {match} from '#core/TypePatterns.js' import {match} from '#core/TypePatterns.js'
import {ReturnsAs} from '#generic/helpers.js' import {ReturnsAs} from '#generic/helpers.js'

View file

@ -35,7 +35,7 @@ export const arg = match(
/* Returns true if w is z multiplied by a complex unit */ /* Returns true if w is z multiplied by a complex unit */
export const associate = match( export const associate = match(
[Complex, Complex], [Complex, Complex],
(math, [W, Z], strategy) => { (math, [W, Z]) => {
if (Z.Component.complex) { if (Z.Component.complex) {
throw new Error( throw new Error(
`The group of units of type ${Z} is not yet implemented`) `The group of units of type ${Z} is not yet implemented`)

View file

@ -1,11 +1,18 @@
import assert from 'assert' import assert from 'assert'
import math from '#nanomath' import math from '#nanomath'
import {ReturnTyping} from '#core/Type.js'
const {Complex, NumberT} = math.types
describe('generic arithmetic', () => { describe('generic arithmetic', () => {
it('squares anything', () => { it('squares anything', () => {
assert.strictEqual(math.square(7), 49) const sq = math.square
assert.strictEqual( assert.strictEqual(sq(7), 49)
math.square.resolve([math.types.NumberT]).returns, assert.strictEqual(math.square.resolve([NumberT]).returns, NumberT)
math.types.NumberT) assert.deepStrictEqual(sq(math.complex(3, 4)), math.complex(-7, 24))
const eyes = math.complex(0, 2)
assert.strictEqual(sq(eyes), -4)
const sqFull = math.square.resolve(Complex(NumberT), ReturnTyping.full)
assert.deepStrictEqual(sqFull(eyes), math.complex(-4, 0))
}) })
}) })

View file

@ -2,7 +2,7 @@ import {Returns} from '#core/Type.js'
import {match, Any} from '#core/TypePatterns.js' import {match, Any} from '#core/TypePatterns.js'
export const conj = match(Any, (_math, T) => Returns(T, a => a)) export const conj = match(Any, (_math, T) => Returns(T, a => a))
export const square = match(Any, (math, T) => { export const square = match(Any, (math, T, strategy) => {
const mult = math.multiply.resolve([T, T]) const mult = math.multiply.resolve([T, T], strategy)
return Returns(mult.returns, a => mult(a, a)) return Returns(mult.returns, a => mult(a, a))
}) })

View file

@ -1,8 +1,10 @@
import {ReturnsAs} from './helpers.js' import {ReturnsAs} from './helpers.js'
import {Returns} from '#core/Type.js' import {Returns, ReturnTyping} from '#core/Type.js'
import {Any, Passthru, match, matched} from '#core/TypePatterns.js' import {Any, Passthru, match, matched} from '#core/TypePatterns.js'
import {boolnum} from '#number/helpers.js' import {boolnum} from '#number/helpers.js'
const {full} = ReturnTyping
export const equal = match([Any, Any], (math, [T, U]) => { export const equal = match([Any, Any], (math, [T, U]) => {
// Finding the correct signature of `indistinguishable` to use for // Finding the correct signature of `indistinguishable` to use for
// testing (approximate) equality is tricky, because T or U might // testing (approximate) equality is tricky, because T or U might
@ -12,7 +14,7 @@ export const equal = match([Any, Any], (math, [T, U]) => {
// the matching type, and then we look up with tolerances. // the matching type, and then we look up with tolerances.
let exactChecker let exactChecker
try { try {
exactChecker = math.indistinguishable.resolve([T, U]) exactChecker = math.indistinguishable.resolve([T, U], full)
} catch { // can't compare, so no way they can be equal } catch { // can't compare, so no way they can be equal
return boolnum(() => false)(math) return boolnum(() => false)(math)
} }
@ -25,7 +27,7 @@ export const equal = match([Any, Any], (math, [T, U]) => {
const {relTol, absTol} = typeConfig const {relTol, absTol} = typeConfig
const RT = math.typeOf(relTol) const RT = math.typeOf(relTol)
const AT = math.typeOf(absTol) const AT = math.typeOf(absTol)
const approx = math.indistinguishable.resolve([T, U, RT, AT]) const approx = math.indistinguishable.resolve([T, U, RT, AT], full)
return ReturnsAs( return ReturnsAs(
approx, (t, u) => approx(t, u, relTol, absTol)) approx, (t, u) => approx(t, u, relTol, absTol))
} catch {} // fall through to case with no tolerances } catch {} // fall through to case with no tolerances