{- |
This module allows abstraction of operations
that operate on the time axis
and do also work on signal types without sample values.
The most distinctive instances are certainly
Dirac signals and chunky time values.
-}
module Synthesizer.Generic.Cut where

import qualified Synthesizer.Plain.Signal as Sig
import qualified Synthesizer.State.Signal as SigS
import qualified Data.StorableVector as SV
import qualified Data.StorableVector.Lazy as SVL

import qualified Algebra.ToInteger as ToInteger
import qualified Algebra.Ring as Ring

import qualified Data.EventList.Relative.BodyTime as EventList
import qualified Data.EventList.Relative.TimeTime as EventListTT
import qualified Data.EventList.Relative.MixedTime as EventListMT

import qualified Algebra.NonNegative as NonNeg
import qualified Number.NonNegativeChunky as Chunky

import qualified Numeric.NonNegative.Class as NonNeg98
import qualified Numeric.NonNegative.Chunky as Chunky98

import Foreign.Storable (Storable, )
import Control.DeepSeq (NFData, rnf, )

import qualified Data.List.HT as ListHT
import qualified Data.List as List
import qualified Data.Monoid as Monoid
import Data.Function (fix, )
import Data.Tuple.HT (mapPair, mapFst, mapSnd, )
import Data.Monoid (Monoid, mempty, )

import qualified Prelude as P
import NumericPrelude.Numeric
import Prelude
   (Bool, String, (++), error,
    pred, (==), (<=), (>=), (<),
    (.), ($), const, snd,
    not, (||), (&&), min, max, )


class Read sig where
   null :: sig -> Bool
   length :: sig -> Int

class (Read sig) => NormalForm sig where
   {- |
   Evaluating the first value of the signal
   is necessary for avoiding a space leaks
   if you repeatedly drop a prefix from the signal
   and do not consume something from it.
   -}
   evaluateHead :: sig -> ()

class (Read sig, Monoid sig) => Transform sig where
   {- Monoid functions
   {-
   In our categorization 'empty' would belong to the Write class,
   but since an empty signal contains no data,
   the maximum packet size is irrelevant.
   This makes e.g. the definition of mixMulti more general.
   -}
   empty :: sig
   cycle :: sig -> sig
   append :: sig -> sig -> sig
   concat :: [sig] -> sig
   -}
   take :: Int -> sig -> sig
   drop :: Int -> sig -> sig
   -- can occur in an inner loop in Interpolation
   dropMarginRem :: Int -> Int -> sig -> (Int, sig)
   splitAt :: Int -> sig -> (sig, sig)
   reverse :: sig -> sig


instance Storable y => Read (SV.Vector y) where
   {-# INLINE null #-}
   null :: Vector y -> Bool
null = forall a. Vector a -> Bool
SV.null
   {-# INLINE length #-}
   length :: Vector y -> Int
length = forall a. Vector a -> Int
SV.length

instance (Storable y) => NormalForm (SV.Vector y) where
   {-# INLINE evaluateHead #-}
   evaluateHead :: Vector y -> ()
evaluateHead Vector y
x =
      if forall a. Vector a -> Bool
SV.null Vector y
x then () else ()

instance Storable y => Transform (SV.Vector y) where
   {-# INLINE take #-}
   take :: Int -> Vector y -> Vector y
take = forall y. Storable y => Int -> Vector y -> Vector y
SV.take
   {-# INLINE drop #-}
   drop :: Int -> Vector y -> Vector y
drop = forall y. Storable y => Int -> Vector y -> Vector y
SV.drop
   {-# INLINE splitAt #-}
   splitAt :: Int -> Vector y -> (Vector y, Vector y)
splitAt = forall y. Storable y => Int -> Vector y -> (Vector y, Vector y)
SV.splitAt
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> Vector y -> (Int, Vector y)
dropMarginRem Int
n Int
m Vector y
xs =
      let d :: Int
d = forall a. Ord a => a -> a -> a
min Int
m forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
max Int
0 forall a b. (a -> b) -> a -> b
$ forall a. Vector a -> Int
SV.length Vector y
xs forall a. C a => a -> a -> a
- Int
n
      in  (Int
mforall a. C a => a -> a -> a
-Int
d, forall y. Storable y => Int -> Vector y -> Vector y
SV.drop Int
d Vector y
xs)
   {-# INLINE reverse #-}
   reverse :: Vector y -> Vector y
reverse = forall y. Storable y => Vector y -> Vector y
SV.reverse


-- instance Storable y => Read SigSt.T y where
instance Storable y => Read (SVL.Vector y) where
   {-# INLINE null #-}
   null :: Vector y -> Bool
null = forall y. Storable y => Vector y -> Bool
SVL.null
   {-# INLINE length #-}
   length :: Vector y -> Int
length = forall a. Vector a -> Int
SVL.length

instance (Storable y) => NormalForm (SVL.Vector y) where
   {-# INLINE evaluateHead #-}
   evaluateHead :: Vector y -> ()
evaluateHead =
      forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL () (\Vector y
x [Vector y]
_ -> forall sig. NormalForm sig => sig -> ()
evaluateHead Vector y
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Vector a -> [Vector a]
SVL.chunks
--      ListHT.switchL () (\x _ -> rnf x) . SVL.chunks
--   evaluateHead x =
--      if SVL.null x then () else ()

{-
instance (Storable y, NFData y) => NormalForm (SVL.Vector y) where
   {-# INLINE evaluateHead #-}
   evaluateHead x = SVL.switchL () (\x _ -> rnf x)
-}

instance Storable y => Transform (SVL.Vector y) where
   {-
   {-# INLINE empty #-}
   empty = SVL.empty
   {-# INLINE cycle #-}
   cycle = SVL.cycle
   {-# INLINE append #-}
   append = SVL.append
   {-# INLINE concat #-}
   concat = SVL.concat
   -}
   {-# INLINE take #-}
   take :: Int -> Vector y -> Vector y
take = forall y. Storable y => Int -> Vector y -> Vector y
SVL.take
   {-# INLINE drop #-}
   drop :: Int -> Vector y -> Vector y
drop = forall y. Storable y => Int -> Vector y -> Vector y
SVL.drop
   {-# INLINE splitAt #-}
   splitAt :: Int -> Vector y -> (Vector y, Vector y)
splitAt = forall y. Storable y => Int -> Vector y -> (Vector y, Vector y)
SVL.splitAt
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> Vector y -> (Int, Vector y)
dropMarginRem = forall y. Storable y => Int -> Int -> Vector y -> (Int, Vector y)
SVL.dropMarginRem
   {-# INLINE reverse #-}
   reverse :: Vector y -> Vector y
reverse = forall y. Storable y => Vector y -> Vector y
SVL.reverse


instance Read ([] y) where
   {-# INLINE null #-}
   null :: [y] -> Bool
null = forall (t :: * -> *) a. Foldable t => t a -> Bool
List.null
   {-# INLINE length #-}
   length :: [y] -> Int
length = forall (t :: * -> *) a. Foldable t => t a -> Int
List.length

instance (NFData y) => NormalForm ([] y) where
   {-# INLINE evaluateHead #-}
   evaluateHead :: [y] -> ()
evaluateHead = forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL () (\y
x [y]
_ -> forall a. NFData a => a -> ()
rnf y
x)

instance Transform ([] y) where
   {-
   {-# INLINE empty #-}
   empty = []
   {-# INLINE cycle #-}
   cycle = List.cycle
   {-# INLINE append #-}
   append = (List.++)
   {-# INLINE concat #-}
   concat = List.concat
   -}
   {-# INLINE take #-}
   take :: Int -> [y] -> [y]
take = forall y. Int -> [y] -> [y]
List.take
   {-# INLINE drop #-}
   drop :: Int -> [y] -> [y]
drop = forall y. Int -> [y] -> [y]
List.drop
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> [y] -> (Int, [y])
dropMarginRem = forall y. Int -> Int -> [y] -> (Int, [y])
Sig.dropMarginRem
   {-# INLINE splitAt #-}
   splitAt :: Int -> [y] -> ([y], [y])
splitAt = forall y. Int -> [y] -> ([y], [y])
List.splitAt
   {-# INLINE reverse #-}
   reverse :: [y] -> [y]
reverse = forall y. [y] -> [y]
List.reverse


instance Read (SigS.T y) where
   {-# INLINE null #-}
   null :: T y -> Bool
null = forall y. T y -> Bool
SigS.null
   {-# INLINE length #-}
   length :: T y -> Int
length = forall y. T y -> Int
SigS.length

instance (NFData y) => NormalForm (SigS.T y) where
   {-
   Evaluating the first element of a generator might look silly,
   since it is not stored in a data structure.
   However, the generator depends on an internal state,
   which might be in turn a list or a storable vector,
   which is evaluated then.
   -}
   {-# INLINE evaluateHead #-}
   evaluateHead :: T y -> ()
evaluateHead = forall b a. b -> (a -> T a -> b) -> T a -> b
SigS.switchL () (\y
x T y
_ -> forall a. NFData a => a -> ()
rnf y
x)

instance Transform (SigS.T y) where
   {-
   {-# INLINE empty #-}
   empty = SigS.empty
   {-# INLINE cycle #-}
   cycle = SigS.cycle
   {-# INLINE append #-}
   append = SigS.append
   {-# INLINE concat #-}
   concat = SigS.concat
   -}

   {-# INLINE take #-}
   take :: Int -> T y -> T y
take = forall y. Int -> T y -> T y
SigS.take
   {-# INLINE drop #-}
   drop :: Int -> T y -> T y
drop = forall y. Int -> T y -> T y
SigS.drop
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> T y -> (Int, T y)
dropMarginRem = forall y. Int -> Int -> T y -> (Int, T y)
SigS.dropMarginRem
   {-# INLINE splitAt #-}
   splitAt :: Int -> T y -> (T y, T y)
splitAt Int
n =
      -- This implementation is slow. Better leave it unimplemented?
      forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall y. [y] -> T y
SigS.fromList, forall y. [y] -> T y
SigS.fromList) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall y. Int -> [y] -> ([y], [y])
List.splitAt Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall y. T y -> [y]
SigS.toList
   {-# INLINE reverse #-}
   reverse :: T y -> T y
reverse = forall y. T y -> T y
SigS.reverse


{- |
We abuse event lists for efficient representation of piecewise constant signals.
-}
instance (P.Integral t) => Read (EventList.T t y) where
   null :: T t y -> Bool
null = forall time body. T time body -> Bool
EventList.null
   length :: T t y -> Int
length = forall a b. (C a, C b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
P.toInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
P.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
EventList.getTimes

instance (P.Integral t, NFData y) => NormalForm (EventList.T t y) where
   evaluateHead :: T t y -> ()
evaluateHead = forall c body time.
c -> (body -> time -> T time body -> c) -> T time body -> c
EventList.switchL () (\y
x t
_ T t y
_ -> forall a. NFData a => a -> ()
rnf y
x)


{-
needed for chunks of MIDI events as input to CausalIO processes
-}
instance (P.Integral t) => Read (EventListTT.T t y) where
   null :: T t y -> Bool
null = forall time body a. (time -> T time body -> a) -> T time body -> a
EventListMT.switchTimeL (\t
t T t y
xs -> t
tforall a. Eq a => a -> a -> Bool
==t
0 Bool -> Bool -> Bool
&& forall time body. T time body -> Bool
EventList.null T t y
xs)
   length :: T t y -> Int
length = forall a b. (C a, C b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
P.toInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
P.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
EventListTT.getTimes

instance (P.Integral t, NonNeg98.C t) => Transform (EventListTT.T t y) where
   take :: Int -> T t y -> T t y
take = forall time body. C time => time -> T time body -> T time body
EventListTT.takeTime forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
   drop :: Int -> T t y -> T t y
drop = forall time body. C time => time -> T time body -> T time body
EventListTT.dropTime forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
   dropMarginRem :: Int -> Int -> T t y -> (Int, T t y)
dropMarginRem =
      forall sig.
Transform sig =>
(sig -> [Int]) -> Int -> Int -> sig -> (Int, sig)
dropMarginRemChunky (forall a b. (a -> b) -> [a] -> [b]
P.map forall a b. (Integral a, Num b) => a -> b
P.fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
EventListTT.getTimes)
   splitAt :: Int -> T t y -> (T t y, T t y)
splitAt = forall time body.
C time =>
time -> T time body -> (T time body, T time body)
EventListTT.splitAtTime forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
   reverse :: T t y -> T t y
reverse = forall time body. T time body -> T time body
EventListTT.reverse


-- cf. StorableVector.Lazy.dropMarginRem
dropMarginRemChunky ::
   (Transform sig) =>
   (sig -> [Int]) -> Int -> Int -> sig -> (Int, sig)
dropMarginRemChunky :: forall sig.
Transform sig =>
(sig -> [Int]) -> Int -> Int -> sig -> (Int, sig)
dropMarginRemChunky sig -> [Int]
getTimes Int
n Int
m sig
xs =
   forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl'
      (\(Int
mi,sig
xsi) Int
k -> (Int
miforall a. C a => a -> a -> a
-Int
k, forall sig. Transform sig => Int -> sig -> sig
drop Int
k sig
xsi))
      (Int
m, sig
xs)
      (sig -> [Int]
getTimes forall a b. (a -> b) -> a -> b
$ forall sig. Transform sig => Int -> sig -> sig
take Int
m forall a b. (a -> b) -> a -> b
$ forall sig. Transform sig => Int -> sig -> sig
drop Int
n sig
xs)

{- |
The function defined here are based on the interpretation
of event lists as piecewise constant signals.
They do not fit to the interpretation of atomic events.
Because e.g. it makes no sense to split an atomic event into two instances by splitAt,
and it is also not clear, whether dropping the first chunk
shall leave a chunk of length zero
or remove that chunk completely.

However, sometimes we also need lists of events.
In this case the 'reverse' method would be different.
For an event-oriented instance of EventList.TimeTime
see NoteOffList in synthesizer-alsa package.
-}
instance (P.Integral t, NonNeg98.C t) => Transform (EventList.T t y) where
   take :: Int -> T t y -> T t y
take Int
n T t y
xs =
      forall body time a.
(body -> time -> a -> a) -> a -> T time body -> a
EventList.foldrPair
         (\y
b t
t t -> T t y
go t
remain ->
            if t
remain forall a. Ord a => a -> a -> Bool
<= forall a. C a => a
NonNeg98.zero
              then forall time body. T time body
EventList.empty
              else
                let (t
m, ~(Bool
le,t
d)) = forall a. C a => a -> a -> (a, (Bool, a))
NonNeg98.split t
t t
remain
                in  forall body time. body -> time -> T time body -> T time body
EventList.cons y
b t
m forall a b. (a -> b) -> a -> b
$
                    t -> T t y
go (if Bool
le then t
d else forall a. C a => a
NonNeg98.zero))
         (forall a b. a -> b -> a
const forall time body. T time body
EventList.empty) T t y
xs
         (forall a b. (Integral a, Num b) => a -> b
P.fromIntegral Int
n)

   drop :: Int -> T t y -> T t y
drop =
      let recourse :: time -> T time body -> T time body
recourse time
n =
             forall c body time.
c -> (body -> time -> T time body -> c) -> T time body -> c
EventList.switchL forall time body. T time body
EventList.empty forall a b. (a -> b) -> a -> b
$ \body
b time
t T time body
xs ->
             let (Bool
le,time
d) = forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. C a => a -> a -> (a, (Bool, a))
NonNeg98.split time
t time
n
             in  if Bool
le
                   then time -> T time body -> T time body
recourse time
d T time body
xs
                   else forall body time. body -> time -> T time body -> T time body
EventList.cons body
b time
d T time body
xs
      in  forall {time} {body}. C time => time -> T time body -> T time body
recourse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
P.fromIntegral

   dropMarginRem :: Int -> Int -> T t y -> (Int, T t y)
dropMarginRem =
      forall sig.
Transform sig =>
(sig -> [Int]) -> Int -> Int -> sig -> (Int, sig)
dropMarginRemChunky (forall a b. (a -> b) -> [a] -> [b]
P.map forall a b. (Integral a, Num b) => a -> b
P.fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
EventList.getTimes)

   -- cf. StorableVector.Lazy.splitAt
   splitAt :: Int -> T t y -> (T t y, T t y)
splitAt =
      let recourse :: t -> T t body -> (T t body, T t body)
recourse t
0 = (,) forall time body. T time body
EventList.empty
          recourse t
n =
             forall c body time.
c -> (body -> time -> T time body -> c) -> T time body -> c
EventList.switchL (forall time body. T time body
EventList.empty, forall time body. T time body
EventList.empty) forall a b. (a -> b) -> a -> b
$ \body
b t
t T t body
xs ->
             let (t
m, ~(Bool
le,t
d)) = forall a. C a => a -> a -> (a, (Bool, a))
NonNeg98.split t
t t
n
             in  forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (forall body time. body -> time -> T time body -> T time body
EventList.cons body
b t
m) forall a b. (a -> b) -> a -> b
$
                 if Bool
le
                   then t -> T t body -> (T t body, T t body)
recourse t
d T t body
xs
                   else (forall time body. T time body
EventList.empty, forall body time. body -> time -> T time body -> T time body
EventList.cons body
b t
d T t body
xs)
      in  forall {t} {body}.
(Num t, C t) =>
t -> T t body -> (T t body, T t body)
recourse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
P.fromIntegral

   reverse :: T t y -> T t y
reverse =
      forall body time. [(body, time)] -> T time body
EventList.fromPairList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall y. [y] -> [y]
List.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [(body, time)]
EventList.toPairList


{-
useful for application of non-negative chunky numbers as gate signals
-}
instance (ToInteger.C a, NonNeg.C a) => Read (Chunky.T a) where
   {-# INLINE null #-}
   null :: T a -> Bool
null = forall (t :: * -> *) a. Foldable t => t a -> Bool
List.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. C a => T a -> [a]
Chunky.toChunks
   {-# INLINE length #-}
   length :: T a -> Int
length = forall a. C a => [a] -> a
sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
List.map (forall a b. (C a, C b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. C a => a -> Integer
toInteger) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. C a => T a -> [a]
Chunky.toChunks

instance (ToInteger.C a, NonNeg.C a, NFData a) => NormalForm (Chunky.T a) where
   {-# INLINE evaluateHead #-}
   evaluateHead :: T a -> ()
evaluateHead = forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL () (\a
x [a]
_ -> forall a. NFData a => a -> ()
rnf a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. C a => T a -> [a]
Chunky.toChunks


intToChunky :: (Ring.C a, NonNeg.C a) => String -> Int -> Chunky.T a
intToChunky :: forall a. (C a, C a) => String -> Int -> T a
intToChunky String
name =
   forall a. C a => a -> T a
Chunky.fromNumber forall b c a. (b -> c) -> (a -> b) -> a -> c
.
-- the non-negative type is not necessarily a wrapper
--   NonNegW.fromNumberMsg ("Generic.Cut."++name) .
   forall a b. (C a, C b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   (\Int
x ->
      if Int
xforall a. Ord a => a -> a -> Bool
<forall a. C a => a
zero
        then forall a. HasCallStack => String -> a
error (String
"Generic.Cut.NonNeg.Chunky."forall a. [a] -> [a] -> [a]
++String
nameforall a. [a] -> [a] -> [a]
++String
": negative argument")
        else Int
x)


instance (ToInteger.C a, NonNeg.C a) => Transform (Chunky.T a) where
   {-# INLINE take #-}
   take :: Int -> T a -> T a
take Int
n = forall a. Ord a => a -> a -> a
P.min (forall a. (C a, C a) => String -> Int -> T a
intToChunky String
"take" Int
n)
   {-# INLINE drop #-}
   drop :: Int -> T a -> T a
drop Int
n T a
x = T a
x forall a. C a => a -> a -> a
NonNeg.-| forall a. (C a, C a) => String -> Int -> T a
intToChunky String
"drop" Int
n
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> T a -> (Int, T a)
dropMarginRem Int
n Int
m T a
x =
      let (T a
z,~(Bool
b,T a
d)) =
             forall a. C a => T a -> T a -> (T a, (Bool, T a))
Chunky.minMaxDiff
                (forall a. (C a, C a) => String -> Int -> T a
intToChunky String
"dropMargin/n" Int
m)
                (T a
x forall a. C a => a -> a -> a
NonNeg.-| forall a. (C a, C a) => String -> Int -> T a
intToChunky String
"dropMargin/m" Int
n)
      in  (if Bool
b then Int
0 else forall a b. (C a, C b) => a -> b
fromIntegral (forall a. C a => T a -> a
Chunky.toNumber T a
d),
           T a
x forall a. C a => a -> a -> a
NonNeg.-| T a
z)
   {-# INLINE splitAt #-}
   splitAt :: Int -> T a -> (T a, T a)
splitAt Int
n T a
x =
      forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd
         (\ ~(Bool
b,T a
d) -> if Bool
b then T a
d else forall a. Monoid a => a
mempty)
         (forall a. C a => T a -> T a -> (T a, (Bool, T a))
Chunky.minMaxDiff (forall a. (C a, C a) => String -> Int -> T a
intToChunky String
"splitAt" Int
n) T a
x)
   {-# INLINE reverse #-}
   reverse :: T a -> T a
reverse = forall a. C a => [a] -> T a
Chunky.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall y. [y] -> [y]
List.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. C a => T a -> [a]
Chunky.toChunks



instance (P.Integral a) => Read (Chunky98.T a) where
   {-# INLINE null #-}
   null :: T a -> Bool
null = forall (t :: * -> *) a. Foldable t => t a -> Bool
List.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> [a]
Chunky98.toChunks
   {-# INLINE length #-}
   length :: T a -> Int
length = forall a. C a => [a] -> a
sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
List.map (forall a b. (Integral a, Num b) => a -> b
P.fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
P.toInteger) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> [a]
Chunky98.toChunks

instance (P.Integral a, NonNeg.C a, NFData a) =>
      NormalForm (Chunky98.T a) where
   {-# INLINE evaluateHead #-}
   evaluateHead :: T a -> ()
evaluateHead = forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL () (\a
x [a]
_ -> forall a. NFData a => a -> ()
rnf a
x) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> [a]
Chunky98.toChunks


intToChunky98 :: (P.Num a, NonNeg98.C a) => String -> Int -> Chunky98.T a
intToChunky98 :: forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
name =
   forall a. C a => a -> T a
Chunky98.fromNumber forall b c a. (b -> c) -> (a -> b) -> a -> c
.
--   NonNegW.fromNumberMsg ("Generic.Cut."++name) .
   forall a b. (Integral a, Num b) => a -> b
P.fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   (\Int
x ->
      if Int
xforall a. Ord a => a -> a -> Bool
<Int
0
        then forall a. HasCallStack => String -> a
error (String
"Generic.Cut.NonNeg.Chunky98."forall a. [a] -> [a] -> [a]
++String
nameforall a. [a] -> [a] -> [a]
++String
": negative argument")
        else Int
x)

instance (P.Integral a, NonNeg98.C a) => Transform (Chunky98.T a) where
   {-# INLINE take #-}
   take :: Int -> T a -> T a
take Int
n = forall a. Ord a => a -> a -> a
P.min (forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
"take" Int
n)
   {-# INLINE drop #-}
   drop :: Int -> T a -> T a
drop Int
n T a
x = T a
x forall a. C a => a -> a -> a
NonNeg98.-| forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
"drop" Int
n
   {-# INLINE dropMarginRem #-}
   dropMarginRem :: Int -> Int -> T a -> (Int, T a)
dropMarginRem Int
n Int
m T a
x =
      let (T a
z,~(Bool
b,T a
d)) =
             forall a. C a => a -> a -> (a, (Bool, a))
NonNeg98.split
                (forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
"dropMargin/n" Int
m)
                (T a
x forall a. C a => a -> a -> a
NonNeg98.-| forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
"dropMargin/m" Int
n)
      in  (if Bool
b then Int
0 else forall a b. (Integral a, Num b) => a -> b
P.fromIntegral (forall a. C a => T a -> a
Chunky98.toNumber T a
d),
           T a
x forall a. C a => a -> a -> a
NonNeg98.-| T a
z)
   {-# INLINE splitAt #-}
   splitAt :: Int -> T a -> (T a, T a)
splitAt Int
n T a
x =
      forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd
         (\ ~(Bool
b,T a
d) -> if Bool
b then T a
d else forall a. T a
Chunky98.zero)
         (forall a. C a => a -> a -> (a, (Bool, a))
NonNeg98.split (forall a. (Num a, C a) => String -> Int -> T a
intToChunky98 String
"splitAt" Int
n) T a
x)
   {-# INLINE reverse #-}
   reverse :: T a -> T a
reverse = forall a. C a => [a] -> T a
Chunky98.fromChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall y. [y] -> [y]
List.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> [a]
Chunky98.toChunks


{-# INLINE empty #-}
empty :: (Monoid sig) => sig
empty :: forall a. Monoid a => a
empty = forall a. Monoid a => a
Monoid.mempty

{-# INLINE cycle #-}
cycle :: (Monoid sig) => sig -> sig
cycle :: forall sig. Monoid sig => sig -> sig
cycle sig
x = forall a. (a -> a) -> a
fix (forall sig. Monoid sig => sig -> sig -> sig
append sig
x)

{-# INLINE append #-}
append :: (Monoid sig) => sig -> sig -> sig
append :: forall sig. Monoid sig => sig -> sig -> sig
append = forall sig. Monoid sig => sig -> sig -> sig
Monoid.mappend

{-# INLINE concat #-}
concat :: (Monoid sig) => [sig] -> sig
concat :: forall sig. Monoid sig => [sig] -> sig
concat = forall sig. Monoid sig => [sig] -> sig
Monoid.mconcat


{- |
Like @lengthAtLeast n xs  =  length xs >= n@,
but is more efficient, because it is more lazy.
-}
{-# INLINE lengthAtLeast #-}
lengthAtLeast :: (Transform sig) =>
   Int -> sig -> Bool
lengthAtLeast :: forall sig. Transform sig => Int -> sig -> Bool
lengthAtLeast Int
n sig
xs =
   Int
nforall a. Ord a => a -> a -> Bool
<=Int
0 Bool -> Bool -> Bool
|| Bool -> Bool
not (forall sig. Read sig => sig -> Bool
null (forall sig. Transform sig => Int -> sig -> sig
drop (forall a. Enum a => a -> a
pred Int
n) sig
xs))

{-# INLINE lengthAtMost #-}
lengthAtMost :: (Transform sig) =>
   Int -> sig -> Bool
lengthAtMost :: forall sig. Transform sig => Int -> sig -> Bool
lengthAtMost Int
n sig
xs =
   Int
nforall a. Ord a => a -> a -> Bool
>=Int
0 Bool -> Bool -> Bool
&& forall sig. Read sig => sig -> Bool
null (forall sig. Transform sig => Int -> sig -> sig
drop Int
n sig
xs)

{-# INLINE sliceVertical #-}
sliceVertical :: (Transform sig) =>
   Int -> sig -> SigS.T sig
sliceVertical :: forall sig. Transform sig => Int -> sig -> T sig
sliceVertical Int
n =
   forall a b. (a -> b) -> T a -> T b
SigS.map (forall sig. Transform sig => Int -> sig -> sig
take Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. (a -> Bool) -> T a -> T a
SigS.takeWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall sig. Read sig => sig -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. (a -> a) -> a -> T a
SigS.iterate (forall sig. Transform sig => Int -> sig -> sig
drop Int
n)