module Music.Theory.Graph.Lgl where
import Data.Bifunctor
import Data.List
import qualified Music.Theory.Graph.Type as T
import qualified Music.Theory.Show as T
import qualified Music.Theory.Tuple as T
type Ncol_Ent t = ((t,t),Maybe Double)
type Ncol t = [Ncol_Ent t]
ncol_parse :: Read t => String -> Ncol_Ent t
ncol_parse :: forall t. Read t => String -> Ncol_Ent t
ncol_parse String
s =
case String -> [String]
words String
s of
[String
i,String
j] -> ((forall a. Read a => String -> a
read String
i,forall a. Read a => String -> a
read String
j),forall a. Maybe a
Nothing)
[String
i,String
j,String
k] -> ((forall a. Read a => String -> a
read String
i,forall a. Read a => String -> a
read String
j),forall a. Read a => String -> a
read String
k)
[String]
_ -> forall a. HasCallStack => String -> a
error String
"ncol_parse"
ncol_load :: Read t => FilePath -> IO (Ncol t)
ncol_load :: forall t. Read t => String -> IO (Ncol t)
ncol_load = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a -> b) -> [a] -> [b]
map forall t. Read t => String -> Ncol_Ent t
ncol_parse forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO String
readFile
ncol_load_int :: FilePath -> IO (Ncol Int)
ncol_load_int :: String -> IO (Ncol Int)
ncol_load_int = forall t. Read t => String -> IO (Ncol t)
ncol_load
ncol_ent_format :: Show t => Int -> Ncol_Ent t -> String
ncol_ent_format :: forall t. Show t => Int -> Ncol_Ent t -> String
ncol_ent_format Int
k ((t
i,t
j),Maybe Double
w) = [String] -> String
unwords (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [t
i,t
j]) forall a. [a] -> [a] -> [a]
++ forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((Char
' 'forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double -> String
T.double_pp Int
k) Maybe Double
w
ncol_store :: Show t => Int -> FilePath -> Ncol t -> IO ()
ncol_store :: forall t. Show t => Int -> String -> Ncol t -> IO ()
ncol_store Int
k String
fn Ncol t
dat = String -> String -> IO ()
writeFile String
fn ([String] -> String
unlines (forall a b. (a -> b) -> [a] -> [b]
map (forall t. Show t => Int -> Ncol_Ent t -> String
ncol_ent_format Int
k) Ncol t
dat))
ncol_store_int :: Int -> FilePath -> Ncol Int -> IO ()
ncol_store_int :: Int -> String -> Ncol Int -> IO ()
ncol_store_int = forall t. Show t => Int -> String -> Ncol t -> IO ()
ncol_store
ncol_rewrite_eset :: Ord t => [(t,t)] -> [(t,t)]
ncol_rewrite_eset :: forall t. Ord t => [(t, t)] -> [(t, t)]
ncol_rewrite_eset [(t, t)]
e = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. Eq a => a -> a -> Bool
(/=)) (forall a. Eq a => [a] -> [a]
nub (forall a. Ord a => [a] -> [a]
sort (forall a b. (a -> b) -> [a] -> [b]
map forall t. Ord t => (t, t) -> (t, t)
T.t2_sort [(t, t)]
e)))
eset_to_ncol :: Ord t => [(t,t)] -> Ncol t
eset_to_ncol :: forall t. Ord t => [(t, t)] -> Ncol t
eset_to_ncol = forall a b. (a -> b) -> [a] -> [b]
map (\(t, t)
e -> ((t, t)
e,forall a. Maybe a
Nothing)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Ord t => [(t, t)] -> [(t, t)]
ncol_rewrite_eset
ncol_to_eset :: Ncol t -> [(t,t)]
ncol_to_eset :: forall t. Ncol t -> [(t, t)]
ncol_to_eset = forall a b. (a -> b) -> [a] -> [b]
map (\((t, t)
e,Maybe Double
w) -> case Maybe Double
w of {Maybe Double
Nothing -> (t, t)
e;Maybe Double
_ -> forall a. HasCallStack => String -> a
error String
"ncol_to_eset?"})
ncol_store_eset :: (Ord t,Show t) => FilePath -> [(t,t)] -> IO ()
ncol_store_eset :: forall t. (Ord t, Show t) => String -> [(t, t)] -> IO ()
ncol_store_eset String
fn = forall t. Show t => Int -> String -> Ncol t -> IO ()
ncol_store forall a. HasCallStack => a
undefined String
fn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Ord t => [(t, t)] -> Ncol t
eset_to_ncol
type Lgl t = [(t,[(t,Maybe Double)])]
lgl_format :: Show t => Int -> Lgl t -> String
lgl_format :: forall t. Show t => Int -> Lgl t -> String
lgl_format Int
k =
let f :: (a, Maybe Double) -> String
f (a
i,Maybe Double
j) = forall a. Show a => a -> String
show a
i forall a. [a] -> [a] -> [a]
++ forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((Char
' ' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Double -> String
T.double_pp Int
k) Maybe Double
j
g :: (a, [(a, Maybe Double)]) -> String
g (a
i,[(a, Maybe Double)]
j) = [String] -> String
unlines ((Char
'#' forall a. a -> [a] -> [a]
: Char
' ' forall a. a -> [a] -> [a]
: forall a. Show a => a -> String
show a
i) forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map forall {a}. Show a => (a, Maybe Double) -> String
f [(a, Maybe Double)]
j)
in forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall {a} {a}.
(Show a, Show a) =>
(a, [(a, Maybe Double)]) -> String
g
lgl_store :: Show t => Int -> FilePath -> Lgl t -> IO ()
lgl_store :: forall t. Show t => Int -> String -> Lgl t -> IO ()
lgl_store Int
k String
fn = String -> String -> IO ()
writeFile String
fn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Show t => Int -> Lgl t -> String
lgl_format Int
k
adj_to_lgl :: T.Adj t -> Lgl t
adj_to_lgl :: forall t. Adj t -> Lgl t
adj_to_lgl = forall a b. (a -> b) -> [a] -> [b]
map (\(t
i,[t]
j) -> (t
i,forall a b. [a] -> [b] -> [(a, b)]
zip [t]
j (forall a. a -> [a]
repeat forall a. Maybe a
Nothing)))
lgl_to_adj :: Lgl t -> T.Adj t
lgl_to_adj :: forall t. Lgl t -> Adj t
lgl_to_adj = forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (forall a b. (a -> b) -> [a] -> [b]
map (\(t
k,Maybe Double
w) -> case Maybe Double
w of {Maybe Double
Nothing -> t
k;Maybe Double
_ -> forall a. HasCallStack => String -> a
error String
"lgl_to_adj?"})))
lgl_store_adj :: Show t => FilePath -> T.Adj t -> IO ()
lgl_store_adj :: forall t. Show t => String -> Adj t -> IO ()
lgl_store_adj String
fn = forall t. Show t => Int -> String -> Lgl t -> IO ()
lgl_store forall a. HasCallStack => a
undefined String
fn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Adj t -> Lgl t
adj_to_lgl