@@ -168,6 +168,9 @@ import Data.Data
168
168
#if __GLASGOW_HASKELL__ >= 709
169
169
import Data.Coerce
170
170
#endif
171
+ #if MIN_VERSION_base(4,8,0)
172
+ import Data.Functor.Identity (Identity (.. ))
173
+ #endif
171
174
172
175
173
176
infixr 5 `consTree`
@@ -554,19 +557,16 @@ instance NFData a => NFData (Elem a) where
554
557
-------------------------------------------------------
555
558
-- Applicative construction
556
559
-------------------------------------------------------
560
+ #if !MIN_VERSION_base(4,8,0)
561
+ newtype Identity a = Identity { runIdentity :: a }
557
562
558
- newtype Id a = Id { runId :: a }
559
-
560
- instance Functor Id where
561
- fmap f (Id x) = Id (f x)
562
-
563
- instance Monad Id where
564
- return = Id
565
- m >>= k = k (runId m)
563
+ instance Functor Identity where
564
+ fmap f (Identity x) = Identity (f x)
566
565
567
- instance Applicative Id where
568
- pure = return
569
- (<*>) = ap
566
+ instance Applicative Identity where
567
+ pure = Identity
568
+ Identity f <*> Identity x = Identity (f x)
569
+ #endif
570
570
571
571
-- | This is essentially a clone of Control.Monad.State.Strict.
572
572
newtype State s a = State { runState :: s -> (s , a )}
@@ -598,26 +598,26 @@ mapAccumL' f s t = runState (traverse (State . flip f) t) s
598
598
-- specified. This is a generalization of 'replicateA', which itself
599
599
-- is a generalization of many Data.Sequence methods.
600
600
{-# SPECIALIZE applicativeTree :: Int -> Int -> State s a -> State s (FingerTree a) #-}
601
- {-# SPECIALIZE applicativeTree :: Int -> Int -> Id a -> Id (FingerTree a) #-}
602
- -- Special note: the Id specialization automatically does node sharing,
601
+ {-# SPECIALIZE applicativeTree :: Int -> Int -> Identity a -> Identity (FingerTree a) #-}
602
+ -- Special note: the Identity specialization automatically does node sharing,
603
603
-- reducing memory usage of the resulting tree to /O(log n)/.
604
604
applicativeTree :: Applicative f => Int -> Int -> f a -> f (FingerTree a )
605
605
applicativeTree n mSize m = mSize `seq` case n of
606
606
0 -> pure Empty
607
- 1 -> liftA Single m
607
+ 1 -> fmap Single m
608
608
2 -> deepA one emptyTree one
609
609
3 -> deepA two emptyTree one
610
610
4 -> deepA two emptyTree two
611
611
5 -> deepA three emptyTree two
612
612
6 -> deepA three emptyTree three
613
613
7 -> deepA four emptyTree three
614
614
8 -> deepA four emptyTree four
615
- _ -> let (q, r) = n `quotRem` 3 in q `seq` case r of
616
- 0 -> deepA three (applicativeTree (q - 2 ) mSize' n3) three
617
- 1 -> deepA four (applicativeTree (q - 2 ) mSize' n3) three
618
- _ -> deepA four (applicativeTree (q - 2 ) mSize' n3) four
615
+ _ -> case n `quotRem` 3 of
616
+ (q, 0 ) -> deepA three (applicativeTree (q - 2 ) mSize' n3) three
617
+ (q, 1 ) -> deepA four (applicativeTree (q - 2 ) mSize' n3) three
618
+ (q,_) -> deepA four (applicativeTree (q - 2 ) mSize' n3) four
619
619
where
620
- one = liftA One m
620
+ one = fmap One m
621
621
two = liftA2 Two m m
622
622
three = liftA3 Three m m m
623
623
four = liftA3 Four m m m <*> m
@@ -641,7 +641,7 @@ singleton x = Seq (Single (Elem x))
641
641
-- | /O(log n)/. @replicate n x@ is a sequence consisting of @n@ copies of @x@.
642
642
replicate :: Int -> a -> Seq a
643
643
replicate n x
644
- | n >= 0 = runId (replicateA n (Id x))
644
+ | n >= 0 = runIdentity (replicateA n (Identity x))
645
645
| otherwise = error " replicate takes a nonnegative integer argument"
646
646
647
647
-- | 'replicateA' is an 'Applicative' version of 'replicate', and makes
0 commit comments