-- vim:fdm=marker:foldtext=foldtext()

--------------------------------------------------------------------
-- |
-- Module    : Test.SmallCheck.Series
-- Copyright : (c) Colin Runciman et al.
-- License   : BSD3
-- Maintainer: Roman Cheplyaka <roma@ro-che.info>
--
-- You need this module if you want to generate test values of your own
-- types.
--
-- You'll typically need the following extensions:
--
-- >{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
--
-- SmallCheck itself defines data generators for all the data types used
-- by the "Prelude".
--
-- In order to generate values and functions of your own types, you need
-- to make them instances of 'Serial' (for values) and 'CoSerial' (for
-- functions). There are two main ways to do so: using Generics or writing
-- the instances by hand.
--------------------------------------------------------------------

{-# LANGUAGE CPP                   #-}
{-# LANGUAGE DefaultSignatures     #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeOperators         #-}

#if MIN_VERSION_base(4,8,0)
{-# LANGUAGE Safe                  #-}
#else
{-# LANGUAGE OverlappingInstances  #-}
{-# LANGUAGE Trustworthy           #-}
#endif

module Test.SmallCheck.Series (
  -- {{{
  -- * Generic instances
  -- | The easiest way to create the necessary instances is to use GHC
  -- generics (available starting with GHC 7.2.1).
  --
  -- Here's a complete example:
  --
  -- >{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
  -- >{-# LANGUAGE DeriveGeneric #-}
  -- >
  -- >import Test.SmallCheck.Series
  -- >import GHC.Generics
  -- >
  -- >data Tree a = Null | Fork (Tree a) a (Tree a)
  -- >    deriving Generic
  -- >
  -- >instance Serial m a => Serial m (Tree a)
  --
  -- Here we enable the @DeriveGeneric@ extension which allows to derive 'Generic'
  -- instance for our data type. Then we declare that @Tree a@ is an instance of
  -- 'Serial', but do not provide any definitions. This causes GHC to use the
  -- default definitions that use the 'Generic' instance.
  --
  -- One minor limitation of generic instances is that there's currently no
  -- way to distinguish newtypes and datatypes. Thus, newtype constructors
  -- will also count as one level of depth.

  -- * Data Generators
  -- | Writing 'Serial' instances for application-specific types is
  -- straightforward. You need to define a 'series' generator, typically using
  -- @consN@ family of generic combinators where N is constructor arity.
  --
  -- For example:
  --
  -- >data Tree a = Null | Fork (Tree a) a (Tree a)
  -- >
  -- >instance Serial m a => Serial m (Tree a) where
  -- >  series = cons0 Null \/ cons3 Fork
  --
  -- For newtypes use 'newtypeCons' instead of 'cons1'.
  -- The difference is that 'cons1' is counts as one level of depth, while
  -- 'newtypeCons' doesn't affect the depth.
  --
  -- >newtype Light a = Light a
  -- >
  -- >instance Serial m a => Serial m (Light a) where
  -- >  series = newtypeCons Light
  --
  -- For data types with more than 4 fields define @consN@ as
  --
  -- >consN f = decDepth $
  -- >  f <$> series
  -- >    <~> series
  -- >    <~> series
  -- >    <~> ...    {- series repeated N times in total -}

  -- ** What does consN do, exactly?

  -- | @consN@ has type
  -- @(Serial t_1, ..., Serial t_N) => (t_1 -> ... -> t_N -> t) -> Series t@.
  --
  -- @consN f@ is a series which, for a given depth @d > 0@, produces values of the
  -- form
  --
  -- >f x_1 ... x_N
  --
  -- where @x_i@ ranges over all values of type @t_i@ of depth up to @d-1@
  -- (as defined by the 'series' functions for @t_i@).
  --
  -- @consN@ functions also ensure that x_i are enumerated in the
  -- breadth-first order. Thus, combinations of smaller depth come first
  -- (assuming the same is true for @t_i@).
  --
  -- If @d <= 0@, no values are produced.

  cons0, cons1, cons2, cons3, cons4, newtypeCons,
  -- * Function Generators

  -- | To generate functions of an application-specific argument type,
  -- make the type an instance of 'CoSerial'.
  --
  -- Again there is a standard pattern, this time using the altsN
  -- combinators where again N is constructor arity.  Here are @Tree@ and
  -- @Light@ instances:
  --
  --
  -- >instance CoSerial m a => CoSerial m (Tree a) where
  -- >  coseries rs =
  -- >    alts0 rs >>- \z ->
  -- >    alts3 rs >>- \f ->
  -- >    return $ \t ->
  -- >      case t of
  -- >        Null -> z
  -- >        Fork t1 x t2 -> f t1 x t2
  --
  -- >instance CoSerial m a => CoSerial m (Light a) where
  -- >  coseries rs =
  -- >    newtypeAlts rs >>- \f ->
  -- >    return $ \l ->
  -- >      case l of
  -- >        Light x -> f x
  --
  -- For data types with more than 4 fields define @altsN@ as
  --
  -- >altsN rs = do
  -- >  rs <- fixDepth rs
  -- >  decDepthChecked
  -- >    (constM $ constM $ ... $ constM rs)
  -- >    (coseries $ coseries $ ... $ coseries rs)
  -- >    {- constM and coseries are repeated N times each -}

  -- ** What does altsN do, exactly?

  -- | @altsN@ has type
  -- @(Serial t_1, ..., Serial t_N) => Series t -> Series (t_1 -> ... -> t_N -> t)@.
  --
  -- @altsN s@ is a series which, for a given depth @d@, produces functions of
  -- type
  --
  -- >t_1 -> ... -> t_N -> t
  --
  -- If @d <= 0@, these are constant functions, one for each value produced
  -- by @s@.
  --
  -- If @d > 0@, these functions inspect each of their arguments up to the depth
  -- @d-1@ (as defined by the 'coseries' functions for the corresponding
  -- types) and return values produced by @s@. The depth to which the
  -- values are enumerated does not depend on the depth of inspection.

  alts0, alts1, alts2, alts3, alts4, newtypeAlts,

  -- * Basic definitions
  Depth, Series, Serial(..), CoSerial(..),

  -- * Generic implementations
  genericSeries,
  genericCoseries,

  -- * Convenient wrappers
  Positive(..), NonNegative(..), NonEmpty(..),

  -- * Other useful definitions
  (\/), (><), (<~>), (>>-),
  localDepth,
  decDepth,
  getDepth,
  generate,
  limit,
  listSeries,
  list,
  listM,
  fixDepth,
  decDepthChecked,
  constM
  -- }}}
  ) where

import Control.Monad (liftM, guard, mzero, mplus, msum)
import Control.Monad.Logic (MonadLogic, (>>-), interleave, msplit, observeAllT)
import Control.Monad.Reader (ask, local)
import Control.Applicative (empty, pure, (<$>))
import Control.Monad.Identity (Identity(..))
import Data.Int (Int, Int8, Int16, Int32, Int64)
import Data.List (intercalate)
import Data.Ratio (Ratio, numerator, denominator, (%))
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Numeric.Natural (Natural)
import Test.SmallCheck.SeriesMonad
import GHC.Generics (Generic, (:+:)(..), (:*:)(..), C1, K1(..), M1(..), U1(..), Rep, to, from)

------------------------------
-- Main types and classes
------------------------------
--{{{

class Monad m => Serial m a where
  series   :: Series m a

  default series :: (Generic a, GSerial m (Rep a)) => Series m a
  series = Series m a
forall (m :: * -> *) a.
(Monad m, Generic a, GSerial m (Rep a)) =>
Series m a
genericSeries

genericSeries
  :: (Monad m, Generic a, GSerial m (Rep a))
  => Series m a
genericSeries :: Series m a
genericSeries = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to (Rep a Any -> a) -> Series m (Rep a Any) -> Series m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (Rep a Any)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries

class Monad m => CoSerial m a where
  -- | A proper 'coseries' implementation should pass the depth unchanged to
  -- its first argument. Doing otherwise will make enumeration of curried
  -- functions non-uniform in their arguments.
  coseries :: Series m b -> Series m (a->b)

  default coseries :: (Generic a, GCoSerial m (Rep a)) => Series m b -> Series m (a->b)
  coseries = Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
(Monad m, Generic a, GCoSerial m (Rep a)) =>
Series m b -> Series m (a -> b)
genericCoseries

genericCoseries
  :: (Monad m, Generic a, GCoSerial m (Rep a))
  => Series m b -> Series m (a->b)
genericCoseries :: Series m b -> Series m (a -> b)
genericCoseries rs :: Series m b
rs = ((Rep a Any -> b) -> (a -> Rep a Any) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from) ((Rep a Any -> b) -> a -> b)
-> Series m (Rep a Any -> b) -> Series m (a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (Rep a Any -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries Series m b
rs
-- }}}

------------------------------
-- Helper functions
------------------------------
-- {{{

-- | A simple series specified by a function from depth to the list of
-- values up to that depth.
generate :: (Depth -> [a]) -> Series m a
generate :: (Depth -> [a]) -> Series m a
generate f :: Depth -> [a]
f = do
  Depth
d <- Series m Depth
forall (m :: * -> *). Series m Depth
getDepth
  [Series m a] -> Series m a
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum ([Series m a] -> Series m a) -> [Series m a] -> Series m a
forall a b. (a -> b) -> a -> b
$ (a -> Series m a) -> [a] -> [Series m a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Series m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> [Series m a]) -> [a] -> [Series m a]
forall a b. (a -> b) -> a -> b
$ Depth -> [a]
f Depth
d

-- | Limit a 'Series' to its first @n@ elements
limit :: forall m a . Monad m => Int -> Series m a -> Series m a
limit :: Depth -> Series m a -> Series m a
limit n0 :: Depth
n0 (Series s :: ReaderT Depth (LogicT m) a
s) = ReaderT Depth (LogicT m) a -> Series m a
forall (m :: * -> *) a. ReaderT Depth (LogicT m) a -> Series m a
Series (ReaderT Depth (LogicT m) a -> Series m a)
-> ReaderT Depth (LogicT m) a -> Series m a
forall a b. (a -> b) -> a -> b
$ Depth -> ReaderT Depth (LogicT m) a -> ReaderT Depth (LogicT m) a
forall (ml :: * -> *) b. MonadLogic ml => Depth -> ml b -> ml b
go Depth
n0 ReaderT Depth (LogicT m) a
s
  where
    go :: MonadLogic ml => Int -> ml b -> ml b
    go :: Depth -> ml b -> ml b
go 0 _ = ml b
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    go n :: Depth
n mb1 :: ml b
mb1 = do
      Maybe (b, ml b)
cons :: Maybe (b, ml b) <- ml b -> ml (Maybe (b, ml b))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit ml b
mb1
      case Maybe (b, ml b)
cons of
        Nothing -> ml b
forall (m :: * -> *) a. MonadPlus m => m a
mzero
        Just (b :: b
b, mb2 :: ml b
mb2) -> b -> ml b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b ml b -> ml b -> ml b
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Depth -> ml b -> ml b
forall (ml :: * -> *) b. MonadLogic ml => Depth -> ml b -> ml b
go (Depth
nDepth -> Depth -> Depth
forall a. Num a => a -> a -> a
-1) ml b
mb2

suchThat :: Series m a -> (a -> Bool) -> Series m a
suchThat :: Series m a -> (a -> Bool) -> Series m a
suchThat s :: Series m a
s p :: a -> Bool
p = Series m a
s Series m a -> (a -> Series m a) -> Series m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x :: a
x -> if a -> Bool
p a
x then a -> Series m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x else Series m a
forall (f :: * -> *) a. Alternative f => f a
empty

-- | Given a depth, return the list of values generated by a Serial instance.
--
-- Example, list all integers up to depth 1:
--
-- * @listSeries 1 :: [Int]   -- returns [0,1,-1]@
listSeries :: Serial Identity a => Depth -> [a]
listSeries :: Depth -> [a]
listSeries d :: Depth
d = Depth -> Series Identity a -> [a]
forall a. Depth -> Series Identity a -> [a]
list Depth
d Series Identity a
forall (m :: * -> *) a. Serial m a => Series m a
series

-- | Return the list of values generated by a 'Series'. Useful for
-- debugging 'Serial' instances.
--
-- Examples:
--
-- * @list 3 'series' :: [Int]                  -- returns [0,1,-1,2,-2,3,-3]@
--
-- * @list 3 ('series' :: 'Series' 'Identity' Int)  -- returns [0,1,-1,2,-2,3,-3]@
--
-- * @list 2 'series' :: [[Bool]]               -- returns [[],[True],[False]]@
--
-- The first two are equivalent. The second has a more explicit type binding.
list :: Depth -> Series Identity a -> [a]
list :: Depth -> Series Identity a -> [a]
list d :: Depth
d s :: Series Identity a
s = Identity [a] -> [a]
forall a. Identity a -> a
runIdentity (Identity [a] -> [a]) -> Identity [a] -> [a]
forall a b. (a -> b) -> a -> b
$ LogicT Identity a -> Identity [a]
forall (m :: * -> *) a. Monad m => LogicT m a -> m [a]
observeAllT (LogicT Identity a -> Identity [a])
-> LogicT Identity a -> Identity [a]
forall a b. (a -> b) -> a -> b
$ Depth -> Series Identity a -> LogicT Identity a
forall (m :: * -> *) a. Depth -> Series m a -> LogicT m a
runSeries Depth
d Series Identity a
s

-- | Monadic version of 'list'
listM :: Monad m => Depth -> Series m a -> m [a]
listM :: Depth -> Series m a -> m [a]
listM d :: Depth
d s :: Series m a
s = LogicT m a -> m [a]
forall (m :: * -> *) a. Monad m => LogicT m a -> m [a]
observeAllT (LogicT m a -> m [a]) -> LogicT m a -> m [a]
forall a b. (a -> b) -> a -> b
$ Depth -> Series m a -> LogicT m a
forall (m :: * -> *) a. Depth -> Series m a -> LogicT m a
runSeries Depth
d Series m a
s

-- | Sum (union) of series
infixr 7 \/
(\/) :: Monad m => Series m a -> Series m a -> Series m a
\/ :: Series m a -> Series m a -> Series m a
(\/) = Series m a -> Series m a -> Series m a
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
interleave

-- | Product of series
infixr 8 ><
(><) :: Monad m => Series m a -> Series m b -> Series m (a,b)
a :: Series m a
a >< :: Series m a -> Series m b -> Series m (a, b)
>< b :: Series m b
b = (,) (a -> b -> (a, b)) -> Series m a -> Series m (b -> (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
a Series m (b -> (a, b)) -> Series m b -> Series m (a, b)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b
b

-- | Fair version of 'ap' and '<*>'
infixl 4 <~>
(<~>) :: Monad m => Series m (a -> b) -> Series m a -> Series m b
a :: Series m (a -> b)
a <~> :: Series m (a -> b) -> Series m a -> Series m b
<~> b :: Series m a
b = Series m (a -> b)
a Series m (a -> b) -> ((a -> b) -> Series m b) -> Series m b
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- ((a -> b) -> Series m a -> Series m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
b)

uncurry3 :: (a->b->c->d) -> ((a,b,c)->d)
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f :: a -> b -> c -> d
f (x :: a
x,y :: b
y,z :: c
z) = a -> b -> c -> d
f a
x b
y c
z

uncurry4 :: (a->b->c->d->e) -> ((a,b,c,d)->e)
uncurry4 :: (a -> b -> c -> d -> e) -> (a, b, c, d) -> e
uncurry4 f :: a -> b -> c -> d -> e
f (w :: a
w,x :: b
x,y :: c
y,z :: d
z) = a -> b -> c -> d -> e
f a
w b
x c
y d
z

-- | Query the current depth
getDepth :: Series m Depth
getDepth :: Series m Depth
getDepth = ReaderT Depth (LogicT m) Depth -> Series m Depth
forall (m :: * -> *) a. ReaderT Depth (LogicT m) a -> Series m a
Series ReaderT Depth (LogicT m) Depth
forall r (m :: * -> *). MonadReader r m => m r
ask

-- | Run a series with a modified depth
localDepth :: (Depth -> Depth) -> Series m a -> Series m a
localDepth :: (Depth -> Depth) -> Series m a -> Series m a
localDepth f :: Depth -> Depth
f (Series a :: ReaderT Depth (LogicT m) a
a) = ReaderT Depth (LogicT m) a -> Series m a
forall (m :: * -> *) a. ReaderT Depth (LogicT m) a -> Series m a
Series (ReaderT Depth (LogicT m) a -> Series m a)
-> ReaderT Depth (LogicT m) a -> Series m a
forall a b. (a -> b) -> a -> b
$ (Depth -> Depth)
-> ReaderT Depth (LogicT m) a -> ReaderT Depth (LogicT m) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local Depth -> Depth
f ReaderT Depth (LogicT m) a
a

-- | Run a 'Series' with the depth decreased by 1.
--
-- If the current depth is less or equal to 0, the result is 'mzero'.
decDepth :: Series m a -> Series m a
decDepth :: Series m a -> Series m a
decDepth a :: Series m a
a = do
  Series m ()
forall (m :: * -> *). Series m ()
checkDepth
  (Depth -> Depth) -> Series m a -> Series m a
forall (m :: * -> *) a.
(Depth -> Depth) -> Series m a -> Series m a
localDepth (Depth -> Depth -> Depth
forall a. Num a => a -> a -> a
subtract 1) Series m a
a

checkDepth :: Series m ()
checkDepth :: Series m ()
checkDepth = do
  Depth
d <- Series m Depth
forall (m :: * -> *). Series m Depth
getDepth
  Bool -> Series m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Series m ()) -> Bool -> Series m ()
forall a b. (a -> b) -> a -> b
$ Depth
d Depth -> Depth -> Bool
forall a. Ord a => a -> a -> Bool
> 0

-- | @'constM' = 'liftM' 'const'@
constM :: Monad m => m b -> m (a -> b)
constM :: m b -> m (a -> b)
constM = (b -> a -> b) -> m b -> m (a -> b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> a -> b
forall a b. a -> b -> a
const

-- | Fix the depth of a series at the current level. The resulting series
-- will no longer depend on the \"ambient\" depth.
fixDepth :: Series m a -> Series m (Series m a)
fixDepth :: Series m a -> Series m (Series m a)
fixDepth s :: Series m a
s = Series m Depth
forall (m :: * -> *). Series m Depth
getDepth Series m Depth
-> (Depth -> Series m (Series m a)) -> Series m (Series m a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \d :: Depth
d -> Series m a -> Series m (Series m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Series m a -> Series m (Series m a))
-> Series m a -> Series m (Series m a)
forall a b. (a -> b) -> a -> b
$ (Depth -> Depth) -> Series m a -> Series m a
forall (m :: * -> *) a.
(Depth -> Depth) -> Series m a -> Series m a
localDepth (Depth -> Depth -> Depth
forall a b. a -> b -> a
const Depth
d) Series m a
s

-- | If the current depth is 0, evaluate the first argument. Otherwise,
-- evaluate the second argument with decremented depth.
decDepthChecked :: Series m a -> Series m a -> Series m a
decDepthChecked :: Series m a -> Series m a -> Series m a
decDepthChecked b :: Series m a
b r :: Series m a
r = do
  Depth
d <- Series m Depth
forall (m :: * -> *). Series m Depth
getDepth
  if Depth
d Depth -> Depth -> Bool
forall a. Ord a => a -> a -> Bool
<= 0
    then Series m a
b
    else Series m a -> Series m a
forall (m :: * -> *) a. Series m a -> Series m a
decDepth Series m a
r

unwind :: MonadLogic m => m a -> m [a]
unwind :: m a -> m [a]
unwind a :: m a
a =
  m a -> m (Maybe (a, m a))
forall (m :: * -> *) a. MonadLogic m => m a -> m (Maybe (a, m a))
msplit m a
a m (Maybe (a, m a)) -> (Maybe (a, m a) -> m [a]) -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
  m [a] -> ((a, m a) -> m [a]) -> Maybe (a, m a) -> m [a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []) (\(x :: a
x,a' :: m a
a') -> (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [a]) -> m [a] -> m [a]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m a -> m [a]
forall (m :: * -> *) a. MonadLogic m => m a -> m [a]
unwind m a
a')

-- }}}

------------------------------
-- cons* and alts* functions
------------------------------
-- {{{

cons0 :: a -> Series m a
cons0 :: a -> Series m a
cons0 x :: a
x = Series m a -> Series m a
forall (m :: * -> *) a. Series m a -> Series m a
decDepth (Series m a -> Series m a) -> Series m a -> Series m a
forall a b. (a -> b) -> a -> b
$ a -> Series m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

cons1 :: Serial m a => (a->b) -> Series m b
cons1 :: (a -> b) -> Series m b
cons1 f :: a -> b
f = Series m b -> Series m b
forall (m :: * -> *) a. Series m a -> Series m a
decDepth (Series m b -> Series m b) -> Series m b -> Series m b
forall a b. (a -> b) -> a -> b
$ a -> b
f (a -> b) -> Series m a -> Series m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series

-- | Same as 'cons1', but preserves the depth.
newtypeCons :: Serial m a => (a->b) -> Series m b
newtypeCons :: (a -> b) -> Series m b
newtypeCons f :: a -> b
f = a -> b
f (a -> b) -> Series m a -> Series m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series

cons2 :: (Serial m a, Serial m b) => (a->b->c) -> Series m c
cons2 :: (a -> b -> c) -> Series m c
cons2 f :: a -> b -> c
f = Series m c -> Series m c
forall (m :: * -> *) a. Series m a -> Series m a
decDepth (Series m c -> Series m c) -> Series m c -> Series m c
forall a b. (a -> b) -> a -> b
$ a -> b -> c
f (a -> b -> c) -> Series m a -> Series m (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series Series m (b -> c) -> Series m b -> Series m c
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b
forall (m :: * -> *) a. Serial m a => Series m a
series

cons3 :: (Serial m a, Serial m b, Serial m c) =>
         (a->b->c->d) -> Series m d
cons3 :: (a -> b -> c -> d) -> Series m d
cons3 f :: a -> b -> c -> d
f = Series m d -> Series m d
forall (m :: * -> *) a. Series m a -> Series m a
decDepth (Series m d -> Series m d) -> Series m d -> Series m d
forall a b. (a -> b) -> a -> b
$
  a -> b -> c -> d
f (a -> b -> c -> d) -> Series m a -> Series m (b -> c -> d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series
    Series m (b -> c -> d) -> Series m b -> Series m (c -> d)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b
forall (m :: * -> *) a. Serial m a => Series m a
series
    Series m (c -> d) -> Series m c -> Series m d
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m c
forall (m :: * -> *) a. Serial m a => Series m a
series

cons4 :: (Serial m a, Serial m b, Serial m c, Serial m d) =>
         (a->b->c->d->e) -> Series m e
cons4 :: (a -> b -> c -> d -> e) -> Series m e
cons4 f :: a -> b -> c -> d -> e
f = Series m e -> Series m e
forall (m :: * -> *) a. Series m a -> Series m a
decDepth (Series m e -> Series m e) -> Series m e -> Series m e
forall a b. (a -> b) -> a -> b
$
  a -> b -> c -> d -> e
f (a -> b -> c -> d -> e)
-> Series m a -> Series m (b -> c -> d -> e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series
    Series m (b -> c -> d -> e) -> Series m b -> Series m (c -> d -> e)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b
forall (m :: * -> *) a. Serial m a => Series m a
series
    Series m (c -> d -> e) -> Series m c -> Series m (d -> e)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m c
forall (m :: * -> *) a. Serial m a => Series m a
series
    Series m (d -> e) -> Series m d -> Series m e
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m d
forall (m :: * -> *) a. Serial m a => Series m a
series

alts0 :: Series m a -> Series m a
alts0 :: Series m a -> Series m a
alts0 s :: Series m a
s = Series m a
s

alts1 :: CoSerial m a => Series m b -> Series m (a->b)
alts1 :: Series m b -> Series m (a -> b)
alts1 rs :: Series m b
rs = do
  Series m b
rs <- Series m b -> Series m (Series m b)
forall (m :: * -> *) a. Series m a -> Series m (Series m a)
fixDepth Series m b
rs
  Series m (a -> b) -> Series m (a -> b) -> Series m (a -> b)
forall (m :: * -> *) a. Series m a -> Series m a -> Series m a
decDepthChecked (Series m b -> Series m (a -> b)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m b
rs) (Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs)

alts2
  :: (CoSerial m a, CoSerial m b)
  => Series m c -> Series m (a->b->c)
alts2 :: Series m c -> Series m (a -> b -> c)
alts2 rs :: Series m c
rs = do
  Series m c
rs <- Series m c -> Series m (Series m c)
forall (m :: * -> *) a. Series m a -> Series m (Series m a)
fixDepth Series m c
rs
  Series m (a -> b -> c)
-> Series m (a -> b -> c) -> Series m (a -> b -> c)
forall (m :: * -> *) a. Series m a -> Series m a -> Series m a
decDepthChecked
    (Series m (b -> c) -> Series m (a -> b -> c)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (b -> c) -> Series m (a -> b -> c))
-> Series m (b -> c) -> Series m (a -> b -> c)
forall a b. (a -> b) -> a -> b
$ Series m c -> Series m (b -> c)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m c
rs)
    (Series m (b -> c) -> Series m (a -> b -> c)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (b -> c) -> Series m (a -> b -> c))
-> Series m (b -> c) -> Series m (a -> b -> c)
forall a b. (a -> b) -> a -> b
$ Series m c -> Series m (b -> c)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m c
rs)

alts3 ::  (CoSerial m a, CoSerial m b, CoSerial m c) =>
            Series m d -> Series m (a->b->c->d)
alts3 :: Series m d -> Series m (a -> b -> c -> d)
alts3 rs :: Series m d
rs = do
  Series m d
rs <- Series m d -> Series m (Series m d)
forall (m :: * -> *) a. Series m a -> Series m (Series m a)
fixDepth Series m d
rs
  Series m (a -> b -> c -> d)
-> Series m (a -> b -> c -> d) -> Series m (a -> b -> c -> d)
forall (m :: * -> *) a. Series m a -> Series m a -> Series m a
decDepthChecked
    (Series m (b -> c -> d) -> Series m (a -> b -> c -> d)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (b -> c -> d) -> Series m (a -> b -> c -> d))
-> Series m (b -> c -> d) -> Series m (a -> b -> c -> d)
forall a b. (a -> b) -> a -> b
$ Series m (c -> d) -> Series m (b -> c -> d)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (c -> d) -> Series m (b -> c -> d))
-> Series m (c -> d) -> Series m (b -> c -> d)
forall a b. (a -> b) -> a -> b
$ Series m d -> Series m (c -> d)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m d
rs)
    (Series m (b -> c -> d) -> Series m (a -> b -> c -> d)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (b -> c -> d) -> Series m (a -> b -> c -> d))
-> Series m (b -> c -> d) -> Series m (a -> b -> c -> d)
forall a b. (a -> b) -> a -> b
$ Series m (c -> d) -> Series m (b -> c -> d)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (c -> d) -> Series m (b -> c -> d))
-> Series m (c -> d) -> Series m (b -> c -> d)
forall a b. (a -> b) -> a -> b
$ Series m d -> Series m (c -> d)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m d
rs)

alts4 ::  (CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d) =>
            Series m e -> Series m (a->b->c->d->e)
alts4 :: Series m e -> Series m (a -> b -> c -> d -> e)
alts4 rs :: Series m e
rs = do
  Series m e
rs <- Series m e -> Series m (Series m e)
forall (m :: * -> *) a. Series m a -> Series m (Series m a)
fixDepth Series m e
rs
  Series m (a -> b -> c -> d -> e)
-> Series m (a -> b -> c -> d -> e)
-> Series m (a -> b -> c -> d -> e)
forall (m :: * -> *) a. Series m a -> Series m a -> Series m a
decDepthChecked
    (Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e))
-> Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m (c -> d -> e) -> Series m (b -> c -> d -> e)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (c -> d -> e) -> Series m (b -> c -> d -> e))
-> Series m (c -> d -> e) -> Series m (b -> c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m (d -> e) -> Series m (c -> d -> e)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM (Series m (d -> e) -> Series m (c -> d -> e))
-> Series m (d -> e) -> Series m (c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m e -> Series m (d -> e)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m e
rs)
    (Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e))
-> Series m (b -> c -> d -> e) -> Series m (a -> b -> c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m (c -> d -> e) -> Series m (b -> c -> d -> e)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (c -> d -> e) -> Series m (b -> c -> d -> e))
-> Series m (c -> d -> e) -> Series m (b -> c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m (d -> e) -> Series m (c -> d -> e)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m (d -> e) -> Series m (c -> d -> e))
-> Series m (d -> e) -> Series m (c -> d -> e)
forall a b. (a -> b) -> a -> b
$ Series m e -> Series m (d -> e)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m e
rs)

-- | Same as 'alts1', but preserves the depth.
newtypeAlts :: CoSerial m a => Series m b -> Series m (a->b)
newtypeAlts :: Series m b -> Series m (a -> b)
newtypeAlts = Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries

-- }}}

------------------------------
-- Generic instances
------------------------------
-- {{{

class GSerial m f where
  gSeries :: Series m (f a)
class GCoSerial m f where
  gCoseries :: Series m b -> Series m (f a -> b)

instance {-# OVERLAPPABLE #-} GSerial m f => GSerial m (M1 i c f) where
  gSeries :: Series m (M1 i c f a)
gSeries = f a -> M1 i c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> M1 i c f a) -> Series m (f a) -> Series m (M1 i c f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (f a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries
  {-# INLINE gSeries #-}
instance GCoSerial m f => GCoSerial m (M1 i c f) where
  gCoseries :: Series m b -> Series m (M1 i c f a -> b)
gCoseries rs :: Series m b
rs = ((f a -> b) -> (M1 i c f a -> f a) -> M1 i c f a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f a -> f a
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1) ((f a -> b) -> M1 i c f a -> b)
-> Series m (f a -> b) -> Series m (M1 i c f a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (f a -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries Series m b
rs
  {-# INLINE gCoseries #-}

instance Serial m c => GSerial m (K1 i c) where
  gSeries :: Series m (K1 i c a)
gSeries = c -> K1 i c a
forall k i c (p :: k). c -> K1 i c p
K1 (c -> K1 i c a) -> Series m c -> Series m (K1 i c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m c
forall (m :: * -> *) a. Serial m a => Series m a
series
  {-# INLINE gSeries #-}
instance CoSerial m c => GCoSerial m (K1 i c) where
  gCoseries :: Series m b -> Series m (K1 i c a -> b)
gCoseries rs :: Series m b
rs = ((c -> b) -> (K1 i c a -> c) -> K1 i c a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i c a -> c
forall i c k (p :: k). K1 i c p -> c
unK1) ((c -> b) -> K1 i c a -> b)
-> Series m (c -> b) -> Series m (K1 i c a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (c -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs
  {-# INLINE gCoseries #-}

instance GSerial m U1 where
  gSeries :: Series m (U1 a)
gSeries = U1 a -> Series m (U1 a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure U1 a
forall k (p :: k). U1 p
U1
  {-# INLINE gSeries #-}
instance GCoSerial m U1 where
  gCoseries :: Series m b -> Series m (U1 a -> b)
gCoseries rs :: Series m b
rs = Series m b -> Series m (U1 a -> b)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m b
rs
  {-# INLINE gCoseries #-}

instance (Monad m, GSerial m a, GSerial m b) => GSerial m (a :*: b) where
  gSeries :: Series m ((:*:) a b a)
gSeries = a a -> b a -> (:*:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (a a -> b a -> (:*:) a b a)
-> Series m (a a) -> Series m (b a -> (:*:) a b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (a a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries Series m (b a -> (:*:) a b a)
-> Series m (b a) -> Series m ((:*:) a b a)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m (b a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries
  {-# INLINE gSeries #-}
instance (Monad m, GCoSerial m a, GCoSerial m b) => GCoSerial m (a :*: b) where
  gCoseries :: Series m b -> Series m ((:*:) a b a -> b)
gCoseries rs :: Series m b
rs = (a a -> b a -> b) -> (:*:) a b a -> b
forall (f :: * -> *) p (g :: * -> *) t.
(f p -> g p -> t) -> (:*:) f g p -> t
uncur ((a a -> b a -> b) -> (:*:) a b a -> b)
-> Series m (a a -> b a -> b) -> Series m ((:*:) a b a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (b a -> b) -> Series m (a a -> b a -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries (Series m b -> Series m (b a -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries Series m b
rs)
      where
        uncur :: (f p -> g p -> t) -> (:*:) f g p -> t
uncur f :: f p -> g p -> t
f (x :: f p
x :*: y :: g p
y) = f p -> g p -> t
f f p
x g p
y
  {-# INLINE gCoseries #-}

instance (Monad m, GSerial m a, GSerial m b) => GSerial m (a :+: b) where
  gSeries :: Series m ((:+:) a b a)
gSeries = (a a -> (:+:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (a a -> (:+:) a b a) -> Series m (a a) -> Series m ((:+:) a b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (a a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries) Series m ((:+:) a b a)
-> Series m ((:+:) a b a) -> Series m ((:+:) a b a)
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
`interleave` (b a -> (:+:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (b a -> (:+:) a b a) -> Series m (b a) -> Series m ((:+:) a b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (b a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries)
  {-# INLINE gSeries #-}
instance (Monad m, GCoSerial m a, GCoSerial m b) => GCoSerial m (a :+: b) where
  gCoseries :: Series m b -> Series m ((:+:) a b a -> b)
gCoseries rs :: Series m b
rs =
    Series m b -> Series m (a a -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries Series m b
rs Series m (a a -> b)
-> ((a a -> b) -> Series m ((:+:) a b a -> b))
-> Series m ((:+:) a b a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: a a -> b
f ->
    Series m b -> Series m (b a -> b)
forall (m :: * -> *) (f :: * -> *) b a.
GCoSerial m f =>
Series m b -> Series m (f a -> b)
gCoseries Series m b
rs Series m (b a -> b)
-> ((b a -> b) -> Series m ((:+:) a b a -> b))
-> Series m ((:+:) a b a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \g :: b a -> b
g ->
    ((:+:) a b a -> b) -> Series m ((:+:) a b a -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return (((:+:) a b a -> b) -> Series m ((:+:) a b a -> b))
-> ((:+:) a b a -> b) -> Series m ((:+:) a b a -> b)
forall a b. (a -> b) -> a -> b
$
    \e :: (:+:) a b a
e -> case (:+:) a b a
e of
      L1 x :: a a
x -> a a -> b
f a a
x
      R1 y :: b a
y -> b a -> b
g b a
y
  {-# INLINE gCoseries #-}

instance {-# OVERLAPPING #-} GSerial m f => GSerial m (C1 c f) where
  gSeries :: Series m (C1 c f a)
gSeries = f a -> C1 c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> C1 c f a) -> Series m (f a) -> Series m (C1 c f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (f a) -> Series m (f a)
forall (m :: * -> *) a. Series m a -> Series m a
decDepth Series m (f a)
forall (m :: * -> *) (f :: * -> *) a. GSerial m f => Series m (f a)
gSeries
  {-# INLINE gSeries #-}
-- }}}

------------------------------
-- Instances for basic types
------------------------------
-- {{{
instance Monad m => Serial m () where
  series :: Series m ()
series = () -> Series m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance Monad m => CoSerial m () where
  coseries :: Series m b -> Series m (() -> b)
coseries rs :: Series m b
rs = Series m b -> Series m (() -> b)
forall (m :: * -> *) b a. Monad m => m b -> m (a -> b)
constM Series m b
rs

instance Monad m => Serial m Integer where series :: Series m Integer
series = M Integer -> Integer
forall a. M a -> a
unM (M Integer -> Integer) -> Series m (M Integer) -> Series m Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Integer)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Integer where coseries :: Series m b -> Series m (Integer -> b)
coseries = ((M Integer -> b) -> Integer -> b)
-> Series m (M Integer -> b) -> Series m (Integer -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Integer -> b) -> (Integer -> M Integer) -> Integer -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> M Integer
forall a. a -> M a
M) (Series m (M Integer -> b) -> Series m (Integer -> b))
-> (Series m b -> Series m (M Integer -> b))
-> Series m b
-> Series m (Integer -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Integer -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Natural where series :: Series m Natural
series = N Natural -> Natural
forall a. N a -> a
unN (N Natural -> Natural) -> Series m (N Natural) -> Series m Natural
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Natural)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Natural where coseries :: Series m b -> Series m (Natural -> b)
coseries = ((N Natural -> b) -> Natural -> b)
-> Series m (N Natural -> b) -> Series m (Natural -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Natural -> b) -> (Natural -> N Natural) -> Natural -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> N Natural
forall a. a -> N a
N) (Series m (N Natural -> b) -> Series m (Natural -> b))
-> (Series m b -> Series m (N Natural -> b))
-> Series m b
-> Series m (Natural -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Natural -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Int where series :: Series m Depth
series = M Depth -> Depth
forall a. M a -> a
unM (M Depth -> Depth) -> Series m (M Depth) -> Series m Depth
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Depth)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Int where coseries :: Series m b -> Series m (Depth -> b)
coseries = ((M Depth -> b) -> Depth -> b)
-> Series m (M Depth -> b) -> Series m (Depth -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Depth -> b) -> (Depth -> M Depth) -> Depth -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Depth -> M Depth
forall a. a -> M a
M) (Series m (M Depth -> b) -> Series m (Depth -> b))
-> (Series m b -> Series m (M Depth -> b))
-> Series m b
-> Series m (Depth -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Depth -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Word where series :: Series m Word
series = N Word -> Word
forall a. N a -> a
unN (N Word -> Word) -> Series m (N Word) -> Series m Word
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Word)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Word where coseries :: Series m b -> Series m (Word -> b)
coseries = ((N Word -> b) -> Word -> b)
-> Series m (N Word -> b) -> Series m (Word -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Word -> b) -> (Word -> N Word) -> Word -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> N Word
forall a. a -> N a
N) (Series m (N Word -> b) -> Series m (Word -> b))
-> (Series m b -> Series m (N Word -> b))
-> Series m b
-> Series m (Word -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Word -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Int8 where series :: Series m Int8
series = M Int8 -> Int8
forall a. M a -> a
unM (M Int8 -> Int8) -> Series m (M Int8) -> Series m Int8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Int8)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Int8 where coseries :: Series m b -> Series m (Int8 -> b)
coseries = ((M Int8 -> b) -> Int8 -> b)
-> Series m (M Int8 -> b) -> Series m (Int8 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Int8 -> b) -> (Int8 -> M Int8) -> Int8 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> M Int8
forall a. a -> M a
M) (Series m (M Int8 -> b) -> Series m (Int8 -> b))
-> (Series m b -> Series m (M Int8 -> b))
-> Series m b
-> Series m (Int8 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Int8 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Word8 where series :: Series m Word8
series = N Word8 -> Word8
forall a. N a -> a
unN (N Word8 -> Word8) -> Series m (N Word8) -> Series m Word8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Word8)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Word8 where coseries :: Series m b -> Series m (Word8 -> b)
coseries = ((N Word8 -> b) -> Word8 -> b)
-> Series m (N Word8 -> b) -> Series m (Word8 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Word8 -> b) -> (Word8 -> N Word8) -> Word8 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> N Word8
forall a. a -> N a
N) (Series m (N Word8 -> b) -> Series m (Word8 -> b))
-> (Series m b -> Series m (N Word8 -> b))
-> Series m b
-> Series m (Word8 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Word8 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Int16 where series :: Series m Int16
series = M Int16 -> Int16
forall a. M a -> a
unM (M Int16 -> Int16) -> Series m (M Int16) -> Series m Int16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Int16)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Int16 where coseries :: Series m b -> Series m (Int16 -> b)
coseries = ((M Int16 -> b) -> Int16 -> b)
-> Series m (M Int16 -> b) -> Series m (Int16 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Int16 -> b) -> (Int16 -> M Int16) -> Int16 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> M Int16
forall a. a -> M a
M) (Series m (M Int16 -> b) -> Series m (Int16 -> b))
-> (Series m b -> Series m (M Int16 -> b))
-> Series m b
-> Series m (Int16 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Int16 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Word16 where series :: Series m Word16
series = N Word16 -> Word16
forall a. N a -> a
unN (N Word16 -> Word16) -> Series m (N Word16) -> Series m Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Word16)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Word16 where coseries :: Series m b -> Series m (Word16 -> b)
coseries = ((N Word16 -> b) -> Word16 -> b)
-> Series m (N Word16 -> b) -> Series m (Word16 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Word16 -> b) -> (Word16 -> N Word16) -> Word16 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> N Word16
forall a. a -> N a
N) (Series m (N Word16 -> b) -> Series m (Word16 -> b))
-> (Series m b -> Series m (N Word16 -> b))
-> Series m b
-> Series m (Word16 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Word16 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Int32 where series :: Series m Int32
series = M Int32 -> Int32
forall a. M a -> a
unM (M Int32 -> Int32) -> Series m (M Int32) -> Series m Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Int32)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Int32 where coseries :: Series m b -> Series m (Int32 -> b)
coseries = ((M Int32 -> b) -> Int32 -> b)
-> Series m (M Int32 -> b) -> Series m (Int32 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Int32 -> b) -> (Int32 -> M Int32) -> Int32 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> M Int32
forall a. a -> M a
M) (Series m (M Int32 -> b) -> Series m (Int32 -> b))
-> (Series m b -> Series m (M Int32 -> b))
-> Series m b
-> Series m (Int32 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Int32 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Word32 where series :: Series m Word32
series = N Word32 -> Word32
forall a. N a -> a
unN (N Word32 -> Word32) -> Series m (N Word32) -> Series m Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Word32)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Word32 where coseries :: Series m b -> Series m (Word32 -> b)
coseries = ((N Word32 -> b) -> Word32 -> b)
-> Series m (N Word32 -> b) -> Series m (Word32 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Word32 -> b) -> (Word32 -> N Word32) -> Word32 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> N Word32
forall a. a -> N a
N) (Series m (N Word32 -> b) -> Series m (Word32 -> b))
-> (Series m b -> Series m (N Word32 -> b))
-> Series m b
-> Series m (Word32 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Word32 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Int64 where series :: Series m Int64
series = M Int64 -> Int64
forall a. M a -> a
unM (M Int64 -> Int64) -> Series m (M Int64) -> Series m Int64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (M Int64)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Int64 where coseries :: Series m b -> Series m (Int64 -> b)
coseries = ((M Int64 -> b) -> Int64 -> b)
-> Series m (M Int64 -> b) -> Series m (Int64 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((M Int64 -> b) -> (Int64 -> M Int64) -> Int64 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> M Int64
forall a. a -> M a
M) (Series m (M Int64 -> b) -> Series m (Int64 -> b))
-> (Series m b -> Series m (M Int64 -> b))
-> Series m b
-> Series m (Int64 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (M Int64 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries
instance Monad m => Serial m Word64 where series :: Series m Word64
series = N Word64 -> Word64
forall a. N a -> a
unN (N Word64 -> Word64) -> Series m (N Word64) -> Series m Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (N Word64)
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Word64 where coseries :: Series m b -> Series m (Word64 -> b)
coseries = ((N Word64 -> b) -> Word64 -> b)
-> Series m (N Word64 -> b) -> Series m (Word64 -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((N Word64 -> b) -> (Word64 -> N Word64) -> Word64 -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> N Word64
forall a. a -> N a
N) (Series m (N Word64 -> b) -> Series m (Word64 -> b))
-> (Series m b -> Series m (N Word64 -> b))
-> Series m b
-> Series m (Word64 -> b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series m b -> Series m (N Word64 -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries

-- | 'N' is a wrapper for 'Integral' types that causes only non-negative values
-- to be generated. Generated functions of type @N a -> b@ do not distinguish
-- different negative values of @a@.
newtype N a = N { N a -> a
unN :: a } deriving (N a -> N a -> Bool
(N a -> N a -> Bool) -> (N a -> N a -> Bool) -> Eq (N a)
forall a. Eq a => N a -> N a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: N a -> N a -> Bool
$c/= :: forall a. Eq a => N a -> N a -> Bool
== :: N a -> N a -> Bool
$c== :: forall a. Eq a => N a -> N a -> Bool
Eq, Eq (N a)
Eq (N a) =>
(N a -> N a -> Ordering)
-> (N a -> N a -> Bool)
-> (N a -> N a -> Bool)
-> (N a -> N a -> Bool)
-> (N a -> N a -> Bool)
-> (N a -> N a -> N a)
-> (N a -> N a -> N a)
-> Ord (N a)
N a -> N a -> Bool
N a -> N a -> Ordering
N a -> N a -> N 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 (N a)
forall a. Ord a => N a -> N a -> Bool
forall a. Ord a => N a -> N a -> Ordering
forall a. Ord a => N a -> N a -> N a
min :: N a -> N a -> N a
$cmin :: forall a. Ord a => N a -> N a -> N a
max :: N a -> N a -> N a
$cmax :: forall a. Ord a => N a -> N a -> N a
>= :: N a -> N a -> Bool
$c>= :: forall a. Ord a => N a -> N a -> Bool
> :: N a -> N a -> Bool
$c> :: forall a. Ord a => N a -> N a -> Bool
<= :: N a -> N a -> Bool
$c<= :: forall a. Ord a => N a -> N a -> Bool
< :: N a -> N a -> Bool
$c< :: forall a. Ord a => N a -> N a -> Bool
compare :: N a -> N a -> Ordering
$ccompare :: forall a. Ord a => N a -> N a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (N a)
Ord)

instance Real a => Real (N a) where
  toRational :: N a -> Rational
toRational (N x :: a
x) = a -> Rational
forall a. Real a => a -> Rational
toRational a
x

instance Enum a => Enum (N a) where
  toEnum :: Depth -> N a
toEnum x :: Depth
x = a -> N a
forall a. a -> N a
N (Depth -> a
forall a. Enum a => Depth -> a
toEnum Depth
x)
  fromEnum :: N a -> Depth
fromEnum (N x :: a
x) = a -> Depth
forall a. Enum a => a -> Depth
fromEnum a
x

instance Num a => Num (N a) where
  N x :: a
x + :: N a -> N a -> N a
+ N y :: a
y = a -> N a
forall a. a -> N a
N (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
y)
  N x :: a
x * :: N a -> N a -> N a
* N y :: a
y = a -> N a
forall a. a -> N a
N (a
x a -> a -> a
forall a. Num a => a -> a -> a
* a
y)
  negate :: N a -> N a
negate (N x :: a
x) = a -> N a
forall a. a -> N a
N (a -> a
forall a. Num a => a -> a
negate a
x)
  abs :: N a -> N a
abs (N x :: a
x) = a -> N a
forall a. a -> N a
N (a -> a
forall a. Num a => a -> a
abs a
x)
  signum :: N a -> N a
signum (N x :: a
x) = a -> N a
forall a. a -> N a
N (a -> a
forall a. Num a => a -> a
signum a
x)
  fromInteger :: Integer -> N a
fromInteger x :: Integer
x = a -> N a
forall a. a -> N a
N (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
x)

instance Integral a => Integral (N a) where
  quotRem :: N a -> N a -> (N a, N a)
quotRem (N x :: a
x) (N y :: a
y) = (a -> N a
forall a. a -> N a
N a
q, a -> N a
forall a. a -> N a
N a
r)
    where
      (q :: a
q, r :: a
r) = a
x a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` a
y
  toInteger :: N a -> Integer
toInteger (N x :: a
x) = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x

instance (Num a, Enum a, Serial m a) => Serial m (N a) where
  series :: Series m (N a)
series = (Depth -> [N a]) -> Series m (N a)
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [N a]) -> Series m (N a))
-> (Depth -> [N a]) -> Series m (N a)
forall a b. (a -> b) -> a -> b
$ \d :: Depth
d -> Depth -> [N a] -> [N a]
forall a. Depth -> [a] -> [a]
take (Depth
dDepth -> Depth -> Depth
forall a. Num a => a -> a -> a
+1) [0..]

instance (Integral a, Monad m) => CoSerial m (N a) where
  coseries :: Series m b -> Series m (N a -> b)
coseries rs :: Series m b
rs =
    -- This is a recursive function, because @alts1 rs@ typically calls
    -- back to 'coseries' (but with lower depth).
    --
    -- The recursion stops when depth == 0. Then alts1 produces a constant
    -- function, and doesn't call back to 'coseries'.
    Series m b -> Series m b
forall (m :: * -> *) a. Series m a -> Series m a
alts0 Series m b
rs Series m b -> (b -> Series m (N a -> b)) -> Series m (N a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \z :: b
z ->
    Series m b -> Series m (N a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs Series m (N a -> b)
-> ((N a -> b) -> Series m (N a -> b)) -> Series m (N a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: N a -> b
f ->
    (N a -> b) -> Series m (N a -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((N a -> b) -> Series m (N a -> b))
-> (N a -> b) -> Series m (N a -> b)
forall a b. (a -> b) -> a -> b
$ \(N i :: a
i) ->
      if a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> 0
        then N a -> b
f (a -> N a
forall a. a -> N a
N (a -> N a) -> a -> N a
forall a b. (a -> b) -> a -> b
$ a
ia -> a -> a
forall a. Num a => a -> a -> a
-1)
        else b
z

-- | 'M' is a helper type to generate values of a signed type of increasing magnitude.
newtype M a = M { M a -> a
unM :: a } deriving (M a -> M a -> Bool
(M a -> M a -> Bool) -> (M a -> M a -> Bool) -> Eq (M a)
forall a. Eq a => M a -> M a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: M a -> M a -> Bool
$c/= :: forall a. Eq a => M a -> M a -> Bool
== :: M a -> M a -> Bool
$c== :: forall a. Eq a => M a -> M a -> Bool
Eq, Eq (M a)
Eq (M a) =>
(M a -> M a -> Ordering)
-> (M a -> M a -> Bool)
-> (M a -> M a -> Bool)
-> (M a -> M a -> Bool)
-> (M a -> M a -> Bool)
-> (M a -> M a -> M a)
-> (M a -> M a -> M a)
-> Ord (M a)
M a -> M a -> Bool
M a -> M a -> Ordering
M a -> M a -> M 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 (M a)
forall a. Ord a => M a -> M a -> Bool
forall a. Ord a => M a -> M a -> Ordering
forall a. Ord a => M a -> M a -> M a
min :: M a -> M a -> M a
$cmin :: forall a. Ord a => M a -> M a -> M a
max :: M a -> M a -> M a
$cmax :: forall a. Ord a => M a -> M a -> M a
>= :: M a -> M a -> Bool
$c>= :: forall a. Ord a => M a -> M a -> Bool
> :: M a -> M a -> Bool
$c> :: forall a. Ord a => M a -> M a -> Bool
<= :: M a -> M a -> Bool
$c<= :: forall a. Ord a => M a -> M a -> Bool
< :: M a -> M a -> Bool
$c< :: forall a. Ord a => M a -> M a -> Bool
compare :: M a -> M a -> Ordering
$ccompare :: forall a. Ord a => M a -> M a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (M a)
Ord)

instance Real a => Real (M a) where
  toRational :: M a -> Rational
toRational (M x :: a
x) = a -> Rational
forall a. Real a => a -> Rational
toRational a
x

instance Enum a => Enum (M a) where
  toEnum :: Depth -> M a
toEnum x :: Depth
x = a -> M a
forall a. a -> M a
M (Depth -> a
forall a. Enum a => Depth -> a
toEnum Depth
x)
  fromEnum :: M a -> Depth
fromEnum (M x :: a
x) = a -> Depth
forall a. Enum a => a -> Depth
fromEnum a
x

instance Num a => Num (M a) where
  M x :: a
x + :: M a -> M a -> M a
+ M y :: a
y = a -> M a
forall a. a -> M a
M (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
y)
  M x :: a
x * :: M a -> M a -> M a
* M y :: a
y = a -> M a
forall a. a -> M a
M (a
x a -> a -> a
forall a. Num a => a -> a -> a
* a
y)
  negate :: M a -> M a
negate (M x :: a
x) = a -> M a
forall a. a -> M a
M (a -> a
forall a. Num a => a -> a
negate a
x)
  abs :: M a -> M a
abs (M x :: a
x) = a -> M a
forall a. a -> M a
M (a -> a
forall a. Num a => a -> a
abs a
x)
  signum :: M a -> M a
signum (M x :: a
x) = a -> M a
forall a. a -> M a
M (a -> a
forall a. Num a => a -> a
signum a
x)
  fromInteger :: Integer -> M a
fromInteger x :: Integer
x = a -> M a
forall a. a -> M a
M (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
x)

instance Integral a => Integral (M a) where
  quotRem :: M a -> M a -> (M a, M a)
quotRem (M x :: a
x) (M y :: a
y) = (a -> M a
forall a. a -> M a
M a
q, a -> M a
forall a. a -> M a
M a
r)
    where
      (q :: a
q, r :: a
r) = a
x a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` a
y
  toInteger :: M a -> Integer
toInteger (M x :: a
x) = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x

instance (Num a, Enum a, Monad m) => Serial m (M a) where
  series :: Series m (M a)
series = Series m (M a)
forall (m :: * -> *). Series m (M a)
others Series m (M a) -> Series m (M a) -> Series m (M a)
forall (m :: * -> *) a. MonadLogic m => m a -> m a -> m a
`interleave` Series m (M a)
forall (m :: * -> *). Series m (M a)
positives
    where positives :: Series m (M a)
positives = (Depth -> [M a]) -> Series m (M a)
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [M a]) -> Series m (M a))
-> (Depth -> [M a]) -> Series m (M a)
forall a b. (a -> b) -> a -> b
$ \d :: Depth
d -> Depth -> [M a] -> [M a]
forall a. Depth -> [a] -> [a]
take Depth
d [1..]
          others :: Series m (M a)
others = (Depth -> [M a]) -> Series m (M a)
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [M a]) -> Series m (M a))
-> (Depth -> [M a]) -> Series m (M a)
forall a b. (a -> b) -> a -> b
$ \d :: Depth
d -> Depth -> [M a] -> [M a]
forall a. Depth -> [a] -> [a]
take (Depth
dDepth -> Depth -> Depth
forall a. Num a => a -> a -> a
+1) [0,-1..]

instance (Ord a, Num a, Monad m) => CoSerial m (M a) where
  coseries :: Series m b -> Series m (M a -> b)
coseries rs :: Series m b
rs =
    Series m b -> Series m b
forall (m :: * -> *) a. Series m a -> Series m a
alts0 Series m b
rs Series m b -> (b -> Series m (M a -> b)) -> Series m (M a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \z :: b
z ->
    Series m b -> Series m (M (M a) -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs Series m (M (M a) -> b)
-> ((M (M a) -> b) -> Series m (M a -> b)) -> Series m (M a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: M (M a) -> b
f ->
    Series m b -> Series m (M (M a) -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs Series m (M (M a) -> b)
-> ((M (M a) -> b) -> Series m (M a -> b)) -> Series m (M a -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \g :: M (M a) -> b
g ->
    (M a -> b) -> Series m (M a -> b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((M a -> b) -> Series m (M a -> b))
-> (M a -> b) -> Series m (M a -> b)
forall a b. (a -> b) -> a -> b
$ \ i :: M a
i -> case M a -> M a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare M a
i 0 of
        GT -> M (M a) -> b
f (M a -> M (M a)
forall a. a -> M a
M (M a
i M a -> M a -> M a
forall a. Num a => a -> a -> a
- 1))
        LT -> M (M a) -> b
g (M a -> M (M a)
forall a. a -> M a
M (M a -> M a
forall a. Num a => a -> a
abs M a
i M a -> M a -> M a
forall a. Num a => a -> a -> a
- 1))
        EQ -> b
z

instance Monad m => Serial m Float where
  series :: Series m Float
series =
    Series m (Integer, Depth)
forall (m :: * -> *) a. Serial m a => Series m a
series Series m (Integer, Depth)
-> ((Integer, Depth) -> Series m Float) -> Series m Float
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \(sig :: Integer
sig, exp :: Depth
exp) ->
    Bool -> Series m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Integer -> Bool
forall a. Integral a => a -> Bool
odd Integer
sig Bool -> Bool -> Bool
|| Integer
sigInteger -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
==0 Bool -> Bool -> Bool
&& Depth
expDepth -> Depth -> Bool
forall a. Eq a => a -> a -> Bool
==0) Series m () -> Series m Float -> Series m Float
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
    Float -> Series m Float
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Depth -> Float
forall a. RealFloat a => Integer -> Depth -> a
encodeFloat Integer
sig Depth
exp)
instance Monad m => CoSerial m Float where
  coseries :: Series m b -> Series m (Float -> b)
coseries rs :: Series m b
rs =
    Series m b -> Series m ((Integer, Depth) -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs Series m ((Integer, Depth) -> b)
-> (((Integer, Depth) -> b) -> Series m (Float -> b))
-> Series m (Float -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: (Integer, Depth) -> b
f ->
      (Float -> b) -> Series m (Float -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Float -> b) -> Series m (Float -> b))
-> (Float -> b) -> Series m (Float -> b)
forall a b. (a -> b) -> a -> b
$ (Integer, Depth) -> b
f ((Integer, Depth) -> b)
-> (Float -> (Integer, Depth)) -> Float -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> (Integer, Depth)
forall a. RealFloat a => a -> (Integer, Depth)
decodeFloat

instance Monad m => Serial m Double where
  series :: Series m Double
series = (Float -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac :: Float -> Double) (Float -> Double) -> Series m Float -> Series m Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m Float
forall (m :: * -> *) a. Serial m a => Series m a
series
instance Monad m => CoSerial m Double where
  coseries :: Series m b -> Series m (Double -> b)
coseries rs :: Series m b
rs =
    ((Float -> b) -> (Double -> Float) -> Double -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac :: Double -> Float)) ((Float -> b) -> Double -> b)
-> Series m (Float -> b) -> Series m (Double -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (Float -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs

instance (Integral i, Serial m i) => Serial m (Ratio i) where
  series :: Series m (Ratio i)
series = (i, Positive i) -> Ratio i
forall a. Integral a => (a, Positive a) -> Ratio a
pairToRatio ((i, Positive i) -> Ratio i)
-> Series m (i, Positive i) -> Series m (Ratio i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m (i, Positive i)
forall (m :: * -> *) a. Serial m a => Series m a
series
    where
      pairToRatio :: (a, Positive a) -> Ratio a
pairToRatio (n :: a
n, Positive d :: a
d) = a
n a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
% a
d
instance (Integral i, CoSerial m i) => CoSerial m (Ratio i) where
  coseries :: Series m b -> Series m (Ratio i -> b)
coseries rs :: Series m b
rs = (((i, i) -> b) -> (Ratio i -> (i, i)) -> Ratio i -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ratio i -> (i, i)
forall b. Ratio b -> (b, b)
ratioToPair) (((i, i) -> b) -> Ratio i -> b)
-> Series m ((i, i) -> b) -> Series m (Ratio i -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m ((i, i) -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs
    where
      ratioToPair :: Ratio b -> (b, b)
ratioToPair r :: Ratio b
r = (Ratio b -> b
forall a. Ratio a -> a
numerator Ratio b
r, Ratio b -> b
forall a. Ratio a -> a
denominator Ratio b
r)

instance Monad m => Serial m Char where
  series :: Series m Char
series = (Depth -> [Char]) -> Series m Char
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [Char]) -> Series m Char)
-> (Depth -> [Char]) -> Series m Char
forall a b. (a -> b) -> a -> b
$ \d :: Depth
d -> Depth -> [Char] -> [Char]
forall a. Depth -> [a] -> [a]
take (Depth
dDepth -> Depth -> Depth
forall a. Num a => a -> a -> a
+1) ['a'..'z']
instance Monad m => CoSerial m Char where
  coseries :: Series m b -> Series m (Char -> b)
coseries rs :: Series m b
rs =
    Series m b -> Series m (N Depth -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
rs Series m (N Depth -> b)
-> ((N Depth -> b) -> Series m (Char -> b)) -> Series m (Char -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: N Depth -> b
f ->
    (Char -> b) -> Series m (Char -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Char -> b) -> Series m (Char -> b))
-> (Char -> b) -> Series m (Char -> b)
forall a b. (a -> b) -> a -> b
$ \c :: Char
c -> N Depth -> b
f (Depth -> N Depth
forall a. a -> N a
N (Char -> Depth
forall a. Enum a => a -> Depth
fromEnum Char
c Depth -> Depth -> Depth
forall a. Num a => a -> a -> a
- Char -> Depth
forall a. Enum a => a -> Depth
fromEnum 'a'))

instance (Serial m a, Serial m b) => Serial m (a,b) where
  series :: Series m (a, b)
series = (a -> b -> (a, b)) -> Series m (a, b)
forall (m :: * -> *) a b c.
(Serial m a, Serial m b) =>
(a -> b -> c) -> Series m c
cons2 (,)
instance (CoSerial m a, CoSerial m b) => CoSerial m (a,b) where
  coseries :: Series m b -> Series m ((a, b) -> b)
coseries rs :: Series m b
rs = (a -> b -> b) -> (a, b) -> b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((a -> b -> b) -> (a, b) -> b)
-> Series m (a -> b -> b) -> Series m ((a, b) -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (a -> b -> b)
forall (m :: * -> *) a b c.
(CoSerial m a, CoSerial m b) =>
Series m c -> Series m (a -> b -> c)
alts2 Series m b
rs

instance (Serial m a, Serial m b, Serial m c) => Serial m (a,b,c) where
  series :: Series m (a, b, c)
series = (a -> b -> c -> (a, b, c)) -> Series m (a, b, c)
forall (m :: * -> *) a b c d.
(Serial m a, Serial m b, Serial m c) =>
(a -> b -> c -> d) -> Series m d
cons3 (,,)
instance (CoSerial m a, CoSerial m b, CoSerial m c) => CoSerial m (a,b,c) where
  coseries :: Series m b -> Series m ((a, b, c) -> b)
coseries rs :: Series m b
rs = (a -> b -> c -> b) -> (a, b, c) -> b
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 ((a -> b -> c -> b) -> (a, b, c) -> b)
-> Series m (a -> b -> c -> b) -> Series m ((a, b, c) -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (a -> b -> c -> b)
forall (m :: * -> *) a b c d.
(CoSerial m a, CoSerial m b, CoSerial m c) =>
Series m d -> Series m (a -> b -> c -> d)
alts3 Series m b
rs

instance (Serial m a, Serial m b, Serial m c, Serial m d) => Serial m (a,b,c,d) where
  series :: Series m (a, b, c, d)
series = (a -> b -> c -> d -> (a, b, c, d)) -> Series m (a, b, c, d)
forall (m :: * -> *) a b c d e.
(Serial m a, Serial m b, Serial m c, Serial m d) =>
(a -> b -> c -> d -> e) -> Series m e
cons4 (,,,)
instance (CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d) => CoSerial m (a,b,c,d) where
  coseries :: Series m b -> Series m ((a, b, c, d) -> b)
coseries rs :: Series m b
rs = (a -> b -> c -> d -> b) -> (a, b, c, d) -> b
forall a b c d e. (a -> b -> c -> d -> e) -> (a, b, c, d) -> e
uncurry4 ((a -> b -> c -> d -> b) -> (a, b, c, d) -> b)
-> Series m (a -> b -> c -> d -> b) -> Series m ((a, b, c, d) -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (a -> b -> c -> d -> b)
forall (m :: * -> *) a b c d e.
(CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d) =>
Series m e -> Series m (a -> b -> c -> d -> e)
alts4 Series m b
rs

instance Monad m => Serial m Bool where
  series :: Series m Bool
series = Bool -> Series m Bool
forall a (m :: * -> *). a -> Series m a
cons0 Bool
True Series m Bool -> Series m Bool -> Series m Bool
forall (m :: * -> *) a.
Monad m =>
Series m a -> Series m a -> Series m a
\/ Bool -> Series m Bool
forall a (m :: * -> *). a -> Series m a
cons0 Bool
False
instance Monad m => CoSerial m Bool where
  coseries :: Series m b -> Series m (Bool -> b)
coseries rs :: Series m b
rs =
    Series m b
rs Series m b -> (b -> Series m (Bool -> b)) -> Series m (Bool -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \r1 :: b
r1 ->
    Series m b
rs Series m b -> (b -> Series m (Bool -> b)) -> Series m (Bool -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \r2 :: b
r2 ->
    (Bool -> b) -> Series m (Bool -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Bool -> b) -> Series m (Bool -> b))
-> (Bool -> b) -> Series m (Bool -> b)
forall a b. (a -> b) -> a -> b
$ \x :: Bool
x -> if Bool
x then b
r1 else b
r2

instance (Serial m a) => Serial m (Maybe a) where
  series :: Series m (Maybe a)
series = Maybe a -> Series m (Maybe a)
forall a (m :: * -> *). a -> Series m a
cons0 Maybe a
forall a. Maybe a
Nothing Series m (Maybe a) -> Series m (Maybe a) -> Series m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
Series m a -> Series m a -> Series m a
\/ (a -> Maybe a) -> Series m (Maybe a)
forall (m :: * -> *) a b. Serial m a => (a -> b) -> Series m b
cons1 a -> Maybe a
forall a. a -> Maybe a
Just
instance (CoSerial m a) => CoSerial m (Maybe a) where
  coseries :: Series m b -> Series m (Maybe a -> b)
coseries rs :: Series m b
rs =
    b -> (a -> b) -> Maybe a -> b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (b -> (a -> b) -> Maybe a -> b)
-> Series m b -> Series m ((a -> b) -> Maybe a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m b
forall (m :: * -> *) a. Series m a -> Series m a
alts0 Series m b
rs Series m ((a -> b) -> Maybe a -> b)
-> Series m (a -> b) -> Series m (Maybe a -> b)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs

instance (Serial m a, Serial m b) => Serial m (Either a b) where
  series :: Series m (Either a b)
series = (a -> Either a b) -> Series m (Either a b)
forall (m :: * -> *) a b. Serial m a => (a -> b) -> Series m b
cons1 a -> Either a b
forall a b. a -> Either a b
Left Series m (Either a b)
-> Series m (Either a b) -> Series m (Either a b)
forall (m :: * -> *) a.
Monad m =>
Series m a -> Series m a -> Series m a
\/ (b -> Either a b) -> Series m (Either a b)
forall (m :: * -> *) a b. Serial m a => (a -> b) -> Series m b
cons1 b -> Either a b
forall a b. b -> Either a b
Right
instance (CoSerial m a, CoSerial m b) => CoSerial m (Either a b) where
  coseries :: Series m b -> Series m (Either a b -> b)
coseries rs :: Series m b
rs =
    (a -> b) -> (b -> b) -> Either a b -> b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ((a -> b) -> (b -> b) -> Either a b -> b)
-> Series m (a -> b) -> Series m ((b -> b) -> Either a b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs Series m ((b -> b) -> Either a b -> b)
-> Series m (b -> b) -> Series m (Either a b -> b)
forall (m :: * -> *) a b.
Monad m =>
Series m (a -> b) -> Series m a -> Series m b
<~> Series m b -> Series m (b -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
alts1 Series m b
rs

instance Serial m a => Serial m [a] where
  series :: Series m [a]
series = [a] -> Series m [a]
forall a (m :: * -> *). a -> Series m a
cons0 [] Series m [a] -> Series m [a] -> Series m [a]
forall (m :: * -> *) a.
Monad m =>
Series m a -> Series m a -> Series m a
\/ (a -> [a] -> [a]) -> Series m [a]
forall (m :: * -> *) a b c.
(Serial m a, Serial m b) =>
(a -> b -> c) -> Series m c
cons2 (:)
instance CoSerial m a => CoSerial m [a] where
  coseries :: Series m b -> Series m ([a] -> b)
coseries rs :: Series m b
rs =
    Series m b -> Series m b
forall (m :: * -> *) a. Series m a -> Series m a
alts0 Series m b
rs Series m b -> (b -> Series m ([a] -> b)) -> Series m ([a] -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \y :: b
y ->
    Series m b -> Series m (a -> [a] -> b)
forall (m :: * -> *) a b c.
(CoSerial m a, CoSerial m b) =>
Series m c -> Series m (a -> b -> c)
alts2 Series m b
rs Series m (a -> [a] -> b)
-> ((a -> [a] -> b) -> Series m ([a] -> b)) -> Series m ([a] -> b)
forall (m :: * -> *) a b. MonadLogic m => m a -> (a -> m b) -> m b
>>- \f :: a -> [a] -> b
f ->
    ([a] -> b) -> Series m ([a] -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return (([a] -> b) -> Series m ([a] -> b))
-> ([a] -> b) -> Series m ([a] -> b)
forall a b. (a -> b) -> a -> b
$ \xs :: [a]
xs -> case [a]
xs of [] -> b
y; x :: a
x:xs' :: [a]
xs' -> a -> [a] -> b
f a
x [a]
xs'

instance (CoSerial m a, Serial m b) => Serial m (a->b) where
  series :: Series m (a -> b)
series = Series m b -> Series m (a -> b)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries Series m b
forall (m :: * -> *) a. Serial m a => Series m a
series
-- Thanks to Ralf Hinze for the definition of coseries
-- using the nest auxiliary.
instance (Serial m a, CoSerial m a, Serial m b, CoSerial m b) => CoSerial m (a->b) where
  coseries :: Series m b -> Series m ((a -> b) -> b)
coseries r :: Series m b
r = do
    [a]
args <- Series m a -> Series m [a]
forall (m :: * -> *) a. MonadLogic m => m a -> m [a]
unwind Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series

    [b] -> b
g <- Series m b -> [a] -> Series m ([b] -> b)
forall a b (m :: * -> *) c.
(Serial m b, CoSerial m b) =>
Series m c -> [a] -> Series m ([b] -> c)
nest Series m b
r [a]
args
    ((a -> b) -> b) -> Series m ((a -> b) -> b)
forall (m :: * -> *) a. Monad m => a -> m a
return (((a -> b) -> b) -> Series m ((a -> b) -> b))
-> ((a -> b) -> b) -> Series m ((a -> b) -> b)
forall a b. (a -> b) -> a -> b
$ \f :: a -> b
f -> [b] -> b
g ([b] -> b) -> [b] -> b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
args

    where

    nest :: forall a b m c . (Serial m b, CoSerial m b) => Series m c -> [a] -> Series m ([b] -> c)
    nest :: Series m c -> [a] -> Series m ([b] -> c)
nest rs :: Series m c
rs args :: [a]
args = do
      case [a]
args of
        [] -> c -> [b] -> c
forall a b. a -> b -> a
const (c -> [b] -> c) -> Series m c -> Series m ([b] -> c)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Series m c
rs
        _:rest :: [a]
rest -> do
          let sf :: Series m (b -> [b] -> c)
sf = Series m ([b] -> c) -> Series m (b -> [b] -> c)
forall (m :: * -> *) a b.
CoSerial m a =>
Series m b -> Series m (a -> b)
coseries (Series m ([b] -> c) -> Series m (b -> [b] -> c))
-> Series m ([b] -> c) -> Series m (b -> [b] -> c)
forall a b. (a -> b) -> a -> b
$ Series m c -> [a] -> Series m ([b] -> c)
forall a b (m :: * -> *) c.
(Serial m b, CoSerial m b) =>
Series m c -> [a] -> Series m ([b] -> c)
nest Series m c
rs [a]
rest
          b -> [b] -> c
f <- Series m (b -> [b] -> c)
sf
          ([b] -> c) -> Series m ([b] -> c)
forall (m :: * -> *) a. Monad m => a -> m a
return (([b] -> c) -> Series m ([b] -> c))
-> ([b] -> c) -> Series m ([b] -> c)
forall a b. (a -> b) -> a -> b
$ \(b :: b
b:bs :: [b]
bs) -> b -> [b] -> c
f b
b [b]
bs

-- show the extension of a function (in part, bounded both by
-- the number and depth of arguments)
instance (Serial Identity a, Show a, Show b) => Show (a -> b) where
  show :: (a -> b) -> [Char]
show f :: a -> b
f =
    if Depth
maxarheight Depth -> Depth -> Bool
forall a. Eq a => a -> a -> Bool
== 1
    Bool -> Bool -> Bool
&& Depth
sumarwidth Depth -> Depth -> Depth
forall a. Num a => a -> a -> a
+ [([Char], [Char])] -> Depth
forall (t :: * -> *) a. Foldable t => t a -> Depth
length [([Char], [Char])]
ars Depth -> Depth -> Depth
forall a. Num a => a -> a -> a
* [Char] -> Depth
forall (t :: * -> *) a. Foldable t => t a -> Depth
length "->;" Depth -> Depth -> Bool
forall a. Ord a => a -> a -> Bool
< Depth
widthLimit then
      "{"[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
      [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate ";" [[Char]
a[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++"->"[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char]
r | (a :: [Char]
a,r :: [Char]
r) <- [([Char], [Char])]
ars]
      [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++"}"
    else
      [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ [[Char]
a[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++"->\n"[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char] -> [Char]
indent [Char]
r | (a :: [Char]
a,r :: [Char]
r) <- [([Char], [Char])]
ars]
    where
    ars :: [([Char], [Char])]
ars = Depth -> [([Char], [Char])] -> [([Char], [Char])]
forall a. Depth -> [a] -> [a]
take Depth
lengthLimit [ (a -> [Char]
forall a. Show a => a -> [Char]
show a
x, b -> [Char]
forall a. Show a => a -> [Char]
show (a -> b
f a
x))
                           | a
x <- Depth -> Series Identity a -> [a]
forall a. Depth -> Series Identity a -> [a]
list Depth
depthLimit Series Identity a
forall (m :: * -> *) a. Serial m a => Series m a
series ]
    maxarheight :: Depth
maxarheight = [Depth] -> Depth
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum  [ Depth -> Depth -> Depth
forall a. Ord a => a -> a -> a
max ([Char] -> Depth
height [Char]
a) ([Char] -> Depth
height [Char]
r)
                           | (a :: [Char]
a,r :: [Char]
r) <- [([Char], [Char])]
ars ]
    sumarwidth :: Depth
sumarwidth = [Depth] -> Depth
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum       [ [Char] -> Depth
forall (t :: * -> *) a. Foldable t => t a -> Depth
length [Char]
a Depth -> Depth -> Depth
forall a. Num a => a -> a -> a
+ [Char] -> Depth
forall (t :: * -> *) a. Foldable t => t a -> Depth
length [Char]
r
                           | (a :: [Char]
a,r :: [Char]
r) <- [([Char], [Char])]
ars]
    indent :: [Char] -> [Char]
indent = [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ("  "[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines
    height :: [Char] -> Depth
height = [[Char]] -> Depth
forall (t :: * -> *) a. Foldable t => t a -> Depth
length ([[Char]] -> Depth) -> ([Char] -> [[Char]]) -> [Char] -> Depth
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines
    (widthLimit :: Depth
widthLimit,lengthLimit :: Depth
lengthLimit,depthLimit :: Depth
depthLimit) = (80,20,3)::(Int,Int,Depth)

-- }}}

------------------------------
-- Convenient wrappers
------------------------------
-- {{{

--------------------------------------------------------------------------
-- | @Positive x@: guarantees that @x \> 0@.
newtype Positive a = Positive { Positive a -> a
getPositive :: a }
 deriving (Positive a -> Positive a -> Bool
(Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Bool) -> Eq (Positive a)
forall a. Eq a => Positive a -> Positive a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Positive a -> Positive a -> Bool
$c/= :: forall a. Eq a => Positive a -> Positive a -> Bool
== :: Positive a -> Positive a -> Bool
$c== :: forall a. Eq a => Positive a -> Positive a -> Bool
Eq, Eq (Positive a)
Eq (Positive a) =>
(Positive a -> Positive a -> Ordering)
-> (Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Positive a)
-> (Positive a -> Positive a -> Positive a)
-> Ord (Positive a)
Positive a -> Positive a -> Bool
Positive a -> Positive a -> Ordering
Positive a -> Positive a -> Positive 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 (Positive a)
forall a. Ord a => Positive a -> Positive a -> Bool
forall a. Ord a => Positive a -> Positive a -> Ordering
forall a. Ord a => Positive a -> Positive a -> Positive a
min :: Positive a -> Positive a -> Positive a
$cmin :: forall a. Ord a => Positive a -> Positive a -> Positive a
max :: Positive a -> Positive a -> Positive a
$cmax :: forall a. Ord a => Positive a -> Positive a -> Positive a
>= :: Positive a -> Positive a -> Bool
$c>= :: forall a. Ord a => Positive a -> Positive a -> Bool
> :: Positive a -> Positive a -> Bool
$c> :: forall a. Ord a => Positive a -> Positive a -> Bool
<= :: Positive a -> Positive a -> Bool
$c<= :: forall a. Ord a => Positive a -> Positive a -> Bool
< :: Positive a -> Positive a -> Bool
$c< :: forall a. Ord a => Positive a -> Positive a -> Bool
compare :: Positive a -> Positive a -> Ordering
$ccompare :: forall a. Ord a => Positive a -> Positive a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Positive a)
Ord)

instance Real a => Real (Positive a) where
  toRational :: Positive a -> Rational
toRational (Positive x :: a
x) = a -> Rational
forall a. Real a => a -> Rational
toRational a
x

instance Enum a => Enum (Positive a) where
  toEnum :: Depth -> Positive a
toEnum x :: Depth
x = a -> Positive a
forall a. a -> Positive a
Positive (Depth -> a
forall a. Enum a => Depth -> a
toEnum Depth
x)
  fromEnum :: Positive a -> Depth
fromEnum (Positive x :: a
x) = a -> Depth
forall a. Enum a => a -> Depth
fromEnum a
x

instance Num a => Num (Positive a) where
  Positive x :: a
x + :: Positive a -> Positive a -> Positive a
+ Positive y :: a
y = a -> Positive a
forall a. a -> Positive a
Positive (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
y)
  Positive x :: a
x * :: Positive a -> Positive a -> Positive a
* Positive y :: a
y = a -> Positive a
forall a. a -> Positive a
Positive (a
x a -> a -> a
forall a. Num a => a -> a -> a
* a
y)
  negate :: Positive a -> Positive a
negate (Positive x :: a
x) = a -> Positive a
forall a. a -> Positive a
Positive (a -> a
forall a. Num a => a -> a
negate a
x)
  abs :: Positive a -> Positive a
abs (Positive x :: a
x) = a -> Positive a
forall a. a -> Positive a
Positive (a -> a
forall a. Num a => a -> a
abs a
x)
  signum :: Positive a -> Positive a
signum (Positive x :: a
x) = a -> Positive a
forall a. a -> Positive a
Positive (a -> a
forall a. Num a => a -> a
signum a
x)
  fromInteger :: Integer -> Positive a
fromInteger x :: Integer
x = a -> Positive a
forall a. a -> Positive a
Positive (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
x)

instance Integral a => Integral (Positive a) where
  quotRem :: Positive a -> Positive a -> (Positive a, Positive a)
quotRem (Positive x :: a
x) (Positive y :: a
y) = (a -> Positive a
forall a. a -> Positive a
Positive a
q, a -> Positive a
forall a. a -> Positive a
Positive a
r)
    where
      (q :: a
q, r :: a
r) = a
x a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` a
y
  toInteger :: Positive a -> Integer
toInteger (Positive x :: a
x) = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x

instance (Num a, Ord a, Serial m a) => Serial m (Positive a) where
  series :: Series m (Positive a)
series = a -> Positive a
forall a. a -> Positive a
Positive (a -> Positive a) -> Series m a -> Series m (Positive a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series Series m a -> (a -> Bool) -> Series m a
forall (m :: * -> *) a. Series m a -> (a -> Bool) -> Series m a
`suchThat` (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> 0)

instance Show a => Show (Positive a) where
  showsPrec :: Depth -> Positive a -> [Char] -> [Char]
showsPrec n :: Depth
n (Positive x :: a
x) = Depth -> a -> [Char] -> [Char]
forall a. Show a => Depth -> a -> [Char] -> [Char]
showsPrec Depth
n a
x

-- | @NonNegative x@: guarantees that @x \>= 0@.
newtype NonNegative a = NonNegative { NonNegative a -> a
getNonNegative :: a }
 deriving (NonNegative a -> NonNegative a -> Bool
(NonNegative a -> NonNegative a -> Bool)
-> (NonNegative a -> NonNegative a -> Bool) -> Eq (NonNegative a)
forall a. Eq a => NonNegative a -> NonNegative a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NonNegative a -> NonNegative a -> Bool
$c/= :: forall a. Eq a => NonNegative a -> NonNegative a -> Bool
== :: NonNegative a -> NonNegative a -> Bool
$c== :: forall a. Eq a => NonNegative a -> NonNegative a -> Bool
Eq, Eq (NonNegative a)
Eq (NonNegative a) =>
(NonNegative a -> NonNegative a -> Ordering)
-> (NonNegative a -> NonNegative a -> Bool)
-> (NonNegative a -> NonNegative a -> Bool)
-> (NonNegative a -> NonNegative a -> Bool)
-> (NonNegative a -> NonNegative a -> Bool)
-> (NonNegative a -> NonNegative a -> NonNegative a)
-> (NonNegative a -> NonNegative a -> NonNegative a)
-> Ord (NonNegative a)
NonNegative a -> NonNegative a -> Bool
NonNegative a -> NonNegative a -> Ordering
NonNegative a -> NonNegative a -> NonNegative 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 (NonNegative a)
forall a. Ord a => NonNegative a -> NonNegative a -> Bool
forall a. Ord a => NonNegative a -> NonNegative a -> Ordering
forall a. Ord a => NonNegative a -> NonNegative a -> NonNegative a
min :: NonNegative a -> NonNegative a -> NonNegative a
$cmin :: forall a. Ord a => NonNegative a -> NonNegative a -> NonNegative a
max :: NonNegative a -> NonNegative a -> NonNegative a
$cmax :: forall a. Ord a => NonNegative a -> NonNegative a -> NonNegative a
>= :: NonNegative a -> NonNegative a -> Bool
$c>= :: forall a. Ord a => NonNegative a -> NonNegative a -> Bool
> :: NonNegative a -> NonNegative a -> Bool
$c> :: forall a. Ord a => NonNegative a -> NonNegative a -> Bool
<= :: NonNegative a -> NonNegative a -> Bool
$c<= :: forall a. Ord a => NonNegative a -> NonNegative a -> Bool
< :: NonNegative a -> NonNegative a -> Bool
$c< :: forall a. Ord a => NonNegative a -> NonNegative a -> Bool
compare :: NonNegative a -> NonNegative a -> Ordering
$ccompare :: forall a. Ord a => NonNegative a -> NonNegative a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (NonNegative a)
Ord)

instance Real a => Real (NonNegative a) where
  toRational :: NonNegative a -> Rational
toRational (NonNegative x :: a
x) = a -> Rational
forall a. Real a => a -> Rational
toRational a
x

instance Enum a => Enum (NonNegative a) where
  toEnum :: Depth -> NonNegative a
toEnum x :: Depth
x = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (Depth -> a
forall a. Enum a => Depth -> a
toEnum Depth
x)
  fromEnum :: NonNegative a -> Depth
fromEnum (NonNegative x :: a
x) = a -> Depth
forall a. Enum a => a -> Depth
fromEnum a
x

instance Num a => Num (NonNegative a) where
  NonNegative x :: a
x + :: NonNegative a -> NonNegative a -> NonNegative a
+ NonNegative y :: a
y = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
y)
  NonNegative x :: a
x * :: NonNegative a -> NonNegative a -> NonNegative a
* NonNegative y :: a
y = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a
x a -> a -> a
forall a. Num a => a -> a -> a
* a
y)
  negate :: NonNegative a -> NonNegative a
negate (NonNegative x :: a
x) = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a -> a
forall a. Num a => a -> a
negate a
x)
  abs :: NonNegative a -> NonNegative a
abs (NonNegative x :: a
x) = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a -> a
forall a. Num a => a -> a
abs a
x)
  signum :: NonNegative a -> NonNegative a
signum (NonNegative x :: a
x) = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a -> a
forall a. Num a => a -> a
signum a
x)
  fromInteger :: Integer -> NonNegative a
fromInteger x :: Integer
x = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
x)

instance Integral a => Integral (NonNegative a) where
  quotRem :: NonNegative a -> NonNegative a -> (NonNegative a, NonNegative a)
quotRem (NonNegative x :: a
x) (NonNegative y :: a
y) = (a -> NonNegative a
forall a. a -> NonNegative a
NonNegative a
q, a -> NonNegative a
forall a. a -> NonNegative a
NonNegative a
r)
    where
      (q :: a
q, r :: a
r) = a
x a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` a
y
  toInteger :: NonNegative a -> Integer
toInteger (NonNegative x :: a
x) = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x

instance (Num a, Ord a, Serial m a) => Serial m (NonNegative a) where
  series :: Series m (NonNegative a)
series = a -> NonNegative a
forall a. a -> NonNegative a
NonNegative (a -> NonNegative a) -> Series m a -> Series m (NonNegative a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Series m a
forall (m :: * -> *) a. Serial m a => Series m a
series Series m a -> (a -> Bool) -> Series m a
forall (m :: * -> *) a. Series m a -> (a -> Bool) -> Series m a
`suchThat` (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 0)

instance Show a => Show (NonNegative a) where
  showsPrec :: Depth -> NonNegative a -> [Char] -> [Char]
showsPrec n :: Depth
n (NonNegative x :: a
x) = Depth -> a -> [Char] -> [Char]
forall a. Show a => Depth -> a -> [Char] -> [Char]
showsPrec Depth
n a
x

-- | @NonEmpty xs@: guarantees that @xs@ is not null
newtype NonEmpty a = NonEmpty { NonEmpty a -> [a]
getNonEmpty :: [a] }

instance (Serial m a) => Serial m (NonEmpty a) where
  series :: Series m (NonEmpty a)
series = [a] -> NonEmpty a
forall a. [a] -> NonEmpty a
NonEmpty ([a] -> NonEmpty a) -> Series m [a] -> Series m (NonEmpty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> [a] -> [a]) -> Series m [a]
forall (m :: * -> *) a b c.
(Serial m a, Serial m b) =>
(a -> b -> c) -> Series m c
cons2 (:)

instance Show a => Show (NonEmpty a) where
  showsPrec :: Depth -> NonEmpty a -> [Char] -> [Char]
showsPrec n :: Depth
n (NonEmpty x :: [a]
x) = Depth -> [a] -> [Char] -> [Char]
forall a. Show a => Depth -> a -> [Char] -> [Char]
showsPrec Depth
n [a]
x

-- }}}