-- | Robert Morris. \"A Similarity Index for Pitch-Class
-- Sets\". Perspectives of New Music, 18(2):445-460, 1980.
module Music.Theory.Metric.Morris_1980 where

import Data.Int {- base -}
import Data.Ratio {- base -}

import Music.Theory.Z {- hmt -}
import Music.Theory.Z.Forte_1973 {- hmt -}

-- | SIM
--
-- > icv 12 [0,1,3,6] == [1,1,2,0,1,1] && icv 12 [0,2,4,7] == [0,2,1,1,2,0]
-- > sim [0,1,3,6] [0,2,4,7] == 6
-- > sim [0,1,2,4,5,8] [0,1,3,7] == 9
sim :: Integral a => [Int8] -> [Int8] -> a
sim :: forall a. Integral a => [Int8] -> [Int8] -> a
sim [Int8]
r [Int8]
s =
    let r' :: [a]
r' = forall i n. (Integral i, Num n) => Z i -> [i] -> [n]
z_icv forall i. Num i => Z i
z12 [Int8]
r
        s' :: [a]
s' = forall i n. (Integral i, Num n) => Z i -> [i] -> [n]
z_icv forall i. Num i => Z i
z12 [Int8]
s
        t :: [a]
t = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (-) [a]
r' [a]
s'
    in forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
abs [a]
t)

-- | ASIM
--
-- > asim [0,1,3,6] [0,2,4,7] == 6/12
-- > asim [0,1,2,4,5,8] [0,1,3,7] == 9/21
-- > asim [0,1,2,3,4] [0,1,4,5,7] == 2/5
-- > asim [0,1,2,3,4] [0,2,4,6,8] == 3/5
-- > asim [0,1,4,5,7] [0,2,4,6,8] == 3/5
asim :: (Integral n) => [Int8] -> [Int8] -> Ratio n
asim :: forall n. Integral n => [Int8] -> [Int8] -> Ratio n
asim [Int8]
r [Int8]
s =
    let r' :: [n]
r' = forall i n. (Integral i, Num n) => Z i -> [i] -> [n]
z_icv forall i. Num i => Z i
z12 [Int8]
r
        s' :: [n]
s' = forall i n. (Integral i, Num n) => Z i -> [i] -> [n]
z_icv forall i. Num i => Z i
z12 [Int8]
s
    in forall a. Integral a => [Int8] -> [Int8] -> a
sim [Int8]
r [Int8]
s forall a. Integral a => a -> a -> Ratio a
% (forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [n]
r' forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [n]
s')