kojima/tests/units/monad.js
2025-04-10 04:37:45 -05:00

68 lines
1.7 KiB
JavaScript

// @ts-nocheck
import { it, assert } from 'folktest'
import { Option, Result } from '../../src/index.js'
import { Chain, Ord, Setoid } from '../../src/algebra/interfaces.js'
const Algebra = [Option, Result]
const inc = x => F.of(x + 1)
const dbl = x => F.of(x * 2)
const impl = i => m => i.implementedBy(m)
export const Tests = [
it('unit is a left identity', () => {
console.log(Chain.implementedBy(Result))
Algebra.filter(impl(Chain)).forEach(algebra => {
assert(
algebra.of(1).chain(inc) == inc(1),
`${algebra.name} is not a left identity`
)
})
}),
it('unit is a right identity', () => {
Algebra.filter(impl(Chain)).forEach(algebra => {
const m = algebra.of(1)
assert(
m.chain(algebra.of).equals(m),
`${algebra.name} is not a right identity`
)
})
}),
it('unit is associative', () => {
Algebra.filter(impl(Chain)).forEach(algebra => {
const a = algebra.of(1)
const x = a.chain(inc).chain(dbl)
const y = a.chain(x => inc(x).chain(dbl))
assert(x == y, `${algebra.name} is not associative`)
})
}),
it('unit is reflexive', () => {
Algebra.filter(impl(Setoid)).forEach(algebra => {
const a = algebra.of(1)
assert(a.equals(a), `${algebra.name} is not reflexive`)
})
}),
it('unit is symmetrical', () => {
Algebra.filter(impl(Setoid)).forEach(algebra => {
const a = algebra.of(1)
const b = algebra.of(1)
assert(a.equals(b) && b.equals(a), `${algebra.name} is not symmetrical`)
})
}),
it('unit is antisymmetrical', () => {
Algebra.filter(impl(Ord)).forEach(algebra => {
const a = algebra.of(1)
const b = algebra.of(1)
assert(a.lte(b) && b.lte(a) && a.equals(b))
})
})
]