Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created December 7, 2018 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danidiaz/e08f64d4e3930e706bf874b607ac6537 to your computer and use it in GitHub Desktop.
Save danidiaz/e08f64d4e3930e706bf874b607ac6537 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE UndecidableInstances #-}
module Main where
import Data.Aeson
import Data.Proxy
import qualified GHC.Generics as GHC
import GHC.TypeLits
import Generics.SOP
import Generics.SOP.NP
data Presence = Present
| Absent
type family Encode p v :: * where
Encode Present v = v
Encode Absent v = ()
type family Mentioned (ns :: [Symbol]) (n :: Symbol) :: Presence where
Mentioned '[] _ = Absent
Mentioned (n ': _) n = Present
Mentioned (_ ': ns) n = Mentioned ns n
data Foo (ns :: [Symbol]) = Foo {
field1 :: Encode (Mentioned ns "field1") Int,
field2 :: Encode (Mentioned ns "field2") Bool,
field3 :: Encode (Mentioned ns "field3") Char
} deriving GHC.Generic
instance Generic (Foo ns) -- not used right now
instance (FromJSON (Encode (Mentioned ns "field1") Int),
FromJSON (Encode (Mentioned ns "field2") Bool),
FromJSON (Encode (Mentioned ns "field3") Char)) => FromJSON (Foo ns)
class Demote (ns :: [Symbol]) where
demote :: Proxy ns -> [String]
instance Demote '[] where
demote _ = []
instance (KnownSymbol n, Demote ns) => Demote (n ': ns) where
demote _ = symbolVal (Proxy @n) : demote (Proxy @ns)
main :: IO ()
main = putStrLn "Hello, Haskell!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment