module Music.Theory.Set.List where
import Control.Monad
import Data.List
import qualified Math.Combinatorics.Multiset as M
import qualified Music.Theory.List as T
set :: (Ord a) => [a] -> [a]
set :: forall a. Ord a => [a] -> [a]
set = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> [a]
sort
n_powerset :: Integral n => n -> n
n_powerset :: forall n. Integral n => n -> n
n_powerset = forall a b. (Num a, Integral b) => a -> b -> a
(^) n
2
powerset :: [a] -> [[a]]
powerset :: forall a. [a] -> [[a]]
powerset = forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (forall a b. a -> b -> a
const [Bool
True,Bool
False])
powerset_sorted :: Ord a => [a] -> [[a]]
powerset_sorted :: forall a. Ord a => [a] -> [[a]]
powerset_sorted = forall a. [a] -> [a]
tail forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b c a. (Ord b, Ord c) => (a -> b) -> (a -> c) -> [a] -> [a]
T.sort_by_two_stage_on forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [[a]]
powerset
pairs :: [a] -> [(a,a)]
pairs :: forall a. [a] -> [(a, a)]
pairs [a]
s =
case [a]
s of
[] -> []
a
x:[a]
s' -> [(a
x,a
y) | a
y <- [a]
s'] forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [(a, a)]
pairs [a]
s'
triples :: [a] -> [(a,a,a)]
triples :: forall a. [a] -> [(a, a, a)]
triples [a]
s =
case [a]
s of
[] -> []
a
x:[a]
s' -> [(a
x,a
y,a
z) | (a
y,a
z) <- forall a. [a] -> [(a, a)]
pairs [a]
s'] forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [(a, a, a)]
triples [a]
s'
expand_set :: (Ord a) => Int -> [a] -> [[a]]
expand_set :: forall a. Ord a => Int -> [a] -> [[a]]
expand_set Int
n [a]
xs =
if forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs forall a. Ord a => a -> a -> Bool
>= Int
n
then [[a]
xs]
else forall a. Eq a => [a] -> [a]
nub (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall a. Ord a => Int -> [a] -> [[a]]
expand_set Int
n) [forall a. Ord a => [a] -> [a]
sort (a
y forall a. a -> [a] -> [a]
: [a]
xs) | a
y <- [a]
xs])
partitions :: Eq a => [a] -> [[[a]]]
partitions :: forall a. Eq a => [a] -> [[[a]]]
partitions = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map forall a. Multiset a -> [a]
M.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Multiset a -> [a]
M.toList) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Multiset a -> [Multiset (Multiset a)]
M.partitions forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> Multiset a
M.fromListEq
cartesian_product :: [a] -> [b] -> [(a,b)]
cartesian_product :: forall a b. [a] -> [b] -> [(a, b)]
cartesian_product [a]
p [b]
q = [(a
i,b
j) | a
i <- [a]
p, b
j <- [b]
q]
nfold_cartesian_product :: [[a]] -> [[a]]
nfold_cartesian_product :: forall a. [[a]] -> [[a]]
nfold_cartesian_product [[a]]
l =
case [[a]]
l of
[] -> []
[[a]
_] -> []
[[a]
x,[a]
y] -> [[a
i,a
j] | a
i <- [a]
x, a
j <- [a]
y]
[a]
x:[[a]]
l' -> forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\a
e -> forall a b. (a -> b) -> [a] -> [b]
map (a
e forall a. a -> [a] -> [a]
:) (forall a. [[a]] -> [[a]]
nfold_cartesian_product [[a]]
l')) [a]
x
multiset_cycles :: Ord t => [t] -> [[t]]
multiset_cycles :: forall a. Ord a => [a] -> [[a]]
multiset_cycles = forall a. Multiset a -> [[a]]
M.cycles forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> Multiset a
M.fromList