From a19a943f9b2b0f937c6fc6ce309bf425659133c9 Mon Sep 17 00:00:00 2001 From: Joe Hermaszewski Date: Wed, 29 Jun 2016 00:10:09 +0100 Subject: [PATCH] Derive foldable and traversable for more types --- Nix/Expr/Types.hs | 20 ++++++++++++++++---- Nix/Expr/Types/Annotated.hs | 4 +++- hnix.cabal | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Nix/Expr/Types.hs b/Nix/Expr/Types.hs index 891d809..265a198 100644 --- a/Nix/Expr/Types.hs +++ b/Nix/Expr/Types.hs @@ -70,7 +70,7 @@ data NExprF r -- evaluate the second argument. | NAssert !r !r -- ^ Assert that the first returns true before evaluating the second. - deriving (Ord, Eq, Generic, Typeable, Data, Functor, Show) + deriving (Ord, Eq, Generic, Typeable, Data, Functor, Foldable, Traversable, Show) instance Show1 NExprF where showsPrec1 = showsPrec @@ -90,7 +90,7 @@ data Binding r | Inherit !(Maybe r) ![NKeyName r] -- ^ Using a name already in scope, such as @inherit x;@ which is shorthand -- for @x = x;@ or @inherit (x) y;@ which means @y = x.y;@. - deriving (Typeable, Data, Ord, Eq, Functor, Show) + deriving (Typeable, Data, Ord, Eq, Functor, Foldable, Traversable, Show) -- | @Params@ represents all the ways the formal parameters to a -- function can be represented. @@ -119,7 +119,7 @@ data ParamSet r -- | 'Antiquoted' represents an expression that is either -- antiquoted (surrounded by ${...}) or plain (not antiquoted). data Antiquoted v r = Plain !v | Antiquoted !r - deriving (Ord, Eq, Generic, Typeable, Data, Functor, Show) + deriving (Ord, Eq, Generic, Typeable, Data, Functor, Foldable, Traversable, Show) -- | An 'NString' is a list of things that are either a plain string -- or an antiquoted expression. After the antiquotes have been evaluated, @@ -131,7 +131,7 @@ data NString r | Indented ![Antiquoted Text r] -- ^ Strings wrapped with two single quotes ('') can contain newlines, -- and their indentation will be stripped. - deriving (Eq, Ord, Generic, Typeable, Data, Functor, Show) + deriving (Eq, Ord, Generic, Typeable, Data, Functor, Foldable, Traversable, Show) -- | For the the 'IsString' instance, we use a plain doublequoted string. instance IsString (NString r) where @@ -173,6 +173,18 @@ instance Functor NKeyName where fmap f (DynamicKey (Antiquoted e)) = DynamicKey . Antiquoted $ f e fmap _ (StaticKey key) = StaticKey key +instance Foldable NKeyName where + foldMap f = \case + DynamicKey (Plain str) -> foldMap f str + DynamicKey (Antiquoted e) -> f e + StaticKey _ -> mempty + +instance Traversable NKeyName where + sequenceA = \case + DynamicKey (Plain str) -> DynamicKey . Plain <$> sequenceA str + DynamicKey (Antiquoted e) -> DynamicKey . Antiquoted <$> e + StaticKey key -> pure (StaticKey key) + -- | A selector (for example in a @let@ or an attribute set) is made up -- of strung-together key names. type NAttrPath r = [NKeyName r] diff --git a/Nix/Expr/Types/Annotated.hs b/Nix/Expr/Types/Annotated.hs index 468583d..14fa6fe 100644 --- a/Nix/Expr/Types/Annotated.hs +++ b/Nix/Expr/Types/Annotated.hs @@ -1,5 +1,7 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveFunctor #-} +{-# LANGUAGE DeriveFoldable #-} +{-# LANGUAGE DeriveTraversable #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleInstances #-} @@ -38,7 +40,7 @@ data SrcSpan = SrcSpan{ spanBegin :: Delta data Ann ann a = Ann{ annotation :: ann , annotated :: a } - deriving (Ord, Eq, Data, Generic, Typeable, Functor, Read, Show) + deriving (Ord, Eq, Data, Generic, Typeable, Functor, Foldable, Traversable, Read, Show) instance Show ann => Show1 (Ann ann) where showsPrec1 = showsPrec diff --git a/hnix.cabal b/hnix.cabal index d93a205..a895ec0 100644 --- a/hnix.cabal +++ b/hnix.cabal @@ -1,5 +1,5 @@ Name: hnix -Version: 0.3.1 +Version: 0.3.2 Synopsis: Haskell implementation of the Nix language Description: Haskell implementation of the Nix language.