{-# LANGUAGE
CPP, BangPatterns,
ScopedTypeVariables, ExistentialQuantification,
DataKinds, KindSignatures, Rank2Types #-}
module Math.Combinat.Groups.Braid.NF
(
BraidNF (..)
, nfReprWord
, braidNormalForm
, braidNormalForm'
, braidNormalFormNaive'
, permWordStartingSet
, permWordFinishingSet
, permutationStartingSet
, permutationFinishingSet
)
where
import Data.Proxy
import GHC.TypeLits
import Control.Monad
import Data.List ( mapAccumL , foldl' , (\\) )
import Data.Array.Unboxed
import Data.Array.ST
import Data.Array.IArray
import Data.Array.MArray
import Data.Array.Unsafe
import Data.Array.Base
import Control.Monad.ST
import Math.Combinat.Helper
import Math.Combinat.Sign
import Math.Combinat.Permutations ( Permutation(..) , (!!!) , isIdentityPermutation , isReversePermutation )
import qualified Math.Combinat.Permutations as P
import Math.Combinat.Groups.Braid
data BraidNF (n :: Nat) = BraidNF
{ forall (n :: Nat). BraidNF n -> Int
_nfDeltaExp :: !Int
, forall (n :: Nat). BraidNF n -> [Permutation]
_nfPerms :: [Permutation]
}
deriving (BraidNF n -> BraidNF n -> Bool
forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BraidNF n -> BraidNF n -> Bool
$c/= :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
== :: BraidNF n -> BraidNF n -> Bool
$c== :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
Eq,BraidNF n -> BraidNF n -> Bool
BraidNF n -> BraidNF n -> Ordering
forall (n :: Nat). Eq (BraidNF n)
forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
forall (n :: Nat). BraidNF n -> BraidNF n -> Ordering
forall (n :: Nat). BraidNF n -> BraidNF n -> BraidNF n
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BraidNF n -> BraidNF n -> BraidNF n
$cmin :: forall (n :: Nat). BraidNF n -> BraidNF n -> BraidNF n
max :: BraidNF n -> BraidNF n -> BraidNF n
$cmax :: forall (n :: Nat). BraidNF n -> BraidNF n -> BraidNF n
>= :: BraidNF n -> BraidNF n -> Bool
$c>= :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
> :: BraidNF n -> BraidNF n -> Bool
$c> :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
<= :: BraidNF n -> BraidNF n -> Bool
$c<= :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
< :: BraidNF n -> BraidNF n -> Bool
$c< :: forall (n :: Nat). BraidNF n -> BraidNF n -> Bool
compare :: BraidNF n -> BraidNF n -> Ordering
$ccompare :: forall (n :: Nat). BraidNF n -> BraidNF n -> Ordering
Ord,Int -> BraidNF n -> ShowS
forall (n :: Nat). Int -> BraidNF n -> ShowS
forall (n :: Nat). [BraidNF n] -> ShowS
forall (n :: Nat). BraidNF n -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BraidNF n] -> ShowS
$cshowList :: forall (n :: Nat). [BraidNF n] -> ShowS
show :: BraidNF n -> String
$cshow :: forall (n :: Nat). BraidNF n -> String
showsPrec :: Int -> BraidNF n -> ShowS
$cshowsPrec :: forall (n :: Nat). Int -> BraidNF n -> ShowS
Show)
nfReprWord :: KnownNat n => BraidNF n -> Braid n
nfReprWord :: forall (n :: Nat). KnownNat n => BraidNF n -> Braid n
nfReprWord (BraidNF Int
k [Permutation]
perms) = forall (n :: Nat). Braid n -> Braid n
freeReduceBraidWord forall a b. (a -> b) -> a -> b
$ forall (n :: Nat). [Braid n] -> Braid n
composeMany ([Braid n]
deltas forall a. [a] -> [a] -> [a]
++ [Braid n]
rest) where
deltas :: [Braid n]
deltas
| Int
k forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. Int -> a -> [a]
replicate Int
k forall (n :: Nat). KnownNat n => Braid n
halfTwist
| Int
k forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. Int -> a -> [a]
replicate (-Int
k) (forall (n :: Nat). Braid n -> Braid n
inverse forall (n :: Nat). KnownNat n => Braid n
halfTwist)
| Bool
otherwise = []
rest :: [Braid n]
rest = forall a b. (a -> b) -> [a] -> [b]
map forall (n :: Nat). KnownNat n => Permutation -> Braid n
permutationBraid [Permutation]
perms
braidNormalForm :: KnownNat n => Braid n -> BraidNF n
braidNormalForm :: forall (n :: Nat). KnownNat n => Braid n -> BraidNF n
braidNormalForm = forall (n :: Nat). KnownNat n => Braid n -> BraidNF n
braidNormalForm' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat). Braid n -> Braid n
freeReduceBraidWord
braidNormalForm' :: KnownNat n => Braid n -> BraidNF n
braidNormalForm' :: forall (n :: Nat). KnownNat n => Braid n -> BraidNF n
braidNormalForm' braid :: Braid n
braid@(Braid [BrGen]
gens) = forall (n :: Nat). Int -> [Permutation] -> BraidNF n
BraidNF (Int
dexpforall a. Num a => a -> a -> a
+Int
pexp) [Permutation]
perms where
n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
invless :: [XGen]
invless = Int -> [BrGen] -> [XGen]
replaceInverses Int
n [BrGen]
gens
(Int
dexp,[XGen]
posxword) = Int -> [XGen] -> (Int, [XGen])
moveDeltasLeft Int
n [XGen]
invless
factors :: [[Int]]
factors = Int -> [Int] -> [[Int]]
leftGreedyFactors Int
n forall a b. (a -> b) -> a -> b
$ Int -> [XGen] -> [Int]
expandPosXWord Int
n [XGen]
posxword
(Int
pexp,[Permutation]
perms) = Int -> [Permutation] -> (Int, [Permutation])
normalizePermFactors Int
n forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Int -> [Int] -> Permutation
_braidPermutation Int
n) [[Int]]
factors
braidNormalFormNaive' :: KnownNat n => Braid n -> BraidNF n
braidNormalFormNaive' :: forall (n :: Nat). KnownNat n => Braid n -> BraidNF n
braidNormalFormNaive' braid :: Braid n
braid@(Braid [BrGen]
gens) = forall (n :: Nat). Int -> [Permutation] -> BraidNF n
BraidNF (Int
dexpforall a. Num a => a -> a -> a
+Int
pexp) [Permutation]
perms where
n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
invless :: [XGen]
invless = [BrGen] -> [XGen]
replaceInversesNaive [BrGen]
gens
(Int
dexp,[XGen]
posxword) = Int -> [XGen] -> (Int, [XGen])
moveDeltasLeft Int
n [XGen]
invless
factors :: [[Int]]
factors = Int -> [Int] -> [[Int]]
leftGreedyFactors Int
n forall a b. (a -> b) -> a -> b
$ Int -> [XGen] -> [Int]
expandPosXWord Int
n [XGen]
posxword
(Int
pexp,[Permutation]
perms) = Int -> [Permutation] -> (Int, [Permutation])
normalizePermFactors Int
n forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Int -> [Int] -> Permutation
_braidPermutation Int
n) [[Int]]
factors
replaceInverses :: Int -> [BrGen] -> [XGen]
replaceInverses :: Int -> [BrGen] -> [XGen]
replaceInverses Int
n [BrGen]
gens = [BrGen] -> [XGen]
worker [BrGen]
gens where
worker :: [BrGen] -> [XGen]
worker [] = []
worker [BrGen]
xs = [BrGen] -> [XGen]
replaceNegs [BrGen]
neg forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (Int -> XGen
XSigma forall b c a. (b -> c) -> (a -> b) -> a -> c
. BrGen -> Int
brGenIdx) [BrGen]
pos forall a. [a] -> [a] -> [a]
++ [BrGen] -> [XGen]
worker [BrGen]
rest where
([BrGen]
neg,[BrGen]
tmp ) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Sign -> Bool
isMinus forall b c a. (b -> c) -> (a -> b) -> a -> c
. BrGen -> Sign
brGenSign) [BrGen]
xs
([BrGen]
pos,[BrGen]
rest) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Sign -> Bool
isPlus forall b c a. (b -> c) -> (a -> b) -> a -> c
. BrGen -> Sign
brGenSign) [BrGen]
tmp
replaceNegs :: [BrGen] -> [XGen]
replaceNegs [BrGen]
gs = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Int] -> [XGen]
replaceFac [[Int]]
facs where
facs :: [[Int]]
facs = Int -> [Int] -> [[Int]]
leftGreedyFactors Int
n forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map BrGen -> Int
brGenIdx [BrGen]
gs
replaceFac :: [Int] -> [XGen]
replaceFac [Int]
idxs = Int -> XGen
XDelta (-Int
1) forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map Int -> XGen
XSigma (Permutation -> [Int]
_permutationBraid Permutation
perm) where
perm :: Permutation
perm = (Int -> Permutation
P.reversePermutation Int
n) Permutation -> Permutation -> Permutation
`P.multiplyPermutation` (Int -> [Int] -> Permutation
P.adjacentTranspositions Int
n [Int]
idxs)
replaceInversesNaive :: [BrGen] -> [XGen]
replaceInversesNaive :: [BrGen] -> [XGen]
replaceInversesNaive [BrGen]
gens = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap BrGen -> [XGen]
f [BrGen]
gens where
f :: BrGen -> [XGen]
f (Sigma Int
i) = [ Int -> XGen
XSigma Int
i ]
f (SigmaInv Int
i) = [ Int -> XGen
XDelta (-Int
1) , Int -> XGen
XL Int
i ]
data XGen
= XDelta !Int
| XSigma !Int
| XL !Int
| XTauL !Int
deriving (XGen -> XGen -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: XGen -> XGen -> Bool
$c/= :: XGen -> XGen -> Bool
== :: XGen -> XGen -> Bool
$c== :: XGen -> XGen -> Bool
Eq,Int -> XGen -> ShowS
[XGen] -> ShowS
XGen -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [XGen] -> ShowS
$cshowList :: [XGen] -> ShowS
show :: XGen -> String
$cshow :: XGen -> String
showsPrec :: Int -> XGen -> ShowS
$cshowsPrec :: Int -> XGen -> ShowS
Show)
isXDelta :: XGen -> Bool
isXDelta :: XGen -> Bool
isXDelta XGen
x = case XGen
x of { XDelta {} -> Bool
True ; XGen
_ -> Bool
False }
moveDeltasLeft :: Int -> [XGen] -> (Int,[XGen])
moveDeltasLeft :: Int -> [XGen] -> (Int, [XGen])
moveDeltasLeft Int
n [XGen]
input = (Int
finalExp, [XGen]
finalPosWord) where
(XDelta Int
finalExp : [XGen]
finalPosWord) = forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Int -> [XGen] -> [XGen]
worker Int
0 (forall a. [a] -> [a]
reverse [XGen]
input)
worker :: Int -> [XGen] -> [XGen]
worker Int
dexp [] = [ Int -> XGen
XDelta Int
dexp ]
worker !Int
dexp [XGen]
xs = [XGen]
this' forall a. [a] -> [a] -> [a]
++ Int -> [XGen] -> [XGen]
worker Int
dexp' [XGen]
rest where
([XGen]
delta,[XGen]
notdelta) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span XGen -> Bool
isXDelta [XGen]
xs
([XGen]
this ,[XGen]
rest ) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. XGen -> Bool
isXDelta) [XGen]
notdelta
dexp' :: Int
dexp' = Int
dexp forall a. Num a => a -> a -> a
+ [XGen] -> Int
sumDeltas [XGen]
delta
this' :: [XGen]
this' = if forall a. Integral a => a -> Bool
even Int
dexp'
then [XGen]
this
else forall a b. (a -> b) -> [a] -> [b]
map XGen -> XGen
xtau [XGen]
this
sumDeltas :: [XGen] -> Int
sumDeltas :: [XGen] -> Int
sumDeltas [XGen]
xs = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Num a => a -> a -> a
(+) Int
0 [ Int
k | XDelta Int
k <- [XGen]
xs ]
xtau :: XGen -> XGen
xtau :: XGen -> XGen
xtau (XSigma Int
j) = Int -> XGen
XSigma (Int
nforall a. Num a => a -> a -> a
-Int
j)
xtau (XDelta Int
k) = Int -> XGen
XDelta Int
k
xtau (XL Int
k) = Int -> XGen
XTauL Int
k
xtau (XTauL Int
k) = Int -> XGen
XL Int
k
expandPosXWord :: Int -> [XGen] -> [Int]
expandPosXWord :: Int -> [XGen] -> [Int]
expandPosXWord Int
n = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap XGen -> [Int]
f where
posHalfTwist :: [Int]
posHalfTwist = Int -> [Int]
_halfTwist Int
n
jtau :: Int -> Int
jtau :: Int -> Int
jtau Int
j = Int
nforall a. Num a => a -> a -> a
-Int
j
posLTable :: Array Int [Int]
posLTable = forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
1,Int
nforall a. Num a => a -> a -> a
-Int
1) [ Permutation -> [Int]
_permutationBraid (Int -> Int -> Permutation
posLPerm Int
n Int
i) | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] ] :: Array Int [Int]
posTauLTable :: Array Int [Int]
posTauLTable = forall (a :: * -> * -> *) e' e i.
(IArray a e', IArray a e, Ix i) =>
(e' -> e) -> a i e' -> a i e
amap (forall a b. (a -> b) -> [a] -> [b]
map Int -> Int
jtau) Array Int [Int]
posLTable
f :: XGen -> [Int]
f XGen
x = case XGen
x of
XSigma Int
i -> [Int
i]
XL Int
i -> Array Int [Int]
posLTable forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
i
XTauL Int
i -> Array Int [Int]
posTauLTable forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
i
XDelta Int
i
| Int
i forall a. Ord a => a -> a -> Bool
> Int
0 -> forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (forall a. Int -> a -> [a]
replicate Int
i [Int]
posHalfTwist)
| Int
i forall a. Ord a => a -> a -> Bool
< Int
0 -> forall a. HasCallStack => String -> a
error String
"expandPosXWord: negative delta power"
| Bool
otherwise -> []
expandAnyXWord :: forall n. KnownNat n => [XGen] -> Braid n
expandAnyXWord :: forall (n :: Nat). KnownNat n => [XGen] -> Braid n
expandAnyXWord [XGen]
xgens = Braid n
braid where
n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
braid :: Braid n
braid = forall (n :: Nat). [Braid n] -> Braid n
composeMany (forall a b. (a -> b) -> [a] -> [b]
map XGen -> Braid n
f [XGen]
xgens)
posHalfTwist :: Braid n
posHalfTwist = forall (n :: Nat). KnownNat n => Braid n
halfTwist :: Braid n
negHalfTwist :: Braid n
negHalfTwist = forall (n :: Nat). Braid n -> Braid n
inverse Braid n
posHalfTwist :: Braid n
posLTable :: Array Int (Braid n)
posLTable = forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Int
1,Int
nforall a. Num a => a -> a -> a
-Int
1) [ forall (n :: Nat). KnownNat n => Permutation -> Braid n
permutationBraid (Int -> Int -> Permutation
posLPerm Int
n Int
i) | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] ] :: Array Int (Braid n)
posTauLTable :: Array Int (Braid n)
posTauLTable = forall (a :: * -> * -> *) e' e i.
(IArray a e', IArray a e, Ix i) =>
(e' -> e) -> a i e' -> a i e
amap forall (n :: Nat). KnownNat n => Braid n -> Braid n
tau Array Int (Braid n)
posLTable
f :: XGen -> Braid n
f :: XGen -> Braid n
f XGen
x = case XGen
x of
XSigma Int
i -> forall (n :: Nat). KnownNat n => Int -> Braid n
sigma Int
i
XL Int
i -> Array Int (Braid n)
posLTable forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
i
XTauL Int
i -> Array Int (Braid n)
posTauLTable forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
i
XDelta Int
i
| Int
i forall a. Ord a => a -> a -> Bool
> Int
0 -> forall (n :: Nat). [Braid n] -> Braid n
composeMany (forall a. Int -> a -> [a]
replicate Int
i Braid n
posHalfTwist)
| Int
i forall a. Ord a => a -> a -> Bool
< Int
0 -> forall (n :: Nat). [Braid n] -> Braid n
composeMany (forall a. Int -> a -> [a]
replicate (-Int
i) Braid n
negHalfTwist)
| Bool
otherwise -> forall (n :: Nat). Braid n
identity
posL :: KnownNat n => Int -> Braid n
posL :: forall (n :: Nat). KnownNat n => Int -> Braid n
posL Int
k = Braid n
braid where
n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
braid :: Braid n
braid = forall (n :: Nat). KnownNat n => Permutation -> Braid n
permutationBraid (Int -> Int -> Permutation
posLPerm Int
n Int
k)
posR :: KnownNat n => Int -> Braid n
posR :: forall (n :: Nat). KnownNat n => Int -> Braid n
posR Int
k = Braid n
braid where
n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
braid :: Braid n
braid = forall (n :: Nat). KnownNat n => Permutation -> Braid n
permutationBraid (Int -> Int -> Permutation
posRPerm Int
n Int
k)
posLPerm :: Int -> Int -> Permutation
posLPerm :: Int -> Int -> Permutation
posLPerm Int
n Int
k
| Int
kforall a. Ord a => a -> a -> Bool
>Int
0 Bool -> Bool -> Bool
&& Int
kforall a. Ord a => a -> a -> Bool
<Int
n = (Int -> Permutation
P.reversePermutation Int
n Permutation -> Permutation -> Permutation
`P.multiplyPermutation` Int -> Int -> Permutation
P.adjacentTransposition Int
n Int
k)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"posLPerm: index out of range"
posRPerm :: Int -> Int -> Permutation
posRPerm :: Int -> Int -> Permutation
posRPerm Int
n Int
k
| Int
kforall a. Ord a => a -> a -> Bool
>Int
0 Bool -> Bool -> Bool
&& Int
kforall a. Ord a => a -> a -> Bool
<Int
n = (Int -> Int -> Permutation
P.adjacentTransposition Int
n Int
k Permutation -> Permutation -> Permutation
`P.multiplyPermutation` Int -> Permutation
P.reversePermutation Int
n )
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"posRPerm: index out of range"
filterDeltaFactors :: Int -> [[Int]] -> (Int, [[Int]])
filterDeltaFactors :: Int -> [[Int]] -> (Int, [[Int]])
filterDeltaFactors Int
n [[Int]]
facs = (Int
exp',[[Int]]
facs'') where
(Int
exp',[[Int]]
facs') = forall {a}. Integral a => a -> [[Int]] -> (a, [[Int]])
go Int
0 (forall a. [a] -> [a]
reverse [[Int]]
facs)
jtau :: Int -> Int
jtau Int
j = Int
nforall a. Num a => a -> a -> a
-Int
j
facs'' :: [[Int]]
facs'' = forall a. [a] -> [a]
reverse [[Int]]
facs'
maxlen :: Int
maxlen = forall a. Integral a => a -> a -> a
div (Int
nforall a. Num a => a -> a -> a
*(Int
nforall a. Num a => a -> a -> a
-Int
1)) Int
2
go :: a -> [[Int]] -> (a, [[Int]])
go !a
e [] = (a
e,[])
go !a
e ([Int]
xs:[[Int]]
xxs)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
xs = a -> [[Int]] -> (a, [[Int]])
go a
e [[Int]]
xxs
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs forall a. Eq a => a -> a -> Bool
== Int
maxlen = a -> [[Int]] -> (a, [[Int]])
go (a
eforall a. Num a => a -> a -> a
+a
1) [[Int]]
xxs
| Bool
otherwise =
if forall a. Integral a => a -> Bool
even a
e
then let (a
e',[[Int]]
yys) = a -> [[Int]] -> (a, [[Int]])
go a
e [[Int]]
xxs in (a
e' , [Int]
xs forall a. a -> [a] -> [a]
: [[Int]]
yys)
else let (a
e',[[Int]]
yys) = a -> [[Int]] -> (a, [[Int]])
go a
e [[Int]]
xxs in (a
e' , forall a b. (a -> b) -> [a] -> [b]
map Int -> Int
jtau [Int]
xs forall a. a -> [a] -> [a]
: [[Int]]
yys)
permWordStartingSet :: Int -> [Int] -> [Int]
permWordStartingSet :: Int -> [Int] -> [Int]
permWordStartingSet Int
n [Int]
xs = Int -> [Int] -> [Int]
permWordFinishingSet Int
n (forall a. [a] -> [a]
reverse [Int]
xs)
permWordFinishingSet :: Int -> [Int] -> [Int]
permWordFinishingSet :: Int -> [Int] -> [Int]
permWordFinishingSet Int
n [Int]
input = forall a. (forall s. ST s a) -> a
runST forall s. ST s [Int]
action where
action :: forall s. ST s [Int]
action :: forall s. ST s [Int]
action = do
STUArray s Int Int
perm <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) :: ST s (STUArray s Int Int)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
i -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm Int
i Int
i
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int]
input forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Int
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm Int
i
Int
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm Int
i Int
b
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
a
forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM [Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Int
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm Int
i
Int
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
bforall a. Ord a => a -> a -> Bool
<Int
a)
permutationStartingSet :: Permutation -> [Int]
permutationStartingSet :: Permutation -> [Int]
permutationStartingSet = Permutation -> [Int]
permutationFinishingSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. Permutation -> Permutation
P.inversePermutation
permutationFinishingSet :: Permutation -> [Int]
permutationFinishingSet :: Permutation -> [Int]
permutationFinishingSet Permutation
perm
= [ Int
i | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] , Permutation
perm Permutation -> Int -> Int
!!! Int
i forall a. Ord a => a -> a -> Bool
> Permutation
perm Permutation -> Int -> Int
!!! (Int
iforall a. Num a => a -> a -> a
+Int
1) ] where n :: Int
n = Permutation -> Int
P.permutationSize Permutation
perm
fails_lemmma_2_5 :: Int -> [Permutation]
fails_lemmma_2_5 :: Int -> [Permutation]
fails_lemmma_2_5 Int
n = [ Permutation
p | Permutation
p <- Int -> [Permutation]
P.permutations Int
n , Bool -> Bool
not (Permutation -> Bool
test Permutation
p) ] where
test :: Permutation -> Bool
test Permutation
p = forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ Int -> Bool
check Int
i | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] ] where
w :: [Int]
w = Permutation -> [Int]
_permutationBraid Permutation
p
s :: [Int]
s = Int -> [Int] -> [Int]
permWordStartingSet Int
n [Int]
w
check :: Int -> Bool
check Int
i = Int -> [Int] -> Bool
_isPermutationBraid Int
n (Int
iforall a. a -> [a] -> [a]
:[Int]
w) forall a. Eq a => a -> a -> Bool
== (Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Int
i [Int]
s)
normalizePermFactors :: Int -> [Permutation] -> (Int,[Permutation])
normalizePermFactors :: Int -> [Permutation] -> (Int, [Permutation])
normalizePermFactors Int
n = Int -> [Permutation] -> (Int, [Permutation])
go Int
0 where
go :: Int -> [Permutation] -> (Int, [Permutation])
go !Int
acc [Permutation]
input =
if (Int
expforall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
&& [Permutation]
input forall a. Eq a => a -> a -> Bool
== [Permutation]
output)
then (Int
acc,[Permutation]
input)
else Int -> [Permutation] -> (Int, [Permutation])
go (Int
accforall a. Num a => a -> a -> a
+Int
exp) [Permutation]
output
where
(Int
exp,[Permutation]
output) = Int -> [Permutation] -> (Int, [Permutation])
normalizePermFactors1 Int
n [Permutation]
input
normalizePermFactors1 :: Int -> [Permutation] -> (Int,[Permutation])
normalizePermFactors1 :: Int -> [Permutation] -> (Int, [Permutation])
normalizePermFactors1 Int
n [Permutation]
input = (Int
exp, forall a. [a] -> [a]
reverse [Permutation]
output) where
(Int
exp, [Permutation]
output) = Int -> [Permutation] -> (Int, [Permutation])
worker Int
0 (forall a. [a] -> [a]
reverse [Permutation]
input)
worker :: Int -> [Permutation] -> (Int,[Permutation])
worker :: Int -> [Permutation] -> (Int, [Permutation])
worker = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' Int
0 Int
0
worker' :: Int -> Int -> Int -> [Permutation] -> (Int,[Permutation])
worker' :: Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' !Int
ep !Int
eq !Int
e (!Permutation
p : rest :: [Permutation]
rest@(!Permutation
q : [Permutation]
rest'))
| Permutation -> Bool
isIdentityPermutation Permutation
p = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' Int
eq Int
0 Int
e [Permutation]
rest
| Permutation -> Bool
isReversePermutation Permutation
p = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' Int
eq Int
0 (Int
eforall a. Num a => a -> a -> a
+Int
1) [Permutation]
rest
| Permutation -> Bool
isIdentityPermutation Permutation
q = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' Int
ep Int
0 Int
e (Permutation
p forall a. a -> [a] -> [a]
: [Permutation]
rest')
| Permutation -> Bool
isReversePermutation Permutation
q = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' (Int
epforall a. Num a => a -> a -> a
-Int
1) Int
0 (Int
eforall a. Num a => a -> a -> a
+Int
1) (Permutation
p forall a. a -> [a] -> [a]
: [Permutation]
rest')
| Bool
otherwise =
case Permutation -> [Int]
permutationStartingSet Permutation
preal forall a. Eq a => [a] -> [a] -> [a]
\\ Permutation -> [Int]
permutationFinishingSet Permutation
qreal of
[] -> let (Int
e',[Permutation]
rs) = Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' Int
eq Int
0 Int
e [Permutation]
rest in (Int
e', Permutation
preal forall a. a -> [a] -> [a]
: [Permutation]
rs)
(Int
j:[Int]
_) -> Int -> Int -> Int -> [Permutation] -> (Int, [Permutation])
worker' (-Int
e) (-Int
e) Int
e (Permutation
p'forall a. a -> [a] -> [a]
:Permutation
q'forall a. a -> [a] -> [a]
:[Permutation]
rest') where
s :: Permutation
s = Int -> Int -> Permutation
P.adjacentTransposition Int
n Int
j
p' :: Permutation
p' = Permutation -> Permutation -> Permutation
P.multiplyPermutation Permutation
s Permutation
preal
q' :: Permutation
q' = Permutation -> Permutation -> Permutation
P.multiplyPermutation Permutation
qreal Permutation
s
where
preal :: Permutation
preal = Int -> Permutation -> Permutation
oddTau (Int
eforall a. Num a => a -> a -> a
+Int
ep) Permutation
p
qreal :: Permutation
qreal = Int -> Permutation -> Permutation
oddTau (Int
eforall a. Num a => a -> a -> a
+Int
eq) Permutation
q
worker' Int
_ Int
_ !Int
e [ ] = (Int
e,[])
worker' !Int
ep Int
_ !Int
e [Permutation
p]
| Permutation -> Bool
isIdentityPermutation Permutation
p = (Int
e , [])
| Permutation -> Bool
isReversePermutation Permutation
p = (Int
eforall a. Num a => a -> a -> a
+Int
1 , [])
| Bool
otherwise = (Int
e , [Int -> Permutation -> Permutation
oddTau (Int
eforall a. Num a => a -> a -> a
+Int
ep) Permutation
p] )
oddTau :: Int -> Permutation -> Permutation
oddTau :: Int -> Permutation -> Permutation
oddTau !Int
e Permutation
p = if forall a. Integral a => a -> Bool
even Int
e then Permutation
p else Permutation -> Permutation
tauPerm Permutation
p
leftGreedyFactors :: Int -> [Int] -> [[Int]]
leftGreedyFactors :: Int -> [Int] -> [[Int]]
leftGreedyFactors Int
n [Int]
input = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null) forall a b. (a -> b) -> a -> b
$ forall a. (forall s. ST s a) -> a
runST (forall s. [Int] -> ST s [[Int]]
action [Int]
input) where
action :: forall s. [Int] -> ST s [[Int]]
action :: forall s. [Int] -> ST s [[Int]]
action [Int]
input = do
STUArray s Int Int
perm <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) :: ST s (STUArray s Int Int)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
i -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm Int
i Int
i
let doSwap :: Int -> ST s ()
doSwap :: Int -> ST s ()
doSwap Int
i = do
Int
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm Int
i
Int
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm Int
i Int
b
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
a
STUArray s (Int, Int) Int
mat <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray ((Int
1,Int
1),(Int
n,Int
n)) Int
0 :: ST s (STUArray s (Int,Int) Int)
let clearMat :: ST s ()
clearMat = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
i ->
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
j -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s (Int, Int) Int
mat (Int
i,Int
j) Int
0
let doAdd1 :: Int -> Int -> ST s Int
doAdd1 :: Int -> Int -> ST s Int
doAdd1 Int
i Int
j = do
Int
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s (Int, Int) Int
mat (Int
i,Int
j)
let y :: Int
y = Int
xforall a. Num a => a -> a -> a
+Int
1
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s (Int, Int) Int
mat (Int
i,Int
j) Int
y
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s (Int, Int) Int
mat (Int
j,Int
i) Int
y
forall (m :: * -> *) a. Monad m => a -> m a
return Int
y
let worker :: [Int] -> ST s [[Int]]
worker :: [Int] -> ST s [[Int]]
worker [] = forall (m :: * -> *) a. Monad m => a -> m a
return [[]]
worker (Int
p:[Int]
ps) = do
Int
u <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm Int
p
Int
v <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
pforall a. Num a => a -> a -> a
+Int
1)
Int
c <- Int -> Int -> ST s Int
doAdd1 Int
u Int
v
Int -> ST s ()
doSwap Int
p
if Int
cforall a. Ord a => a -> a -> Bool
<=Int
1
then do
[[Int]]
ffs <- [Int] -> ST s [[Int]]
worker [Int]
ps
case [[Int]]
ffs of
([Int]
f:[[Int]]
fs) -> forall (m :: * -> *) a. Monad m => a -> m a
return ((Int
pforall a. a -> [a] -> [a]
:[Int]
f)forall a. a -> [a] -> [a]
:[[Int]]
fs)
[[Int]]
_ -> forall a. HasCallStack => String -> a
error String
"Braid/NF/leftGreedyFactors/worker: fatal error; should not happen"
else do
ST s ()
clearMat
[[Int]]
fs <- [Int] -> ST s [[Int]]
worker (Int
pforall a. a -> [a] -> [a]
:[Int]
ps)
forall (m :: * -> *) a. Monad m => a -> m a
return ([]forall a. a -> [a] -> [a]
:[[Int]]
fs)
[Int] -> ST s [[Int]]
worker [Int]
input