MyNixOS website logo
Description

applicative (functional) bidirectional programming beyond composition chains.

A bidirectional transformation connects data in difference formats, maintaining consistency amid separate updates. The "lens" programming language---with Kmett's Haskell lens package being one of the most influentials---is a solution to this problem.

Many lens implementations (including Kmett's Haskell library) only support the point-free style of programming. Though concise at times, this style becomes less handy when programs get more complicated.

This module provides the infrastructure for programming complex bidirectional transformations, by representing lenses as functions that are subject to the normal applicative-style programming. For example, let us consider the unlines functions and to define a lens version of it. In our framework we can program through pattern matching and explicit recursion as in normal functional programming.

unlinesF :: [L s String] -> L s String
unlinesF []     = new ""
unlinesF (x:xs) = catLineF x (unlinesF xs)
   where catLineF = lift2 catLineL

Here, lift2 :: Lens' (a,b) c -> (forall s. L s a -> L s b -> L s c) and new :: a -> (forall s. L s a) lift lenses to functions. The former is for binary lenses and the latter is for constant lenses. We can then apply lenses as functions, alleviating the need of specialized combinators. In the above, we omitted the definition of a primitive lens catLineL :: Lens' (String, String) String that concatenates two strings with a newline in between.

Simply unlifting (unlift, unlift2, unliftT) such "lens functions" gives us the desired lenses.

unlinesL :: Lens' [String] String
unlinesL = unliftT unlinesF

The obtained lens works as expected.

>>> ["banana", "orange", "apple"] ^. unlinesL
"banana\norange\napple\n"
>>> ["banana", "orange", "apple"] & unlinesL .~ "Banana\nOrange\nApple\n"
["Banana","Orange","Apple"]

One may prefer to define unlinesF with foldr. Indeed, we can use foldr as below because catLineF and unlinesF are simply Haskell functions.

unlinesF = foldr (lift2 catLineL) (new "")

Here, the program is written in a point-free manner similar to that of the other lens frameworks. But note that this foldr is just Haskell's foldr, instead of a special combinator for lenses.

More examples can be found at "Examples" in the source code https://bitbucket.org/kztk/app-lens/downloads.

Remark. The applicative-style programming is possible in our implementation because a function representation different from Kmett's is used for lenses. As a result, when we program record-field access chains such as

src .^ l1 . l2
src & l1 . l2 .~ tgt'

The order of composition is inverted in our implementation.

src .^ unlift (lift l2 . lift l1)
src & unlift (lift l2 . lift l1) .~ tgt'

This difference causes slight inconvenience for record updates, but is crucial in allowing the applicative-style lens programming we aim for.

Metadata

Version

0.1.0.3

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