data-diverse 1.2.0.3 → 1.3.0.0
raw patch · 7 files changed
+100/−437 lines, 7 filesdep ~containersPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: containers
API changes (from Hackage documentation)
- Data.Diverse.Many: insertAfter :: forall x y xs proxy. (UniqueMember x xs) => proxy x -> y -> Many xs -> Many (Append (To x xs) (y : After x xs))
- Data.Diverse.Many: insertAfterL :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> y -> Many xs -> Many (Append (To x xs) (y : After x xs))
- Data.Diverse.Many: insertAfterN :: forall n y xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> y -> Many xs -> Many (Append (ToIndex n xs) (y : AfterIndex n xs))
- Data.Diverse.Many: insertBefore :: forall x y xs proxy. (UniqueMember x xs) => proxy x -> y -> Many xs -> Many (Append (Before x xs) (y : From x xs))
- Data.Diverse.Many: insertBeforeL :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> y -> Many xs -> Many (Append (Before x xs) (y : From x xs))
- Data.Diverse.Many: insertBeforeN :: forall n y xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> y -> Many xs -> Many (Append (BeforeIndex n xs) (y : FromIndex n xs))
- Data.Diverse.Many: insetAfter :: forall x ys xs proxy. (UniqueMember x xs) => proxy x -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))
- Data.Diverse.Many: insetAfterL :: forall l ys xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))
- Data.Diverse.Many: insetAfterN :: forall n ys xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many ys -> Many xs -> Many (Append (ToIndex n xs) (Append ys (AfterIndex n xs)))
- Data.Diverse.Many: insetBefore :: forall x ys xs proxy. (UniqueMember x xs) => proxy x -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))
- Data.Diverse.Many: insetBeforeL :: forall l ys xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))
- Data.Diverse.Many: insetBeforeN :: forall n ys xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many ys -> Many xs -> Many (Append (BeforeIndex n xs) (Append ys (FromIndex n xs)))
- Data.Diverse.Many: remove :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> Many (Remove x xs)
- Data.Diverse.Many: removeL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> Many (Remove x xs)
- Data.Diverse.Many: removeN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> Many (RemoveIndex n xs)
- Data.Diverse.Many: splitAfter :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> (Many (To x xs), Many (After x xs))
- Data.Diverse.Many: splitAfterL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> (Many (To x xs), Many (After x xs))
- Data.Diverse.Many: splitAfterN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> (Many (ToIndex n xs), Many (AfterIndex n xs))
- Data.Diverse.Many: splitBefore :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> (Many (Before x xs), Many (From x xs))
- Data.Diverse.Many: splitBeforeL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> (Many (Before x xs), Many (From x xs))
- Data.Diverse.Many: splitBeforeN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> (Many (BeforeIndex n xs), Many (FromIndex n xs))
- Data.Diverse.Many.Internal: insertAfter :: forall x y xs proxy. (UniqueMember x xs) => proxy x -> y -> Many xs -> Many (Append (To x xs) (y : After x xs))
- Data.Diverse.Many.Internal: insertAfterL :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> y -> Many xs -> Many (Append (To x xs) (y : After x xs))
- Data.Diverse.Many.Internal: insertAfterN :: forall n y xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> y -> Many xs -> Many (Append (ToIndex n xs) (y : AfterIndex n xs))
- Data.Diverse.Many.Internal: insertBefore :: forall x y xs proxy. (UniqueMember x xs) => proxy x -> y -> Many xs -> Many (Append (Before x xs) (y : From x xs))
- Data.Diverse.Many.Internal: insertBeforeL :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> y -> Many xs -> Many (Append (Before x xs) (y : From x xs))
- Data.Diverse.Many.Internal: insertBeforeN :: forall n y xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> y -> Many xs -> Many (Append (BeforeIndex n xs) (y : FromIndex n xs))
- Data.Diverse.Many.Internal: insetAfter :: forall x ys xs proxy. (UniqueMember x xs) => proxy x -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))
- Data.Diverse.Many.Internal: insetAfterL :: forall l ys xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))
- Data.Diverse.Many.Internal: insetAfterN :: forall n ys xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many ys -> Many xs -> Many (Append (ToIndex n xs) (Append ys (AfterIndex n xs)))
- Data.Diverse.Many.Internal: insetBefore :: forall x ys xs proxy. (UniqueMember x xs) => proxy x -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))
- Data.Diverse.Many.Internal: insetBeforeL :: forall l ys xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))
- Data.Diverse.Many.Internal: insetBeforeN :: forall n ys xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many ys -> Many xs -> Many (Append (BeforeIndex n xs) (Append ys (FromIndex n xs)))
- Data.Diverse.Many.Internal: remove :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> Many (Remove x xs)
- Data.Diverse.Many.Internal: removeL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> Many (Remove x xs)
- Data.Diverse.Many.Internal: removeN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> Many (RemoveIndex n xs)
- Data.Diverse.Many.Internal: splitAfter :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> (Many (To x xs), Many (After x xs))
- Data.Diverse.Many.Internal: splitAfterL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> (Many (To x xs), Many (After x xs))
- Data.Diverse.Many.Internal: splitAfterN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> (Many (ToIndex n xs), Many (AfterIndex n xs))
- Data.Diverse.Many.Internal: splitBefore :: forall x xs proxy. (UniqueMember x xs) => proxy x -> Many xs -> (Many (Before x xs), Many (From x xs))
- Data.Diverse.Many.Internal: splitBeforeL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> (Many (Before x xs), Many (From x xs))
- Data.Diverse.Many.Internal: splitBeforeN :: forall n xs proxy. (KnownNat n, (n + 1) <= Length xs) => proxy n -> Many xs -> (Many (BeforeIndex n xs), Many (FromIndex n xs))
- Data.Diverse.Which.Internal: instance (Control.DeepSeq.NFData x, Control.DeepSeq.NFData (Data.Diverse.Which.Internal.Which (x' : xs))) => Control.DeepSeq.NFData (Data.Diverse.Which.Internal.Which (x : x' : xs))
- Data.Diverse.Which.Internal: instance Control.DeepSeq.NFData x => Control.DeepSeq.NFData (Data.Diverse.Which.Internal.Which '[x])
+ Data.Diverse.Many: fetchTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Many xs -> x
+ Data.Diverse.Many: replaceTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Many xs -> x -> Many xs
+ Data.Diverse.Many: replaceTag' :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> y -> Many (Replace x (Tagged l y) xs)
+ Data.Diverse.Many.Internal: fetchTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Many xs -> x
+ Data.Diverse.Many.Internal: replaceTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Many xs -> x -> Many xs
+ Data.Diverse.Many.Internal: replaceTag' :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> y -> Many (Replace x (Tagged l y) xs)
+ Data.Diverse.Which: pickTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> x -> Which xs
+ Data.Diverse.Which: trialTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Which xs -> Either (Which (Remove (Tagged l x) xs)) x
+ Data.Diverse.Which: trialTag' :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Which xs -> Maybe x
+ Data.Diverse.Which.Internal: instance Data.Diverse.Reduce.Reduce (Data.Diverse.Which.Internal.Which (x : xs)) (Data.Diverse.Which.Internal.Switcher (Data.Diverse.CaseFunc.CaseFunc Control.DeepSeq.NFData) () (x : xs)) => Control.DeepSeq.NFData (Data.Diverse.Which.Internal.Which (x : xs))
+ Data.Diverse.Which.Internal: pickTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> x -> Which xs
+ Data.Diverse.Which.Internal: trialTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Which xs -> Either (Which (Remove (Tagged l x) xs)) x
+ Data.Diverse.Which.Internal: trialTag' :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs) => proxy l -> Which xs -> Maybe x
Files
- README.md +4/−0
- data-diverse.cabal +3/−6
- src/Data/Diverse/Many.hs +3/−32
- src/Data/Diverse/Many/Internal.hs +42/−271
- src/Data/Diverse/Which.hs +3/−0
- src/Data/Diverse/Which/Internal.hs +35/−7
- test/Data/Diverse/ManySpec.hs +10/−121
README.md view
@@ -14,6 +14,10 @@ # Changelog +* 1.3.0.0+ - Removed splitting operations added in 1.2.0.0+ - added 'xxxTag' version of label operations that also automatically untags the field.+ * 1.2.0.3 - PolyKinds for Which - Removed cabal upper bounds
data-diverse.cabal view
@@ -1,5 +1,5 @@ name: data-diverse-version: 1.2.0.3+version: 1.3.0.0 synopsis: Extensible records and polymorphic variants. description: "Data.Diverse.Many" is an extensible record for any size encoded efficiently as (Seq Any). "Data.Diverse.Which" is a polymorphic variant of possibilities encoded as (Int, Any).@@ -20,7 +20,7 @@ build-type: Simple extra-source-files: README.md cabal-version: >=1.10-tested-with: GHC == 8.0.2, GHC == 8.2.1+tested-with: GHC == 8.0.1, GHC == 8.2.2 library hs-source-dirs: src@@ -39,13 +39,10 @@ Data.Diverse.Which Data.Diverse.Which.Internal build-depends: base >= 4.7 && < 5+ , containers >= 0.5 , deepseq >= 1.4 , ghc-prim >= 0.5 , tagged >= 0.8.5- if impl(ghc >= 8.2.0)- build-depends: containers >= 0.5.8.2- else- build-depends: containers >= 0.5 ghc-options: -Wall default-language: Haskell2010
src/Data/Diverse/Many.hs view
@@ -34,12 +34,15 @@ -- ** Getter for single field , fetch , fetchL+ , fetchTag , fetchN -- ** Setter for single field , replace , replace' , replaceL , replaceL'+ , replaceTag+ , replaceTag' , replaceN , replaceN' @@ -73,38 +76,6 @@ , CollectorN , forManyN , collectN-- -- * Splitting operations-- -- * Splitting- , splitBefore- , splitBeforeL- , splitBeforeN- , splitAfter- , splitAfterL- , splitAfterN-- -- * inset multiple items- , insetBefore- , insetBeforeL- , insetBeforeN- , insetAfter- , insetAfterL- , insetAfterN-- -- * insert single item- , insertBefore- , insertBeforeL- , insertBeforeN- , insertAfter- , insertAfterL- , insertAfterN-- -- * Deleting single item- , remove- , removeL- , removeN- ) where import Data.Diverse.Many.Internal
src/Data/Diverse/Many/Internal.hs view
@@ -1,3 +1,5 @@+{-# OPTIONS_GHC -Wno-redundant-constraints #-}+ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE CPP #-}@@ -50,12 +52,15 @@ -- ** Getter for single field , fetch , fetchL+ , fetchTag , fetchN -- ** Setter for single field , replace , replace' , replaceL , replaceL'+ , replaceTag+ , replaceTag' , replaceN , replaceN' @@ -89,38 +94,6 @@ , CollectorN , forManyN , collectN-- -- * Splitting operations-- -- ** Splitting- , splitBefore- , splitBeforeL- , splitBeforeN- , splitAfter- , splitAfterL- , splitAfterN-- -- ** inset multiple items- , insetBefore- , insetBeforeL- , insetBeforeN- , insetAfter- , insetAfterL- , insetAfterN-- -- ** insert single item- , insertBefore- , insertBeforeL- , insertBeforeN- , insertAfter- , insertAfterL- , insertAfterN-- -- ** Deleting single item- , remove- , removeL- , removeN- ) where import Control.Applicative@@ -203,11 +176,6 @@ ----------------------------------------------------------------------- -instance NFData (Many '[])-instance (NFData x, NFData (Many xs)) => NFData (Many (x ': xs))-------------------------------------------------------------------------- -- | A terminating 'G.Generic' instance encoded as a 'nil'. instance G.Generic (Many '[]) where type Rep (Many '[]) = G.U1@@ -440,237 +408,6 @@ -------------------------------------------------- --- | Split a Many into two, where the last type in the first Many is unique @x@-splitAfter_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> (S.Seq Any, S.Seq Any)-splitAfter_ _ xs = let (as, bs) = S.splitAt (i + 1) xs in (as, bs)- where- i = fromInteger (natVal @n Proxy) :: Int---- | Split a Many into two, where the first type in the second Many is unique @x@-splitBefore_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> (S.Seq Any, S.Seq Any)-splitBefore_ _ xs = let (as, bs) = S.splitAt i xs in (as, bs)- where- i = fromInteger (natVal @n Proxy) :: Int---- | Split a Many into two, where the last type in the first Many is unique @x@-splitAfter- :: forall x xs proxy.- (UniqueMember x xs)- => proxy x -> Many xs -> (Many (To x xs), Many (After x xs))-splitAfter _ (Many xs) = let (as, bs) = splitAfter_ (Proxy @(IndexOf x xs)) xs in (Many as, Many bs)---- | Split a Many into two, where the first type in the second Many is unique @x@-splitBefore- :: forall x xs proxy.- (UniqueMember x xs)- => proxy x -> Many xs -> (Many (Before x xs), Many (From x xs))-splitBefore _ (Many xs) = let (as, bs) = splitBefore_ (Proxy @(IndexOf x xs)) xs in (Many as, Many bs)---- | Split a Many into two, where the last type in the first Many is unique label @l@-splitAfterL- :: forall l xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> Many xs -> (Many (To x xs), Many (After x xs))-splitAfterL _ (Many xs) = let (as, bs) = splitAfter_ (Proxy @(IndexOf x xs)) xs in (Many as, Many bs)---- | Split a Many into two, where the first type in the second Many is unique label @l@-splitBeforeL- :: forall l xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> Many xs -> (Many (Before x xs), Many (From x xs))-splitBeforeL _ (Many xs) = let (as, bs) = splitBefore_ (Proxy @(IndexOf x xs)) xs in (Many as, Many bs)---- | Split a Many into two, where the second Many starts at index @(n + 1)@-splitAfterN- :: forall n xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> Many xs -> (Many (ToIndex n xs), Many (AfterIndex n xs))-splitAfterN p (Many xs) = let (as, bs) = splitAfter_ p xs in (Many as, Many bs)---- | Split a Many into two, where the second Many starts at index @n@-splitBeforeN- :: forall n xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> Many xs -> (Many (BeforeIndex n xs), Many (FromIndex n xs))-splitBeforeN p (Many xs) = let (as, bs) = splitBefore_ p xs in (Many as, Many bs)-------------------------------------------------------- | Insert a Many into another Many, inserting after a unique @x@-insetAfter_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> S.Seq Any -> S.Seq Any-insetAfter_ _ ys xs = let (as, bs) = S.splitAt (i + 1) xs in as S.>< ys S.>< bs- where- i = fromInteger (natVal @n Proxy) :: Int---- | Insert a Many into another Many, inserting before a unique @x@-insetBefore_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> S.Seq Any -> S.Seq Any-insetBefore_ _ ys xs = let (as, bs) = S.splitAt i xs in as S.>< ys S.>< bs- where- i = fromInteger (natVal @n Proxy) :: Int---- | Insert a Many into another Many, inserting after a unique @x@-insetAfter- :: forall x ys xs proxy.- (UniqueMember x xs)- => proxy x -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))-insetAfter _ (Many ys) (Many xs) = Many $ insetAfter_ (Proxy @(IndexOf x xs)) ys xs---- | Insert a Many into another Many, inserting before a unique @x@-insetBefore- :: forall x ys xs proxy.- (UniqueMember x xs)- => proxy x -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))-insetBefore _ (Many ys) (Many xs) = Many $ insetBefore_ (Proxy @(IndexOf x xs)) ys xs---- | Insert a Many into another Many, inserting after a unique label @l@-insetAfterL- :: forall l ys xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> Many ys -> Many xs -> Many (Append (To x xs) (Append ys (After x xs)))-insetAfterL _ (Many ys) (Many xs) = Many $ insetAfter_ (Proxy @(IndexOf x xs)) ys xs---- | Insert a Many into another Many, inserting before a unique label @l@-insetBeforeL- :: forall l ys xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> Many ys -> Many xs -> Many (Append (Before x xs) (Append ys (From x xs)))-insetBeforeL _ (Many ys) (Many xs) = Many $ insetBefore_ (Proxy @(IndexOf x xs)) ys xs---- | Insert a Many into another Many, starting at index @(n + 1)@-insetAfterN- :: forall n ys xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> Many ys -> Many xs -> Many (Append (ToIndex n xs) (Append ys (AfterIndex n xs)))-insetAfterN p (Many ys) (Many xs) = Many $ insetAfter_ p ys xs---- | Insert a Many into another Many, starting at index @n@-insetBeforeN- :: forall n ys xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> Many ys -> Many xs -> Many (Append (BeforeIndex n xs) (Append ys (FromIndex n xs)))-insetBeforeN p (Many ys) (Many xs) = Many $ insetBefore_ p ys xs-----------------------------------------------------#if __GLASGOW_HASKELL__ >= 802--- The following uses containers > 0.5.7.1 for insertAt and deleteAt only available after GHC 8.2---- | Insert an item into a Many, inserting after unique type @x@--- | Insert an item into a Many, inserting after unique type @x@-insertAfter_ :: forall n proxy. KnownNat n => proxy n -> Any -> S.Seq Any -> S.Seq Any-insertAfter_ _ y xs = S.insertAt (i + 1) y xs- where- i = fromInteger (natVal @n Proxy) :: Int---- | Insert an item into a Many, inserting before unique type @x@-insertBefore_ :: forall n proxy. KnownNat n => proxy n -> Any -> S.Seq Any -> S.Seq Any-insertBefore_ _ y xs = S.insertAt i y xs- where- i = fromInteger (natVal @n Proxy) :: Int-------------------------------------------------------- | Remove the unique @x@ from a Many.--- Not named 'delete' to avoid conflicts with 'Data.List.delete'-remove_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> S.Seq Any-remove_ _ xs = S.deleteAt i xs- where- i = fromInteger (natVal @n Proxy) :: Int--#else---- Emulate insertAt and deleteAt for GHC < 8.2 && containers <= 0.5.7.1---- | Insert an item into a Many, inserting after unique type @x@-insertAfter_ :: forall n proxy. KnownNat n => proxy n -> Any -> S.Seq Any -> S.Seq Any-insertAfter_ _ y xs = let (as, bs) = S.splitAt (i + 1) xs in (as S.>< (y S.<| bs))- where- i = fromInteger (natVal @n Proxy) :: Int---- | Insert an item into a Many, inserting before unique type @x@-insertBefore_ :: forall n proxy. KnownNat n => proxy n -> Any -> S.Seq Any -> S.Seq Any-insertBefore_ _ y xs = let (as, bs) = S.splitAt i xs in as S.>< (y S.<| bs)- where- i = fromInteger (natVal @n Proxy) :: Int-------------------------------------------------------- | Remove the unique @x@ from a Many.--- Not named 'delete' to avoid conflicts with 'Data.List.delete'-remove_ :: forall n proxy. KnownNat n => proxy n -> S.Seq Any -> S.Seq Any-remove_ _ xs = let (as, bs) = S.splitAt i xs in as S.>< S.drop 1 bs- where- i = fromInteger (natVal @n Proxy) :: Int--#endif---- | Insert an item into a Many, inserting after unique type @x@-insertAfter- :: forall x y xs proxy.- (UniqueMember x xs)- => proxy x -> y -> Many xs -> Many (Append (To x xs) (y ': After x xs))-insertAfter _ y (Many xs) = Many (insertAfter_ (Proxy @(IndexOf x xs)) (unsafeCoerce y) xs)---- | Insert an item into a Many, inserting before unique type @x@-insertBefore- :: forall x y xs proxy.- (UniqueMember x xs)- => proxy x -> y -> Many xs -> Many (Append (Before x xs) (y ': From x xs))-insertBefore _ y (Many xs) = Many $ insertBefore_ (Proxy @(IndexOf x xs)) (unsafeCoerce y) xs---- | Insert an item into a Many, inserting after unique label @l@-insertAfterL- :: forall l y xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> y -> Many xs -> Many (Append (To x xs) (y ': After x xs))-insertAfterL _ y (Many xs) = Many $ insertAfter_ (Proxy @(IndexOf x xs)) (unsafeCoerce y) xs---- | Insert an item into a Many, inserting before unique label @l@-insertBeforeL- :: forall l y xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> y -> Many xs -> Many (Append (Before x xs) (y ': From x xs))-insertBeforeL _ y (Many xs) = Many $ insertBefore_ (Proxy @(IndexOf x xs)) (unsafeCoerce y) xs---- | Insert an item into a Many, inserting after index @n@-insertAfterN- :: forall n y xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> y -> Many xs -> Many (Append (ToIndex n xs) (y ': AfterIndex n xs))-insertAfterN p y (Many xs) = Many $ insertAfter_ p (unsafeCoerce y) xs---- | Insert an item into a Many, inserting before index @n@-insertBeforeN- :: forall n y xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> y -> Many xs -> Many (Append (BeforeIndex n xs) (y ': FromIndex n xs))-insertBeforeN p y (Many xs) = Many $ insertBefore_ p (unsafeCoerce y) xs-------------------------------------------------------- | Remove the unique @x@ from a Many.--- Not named 'delete' to avoid conflicts with 'Data.List.delete'-remove- :: forall x xs proxy.- (UniqueMember x xs)- => proxy x -> Many xs -> Many (Remove x xs)-remove _ (Many xs) = Many $ remove_ (Proxy @(IndexOf x xs)) xs---- | Remove the unique label @l@ from a Many.-removeL- :: forall l xs proxy x.- (UniqueLabelMember l xs, x ~ KindAtLabel l xs)- => proxy l -> Many xs -> Many (Remove x xs)-removeL _ (Many xs) = Many $ remove_ (Proxy @(IndexOf x xs)) xs---- | Remove the @n@-th item from a Many.-removeN- :: forall n xs proxy.- (KnownNat n, n + 1 <= Length xs)- => proxy n -> Many xs -> Many (RemoveIndex n xs)-removeN p (Many xs) = Many $ remove_ p xs----------------------------------------------------- -- | Getter by unique type. Get the field with type @x@. -- -- @@@ -699,6 +436,13 @@ -------------------------------------------------- +-- | Variation of 'fetchL' specialized for 'Tagged' that untags the field.+fetchTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs)+ => proxy l -> Many xs -> x+fetchTag p xs = unTagged (fetchL p xs)++--------------------------------------------------+ -- | Getter by index. Get the value of the field at index type-level Nat @n@ -- -- @@@ -743,7 +487,8 @@ -- 'replaceL' \@\"Hello" Proxy y (Tagged \@\"Hello" 7) \`shouldBe` -- (5 :: Int) './' False './' Tagged \@Foo \'X' './' Tagged \@\"Hello" (7 :: Int) './' 'nil' -- @-replaceL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> x -> Many xs+replaceL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs)+ => proxy l -> Many xs -> x -> Many xs replaceL _ (Many xs) x = Many $ replace_ (Proxy @(IndexOf x xs)) xs (unsafeCoerce x) -- | Polymorphic setter by unique type. Set the field with type @x@, and replace with type @y@@@ -755,11 +500,24 @@ -- replaceL' \@\"Hello" Proxy y (Tagged \@\"Hello" False) \`shouldBe` -- (5 :: Int) './' False './' Tagged \@Foo \'X' './' Tagged \@\"Hello" False './' 'nil' -- @-replaceL' :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> Many xs -> y -> Many (Replace x y xs)-replaceL' _ (Many xs) x = Many $ replace_ (Proxy @(IndexOf x xs)) xs (unsafeCoerce x)+replaceL' :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs)+ => proxy l -> Many xs -> y -> Many (Replace x y xs)+replaceL' _ (Many xs) y = Many $ replace_ (Proxy @(IndexOf x xs)) xs (unsafeCoerce y) -------------------------------------------------- +-- | Variation of 'replaceL' specialized to 'Tagged' that automatically tags the value to be replaced.+replaceTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs)+ => proxy l -> Many xs -> x -> Many xs+replaceTag p xs x = replaceL p xs (Tagged @l x)++-- | Variation of 'replaceL' specialized to 'Tagged' that automatically tags the value to be replaced.+replaceTag' :: forall l y xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs)+ => proxy l -> Many xs -> y -> Many (Replace x (Tagged l y) xs)+replaceTag' p xs y = replaceL' p xs (Tagged @l y)++--------------------------------------------------+ -- | Setter by index. Set the value of the field at index type-level Nat @n@ -- -- @@@ -1372,6 +1130,19 @@ xs <- readPrec @(Many_ xs) pure $ fromMany_ xs +-----------------------------------------------------------------------++instance NFData (Many '[]) where+ rnf _ = ()+++instance (NFData x, NFData (Many xs)) => NFData (Many (x ': xs)) where+ rnf xs = rnf (front xs) `seq` rnf (aft xs)++-----------------------------------------------------------------------+ -- | 'WrappedAny' avoids the following: -- Illegal type synonym family application in instance: Any newtype WrappedAny = WrappedAny Any++-----------------------------------------------------------------------
src/Data/Diverse/Which.hs view
@@ -10,6 +10,7 @@ , pick0 , pickOnly , pickL+ , pickTag , pickN -- ** Destruction , obvious@@ -19,6 +20,8 @@ , trial0' , trialL , trialL'+ , trialTag+ , trialTag' , trialN , trialN'
src/Data/Diverse/Which/Internal.hs view
@@ -1,3 +1,5 @@+{-# OPTIONS_GHC -Wno-redundant-constraints #-}+ {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE EmptyCase #-}@@ -26,6 +28,7 @@ , pick0 , pickOnly , pickL+ , pickTag , pickN -- ** Destruction , obvious@@ -35,6 +38,8 @@ , trial0' , trialL , trialL'+ , trialTag+ , trialTag' , trialN , trialN' @@ -72,12 +77,14 @@ import Control.DeepSeq import Control.Monad import Data.Diverse.Case+import Data.Diverse.CaseFunc import Data.Diverse.Reduce import Data.Diverse.Reiterate import Data.Diverse.TypeLevel import Data.Kind import Data.Proxy import Data.Semigroup (Semigroup(..))+import Data.Tagged import qualified GHC.Generics as G import GHC.Exts (Any, coerce) import GHC.TypeLits@@ -122,12 +129,6 @@ ---------------------------------------------- -instance NFData (Which '[])-instance (NFData x) => NFData (Which '[x])-instance (NFData x, NFData (Which (x' ': xs))) => NFData (Which (x ': x' ': xs))------------------------------------------------- -- | A terminating 'G.Generic' instance for no types encoded as a 'zilch'. -- The 'G.C1' and 'G.S1' metadata are not encoded. instance G.Generic (Which '[]) where@@ -190,9 +191,15 @@ -- x = 'trialL' \@Foo Proxy y -- x `shouldBe` (Right (Tagged 5)) -- @-pickL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l -> x -> Which xs+pickL :: forall l xs proxy x. (UniqueLabelMember l xs, x ~ KindAtLabel l xs)+ => proxy l -> x -> Which xs pickL _ = pick_ @x +-- | Variation of 'pickL' specialized to 'Tagged' that automatically tags the value.+pickTag :: forall l xs proxy x. (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs)+ => proxy l -> x -> Which xs+pickTag p a = pickL p (Tagged @l a)+ -- | A variation of 'pick' into a 'Which' of a single type. -- -- @@@ -291,6 +298,12 @@ then Just (unsafeCoerce v) else Nothing +-- | Variation of 'trialL' specialized to 'Tagged' which untags the field.+trialTag+ :: forall l xs proxy x.+ (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs)+ => proxy l -> Which xs -> Either (Which (Remove (Tagged l x) xs)) x+trialTag p xs = unTagged <$> trialL p xs -- | Variation of 'trialN' which returns a Maybe trialN'@@ -316,6 +329,13 @@ => proxy l -> Which xs -> Maybe x trialL' _ = trial_' @_ @x +-- | Variation of 'trialL'' specialized to 'Tagged' which untags the field.+trialTag'+ :: forall l xs proxy x.+ (UniqueLabelMember l xs, Tagged l x ~ KindAtLabel l xs)+ => proxy l -> Which xs -> Maybe x+trialTag' p xs = unTagged <$> trialL' p xs+ -- | A variation of a 'Which' 'trial' which 'trial's the first type in the type list. -- -- @@@ -816,3 +836,11 @@ -- 'Which '[]' as a data type with no constructors. instance Read (Which '[]) where readsPrec _ _ = []++------------------------------------------------------------------+instance NFData (Which '[]) where+ rnf = impossible++instance (Reduce (Which (x ': xs)) (Switcher (CaseFunc NFData) () (x ': xs))) =>+ NFData (Which (x ': xs)) where+ rnf x = switch x (CaseFunc @NFData rnf)
test/Data/Diverse/ManySpec.hs view
@@ -26,11 +26,20 @@ data Foo data Bar-data Dee spec :: Spec spec = do describe "Many" $ do++ -- it "Test user friendly compile errors" $ do+ -- let y = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ -- ghc 8.0.2: IndexOf error: ‘Maybe Bool’ is not a member of ...+ -- ghc 8.0.1 has terrible error message: "No instance for (GHC.TypeLits.KnownNat"+ -- fetch @(Maybe Bool) y `shouldBe` (Just False)++ -- Not unique error: ‘Maybe Char’ is a duplicate in ...+ -- fetch @(Maybe Bool) y `shouldBe` (Just False)+ it "is a Typeable" $ do let x = (5 :: Int) ./ False ./ nil y = cast x :: Maybe (Many '[Int, String])@@ -323,123 +332,3 @@ z = ("5" :: String) ./ ("6" :: String) ./ ("7" :: String) ./ ("8" :: String) ./ nil afmap (CaseFunc' @Num (+10)) x `shouldBe` y afmap (CaseFunc @Show @String show) x `shouldBe` z-- it "can be split into two 'Many's" $ do- let x = (5 :: Int) ./ False ./ 'X' ./ Just True ./ Just 'A' ./ nil- y = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- x1a = (5 :: Int) ./ False ./ nil- x1b = 'X' ./ Just True ./ Just 'A' ./ nil- x2a = (5 :: Int) ./ False ./ 'X' ./ nil- x2b = Just True ./ Just 'A' ./ nil- y1a = (Tagged 5 :: Tagged Foo Int) ./ False ./ nil- y1b = Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- y2a = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ nil- y2b = Just True ./ Tagged @Bar (Just 'A') ./ nil-- splitBefore (Proxy @Int) x `shouldBe` (nil, x)- splitBeforeL (Proxy @Foo) y `shouldBe` (nil, y)- splitBeforeN (Proxy @0) x `shouldBe` (nil, x)- splitAfter (Proxy @Int) x `shouldBe` let (a, b) = viewf x in (single a, b)- splitAfterL (Proxy @Foo) y `shouldBe` let (a, b) = viewf y in (single a, b)- splitAfterN (Proxy @0) x `shouldBe` let (a, b) = viewf x in (single a, b)-- splitBefore (Proxy @(Maybe Char)) x `shouldBe` let (a, b) = viewb x in (a, single b)- splitBeforeL (Proxy @Bar) y `shouldBe` let (a, b) = viewb y in (a, single b)- splitBeforeN (Proxy @4) x `shouldBe` let (a, b) = viewb x in (a, single b)- splitAfter (Proxy @(Maybe Char)) x `shouldBe` (x, nil)- splitAfterL (Proxy @Bar) y `shouldBe` (y, nil)- splitAfterN (Proxy @4) x `shouldBe` (x, nil)-- splitBefore (Proxy @(Char)) x `shouldBe` (x1a, x1b)- splitBeforeL (Proxy @Dee) y `shouldBe` (y1a, y1b)- splitBeforeN (Proxy @2) x `shouldBe` (x1a, x1b)- splitAfter(Proxy @(Char)) x `shouldBe` (x2a, x2b)- splitAfterL (Proxy @Dee) y `shouldBe` (y2a, y2b)- splitAfterN (Proxy @2) x `shouldBe` (x2a, x2b)-- it "can be 'inset'ted into another 'Many'" $ do- let x = (5 :: Int) ./ False ./ 'X' ./ Just True ./ Just 'A' ./ nil- y = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- x1a = (5 :: Int) ./ False ./ nil- x1b = 'X' ./ Just True ./ Just 'A' ./ nil- x2a = (5 :: Int) ./ False ./ 'X' ./ nil- x2b = Just True ./ Just 'A' ./ nil- y1a = (Tagged 5 :: Tagged Foo Int) ./ False ./ nil- y1b = Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- y2a = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ nil- y2b = Just True ./ Tagged @Bar (Just 'A') ./ nil- z = True ./ 'Y' ./ nil-- insetBefore (Proxy @Int) z x `shouldBe` z /./ x- insetBeforeL (Proxy @Foo) z y `shouldBe` z /./ y- insetBeforeN (Proxy @0) z x `shouldBe` z /./ x- insetAfter (Proxy @Int) z x `shouldBe` let (a, b) = viewf x in a ./ z /./ b- insetAfterL (Proxy @Foo) z y `shouldBe` let (a, b) = viewf y in a ./ z /./ b- insetAfterN (Proxy @0) z x `shouldBe` let (a, b) = viewf x in a ./ z /./ b-- insetBefore (Proxy @(Maybe Char)) z x `shouldBe` let (a, b) = viewb x in a /./ (z \. b)- insetBeforeL (Proxy @Bar) z y `shouldBe` let (a, b) = viewb y in a /./ (z \. b)- insetBeforeN (Proxy @4) z x `shouldBe` let (a, b) = viewb x in a /./ (z \. b)- insetAfter (Proxy @(Maybe Char)) z x `shouldBe` x /./ z- insetAfterL (Proxy @Bar) z y `shouldBe` y /./ z- insetAfterN (Proxy @4) z x `shouldBe` x /./ z-- insetBefore (Proxy @(Char)) z x `shouldBe` x1a /./ z /./ x1b- insetBeforeL (Proxy @Dee) z y `shouldBe` y1a /./ z /./ y1b- insetBeforeN (Proxy @2) z x `shouldBe` x1a /./ z /./ x1b- insetAfter(Proxy @(Char)) z x `shouldBe` x2a /./ z /./ x2b- insetAfterL (Proxy @Dee) z y `shouldBe` y2a /./ z /./ y2b- insetAfterN (Proxy @2) z x `shouldBe` x2a /./ z /./ x2b-- it "can be 'insert'ted with an item at a specific place" $ do- let x = (5 :: Int) ./ False ./ 'X' ./ Just True ./ Just 'A' ./ nil- y = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- x1a = (5 :: Int) ./ False ./ nil- x1b = 'X' ./ Just True ./ Just 'A' ./ nil- x2a = (5 :: Int) ./ False ./ 'X' ./ nil- x2b = Just True ./ Just 'A' ./ nil- y1a = (Tagged 5 :: Tagged Foo Int) ./ False ./ nil- y1b = Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- y2a = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ nil- y2b = Just True ./ Tagged @Bar (Just 'A') ./ nil- z = True-- insertBefore (Proxy @Int) z x `shouldBe` z ./ x- insertBeforeL (Proxy @Foo) z y `shouldBe` z ./ y- insertBeforeN (Proxy @0) z x `shouldBe` z ./ x- insertAfter (Proxy @Int) z x `shouldBe` let (a, b) = viewf x in a ./ single z /./ b- insertAfterL (Proxy @Foo) z y `shouldBe` let (a, b) = viewf y in a ./ single z /./ b- insertAfterN (Proxy @0) z x `shouldBe` let (a, b) = viewf x in a ./ single z /./ b-- insertBefore (Proxy @(Maybe Char)) z x `shouldBe` let (a, b) = viewb x in a /./ (single z \. b)- insertBeforeL (Proxy @Bar) z y `shouldBe` let (a, b) = viewb y in a /./ (single z \. b)- insertBeforeN (Proxy @4) z x `shouldBe` let (a, b) = viewb x in a /./ (single z \. b)- insertAfter (Proxy @(Maybe Char)) z x `shouldBe` x /./ single z- insertAfterL (Proxy @Bar) z y `shouldBe` y /./ single z- insertAfterN (Proxy @4) z x `shouldBe` x /./ single z-- insertBefore (Proxy @(Char)) z x `shouldBe` x1a /./ single z /./ x1b- insertBeforeL (Proxy @Dee) z y `shouldBe` y1a /./ single z /./ y1b- insertBeforeN (Proxy @2) z x `shouldBe` x1a /./ single z /./ x1b- insertAfter(Proxy @(Char)) z x `shouldBe` x2a /./ single z /./ x2b- insertAfterL (Proxy @Dee) z y `shouldBe` y2a /./ single z /./ y2b- insertAfterN (Proxy @2) z x `shouldBe` x2a /./ single z /./ x2b--- it "can 'remove' an item at a specific place" $ do- let x = (5 :: Int) ./ False ./ 'X' ./ Just True ./ Just 'A' ./ nil- y = (Tagged 5 :: Tagged Foo Int) ./ False ./ Tagged @Dee 'X' ./ Just True ./ Tagged @Bar (Just 'A') ./ nil- x' = (5 :: Int) ./ False ./ Just True ./ Just 'A' ./ nil- y' = (Tagged 5 :: Tagged Foo Int) ./ False ./ Just True ./ Tagged @Bar (Just 'A') ./ nil-- remove (Proxy @Int) x `shouldBe` snd (viewf x)- removeL (Proxy @Foo) y `shouldBe` snd (viewf y)- removeN (Proxy @0) x `shouldBe` snd (viewf x)-- remove (Proxy @(Maybe Char)) x `shouldBe` fst (viewb x)- removeL (Proxy @Bar) y `shouldBe` fst (viewb y)- removeN (Proxy @4) x `shouldBe` fst (viewb x)-- remove (Proxy @(Char)) x `shouldBe` x'- removeL (Proxy @Dee) y `shouldBe` y'- removeN (Proxy @2) x `shouldBe` x'