packages feed

uniplate 1.6.12 → 1.6.13

raw patch · 23 files changed

+369/−548 lines, 23 filesdep +ghc-primdep ~base

Dependencies added: ghc-prim

Dependency ranges changed: base

Files

+ CHANGES.txt view
@@ -0,0 +1,57 @@+Changelog for Uniplate++1.6.13, released 2020-11-07+    Remove support from pre-GHC 8.0+    #31, GHC 9.0 compatibility+    Change descendM to be applicative, not monadic+1.6.12, released 2013-10-26+    Allow compilation with clang+1.6.11, released 2013-08-14+    Work around more excessive strictness, gives 10x speed improvement+1.6.10, released 2012-12-14+    Allow hashable-1.2+    Work around excessive strictness in unordered-containers-0.2.3.0+1.6.9, released 2012-12-08+    Remove dependencies on an old internal module+    More performance work (speculative)+1.6.8, released 2012-12-05+    Significant speed up to default descend/descendM versions+    Implement faster descendM/descendBiM for the Data version+    Add RULES for the Direct method which follow plate identities+    Disallow unordered-containers 0.2.0.*+1.6.7, released 2012-03-08+    Allow unordered-containers 0.2.*+1.6.6, released 2012-02-15+    Require hashable-1.1.2.3, which has a TypeRep instance+1.6.5, released 2011-11-05+    Add more instances for the Data.Instances, such as Ord/Eq+1.6.4, released 2011-11-05+    Give better Data instances for the containers package+1.6.3, released 2011-10-11+    #454, use unordered-containers on GHC 7.2 and above (faster)+1.6.2, released 2011-08-16+    Given a Map/Set (or anything with NorepType) ignore it+    Add $UNIPLATE_VERBOSE to give messages about cache construction+1.6.1, released 2011-08-11+    #435, mark things that were recommended not to use as deprecated+    #449, GHC 7.2 compatibility+1.6, released 2010-11-10+    GHC 7 compatibility+    Eliminate mtl dependency+    Add transformer/transformBis, along with better documentation+    #364, add a zipper in Data.Generics.Uniplate.Zipper+    Add an Eq instance for Str+1.5.1, released 2010-01-24+    Fix a typo in the synopsis+1.5, released 2010-01-23+    Massive speed improvements to Data loading initial cache+1.4, released 2010-01-12+    Add back performance enhancement for Rationals+1.3, released 2010-01-03+    Rewrite, simplify, roll out Data.Generics.Uniplate.*+1.2, released 2008-07-08+    Allow Data operations to work on Rational+1.1, not on Hackage+    Add versions based on Str+1.0, released 2007-06-13+    Initial release
Data/Generics/Biplate.hs view
@@ -6,15 +6,15 @@     Requires multi-parameter type classes, so is no longer Haskell 98. These operations     are easier to use and construct than the equivalent "Data.Generics.UniplateStrOn" methods,     but perform the same operation.-    +     It is recommended that instead of importing this module, you import one of the following     modules, to construct instances:-    +     * "Data.Generics.PlateDirect" - does not require overlapping instances, highest performance     but requires /O(n^2)/ instances in the worst case.-    +     * "Data.Generics.PlateTypeable" - requires the "Data.Typeable" class for all data structures.-    +     * "Data.Generics.PlateData" - requires "Data.Generics" and the 'Data' class, which is only     available on GHC, but automatically infers instances. -}
Data/Generics/Compos.hs view
@@ -16,7 +16,6 @@ module Data.Generics.Compos where  import Control.Monad-import Data.Monoid import Data.Generics.Uniplate.Operations  @@ -30,7 +29,7 @@   -- | @composOpM == 'descendM'@-composOpM :: (Uniplate a, Monad m) => (a -> m a) -> a -> m a+composOpM :: (Uniplate a, Applicative m) => (a -> m a) -> a -> m a composOpM = descendM  
Data/Generics/PlateData.hs view
@@ -1,4 +1,5 @@-{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses, UndecidableInstances, ExistentialQuantification, Rank2Types, CPP #-}+{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses, UndecidableInstances, ExistentialQuantification, Rank2Types #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     /DEPRECATED/: Use "Data.Generics.Uniplate.Data" instead.@@ -16,20 +17,10 @@ import Data.Generics.Uniplate.Internal.Utils import Data.Generics -#if !(__GLASGOW_HASKELL__ < 606 || __GLASGOW_HASKELL__ >= 702)-import Data.List-import qualified Data.IntSet as IntSet-import Data.Ratio-#endif ---- | An existential box representing a type which supports SYB--- operations.-data DataBox = forall a . (Typeable a, Data a) => DataBox a- data Box find = Box {fromBox :: forall a . Typeable a => a -> Answer find} -data Answer a = Hit {fromHit :: a} -- you just hit the element you were after (here is a cast)+data Answer a = Hit {_fromHit :: a} -- you just hit the element you were after (here is a cast)               | Follow -- go forward, you will find something               | Miss -- you failed to sink my battleship! @@ -39,7 +30,6 @@                  start -> find ->                  Box find -#if __GLASGOW_HASKELL__ < 606 || __GLASGOW_HASKELL__ >= 702 -- GHC 6.4.2 does not export typeRepKey, so we can't do the trick -- as efficiently, so we just give up and revert to always following @@ -48,56 +38,6 @@         query a = case cast a of                        Just y -> Hit y                        Nothing -> Follow--#else--- GHC 6.6 does contain typeRepKey, so only follow when appropriate--containsMatch start find = Box query-    where-        typeInt x = inlinePerformIO $ typeRepKey x-    -        query :: Typeable a => a -> Answer find-        query a = if tifind == tia then Hit (unsafeCoerce a)-                  else if tia `IntSet.member` timatch then Follow else Miss-            where tia = typeInt $ typeOf a-    -        tifind = typeInt tfind-        timatch = IntSet.fromList $ map typeInt tmatch--        tfind = typeOf find-        tmatch = f [tfind] (filter ((/=) tfind . fst) $ containsList start)--        f want have = if null want2 then [] else want2 ++ f want2 no-            where-                want2 = map fst yes-                (yes,no) = partition (not . null . intersect want . snd) have--containsList :: (Data a, Typeable a) => a -> [(TypeRep, [TypeRep])]-containsList x = f [] [DataBox x]-    where-        f done [] = []-        f done (DataBox t:odo)-            | tt `elem` done = f done odo-            | otherwise = (tt,map (\(DataBox a) -> typeOf a) xs) : f (tt:done) (xs++odo)-            where-                tt = typeOf t-                xs = contains t----- Ratio is strict and causes bugs with fromConstr in GHC 6.10.1--- See bug http://hackage.haskell.org/trac/ghc/ticket/2782-evilRatio = fst $ splitTyConApp $ typeOf (undefined :: Ratio Int) --contains :: (Data a, Typeable a) => a -> [DataBox]-contains x | fst (splitTyConApp $ typeOf x) == evilRatio = []-           | isAlgType dtyp = concatMap f ctrs-           | otherwise = []-    where-        f ctr = gmapQ DataBox (asTypeOf (fromConstr ctr) x)-        ctrs = dataTypeConstrs dtyp-        dtyp = dataTypeOf x--#endif   instance (Data a, Typeable a) => Uniplate a where
Data/Generics/PlateDirect.hs view
@@ -6,9 +6,9 @@     This module supplies a method for writing 'Biplate' instances more easily.     This module requires fewest extensions, highest performance, and most instance     definitions.-    +     To take an example:-    +     > data Expr = Var Int | Pos Expr String | Neg Expr | Add Expr Expr     > data Stmt = Seq [Stmt] | Sel [Expr] | Let String Expr     >@@ -34,7 +34,7 @@     >     biplate (Sel x  ) = plate Sel ||* x     >     biplate (Let x y) = plate Let |-  x |* y -}-    +  module Data.Generics.PlateDirect     {-# DEPRECATED "Use Data.Generics.Uniplate.Direct instead" #-}
Data/Generics/PlateTypeable.hs view
@@ -1,12 +1,13 @@ {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     /DEPRECATED/: Use "Data.Generics.Uniplate.Typeable" instead.      This module supplies a method for writing 'Biplate' instances more easily.-    +     To take an example:-    +     > data Expr = Var Int | Neg Expr | Add Expr Expr     >     > instance Typeable Expr where ...@@ -153,4 +154,3 @@          ,Typeable to, Uniplate to) =>          PlateAll (a,b,c,d,e) to where     plateAll (a,b,c,d,e) = plate (,,,,) |+ a |+ b |+ c |+ d |+ e-
Data/Generics/SYB.hs view
@@ -18,6 +18,7 @@  import Data.Generics.Uniplate.Operations + -- | @gmapT == 'descend'@ gmapT :: Uniplate a => (a -> a) -> a -> a gmapT = descend@@ -44,7 +45,7 @@   -- | @gmapM == 'descendM'@-gmapM :: (Uniplate a, Monad m) => (a -> m a) -> a -> m a+gmapM :: (Uniplate a, Applicative m) => (a -> m a) -> a -> m a gmapM = descendM  @@ -60,12 +61,12 @@   -- | @mkM == id@-mkM :: Monad m => (a -> m a) -> a -> m a+mkM :: (a -> m a) -> a -> m a mkM = id   -- | @everywhereM == 'transformBiM'@-everywhereM :: (Biplate b a, Monad m) => (a -> m a) -> b -> m b+everywhereM :: (Biplate b a, Monad m, Applicative m) => (a -> m a) -> b -> m b everywhereM = transformBiM  
Data/Generics/Str.hs view
@@ -9,13 +9,6 @@  import Data.Generics.Uniplate.Internal.Utils -import Control.Applicative-import Control.Monad-import Data.Foldable-import Data.Monoid-import Data.Traversable-- -- * The Data Type  data Str a = Zero | One a | Two (Str a) (Str a)@@ -39,12 +32,12 @@   {-# INLINE strMapM #-}-strMapM :: Monad m => (a -> m b) -> Str a -> m (Str b)+strMapM :: Applicative m => (a -> m b) -> Str a -> m (Str b) strMapM f x = g SPEC x     where-        g !spec Zero = return Zero-        g !spec (One x) = liftM One $ f x-        g !spec (Two x y) = liftM2 Two (g spec x) (g spec y)+        g !spec Zero = pure Zero+        g !spec (One x) = One <$> f x+        g !spec (Two x y) = Two <$> g spec x <*> g spec y   instance Functor Str where
Data/Generics/Uniplate.hs view
@@ -36,7 +36,7 @@     -- > uniplate (Add (Val 1) (Neg (Val 2))) = ([Val 1, Neg (Val 2)], \[a,b] -> Add a b)     -- > uniplate (Val 1)                     = ([]                  , \[]    -> Val 1  )     uniplate :: UniplateType on-    + -- * The Operations  -- ** Queries@@ -112,7 +112,7 @@     where (current, generate) = uniplate x  --- | Monadic variant of 'descend'    +-- | Monadic variant of 'descend' descendM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on descendM f x = liftM generate $ mapM f current     where (current, generate) = uniplate x@@ -148,4 +148,3 @@ --   technically a paramorphism para :: Uniplate on => (on -> [r] -> r) -> on -> r para op x = op x $ map (para op) $ children x-
Data/Generics/Uniplate/Data.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE ScopedTypeVariables, ExistentialQuantification, Rank2Types, CPP,     MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     This module defines 'Uniplate' / 'Biplate' instances for every type with a
Data/Generics/Uniplate/Data/Instances.hs view
@@ -39,7 +39,7 @@ -- >     where -- >         create x = (Trigger False ks, Trigger False vs, Hide x) -- >             where (ks,vs) = unzip $ Map.toAscList x--- > +-- > -- >         inv (ks,vs,x) -- >             | trigger ks = create $ Map.fromList $ zip (fromTrigger ks) (fromTrigger vs) -- >             | trigger vs = create $ Map.fromDistinctAscList $ zip (fromTrigger ks) (fromTrigger vs)
Data/Generics/Uniplate/DataOnly.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE ScopedTypeVariables, ExistentialQuantification, Rank2Types, CPP,     MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     This module functions identically to "Data.Generics.Uniplate.Data", but instead of
Data/Generics/Uniplate/Direct.hs view
@@ -1,12 +1,13 @@ {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     This module supplies a method for writing 'Uniplate' and 'Biplate' instances.     This moulde gives the highest performance, but requires many instance definitions. The     instances can be generated using Derive: <http://community.haskell.org/~ndm/derive/>.-    +     To take an example:-    +     > data Expr = Var Int | Pos Expr String | Neg Expr | Add Expr Expr     > data Stmt = Seq [Stmt] | Sel [Expr] | Let String Expr     >
Data/Generics/Uniplate/Internal/Data.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE CPP, Rank2Types, MagicHash, UnboxedTuples, ExistentialQuantification, ScopedTypeVariables #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     Internal module, do not import or use.@@ -18,40 +19,7 @@ import System.Environment(getEnv) import qualified Data.IntMap as IntMap; import Data.IntMap(IntMap) --#if __GLASGOW_HASKELL__ < 606- ------------------------------------------------------------------------ GHC 6.4 and below--import qualified Data.Set as Set-import qualified Data.Map as Map--type TypeKey = TypeRep-type TypeSet = Set.Set TypeKey-type TypeMap = Map.Map TypeKey--typeKey :: Typeable a => a -> TypeKey-typeKey = typeOf--#elif __GLASGOW_HASKELL__ < 702-------------------------------------------------------------------------- GHC 6.6 to 7.0 (has typeRepKey)--import qualified Data.IntSet as Set-import qualified Data.IntMap as Map--type TypeKey = Int-type TypeSet = Set.IntSet-type TypeMap = Map.IntMap--typeKey :: Typeable a => a -> TypeKey-typeKey x = inlinePerformIO $ typeRepKey $ typeOf x--#else----------------------------------------------------------------------- -- GHC 7.2 and above (using fingerprint)  import qualified Data.HashMap.Strict as Map@@ -64,28 +32,6 @@ typeKey :: Typeable a => a -> TypeKey typeKey = typeOf -#endif---#if __GLASGOW_HASKELL__ < 702-------------------------------------------------------------------------- GHC 7.0 and below (using containers API)--(!) = (Map.!)-map_findWithDefault = Map.findWithDefault-map_fromAscList = Map.fromAscList-map_keysSet = Map.keysSet-map_member = Map.member-set_partition = Set.partition-set_toAscList = Set.toAscList-set_unions = Set.unions--#else-------------------------------------------------------------------------- GHC 7.2 and above (using unordered-containers API)- (!) mp k = map_findWithDefault (error "Could not find element") k mp map_findWithDefault d k mp = fromMaybe d $ Map.lookup k mp -- in 0.2.3.0 lookupDefault is strict in the default :( map_fromAscList = Map.fromList@@ -95,9 +41,7 @@ set_toAscList = Set.toList set_unions = foldr Set.union Set.empty -#endif - {-# NOINLINE uniplateVerbose #-} uniplateVerbose :: Int -- -1 = error if failed, 0 = quiet, 1 = print errors only, 2 = print everything uniplateVerbose = unsafePerformIO $ do@@ -322,14 +266,19 @@     Follow -> gmapT (descendBiData oracle op) x     Miss -> x -descendDataM :: (Data on, Monad m) => (forall a . Typeable a => a -> Answer on) -> (on -> m on) -> on -> m on-descendDataM oracle op = gmapM (descendBiDataM oracle op)+descendDataM :: (Data on, Applicative m) => (forall a . Typeable a => a -> Answer on) -> (on -> m on) -> on -> m on+descendDataM oracle op = gmapA (descendBiDataM oracle op) -descendBiDataM :: (Data on, Data with, Monad m) => (forall a . Typeable a => a -> Answer with) -> (with -> m with) -> on -> m on+descendBiDataM :: (Data on, Data with, Applicative m) => (forall a . Typeable a => a -> Answer with) -> (with -> m with) -> on -> m on descendBiDataM oracle op x = case oracle x of     Hit y -> unsafeCoerce $ op y-    Follow -> gmapM (descendBiDataM oracle op) x-    Miss -> return x+    Follow -> gmapA (descendBiDataM oracle op) x+    Miss -> pure x++gmapA :: forall m a. (Data a, Applicative m) => (forall d. Data d => d -> m d) -> a -> m a+gmapA f = gfoldl k pure+    where k :: Data d => m (d -> b) -> d -> m b+          k c x = c <*> f x   ---------------------------------------------------------------------
Data/Generics/Uniplate/Internal/OperationsInc.hs view
@@ -1,4 +1,3 @@-import Control.Monad import Data.Generics.Str import Data.Generics.Uniplate.Internal.Utils @@ -42,11 +41,11 @@     descend f x = case uniplate x of         (current, generate) -> generate $ strMap f current -    -- | Monadic variant of 'descend'+    -- | Applicative variant of 'descend'     {-# INLINE descendM #-}-    descendM :: Monad m => (on -> m on) -> on -> m on+    descendM :: Applicative m => (on -> m on) -> on -> m on     descendM f x = case uniplate x of-        (current, generate) -> liftM generate $ strMapM f current+        (current, generate) -> generate <$> strMapM f current   @@ -72,9 +71,9 @@         (current, generate) -> generate $ strMap f current      {-# INLINE descendBiM #-}-    descendBiM :: Monad m => (to -> m to) -> from -> m from+    descendBiM :: Applicative m => (to -> m to) -> from -> m from     descendBiM f x = case biplate x of-        (current, generate) -> liftM generate $ strMapM f current+        (current, generate) -> generate <$> strMapM f current   -- * Single Type Operations@@ -125,8 +124,8 @@     where g = f . descend g  --- | Monadic variant of 'transform'-transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on+-- | Applicative variant of 'transform'+transformM :: (Monad m, Applicative m, Uniplate on) => (on -> m on) -> on -> m on transformM f = g     where g x = f =<< descendM g x @@ -143,8 +142,8 @@     where g x = maybe x (rewrite f) (f x)  --- | Monadic variant of 'rewrite'-rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on+-- | Applicative variant of 'rewrite'+rewriteM :: (Monad m, Applicative m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on rewriteM f = transformM g     where g x = f x >>= maybe (return x) (rewriteM f) @@ -210,24 +209,20 @@  {-# INLINE transformBi #-} transformBi :: Biplate from to => (to -> to) -> from -> from-transformBi f x = case biplate x of-    (current, generate) -> generate $ strMap (transform f) current+transformBi f = descendBi (transform f)   {-# INLINE transformBiM #-}-transformBiM :: (Monad m, Biplate from to) => (to -> m to) -> from -> m from-transformBiM f x = case biplate x of-    (current, generate) -> liftM generate $ strMapM (transformM f) current+transformBiM :: (Monad m, Applicative m, Biplate from to) => (to -> m to) -> from -> m from+transformBiM f = descendBiM (transformM f)   rewriteBi :: Biplate from to => (to -> Maybe to) -> from -> from-rewriteBi f x = case biplate x of-    (current, generate) -> generate $ strMap (rewrite f) current+rewriteBi f = descendBi (rewrite f)  -rewriteBiM :: (Monad m, Biplate from to) => (to -> m (Maybe to)) -> from -> m from-rewriteBiM f x = case biplate x of-    (current, generate) -> liftM generate $ strMapM (rewriteM f) current+rewriteBiM :: (Monad m, Applicative m, Biplate from to) => (to -> m (Maybe to)) -> from -> m from+rewriteBiM f = descendBiM (rewriteM f)   -- ** Others
Data/Generics/Uniplate/Internal/Utils.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE CPP, Rank2Types, MagicHash, UnboxedTuples, ExistentialQuantification #-}+{-# LANGUAGE Rank2Types, MagicHash, UnboxedTuples, ExistentialQuantification #-} {-# OPTIONS_GHC -fno-warn-unused-binds #-} -- SPEC2  -- | Internal module, do not import or use.@@ -6,54 +6,27 @@     unsafeCoerce, builder, unsafePerformIO, inlinePerformIO, concatCont, SPEC(SPEC)     ) where -#if __GLASGOW_HASKELL__ >= 702 import System.IO.Unsafe(unsafePerformIO)-#else-import Foreign(unsafePerformIO)-#endif import Unsafe.Coerce(unsafeCoerce) -#ifdef __GLASGOW_HASKELL__ import GHC.Exts(build, realWorld#)-#if __GLASGOW_HASKELL__ < 612-import GHC.IOBase(IO(IO))-#else import GHC.IO(IO(IO))-#endif-#endif--#if __GLASGOW_HASKELL__ >= 701-import GHC.Exts(SpecConstrAnnotation(..))-{-# ANN type SPEC ForceSpecConstr #-}-#endif+import GHC.Types(SPEC(..))   {-# INLINE builder #-} -- | GHCs @foldr@\/@build@ system, but on all platforms-#ifdef __GLASGOW_HASKELL__ builder :: forall a . (forall b . (a -> b -> b) -> b -> b) -> [a] builder = build-#else-builder :: ((x -> [x] -> [x]) -> [x] -> [x]) -> [x]-builder f = f (:) []-#endif   {-# INLINE inlinePerformIO #-} -- | 'unsafePerformIO', but suitable for inlining. Copied from "Data.ByteString.Base". inlinePerformIO :: IO a -> a-#ifdef __GLASGOW_HASKELL__ inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r-#else-inlinePerformIO = unsafePerformIO-#endif   {-# INLINE concatCont #-} -- | Perform concatentation of continuations concatCont :: [a -> a] -> a -> a concatCont xs rest = foldr ($) rest xs----- | Constructor specialisation on newer GHC-data SPEC = SPEC | SPEC2
Data/Generics/Uniplate/Typeable.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}  {- |     /RECOMMENDATION:/ Use "Data.Generics.Uniplate.Data" instead - it usually performs
Data/Generics/UniplateOn.hs view
@@ -107,4 +107,3 @@     where         (a , b ) = f x         (as, bs) = uniplateOnList f xs-
Data/Generics/UniplateStr.hs view
@@ -144,7 +144,7 @@     where (current, generate) = uniplate x  --- | Monadic variant of 'descend'    +-- | Monadic variant of 'descend' descendM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on descendM f x = liftM generate $ mapM f current     where (current, generate) = uniplate x
LICENSE view
@@ -1,4 +1,4 @@-Copyright Neil Mitchell 2006-2013.+Copyright Neil Mitchell 2006-2020. All rights reserved.  Redistribution and use in source and binary forms, with or without
+ README.md view
@@ -0,0 +1,236 @@+# Boilerplate Removal with Uniplate [![Hackage version](https://img.shields.io/hackage/v/uniplate.svg?label=Hackage)](https://hackage.haskell.org/package/uniplate) [![Stackage version](https://www.stackage.org/package/uniplate/badge/nightly?label=Stackage)](https://www.stackage.org/package/uniplate) [![Build status](https://img.shields.io/travis/ndmitchell/uniplate/master.svg?label=Build)](https://travis-ci.org/ndmitchell/uniplate)++Generic transformations and queries are often referred to as boilerplate code - they remain relatively similar as the action performed by the code changes, and can often outnumber the actual intent of the code in terms of lines. While other generic traversal schemes have shown how powerful new features can be added to compilers, and how the type system can be manipulated into accepting these operations, the Uniplate library focuses on a conceptually simpler generic concept. A more complete document on Uniplate was published at the Haskell Workshop 2007, and is available from [here](https://ndmitchell.com/#uniplate_30_sep_2007), along with a video presentation, and the associated thesis chapter.++Uniplate is a simple, concise and fast generics library. To expand on that sentence:++1. A generics library is one which allows you to write functions that operate over a data structure without tying down all aspects of the data structure. In particular, when writing an operation, you don't need to give a case for each constructor, and you don't have to state which fields are recursive.+2. Uniplate is the simplest generics library. Using Uniplate is within the reach of all Haskell programmers.+3. Uniplate is more concise than any other generics library.+4. Uniplate is fast, not always the absolute fastest, but massively faster than many generics libraries.+5. Uniplate is also less powerful than some other generics libraries, but if it does the job, you should use it.++The Uniplate library can be installed with the standard sequence of cabal commands:++    cabal update+    cabal install uniplate++This document proceeds as follows:++1. Using Uniplate+2. Using Biplate+3. Making Uniplate Faster++#### Acknowledgements++Thanks to Bj&ouml;rn Bringert for feedback on an earlier version of this document, Eric Mertens for various ideas and code snippets, and to Matt Naylor and Tom Shackell for helpful discussions.++## Using Uniplate++To demonstrate the facilities of Uniplate, we use a simple arithmetic type:++<pre>+<b>{-# LANGUAGE DeriveDataTypeable #-}</b>+module Expr where+<b>import Data.Data</b>+<b>import Data.Generics.Uniplate.Data</b>++data Expr = Val Int+          | Add Expr Expr+          | Sub Expr Expr+          | Div Expr Expr+          | Mul Expr Expr+          | Neg Expr+          deriving (Show, Eq, <b>Data, Typeable</b>)+</pre>++In this definition, the Uniplate specific bits are bolded. The three extra parts are:++* `import Data.Generics.Uniplate.Data`, this module contains all the Uniplate functions and definitions.+* `deriving (Data,Typeable)`, this deriving clause automatically adds the necessary instances for Uniplate.+* `{-# LANGUAGE DeriveDataTypeable #-}`, this pragma turns on language support for the deriving line.++This definition makes use of the [Scrap Your Boilerplate (SYB)](http://doi.acm.org/10.1145/604174.604179) based Uniplate implementation. The SYB implementation is compatible with the other implementations, but is slower (between 2 and 8 times) and requires some modest compiler extensions (implemented in [GHC](http://haskell.org/ghc/) for many years). The alternative definition scheme is described towards the end of this document, in "Making Uniplate Faster". I recommend using the SYB implementation to start with, as it requires least work to use.++The Uniplate library defines two classes, `Uniplate` and `Biplate`, along with a number of functions. After importing `Data.Generics.Uniplate.Data` all types which have `Data`instances automatically have the necessary Uniplate instances. In the following subsections we introduce the Uniplate functions, along with examples of using them. The two most commonly used functions are `universe` (used for queries) and `transform` (used for transformations).++### Finding the constant values++```haskell+universe :: Uniplate on => on -> [on]+```++When manipulating our little language it may be useful to know which constants have been used. This can be done with the following code:++```haskell+constants :: Expr -> [Int]+constants x = nub [y | Val y <- universe x]+```++Here the only Uniplate method being used is `universe`, which when given a tree returns the root of the tree, and all its subtrees at all levels. This can be used to quickly flatten a tree structure into a list, for quick analysis via list comprehensions, as is done above.++_Exercise:_ Write a function to test if an expression performs a division by the literal zero.++### Basic optimisation++```haskell+transform :: Uniplate on => (on -> on) -> on -> on+```++If we are negating a literal value, this computation can be performed in advance, so let's write a function to do this.++```haskell+optimise :: Expr -> Expr+optimise = transform f+    where f (Neg (Val i)) = Val (negate i)+            f x = x+```++Here the Uniplate method being used is `transform`, which applies the given function to all the children of an expression, before applying it to the parent. This function can be thought of as bottom-up traversal of the data structure. The optimise code merely pattern matches on the negation of a literal, and replaces it with the literal.++Now let's add another optimisation into the same pass, just before the `f x = x` line insert:++```haskell+f (Add x y) | x == y = Mul x (Val 2)+```++This takes an addition where two terms are equal and changes it into a multiplication, causing the nested expression to be executed only once.++_Exercise:_ Extend the optimisation so that adding `x` to `Mul x (Val 2)` produces a multiplication by 3.++### Depth of an expression++```haskell+para :: Uniplate on => (on -> [res] -> res) -> on -> res+```++Now let's imagine that programmers in your language are paid by the depth of expression they produce, so let's write a function that computes the maximum depth of an expression.++```haskell+depth :: Expr -> Int+depth = para (\_ cs -> 1 + maximum (0:cs))+```++This function performs a paramorphism (a bit like a fold) over the data structure. The function simply says that for each iteration, add one to the previous depth.++_Exercise:_ Write a function that counts the maximum depth of addition only.++### Renumbering literals++```haskell+transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on+```++The literal values need to be replaced with a sequence of numbers, each unique. This is unlikely for an arithmetic expression, but consider bound variables in lambda calculus and it starts to become a bit more plausible:++```haskell+uniqueLits :: Expr -> Expr+uniqueLits x = evalState (transformM f x) [0..]+    where+        f (Val i) = do+            y:ys <- get+            put ys+            return (Val y)+        f x = return x+```++Here a monadic computation is required, the program needs to keep track of what the next item in the list to use is, and replace the current item. By using the state monad, this can be done easily.++_Exercise:_ Allow each literal to occur only once, when a second occurrence is detected, replace that literal with zero.++### Generating mutants++```haskell+contexts :: Uniplate on => on -> [(on, on -> on)]+```++The person who is inputting the expression thinks they made a mistake, they suspect they got one of the values wrong by plus or minus one. Generate all the expressions they might have written.++```haskell+mutate :: Expr -> [Expr]+mutate x = concat [[gen $ Val $ i-1, gen $ Val $ i+1]+                    | (Val i, gen) <- contexts x]+```++The `transform` function is useful for doing an operation to all nodes in a tree, but sometimes you only want to apply a transformation once. This is less common, but is sometimes required. The idea is that the context provides the information required to recreate the original expression, but with this node altered.++_Exercise:_ Replace one multiplication with addition, if there are no multiplications return the original expression.++### Fixed point optimisation++```haskell+rewrite :: Uniplate on => (on -> Maybe on) -> on -> on+```++When slotting many transformations together, often one optimisation will enable another. For example, the the optimisation to reduce.++### Descend++Do something different in the odd and even cases. Particularly useful if you have free variables and are passing state downwards.++### Monadic Variants++```haskell+descendM :: Monad m => (on -> m on) -> on -> m on                         -- descend+transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on        -- transform+rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on  -- rewrite+```++All the transformations have both monadic and non-monadic versions.++### Single Depth Varaints++```haskell+children :: Uniplate on => on -> [on]           -- universe+descend :: (on -> on) -> on -> on               -- transform+holes :: Uniplate on => on -> [(on, on -> on)]  -- contexts+```++Lots of functions which operate over the entire tree also operate over just one level. Usually you want to use the multiple level version, but when needing more explicit control the others are handy.++### Evaluation++If we need to evaluate an expression in our language, the answer is simple, don't use Uniplate! The reasons are that there is little boilerplate, you have to handle every case separately. For example in our language we can write:++```haskell+eval :: Expr -> Int+eval (Val i) = i+eval (Add a b) = eval a + eval b+eval (Sub a b) = eval a - eval b+eval (Div a b) = eval a `div` eval b+eval (Mul a b) = eval a * eval b+eval (Neg a) = negate a+```++## Using Biplate++All the operations defined in Uniplate have a corresponding Biplate instance. Typically the operations are just the same as Uniplate, with `Bi` on the end.++```haskell+universeBi :: Biplate on with => on -> [with]+transformBi :: Biplate on with => (with -> with) -> on -> on+transformBiM :: (Monad m, Biplate on with) => (with -> m with) -> on -> m on+```++The biggest difference is for the functions `childrenBi` and `descendBi`. In these cases, if the starting type and the target type are the same, then the input value will be returned. For example:++```haskell+childrenBi (Add (Val 1) (Val 2)) == [Add (Val 1) (Val 2)]+children (Add (Val 1) (Val 2)) == [Val 1, Val 2]+```++For example, you should never have `descendBi` in an inner recursive loop.++## Making Uniplate Faster++To make Uniplate faster import `Data.Generics.Uniplate.Direct` and write your instances by hand.++## Related work++* [Geniplate](http://hackage.haskell.org/package/geniplate), by Lennart Augustsson, Uniplate compatible but implemented using Template Haskell.+* [Refactoring Uniplate](http://www-ps.informatik.uni-kiel.de/~sebf/projects/traversal.html), by Sebastian Fischer - proposing a slightly different Uniplate API, but with the same underlying concepts.+* [Uniplate for Curry](http://www.informatik.uni-kiel.de/~pakcs/lib/CDOC/Traversal.html), by Sebastian Fischer - using his revised API as above.+* [Uniplate for ML (in MLton)](https://github.com/MLton/mltonlib/blob/master/com/ssh/generic/unstable/public/value/uniplate.sig), as part of the MLton generics library.+* [Uniplate for data types with embedded monads](http://tomschrijvers.blogspot.com/2007/11/extension-proposal-for-uniplate.html), by Tom Schrijvers+* [Multiplate](http://hackage.haskell.org/package/multiplate), by Russell O'Connor, similar ideas to Uniplate but with a very different underlying substrate.+* [Infer.Toys](https://infers.github.io/Infers/Infers.Toys.html) provides a Uniplate-inspired `Elems` module.
uniplate.cabal view
@@ -1,11 +1,12 @@-cabal-version:      >= 1.6+cabal-version:      >= 1.18 build-type:         Simple name:               uniplate-version:            1.6.12+version:            1.6.13 author:             Neil Mitchell <ndmitchell@gmail.com> maintainer:         Neil Mitchell <ndmitchell@gmail.com>-copyright:          Neil Mitchell 2006-2013-homepage:           http://community.haskell.org/~ndm/uniplate/+copyright:          Neil Mitchell 2006-2020+homepage:           https://github.com/ndmitchell/uniplate#readme+bug-reports:        https://github.com/ndmitchell/uniplate/issues license:            BSD3 license-file:       LICENSE synopsis:           Help writing simple, concise and fast generic operations.@@ -13,8 +14,7 @@ description:     Uniplate is library for writing simple and concise generic operations.     Uniplate has similar goals to the original Scrap Your Boilerplate work,-    but is substantially simpler and faster. The Uniplate manual is available at-    <http://community.haskell.org/~ndm/darcs/uniplate/uniplate.htm>.+    but is substantially simpler and faster.     .     To get started with Uniplate you should import one of the three following     modules:@@ -41,28 +41,23 @@     to avoid getting instance conflicts.  extra-source-files:-    uniplate.htm     Data/Generics/Uniplate/Internal/DataInc.hs     Data/Generics/Uniplate/Internal/OperationsInc.hs+extra-doc-files:+    README.md+    CHANGES.txt+tested-with: GHC==8.10, GHC==8.8, GHC==8.6, GHC==8.4, GHC==8.2, GHC==8.0  source-repository head     type:     git     location: https://github.com/ndmitchell/uniplate.git -flag typeable_fingerprint-flag separate_syb- library-    if flag(typeable_fingerprint)-        build-depends:-            base >=4.4 && <5, containers, syb,-            hashable >= 1.1.2.3 && < 1.3,-            unordered-containers >= 0.2.1 && < 0.3-    else-        if flag(separate_syb)-            build-depends: base >=4 && <4.4, containers, syb-        else-            build-depends: base >=3 && <4, containers+    default-language: Haskell2010+    build-depends:+        base >=4.4 && <5, containers, syb, ghc-prim,+        hashable >= 1.1.2.3,+        unordered-containers >= 0.2.1      exposed-modules:         Data.Generics.Str@@ -90,5 +85,3 @@         Data.Generics.Uniplate.Internal.Data         Data.Generics.Uniplate.Internal.DataOnlyOperations         Data.Generics.Uniplate.Internal.Utils--    extensions: CPP
− uniplate.htm
@@ -1,317 +0,0 @@-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"-        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">-<html>-    <head>-        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />-        <title>Boilerplate Removal with Uniplate</title>-        <style type="text/css">-pre {-    border: 2px solid gray;-    padding: 1px;-    padding-left: 5px;-    margin-left: 10px;-    background-color: #eee;-}--pre.define {-    background-color: #ffb;-    border-color: #cc0;-}--body {-    font-family: sans-serif;-}--h1, h2, h3 {-    font-family: serif;-}--h1 {-    color: rgb(23,54,93);-    border-bottom: 1px solid rgb(79,129,189);-    padding-bottom: 2px;-    font-variant: small-caps;-    text-align: center;-}--a {-    color: rgb(54,95,145);-}--h2 {-    color: rgb(54,95,145);-}--h3 {-    color: rgb(79,129,189);-}-        </style>-    </head>-    <body>--<h1>Boilerplate Removal with Uniplate</h1>--<p style="text-align:right;margin-bottom:25px;">-    by <a href="http://community.haskell.org/~ndm/">Neil Mitchell</a>-</p>--<p style="margin-left:10%;margin-right:10%;margin-bottom:25px;text-align:justify;">-    Generic transformations and queries are often referred to as boilerplate code - they remain relatively similar as the action performed by the code changes, and can often outnumber the actual intent of the code in terms of lines. While other generic traversal schemes have shown how powerful new features can be added to compilers, and how the type system can be manipulated into accepting these operations, the Uniplate library focuses on a conceptually simpler generic concept. A more complete document on Uniplate was published at the Haskell Workshop 2007, and is available from the <a href="http://community.haskell.org/~ndm/uniplate/">project website</a>, along with a video presentation, and the associated thesis chapter.-</p>-<p>-	Uniplate is a simple, concise and fast generics library. To expand on that sentence:-</p>-<ol>-	<li>A generics library is one which allows you to write functions that operate over a data structure without tying down all aspects of the data structure. In particular, when writing an operation, you don't need to give a case for each constructor, and you don't have to state which fields are recursive.</li>-	<li>Uniplate is the simplest generics library. Using Uniplate is within the reach of all Haskell programmers.</li>-	<li>Uniplate is more concise than any other generics library.</li>-	<li>Uniplate is fast, not always the absolute fastest, but massively faster than many generics libraries.</li>-	<li>Uniplate is also less powerful than some other generics libraries, but if it does the job, you should use it.</li>-</ol>-<p>-    The Uniplate library can be installed with the standard sequence of cabal commands:-</p>-<pre>-cabal update-cabal install uniplate-</pre>-<p>-    This document proceeds as follows:-</p>-<ol>-    <li>Using Uniplate</li>-    <li>Using Biplate</li>-    <li>Making Uniplate Faster</li>-</ol>--<h3>Acknowledgements</h3>--<p>-    Thanks to Bj&ouml;rn Bringert for feedback on an earlier version of this document, Eric Mertens for various ideas and code snippets, and to Matt Naylor and Tom Shackell for helpful discussions.-</p>---<h2>Using Uniplate</h2>--<p>-	To demonstrate the facilities of Uniplate, we use a simple arithmetic type:-</p>-<pre>-<b>{-# LANGUAGE DerivingDataTypeable #-}</b>-module Expr where-<b>import Data.Generics.Uniplate.Data</b>--data Expr = Val Int-          | Add Expr Expr-          | Sub Expr Expr-          | Div Expr Expr-          | Mul Expr Expr-          | Neg Expr-          deriving (Show, Eq, <b>Data, Typeable</b>)-</pre>-<p>-	In this definition, the Uniplate specific bits are bolded. The three extra parts are:-</p>-<ul>-<li><tt>import Data.Generics.Uniplate.Data</tt>, this module contains all the Uniplate functions and definitions.</li>-<li><tt>deriving (Data,Typeable)</tt>, this deriving clause automatically adds the necessary instances for Uniplate.</li>-<li><tt>{-# LANGUAGE DerivingDataTypeable #-}</tt>, this pragma turns on language support for the deriving line.</li>-</ul>-<p>-	This definition makes use of the <a href="http://doi.acm.org/10.1145/604174.604179">Scrap Your Boilerplate (SYB)</a> based Uniplate implementation. The SYB implementation is compatible with the other implementations, but is slower (between 2 and 8 times) and requires some modest compiler extensions (implemented in <a href="http://haskell.org/ghc/">GHC</a> for many years). The alternative definition scheme is described towards the end of this document, in "Making Uniplate Faster". I recommend using the SYB implementation to start with, as it requires least work to use.-</p>-<p>-	The Uniplate library defines two classes, <tt>Uniplate</tt> and <tt>Biplate</tt>, along with a number of functions. After importing <tt>Data.Generics.Uniplate.Data</tt> all types which have <tt>Data</tt> instances automatically have the necessary Uniplate instances. In the following subsections we introduce the Uniplate functions, along with examples of using them. The two most commonly used functions are <tt>universe</tt> (used for queries) and <tt>transform</tt> (used for transformations).-</p>--<h3>Finding the constant values</h3>--<pre class="define">-universe :: Uniplate on => on -> [on]-</pre>-<p>-	When manipulating our little language it may be useful to know which constants have been used. This can be done with the following code:-</p>-<pre>-constants :: Expr -> [Int]-constants x = nub [y | Val y <- universe x]-</pre>-<p>-    Here the only Uniplate method being used is <tt>universe</tt>, which when given a tree returns all the root of the tree, and all it's subtrees at all levels. This can be used to quickly flatten a tree structure into a list, for quick analysis via list comprehensions, as is done above.-</p>-<p>-    <i>Exercise:</i> Write a function to test if an expression performs a division by the literal zero.-</p>--<h3>Basic optimisation</h3>--<pre class="define">-transform :: Uniplate on => (on -> on) -> on -> on-</pre>-<p>-    If we are negating a literal value, this computation can be performed in advance, so let's write a function to do this.-</p>-<pre>-optimise :: Expr -> Expr-optimise = transform f-    where f (Neg (Val i)) = Val (negate i)-          f x = x-</pre>-<p>-    Here the Uniplate method being used is <tt>transform</tt>, which applies the given function to all the children of an expression, before applying it to the parent. This function can be thought of as bottom-up traversal of the data structure. The optimise code merely pattern matches on the negation of a literal, and replaces it with the literal.-</p>-<p>-    Now lets add another optimisation into the same pass, just before the <tt>f x = x</tt> line insert:-</p>-<pre>-    f (Add x y) | x == y = Mul x (Val 2)-</pre>-<p>-    This takes an addition where two terms are equal and changes it into a multiplication, causing the nested expression to be executed only once.-</p>-<p>-    <i>Exercise:</i> Extend the optimisation to so that adding <tt>x</tt> to <tt>Mul x (Val 2)</tt> produces a multiplication by 3.-</p>---<h3>Depth of an expression</h3>--<pre class="define">-para :: Uniplate on => (on -> [res] -> res) -> on -> res-</pre>-<p>-    Now lets imagine that programmers in your language are paid by the depth of expression they produce, so lets write a function that computes the maximum depth of an expression.-</p>-<pre>-depth :: Expr -> Int-depth = para (\_ cs -> 1 + maximum (0:cs))-</pre>-<p>-    This function performs a paramorphism (a bit like a fold) over the data structure. The function simply says that for each iteration, add one to the previous depth.-</p>-<p>-    <i>Exercise:</i> Write a function that counts the maximum depth of addition only.-</p>--<h3>Renumbering literals</h3>--<pre class="define">-transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on-</pre>-<p>-    The literal values need to be replaced with a sequence of numbers, each unique. This is unlikely for an arithmetic expression, but consider bound variables in lambda calculus and it starts to become a bit more plausible:-</p>-<pre>-uniqueLits :: Expr -> Expr-uniqueLits x = evalState (transformM f x) [0..]-    where-        f (Val i) = do-            y:ys <- get-            put ys-            return (Val y)-        f x = return x-</pre>-<p>-    Here a monadic computation is required, the program needs to keep track of what the next item in the list to use is, and replace the current item. By using the state monad, this can be done easily.-</p>-<p>-    <i>Exercise:</i> Allow each literal to occur only once, when a second occurance is detected, replace that literal with zero.-</p>--<h3>Generating mutants</h3>--<pre class="define">-contexts :: Uniplate on => on -> [(on, on -> on)]-</pre>-<p>-    The person who is inputting the expression thinks they made a mistake, they suspect they got one of the values wrong by plus or minus one. Generate all the expressions they might have written.-</p>-<pre>-mutate :: Expr -> [Expr]-mutate x = concat [[gen $ Val $ i-1, gen $ Val $ i+1]-                  | (Val i, gen) <- contexts x]-</pre>-<p>-   The <tt>transform</tt> function is useful for doing an operation to all nodes in a tree, but sometimes you only want to apply a transformation once. This is less common, but is sometimes required. The idea is that the context provides the information required to recreate the original expression, but with this node altered.-</p>-<p>-    <i>Exercise:</i> Replace one multiplication with addition, if there are no multiplications return the original expression.-</p>--<h3>Fixed point optimisation</h3>--<pre class="define">-rewrite :: Uniplate on => (on -> Maybe on) -> on -> on-</pre>-<p>-	When slotting many transformations together, often one optimisation will enable another. For example, the the optimisation to reduce.-</p>--<h3>Descend</h3>--<p>-	Do something different in the odd and even cases. Particularly useful if you have free variables and are passing state downwards.-</p>--<h3>Monadic Variants</h3>--<pre class="define">-descendM :: Monad m => (on -> m on) -> on -> m on                         -- descend-transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on        -- transform-rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on  -- rewrite-</pre>--<p>-	All the transformations have both monadic and non-monadic versions.-</p>--<h3>Single Depth Varaints</h3>--<pre class="define">-children :: Uniplate on => on -> [on]           -- universe-descend :: (on -> on) -> on -> on               -- transform-holes :: Uniplate on => on -> [(on, on -> on)]  -- contexts-</pre>--<p>-	Lot's of functions which operate over the entire tree also operate over just one level. Usually you want to use the multiple level version, but when needing more explicit control the others are handy.-</p>--<h3>Evaluation</h3>--<p>-	Don't use Uniplate! The reasons are that there is little boilerplate, you have to handle every case separately. For example in our language we can write:-</p>---<h2>Using Biplate</h2>--<p>-	All the operations defined in Uniplate have a corresponding Biplate instance. Typically the operations are just the same as Uniplate, with <tt>Bi</tt> on the end.-</p>-<pre class="define">-universeBi :: Biplate on with => on -> [with]-transformBi :: Biplate on with => (with -> with) -> on -> on-transformBiM :: (Monad m, Biplate on with) => (with -> m with) -> on -> m on-</pre>-<p>-	The biggest difference is for the functions <tt>childrenBi</tt> and <tt>descendBi</tt>. In these cases, if the starting type and the target type are the same, then the input value will be returned. For example:-</p>-<pre>-childrenBi (Add (Val 1) (Val 2)) == [Add (Val 1) (Val 2)]-children (Add (Val 1) (Val 2)) == [Val 1, Val 2]-</pre>--<p>-	For example, you should never hvae <tt>descendBi</tt> in an inner recursive loop.-</p>--<h2>Making Uniplate Faster</h2>--<p>-	To make Uniplate faster import <tt>Data.Generics.Uniplate.Direct</tt>, and provide Uniplate instances by generating them with the <a href="http://community.haskell.org/~ndm/derive/">Derive tool</a>.-</p>---    </body>-</html>