Skip to content

Instantly share code, notes, and snippets.

@kosmikus
kosmikus / FoldMutRec.hs
Last active October 13, 2015 11:02
Two examples of Foldable for mutually recursive datatypes
module FoldMutRec where
-- Note: DeriveFoldable could be used, but I am providing
-- hand-written instances to better illustrate the mechanics.
import Data.Monoid
data Forest a = Forest [Tree a]
data Tree a = Node a (Forest a)
{-# LANGUAGE DataKinds, TypeOperators, GADTs, TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module FirstField where
import Generics.SOP
class HasHead (y :: *) (xs :: [*]) where
hhead :: NP I xs -> K y xs
instance (x ~ y) => HasHead y (x ': xs) where