Skip to content

Instantly share code, notes, and snippets.

@kosmikus
Created October 8, 2015 07:07
Show Gist options
  • Save kosmikus/a38115737fc482ba2a9f to your computer and use it in GitHub Desktop.
Save kosmikus/a38115737fc482ba2a9f to your computer and use it in GitHub Desktop.
{-# 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
hhead (I x :* _) = K x
firstField :: forall a x . (Generic a, All (HasHead x) (Code a)) => a -> x
firstField x = unI (hcollapse (hcliftA p hhead (unSOP (from x))))
where
p = Proxy :: Proxy (HasHead x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment