Skip to content

Instantly share code, notes, and snippets.

@smatting
Last active January 18, 2019 16:37
Show Gist options
  • Select an option

  • Save smatting/6bcf883fb6b6d55907fd03bd3b17ecea to your computer and use it in GitHub Desktop.

Select an option

Save smatting/6bcf883fb6b6d55907fd03bd3b17ecea to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
-- {-# LANGUAGE FlexibleInstances #-}
-- {-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
-- {-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DefaultSignatures #-}
module Foo.Gen
where
import GHC.Generics
class Encode' f where
encode' :: f p -> [Bool]
instance Encode' V1 where
encode' x = undefined
instance Encode' U1 where
encode' U1 = []
instance (Encode' f, Encode' g) => Encode' (f :+: g) where
encode' (L1 x) = False : encode' x
encode' (R1 x) = True : encode' x
instance (Encode' f, Encode' g) => Encode' (f :*: g) where
encode' (x :*: y) = encode' x ++ encode' y
instance (Encode c) => Encode' (K1 i c) where
encode' (K1 x) = encode x
class Encode a where
encode :: a -> [Bool]
default encode :: (Generic a) => a -> [Bool]
encode x = encode' (from x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment