Type level record catenation in Haskell

This Haskell code shows a way to catenate records such that their types are also catenated (list-style), creating new types on the fly. It uses comparatively few post-Haskell98 language extensions. You can can load it in ghci and examine the types of things like cat a b.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}

data Empty = Empty
  deriving Show

data Cons a b = Cons a b
  deriving Show

class Catty a b c | a b -> c where
  cat :: a -> b -> c

instance Catty Empty Empty Empty where
  cat Empty Empty = Empty

instance Catty Empty b c => Catty Empty (Cons d b) (Cons d c) where
  cat Empty (Cons d b) = Cons d (cat Empty b)

instance Catty a b c => Catty (Cons d a) b (Cons d c) where
  cat (Cons k a) b = Cons k (cat a b)

data A = A deriving Show
data B = B deriving Show
data C = C deriving Show
data D = D deriving Show

a = Cons A $ Cons B $ Cons C $ Empty
b = Cons D $ Cons A $ Empty
c = Cons B $ Cons C $ Cons D $ Cons A $ Empty

Created: 2015-01-11.

Last updated: 2017-04-05.

Back to the landing page