Skip to content

Instantly share code, notes, and snippets.

@nmattia
Last active June 26, 2018 13:04
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 nmattia/304a4b98b9ffbce8c2a7aa3e412100f6 to your computer and use it in GitHub Desktop.
Save nmattia/304a4b98b9ffbce8c2a7aa3e412100f6 to your computer and use it in GitHub Desktop.
Config with generics-sop
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
module Foo where
import Control.Monad
import qualified GHC.Generics as GHC
import qualified Generics.SOP as SOP
import qualified Generics.SOP.NS as NS
import Data.Functor.Identity
type AppSettings = AppSettings_ Identity
-- Squash Identity
type family SQ f t where
SQ Identity t = t
SQ f t = f t
data AppSettings_ f = AppSettings
{ port :: SQ f Int
, host :: SQ f String
} deriving GHC.Generic
instance SOP.Generic (AppSettings_ f)
as :: AppSettings_ Maybe
as = AppSettings (Just 2) (Just "foo")
fff :: AppSettings_ Maybe -> Maybe AppSettings
fff = fmap SOP.to . NS.sequence'_SOP . NS.fromI_SOP . SOP.from
baz2 :: Maybe AppSettings
baz2 = fff as
maybePort :: Maybe Int
maybePort = port <$> baz2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment