MyNixOS website logo
Description

Model and test API surfaces algebraically.

algebra-checkers

Build Status

Dedication

"Any fool can make a rule, and any fool will mind it."

Henry David Thoreau

Overview

algebra-checkers is a little library for testing algebraic laws. For example, imagine we're writing an ADT:

data Foo a
instance Semigroup (Foo a)
instance Monoid (Foo a)

data Key


get :: Key -> Foo a -> a
get = undefined

set :: Key -> a -> Foo a -> Foo a
set = undefined

Let's say we expect the lens laws to hold for get and set, as well for set to be a monoid homomorphism. We can express those facts to algebra-checkers and have it generate tests for us:

lawTests :: [Property]
lawTests = $(testModel [e| do

law "set/set"
    (set i x' (set i x s) == set i x' s)

law "set/get"
    (set i (get i s) s == s)

law "get/set"
    (get i (set i x s) == x)

homo @Monoid
    (\s -> set i x s)

|])

Furthermore, algebra-checkers will generate tests to show that these laws are confluent. We can run these tests via quickCheck lawTests.

If we use the theoremsOf function instead of testModel, algebra-checkers will dump out all the additional theorems it has proven about our algebra. This serves as a good sanity check:

Theorems:

• set i x' (set i x s) = set i x' s (definition of "set/set")
• set i (get i s) s = s (definition of "set/get")
• get i (set i x s) = x (definition of "get/set")
• set i x mempty = mempty (definition of "set:Monoid:mempty")
• set i x (s1 <> s2) = set i x s1 <> set i x s2
    (definition of "set:Monoid:<>")
• set i1 (get i1 (set i1 x1 s1)) s1 = set i1 x1 s1
    (implied by "set/get" and "set/set")
• set i1 (get i1 (s12 <> s22)) s12 <> set i1 (get i1 (s12 <> s22)) s22
        = s12 <> s22
    (implied by "set/get" and "set:Monoid:<>")
• set i1 x'2 (set i1 x1 s11 <> set i1 x1 s21)
        = set i1 x'2 (s11 <> s21)
    (implied by "set/set" and "set:Monoid:<>")
• get i1 (set i1 x1 s11 <> set i1 x1 s21) = x1
    (implied by "get/set" and "set:Monoid:<>")


Contradictions:

• get i1 mempty = x1
    the variable x1 is undetermined
    (implied by "get/set" and "set:Monoid:mempty")

Uh oh! Look at that! This contradiction is clearly a bogus theorem, which lets us know that "get/set" and "set mempty" are nonconfluent with one another!

Metadata

Version

0.1.0.1

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows