--------------------------------------------------------------------------------
-- |
-- Module      :  HGeometry.Measured.Report
-- Copyright   :  (C) Frank Staals
-- License     :  see the LICENSE file
-- Maintainer  :  Frank Staals
--
--
--------------------------------------------------------------------------------
module HGeometry.Measured.Report
  ( Report(Report, MkReport)
  , Report1(Report1)
  , ReportList(ReportList)
  ) where

import           Data.Coerce
import qualified Data.Foldable as F
import           Data.Foldable1
import           Data.Functor.Const
import qualified Data.List as List
import           Data.Semigroup.Traversable
import           HGeometry.Measured.Class
import           HGeometry.Tree.Binary.Static

--------------------------------------------------------------------------------

-- | Report is a type to be used for reporting/collecting all results of a query.
--
-- The result is internally represented as a binary tree, so that we can guarantee O(1)
-- time and space (<>).
newtype Report a = MkReport (Maybe (Report1 a))
  deriving (Int -> Report a -> ShowS
[Report a] -> ShowS
Report a -> String
(Int -> Report a -> ShowS)
-> (Report a -> String) -> ([Report a] -> ShowS) -> Show (Report a)
forall a. Show a => Int -> Report a -> ShowS
forall a. Show a => [Report a] -> ShowS
forall a. Show a => Report a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Report a -> ShowS
showsPrec :: Int -> Report a -> ShowS
$cshow :: forall a. Show a => Report a -> String
show :: Report a -> String
$cshowList :: forall a. Show a => [Report a] -> ShowS
showList :: [Report a] -> ShowS
Show,Report a -> Report a -> Bool
(Report a -> Report a -> Bool)
-> (Report a -> Report a -> Bool) -> Eq (Report a)
forall a. Eq a => Report a -> Report a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Report a -> Report a -> Bool
== :: Report a -> Report a -> Bool
$c/= :: forall a. Eq a => Report a -> Report a -> Bool
/= :: Report a -> Report a -> Bool
Eq,Eq (Report a)
Eq (Report a) =>
(Report a -> Report a -> Ordering)
-> (Report a -> Report a -> Bool)
-> (Report a -> Report a -> Bool)
-> (Report a -> Report a -> Bool)
-> (Report a -> Report a -> Bool)
-> (Report a -> Report a -> Report a)
-> (Report a -> Report a -> Report a)
-> Ord (Report a)
Report a -> Report a -> Bool
Report a -> Report a -> Ordering
Report a -> Report a -> Report a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Report a)
forall a. Ord a => Report a -> Report a -> Bool
forall a. Ord a => Report a -> Report a -> Ordering
forall a. Ord a => Report a -> Report a -> Report a
$ccompare :: forall a. Ord a => Report a -> Report a -> Ordering
compare :: Report a -> Report a -> Ordering
$c< :: forall a. Ord a => Report a -> Report a -> Bool
< :: Report a -> Report a -> Bool
$c<= :: forall a. Ord a => Report a -> Report a -> Bool
<= :: Report a -> Report a -> Bool
$c> :: forall a. Ord a => Report a -> Report a -> Bool
> :: Report a -> Report a -> Bool
$c>= :: forall a. Ord a => Report a -> Report a -> Bool
>= :: Report a -> Report a -> Bool
$cmax :: forall a. Ord a => Report a -> Report a -> Report a
max :: Report a -> Report a -> Report a
$cmin :: forall a. Ord a => Report a -> Report a -> Report a
min :: Report a -> Report a -> Report a
Ord,(forall a b. (a -> b) -> Report a -> Report b)
-> (forall a b. a -> Report b -> Report a) -> Functor Report
forall a b. a -> Report b -> Report a
forall a b. (a -> b) -> Report a -> Report b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Report a -> Report b
fmap :: forall a b. (a -> b) -> Report a -> Report b
$c<$ :: forall a b. a -> Report b -> Report a
<$ :: forall a b. a -> Report b -> Report a
Functor,(forall m. Monoid m => Report m -> m)
-> (forall m a. Monoid m => (a -> m) -> Report a -> m)
-> (forall m a. Monoid m => (a -> m) -> Report a -> m)
-> (forall a b. (a -> b -> b) -> b -> Report a -> b)
-> (forall a b. (a -> b -> b) -> b -> Report a -> b)
-> (forall b a. (b -> a -> b) -> b -> Report a -> b)
-> (forall b a. (b -> a -> b) -> b -> Report a -> b)
-> (forall a. (a -> a -> a) -> Report a -> a)
-> (forall a. (a -> a -> a) -> Report a -> a)
-> (forall a. Report a -> [a])
-> (forall a. Report a -> Bool)
-> (forall a. Report a -> Int)
-> (forall a. Eq a => a -> Report a -> Bool)
-> (forall a. Ord a => Report a -> a)
-> (forall a. Ord a => Report a -> a)
-> (forall a. Num a => Report a -> a)
-> (forall a. Num a => Report a -> a)
-> Foldable Report
forall a. Eq a => a -> Report a -> Bool
forall a. Num a => Report a -> a
forall a. Ord a => Report a -> a
forall m. Monoid m => Report m -> m
forall a. Report a -> Bool
forall a. Report a -> Int
forall a. Report a -> [a]
forall a. (a -> a -> a) -> Report a -> a
forall m a. Monoid m => (a -> m) -> Report a -> m
forall b a. (b -> a -> b) -> b -> Report a -> b
forall a b. (a -> b -> b) -> b -> Report a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Report m -> m
fold :: forall m. Monoid m => Report m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Report a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Report a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Report a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Report a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Report a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Report a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Report a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Report a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Report a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Report a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Report a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Report a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Report a -> a
foldr1 :: forall a. (a -> a -> a) -> Report a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Report a -> a
foldl1 :: forall a. (a -> a -> a) -> Report a -> a
$ctoList :: forall a. Report a -> [a]
toList :: forall a. Report a -> [a]
$cnull :: forall a. Report a -> Bool
null :: forall a. Report a -> Bool
$clength :: forall a. Report a -> Int
length :: forall a. Report a -> Int
$celem :: forall a. Eq a => a -> Report a -> Bool
elem :: forall a. Eq a => a -> Report a -> Bool
$cmaximum :: forall a. Ord a => Report a -> a
maximum :: forall a. Ord a => Report a -> a
$cminimum :: forall a. Ord a => Report a -> a
minimum :: forall a. Ord a => Report a -> a
$csum :: forall a. Num a => Report a -> a
sum :: forall a. Num a => Report a -> a
$cproduct :: forall a. Num a => Report a -> a
product :: forall a. Num a => Report a -> a
Foldable,Functor Report
Foldable Report
(Functor Report, Foldable Report) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Report a -> f (Report b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Report (f a) -> f (Report a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Report a -> m (Report b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Report (m a) -> m (Report a))
-> Traversable Report
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Report (m a) -> m (Report a)
forall (f :: * -> *) a.
Applicative f =>
Report (f a) -> f (Report a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Report a -> m (Report b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Report a -> f (Report b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Report a -> f (Report b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Report a -> f (Report b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Report (f a) -> f (Report a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Report (f a) -> f (Report a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Report a -> m (Report b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Report a -> m (Report b)
$csequence :: forall (m :: * -> *) a. Monad m => Report (m a) -> m (Report a)
sequence :: forall (m :: * -> *) a. Monad m => Report (m a) -> m (Report a)
Traversable)

-- | Get the results of a Report as a list.
pattern Report    :: [a] -> Report a
pattern $mReport :: forall {r} {a}. Report a -> ([a] -> r) -> ((# #) -> r) -> r
Report xs <- (F.toList -> xs)
{-# COMPLETE Report #-}

instance Semigroup (Report a) where
  (MkReport Maybe (Report1 a)
m) <> :: Report a -> Report a -> Report a
<> (MkReport Maybe (Report1 a)
m') = Maybe (Report1 a) -> Report a
forall a. Maybe (Report1 a) -> Report a
MkReport (Maybe (Report1 a) -> Report a) -> Maybe (Report1 a) -> Report a
forall a b. (a -> b) -> a -> b
$ case (Maybe (Report1 a)
m,Maybe (Report1 a)
m') of
    (Maybe (Report1 a)
Nothing, Maybe (Report1 a)
Nothing) -> Maybe (Report1 a)
forall a. Maybe a
Nothing
    (Maybe (Report1 a)
Nothing, Just Report1 a
_)  -> Maybe (Report1 a)
m'
    (Just Report1 a
_, Maybe (Report1 a)
Nothing)  -> Maybe (Report1 a)
m
    (Just Report1 a
l, Just Report1 a
r)   -> Report1 a -> Maybe (Report1 a)
forall a. a -> Maybe a
Just (Report1 a -> Maybe (Report1 a)) -> Report1 a -> Maybe (Report1 a)
forall a b. (a -> b) -> a -> b
$ Report1 a
l Report1 a -> Report1 a -> Report1 a
forall a. Semigroup a => a -> a -> a
<> Report1 a
r

instance Monoid (Report a) where
  mempty :: Report a
mempty = Maybe (Report1 a) -> Report a
forall a. Maybe (Report1 a) -> Report a
MkReport Maybe (Report1 a)
forall a. Maybe a
Nothing

instance Measured Report a where
  measure :: a -> Report a
measure = Maybe (Report1 a) -> Report a
forall a. Maybe (Report1 a) -> Report a
MkReport (Maybe (Report1 a) -> Report a)
-> (a -> Maybe (Report1 a)) -> a -> Report a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Report1 a -> Maybe (Report1 a)
forall a. a -> Maybe a
Just (Report1 a -> Maybe (Report1 a))
-> (a -> Report1 a) -> a -> Maybe (Report1 a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Report1 a
forall (f :: * -> *) a. Measured f a => a -> f a
measure

instance CanInsert Report a where
  insertMeasure :: a -> Report a -> Report a
insertMeasure a
x (MkReport Maybe (Report1 a)
m) = case Maybe (Report1 a)
m of
                                   Maybe (Report1 a)
Nothing -> a -> Report a
forall (f :: * -> *) a. Measured f a => a -> f a
measure a
x
                                   Just Report1 a
xs -> Maybe (Report1 a) -> Report a
forall a. Maybe (Report1 a) -> Report a
MkReport (Maybe (Report1 a) -> Report a)
-> (Report1 a -> Maybe (Report1 a)) -> Report1 a -> Report a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Report1 a -> Maybe (Report1 a)
forall a. a -> Maybe a
Just (Report1 a -> Report a) -> Report1 a -> Report a
forall a b. (a -> b) -> a -> b
$ a -> Report1 a -> Report1 a
forall (f :: * -> *) a. CanInsert f a => a -> f a -> f a
insertMeasure a
x Report1 a
xs

--------------------------------------------------------------------------------

-- | Type to represent reporting elements. The result is represented as a binary tree so
-- that we can support O(1) time and space (<>)
newtype Report1 a = Report1 (BinLeafTree (Const () a) a)
  deriving (Int -> Report1 a -> ShowS
[Report1 a] -> ShowS
Report1 a -> String
(Int -> Report1 a -> ShowS)
-> (Report1 a -> String)
-> ([Report1 a] -> ShowS)
-> Show (Report1 a)
forall a. Show a => Int -> Report1 a -> ShowS
forall a. Show a => [Report1 a] -> ShowS
forall a. Show a => Report1 a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Report1 a -> ShowS
showsPrec :: Int -> Report1 a -> ShowS
$cshow :: forall a. Show a => Report1 a -> String
show :: Report1 a -> String
$cshowList :: forall a. Show a => [Report1 a] -> ShowS
showList :: [Report1 a] -> ShowS
Show,Report1 a -> Report1 a -> Bool
(Report1 a -> Report1 a -> Bool)
-> (Report1 a -> Report1 a -> Bool) -> Eq (Report1 a)
forall a. Eq a => Report1 a -> Report1 a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Report1 a -> Report1 a -> Bool
== :: Report1 a -> Report1 a -> Bool
$c/= :: forall a. Eq a => Report1 a -> Report1 a -> Bool
/= :: Report1 a -> Report1 a -> Bool
Eq,Eq (Report1 a)
Eq (Report1 a) =>
(Report1 a -> Report1 a -> Ordering)
-> (Report1 a -> Report1 a -> Bool)
-> (Report1 a -> Report1 a -> Bool)
-> (Report1 a -> Report1 a -> Bool)
-> (Report1 a -> Report1 a -> Bool)
-> (Report1 a -> Report1 a -> Report1 a)
-> (Report1 a -> Report1 a -> Report1 a)
-> Ord (Report1 a)
Report1 a -> Report1 a -> Bool
Report1 a -> Report1 a -> Ordering
Report1 a -> Report1 a -> Report1 a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Report1 a)
forall a. Ord a => Report1 a -> Report1 a -> Bool
forall a. Ord a => Report1 a -> Report1 a -> Ordering
forall a. Ord a => Report1 a -> Report1 a -> Report1 a
$ccompare :: forall a. Ord a => Report1 a -> Report1 a -> Ordering
compare :: Report1 a -> Report1 a -> Ordering
$c< :: forall a. Ord a => Report1 a -> Report1 a -> Bool
< :: Report1 a -> Report1 a -> Bool
$c<= :: forall a. Ord a => Report1 a -> Report1 a -> Bool
<= :: Report1 a -> Report1 a -> Bool
$c> :: forall a. Ord a => Report1 a -> Report1 a -> Bool
> :: Report1 a -> Report1 a -> Bool
$c>= :: forall a. Ord a => Report1 a -> Report1 a -> Bool
>= :: Report1 a -> Report1 a -> Bool
$cmax :: forall a. Ord a => Report1 a -> Report1 a -> Report1 a
max :: Report1 a -> Report1 a -> Report1 a
$cmin :: forall a. Ord a => Report1 a -> Report1 a -> Report1 a
min :: Report1 a -> Report1 a -> Report1 a
Ord)

instance Semigroup (Report1 a) where
  (Report1 BinLeafTree (Const () a) a
l) <> :: Report1 a -> Report1 a -> Report1 a
<> (Report1 BinLeafTree (Const () a) a
r) = BinLeafTree (Const () a) a -> Report1 a
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () a) a -> Report1 a)
-> BinLeafTree (Const () a) a -> Report1 a
forall a b. (a -> b) -> a -> b
$ BinLeafTree (Const () a) a
-> Const () a
-> BinLeafTree (Const () a) a
-> BinLeafTree (Const () a) a
forall v a.
BinLeafTree v a -> v -> BinLeafTree v a -> BinLeafTree v a
Node BinLeafTree (Const () a) a
l Const () a
forall a. Monoid a => a
mempty BinLeafTree (Const () a) a
r

instance Functor Report1 where
  fmap :: forall a b. (a -> b) -> Report1 a -> Report1 b
fmap a -> b
f (Report1 BinLeafTree (Const () a) a
t) = BinLeafTree (Const () b) b -> Report1 b
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () b) b -> Report1 b)
-> (BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b)
-> BinLeafTree (Const () a) b
-> Report1 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b
forall a b. Coercible a b => a -> b
coerce (BinLeafTree (Const () a) b -> Report1 b)
-> BinLeafTree (Const () a) b -> Report1 b
forall a b. (a -> b) -> a -> b
$ (a -> b)
-> BinLeafTree (Const () a) a -> BinLeafTree (Const () a) b
forall a b.
(a -> b)
-> BinLeafTree (Const () a) a -> BinLeafTree (Const () a) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f BinLeafTree (Const () a) a
t

instance Foldable Report1 where
  foldMap :: forall m a. Monoid m => (a -> m) -> Report1 a -> m
foldMap a -> m
f (Report1 BinLeafTree (Const () a) a
t) = (a -> m) -> BinLeafTree (Const () a) a -> m
forall m a. Monoid m => (a -> m) -> BinLeafTree (Const () a) a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f BinLeafTree (Const () a) a
t

instance Foldable1 Report1 where
  foldMap1 :: forall m a. Semigroup m => (a -> m) -> Report1 a -> m
foldMap1 a -> m
f (Report1 BinLeafTree (Const () a) a
t) = (a -> m) -> BinLeafTree (Const () a) a -> m
forall m a.
Semigroup m =>
(a -> m) -> BinLeafTree (Const () a) a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f BinLeafTree (Const () a) a
t

instance Traversable Report1 where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Report1 a -> f (Report1 b)
traverse a -> f b
f (Report1 BinLeafTree (Const () a) a
t) = BinLeafTree (Const () b) b -> Report1 b
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () b) b -> Report1 b)
-> (BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b)
-> BinLeafTree (Const () a) b
-> Report1 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b
forall a b. Coercible a b => a -> b
coerce (BinLeafTree (Const () a) b -> Report1 b)
-> f (BinLeafTree (Const () a) b) -> f (Report1 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b)
-> BinLeafTree (Const () a) a -> f (BinLeafTree (Const () a) b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> BinLeafTree (Const () a) a -> f (BinLeafTree (Const () a) b)
traverse a -> f b
f BinLeafTree (Const () a) a
t

instance Traversable1 Report1 where
  traverse1 :: forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> Report1 a -> f (Report1 b)
traverse1 a -> f b
f (Report1 BinLeafTree (Const () a) a
t) = BinLeafTree (Const () b) b -> Report1 b
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () b) b -> Report1 b)
-> (BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b)
-> BinLeafTree (Const () a) b
-> Report1 b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinLeafTree (Const () a) b -> BinLeafTree (Const () b) b
forall a b. Coercible a b => a -> b
coerce (BinLeafTree (Const () a) b -> Report1 b)
-> f (BinLeafTree (Const () a) b) -> f (Report1 b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b)
-> BinLeafTree (Const () a) a -> f (BinLeafTree (Const () a) b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable1 t, Apply f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Apply f =>
(a -> f b)
-> BinLeafTree (Const () a) a -> f (BinLeafTree (Const () a) b)
traverse1 a -> f b
f BinLeafTree (Const () a) a
t


instance Measured Report1 a where
  measure :: a -> Report1 a
measure = BinLeafTree (Const () a) a -> Report1 a
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () a) a -> Report1 a)
-> (a -> BinLeafTree (Const () a) a) -> a -> Report1 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> BinLeafTree (Const () a) a
forall v a. a -> BinLeafTree v a
Leaf

instance CanInsert Report1 a where
  insertMeasure :: a -> Report1 a -> Report1 a
insertMeasure a
x (Report1 BinLeafTree (Const () a) a
xs) = BinLeafTree (Const () a) a -> Report1 a
forall a. BinLeafTree (Const () a) a -> Report1 a
Report1 (BinLeafTree (Const () a) a -> Report1 a)
-> BinLeafTree (Const () a) a -> Report1 a
forall a b. (a -> b) -> a -> b
$ BinLeafTree (Const () a) a
-> Const () a
-> BinLeafTree (Const () a) a
-> BinLeafTree (Const () a) a
forall v a.
BinLeafTree v a -> v -> BinLeafTree v a -> BinLeafTree v a
Node (a -> BinLeafTree (Const () a) a
forall v a. a -> BinLeafTree v a
Leaf a
x) Const () a
forall a. Monoid a => a
mempty BinLeafTree (Const () a) a
xs


--------------------------------------------------------------------------------

-- | Report is a type to be used for reporting/collecting all results of a query. The
-- result is directly represented as a list.
newtype ReportList a = ReportList [a]
  deriving (Int -> ReportList a -> ShowS
[ReportList a] -> ShowS
ReportList a -> String
(Int -> ReportList a -> ShowS)
-> (ReportList a -> String)
-> ([ReportList a] -> ShowS)
-> Show (ReportList a)
forall a. Show a => Int -> ReportList a -> ShowS
forall a. Show a => [ReportList a] -> ShowS
forall a. Show a => ReportList a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ReportList a -> ShowS
showsPrec :: Int -> ReportList a -> ShowS
$cshow :: forall a. Show a => ReportList a -> String
show :: ReportList a -> String
$cshowList :: forall a. Show a => [ReportList a] -> ShowS
showList :: [ReportList a] -> ShowS
Show,ReportList a -> ReportList a -> Bool
(ReportList a -> ReportList a -> Bool)
-> (ReportList a -> ReportList a -> Bool) -> Eq (ReportList a)
forall a. Eq a => ReportList a -> ReportList a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ReportList a -> ReportList a -> Bool
== :: ReportList a -> ReportList a -> Bool
$c/= :: forall a. Eq a => ReportList a -> ReportList a -> Bool
/= :: ReportList a -> ReportList a -> Bool
Eq,Eq (ReportList a)
Eq (ReportList a) =>
(ReportList a -> ReportList a -> Ordering)
-> (ReportList a -> ReportList a -> Bool)
-> (ReportList a -> ReportList a -> Bool)
-> (ReportList a -> ReportList a -> Bool)
-> (ReportList a -> ReportList a -> Bool)
-> (ReportList a -> ReportList a -> ReportList a)
-> (ReportList a -> ReportList a -> ReportList a)
-> Ord (ReportList a)
ReportList a -> ReportList a -> Bool
ReportList a -> ReportList a -> Ordering
ReportList a -> ReportList a -> ReportList a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ReportList a)
forall a. Ord a => ReportList a -> ReportList a -> Bool
forall a. Ord a => ReportList a -> ReportList a -> Ordering
forall a. Ord a => ReportList a -> ReportList a -> ReportList a
$ccompare :: forall a. Ord a => ReportList a -> ReportList a -> Ordering
compare :: ReportList a -> ReportList a -> Ordering
$c< :: forall a. Ord a => ReportList a -> ReportList a -> Bool
< :: ReportList a -> ReportList a -> Bool
$c<= :: forall a. Ord a => ReportList a -> ReportList a -> Bool
<= :: ReportList a -> ReportList a -> Bool
$c> :: forall a. Ord a => ReportList a -> ReportList a -> Bool
> :: ReportList a -> ReportList a -> Bool
$c>= :: forall a. Ord a => ReportList a -> ReportList a -> Bool
>= :: ReportList a -> ReportList a -> Bool
$cmax :: forall a. Ord a => ReportList a -> ReportList a -> ReportList a
max :: ReportList a -> ReportList a -> ReportList a
$cmin :: forall a. Ord a => ReportList a -> ReportList a -> ReportList a
min :: ReportList a -> ReportList a -> ReportList a
Ord,NonEmpty (ReportList a) -> ReportList a
ReportList a -> ReportList a -> ReportList a
(ReportList a -> ReportList a -> ReportList a)
-> (NonEmpty (ReportList a) -> ReportList a)
-> (forall b. Integral b => b -> ReportList a -> ReportList a)
-> Semigroup (ReportList a)
forall b. Integral b => b -> ReportList a -> ReportList a
forall a. NonEmpty (ReportList a) -> ReportList a
forall a. ReportList a -> ReportList a -> ReportList a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a b. Integral b => b -> ReportList a -> ReportList a
$c<> :: forall a. ReportList a -> ReportList a -> ReportList a
<> :: ReportList a -> ReportList a -> ReportList a
$csconcat :: forall a. NonEmpty (ReportList a) -> ReportList a
sconcat :: NonEmpty (ReportList a) -> ReportList a
$cstimes :: forall a b. Integral b => b -> ReportList a -> ReportList a
stimes :: forall b. Integral b => b -> ReportList a -> ReportList a
Semigroup,Semigroup (ReportList a)
ReportList a
Semigroup (ReportList a) =>
ReportList a
-> (ReportList a -> ReportList a -> ReportList a)
-> ([ReportList a] -> ReportList a)
-> Monoid (ReportList a)
[ReportList a] -> ReportList a
ReportList a -> ReportList a -> ReportList a
forall a. Semigroup (ReportList a)
forall a. ReportList a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. [ReportList a] -> ReportList a
forall a. ReportList a -> ReportList a -> ReportList a
$cmempty :: forall a. ReportList a
mempty :: ReportList a
$cmappend :: forall a. ReportList a -> ReportList a -> ReportList a
mappend :: ReportList a -> ReportList a -> ReportList a
$cmconcat :: forall a. [ReportList a] -> ReportList a
mconcat :: [ReportList a] -> ReportList a
Monoid,(forall a b. (a -> b) -> ReportList a -> ReportList b)
-> (forall a b. a -> ReportList b -> ReportList a)
-> Functor ReportList
forall a b. a -> ReportList b -> ReportList a
forall a b. (a -> b) -> ReportList a -> ReportList b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ReportList a -> ReportList b
fmap :: forall a b. (a -> b) -> ReportList a -> ReportList b
$c<$ :: forall a b. a -> ReportList b -> ReportList a
<$ :: forall a b. a -> ReportList b -> ReportList a
Functor,(forall m. Monoid m => ReportList m -> m)
-> (forall m a. Monoid m => (a -> m) -> ReportList a -> m)
-> (forall m a. Monoid m => (a -> m) -> ReportList a -> m)
-> (forall a b. (a -> b -> b) -> b -> ReportList a -> b)
-> (forall a b. (a -> b -> b) -> b -> ReportList a -> b)
-> (forall b a. (b -> a -> b) -> b -> ReportList a -> b)
-> (forall b a. (b -> a -> b) -> b -> ReportList a -> b)
-> (forall a. (a -> a -> a) -> ReportList a -> a)
-> (forall a. (a -> a -> a) -> ReportList a -> a)
-> (forall a. ReportList a -> [a])
-> (forall a. ReportList a -> Bool)
-> (forall a. ReportList a -> Int)
-> (forall a. Eq a => a -> ReportList a -> Bool)
-> (forall a. Ord a => ReportList a -> a)
-> (forall a. Ord a => ReportList a -> a)
-> (forall a. Num a => ReportList a -> a)
-> (forall a. Num a => ReportList a -> a)
-> Foldable ReportList
forall a. Eq a => a -> ReportList a -> Bool
forall a. Num a => ReportList a -> a
forall a. Ord a => ReportList a -> a
forall m. Monoid m => ReportList m -> m
forall a. ReportList a -> Bool
forall a. ReportList a -> Int
forall a. ReportList a -> [a]
forall a. (a -> a -> a) -> ReportList a -> a
forall m a. Monoid m => (a -> m) -> ReportList a -> m
forall b a. (b -> a -> b) -> b -> ReportList a -> b
forall a b. (a -> b -> b) -> b -> ReportList a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => ReportList m -> m
fold :: forall m. Monoid m => ReportList m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> ReportList a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ReportList a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> ReportList a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ReportList a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> ReportList a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ReportList a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> ReportList a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ReportList a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> ReportList a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ReportList a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> ReportList a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ReportList a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> ReportList a -> a
foldr1 :: forall a. (a -> a -> a) -> ReportList a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ReportList a -> a
foldl1 :: forall a. (a -> a -> a) -> ReportList a -> a
$ctoList :: forall a. ReportList a -> [a]
toList :: forall a. ReportList a -> [a]
$cnull :: forall a. ReportList a -> Bool
null :: forall a. ReportList a -> Bool
$clength :: forall a. ReportList a -> Int
length :: forall a. ReportList a -> Int
$celem :: forall a. Eq a => a -> ReportList a -> Bool
elem :: forall a. Eq a => a -> ReportList a -> Bool
$cmaximum :: forall a. Ord a => ReportList a -> a
maximum :: forall a. Ord a => ReportList a -> a
$cminimum :: forall a. Ord a => ReportList a -> a
minimum :: forall a. Ord a => ReportList a -> a
$csum :: forall a. Num a => ReportList a -> a
sum :: forall a. Num a => ReportList a -> a
$cproduct :: forall a. Num a => ReportList a -> a
product :: forall a. Num a => ReportList a -> a
Foldable,Functor ReportList
Foldable ReportList
(Functor ReportList, Foldable ReportList) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> ReportList a -> f (ReportList b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ReportList (f a) -> f (ReportList a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ReportList a -> m (ReportList b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ReportList (m a) -> m (ReportList a))
-> Traversable ReportList
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
ReportList (m a) -> m (ReportList a)
forall (f :: * -> *) a.
Applicative f =>
ReportList (f a) -> f (ReportList a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ReportList a -> m (ReportList b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ReportList a -> f (ReportList b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ReportList a -> f (ReportList b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ReportList a -> f (ReportList b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
ReportList (f a) -> f (ReportList a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ReportList (f a) -> f (ReportList a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ReportList a -> m (ReportList b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ReportList a -> m (ReportList b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
ReportList (m a) -> m (ReportList a)
sequence :: forall (m :: * -> *) a.
Monad m =>
ReportList (m a) -> m (ReportList a)
Traversable)

instance Measured ReportList a where
  measure :: a -> ReportList a
measure = [a] -> ReportList a
forall a. [a] -> ReportList a
ReportList ([a] -> ReportList a) -> (a -> [a]) -> a -> ReportList a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[])

instance CanInsert ReportList a where
  insertMeasure :: a -> ReportList a -> ReportList a
insertMeasure a
x (ReportList [a]
xs) = [a] -> ReportList a
forall a. [a] -> ReportList a
ReportList (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)

instance Eq a => CanDelete ReportList a where
  -- | Removes the first occurance of the element
  deleteMeasure :: a -> ReportList a -> Maybe (ReportList a)
deleteMeasure a
x (ReportList [a]
xs) = ReportList a -> Maybe (ReportList a)
forall a. a -> Maybe a
Just (ReportList a -> Maybe (ReportList a))
-> ([a] -> ReportList a) -> [a] -> Maybe (ReportList a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ReportList a
forall a. [a] -> ReportList a
ReportList ([a] -> Maybe (ReportList a)) -> [a] -> Maybe (ReportList a)
forall a b. (a -> b) -> a -> b
$ a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
List.delete a
x [a]
xs