Skip to content
Describe types as configurable and then write specializers all in one place
Haskell
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/Control
.gitignore
Configurable.cabal
LICENSE
README.md
Setup.hs

README.md

Control.Configurable

Declare types as Configurable and then specialize them all in one place. For example

data A = A VarA deriving (Show)
data B = B VarB deriving (Show)

data VarA = VarA Int deriving (Typeable, Show)
data VarB = VarB Int deriving (Typeable, Show)

instance Monoid VarA where
  mempty      = VarA 0
  mappend v _ = v

instance Monoid VarB where
  mempty      = VarB 0
  mappend v _ = v

instance Configurable VarA
instance Configurable VarB

instance ConfiguredBy A VarA
instance ConfiguredBy B VarB

c = unsafeDoConfig [confs (undefined :: A),
                    confs (undefined :: B)]
    $ toConfigure (\(VarA n) -> VarA (n+1))
    . toConfigure (\(VarB n) -> VarB (n-1))

fmap A (getConfig c)
> Just (A (VarA 1))
fmap B (getConfig c)
> Just (A (VarA (-1)))
Something went wrong with that request. Please try again.