MyNixOS website logo
Description

Generate a special f-algebra combinator from any data type.

This library provides a function to generate a special f-algebra combinator from any data type (GADTs are not currently supported). This was inspired by the recursion-schemes library where they have a function to automagically generate a base functor. Although, this new base functor data type has custom constructors and to define the *-morphism algebras turns into boring pattern matching. So, this library provides a function called makeCombinator that produces a nice combinator to deal with data types as they were defined in terms of Pairs ( (,) ) and Sums (Either). With this nice combinator we are able to view a data type as its equivalent categorical isomorphism and manipulate it with an interface similar as the either function provided from base.

F-Algebra Data Combinator Generator

Generate an f-algebra combinator from any data type.

https://hackage.haskell.org/package/f-algebra-gen-0.1.0.0

Description

This library provides a function to generate a special f-algebra combinator from any data type (GADTs are not currently supported).

This was inspired by the recursion-schemes library where they have a function to automagically generate a base functor. Although, this new base functor data type has custom constructors and to define the *-morphism algebras turns into boring pattern matching.

So, this library provides a function called makeCombinator that produces a nice combinator to deal with data types as they were defined in terms of Pairs ((,)) and Sums (Either). With this nice combinator we are able to view a data type as its equivalent categorical isomorphism and manipulate it with an interface similar as the either function provided from base.

Example

To create this special combinator you just need to call makeCombinator ''<data type name> as in the example below:


-- List type
data List a = Nil | List a (List a)

makeBaseFunctor ''List

makeCombinator ''ListF

This example will generate the following code:

makeCombinator ''ListF
  ======>
    listf f_acw7 f_acw8 Nil = f_acw7 ()
    listf f_acw7 f_acw8 (Cons a_acw9 a_acwa) = f_acw8 (a_acw9, a_acwa)

As you can see it's pretty close as to have the type defined as the set of sums and pairs data List a = Either () (a, List a), which we could then use either function as well as other convinent (,) combinators.

An important note is that the generated function has always the same name as the data type but in low characters and the order of the functions to be applied to the type constructors it's the same order which they were declared.

A simple example on how we can beneficiate from using this special combinator when defining catamorphisms using recursion-schemes:

  • Without the combinator:

    length :: [a] -> Int
    length = cata gene
      where
        gene Nil = 0
        gene (Cons a x) = x + 1
    
  • With the combinator:

    makeCombinator'' ListF
    
    length :: [a] -> Int
    length = cata (listf (const 0) (succ . snd))
    

I recognize that for such a simple data type and catamorphism it's hard to see any gain in readability/implementation. But with this special combinator it's a lot easier to go from paper to code as it's almost a direct translation.

There's a fully working example in the examples folder that uses the recursion-schemes library as well as a nice small program calculus (AoP inspired) combinators library to show how simple and straightforward it is to use it with this new combinator.

Metadata

Version

0.1.0.2

License

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