{-# Language FlexibleInstances #-}
module Sound.Sc3.Common.Uid where
import Data.Functor.Identity
import Data.List
import qualified Data.Unique as Unique
import qualified Control.Monad.Trans.Reader as Reader
import qualified Control.Monad.Trans.State as State
import qualified Data.Digest.Murmur32 as Murmur32
import qualified Sound.Sc3.Common.Base as Base
type Id = Int
class (Functor m, Applicative m, Monad m) => Uid m where
generateUid :: m Int
instance Uid (State.StateT Int Identity) where
generateUid :: StateT Int Identity Int
generateUid = forall (m :: * -> *) s. Monad m => StateT s m s
State.get forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
n -> forall (m :: * -> *) s. Monad m => s -> StateT s m ()
State.put (Int
n forall a. Num a => a -> a -> a
+ Int
1) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
instance Uid IO where
generateUid :: IO Int
generateUid = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Unique -> Int
Unique.hashUnique IO Unique
Unique.newUnique
instance Uid m => Uid (Reader.ReaderT t m) where
generateUid :: ReaderT t m Int
generateUid = forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
Reader.ReaderT (forall a b. a -> b -> a
const forall (m :: * -> *). Uid m => m Int
generateUid)
type Uid_St = State.State Int
uid_id_eval :: Identity t -> t
uid_id_eval :: forall t. Identity t -> t
uid_id_eval = forall t. Identity t -> t
runIdentity
uid_st_eval :: Uid_St t -> t
uid_st_eval :: forall t. Uid_St t -> t
uid_st_eval Uid_St t
x = forall s a. State s a -> s -> a
State.evalState Uid_St t
x Int
0
uid_st_seq :: [Uid_St t] -> ([t],Int)
uid_st_seq :: forall t. [Uid_St t] -> ([t], Int)
uid_st_seq =
let swap :: (b, a) -> (a, b)
swap (b
p,a
q) = (a
q,b
p)
step_f :: a -> State a b -> (a, b)
step_f a
n State a b
x = forall {b} {a}. (b, a) -> (a, b)
swap (forall s a. State s a -> s -> (a, s)
State.runState State a b
x a
n)
in forall {b} {a}. (b, a) -> (a, b)
swap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL forall {a} {b}. a -> State a b -> (a, b)
step_f Int
0
uid_st_seq_ :: [Uid_St t] -> [t]
uid_st_seq_ :: forall t. [Uid_St t] -> [t]
uid_st_seq_ = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. [Uid_St t] -> ([t], Int)
uid_st_seq
liftUid1 :: Uid m => (Int -> Base.Fn1 a b) -> Base.Fn1 a (m b)
liftUid1 :: forall (m :: * -> *) a b. Uid m => (Int -> Fn1 a b) -> Fn1 a (m b)
liftUid1 Int -> Fn1 a b
fn a
a = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn1 a b
fn Int
z a
a)
liftUid2 :: Uid m => (Int -> Base.Fn2 a b c) -> Base.Fn2 a b (m c)
liftUid2 :: forall (m :: * -> *) a b c.
Uid m =>
(Int -> Fn2 a b c) -> Fn2 a b (m c)
liftUid2 Int -> Fn2 a b c
fn a
a b
b = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn2 a b c
fn Int
z a
a b
b)
liftUid3 :: Uid m => (Int -> Base.Fn3 a b c d) -> Base.Fn3 a b c (m d)
liftUid3 :: forall (m :: * -> *) a b c d.
Uid m =>
(Int -> Fn3 a b c d) -> Fn3 a b c (m d)
liftUid3 Int -> Fn3 a b c d
fn a
a b
b c
c = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn3 a b c d
fn Int
z a
a b
b c
c)
liftUid4 :: Uid m => (Int -> Base.Fn4 a b c d e) -> Base.Fn4 a b c d (m e)
liftUid4 :: forall (m :: * -> *) a b c d e.
Uid m =>
(Int -> Fn4 a b c d e) -> Fn4 a b c d (m e)
liftUid4 Int -> Fn4 a b c d e
fn a
a b
b c
c d
d = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn4 a b c d e
fn Int
z a
a b
b c
c d
d)
liftUid5 :: Uid m => (Int -> Base.Fn5 a b c d e f) -> Base.Fn5 a b c d e (m f)
liftUid5 :: forall (m :: * -> *) a b c d e f.
Uid m =>
(Int -> Fn5 a b c d e f) -> Fn5 a b c d e (m f)
liftUid5 Int -> Fn5 a b c d e f
fn a
a b
b c
c d
d e
e = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn5 a b c d e f
fn Int
z a
a b
b c
c d
d e
e)
liftUid6 :: Uid m => (Int -> Base.Fn6 a b c d e f g) -> Base.Fn6 a b c d e f (m g)
liftUid6 :: forall (m :: * -> *) a b c d e f g.
Uid m =>
(Int -> Fn6 a b c d e f g) -> Fn6 a b c d e f (m g)
liftUid6 Int -> Fn6 a b c d e f g
fn a
a b
b c
c d
d e
e f
f = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn6 a b c d e f g
fn Int
z a
a b
b c
c d
d e
e f
f)
liftUid10 :: Uid m => (Int -> Base.Fn10 a b c d e f g h i j k) -> Base.Fn10 a b c d e f g h i j (m k)
liftUid10 :: forall (m :: * -> *) a b c d e f g h i j k.
Uid m =>
(Int -> Fn10 a b c d e f g h i j k)
-> Fn10 a b c d e f g h i j (m k)
liftUid10 Int -> Fn10 a b c d e f g h i j k
fn a
a b
b c
c d
d e
e f
f g
g h
h i
i j
j = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn10 a b c d e f g h i j k
fn Int
z a
a b
b c
c d
d e
e f
f g
g h
h i
i j
j)
liftUid11 :: Uid m => (Int -> Base.Fn11 a b c d e f g h i j k l) -> Base.Fn11 a b c d e f g h i j k (m l)
liftUid11 :: forall (m :: * -> *) a b c d e f g h i j k l.
Uid m =>
(Int -> Fn11 a b c d e f g h i j k l)
-> Fn11 a b c d e f g h i j k (m l)
liftUid11 Int -> Fn11 a b c d e f g h i j k l
fn a
a b
b c
c d
d e
e f
f g
g h
h i
i j
j k
k = do
Int
z <- forall (m :: * -> *). Uid m => m Int
generateUid
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Fn11 a b c d e f g h i j k l
fn Int
z a
a b
b c
c d
d e
e f
f g
g h
h i
i j
j k
k)
class Murmur32.Hashable32 a => ID a where
resolveID :: a -> Id
resolveID = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash32 -> Word32
Murmur32.asWord32 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Hashable32 a => a -> Hash32
Murmur32.hash32
instance ID Char where
instance ID Int where resolveID :: Int -> Int
resolveID = forall a. a -> a
id
instance (ID p,ID q) => ID (p,q) where
instance (ID p,ID q,ID r) => ID (p,q,r) where
id_seq :: ID a => Int -> a -> [Id]
id_seq :: forall a. ID a => Int -> a -> [Int]
id_seq Int
n a
x = forall a. Int -> [a] -> [a]
take Int
n [forall a. ID a => a -> Int
resolveID a
x ..]