Learn / Define an operation and check its laws

Example

Declare a bitwise OR operation over non-negative 31-bit integers, implement it in memory, and check its claims:

ts
import { Schema } from 'effect'
import * as Operation from '@doeixd/metakit/operation'
import { checkOperation } from '@doeixd/metakit/conformance'

const Flags = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 2147483647 }))

export const flagsOr = Operation.define({
  id: 'flags.or',
  args: [Flags, Flags],
  returns: Flags,
  claims: { commutative: true, associative: true, idempotent: true, identity: { value: 0, side: 'both' } },
})

export const impls = Operation.implementations(
  'permissions',
  Operation.implement(flagsOr, (a, b) => a | b),
)

const report = checkOperation(flagsOr, impls)

The operation is named flags.or@1: a version is 1 unless you give one, and when you change what an operation means you give it a new name or version: 2, so a stored expression never runs under a meaning it was not written for.

checkOperation samples boundary values, 200 seeded pairs, and 1,000 triples to verify every claim. If you mistakenly claim idempotent: true on XOR, checkOperation fails with the counterexample (flags.xor(5, 5) is 0, not 5).