module Control.Sized (module Control.Applicative, Sized(..), kbits) where
import Control.Applicative
class Alternative f => Sized f where
pay :: f a -> f a
pair :: f a -> f b -> f (a,b)
pair f a
a f b
b = (,) (a -> b -> (a, b)) -> f a -> f (b -> (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (b -> (a, b)) -> f b -> f (a, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f b
b
aconcat :: [f a] -> f a
aconcat [] = f a
forall (f :: * -> *) a. Alternative f => f a
empty
aconcat [f a]
xs = (f a -> f a -> f a) -> [f a] -> f a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) [f a]
xs
fin :: Integer -> f Integer
fin Integer
n = [f Integer] -> f Integer
forall (f :: * -> *) a. Sized f => [f a] -> f a
aconcat ((Integer -> f Integer) -> [Integer] -> [f Integer]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> f Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Integer
0..Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
1])
finSized :: Integer -> f Integer
finSized = Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
stdFinBits
naturals :: f Integer
naturals = f Integer
forall (f :: * -> *). Sized f => f Integer
stdNaturals
stdNaturals :: Sized f => f Integer
stdNaturals :: f Integer
stdNaturals = Integer -> f Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
0 f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> f Integer
forall (f :: * -> *) t. (Sized f, Integral t) => t -> f Integer
go Integer
0 where
go :: t -> f Integer
go t
n = f Integer -> f Integer
forall (f :: * -> *) a. Sized f => f a -> f a
pay (f Integer -> f Integer) -> f Integer -> f Integer
forall a b. (a -> b) -> a -> b
$ ((Integer
2Integer -> t -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^t
n)Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Integer) -> f Integer -> f Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
fin (Integer
2Integer -> t -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^t
n) f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (t -> f Integer
go (t
nt -> t -> t
forall a. Num a => a -> a -> a
+t
1))
stdNaturals' :: Sized f => f Integer
stdNaturals' :: f Integer
stdNaturals' = Integer -> f Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
0 f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
go Integer
1 where
go :: Integer -> f Integer
go Integer
n = f Integer -> f Integer
forall (f :: * -> *) a. Sized f => f a -> f a
pay (f Integer -> f Integer) -> f Integer -> f Integer
forall a b. (a -> b) -> a -> b
$ (Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Integer) -> f Integer -> f Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
fin Integer
n f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> f Integer
go (Integer
2Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
n)
stdFinBits :: Sized f => Integer -> f Integer
stdFinBits :: Integer -> f Integer
stdFinBits Integer
i | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
0 = f Integer
forall (f :: * -> *) a. Alternative f => f a
empty
stdFinBits Integer
i = Integer -> f Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
0 f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
go Integer
1 where
go :: Integer -> f Integer
go Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
lim = f Integer -> f Integer
forall (f :: * -> *) a. Sized f => f a -> f a
pay (f Integer -> f Integer) -> f Integer -> f Integer
forall a b. (a -> b) -> a -> b
$ (Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Integer) -> f Integer -> f Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
fin Integer
n f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> f Integer
go (Integer
2Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
n)
go Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
i = f Integer
forall (f :: * -> *) a. Alternative f => f a
empty
go Integer
n = f Integer -> f Integer
forall (f :: * -> *) a. Sized f => f a -> f a
pay (f Integer -> f Integer) -> f Integer -> f Integer
forall a b. (a -> b) -> a -> b
$ (Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+) (Integer -> Integer) -> f Integer -> f Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
fin (Integer
iInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
n)
lim :: Integer
lim = Integer
i Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
2
kbits :: Sized f => Int -> f Integer
kbits :: Int -> f Integer
kbits Int
k = Integer -> f Integer
forall (f :: * -> *). Sized f => Integer -> f Integer
finSized (Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
k)