MyNixOS website logo
Description

Interoperate mutable references with regular lens.

Use mutable references as lens, compose them with other lens, and run them.

You may have heard of lens being described as "functional" or "first-class" references, with some nice properties such as composition. However the equivalence is more than just abstract - mutable references can be readily converted into lens form, interoperable with other pure lens. This makes use of the fact that "mutability" in a high-level language focusing on purity, is really just an alias for a pure state monad that is treated specially by the runtime system. A mutable reference over a value of type a with world state of type s, *is* therefore an actual Lens s s a a. There is no need for handwaving or connotative language, the isomorphism is direct and concrete.

This package demonstrates this isomorphism, converting between the two forms. You then have access to the whole of the lens ecosystem and its compositional properties, and use them with mutable references without other special-cased shims or mutable-specific abstractions.

Uniformity of API is another major benefit - if you want to switch to a different reference type, you only have to change your type definitions and imports, you don't have to change all readSomeVar to readOtherVar, writeSomeVar to writeOtherVar, or was it putOtherVar, and does the atomicModifyVar function take a s -> (a, s) or a s -> (s, a) again?

A word of warning: this package has not been optimised for performance, and currently only supports GHC. We would certainly welcome contributions in these areas, if you are interested in running this code in production. On the other hand for what it's worth, our initial benchmarks suggest that the performance degradation is not signficant - ~13% with -O2 in a program that reads and writes to references and does no other useful work. In a real program that does other useful work, this number will be smaller.

See unit tests for example usage, e.g. MutableTest

Metadata

Version

0.4.1.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • 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