apart 0.1.1 → 0.1.3
raw patch · 24 files changed
+216/−151 lines, 24 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Apart: Converted :: raw -> Shape t raw value
- Data.Apart: Ready :: (t value) -> Shape t raw value
- Data.Apart: data Shape t raw value
- Data.Apart: inmemory :: (Functor t, Alternative t) => Apart t raw value -> Cofree t value
- Data.Apart: limit :: (Traversable t, Applicative g) => Int -> Materializer g t raw value -> Cofree t value -> g (Scattered (Cofree t) value raw)
- Data.Apart: recover :: (Traversable t, Applicative g) => Restorer g t raw value -> Scattered (Cofree t) value raw -> g (Cofree t value)
- Data.Apart: throughout :: (Traversable t, Monad g) => (value -> g result) -> Restorer g t raw value -> (Scattered (Cofree t) value raw) -> g (Cofree t result)
- Data.Apart: type Materializer g t raw value = (Traversable t, Applicative g) => Segment (Cofree t) value -> g raw
- Data.Apart: type Restorer g t raw value = (Traversable t, Applicative g) => raw -> g (Segment (Cofree t) value)
- Data.Apart.Structures.Graph: Single :: a -> Edge a
- Data.Apart.Structures.Graph: data Edge a
- Data.Apart.Structures.Graph: instance Data.Foldable.Foldable Data.Apart.Structures.Graph.Edge
- Data.Apart.Structures.Graph: instance Data.Traversable.Traversable Data.Apart.Structures.Graph.Edge
- Data.Apart.Structures.Graph: instance GHC.Base.Functor Data.Apart.Structures.Graph.Edge
- Data.Apart.Structures.Graph: instance GHC.Show.Show a => GHC.Show.Show (Data.Apart.Structures.Graph.Edge a)
- Data.Apart.Structures.Tree.Binary: instance Data.Semigroup.Semigroup (Data.Apart.Structures.Tree.Binary.Branches a)
- Data.Apart.Structures.Tree.Prefix: instance forall s k (t :: k -> *) (a :: k). (GHC.Show.Show (t a), GHC.Show.Show s) => GHC.Show.Show (Data.Apart.Structures.Tree.Prefix.Labeled s t a)
+ Data.Apart: instance Data.Foldable.Foldable t => Data.Bifoldable.Bifoldable (Data.Apart.Apart t)
+ Data.Apart: instance Data.Functor.Bind.Class.Apply t => Data.Functor.Bind.Class.Apply (Data.Apart.Apart t raw)
+ Data.Apart: instance Data.Traversable.Traversable t => Data.Bitraversable.Bitraversable (Data.Apart.Apart t)
+ Data.Apart: instance GHC.Base.Functor t => Data.Bifunctor.Bifunctor (Data.Apart.Apart t)
+ Data.Apart: instance GHC.Base.Functor t => GHC.Base.Functor (Data.Apart.Apart t raw)
+ Data.Apart.Combinators: inmemory :: (Functor t, Alternative t) => Apart t raw value -> Cofree t value
+ Data.Apart.Combinators: limit :: (Traversable t, Applicative g) => Int -> Materializer g t raw value -> Cofree t value -> g (Scattered (Cofree t) value raw)
+ Data.Apart.Combinators: recover :: (Traversable t, Applicative g) => Restorer g t raw value -> Scattered (Cofree t) value raw -> g (Cofree t value)
+ Data.Apart.Combinators: throughout :: (Traversable t, Monad g) => (value -> g result) -> Restorer g t raw value -> (Scattered (Cofree t) value raw) -> g (Cofree t result)
+ Data.Apart.Combinators: type Materializer g t raw value = (Traversable t, Applicative g) => Segmented (Cofree t) value -> g raw
+ Data.Apart.Combinators: type Restorer g t raw value = (Traversable t, Applicative g) => raw -> g (Segmented (Cofree t) value)
+ Data.Apart.Shape: Converted :: raw -> Shape t raw value
+ Data.Apart.Shape: Ready :: (t value) -> Shape t raw value
+ Data.Apart.Shape: data Shape t raw value
+ Data.Apart.Shape: instance (GHC.Show.Show (t value), GHC.Show.Show value, GHC.Show.Show raw) => GHC.Show.Show (Data.Apart.Shape.Shape t raw value)
+ Data.Apart.Shape: instance Data.Foldable.Foldable t => Data.Bifoldable.Bifoldable (Data.Apart.Shape.Shape t)
+ Data.Apart.Shape: instance Data.Foldable.Foldable t => Data.Foldable.Foldable (Data.Apart.Shape.Shape t raw)
+ Data.Apart.Shape: instance Data.Functor.Alt.Alt t => Data.Functor.Alt.Alt (Data.Apart.Shape.Shape t raw)
+ Data.Apart.Shape: instance Data.Functor.Bind.Class.Apply t => Data.Functor.Bind.Class.Apply (Data.Apart.Shape.Shape t raw)
+ Data.Apart.Shape: instance Data.Traversable.Traversable t => Data.Bitraversable.Bitraversable (Data.Apart.Shape.Shape t)
+ Data.Apart.Shape: instance Data.Traversable.Traversable t => Data.Traversable.Traversable (Data.Apart.Shape.Shape t raw)
+ Data.Apart.Shape: instance GHC.Base.Functor t => Data.Bifunctor.Bifunctor (Data.Apart.Shape.Shape t)
+ Data.Apart.Shape: instance GHC.Base.Functor t => GHC.Base.Functor (Data.Apart.Shape.Shape t raw)
+ Data.Apart.Structures.Dictionary: Association :: value -> key -> Association value key
+ Data.Apart.Structures.Dictionary: data Association value key
+ Data.Apart.Structures.Dictionary: instance Data.Foldable.Foldable (Data.Apart.Structures.Dictionary.Association value)
+ Data.Apart.Structures.Dictionary: instance Data.Traversable.Traversable (Data.Apart.Structures.Dictionary.Association value)
+ Data.Apart.Structures.Dictionary: instance GHC.Base.Functor (Data.Apart.Structures.Dictionary.Association value)
+ Data.Apart.Structures.Dictionary: type Dictionary value = Cofree (Association value :.: Maybe)
+ Data.Apart.Structures.Graph: data Edges a
+ Data.Apart.Structures.Graph: instance Data.Foldable.Foldable Data.Apart.Structures.Graph.Edges
+ Data.Apart.Structures.Graph: instance Data.Traversable.Traversable Data.Apart.Structures.Graph.Edges
+ Data.Apart.Structures.Graph: instance GHC.Base.Functor Data.Apart.Structures.Graph.Edges
+ Data.Apart.Structures.Graph: instance GHC.Show.Show a => GHC.Show.Show (Data.Apart.Structures.Graph.Edges a)
+ Data.Apart.Structures.Tree.Binary: instance GHC.Base.Semigroup (Data.Apart.Structures.Tree.Binary.Branches a)
+ Data.Apart.Structures.Tree.Binary.Redblack: Black :: Color
+ Data.Apart.Structures.Tree.Binary.Redblack: Red :: Color
+ Data.Apart.Structures.Tree.Binary.Redblack: data Color
+ Data.Apart.Structures.Tree.Binary.Redblack: instance GHC.Show.Show Data.Apart.Structures.Tree.Binary.Redblack.Color
+ Data.Apart.Structures.Tree.Binary.Redblack: paint :: Binary a -> Redblack a
+ Data.Apart.Structures.Tree.Binary.Redblack: type Redblack a = Binary (a, Color)
+ Data.Apart.Structures.Tree.Prefix: instance forall s k (t :: k -> *) (a :: k). (GHC.Show.Show s, GHC.Show.Show (t a)) => GHC.Show.Show (Data.Apart.Structures.Tree.Prefix.Labeled s t a)
- Data.Apart.Structures.Graph: Connect :: a -> Edge a
+ Data.Apart.Structures.Graph: Connect :: a -> Edges a
- Data.Apart.Structures.Graph: Empty :: Edge a
+ Data.Apart.Structures.Graph: Empty :: Edges a
- Data.Apart.Structures.Graph: Overlay :: a -> Edge a
+ Data.Apart.Structures.Graph: Overlay :: a -> Edges a
- Data.Apart.Structures.Graph: isolated :: Foldable t => t a -> Segment Graph a
+ Data.Apart.Structures.Graph: isolated :: Foldable t => t a -> Segmented Graph a
- Data.Apart.Structures.Graph: remove :: Eq a => a -> Cofree Edge a -> Edge (Cofree Edge a)
+ Data.Apart.Structures.Graph: remove :: Eq a => a -> Graph a -> Segmented Graph a
- Data.Apart.Structures.Graph: type Graph = Cofree Edge
+ Data.Apart.Structures.Graph: type Graph = Cofree Edges
- Data.Apart.Structures.Stack: foldaway :: Foldable t => t a -> Segment Stack a
+ Data.Apart.Structures.Stack: foldaway :: Foldable t => t a -> Segmented Stack a
- Data.Apart.Structures.Tree.Binary: gt :: Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary: gt :: Binary a -> Segmented Binary a
- Data.Apart.Structures.Tree.Binary: ls :: Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary: ls :: Binary a -> Segmented Binary a
- Data.Apart.Structures.Tree.Binary.AVL: insert :: Ord a => a -> Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary.AVL: insert :: Ord a => a -> Binary a -> Segmented Binary a
- Data.Apart.Structures.Tree.Binary.Rotation: rtt :: Rotate -> Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary.Rotation: rtt :: Rotate -> Binary a -> Segmented Binary a
- Data.Apart.Structures.Tree.Binary.Splay: insert :: Ord a => a -> Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary.Splay: insert :: Ord a => a -> Binary a -> Segmented Binary a
- Data.Apart.Structures.Tree.Binary.Splay: search :: Eq a => a -> Binary a -> Segment Binary a
+ Data.Apart.Structures.Tree.Binary.Splay: search :: Eq a => a -> Binary a -> Segmented Binary a
- Data.Apart.Usage.LRU: type LRU a = Segment Binary a
+ Data.Apart.Usage.LRU: type LRU a = Segmented Binary a
Files
- Data/Apart.hs +35/−6
- Data/Apart/Apart.hs +0/−43
- Data/Apart/Combinators.hs +8/−6
- Data/Apart/Machinery/Moore.hs +1/−1
- Data/Apart/Shape.hs +6/−6
- Data/Apart/Structures/Dictionary.hs +19/−0
- Data/Apart/Structures/Graph.hs +18/−16
- Data/Apart/Structures/Stack.hs +4/−4
- Data/Apart/Structures/Stream.hs +2/−2
- Data/Apart/Structures/Tree/Binary.hs +8/−8
- Data/Apart/Structures/Tree/Binary/AVL.hs +9/−8
- Data/Apart/Structures/Tree/Binary/Redblack.hs +20/−0
- Data/Apart/Structures/Tree/Binary/Rotation.hs +7/−7
- Data/Apart/Structures/Tree/Binary/Splay.hs +11/−9
- Data/Apart/Structures/Tree/Prefix.hs +11/−11
- Data/Apart/Structures/Tree/Rose.hs +2/−2
- Data/Apart/Structures/Tree/T23.hs +1/−1
- Data/Apart/Structures/Tree/T234.hs +1/−1
- Data/Apart/Transformations.hs +24/−0
- Data/Apart/Usage/Blockchain.hs +6/−4
- Data/Apart/Usage/LRU.hs +5/−5
- Example/Main.hs +7/−4
- Test/Apart.hs +2/−2
- apart.cabal +9/−5
Data/Apart.hs view
@@ -14,10 +14,39 @@ -- A simple introduction to this library can be found here: https://iokasimov.github.io/posts/2018/05/cofree-will-tear-us-apart ---------------------------------------------------------------------------- -module Data.Apart- ( module Data.Apart.Apart- , module Data.Apart.Combinators- ) where+module Data.Apart (Apart (..)) where -import Data.Apart.Apart-import Data.Apart.Combinators+import "base" Data.Bifoldable (Bifoldable (..))+import "base" Data.Bifunctor (Bifunctor (..))+import "base" Data.Bitraversable (Bitraversable (..))+import "free" Control.Comonad.Cofree (Cofree (..))+import "semigroupoids" Data.Functor.Apply (Apply (..))++import Data.Apart.Shape (Shape (..))++-- | Structure with scattered segments.+newtype Apart t raw value = Apart+ { part :: (Cofree (Shape t raw) value) }++instance Functor t => Functor (Apart t raw) where+ fmap f (Apart structure) = Apart $ f <$> structure++instance Apply t => Apply (Apart t raw) where+ Apart fs <.> Apart structure = Apart $ fs <.> structure++instance Functor t => Bifunctor (Apart t) where+ bimap g f (Apart (x :< Ready values)) = Apart $+ f x :< Ready (part . bimap g f . Apart <$> values)+ bimap g f (Apart (x :< Converted raw)) = Apart $+ f x :< (Converted $ g raw)++instance Foldable t => Bifoldable (Apart t) where+ bifoldr g f acc (Apart (x :< Ready values)) = f x $+ foldr (\st a -> bifoldr g f a $ Apart st) acc values+ bifoldr g f acc (Apart (x :< Converted raw)) = f x $ g raw acc++instance Traversable t => Bitraversable (Apart t) where+ bitraverse g f (Apart (x :< Ready values)) = (<$>) Apart $ (:<) <$> f x <*>+ (Ready <$> traverse ((<$>) part . bitraverse g f . Apart) values)+ bitraverse g f (Apart (x :< Converted raw)) = (<$>) Apart $+ (:<) <$> f x <*> (Converted <$> g raw)
− Data/Apart/Apart.hs
@@ -1,43 +0,0 @@-module Data.Apart.Apart (Apart (..), Shape (..), Segment (..), Scattered (..)) where--import Control.Comonad.Cofree (Cofree (..))-import Data.Bifoldable (Bifoldable (..))-import Data.Bifunctor (Bifunctor (..))-import Data.Bitraversable (Bitraversable (..))-import Data.Functor.Apply (Apply (..))-import Data.Kind (Type)--import Data.Apart.Shape (Shape (..))---- | Structure with scattered segments.-newtype Apart t raw value = Apart- { part :: (Cofree (Shape t raw) value) }--instance Functor t => Functor (Apart t raw) where- fmap f (Apart structure) = Apart $ f <$> structure--instance Apply t => Apply (Apart t raw) where- Apart fs <.> Apart structure = Apart $ fs <.> structure--instance Functor t => Bifunctor (Apart t) where- bimap g f (Apart (x :< Ready values)) = Apart $- f x :< Ready (part . bimap g f . Apart <$> values)- bimap g f (Apart (x :< Converted raw)) = Apart $- f x :< (Converted $ g raw)--instance Foldable t => Bifoldable (Apart t) where- bifoldr g f acc (Apart (x :< Ready values)) = f x $- foldr (\st a -> bifoldr g f a $ Apart st) acc values- bifoldr g f acc (Apart (x :< Converted raw)) = f x $ g raw acc--instance Traversable t => Bitraversable (Apart t) where- bitraverse g f (Apart (x :< Ready values)) = (<$>) Apart $ (:<) <$> f x <*>- (Ready <$> traverse ((<$>) part . bitraverse g f . Apart) values)- bitraverse g f (Apart (x :< Converted raw)) = (<$>) Apart $- (:<) <$> f x <*> (Converted <$> g raw)--type family Segment (structure :: Type -> Type) (value :: Type) :: Type where- Segment (Cofree t) value = t (Cofree t value)--type family Scattered (structure :: Type -> Type) (value :: Type) (raw :: Type) :: Type where- Scattered (Cofree t) value raw = Apart t raw value
Data/Apart/Combinators.hs view
@@ -1,18 +1,20 @@ module Data.Apart.Combinators (Restorer, Materializer, recover, limit, throughout, inmemory) where -import Control.Applicative (Alternative (..))-import Control.Comonad.Cofree (Cofree (..))-import Control.Monad (join)+import "base" Control.Applicative (Alternative (..))+import "base" Control.Monad (join)+import "free" Control.Comonad.Cofree (Cofree (..)) -import Data.Apart.Apart (Apart (..), Shape (..), Scattered (..), Segment (..))+import Data.Apart (Apart (..))+import Data.Apart.Shape (Shape (..))+import Data.Apart.Transformations (Segmented (..), Scattered (..)) -- | Pull back segment of values to memory. type Restorer g t raw value = (Traversable t, Applicative g) =>- raw -> g (Segment (Cofree t) value)+ raw -> g (Segmented (Cofree t) value) -- | Put in-memory values to somewhere else. type Materializer g t raw value = (Traversable t, Applicative g) =>- Segment (Cofree t) value -> g raw+ Segmented (Cofree t) value -> g raw -- | Do nothing with in-memory part, pull back all values of structure to memory. recover :: (Traversable t, Applicative g) => Restorer g t raw value
Data/Apart/Machinery/Moore.hs view
@@ -1,6 +1,6 @@ module Data.Apart.Machinery.Moore (Moore, dumb) where -import Control.Comonad.Cofree (Cofree (..))+import "free" Control.Comonad.Cofree (Cofree (..)) type Moore a b = Cofree ((->) b) a
Data/Apart/Shape.hs view
@@ -1,11 +1,11 @@ module Data.Apart.Shape (Shape (..)) where -import Data.Bifoldable (Bifoldable (..))-import Data.Bifunctor (Bifunctor (..))-import Data.Bitraversable (Bitraversable (..))-import Data.Functor.Apply (Apply (..))-import Data.Functor.Alt (Alt (..))-import Data.Semigroup (Semigroup (..))+import "base" Data.Bifoldable (Bifoldable (..))+import "base" Data.Bifunctor (Bifunctor (..))+import "base" Data.Bitraversable (Bitraversable (..))+import "base" Data.Semigroup (Semigroup (..))+import "semigroupoids" Data.Functor.Apply (Apply (..))+import "semigroupoids" Data.Functor.Alt (Alt (..)) -- | Type that can tell you about aggregate state of your structure. data Shape t raw value
+ Data/Apart/Structures/Dictionary.hs view
@@ -0,0 +1,19 @@+module Data.Apart.Structures.Dictionary (Dictionary, Association (..)) where++import "base" Data.Functor.Compose (Compose)+import "free" Control.Comonad.Cofree (Cofree (..))++type Dictionary value = Cofree (Association value :.: Maybe)++type (:.:) = Compose++data Association value key = Association value key++instance Functor (Association value) where+ fmap f (Association value key) = Association value $ f key++instance Foldable (Association value) where+ foldr f acc (Association value key) = f key acc++instance Traversable (Association value) where+ traverse f (Association value key) = Association value <$> f key
Data/Apart/Structures/Graph.hs view
@@ -1,46 +1,48 @@-module Data.Apart.Structures.Graph (Graph, Edge (..), isolated, star, remove) where+module Data.Apart.Structures.Graph (Graph, Edges (..), isolated, star, remove) where -import Control.Comonad.Cofree (Cofree (..), unwrap)-import Control.Comonad (Comonad (..))+import "free" Control.Comonad.Cofree (Cofree (..), unwrap)+import "comonad" Control.Comonad (Comonad (..)) -import Data.Apart.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..)) -- | Directed acyclic graph.-type Graph = Cofree Edge+type Graph = Cofree Edges -data Edge a = Empty | Single a | Connect a | Overlay a deriving Show+data Edges a = Empty | Connect a | Overlay a deriving Show -instance Functor Edge where+instance Functor Edges where fmap f Empty = Empty- fmap f (Single x) = Single $ f x fmap f (Connect x) = Connect $ f x fmap f (Overlay x) = Overlay $ f x -instance Foldable Edge where+instance Foldable Edges where foldr f acc Empty = acc- foldr f acc (Single x) = f x acc foldr f acc (Connect x) = f x acc foldr f acc (Overlay x) = f x acc -instance Traversable Edge where+instance Traversable Edges where traverse f Empty = pure Empty- traverse f (Single x) = Connect <$> f x traverse f (Connect x) = Connect <$> f x traverse f (Overlay x) = Overlay <$> f x -single, connect, overlay, empty :: Segment Graph a -> Segment Graph a-single = foldr (\x _ -> Single x) Empty+connect, overlay, empty :: Segmented Graph a -> Segmented Graph a connect = foldr (\x _ -> Connect x) Empty overlay = foldr (\x _ -> Overlay x) Empty empty = const Empty -isolated :: Foldable t => t a -> Segment Graph a+isolated :: Foldable t => t a -> Segmented Graph a isolated = foldr (\el -> Overlay . (:<) el) Empty star :: Foldable t => a -> t a -> Graph a star x structure = x :< connect (isolated structure) -- | Remove vertex and all of its edges.-remove :: Eq a => a -> Cofree Edge a -> Edge (Cofree Edge a)+remove :: Eq a => a -> Graph a -> Segmented Graph a remove x graph@((==) x . extract -> True) = overlay $ unwrap graph remove x graph@(y :< segment) = ((:<) y . overlay . remove x) <$> segment++-- Take a degree of focused value+degree :: Graph a -> Int+degree (x :< Empty) = 0+degree (x :< Overlay xs) = 0+degree (x :< Connect xs) = length xs
Data/Apart/Structures/Stack.hs view
@@ -1,10 +1,10 @@ module Data.Apart.Structures.Stack (Stack, insert, singleton, foldaway, final) where -import Control.Comonad.Cofree (Cofree (..), unwrap)-import Data.Functor.Contravariant (Predicate (..))+import "contravariant" Data.Functor.Contravariant (Predicate (..))+import "free" Control.Comonad.Cofree (Cofree (..), unwrap) -import Data.Apart.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..)) -- | Or non-empty list. type Stack = Cofree Maybe@@ -18,7 +18,7 @@ -- when I understand how to use partially applied -- type families correctly, it can be rewritten -- slightly as natural transformation-foldaway :: Foldable t => t a -> Segment Stack a+foldaway :: Foldable t => t a -> Segmented Stack a foldaway = foldr (\el -> Just . (:<) el) Nothing final :: Eq a => Predicate (Stack a)
Data/Apart/Structures/Stream.hs view
@@ -1,7 +1,7 @@ module Data.Apart.Structures.Stream (Stream, same) where -import Control.Comonad.Cofree (Cofree (..))-import Data.Functor.Identity (Identity (..))+import "base" Data.Functor.Identity (Identity (..))+import "free" Control.Comonad.Cofree (Cofree (..)) -- | Infinite sequence. type Stream = Cofree Identity
Data/Apart/Structures/Tree/Binary.hs view
@@ -1,13 +1,13 @@ module Data.Apart.Structures.Tree.Binary (Binary, Branches (..), ls, gt, singleton, insert, height, factor) where -import Control.Comonad.Cofree (Cofree (..))-import Data.Functor.Apply (Apply (..))-import Data.Functor.Alt (Alt (..))-import Data.Functor.Bind (Bind (..))-import Data.Semigroup (Semigroup (..))+import "base" Data.Semigroup (Semigroup (..))+import "free" Control.Comonad.Cofree (Cofree (..))+import "semigroupoids" Data.Functor.Apply (Apply (..))+import "semigroupoids" Data.Functor.Alt (Alt (..))+import "semigroupoids" Data.Functor.Bind (Bind (..)) -import Data.Apart.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..)) type Binary = Cofree Branches @@ -75,13 +75,13 @@ traverse f (Branches l g) = Branches <$> f l <*> f g -- | Get @x@ from @Branches x y@ or from @Less x@.-ls :: Binary a -> Segment Binary a+ls :: Binary a -> Segmented Binary a ls (_ :< Less x) = Less x ls (_ :< Branches x _) = Less x ls (_ :< _) = End -- | Get @y@ from @Branches x y@ or from @Greater y@.-gt :: Binary a -> Segment Binary a+gt :: Binary a -> Segmented Binary a gt (_ :< Greater x) = Greater x gt (_ :< Branches _ x) = Greater x gt (_ :< _) = End
Data/Apart/Structures/Tree/Binary/AVL.hs view
@@ -1,26 +1,27 @@ module Data.Apart.Structures.Tree.Binary.AVL (insert) where -import Control.Arrow ((&&&))-import Data.Functor.Contravariant (Predicate (..))-import Data.Functor.Contravariant.Divisible (Divisible (..))-import Data.Functor.Bind (Bind (..))+import "base" Control.Arrow ((&&&))+import "contravariant" Data.Functor.Contravariant (Predicate (..))+import "contravariant" Data.Functor.Contravariant.Divisible (Divisible (..))+import "semigroupoids" Data.Functor.Bind (Bind (..)) -import Data.Apart.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..))+ import Data.Apart.Structures.Tree.Binary (Binary, Branches (..), ls, gt, height) import qualified Data.Apart.Structures.Tree.Binary as Binary (insert) import Data.Apart.Structures.Tree.Binary.Rotation (Rotate (..), rtt) -- | Trying rebalance tree after each insert.-insert :: Ord a => a -> Binary a -> Segment Binary a+insert :: Ord a => a -> Binary a -> Segmented Binary a insert x tree = balancing $ Binary.insert tree x -balancing :: Binary a -> Segment Binary a+balancing :: Binary a -> Segmented Binary a balancing t@(getPredicate simple_left -> True) = rtt L t balancing t@(getPredicate simple_right -> True) = rtt R t balancing t@(getPredicate double_left -> True) = rtt RL t balancing t@(getPredicate double_right -> True) = rtt LR t -subheight :: Segment Binary a -> Int+subheight :: Segmented Binary a -> Int subheight = foldr (\t _ -> height t) 0 simple_left :: Predicate (Binary a)
+ Data/Apart/Structures/Tree/Binary/Redblack.hs view
@@ -0,0 +1,20 @@+module Data.Apart.Structures.Tree.Binary.Redblack (Redblack, Color (..), paint) where++import "free" Control.Comonad.Cofree (Cofree (..))++import Data.Apart.Structures.Tree.Binary (Binary, Branches (..))++data Color = Red | Black deriving Show++type Redblack a = Binary (a, Color)++paint :: Binary a -> Redblack a+paint tree = step Black tree where++ step :: Color -> Binary a -> Redblack a+ step color (x :< End) = (x, Black) :< End+ step color (x :< rest) = (x, color) :< (step (invert color) <$> rest)++ invert :: Color -> Color+ invert Black = Red+ invert Red = Black
Data/Apart/Structures/Tree/Binary/Rotation.hs view
@@ -1,13 +1,13 @@ module Data.Apart.Structures.Tree.Binary.Rotation (Rotate (..), rtt) where -import Control.Comonad (Comonad (..))-import Control.Comonad.Cofree (Cofree (..))-import Control.Lens ((<&>))-import Data.Functor.Bind (Bind (..))-import Data.Semigroup (Semigroup (..))+import "base" Data.Semigroup (Semigroup (..))+import "comonad" Control.Comonad (Comonad (..))+import "free" Control.Comonad.Cofree (Cofree (..))+import "lens" Control.Lens ((<&>))+import "semigroupoids" Data.Functor.Bind (Bind (..)) -import Data.Apart.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..)) import Data.Apart.Structures.Tree.Binary (Binary, Branches (..), ls, gt, height) data Rotate@@ -18,7 +18,7 @@ | LL -- ^ Left zig-zig (Splay) | RR -- ^ Right zig-zig (Splay) -rtt :: Rotate -> Binary a -> Segment Binary a+rtt :: Rotate -> Binary a -> Segmented Binary a rtt L t = (<&>) (extract <$> ls t) $ flip (:<) $ (gt t >>- gt) <> (Less $ (extract t) :< (ls t <> (gt t >>- ls))) rtt R t = (<&>) (extract <$> gt t) $ flip (:<) $ (ls t >>- ls)
Data/Apart/Structures/Tree/Binary/Splay.hs view
@@ -1,22 +1,24 @@ module Data.Apart.Structures.Tree.Binary.Splay (search, insert) where -import Control.Comonad (Comonad (..))-import Data.Foldable (find)-import Data.Functor.Bind (Bind (..))-import Data.Functor.Contravariant (Predicate (..))-import Data.Function ((&)) -import Data.Apart.Apart (Segment (..))+import "base" Data.Function ((&))+import "base" Data.Foldable (find)+import "comonad" Control.Comonad (Comonad (..))+import "contravariant" Data.Functor.Contravariant (Predicate (..))+import "semigroupoids" Data.Functor.Bind (Bind (..))+++import Data.Apart.Transformations (Segmented (..)) import Data.Apart.Structures.Tree.Binary (Binary, Branches (..), ls, gt) import qualified Data.Apart.Structures.Tree.Binary as Binary (insert) import Data.Apart.Structures.Tree.Binary.Rotation (Rotate (..), rtt) -- | Splay tree after each insert.-insert :: Ord a => a -> Binary a -> Segment Binary a+insert :: Ord a => a -> Binary a -> Segmented Binary a insert x t = splay x $ Binary.insert t x -- | If needed element not in the root - it isn't found.-search :: Eq a => a -> Binary a -> Segment Binary a+search :: Eq a => a -> Binary a -> Segmented Binary a search x t = maybe End (const $ splay x t) $ find (== x) t left_zig :: Eq a => Predicate (a, Binary a)@@ -43,7 +45,7 @@ right_zig_zag = Predicate $ \ (x, t) -> ls t >>- gt & foldr (\lg _ -> extract lg == x) False -splay :: Eq a => a -> Binary a -> Segment Binary a+splay :: Eq a => a -> Binary a -> Segmented Binary a splay x t@(getPredicate left_zig . (x,) -> True) = rtt L t splay x t@(getPredicate right_zig . (x,) -> True) = rtt R t splay x t@(getPredicate left_zig_zig . (x,) -> True) = rtt LL t
Data/Apart/Structures/Tree/Prefix.hs view
@@ -1,17 +1,17 @@ module Data.Apart.Structures.Tree.Prefix (Prefix, Labeled (..), singleton, seek, insert, crumbs) where -import Control.Applicative (Alternative (..))-import Control.Arrow ((&&&))-import Control.Comonad (Comonad (..))-import Control.Comonad.Cofree (Cofree (..), unwrap)-import Control.Lens (Lens', (^.), (%~))-import Data.Maybe (isJust)-import Data.Foldable (find)-import Data.Function ((&))-import Data.Functor.Contravariant (Predicate (..))-import Data.Functor.Contravariant.Divisible (Divisible (..))-import Data.Monoid (Monoid (..), (<>))+import "base" Control.Applicative (Alternative (..))+import "base" Control.Arrow ((&&&))+import "base" Data.Foldable (find)+import "base" Data.Function ((&))+import "base" Data.Maybe (isJust)+import "base" Data.Monoid (Monoid (..), (<>))+import "comonad" Control.Comonad (Comonad (..))+import "contravariant" Data.Functor.Contravariant (Predicate (..))+import "contravariant" Data.Functor.Contravariant.Divisible (Divisible (..))+import "free" Control.Comonad.Cofree (Cofree (..), unwrap)+import "lens" Control.Lens (Lens', (^.), (%~)) import Data.Apart.Structures.Stack (Stack)
Data/Apart/Structures/Tree/Rose.hs view
@@ -1,7 +1,7 @@ module Data.Apart.Structures.Tree.Rose (Rose, singleton, construct) where -import Control.Applicative (Alternative (..))-import Control.Comonad.Cofree (Cofree (..), coiter)+import "base" Control.Applicative (Alternative (..))+import "free" Control.Comonad.Cofree (Cofree (..), coiter) type Rose t = Cofree t
Data/Apart/Structures/Tree/T23.hs view
@@ -1,6 +1,6 @@ module Data.Apart.Structures.Tree.T23 (T23, N23 (..)) where -import Control.Comonad.Cofree (Cofree (..))+import "free" Control.Comonad.Cofree (Cofree (..)) data N23 a b = L2 a a | L3 a a a a | B2 b b | B3 a b b b
Data/Apart/Structures/Tree/T234.hs view
@@ -1,6 +1,6 @@ module Data.Apart.Structures.Tree.T234 (T234, N234 (..)) where -import Control.Comonad.Cofree (Cofree (..))+import "free" Control.Comonad.Cofree (Cofree (..)) data N234 a b = L2 a a | L3 a a a a | L4 a a a a a a | B2 b b | B3 a b b b | B4 a a b b b b
+ Data/Apart/Transformations.hs view
@@ -0,0 +1,24 @@+module Data.Apart.Transformations (Attached (..), Embedded (..), Injected (..), Segmented (..), Scattered (..)) where++import "base" Data.Functor.Compose (Compose)+import "base" Data.Kind (Type)+import "free" Control.Comonad.Cofree (Cofree)++import Data.Apart (Apart)++type (:.:) = Compose++type family Attached (structure :: Type -> Type) (extension :: Type -> Type) (value :: Type) :: Type where+ Attached (Cofree t) extension value = Cofree t (extension value)++type family Embedded (structure :: Type -> Type) (extension :: Type -> Type) (value :: Type) :: Type where+ Embedded (Cofree t) extension value = Cofree (extension :.: t) value++type family Injected (structure :: Type -> Type) (extension :: Type -> Type) (value :: Type) :: Type where+ Injected (Cofree t) extension value = Cofree (t :.: extension) value++type family Segmented (structure :: Type -> Type) (value :: Type) :: Type where+ Segmented (Cofree t) value = t (Cofree t value)++type family Scattered (structure :: Type -> Type) (value :: Type) (raw :: Type) :: Type where+ Scattered (Cofree t) value raw = Apart t raw value
Data/Apart/Usage/Blockchain.hs view
@@ -1,11 +1,13 @@ module Data.Apart.Usage.Blockchain (Transaction (..), Block, Blockchain, genesis, block, mainchain, verify) where -import Control.Comonad.Cofree (Cofree (..))-import Data.Bitraversable (Bitraversable (..))-import Data.Functor.Compose (Compose (..))+import "free" Control.Comonad.Cofree (Cofree (..))+import "base" Data.Bitraversable (Bitraversable (..))+import "base" Data.Functor.Compose (Compose (..)) -import Data.Apart (Apart (..), Shape (..), Scattered (..))+import Data.Apart (Apart (..))+import Data.Apart.Shape (Shape (..))+import Data.Apart.Transformations (Scattered (..)) import Data.Apart.Structures.Stack (Stack) type Account = Int
Data/Apart/Usage/LRU.hs view
@@ -1,10 +1,10 @@ module Data.Apart.Usage.LRU (LRU, cache) where -import Control.Comonad.Cofree (Cofree (..))-import Data.Functor.Alt (Alt (..))-import Data.Functor.Bind (Bind (..))+import "free" Control.Comonad.Cofree (Cofree (..))+import "semigroupoids" Data.Functor.Alt (Alt (..))+import "semigroupoids" Data.Functor.Bind (Bind (..)) -import Data.Apart (Segment (..))+import Data.Apart.Transformations (Segmented (..)) import Data.Apart.Structures.Tree.Binary (Binary, Branches (..)) import Data.Apart.Structures.Tree.Binary.Splay (insert) @@ -14,7 +14,7 @@ so we need to add priority for the most recently used elements. Very resembles behavoir of Splay trees, actually. -}-type LRU a = Segment Binary a+type LRU a = Segmented Binary a -- | Insert sortable value to cache, aftear that, value moved to root cache :: Ord a => a -> LRU a -> LRU a
Example/Main.hs view
@@ -1,21 +1,24 @@ import Control.Comonad.Cofree (Cofree (..)) import Data.Foldable (toList) -import Data.Apart (Apart (..), Shape (..), Scattered (..), Segment (..), limit, throughout, recover)+import Data.Apart (Apart (..))+import Data.Apart.Shape (Shape (..))+import Data.Apart.Combinators (limit, throughout, recover)+import Data.Apart.Transformations (Segmented (..), Scattered (..)) import Data.Apart.Structures.Stack (Stack) -- part of data structure in some file scattered :: Scattered Stack Int FilePath scattered = Apart $ 1 :< Ready (Just $ 2 :< Ready (Just $ 3 :< Converted "Example/piece.txt")) -read_from_file :: FilePath -> IO (Segment Stack Int)-read_from_file fp = read @(Segment Stack Int) <$> readFile fp+read_from_file :: FilePath -> IO (Segmented Stack Int)+read_from_file fp = read @(Segmented Stack Int) <$> readFile fp -- the whole structure in memory in_memory :: Stack Int in_memory = 1 :< Just (2 :< Just (3 :< Just (4 :< Just (5 :< Nothing)))) -save_to_file :: FilePath -> Segment Stack Int -> IO FilePath+save_to_file :: FilePath -> Segmented Stack Int -> IO FilePath save_to_file fp structure = writeFile fp (show structure) *> pure fp main = do
Test/Apart.hs view
@@ -1,7 +1,7 @@ module Main where -import System.IO (BufferMode(..), hSetBuffering, stdout, stderr)-import Hedgehog (Group (..), checkParallel)+import "base" System.IO (BufferMode(..), hSetBuffering, stdout, stderr)+import "hedgehog" Hedgehog (Group (..), checkParallel) import Test.Apart.Structures.Stack import Test.Apart.Structures.Tree.Binary
apart.cabal view
@@ -1,5 +1,5 @@ name: apart-version: 0.1.1+version: 0.1.3 synopsis: Get all your structure and rip it apart. homepage: https://github.com/iokasimov/apart license: BSD3@@ -20,6 +20,10 @@ library exposed-modules: Data.Apart,+ Data.Apart.Combinators+ Data.Apart.Shape,+ Data.Apart.Transformations,+ Data.Apart.Structures.Dictionary, Data.Apart.Structures.Graph, Data.Apart.Structures.Stack, Data.Apart.Structures.Stream,@@ -31,13 +35,10 @@ Data.Apart.Structures.Tree.Binary.Rotation, Data.Apart.Structures.Tree.Binary.AVL, Data.Apart.Structures.Tree.Binary.Splay,+ Data.Apart.Structures.Tree.Binary.Redblack, Data.Apart.Machinery.Moore, Data.Apart.Usage.Blockchain, Data.Apart.Usage.LRU- other-modules:- Data.Apart.Apart,- Data.Apart.Shape,- Data.Apart.Combinators build-depends: base == 4.* , free@@ -59,6 +60,7 @@ TypeFamilies, TypeOperators, ViewPatterns,+ PackageImports PatternSynonyms, TupleSections @@ -85,6 +87,7 @@ TypeFamilies, TypeOperators, ViewPatterns,+ PackageImports PatternSynonyms, TupleSections @@ -113,6 +116,7 @@ TypeFamilies, TypeOperators, ViewPatterns,+ PackageImports PatternSynonyms, TupleSections ghc-options: -fno-warn-tabs