/usr/share/doc/libghc-maths-doc/html/src/Math-Combinatorics-CombinatorialHopfAlgebra.html is in libghc-maths-doc 0.4.8-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Math/Combinatorics/CombinatorialHopfAlgebra.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>-- Copyright (c) 2012-2015, David Amos. All rights reserved.</span>
<a name="line-2"></a>
<a name="line-3"></a><span class='hs-comment'>{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, NoMonomorphismRestriction, ScopedTypeVariables, DeriveFunctor #-}</span>
<a name="line-4"></a>
<a name="line-5"></a><span class='hs-comment'>-- |A module defining the following Combinatorial Hopf Algebras, together with coalgebra or Hopf algebra morphisms between them:</span>
<a name="line-6"></a><span class='hs-comment'>--</span>
<a name="line-7"></a><span class='hs-comment'>-- * Sh, the Shuffle Hopf algebra</span>
<a name="line-8"></a><span class='hs-comment'>--</span>
<a name="line-9"></a><span class='hs-comment'>-- * SSym, the Malvenuto-Reutnenauer Hopf algebra of permutations</span>
<a name="line-10"></a><span class='hs-comment'>--</span>
<a name="line-11"></a><span class='hs-comment'>-- * YSym, the (dual of the) Loday-Ronco Hopf algebra of binary trees</span>
<a name="line-12"></a><span class='hs-comment'>--</span>
<a name="line-13"></a><span class='hs-comment'>-- * QSym, the Hopf algebra of quasi-symmetric functions (having a basis indexed by compositions)</span>
<a name="line-14"></a><span class='hs-comment'>--</span>
<a name="line-15"></a><span class='hs-comment'>-- * Sym, the Hopf algebra of symmetric functions (having a basis indexed by integer partitions)</span>
<a name="line-16"></a><span class='hs-comment'>--</span>
<a name="line-17"></a><span class='hs-comment'>-- * NSym, the Hopf algebra of non-commutative symmetric functions</span>
<a name="line-18"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Combinatorics</span><span class='hs-varop'>.</span><span class='hs-conid'>CombinatorialHopfAlgebra</span> <span class='hs-keyword'>where</span>
<a name="line-19"></a>
<a name="line-20"></a><span class='hs-comment'>-- Sources:</span>
<a name="line-21"></a>
<a name="line-22"></a><span class='hs-comment'>-- Structure of the Malvenuto-Reutenauer Hopf algebra of permutations</span>
<a name="line-23"></a><span class='hs-comment'>-- Marcelo Aguiar and Frank Sottile</span>
<a name="line-24"></a><span class='hs-comment'>-- <a href="http://www.math.tamu.edu/~sottile/research/pdf/SSym.pdf">http://www.math.tamu.edu/~sottile/research/pdf/SSym.pdf</a></span>
<a name="line-25"></a>
<a name="line-26"></a><span class='hs-comment'>-- Structure of the Loday-Ronco Hopf algebra of trees</span>
<a name="line-27"></a><span class='hs-comment'>-- Marcelo Aguiar and Frank Sottile</span>
<a name="line-28"></a><span class='hs-comment'>-- <a href="http://www.math.tamu.edu/~sottile/research/pdf/Loday.pdf">http://www.math.tamu.edu/~sottile/research/pdf/Loday.pdf</a></span>
<a name="line-29"></a>
<a name="line-30"></a><span class='hs-comment'>-- Hopf Structures on the Multiplihedra</span>
<a name="line-31"></a><span class='hs-comment'>-- Stefan Forcey, Aaron Lauve and Frank Sottile</span>
<a name="line-32"></a><span class='hs-comment'>-- <a href="http://www.math.tamu.edu/~sottile/research/pdf/MSym.pdf">http://www.math.tamu.edu/~sottile/research/pdf/MSym.pdf</a></span>
<a name="line-33"></a>
<a name="line-34"></a><span class='hs-comment'>-- Lie Algebras and Hopf Algebras</span>
<a name="line-35"></a><span class='hs-comment'>-- Michiel Hazewinkel, Nadiya Gubareni, V.V.Kirichenko</span>
<a name="line-36"></a>
<a name="line-37"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Prelude</span> <span class='hs-varid'>hiding</span> <span class='hs-layout'>(</span> <span class='hs-layout'>(</span><span class='hs-varop'>*></span><span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-38"></a>
<a name="line-39"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>L</span>
<a name="line-40"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromJust</span><span class='hs-layout'>)</span>
<a name="line-41"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Set</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>S</span>
<a name="line-42"></a>
<a name="line-43"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Core</span><span class='hs-varop'>.</span><span class='hs-conid'>Field</span>
<a name="line-44"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Core</span><span class='hs-varop'>.</span><span class='hs-conid'>Utils</span>
<a name="line-45"></a>
<a name="line-46"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Algebras</span><span class='hs-varop'>.</span><span class='hs-conid'>VectorSpace</span> <span class='hs-varid'>hiding</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span><span class='hs-layout'>)</span>
<a name="line-47"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Algebras</span><span class='hs-varop'>.</span><span class='hs-conid'>TensorProduct</span>
<a name="line-48"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Algebras</span><span class='hs-varop'>.</span><span class='hs-conid'>Structures</span>
<a name="line-49"></a>
<a name="line-50"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>Combinatorics</span><span class='hs-varop'>.</span><span class='hs-conid'>Poset</span>
<a name="line-51"></a>
<a name="line-52"></a><span class='hs-comment'>-- import Math.Algebra.Group.PermutationGroup</span>
<a name="line-53"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Math</span><span class='hs-varop'>.</span><span class='hs-conid'>CommutativeAlgebra</span><span class='hs-varop'>.</span><span class='hs-conid'>Polynomial</span>
<a name="line-54"></a>
<a name="line-55"></a>
<a name="line-56"></a><span class='hs-comment'>-- SHUFFLE ALGEBRA</span>
<a name="line-57"></a><span class='hs-comment'>-- This is just the tensor algebra, but with shuffle product (and deconcatenation coproduct)</span>
<a name="line-58"></a>
<a name="line-59"></a><a name="Shuffle"></a><span class='hs-comment'>-- |A basis for the shuffle algebra. As a vector space, the shuffle algebra is identical to the tensor algebra.</span>
<a name="line-60"></a><a name="Shuffle"></a><span class='hs-comment'>-- However, we consider a different algebra structure, based on the shuffle product. Together with the</span>
<a name="line-61"></a><a name="Shuffle"></a><span class='hs-comment'>-- deconcatenation coproduct, this leads to a Hopf algebra structure.</span>
<a name="line-62"></a><a name="Shuffle"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sh</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Ord</span><span class='hs-layout'>,</span><span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-63"></a>
<a name="line-64"></a><a name="sh"></a><span class='hs-comment'>-- |Construct a basis element of the shuffle algebra</span>
<a name="line-65"></a><span class='hs-definition'>sh</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-66"></a><span class='hs-definition'>sh</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-varop'>.</span> <span class='hs-conid'>Sh</span>
<a name="line-67"></a>
<a name="line-68"></a><a name="shuffles"></a><span class='hs-definition'>shuffles</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>shuffles</span> <span class='hs-varid'>xs</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>shuffles</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-69"></a><span class='hs-definition'>shuffles</span> <span class='hs-varid'>xs</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-70"></a><span class='hs-definition'>shuffles</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>ys</span><span class='hs-keyglyph'>]</span>
<a name="line-71"></a>
<a name="line-72"></a><a name="instance%20Algebra%20k%20(Shuffle%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-73"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-74"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-75"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-conid'>Sh</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>zs</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>shuffles</span> <span class='hs-varid'>xs</span> <span class='hs-varid'>ys</span><span class='hs-keyglyph'>]</span>
<a name="line-76"></a>
<a name="line-77"></a><a name="deconcatenations"></a><span class='hs-definition'>deconcatenations</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zip</span> <span class='hs-layout'>(</span><span class='hs-varid'>inits</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>tails</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-78"></a>
<a name="line-79"></a><a name="instance%20Coalgebra%20k%20(Shuffle%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-80"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-81"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-82"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>us</span><span class='hs-layout'>,</span> <span class='hs-conid'>Sh</span> <span class='hs-varid'>vs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>us</span><span class='hs-layout'>,</span> <span class='hs-varid'>vs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>deconcatenations</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-83"></a>
<a name="line-84"></a><a name="instance%20Bialgebra%20k%20(Shuffle%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-85"></a>
<a name="line-86"></a><a name="instance%20HopfAlgebra%20k%20(Shuffle%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Shuffle</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-87"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-varop'>^</span><span class='hs-varid'>length</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sh</span> <span class='hs-layout'>(</span><span class='hs-varid'>reverse</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-88"></a>
<a name="line-89"></a>
<a name="line-90"></a><span class='hs-comment'>-- SSYM: PERMUTATIONS</span>
<a name="line-91"></a><span class='hs-comment'>-- (This is permutations considered as combinatorial objects rather than as algebraic objects)</span>
<a name="line-92"></a>
<a name="line-93"></a><span class='hs-comment'>-- Permutations with shifted shuffle product and flattened deconcatenation coproduct</span>
<a name="line-94"></a><span class='hs-comment'>-- This is the Malvenuto-Reutenauer Hopf algebra of permutations, SSym.</span>
<a name="line-95"></a><span class='hs-comment'>-- It is neither commutative nor co-commutative</span>
<a name="line-96"></a>
<a name="line-97"></a><span class='hs-comment'>-- ssymF xs is the fundamental basis F_xs (Aguiar and Sottile)</span>
<a name="line-98"></a>
<a name="line-99"></a><a name="SSymF"></a><span class='hs-comment'>-- |The fundamental basis for the Malvenuto-Reutenauer Hopf algebra of permutations, SSym.</span>
<a name="line-100"></a><a name="SSymF"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-101"></a>
<a name="line-102"></a><a name="instance%20Ord%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-103"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-104"></a>
<a name="line-105"></a><a name="instance%20Show%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-106"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"F "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>xs</span>
<a name="line-107"></a>
<a name="line-108"></a><a name="ssymF"></a><span class='hs-comment'>-- |Construct a fundamental basis element in SSym.</span>
<a name="line-109"></a><span class='hs-comment'>-- The list of ints must be a permutation of [1..n], eg [1,2], [3,4,2,1].</span>
<a name="line-110"></a><span class='hs-definition'>ssymF</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>SSymF</span>
<a name="line-111"></a><span class='hs-definition'>ssymF</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>sort</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>==</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-112"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"Not a permutation of [1..n]"</span>
<a name="line-113"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span>
<a name="line-114"></a>
<a name="line-115"></a><a name="shiftedConcat"></a><span class='hs-comment'>-- so this is a candidate mult. It is associative and SSymF [] is obviously a left and right identity</span>
<a name="line-116"></a><span class='hs-comment'>-- (need quickcheck properties to prove that)</span>
<a name="line-117"></a><span class='hs-definition'>shiftedConcat</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>k</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>in</span> <span class='hs-conid'>SSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>xs</span> <span class='hs-varop'>++</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-118"></a>
<a name="line-119"></a><a name="prop_Associative"></a><span class='hs-definition'>prop_Associative</span> <span class='hs-varid'>f</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-varid'>y</span><span class='hs-layout'>,</span><span class='hs-varid'>z</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>f</span> <span class='hs-varid'>x</span> <span class='hs-layout'>(</span><span class='hs-varid'>f</span> <span class='hs-varid'>y</span> <span class='hs-varid'>z</span><span class='hs-layout'>)</span> <span class='hs-varop'>==</span> <span class='hs-varid'>f</span> <span class='hs-layout'>(</span><span class='hs-varid'>f</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span><span class='hs-layout'>)</span> <span class='hs-varid'>z</span>
<a name="line-120"></a>
<a name="line-121"></a><span class='hs-comment'>-- > quickCheck (prop_Associative shiftedConcat)</span>
<a name="line-122"></a><span class='hs-comment'>-- +++ OK, passed 100 tests.</span>
<a name="line-123"></a>
<a name="line-124"></a>
<a name="line-125"></a><a name="instance%20Algebra%20k%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-126"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-127"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-128"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-129"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>k</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span>
<a name="line-130"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>zs</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>shuffles</span> <span class='hs-varid'>xs</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-131"></a>
<a name="line-132"></a>
<a name="line-133"></a><a name="flatten"></a><span class='hs-comment'>-- standard permutation, also called flattening, eg [6,2,5] -> [3,1,2]</span>
<a name="line-134"></a><span class='hs-definition'>flatten</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>mapping</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zip</span> <span class='hs-layout'>(</span><span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>sort</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-keyglyph'>]</span>
<a name="line-135"></a> <span class='hs-keyword'>in</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>y</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-keyword'>let</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lookup</span> <span class='hs-varid'>x</span> <span class='hs-varid'>mapping</span><span class='hs-keyglyph'>]</span>
<a name="line-136"></a>
<a name="line-137"></a><a name="instance%20Coalgebra%20k%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-138"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-139"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span>
<a name="line-140"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>st</span> <span class='hs-varid'>us</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>SSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>st</span> <span class='hs-varid'>vs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>us</span><span class='hs-layout'>,</span> <span class='hs-varid'>vs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>deconcatenations</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-141"></a> <span class='hs-varid'>st</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>flatten</span>
<a name="line-142"></a>
<a name="line-143"></a><a name="instance%20Bialgebra%20k%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-144"></a>
<a name="line-145"></a><a name="instance%20HopfAlgebra%20k%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-146"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-147"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-148"></a> <span class='hs-varid'>antipode'</span> <span class='hs-varid'>x</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>negatev</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>id</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>antipode</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>removeTerm</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>comult</span> <span class='hs-varop'>.</span> <span class='hs-varid'>return</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span>
<a name="line-149"></a> <span class='hs-comment'>-- This expression for antipode is derived from mult . (id `tf` antipode) . comult == unit . counit</span>
<a name="line-150"></a> <span class='hs-comment'>-- It's possible because this is a graded, connected Hopf algebra. (connected means the counit is projection onto the grade 0 part)</span>
<a name="line-151"></a><span class='hs-comment'>-- It would be nicer to have an explicit expression for antipode.</span>
<a name="line-152"></a><span class='hs-comment'>{-
<a name="line-153"></a>instance (Eq k, Num k) => HopfAlgebra k SSymF where
<a name="line-154"></a> antipode = linear antipode'
<a name="line-155"></a> where antipode' (SSymF v) = sumv [lambda v w *> return (SSymF w) | w <- L.permutations v]
<a name="line-156"></a> lambda v w = length [s | s <- powerset [1..n-1], odd (length s), descentSet (w^-1 * v_s) `isSubset` s]
<a name="line-157"></a> - length [s | s <- powerset [1..n-1], even (length s), descentSet (w^-1 * v_s) `isSubset` s]
<a name="line-158"></a>-}</span>
<a name="line-159"></a>
<a name="line-160"></a><a name="instance%20HasInverses%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>HasInverses</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-161"></a> <span class='hs-varid'>inverse</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SSymF</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>snd</span> <span class='hs-varop'>$</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>sort</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-varid'>s</span><span class='hs-layout'>,</span><span class='hs-varid'>t</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>-></span><span class='hs-layout'>(</span><span class='hs-varid'>t</span><span class='hs-layout'>,</span><span class='hs-varid'>s</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>zip</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>xs</span>
<a name="line-162"></a>
<a name="line-163"></a><a name="instance%20HasPairing%20k%20SSymF%20SSymF"></a><span class='hs-comment'>-- Hazewinkel p267</span>
<a name="line-164"></a><a name="instance%20HasPairing%20k%20SSymF%20SSymF"></a><span class='hs-comment'>-- |A pairing showing that SSym is self-adjoint</span>
<a name="line-165"></a><a name="instance%20HasPairing%20k%20SSymF%20SSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HasPairing</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyword'>where</span>
<a name="line-166"></a> <span class='hs-varid'>pairing</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>pairing'</span> <span class='hs-keyword'>where</span>
<a name="line-167"></a> <span class='hs-varid'>pairing'</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span><span class='hs-varid'>y</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>delta</span> <span class='hs-varid'>x</span> <span class='hs-layout'>(</span><span class='hs-varid'>inverse</span> <span class='hs-varid'>y</span><span class='hs-layout'>)</span>
<a name="line-168"></a><span class='hs-comment'>-- Not entirely clear to me why this works</span>
<a name="line-169"></a><span class='hs-comment'>-- The pairing is *not* positive definite (Hazewinkel p267)</span>
<a name="line-170"></a><span class='hs-comment'>-- eg (\x -> pairing' x x >= 0) (ssymF [1,3,2] + ssymF [2,3,1] - ssymF [3,1,2]) == False</span>
<a name="line-171"></a>
<a name="line-172"></a>
<a name="line-173"></a><a name="SSymM"></a><span class='hs-comment'>-- |An alternative \"monomial\" basis for the Malvenuto-Reutenauer Hopf algebra of permutations, SSym.</span>
<a name="line-174"></a><a name="SSymM"></a><span class='hs-comment'>-- This basis is related to the fundamental basis by Mobius inversion in the poset of permutations with the weak order.</span>
<a name="line-175"></a><a name="SSymM"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-176"></a>
<a name="line-177"></a><a name="instance%20Ord%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span>
<a name="line-178"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-179"></a>
<a name="line-180"></a><a name="instance%20Show%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span>
<a name="line-181"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"M "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>xs</span>
<a name="line-182"></a>
<a name="line-183"></a><a name="ssymM"></a><span class='hs-comment'>-- |Construct a monomial basis element in SSym.</span>
<a name="line-184"></a><span class='hs-comment'>-- The list of ints must be a permutation of [1..n], eg [1,2], [3,4,2,1].</span>
<a name="line-185"></a><span class='hs-definition'>ssymM</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>SSymM</span>
<a name="line-186"></a><span class='hs-definition'>ssymM</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>sort</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>==</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-187"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"Not a permutation of [1..n]"</span>
<a name="line-188"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span>
<a name="line-189"></a>
<a name="line-190"></a><a name="inversions"></a><span class='hs-definition'>inversions</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>ixs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zip</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>xs</span>
<a name="line-191"></a> <span class='hs-keyword'>in</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>i</span><span class='hs-layout'>,</span><span class='hs-varid'>j</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>i</span><span class='hs-layout'>,</span><span class='hs-varid'>xi</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span><span class='hs-layout'>(</span><span class='hs-varid'>j</span><span class='hs-layout'>,</span><span class='hs-varid'>xj</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>pairs</span> <span class='hs-varid'>ixs</span><span class='hs-layout'>,</span> <span class='hs-varid'>xi</span> <span class='hs-varop'>></span> <span class='hs-varid'>xj</span><span class='hs-keyglyph'>]</span>
<a name="line-192"></a>
<a name="line-193"></a><a name="weakOrder"></a><span class='hs-comment'>-- should really check that xs and ys have the same length, and perhaps insist also on same type</span>
<a name="line-194"></a><span class='hs-definition'>weakOrder</span> <span class='hs-varid'>xs</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>inversions</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>`isSubsetAsc`</span> <span class='hs-varid'>inversions</span> <span class='hs-varid'>ys</span>
<a name="line-195"></a>
<a name="line-196"></a><a name="mu"></a><span class='hs-definition'>mu</span> <span class='hs-layout'>(</span><span class='hs-varid'>set</span><span class='hs-layout'>,</span><span class='hs-varid'>po</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>mu'</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyword'>where</span>
<a name="line-197"></a> <span class='hs-varid'>mu'</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-varop'>==</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span>
<a name="line-198"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>po</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>negate</span> <span class='hs-varop'>$</span> <span class='hs-varid'>sum</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>mu'</span> <span class='hs-varid'>x</span> <span class='hs-varid'>z</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>z</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>set</span><span class='hs-layout'>,</span> <span class='hs-varid'>po</span> <span class='hs-varid'>x</span> <span class='hs-varid'>z</span><span class='hs-layout'>,</span> <span class='hs-varid'>po</span> <span class='hs-varid'>z</span> <span class='hs-varid'>y</span><span class='hs-layout'>,</span> <span class='hs-varid'>z</span> <span class='hs-varop'>/=</span> <span class='hs-varid'>y</span><span class='hs-keyglyph'>]</span>
<a name="line-199"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-200"></a>
<a name="line-201"></a><a name="ssymMtoF"></a><span class='hs-comment'>-- |Convert an element of SSym represented in the monomial basis to the fundamental basis</span>
<a name="line-202"></a><span class='hs-definition'>ssymMtoF</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span>
<a name="line-203"></a><span class='hs-definition'>ssymMtoF</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>ssymMtoF'</span> <span class='hs-keyword'>where</span>
<a name="line-204"></a> <span class='hs-varid'>ssymMtoF'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>mu</span> <span class='hs-layout'>(</span><span class='hs-varid'>set</span><span class='hs-layout'>,</span><span class='hs-varid'>po</span><span class='hs-layout'>)</span> <span class='hs-varid'>u</span> <span class='hs-varid'>v</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>set</span><span class='hs-layout'>,</span> <span class='hs-varid'>po</span> <span class='hs-varid'>u</span> <span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span>
<a name="line-205"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>set</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>permutations</span> <span class='hs-varid'>u</span>
<a name="line-206"></a> <span class='hs-varid'>po</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>weakOrder</span>
<a name="line-207"></a>
<a name="line-208"></a><a name="ssymFtoM"></a><span class='hs-comment'>-- |Convert an element of SSym represented in the fundamental basis to the monomial basis</span>
<a name="line-209"></a><span class='hs-definition'>ssymFtoM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span>
<a name="line-210"></a><span class='hs-definition'>ssymFtoM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>ssymFtoM'</span> <span class='hs-keyword'>where</span>
<a name="line-211"></a> <span class='hs-varid'>ssymFtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>set</span><span class='hs-layout'>,</span> <span class='hs-varid'>po</span> <span class='hs-varid'>u</span> <span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span>
<a name="line-212"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>set</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>permutations</span> <span class='hs-varid'>u</span>
<a name="line-213"></a> <span class='hs-varid'>po</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>weakOrder</span>
<a name="line-214"></a>
<a name="line-215"></a><span class='hs-comment'>-- (p,q)-shuffles: permutations of [1..p+q] having at most one descent, at position p</span>
<a name="line-216"></a><span class='hs-comment'>-- denoted S^{(p,q)} in Aguiar&Sottile</span>
<a name="line-217"></a><span class='hs-comment'>-- (Grassmannian permutations?)</span>
<a name="line-218"></a><span class='hs-comment'>-- pqShuffles p q = [u++v | u <- combinationsOf p [1..n], let v = [1..n] `diffAsc` u] where n = p+q</span>
<a name="line-219"></a>
<a name="line-220"></a><span class='hs-comment'>-- The inverse of a (p,q)-shuffle.</span>
<a name="line-221"></a><span class='hs-comment'>-- The special form of (p,q)-shuffles makes an O(n) algorithm possible</span>
<a name="line-222"></a><span class='hs-comment'>-- pqInverse :: Int -> Int -> [Int] -> [Int]</span>
<a name="line-223"></a><span class='hs-comment'>{-
<a name="line-224"></a>-- incorrect
<a name="line-225"></a>pqInverse p q xs = pqInverse' [1..p] [p+1..p+q] xs
<a name="line-226"></a> where pqInverse' (l:ls) (r:rs) (x:xs) =
<a name="line-227"></a> if x <= p then l : pqInverse' ls (r:rs) xs else r : pqInverse' (l:ls) rs xs
<a name="line-228"></a> pqInverse' ls rs _ = ls ++ rs -- one of them is null
<a name="line-229"></a>-}</span>
<a name="line-230"></a><span class='hs-comment'>-- pqInverseShuffles p q = shuffles [1..p] [p+1..p+q]</span>
<a name="line-231"></a>
<a name="line-232"></a>
<a name="line-233"></a><a name="instance%20Algebra%20k%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span>
<a name="line-234"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-235"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ssymFtoM</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>ssymMtoF</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>ssymMtoF</span><span class='hs-layout'>)</span>
<a name="line-236"></a>
<a name="line-237"></a><span class='hs-comment'>{-
<a name="line-238"></a>mult2 = linear mult'
<a name="line-239"></a> where mult' (SSymM u, SSymM v) = sumv [alpha u v w *> return (SSymM w) | w <- L.permutations [1..p+q] ]
<a name="line-240"></a> where p = length u; q = length v
<a name="line-241"></a>
<a name="line-242"></a>alpha u v w = length [z | z <- pqInverseShuffles p q, let uv = shiftedConcat u v,
<a name="line-243"></a> uv * z `weakOrder` w, u and v are maximal, ie no transposition of adjacents in either also works]
<a name="line-244"></a> where p = length u
<a name="line-245"></a> q = length v
<a name="line-246"></a>-- so we need to define (*) for permutations in row form
<a name="line-247"></a>-}</span>
<a name="line-248"></a>
<a name="line-249"></a><a name="instance%20Coalgebra%20k%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span>
<a name="line-250"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-251"></a> <span class='hs-comment'>-- comult = (ssymFtoM `tf` ssymFtoM) . comult . ssymMtoF</span>
<a name="line-252"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span>
<a name="line-253"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymM</span> <span class='hs-layout'>(</span><span class='hs-varid'>flatten</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>SSymM</span> <span class='hs-layout'>(</span><span class='hs-varid'>flatten</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-254"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>ys</span><span class='hs-layout'>,</span><span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>deconcatenations</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span>
<a name="line-255"></a> <span class='hs-varid'>minimum</span> <span class='hs-layout'>(</span><span class='hs-varid'>infinity</span><span class='hs-conop'>:</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-varop'>></span> <span class='hs-varid'>maximum</span> <span class='hs-layout'>(</span><span class='hs-num'>0</span><span class='hs-conop'>:</span><span class='hs-varid'>zs</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- ie deconcatenations at a global descent</span>
<a name="line-256"></a> <span class='hs-varid'>infinity</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maxBound</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span>
<a name="line-257"></a>
<a name="line-258"></a><a name="instance%20Bialgebra%20k%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-259"></a>
<a name="line-260"></a><a name="instance%20HopfAlgebra%20k%20SSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymM</span> <span class='hs-keyword'>where</span>
<a name="line-261"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ssymFtoM</span> <span class='hs-varop'>.</span> <span class='hs-varid'>antipode</span> <span class='hs-varop'>.</span> <span class='hs-varid'>ssymMtoF</span>
<a name="line-262"></a>
<a name="line-263"></a>
<a name="line-264"></a><a name="instance%20Algebra%20k%20(Dual%20SSymF)"></a><span class='hs-comment'>-- Hazewinkel p265</span>
<a name="line-265"></a><a name="instance%20Algebra%20k%20(Dual%20SSymF)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-266"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-267"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-268"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-269"></a> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-varop'>.</span> <span class='hs-conid'>Dual</span> <span class='hs-varop'>.</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>xs''</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ys''</span><span class='hs-layout'>)</span>
<a name="line-270"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>xs'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>combinationsOf</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>r</span><span class='hs-varop'>+</span><span class='hs-varid'>s</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>ys'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>diffAsc</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>r</span><span class='hs-varop'>+</span><span class='hs-varid'>s</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>xs'</span><span class='hs-layout'>,</span>
<a name="line-271"></a> <span class='hs-varid'>xs''</span> <span class='hs-keyglyph'><-</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>permutations</span> <span class='hs-varid'>xs'</span><span class='hs-layout'>,</span> <span class='hs-varid'>flatten</span> <span class='hs-varid'>xs''</span> <span class='hs-varop'>==</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span>
<a name="line-272"></a> <span class='hs-varid'>ys''</span> <span class='hs-keyglyph'><-</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>permutations</span> <span class='hs-varid'>ys'</span><span class='hs-layout'>,</span> <span class='hs-varid'>flatten</span> <span class='hs-varid'>ys''</span> <span class='hs-varop'>==</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>]</span>
<a name="line-273"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span><span class='hs-layout'>;</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ys</span>
<a name="line-274"></a><span class='hs-comment'>-- In other words, mult x y is the sum of those z whose comult (in SSymF) has an (x,y) term</span>
<a name="line-275"></a><span class='hs-comment'>-- So the matrix for mult is the transpose of the matrix for comult in SSymF</span>
<a name="line-276"></a>
<a name="line-277"></a><a name="instance%20Coalgebra%20k%20(Dual%20SSymF)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-278"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-279"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-280"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-281"></a> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>flatten</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>ys</span><span class='hs-layout'>,</span><span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>partition</span> <span class='hs-layout'>(</span><span class='hs-varop'><=</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>]</span>
<a name="line-282"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>xs</span>
<a name="line-283"></a><span class='hs-comment'>-- In other words, comult x is the sum of those (y,z) whose mult (in SSymF) has a z term</span>
<a name="line-284"></a><span class='hs-comment'>-- So the matrix for comult is the transpose of the matrix for mult in SSymF</span>
<a name="line-285"></a>
<a name="line-286"></a><a name="instance%20Bialgebra%20k%20(Dual%20SSymF)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-287"></a>
<a name="line-288"></a><a name="instance%20HopfAlgebra%20k%20(Dual%20SSymF)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-289"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-290"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-291"></a> <span class='hs-varid'>antipode'</span> <span class='hs-varid'>x</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-292"></a> <span class='hs-layout'>(</span><span class='hs-varid'>negatev</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>id</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>antipode</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>removeTerm</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>comult</span> <span class='hs-varop'>.</span> <span class='hs-varid'>return</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span>
<a name="line-293"></a>
<a name="line-294"></a><a name="instance%20HasPairing%20k%20SSymF%20(Dual%20SSymF)"></a><span class='hs-comment'>-- This pairing is positive definite (Hazewinkel p267)</span>
<a name="line-295"></a><a name="instance%20HasPairing%20k%20SSymF%20(Dual%20SSymF)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HasPairing</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-296"></a> <span class='hs-varid'>pairing</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>pairing'</span> <span class='hs-keyword'>where</span>
<a name="line-297"></a> <span class='hs-varid'>pairing'</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-conid'>Dual</span> <span class='hs-varid'>y</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>delta</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span>
<a name="line-298"></a>
<a name="line-299"></a><a name="ssymFtoDual"></a><span class='hs-comment'>-- |The isomorphism from SSym to its dual that takes a permutation in the fundamental basis to its inverse in the dual basis</span>
<a name="line-300"></a><span class='hs-definition'>ssymFtoDual</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-conid'>SSymF</span><span class='hs-layout'>)</span>
<a name="line-301"></a><span class='hs-definition'>ssymFtoDual</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nf</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-conid'>Dual</span> <span class='hs-varop'>.</span> <span class='hs-varid'>inverse</span><span class='hs-layout'>)</span>
<a name="line-302"></a><span class='hs-comment'>-- This is theta on Hazewinkel p266 (though later he also uses theta for the inverse of this map)</span>
<a name="line-303"></a>
<a name="line-304"></a>
<a name="line-305"></a><span class='hs-comment'>-- YSYM: PLANAR BINARY TREES</span>
<a name="line-306"></a><span class='hs-comment'>-- These are really rooted planar binary trees.</span>
<a name="line-307"></a><span class='hs-comment'>-- It's because they're planar that we can distinguish left and right child branches.</span>
<a name="line-308"></a><span class='hs-comment'>-- (Non-planar would be if we considered trees where left and right children are swapped relative to one another as the same tree)</span>
<a name="line-309"></a><span class='hs-comment'>-- It is neither commutative nor co-commutative</span>
<a name="line-310"></a>
<a name="line-311"></a><a name="PBT"></a><span class='hs-comment'>-- |A type for (rooted) planar binary trees. The basis elements of the Loday-Ronco Hopf algebra are indexed by these.</span>
<a name="line-312"></a><a name="PBT"></a><span class='hs-comment'>--</span>
<a name="line-313"></a><a name="PBT"></a><span class='hs-comment'>-- Although the trees are labelled, we're really only interested in the shapes of the trees, and hence in the type PBT ().</span>
<a name="line-314"></a><a name="PBT"></a><span class='hs-comment'>-- The Algebra, Coalgebra and HopfAlgebra instances all ignore the labels.</span>
<a name="line-315"></a><a name="PBT"></a><span class='hs-comment'>-- However, it is convenient to allow labels, as they can be useful for seeing what is going on, and they also make it possible</span>
<a name="line-316"></a><a name="PBT"></a><span class='hs-comment'>-- to define various ways to create trees from lists of labels.</span>
<a name="line-317"></a><a name="PBT"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>E</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span><span class='hs-layout'>,</span> <span class='hs-conid'>Functor</span><span class='hs-layout'>)</span>
<a name="line-318"></a>
<a name="line-319"></a><a name="instance%20Ord%20(PBT%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Ord</span> <span class='hs-layout'>(</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-320"></a> <span class='hs-varid'>compare</span> <span class='hs-varid'>u</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>shapeSignature</span> <span class='hs-varid'>u</span><span class='hs-layout'>,</span> <span class='hs-varid'>prefix</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>shapeSignature</span> <span class='hs-varid'>v</span><span class='hs-layout'>,</span> <span class='hs-varid'>prefix</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-321"></a>
<a name="line-322"></a><a name="YSymF"></a><span class='hs-comment'>-- |The fundamental basis for (the dual of) the Loday-Ronco Hopf algebra of binary trees, YSym.</span>
<a name="line-323"></a><a name="YSymF"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>YSymF</span> <span class='hs-layout'>(</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span><span class='hs-layout'>,</span> <span class='hs-conid'>Functor</span><span class='hs-layout'>)</span>
<a name="line-324"></a>
<a name="line-325"></a><a name="instance%20Show%20(YSymF%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Show</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-326"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"F("</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>t</span> <span class='hs-varop'>++</span> <span class='hs-str'>")"</span>
<a name="line-327"></a>
<a name="line-328"></a><a name="ysymF"></a><span class='hs-comment'>-- |Construct the element of YSym in the fundamental basis indexed by the given tree</span>
<a name="line-329"></a><span class='hs-definition'>ysymF</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-330"></a><span class='hs-definition'>ysymF</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-331"></a>
<a name="line-332"></a><a name="nodecount"></a><span class='hs-comment'>{-
<a name="line-333"></a>depth (T l x r) = 1 + max (depth l) (depth r)
<a name="line-334"></a>depth E = 0
<a name="line-335"></a>-}</span>
<a name="line-336"></a><span class='hs-definition'>nodecount</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>nodecount</span> <span class='hs-varid'>l</span> <span class='hs-varop'>+</span> <span class='hs-varid'>nodecount</span> <span class='hs-varid'>r</span>
<a name="line-337"></a><span class='hs-definition'>nodecount</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-338"></a>
<a name="line-339"></a><a name="leafcount"></a><span class='hs-comment'>-- in fact leafcount t = 1 + nodecount t (easiest to see with a picture)</span>
<a name="line-340"></a><span class='hs-definition'>leafcount</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>leafcount</span> <span class='hs-varid'>l</span> <span class='hs-varop'>+</span> <span class='hs-varid'>leafcount</span> <span class='hs-varid'>r</span>
<a name="line-341"></a><span class='hs-definition'>leafcount</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span>
<a name="line-342"></a>
<a name="line-343"></a><a name="prefix"></a><span class='hs-definition'>prefix</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-344"></a><span class='hs-definition'>prefix</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-conop'>:</span> <span class='hs-varid'>prefix</span> <span class='hs-varid'>l</span> <span class='hs-varop'>++</span> <span class='hs-varid'>prefix</span> <span class='hs-varid'>r</span>
<a name="line-345"></a>
<a name="line-346"></a><a name="shapeSignature"></a><span class='hs-comment'>-- The shape signature uniquely identifies the shape of a tree.</span>
<a name="line-347"></a><span class='hs-comment'>-- Trees with distinct shapes have distinct signatures.</span>
<a name="line-348"></a><span class='hs-comment'>-- In addition, if sorting on shapeSignature, smaller trees sort before larger trees,</span>
<a name="line-349"></a><span class='hs-comment'>-- and leftward leaning trees sort before rightward leaning trees</span>
<a name="line-350"></a><span class='hs-definition'>shapeSignature</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>shapeSignature'</span> <span class='hs-layout'>(</span><span class='hs-varid'>nodeCountTree</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-351"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>shapeSignature'</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- not [], otherwise we can't distinguish T (T E () E) () E from T E () (T E () E)</span>
<a name="line-352"></a> <span class='hs-varid'>shapeSignature'</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-conop'>:</span> <span class='hs-varid'>shapeSignature'</span> <span class='hs-varid'>r</span> <span class='hs-varop'>++</span> <span class='hs-varid'>shapeSignature'</span> <span class='hs-varid'>l</span>
<a name="line-353"></a>
<a name="line-354"></a><a name="nodeCountTree"></a><span class='hs-definition'>nodeCountTree</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-355"></a><span class='hs-definition'>nodeCountTree</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l'</span> <span class='hs-varid'>n</span> <span class='hs-varid'>r'</span>
<a name="line-356"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>l'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nodeCountTree</span> <span class='hs-varid'>l</span>
<a name="line-357"></a> <span class='hs-varid'>r'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nodeCountTree</span> <span class='hs-varid'>r</span>
<a name="line-358"></a> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-keyword'>case</span> <span class='hs-varid'>l'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>0</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>lc</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>lc</span><span class='hs-layout'>)</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-keyword'>case</span> <span class='hs-varid'>r'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>0</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>rc</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>rc</span><span class='hs-layout'>)</span>
<a name="line-359"></a>
<a name="line-360"></a><a name="leafCountTree"></a><span class='hs-definition'>leafCountTree</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-361"></a><span class='hs-definition'>leafCountTree</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l'</span> <span class='hs-varid'>n</span> <span class='hs-varid'>r'</span>
<a name="line-362"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>l'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>leafCountTree</span> <span class='hs-varid'>l</span>
<a name="line-363"></a> <span class='hs-varid'>r'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>leafCountTree</span> <span class='hs-varid'>r</span>
<a name="line-364"></a> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-keyword'>case</span> <span class='hs-varid'>l'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>1</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>lc</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>lc</span><span class='hs-layout'>)</span> <span class='hs-varop'>+</span> <span class='hs-layout'>(</span><span class='hs-keyword'>case</span> <span class='hs-varid'>r'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>1</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>rc</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>rc</span><span class='hs-layout'>)</span>
<a name="line-365"></a>
<a name="line-366"></a><a name="lrCountTree"></a><span class='hs-comment'>-- A tree that counts nodes in left and right subtrees</span>
<a name="line-367"></a><span class='hs-definition'>lrCountTree</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-368"></a><span class='hs-definition'>lrCountTree</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lc</span><span class='hs-layout'>,</span><span class='hs-varid'>rc</span><span class='hs-layout'>)</span> <span class='hs-varid'>r'</span>
<a name="line-369"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>l'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lrCountTree</span> <span class='hs-varid'>l</span>
<a name="line-370"></a> <span class='hs-varid'>r'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lrCountTree</span> <span class='hs-varid'>r</span>
<a name="line-371"></a> <span class='hs-varid'>lc</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>l'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>0</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-varid'>llc</span><span class='hs-layout'>,</span><span class='hs-varid'>lrc</span><span class='hs-layout'>)</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>llc</span> <span class='hs-varop'>+</span> <span class='hs-varid'>lrc</span>
<a name="line-372"></a> <span class='hs-varid'>rc</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>r'</span> <span class='hs-keyword'>of</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>0</span><span class='hs-layout'>;</span> <span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-varid'>rlc</span><span class='hs-layout'>,</span><span class='hs-varid'>rrc</span><span class='hs-layout'>)</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>rlc</span> <span class='hs-varop'>+</span> <span class='hs-varid'>rrc</span>
<a name="line-373"></a>
<a name="line-374"></a><a name="shape"></a><span class='hs-definition'>shape</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>PBT</span> <span class='hs-conid'>()</span>
<a name="line-375"></a><span class='hs-definition'>shape</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span>
<a name="line-376"></a>
<a name="line-377"></a><a name="numbered"></a><span class='hs-comment'>-- label the nodes of a tree in infix order while preserving its shape</span>
<a name="line-378"></a><span class='hs-definition'>numbered</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>numbered'</span> <span class='hs-num'>1</span> <span class='hs-varid'>t</span>
<a name="line-379"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>numbered'</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-380"></a> <span class='hs-varid'>numbered'</span> <span class='hs-varid'>i</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>k</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nodecount</span> <span class='hs-varid'>l</span> <span class='hs-keyword'>in</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-varid'>numbered'</span> <span class='hs-varid'>i</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>i</span><span class='hs-varop'>+</span><span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>numbered'</span> <span class='hs-layout'>(</span><span class='hs-varid'>i</span><span class='hs-varop'>+</span><span class='hs-varid'>k</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-381"></a><span class='hs-comment'>-- could also pair the numbers with the input labels</span>
<a name="line-382"></a>
<a name="line-383"></a>
<a name="line-384"></a><a name="splits"></a><span class='hs-definition'>splits</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span><span class='hs-layout'>,</span><span class='hs-conid'>E</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-385"></a><span class='hs-definition'>splits</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span> <span class='hs-conid'>T</span> <span class='hs-varid'>v</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>splits</span> <span class='hs-varid'>l</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>u</span><span class='hs-layout'>,</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>splits</span> <span class='hs-varid'>r</span><span class='hs-keyglyph'>]</span>
<a name="line-386"></a>
<a name="line-387"></a><a name="instance%20Coalgebra%20k%20(YSymF%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-388"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span><span class='hs-layout'>;</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-389"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span>
<a name="line-390"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>u</span><span class='hs-layout'>,</span> <span class='hs-conid'>YSymF</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>splits</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-391"></a> <span class='hs-comment'>-- using sumv rather than sum to avoid requiring Show a</span>
<a name="line-392"></a> <span class='hs-comment'>-- so again this is a kind of deconcatenation coproduct</span>
<a name="line-393"></a>
<a name="line-394"></a><a name="multisplits"></a><span class='hs-definition'>multisplits</span> <span class='hs-num'>1</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>]</span>
<a name="line-395"></a><span class='hs-definition'>multisplits</span> <span class='hs-num'>2</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>splits</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>]</span>
<a name="line-396"></a><span class='hs-definition'>multisplits</span> <span class='hs-varid'>n</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>u</span><span class='hs-conop'>:</span><span class='hs-varid'>ws</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>u</span><span class='hs-layout'>,</span><span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>splits</span> <span class='hs-varid'>t</span><span class='hs-layout'>,</span> <span class='hs-varid'>ws</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>multisplits</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>]</span>
<a name="line-397"></a>
<a name="line-398"></a><a name="graft"></a><span class='hs-definition'>graft</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t</span>
<a name="line-399"></a><span class='hs-definition'>graft</span> <span class='hs-varid'>ts</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>ls</span><span class='hs-layout'>,</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>splitAt</span> <span class='hs-layout'>(</span><span class='hs-varid'>leafcount</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varid'>ts</span>
<a name="line-400"></a> <span class='hs-keyword'>in</span> <span class='hs-conid'>T</span> <span class='hs-layout'>(</span><span class='hs-varid'>graft</span> <span class='hs-varid'>ls</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span> <span class='hs-layout'>(</span><span class='hs-varid'>graft</span> <span class='hs-varid'>rs</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-401"></a>
<a name="line-402"></a><a name="instance%20Algebra%20k%20(YSymF%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-403"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span>
<a name="line-404"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-405"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>,</span> <span class='hs-conid'>YSymF</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>graft</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>ts</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>multisplits</span> <span class='hs-layout'>(</span><span class='hs-varid'>leafcount</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-406"></a> <span class='hs-comment'>-- using sumv rather than sum to avoid requiring Show a</span>
<a name="line-407"></a>
<a name="line-408"></a><a name="instance%20Bialgebra%20k%20(YSymF%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-409"></a>
<a name="line-410"></a><a name="instance%20HopfAlgebra%20k%20(YSymF%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-411"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-412"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span>
<a name="line-413"></a> <span class='hs-varid'>antipode'</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>negatev</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>id</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>antipode</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>removeTerm</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>E</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>comult</span> <span class='hs-varop'>.</span> <span class='hs-varid'>return</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span>
<a name="line-414"></a>
<a name="line-415"></a>
<a name="line-416"></a><a name="YSymM"></a><span class='hs-comment'>-- |An alternative \"monomial\" basis for (the dual of) the Loday-Ronco Hopf algebra of binary trees, YSym.</span>
<a name="line-417"></a><a name="YSymM"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>YSymM</span> <span class='hs-layout'>(</span><span class='hs-conid'>PBT</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span><span class='hs-layout'>)</span>
<a name="line-418"></a>
<a name="line-419"></a><a name="instance%20Show%20YSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyword'>where</span>
<a name="line-420"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"M("</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>t</span> <span class='hs-varop'>++</span> <span class='hs-str'>")"</span>
<a name="line-421"></a>
<a name="line-422"></a><a name="ysymM"></a><span class='hs-comment'>-- |Construct the element of YSym in the monomial basis indexed by the given tree</span>
<a name="line-423"></a><span class='hs-definition'>ysymM</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PBT</span> <span class='hs-conid'>()</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>YSymM</span>
<a name="line-424"></a><span class='hs-definition'>ysymM</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-425"></a>
<a name="line-426"></a><a name="trees"></a><span class='hs-comment'>-- |List all trees with the given number of nodes</span>
<a name="line-427"></a><span class='hs-definition'>trees</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>PBT</span> <span class='hs-conid'>()</span><span class='hs-keyglyph'>]</span>
<a name="line-428"></a><span class='hs-definition'>trees</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>E</span><span class='hs-keyglyph'>]</span>
<a name="line-429"></a><span class='hs-definition'>trees</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-conid'>()</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-varid'>l</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>trees</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-comment'>-</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>trees</span> <span class='hs-varid'>i</span><span class='hs-keyglyph'>]</span>
<a name="line-430"></a>
<a name="line-431"></a><a name="tamariCovers"></a><span class='hs-comment'>-- |The covering relation for the Tamari partial order on binary trees</span>
<a name="line-432"></a><span class='hs-definition'>tamariCovers</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-433"></a><span class='hs-definition'>tamariCovers</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-434"></a><span class='hs-definition'>tamariCovers</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>u</span> <span class='hs-varid'>x</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>y</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>T</span> <span class='hs-varid'>t'</span> <span class='hs-varid'>y</span> <span class='hs-varid'>w</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>t'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>tamariCovers</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-435"></a> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>T</span> <span class='hs-varid'>t</span> <span class='hs-varid'>y</span> <span class='hs-varid'>w'</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>w'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>tamariCovers</span> <span class='hs-varid'>w</span><span class='hs-keyglyph'>]</span>
<a name="line-436"></a> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>T</span> <span class='hs-varid'>u</span> <span class='hs-varid'>y</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>v</span> <span class='hs-varid'>x</span> <span class='hs-varid'>w</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-437"></a> <span class='hs-comment'>-- Note that this preserves the descending property, and hence the bijection with permutations</span>
<a name="line-438"></a> <span class='hs-comment'>-- If we were to swap x and y, we would preserve the binary search tree property instead (if our trees had it)</span>
<a name="line-439"></a><span class='hs-definition'>tamariCovers</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-conid'>E</span> <span class='hs-varid'>x</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>T</span> <span class='hs-conid'>E</span> <span class='hs-varid'>x</span> <span class='hs-varid'>u'</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>u'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>tamariCovers</span> <span class='hs-varid'>u</span><span class='hs-keyglyph'>]</span>
<a name="line-440"></a>
<a name="line-441"></a><a name="tamariUpSet"></a><span class='hs-comment'>-- |The up-set of a binary tree in the Tamari partial order</span>
<a name="line-442"></a><span class='hs-definition'>tamariUpSet</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-443"></a><span class='hs-definition'>tamariUpSet</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>upSet'</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-444"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>upSet'</span> <span class='hs-varid'>interior</span> <span class='hs-varid'>boundary</span> <span class='hs-keyglyph'>=</span>
<a name="line-445"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>boundary</span>
<a name="line-446"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>interior</span>
<a name="line-447"></a> <span class='hs-keyword'>else</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>interior'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>setUnionAsc</span> <span class='hs-varid'>interior</span> <span class='hs-varid'>boundary</span>
<a name="line-448"></a> <span class='hs-varid'>boundary'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>toSet</span> <span class='hs-varop'>$</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>tamariCovers</span> <span class='hs-varid'>boundary</span>
<a name="line-449"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>upSet'</span> <span class='hs-varid'>interior'</span> <span class='hs-varid'>boundary'</span>
<a name="line-450"></a>
<a name="line-451"></a><span class='hs-comment'>-- tamariOrder1 u v = v `elem` upSet u</span>
<a name="line-452"></a>
<a name="line-453"></a><a name="tamariOrder"></a><span class='hs-comment'>-- |The Tamari partial order on binary trees.</span>
<a name="line-454"></a><span class='hs-comment'>-- This is only defined between trees of the same size (number of nodes).</span>
<a name="line-455"></a><span class='hs-comment'>-- The result between trees of different sizes is undefined (we don't check).</span>
<a name="line-456"></a><span class='hs-definition'>tamariOrder</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>PBT</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-457"></a><span class='hs-definition'>tamariOrder</span> <span class='hs-varid'>u</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>weakOrder</span> <span class='hs-layout'>(</span><span class='hs-varid'>minPerm</span> <span class='hs-varid'>u</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>minPerm</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-458"></a><span class='hs-comment'>-- It should be possible to unpack this to be a statement purely about trees, but probably not worth it</span>
<a name="line-459"></a>
<a name="line-460"></a><a name="ysymMtoF"></a><span class='hs-comment'>-- |Convert an element of YSym represented in the monomial basis to the fundamental basis</span>
<a name="line-461"></a><span class='hs-definition'>ysymMtoF</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span>
<a name="line-462"></a><span class='hs-definition'>ysymMtoF</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>ysymMtoF'</span> <span class='hs-keyword'>where</span>
<a name="line-463"></a> <span class='hs-varid'>ysymMtoF'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>mu</span> <span class='hs-layout'>(</span><span class='hs-varid'>set</span><span class='hs-layout'>,</span><span class='hs-varid'>po</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span> <span class='hs-varid'>s</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>set</span><span class='hs-keyglyph'>]</span>
<a name="line-464"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>po</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tamariOrder</span>
<a name="line-465"></a> <span class='hs-varid'>set</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tamariUpSet</span> <span class='hs-varid'>t</span> <span class='hs-comment'>-- [s | s <- trees (nodecount t), t `tamariOrder` s]</span>
<a name="line-466"></a>
<a name="line-467"></a><a name="ysymFtoM"></a><span class='hs-comment'>-- |Convert an element of YSym represented in the fundamental basis to the monomial basis</span>
<a name="line-468"></a><span class='hs-definition'>ysymFtoM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span>
<a name="line-469"></a><span class='hs-definition'>ysymFtoM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>ysymFtoM'</span> <span class='hs-keyword'>where</span>
<a name="line-470"></a> <span class='hs-varid'>ysymFtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>tamariUpSet</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-471"></a> <span class='hs-comment'>-- sumv [return (YSymM s) | s <- trees (nodecount t), t `tamariOrder` s]</span>
<a name="line-472"></a>
<a name="line-473"></a>
<a name="line-474"></a><a name="instance%20Algebra%20k%20YSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyword'>where</span>
<a name="line-475"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span>
<a name="line-476"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ysymFtoM</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>ysymMtoF</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>ysymMtoF</span><span class='hs-layout'>)</span>
<a name="line-477"></a>
<a name="line-478"></a><a name="instance%20Coalgebra%20k%20YSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyword'>where</span>
<a name="line-479"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span><span class='hs-layout'>;</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-480"></a> <span class='hs-comment'>-- comult = (ysymFtoM `tf` ysymFtoM) . comult . ysymMtoF</span>
<a name="line-481"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-482"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>r</span><span class='hs-layout'>,</span> <span class='hs-conid'>YSymM</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>rs</span><span class='hs-layout'>,</span><span class='hs-varid'>ss</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>deconcatenations</span> <span class='hs-layout'>(</span><span class='hs-varid'>underDecomposition</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-483"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldl</span> <span class='hs-varid'>under</span> <span class='hs-conid'>E</span> <span class='hs-varid'>rs</span><span class='hs-layout'>,</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldl</span> <span class='hs-varid'>under</span> <span class='hs-conid'>E</span> <span class='hs-varid'>ss</span><span class='hs-keyglyph'>]</span>
<a name="line-484"></a>
<a name="line-485"></a>
<a name="line-486"></a><a name="instance%20Bialgebra%20k%20YSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-487"></a>
<a name="line-488"></a><a name="instance%20HopfAlgebra%20k%20YSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>YSymM</span> <span class='hs-keyword'>where</span>
<a name="line-489"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ysymFtoM</span> <span class='hs-varop'>.</span> <span class='hs-varid'>antipode</span> <span class='hs-varop'>.</span> <span class='hs-varid'>ysymMtoF</span>
<a name="line-490"></a>
<a name="line-491"></a>
<a name="line-492"></a><span class='hs-comment'>-- QSYM: QUASI-SYMMETRIC FUNCTIONS</span>
<a name="line-493"></a><span class='hs-comment'>-- The following is the Hopf algebra QSym of quasi-symmetric functions</span>
<a name="line-494"></a><span class='hs-comment'>-- using the monomial and fundamental bases (indexed by compositions)</span>
<a name="line-495"></a>
<a name="line-496"></a><a name="compositions"></a><span class='hs-comment'>-- compositions in ascending order</span>
<a name="line-497"></a><span class='hs-comment'>-- might be better to use bfs to get length order</span>
<a name="line-498"></a><span class='hs-comment'>-- |List the compositions of an integer n. For example, the compositions of 4 are [[1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1],[4]]</span>
<a name="line-499"></a><span class='hs-definition'>compositions</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span><span class='hs-keyglyph'>]</span>
<a name="line-500"></a><span class='hs-definition'>compositions</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>[]</span><span class='hs-keyglyph'>]</span>
<a name="line-501"></a><span class='hs-definition'>compositions</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>i</span><span class='hs-conop'>:</span><span class='hs-varid'>is</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-varid'>is</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>compositions</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-502"></a>
<a name="line-503"></a><span class='hs-comment'>-- can retrieve subsets of [1..n-1] from compositions n as follows</span>
<a name="line-504"></a><span class='hs-comment'>-- > map (tail . scanl (+) 0) (map init $ compositions 4)</span>
<a name="line-505"></a><span class='hs-comment'>-- [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]</span>
<a name="line-506"></a>
<a name="line-507"></a><a name="quasiShuffles"></a><span class='hs-comment'>-- quasi shuffles of two compositions</span>
<a name="line-508"></a><span class='hs-definition'>quasiShuffles</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span><span class='hs-keyglyph'>]</span>
<a name="line-509"></a><span class='hs-definition'>quasiShuffles</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>quasiShuffles</span> <span class='hs-varid'>xs</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-varid'>ys</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span>
<a name="line-510"></a> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-varop'>+</span><span class='hs-varid'>y</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>quasiShuffles</span> <span class='hs-varid'>xs</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span>
<a name="line-511"></a> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>quasiShuffles</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-512"></a><span class='hs-definition'>quasiShuffles</span> <span class='hs-varid'>xs</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-513"></a><span class='hs-definition'>quasiShuffles</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>ys</span><span class='hs-keyglyph'>]</span>
<a name="line-514"></a>
<a name="line-515"></a>
<a name="line-516"></a><a name="QSymM"></a><span class='hs-comment'>-- |A type for the monomial basis for the quasi-symmetric functions, indexed by compositions.</span>
<a name="line-517"></a><a name="QSymM"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-518"></a>
<a name="line-519"></a><a name="instance%20Ord%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-520"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-521"></a>
<a name="line-522"></a><a name="instance%20Show%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-523"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"M "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>xs</span>
<a name="line-524"></a>
<a name="line-525"></a><a name="qsymM"></a><span class='hs-comment'>-- |Construct the element of QSym in the monomial basis indexed by the given composition</span>
<a name="line-526"></a><span class='hs-definition'>qsymM</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>QSymM</span>
<a name="line-527"></a><span class='hs-definition'>qsymM</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-528"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"qsymM: not a composition"</span>
<a name="line-529"></a>
<a name="line-530"></a><a name="instance%20Algebra%20k%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-531"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-532"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-533"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>,</span> <span class='hs-conid'>QSymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>gamma</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>gamma</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>quasiShuffles</span> <span class='hs-varid'>alpha</span> <span class='hs-varid'>beta</span><span class='hs-keyglyph'>]</span>
<a name="line-534"></a>
<a name="line-535"></a><a name="instance%20Coalgebra%20k%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-536"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>alpha</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-537"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-538"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>gamma</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>,</span> <span class='hs-conid'>QSymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>alpha</span><span class='hs-layout'>,</span><span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>deconcatenations</span> <span class='hs-varid'>gamma</span><span class='hs-keyglyph'>]</span>
<a name="line-539"></a>
<a name="line-540"></a><a name="instance%20Bialgebra%20k%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-541"></a>
<a name="line-542"></a><a name="instance%20HopfAlgebra%20k%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-543"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-544"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-varop'>^</span><span class='hs-varid'>length</span> <span class='hs-varid'>alpha</span> <span class='hs-varop'>*</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>beta</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>coarsenings</span> <span class='hs-layout'>(</span><span class='hs-varid'>reverse</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-545"></a> <span class='hs-comment'>-- antipode' (QSymM alpha) = (-1)^length alpha * sumv [return (QSymM (reverse beta)) | beta <- coarsenings alpha]</span>
<a name="line-546"></a>
<a name="line-547"></a><a name="coarsenings"></a><span class='hs-definition'>coarsenings</span> <span class='hs-layout'>(</span><span class='hs-varid'>x1</span><span class='hs-conop'>:</span><span class='hs-varid'>x2</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>x1</span><span class='hs-conop'>:</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>coarsenings</span> <span class='hs-layout'>(</span><span class='hs-varid'>x2</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-varid'>coarsenings</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>x1</span><span class='hs-varop'>+</span><span class='hs-varid'>x2</span><span class='hs-layout'>)</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-548"></a><span class='hs-definition'>coarsenings</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- for xs a singleton or null</span>
<a name="line-549"></a>
<a name="line-550"></a><a name="refinements"></a><span class='hs-definition'>refinements</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>y</span><span class='hs-varop'>++</span><span class='hs-varid'>ys</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>compositions</span> <span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>refinements</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-551"></a><span class='hs-definition'>refinements</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>[]</span><span class='hs-keyglyph'>]</span>
<a name="line-552"></a>
<a name="line-553"></a>
<a name="line-554"></a><a name="QSymF"></a><span class='hs-comment'>-- |A type for the fundamental basis for the quasi-symmetric functions, indexed by compositions.</span>
<a name="line-555"></a><a name="QSymF"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>)</span>
<a name="line-556"></a>
<a name="line-557"></a><a name="instance%20Ord%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span>
<a name="line-558"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span>
<a name="line-559"></a>
<a name="line-560"></a><a name="instance%20Show%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span>
<a name="line-561"></a> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"F "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>xs</span>
<a name="line-562"></a>
<a name="line-563"></a><a name="qsymF"></a><span class='hs-comment'>-- |Construct the element of QSym in the fundamental basis indexed by the given composition</span>
<a name="line-564"></a><span class='hs-definition'>qsymF</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>QSymF</span>
<a name="line-565"></a><span class='hs-definition'>qsymF</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-566"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"qsymF: not a composition"</span>
<a name="line-567"></a>
<a name="line-568"></a><a name="qsymMtoF"></a><span class='hs-comment'>-- |Convert an element of QSym represented in the monomial basis to the fundamental basis</span>
<a name="line-569"></a><span class='hs-definition'>qsymMtoF</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span>
<a name="line-570"></a><span class='hs-definition'>qsymMtoF</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>qsymMtoF'</span> <span class='hs-keyword'>where</span>
<a name="line-571"></a> <span class='hs-varid'>qsymMtoF'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varop'>^</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>beta</span> <span class='hs-comment'>-</span> <span class='hs-varid'>length</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>beta</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>refinements</span> <span class='hs-varid'>alpha</span><span class='hs-keyglyph'>]</span>
<a name="line-572"></a>
<a name="line-573"></a><a name="qsymFtoM"></a><span class='hs-comment'>-- |Convert an element of QSym represented in the fundamental basis to the monomial basis</span>
<a name="line-574"></a><span class='hs-definition'>qsymFtoM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span>
<a name="line-575"></a><span class='hs-definition'>qsymFtoM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>qsymFtoM'</span> <span class='hs-keyword'>where</span>
<a name="line-576"></a> <span class='hs-varid'>qsymFtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>beta</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>refinements</span> <span class='hs-varid'>alpha</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- ie beta <- up-set of alpha</span>
<a name="line-577"></a>
<a name="line-578"></a><a name="instance%20Algebra%20k%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span>
<a name="line-579"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-580"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>qsymMtoF</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>qsymFtoM</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>qsymFtoM</span><span class='hs-layout'>)</span>
<a name="line-581"></a>
<a name="line-582"></a><a name="instance%20Coalgebra%20k%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span>
<a name="line-583"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-584"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>qsymMtoF</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>qsymMtoF</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>comult</span> <span class='hs-varop'>.</span> <span class='hs-varid'>qsymFtoM</span>
<a name="line-585"></a>
<a name="line-586"></a><a name="instance%20Bialgebra%20k%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-587"></a>
<a name="line-588"></a><a name="instance%20HopfAlgebra%20k%20QSymF"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span> <span class='hs-keyword'>where</span>
<a name="line-589"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>qsymMtoF</span> <span class='hs-varop'>.</span> <span class='hs-varid'>antipode</span> <span class='hs-varop'>.</span> <span class='hs-varid'>qsymFtoM</span>
<a name="line-590"></a>
<a name="line-591"></a>
<a name="line-592"></a><span class='hs-comment'>-- QUASI-SYMMETRIC POLYNOMIALS</span>
<a name="line-593"></a>
<a name="line-594"></a><span class='hs-comment'>-- the above induces Hopf algebra structure on quasi-symmetric functions via</span>
<a name="line-595"></a><span class='hs-comment'>-- m_alpha -> sum [product (zipWith (^) (map x_ is) alpha | is <- combinationsOf k [] ] where k = length alpha</span>
<a name="line-596"></a>
<a name="line-597"></a><span class='hs-comment'>-- xvars n = [glexvar ("x" ++ show i) | i <- [1..n] ]</span>
<a name="line-598"></a>
<a name="line-599"></a><a name="qsymPoly"></a><span class='hs-comment'>-- |@qsymPoly n is@ is the quasi-symmetric polynomial in n variables for the indices is. (This corresponds to the</span>
<a name="line-600"></a><span class='hs-comment'>-- monomial basis for QSym.) For example, qsymPoly 3 [2,1] == x1^2*x2+x1^2*x3+x2^2*x3.</span>
<a name="line-601"></a><span class='hs-definition'>qsymPoly</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>GlexPoly</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>String</span>
<a name="line-602"></a><span class='hs-definition'>qsymPoly</span> <span class='hs-varid'>n</span> <span class='hs-varid'>is</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sum</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>product</span> <span class='hs-layout'>(</span><span class='hs-varid'>zipWith</span> <span class='hs-layout'>(</span><span class='hs-varop'>^</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs'</span> <span class='hs-varid'>is</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>xs'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>combinationsOf</span> <span class='hs-varid'>r</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-603"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>glexvar</span> <span class='hs-layout'>(</span><span class='hs-str'>"x"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>i</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>]</span>
<a name="line-604"></a> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>is</span>
<a name="line-605"></a>
<a name="line-606"></a>
<a name="line-607"></a><span class='hs-comment'>-- SYM, THE HOPF ALGEBRA OF SYMMETRIC FUNCTIONS</span>
<a name="line-608"></a>
<a name="line-609"></a><a name="SymM"></a><span class='hs-comment'>-- |A type for the monomial basis for Sym, the Hopf algebra of symmetric functions, indexed by integer partitions</span>
<a name="line-610"></a><a name="SymM"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>SymM</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymM</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-611"></a>
<a name="line-612"></a><a name="instance%20Ord%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Ord</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span>
<a name="line-613"></a> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>compare</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>sum</span> <span class='hs-varid'>ys</span><span class='hs-layout'>,</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- note the order reversal in snd</span>
<a name="line-614"></a>
<a name="line-615"></a><a name="symM"></a><span class='hs-comment'>-- |Construct the element of Sym in the monomial basis indexed by the given integer partition</span>
<a name="line-616"></a><span class='hs-definition'>symM</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>SymM</span>
<a name="line-617"></a><span class='hs-definition'>symM</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varop'>$</span> <span class='hs-varid'>sortDesc</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-618"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"symM: not a partition"</span>
<a name="line-619"></a>
<a name="line-620"></a><a name="instance%20Algebra%20k%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span>
<a name="line-621"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-622"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-623"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>,</span> <span class='hs-conid'>SymM</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>nu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>nu</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>symMult</span> <span class='hs-varid'>lambda</span> <span class='hs-varid'>mu</span><span class='hs-keyglyph'>]</span>
<a name="line-624"></a>
<a name="line-625"></a><span class='hs-comment'>-- multisetPermutations = toSet . L.permutations</span>
<a name="line-626"></a>
<a name="line-627"></a><span class='hs-comment'>-- compositionsFromPartition2 = foldl (\xss ys -> concatMap (shuffles ys) xss) [[]] . L.group</span>
<a name="line-628"></a><span class='hs-comment'>-- compositionsFromPartition2 = foldl (\ls r -> concat [shuffles l r | l <- ls]) [[]] . L.group</span>
<a name="line-629"></a>
<a name="line-630"></a><a name="compositionsFromPartition"></a><span class='hs-comment'>-- The partition must be in either ascending or descending order (so that L.group does as expected)</span>
<a name="line-631"></a><span class='hs-definition'>compositionsFromPartition</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>l</span> <span class='hs-varid'>rs</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>concatMap</span> <span class='hs-layout'>(</span><span class='hs-varid'>shuffles</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>[]</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>.</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>group</span>
<a name="line-632"></a>
<a name="line-633"></a><a name="symMult"></a><span class='hs-comment'>-- In effect, we multiply in Sym by converting to QSym, multiplying there, and converting back.</span>
<a name="line-634"></a><span class='hs-comment'>-- It would be nice to find a more direct method.</span>
<a name="line-635"></a><span class='hs-definition'>symMult</span> <span class='hs-varid'>xs</span> <span class='hs-varid'>ys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>filter</span> <span class='hs-varid'>isWeaklyDecreasing</span> <span class='hs-varop'>$</span> <span class='hs-varid'>concat</span>
<a name="line-636"></a> <span class='hs-keyglyph'>[</span><span class='hs-varid'>quasiShuffles</span> <span class='hs-varid'>xs'</span> <span class='hs-varid'>ys'</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>xs'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>compositionsFromPartition</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-varid'>ys'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>compositionsFromPartition</span> <span class='hs-varid'>ys</span><span class='hs-keyglyph'>]</span>
<a name="line-637"></a>
<a name="line-638"></a><a name="instance%20Coalgebra%20k%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span>
<a name="line-639"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>lambda</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-640"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-641"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>mu</span><span class='hs-layout'>,</span> <span class='hs-conid'>SymM</span> <span class='hs-varid'>nu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>mu</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>toSet</span> <span class='hs-layout'>(</span><span class='hs-varid'>powersetdfs</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>nu</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>diffDesc</span> <span class='hs-varid'>lambda</span> <span class='hs-varid'>mu</span><span class='hs-keyglyph'>]</span>
<a name="line-642"></a>
<a name="line-643"></a><a name="instance%20Bialgebra%20k%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-644"></a>
<a name="line-645"></a><a name="instance%20HopfAlgebra%20k%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span>
<a name="line-646"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-647"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-648"></a> <span class='hs-varid'>antipode'</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>negatev</span> <span class='hs-varop'>.</span> <span class='hs-varid'>mult</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>id</span> <span class='hs-varop'>`tf`</span> <span class='hs-varid'>antipode</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>removeTerm</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>comult</span> <span class='hs-varop'>.</span> <span class='hs-varid'>return</span><span class='hs-layout'>)</span> <span class='hs-varid'>x</span>
<a name="line-649"></a>
<a name="line-650"></a>
<a name="line-651"></a><a name="SymE"></a><span class='hs-comment'>-- |The elementary basis for Sym, the Hopf algebra of symmetric functions. Defined informally as</span>
<a name="line-652"></a><a name="SymE"></a><span class='hs-comment'>-- > symE [n] = symM (replicate n 1)</span>
<a name="line-653"></a><a name="SymE"></a><span class='hs-comment'>-- > symE lambda = product [symE [p] | p <- lambda]</span>
<a name="line-654"></a><a name="SymE"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Ord</span><span class='hs-layout'>,</span><span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-655"></a>
<a name="line-656"></a><a name="symE"></a><span class='hs-definition'>symE</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>SymE</span>
<a name="line-657"></a><span class='hs-definition'>symE</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-varop'>$</span> <span class='hs-varid'>sortDesc</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-658"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"symE: not a partition"</span>
<a name="line-659"></a>
<a name="line-660"></a><a name="instance%20Algebra%20k%20SymE"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymE</span> <span class='hs-keyword'>where</span>
<a name="line-661"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-662"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>,</span> <span class='hs-conid'>SymE</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-varop'>$</span> <span class='hs-conid'>SymE</span> <span class='hs-varop'>$</span> <span class='hs-varid'>multisetSumDesc</span> <span class='hs-varid'>lambda</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span>
<a name="line-663"></a>
<a name="line-664"></a><a name="instance%20Coalgebra%20k%20SymE"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymE</span> <span class='hs-keyword'>where</span>
<a name="line-665"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>lambda</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-666"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-667"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>e</span> <span class='hs-varid'>i</span><span class='hs-layout'>,</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>]</span>
<a name="line-668"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>lambda</span><span class='hs-keyglyph'>]</span>
<a name="line-669"></a> <span class='hs-varid'>e</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymE</span> <span class='hs-conid'>[]</span>
<a name="line-670"></a> <span class='hs-varid'>e</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>i</span><span class='hs-keyglyph'>]</span>
<a name="line-671"></a>
<a name="line-672"></a><a name="instance%20Bialgebra%20k%20SymE"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymE</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-673"></a>
<a name="line-674"></a><a name="symEtoM"></a><span class='hs-comment'>-- |Convert from the elementary to the monomial basis of Sym</span>
<a name="line-675"></a><span class='hs-definition'>symEtoM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span>
<a name="line-676"></a><span class='hs-definition'>symEtoM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>symEtoM'</span> <span class='hs-keyword'>where</span>
<a name="line-677"></a> <span class='hs-varid'>symEtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-layout'>(</span><span class='hs-varid'>replicate</span> <span class='hs-varid'>n</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-678"></a> <span class='hs-varid'>symEtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>symEtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>p</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>lambda</span><span class='hs-keyglyph'>]</span>
<a name="line-679"></a>
<a name="line-680"></a>
<a name="line-681"></a><a name="SymH"></a><span class='hs-comment'>-- |The complete basis for Sym, the Hopf algebra of symmetric functions. Defined informally as</span>
<a name="line-682"></a><a name="SymH"></a><span class='hs-comment'>-- > symH [n] = sum [symM lambda | lambda <- integerPartitions n] -- == all monomials of weight n</span>
<a name="line-683"></a><a name="SymH"></a><span class='hs-comment'>-- > symH lambda = product [symH [p] | p <- lambda]</span>
<a name="line-684"></a><a name="SymH"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Ord</span><span class='hs-layout'>,</span><span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-685"></a>
<a name="line-686"></a><a name="symH"></a><span class='hs-definition'>symH</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>SymH</span>
<a name="line-687"></a><span class='hs-definition'>symH</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varop'>$</span> <span class='hs-varid'>sortDesc</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-688"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"symH: not a partition"</span>
<a name="line-689"></a>
<a name="line-690"></a><a name="instance%20Algebra%20k%20SymH"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span> <span class='hs-keyword'>where</span>
<a name="line-691"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-692"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>,</span> <span class='hs-conid'>SymH</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-varop'>$</span> <span class='hs-conid'>SymH</span> <span class='hs-varop'>$</span> <span class='hs-varid'>multisetSumDesc</span> <span class='hs-varid'>lambda</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span>
<a name="line-693"></a>
<a name="line-694"></a><a name="instance%20Coalgebra%20k%20SymH"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span> <span class='hs-keyword'>where</span>
<a name="line-695"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>lambda</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-696"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-697"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>h</span> <span class='hs-varid'>i</span><span class='hs-layout'>,</span> <span class='hs-varid'>h</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>]</span>
<a name="line-698"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>lambda</span><span class='hs-keyglyph'>]</span>
<a name="line-699"></a> <span class='hs-varid'>h</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymH</span> <span class='hs-conid'>[]</span>
<a name="line-700"></a> <span class='hs-varid'>h</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>i</span><span class='hs-keyglyph'>]</span>
<a name="line-701"></a>
<a name="line-702"></a><a name="instance%20Bialgebra%20k%20SymH"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-703"></a>
<a name="line-704"></a><a name="symHtoM"></a><span class='hs-comment'>-- |Convert from the complete to the monomial basis of Sym</span>
<a name="line-705"></a><span class='hs-definition'>symHtoM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span>
<a name="line-706"></a><span class='hs-definition'>symHtoM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>symHtoM'</span> <span class='hs-keyword'>where</span>
<a name="line-707"></a> <span class='hs-varid'>symHtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>mu</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>mu</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>integerPartitions</span> <span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span>
<a name="line-708"></a> <span class='hs-varid'>symHtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varid'>lambda</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>symHtoM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>p</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>lambda</span><span class='hs-keyglyph'>]</span>
<a name="line-709"></a>
<a name="line-710"></a>
<a name="line-711"></a><span class='hs-comment'>-- NSYM, THE HOPF ALGEBRA OF NON-COMMUTATIVE SYMMETRIC FUNCTIONS</span>
<a name="line-712"></a>
<a name="line-713"></a><a name="NSym"></a><span class='hs-comment'>-- |A basis for NSym, the Hopf algebra of non-commutative symmetric functions, indexed by compositions</span>
<a name="line-714"></a><a name="NSym"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span><span class='hs-conid'>Ord</span><span class='hs-layout'>,</span><span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-715"></a>
<a name="line-716"></a><a name="nsym"></a><span class='hs-definition'>nsym</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>NSym</span>
<a name="line-717"></a><span class='hs-definition'>nsym</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-718"></a><span class='hs-definition'>nsym</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-719"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"nsym: not a composition"</span>
<a name="line-720"></a>
<a name="line-721"></a><a name="instance%20Algebra%20k%20NSym"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Algebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-keyword'>where</span>
<a name="line-722"></a> <span class='hs-varid'>unit</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-723"></a> <span class='hs-varid'>mult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>mult'</span> <span class='hs-keyword'>where</span>
<a name="line-724"></a> <span class='hs-varid'>mult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>xs</span><span class='hs-layout'>,</span> <span class='hs-conid'>NSym</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-varop'>$</span> <span class='hs-conid'>NSym</span> <span class='hs-varop'>$</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ys</span>
<a name="line-725"></a>
<a name="line-726"></a><a name="instance%20Coalgebra%20k%20NSym"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Coalgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-keyword'>where</span>
<a name="line-727"></a> <span class='hs-varid'>counit</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unwrap</span> <span class='hs-varop'>.</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>counit'</span> <span class='hs-keyword'>where</span> <span class='hs-varid'>counit'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>zs</span> <span class='hs-keyword'>then</span> <span class='hs-num'>1</span> <span class='hs-keyword'>else</span> <span class='hs-num'>0</span>
<a name="line-728"></a> <span class='hs-varid'>comult</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>comult'</span> <span class='hs-keyword'>where</span>
<a name="line-729"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varid'>i</span><span class='hs-layout'>,</span> <span class='hs-varid'>z</span> <span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>0</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>]</span>
<a name="line-730"></a> <span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>comult'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>zs</span><span class='hs-keyglyph'>]</span>
<a name="line-731"></a> <span class='hs-varid'>z</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NSym</span> <span class='hs-conid'>[]</span>
<a name="line-732"></a> <span class='hs-varid'>z</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>i</span><span class='hs-keyglyph'>]</span>
<a name="line-733"></a>
<a name="line-734"></a><a name="instance%20Bialgebra%20k%20NSym"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Bialgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-keyword'>where</span> <span class='hs-layout'>{</span><span class='hs-layout'>}</span>
<a name="line-735"></a>
<a name="line-736"></a><a name="instance%20HopfAlgebra%20k%20NSym"></a><span class='hs-comment'>-- Hazewinkel et al p233</span>
<a name="line-737"></a><a name="instance%20HopfAlgebra%20k%20NSym"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HopfAlgebra</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-keyword'>where</span>
<a name="line-738"></a> <span class='hs-varid'>antipode</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>antipode'</span> <span class='hs-keyword'>where</span>
<a name="line-739"></a> <span class='hs-varid'>antipode'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-varop'>^</span><span class='hs-varid'>length</span> <span class='hs-varid'>beta</span> <span class='hs-varop'>*></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>beta</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>refinements</span> <span class='hs-layout'>(</span><span class='hs-varid'>reverse</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span>
<a name="line-740"></a>
<a name="line-741"></a>
<a name="line-742"></a>
<a name="line-743"></a><span class='hs-comment'>-- MAPS BETWEEN (POSETS AND) HOPF ALGEBRAS</span>
<a name="line-744"></a>
<a name="line-745"></a><a name="descendingTree"></a><span class='hs-comment'>-- A descending tree is one in which a child is always less than a parent.</span>
<a name="line-746"></a><span class='hs-definition'>descendingTree</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-747"></a><span class='hs-definition'>descendingTree</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>x</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-conid'>E</span> <span class='hs-varid'>x</span> <span class='hs-conid'>E</span>
<a name="line-748"></a><span class='hs-definition'>descendingTree</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span>
<a name="line-749"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maximum</span> <span class='hs-varid'>xs</span>
<a name="line-750"></a> <span class='hs-layout'>(</span><span class='hs-varid'>ls</span><span class='hs-layout'>,</span><span class='hs-keyword'>_</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>break</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span>
<a name="line-751"></a> <span class='hs-varid'>l</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>descendingTree</span> <span class='hs-varid'>ls</span>
<a name="line-752"></a> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>descendingTree</span> <span class='hs-varid'>rs</span>
<a name="line-753"></a><span class='hs-comment'>-- This is a bijection from permutations to "ordered trees".</span>
<a name="line-754"></a><span class='hs-comment'>-- It is order-preserving on trees with the same nodecount.</span>
<a name="line-755"></a><span class='hs-comment'>-- We can recover the permutation by reading the node labels in infix order.</span>
<a name="line-756"></a><span class='hs-comment'>-- This is the map called lambda in Loday.pdf</span>
<a name="line-757"></a>
<a name="line-758"></a>
<a name="line-759"></a><a name="descendingTreeMap"></a><span class='hs-comment'>-- |Given a permutation p of [1..n], we can construct a tree (the descending tree of p) as follows:</span>
<a name="line-760"></a><span class='hs-comment'>--</span>
<a name="line-761"></a><span class='hs-comment'>-- * Split the permutation as p = ls ++ [n] ++ rs</span>
<a name="line-762"></a><span class='hs-comment'>--</span>
<a name="line-763"></a><span class='hs-comment'>-- * Place n at the root of the tree, and recursively place the descending trees of ls and rs as the left and right children of the root</span>
<a name="line-764"></a><span class='hs-comment'>--</span>
<a name="line-765"></a><span class='hs-comment'>-- * To bottom out the recursion, the descending tree of the empty permutation is of course the empty tree</span>
<a name="line-766"></a><span class='hs-comment'>--</span>
<a name="line-767"></a><span class='hs-comment'>-- This map between bases SSymF -> YSymF turns out to induce a morphism of Hopf algebras.</span>
<a name="line-768"></a><span class='hs-definition'>descendingTreeMap</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span>
<a name="line-769"></a><span class='hs-definition'>descendingTreeMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nf</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varop'>.</span> <span class='hs-varid'>shape</span> <span class='hs-varop'>.</span> <span class='hs-varid'>descendingTree'</span><span class='hs-layout'>)</span>
<a name="line-770"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>descendingTree'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>descendingTree</span> <span class='hs-varid'>xs</span>
<a name="line-771"></a><span class='hs-comment'>-- This is the map called Lambda in Loday.pdf, or tau in MSym.pdf</span>
<a name="line-772"></a><span class='hs-comment'>-- It is an algebra morphism.</span>
<a name="line-773"></a>
<a name="line-774"></a><span class='hs-comment'>-- One of the ideas in the MSym paper is to look at the intermediate result (fmap descendingTree' x),</span>
<a name="line-775"></a><span class='hs-comment'>-- which is an "ordered tree", and consider the map as factored through this</span>
<a name="line-776"></a>
<a name="line-777"></a><span class='hs-comment'>-- The map is surjective but not injective. The fibers tau^-1(t) are intervals in the weak order on permutations</span>
<a name="line-778"></a>
<a name="line-779"></a><a name="minPerm"></a><span class='hs-comment'>-- "inverse" for descendingTree</span>
<a name="line-780"></a><span class='hs-comment'>-- These are the maps called gamma in Loday.pdf</span>
<a name="line-781"></a><span class='hs-comment'>-- or are they? - these give the min and max inverse images in the lexicographic order, rather than the weak order?</span>
<a name="line-782"></a><span class='hs-definition'>minPerm</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>minPerm'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lrCountTree</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-783"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>minPerm'</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-784"></a> <span class='hs-varid'>minPerm'</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-layout'>(</span><span class='hs-varid'>lc</span><span class='hs-layout'>,</span><span class='hs-varid'>rc</span><span class='hs-layout'>)</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>minPerm'</span> <span class='hs-varid'>l</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>lc</span><span class='hs-varop'>+</span><span class='hs-varid'>rc</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-varid'>lc</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>minPerm'</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-785"></a>
<a name="line-786"></a><a name="maxPerm"></a><span class='hs-definition'>maxPerm</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maxPerm'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lrCountTree</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-787"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>maxPerm'</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-788"></a> <span class='hs-varid'>maxPerm'</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-layout'>(</span><span class='hs-varid'>lc</span><span class='hs-layout'>,</span><span class='hs-varid'>rc</span><span class='hs-layout'>)</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-varid'>rc</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>maxPerm'</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>lc</span><span class='hs-varop'>+</span><span class='hs-varid'>rc</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-varid'>maxPerm'</span> <span class='hs-varid'>r</span>
<a name="line-789"></a>
<a name="line-790"></a>
<a name="line-791"></a><a name="leftLeafComposition"></a><span class='hs-comment'>-- The composition of [1..n] obtained by treating each left-facing leaf as a cut</span>
<a name="line-792"></a><span class='hs-comment'>-- Specifically, we visit the nodes in infix order, cutting after a node if it does not have an E as its right child</span>
<a name="line-793"></a><span class='hs-comment'>-- This is the map called L in Loday.pdf</span>
<a name="line-794"></a><span class='hs-definition'>leftLeafComposition</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-795"></a><span class='hs-definition'>leftLeafComposition</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>cuts</span> <span class='hs-varop'>$</span> <span class='hs-varid'>tail</span> <span class='hs-varop'>$</span> <span class='hs-varid'>leftLeafs</span> <span class='hs-varid'>t</span>
<a name="line-796"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>leftLeafs</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>leftLeafs</span> <span class='hs-varid'>l</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>False</span><span class='hs-keyglyph'>]</span>
<a name="line-797"></a> <span class='hs-varid'>leftLeafs</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>leftLeafs</span> <span class='hs-varid'>l</span> <span class='hs-varop'>++</span> <span class='hs-varid'>leftLeafs</span> <span class='hs-varid'>r</span>
<a name="line-798"></a> <span class='hs-varid'>leftLeafs</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>True</span><span class='hs-keyglyph'>]</span>
<a name="line-799"></a> <span class='hs-varid'>cuts</span> <span class='hs-varid'>bs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>break</span> <span class='hs-varid'>id</span> <span class='hs-varid'>bs</span> <span class='hs-keyword'>of</span>
<a name="line-800"></a> <span class='hs-layout'>(</span><span class='hs-varid'>ls</span><span class='hs-layout'>,</span><span class='hs-varid'>r</span><span class='hs-conop'>:</span><span class='hs-varid'>rs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>ls</span> <span class='hs-varop'>+</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-conop'>:</span> <span class='hs-varid'>cuts</span> <span class='hs-varid'>rs</span>
<a name="line-801"></a> <span class='hs-layout'>(</span><span class='hs-varid'>ls</span><span class='hs-layout'>,</span><span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>length</span> <span class='hs-varid'>ls</span><span class='hs-keyglyph'>]</span>
<a name="line-802"></a>
<a name="line-803"></a><a name="leftLeafComposition'"></a><span class='hs-definition'>leftLeafComposition'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>QSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>leftLeafComposition</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-804"></a>
<a name="line-805"></a><a name="leftLeafCompositionMap"></a><span class='hs-comment'>-- |A Hopf algebra morphism from YSymF to QSymF</span>
<a name="line-806"></a><span class='hs-definition'>leftLeafCompositionMap</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymF</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span>
<a name="line-807"></a><span class='hs-definition'>leftLeafCompositionMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nf</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fmap</span> <span class='hs-varid'>leftLeafComposition'</span>
<a name="line-808"></a>
<a name="line-809"></a>
<a name="line-810"></a><a name="descents"></a><span class='hs-comment'>-- The descent set of a permutation is [i | x_i > x_i+1], where we start the indexing from 1</span>
<a name="line-811"></a><span class='hs-definition'>descents</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-812"></a><span class='hs-definition'>descents</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>elemIndices</span> <span class='hs-conid'>True</span> <span class='hs-varop'>$</span> <span class='hs-varid'>zipWith</span> <span class='hs-layout'>(</span><span class='hs-varop'>></span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-layout'>(</span><span class='hs-varid'>tail</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-813"></a>
<a name="line-814"></a><a name="descentComposition"></a><span class='hs-comment'>-- The composition of [1..n] obtained by treating each descent as a cut</span>
<a name="line-815"></a><span class='hs-definition'>descentComposition</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-816"></a><span class='hs-definition'>descentComposition</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>descComp</span> <span class='hs-num'>0</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>where</span>
<a name="line-817"></a> <span class='hs-varid'>descComp</span> <span class='hs-varid'>c</span> <span class='hs-layout'>(</span><span class='hs-varid'>x1</span><span class='hs-conop'>:</span><span class='hs-varid'>x2</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>x1</span> <span class='hs-varop'><</span> <span class='hs-varid'>x2</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>descComp</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>x2</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyword'>else</span> <span class='hs-layout'>(</span><span class='hs-varid'>c</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-conop'>:</span> <span class='hs-varid'>descComp</span> <span class='hs-num'>0</span> <span class='hs-layout'>(</span><span class='hs-varid'>x2</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-818"></a> <span class='hs-varid'>descComp</span> <span class='hs-varid'>c</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>x</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>c</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-keyglyph'>]</span>
<a name="line-819"></a>
<a name="line-820"></a><a name="descentMap"></a><span class='hs-comment'>-- |Given a permutation of [1..n], its descents are those positions where the next number is less than the previous number.</span>
<a name="line-821"></a><span class='hs-comment'>-- For example, the permutation [2,3,5,1,6,4] has descents from 5 to 1 and from 6 to 4. The descents can be regarded as cutting</span>
<a name="line-822"></a><span class='hs-comment'>-- the permutation sequence into segments - 235-16-4 - and by counting the lengths of the segments, we get a composition 3+2+1.</span>
<a name="line-823"></a><span class='hs-comment'>-- This map between bases SSymF -> QSymF turns out to induce a morphism of Hopf algebras.</span>
<a name="line-824"></a><span class='hs-definition'>descentMap</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymF</span>
<a name="line-825"></a><span class='hs-definition'>descentMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nf</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>QSymF</span> <span class='hs-layout'>(</span><span class='hs-varid'>descentComposition</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-826"></a><span class='hs-comment'>-- descentMap == leftLeafCompositionMap . descendingTreeMap</span>
<a name="line-827"></a>
<a name="line-828"></a><a name="underComposition"></a><span class='hs-definition'>underComposition</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymF</span> <span class='hs-varid'>ps</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr</span> <span class='hs-varid'>under</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>p</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>ps</span><span class='hs-keyglyph'>]</span>
<a name="line-829"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>under</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-varid'>ys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ys</span>
<a name="line-830"></a> <span class='hs-varid'>zs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>+</span><span class='hs-varid'>q</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span> <span class='hs-varop'>++</span> <span class='hs-varid'>ys</span> <span class='hs-comment'>-- so it has a global descent at the split</span>
<a name="line-831"></a> <span class='hs-keyword'>in</span> <span class='hs-conid'>SSymF</span> <span class='hs-varid'>zs</span>
<a name="line-832"></a><span class='hs-comment'>-- This is a poset morphism (indeed, it forms a Galois connection with descentComposition)</span>
<a name="line-833"></a><span class='hs-comment'>-- but it does not extend to a Hopf algebra morphism.</span>
<a name="line-834"></a><span class='hs-comment'>-- (It does extend to a coalgebra morphism.)</span>
<a name="line-835"></a><span class='hs-comment'>-- (It is picking the maximum permutation having a given descent composition,</span>
<a name="line-836"></a><span class='hs-comment'>-- so there's an element of arbitrariness to it.)</span>
<a name="line-837"></a><span class='hs-comment'>-- This is the map called Z (Zeta?) in Loday.pdf</span>
<a name="line-838"></a>
<a name="line-839"></a><span class='hs-comment'>{-
<a name="line-840"></a>-- This is O(n^2), whereas an O(n) implementation should be possible
<a name="line-841"></a>-- Also, we would really like the associated composition (obtained by treating each global descent as a cut)?
<a name="line-842"></a>globalDescents xs = globalDescents' 0 [] xs
<a name="line-843"></a> where globalDescents' i ls (r:rs) = (if minimum (infinity:ls) > maximum (0:r:rs) then [i] else [])
<a name="line-844"></a> ++ globalDescents' (i+1) (r:ls) rs
<a name="line-845"></a> globalDescents' n _ [] = [n]
<a name="line-846"></a> infinity = maxBound :: Int
<a name="line-847"></a>-- The idea is that this leads to a map from SSymM to QSymM
<a name="line-848"></a>
<a name="line-849"></a>globalDescentComposition [] = []
<a name="line-850"></a>globalDescentComposition (x:xs) = globalDescents' 1 x xs
<a name="line-851"></a> where globalDescents' i minl (r:rs) = if minl > maximum (r:rs)
<a name="line-852"></a> then i : globalDescents' 1 r rs
<a name="line-853"></a> else globalDescents' (i+1) r rs
<a name="line-854"></a> globalDescents' i _ [] = [i]
<a name="line-855"></a>
<a name="line-856"></a>globalDescentMap :: (Eq k, Num k) => Vect k SSymM -> Vect k QSymM
<a name="line-857"></a>globalDescentMap = nf . fmap (\(SSymM xs) -> QSymM (globalDescentComposition xs))
<a name="line-858"></a>-}</span>
<a name="line-859"></a>
<a name="line-860"></a><a name="under"></a><span class='hs-comment'>-- A multiplication operation on trees</span>
<a name="line-861"></a><span class='hs-comment'>-- (Connected with their being cofree)</span>
<a name="line-862"></a><span class='hs-comment'>-- (intended to be used as infix)</span>
<a name="line-863"></a><span class='hs-definition'>under</span> <span class='hs-conid'>E</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t</span>
<a name="line-864"></a><span class='hs-definition'>under</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-layout'>(</span><span class='hs-varid'>under</span> <span class='hs-varid'>r</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-865"></a>
<a name="line-866"></a><a name="isUnderIrreducible"></a><span class='hs-definition'>isUnderIrreducible</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-conid'>E</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-867"></a><span class='hs-definition'>isUnderIrreducible</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-868"></a>
<a name="line-869"></a><a name="underDecomposition"></a><span class='hs-definition'>underDecomposition</span> <span class='hs-layout'>(</span><span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>T</span> <span class='hs-varid'>l</span> <span class='hs-varid'>x</span> <span class='hs-conid'>E</span> <span class='hs-conop'>:</span> <span class='hs-varid'>underDecomposition</span> <span class='hs-varid'>r</span>
<a name="line-870"></a><span class='hs-definition'>underDecomposition</span> <span class='hs-conid'>E</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-871"></a>
<a name="line-872"></a>
<a name="line-873"></a><a name="ysymmToSh"></a><span class='hs-comment'>-- GHC7.4.1 doesn't like the following type signature - a bug.</span>
<a name="line-874"></a><span class='hs-comment'>-- ysymmToSh :: (Eq k, Num k) => Vect k (YSymM) => Vect k (Shuffle (PBT ()))</span>
<a name="line-875"></a><span class='hs-definition'>ysymmToSh</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-varid'>ysymmToSh'</span>
<a name="line-876"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>ysymmToSh'</span> <span class='hs-layout'>(</span><span class='hs-conid'>YSymM</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sh</span> <span class='hs-layout'>(</span><span class='hs-varid'>underDecomposition</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-877"></a><span class='hs-comment'>-- This is a coalgebra morphism (but not an algebra morphism)</span>
<a name="line-878"></a><span class='hs-comment'>-- It shows that YSym is co-free</span>
<a name="line-879"></a><span class='hs-comment'>{-
<a name="line-880"></a>-- This one not working yet - perhaps it needs an nf, or to go via S/YSymF, or ...
<a name="line-881"></a>ssymmToSh = nf . fmap ssymmToSh'
<a name="line-882"></a> where ssymmToSh' (SSymM xs) = (Sh . underDecomposition . shape . descendingTree) xs
<a name="line-883"></a>-}</span>
<a name="line-884"></a>
<a name="line-885"></a><a name="symToQSymM"></a><span class='hs-comment'>-- |The injection of Sym into QSym (defined over the monomial basis)</span>
<a name="line-886"></a><span class='hs-definition'>symToQSymM</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymM</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>QSymM</span>
<a name="line-887"></a><span class='hs-definition'>symToQSymM</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>symToQSymM'</span> <span class='hs-keyword'>where</span>
<a name="line-888"></a> <span class='hs-varid'>symToQSymM'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymM</span> <span class='hs-varid'>ps</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>sumv</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>QSymM</span> <span class='hs-varid'>c</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>c</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>compositionsFromPartition</span> <span class='hs-varid'>ps</span><span class='hs-keyglyph'>]</span>
<a name="line-889"></a>
<a name="line-890"></a><a name="nsymToSymH"></a><span class='hs-comment'>-- We could equally well send NSym -> SymE, since the algebra and coalgebra definitions for SymE and SymH are exactly analogous.</span>
<a name="line-891"></a><span class='hs-comment'>-- However, NSym -> SymH is more natural, since it is consistent with the duality pairings below.</span>
<a name="line-892"></a><span class='hs-comment'>-- eg Hazewinkel 238ff</span>
<a name="line-893"></a><span class='hs-comment'>-- (Why do SymE and SymH have the same definitions? They're not dual bases. It's because of the Wronski relations.)</span>
<a name="line-894"></a><span class='hs-comment'>-- |A surjection of NSym onto Sym (defined over the complete basis)</span>
<a name="line-895"></a><span class='hs-definition'>nsymToSymH</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Vect</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span>
<a name="line-896"></a><span class='hs-definition'>nsymToSymH</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>nsymToSym'</span> <span class='hs-keyword'>where</span>
<a name="line-897"></a> <span class='hs-varid'>nsymToSym'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varop'>$</span> <span class='hs-varid'>sortDesc</span> <span class='hs-varid'>zs</span><span class='hs-layout'>)</span>
<a name="line-898"></a>
<a name="line-899"></a><a name="nsymToSSym"></a><span class='hs-comment'>-- The Hopf algebra morphism NSym -> Sym factors through NSym -> SSym -> YSym -> Sym (contained in QSym)</span>
<a name="line-900"></a><span class='hs-comment'>-- (?? This map NSym -> SSym is the dual of the descent map SSym -> QSym ??)</span>
<a name="line-901"></a><span class='hs-comment'>-- (Loday.pdf, p30)</span>
<a name="line-902"></a><span class='hs-comment'>-- (See also Hazewinkel p267-9)</span>
<a name="line-903"></a><span class='hs-definition'>nsymToSSym</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>nsymToSSym'</span> <span class='hs-keyword'>where</span>
<a name="line-904"></a> <span class='hs-varid'>nsymToSSym'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>product</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>SSymF</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-varid'>n</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>xs</span><span class='hs-keyglyph'>]</span>
<a name="line-905"></a>
<a name="line-906"></a>
<a name="line-907"></a><a name="instance%20HasPairing%20k%20SymH%20SymM"></a><span class='hs-comment'>-- |A duality pairing between the complete and monomial bases of Sym, showing that Sym is self-dual.</span>
<a name="line-908"></a><a name="instance%20HasPairing%20k%20SymH%20SymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HasPairing</span> <span class='hs-varid'>k</span> <span class='hs-conid'>SymH</span> <span class='hs-conid'>SymM</span> <span class='hs-keyword'>where</span>
<a name="line-909"></a> <span class='hs-varid'>pairing</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>pairing'</span> <span class='hs-keyword'>where</span>
<a name="line-910"></a> <span class='hs-varid'>pairing'</span> <span class='hs-layout'>(</span><span class='hs-conid'>SymH</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>,</span> <span class='hs-conid'>SymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>delta</span> <span class='hs-varid'>alpha</span> <span class='hs-varid'>beta</span> <span class='hs-comment'>-- Kronecker delta</span>
<a name="line-911"></a><span class='hs-comment'>-- Hazewinkel p178</span>
<a name="line-912"></a><span class='hs-comment'>-- Actually to show duality you would need to show that the map SymH -> SymM*, v -> <v,.> is onto</span>
<a name="line-913"></a>
<a name="line-914"></a><a name="instance%20HasPairing%20k%20NSym%20QSymM"></a><span class='hs-comment'>-- |A duality pairing between NSym and QSymM (monomial basis), showing that NSym and QSym are dual.</span>
<a name="line-915"></a><a name="instance%20HasPairing%20k%20NSym%20QSymM"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span> <span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>HasPairing</span> <span class='hs-varid'>k</span> <span class='hs-conid'>NSym</span> <span class='hs-conid'>QSymM</span> <span class='hs-keyword'>where</span>
<a name="line-916"></a> <span class='hs-varid'>pairing</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>linear</span> <span class='hs-varid'>pairing'</span> <span class='hs-keyword'>where</span>
<a name="line-917"></a> <span class='hs-varid'>pairing'</span> <span class='hs-layout'>(</span><span class='hs-conid'>NSym</span> <span class='hs-varid'>alpha</span><span class='hs-layout'>,</span> <span class='hs-conid'>QSymM</span> <span class='hs-varid'>beta</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>delta</span> <span class='hs-varid'>alpha</span> <span class='hs-varid'>beta</span> <span class='hs-comment'>-- Kronecker delta</span>
<a name="line-918"></a><span class='hs-comment'>-- Hazewinkel p236-7</span>
<a name="line-919"></a><span class='hs-comment'>-- Actually to show duality you would need to show that the map NSym -> QSymM*, v -> <v,.> is onto</span>
<a name="line-920"></a>
</pre></body>
</html>
|