/usr/share/doc/libghc-bifunctors-doc/html/src/Data-Bifunctor-TH.html is in libghc-bifunctors-doc 5.4.1-2.
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 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | <?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>src/Data/Bifunctor/TH.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE CPP #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-# LANGUAGE PatternGuards #-}</span>
<a name="line-3"></a><span class='hs-comment'>{-# LANGUAGE BangPatterns #-}</span>
<a name="line-4"></a>
<a name="line-5"></a><span class='hs-cpp'>#if __GLASGOW_HASKELL__ >= 704</span>
<a name="line-6"></a><span class='hs-comment'>{-# LANGUAGE Unsafe #-}</span>
<a name="line-7"></a><span class='hs-cpp'>#endif</span>
<a name="line-8"></a>
<a name="line-9"></a><span class='hs-cpp'>#ifndef MIN_VERSION_template_haskell</span>
<a name="line-10"></a><span class='hs-cpp'>#define MIN_VERSION_template_haskell(x,y,z) 1</span>
<a name="line-11"></a><span class='hs-cpp'>#endif</span>
<a name="line-12"></a><span class='hs-comment'>-----------------------------------------------------------------------------</span>
<a name="line-13"></a><span class='hs-comment'>-- |</span>
<a name="line-14"></a><span class='hs-comment'>-- Copyright : (C) 2008-2016 Edward Kmett, (C) 2015-2016 Ryan Scott</span>
<a name="line-15"></a><span class='hs-comment'>-- License : BSD-style (see the file LICENSE)</span>
<a name="line-16"></a><span class='hs-comment'>--</span>
<a name="line-17"></a><span class='hs-comment'>-- Maintainer : Edward Kmett <ekmett@gmail.com></span>
<a name="line-18"></a><span class='hs-comment'>-- Stability : provisional</span>
<a name="line-19"></a><span class='hs-comment'>-- Portability : portable</span>
<a name="line-20"></a><span class='hs-comment'>--</span>
<a name="line-21"></a><span class='hs-comment'>-- Functions to mechanically derive 'Bifunctor', 'Bifoldable',</span>
<a name="line-22"></a><span class='hs-comment'>-- or 'Bitraversable' instances, or to splice their functions directly into</span>
<a name="line-23"></a><span class='hs-comment'>-- source code. You need to enable the @TemplateHaskell@ language extension</span>
<a name="line-24"></a><span class='hs-comment'>-- in order to use this module.</span>
<a name="line-25"></a><span class='hs-comment'>----------------------------------------------------------------------------</span>
<a name="line-26"></a>
<a name="line-27"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Bifunctor</span><span class='hs-varop'>.</span><span class='hs-conid'>TH</span> <span class='hs-layout'>(</span>
<a name="line-28"></a> <span class='hs-comment'>-- * @derive@- functions</span>
<a name="line-29"></a> <span class='hs-comment'>-- $derive</span>
<a name="line-30"></a> <span class='hs-comment'>-- * @make@- functions</span>
<a name="line-31"></a> <span class='hs-comment'>-- $make</span>
<a name="line-32"></a> <span class='hs-comment'>-- * 'Bifunctor'</span>
<a name="line-33"></a> <span class='hs-varid'>deriveBifunctor</span>
<a name="line-34"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBimap</span>
<a name="line-35"></a> <span class='hs-comment'>-- * 'Bifoldable'</span>
<a name="line-36"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>deriveBifoldable</span>
<a name="line-37"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBifold</span>
<a name="line-38"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBifoldMap</span>
<a name="line-39"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBifoldr</span>
<a name="line-40"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBifoldl</span>
<a name="line-41"></a> <span class='hs-comment'>-- * 'Bitraversable'</span>
<a name="line-42"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>deriveBitraversable</span>
<a name="line-43"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBitraverse</span>
<a name="line-44"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBisequenceA</span>
<a name="line-45"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBimapM</span>
<a name="line-46"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>makeBisequence</span>
<a name="line-47"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-48"></a>
<a name="line-49"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <span class='hs-layout'>(</span><span class='hs-varid'>guard</span><span class='hs-layout'>,</span> <span class='hs-varid'>unless</span><span class='hs-layout'>,</span> <span class='hs-varid'>when</span><span class='hs-layout'>,</span> <span class='hs-varid'>zipWithM</span><span class='hs-layout'>)</span>
<a name="line-50"></a>
<a name="line-51"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Bifunctor</span><span class='hs-varop'>.</span><span class='hs-conid'>TH</span><span class='hs-varop'>.</span><span class='hs-conid'>Internal</span>
<a name="line-52"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Either</span> <span class='hs-layout'>(</span><span class='hs-varid'>rights</span><span class='hs-layout'>)</span>
<a name="line-53"></a><span class='hs-cpp'>#if MIN_VERSION_template_haskell(2,8,0) && !(MIN_VERSION_template_haskell(2,10,0))</span>
<a name="line-54"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Foldable</span> <span class='hs-layout'>(</span><span class='hs-varid'>foldr'</span><span class='hs-layout'>)</span>
<a name="line-55"></a><span class='hs-cpp'>#endif</span>
<a name="line-56"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span>
<a name="line-57"></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'>Map</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>Map</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromList</span><span class='hs-layout'>,</span> <span class='hs-varid'>keys</span><span class='hs-layout'>,</span> <span class='hs-varid'>lookup</span><span class='hs-layout'>,</span> <span class='hs-varid'>size</span><span class='hs-layout'>)</span>
<a name="line-58"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Maybe</span>
<a name="line-59"></a>
<a name="line-60"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Language</span><span class='hs-varop'>.</span><span class='hs-conid'>Haskell</span><span class='hs-varop'>.</span><span class='hs-conid'>TH</span><span class='hs-varop'>.</span><span class='hs-conid'>Lib</span>
<a name="line-61"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Language</span><span class='hs-varop'>.</span><span class='hs-conid'>Haskell</span><span class='hs-varop'>.</span><span class='hs-conid'>TH</span><span class='hs-varop'>.</span><span class='hs-conid'>Ppr</span>
<a name="line-62"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Language</span><span class='hs-varop'>.</span><span class='hs-conid'>Haskell</span><span class='hs-varop'>.</span><span class='hs-conid'>TH</span><span class='hs-varop'>.</span><span class='hs-conid'>Syntax</span>
<a name="line-63"></a>
<a name="line-64"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-65"></a><span class='hs-comment'>-- User-facing API</span>
<a name="line-66"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-67"></a>
<a name="line-68"></a><span class='hs-comment'>{- $derive
<a name="line-69"></a>
<a name="line-70"></a>'deriveBifunctor', 'deriveBifoldable', and 'deriveBitraversable' automatically
<a name="line-71"></a>generate their respective class instances for a given data type, newtype, or data
<a name="line-72"></a>family instance that has at least two type variable. Examples:
<a name="line-73"></a>
<a name="line-74"></a>@
<a name="line-75"></a>&#123;-&#35; LANGUAGE TemplateHaskell &#35;-&#125;
<a name="line-76"></a>import Data.Bifunctor.TH
<a name="line-77"></a>
<a name="line-78"></a>data Pair a b = Pair a b
<a name="line-79"></a>$('deriveBifunctor' ''Pair) -- instance Bifunctor Pair where ...
<a name="line-80"></a>
<a name="line-81"></a>data WrapLeftPair f g a b = WrapLeftPair (f a) (g a b)
<a name="line-82"></a>$('deriveBifoldable' ''WrapLeftPair)
<a name="line-83"></a>-- instance (Foldable f, Bifoldable g) => Bifoldable (WrapLeftPair f g) where ...
<a name="line-84"></a>@
<a name="line-85"></a>
<a name="line-86"></a>If you are using @template-haskell-2.7.0.0@ or later (i.e., GHC 7.4 or later),
<a name="line-87"></a>the @derive@ functions can be used data family instances (which requires the
<a name="line-88"></a>@-XTypeFamilies@ extension). To do so, pass the name of a data or newtype instance
<a name="line-89"></a>constructor (NOT a data family name!) to a @derive@ function. Note that the
<a name="line-90"></a>generated code may require the @-XFlexibleInstances@ extension. Example:
<a name="line-91"></a>
<a name="line-92"></a>@
<a name="line-93"></a>&#123;-&#35; LANGUAGE FlexibleInstances, TemplateHaskell, TypeFamilies &#35;-&#125;
<a name="line-94"></a>import Data.Bifunctor.TH
<a name="line-95"></a>
<a name="line-96"></a>class AssocClass a b c where
<a name="line-97"></a> data AssocData a b c
<a name="line-98"></a>instance AssocClass Int b c where
<a name="line-99"></a> data AssocData Int b c = AssocDataInt1 Int | AssocDataInt2 b c
<a name="line-100"></a>$('deriveBitraversable' 'AssocDataInt1) -- instance Bitraversable (AssocData Int) where ...
<a name="line-101"></a>-- Alternatively, one could use $(deriveBitraversable 'AssocDataInt2)
<a name="line-102"></a>@
<a name="line-103"></a>
<a name="line-104"></a>Note that there are some limitations:
<a name="line-105"></a>
<a name="line-106"></a>* The 'Name' argument to a @derive@ function must not be a type synonym.
<a name="line-107"></a>
<a name="line-108"></a>* With a @derive@ function, the last two type variables must both be of kind @*@.
<a name="line-109"></a> Other type variables of kind @* -> *@ are assumed to require a 'Functor',
<a name="line-110"></a> 'Foldable', or 'Traversable' constraint (depending on which @derive@ function is
<a name="line-111"></a> used), and other type variables of kind @* -> * -> *@ are assumed to require an
<a name="line-112"></a> 'Bifunctor', 'Bifoldable', or 'Bitraversable' constraint. If your data type
<a name="line-113"></a> doesn't meet these assumptions, use a @make@ function.
<a name="line-114"></a>
<a name="line-115"></a>* If using the @-XDatatypeContexts@, @-XExistentialQuantification@, or @-XGADTs@
<a name="line-116"></a> extensions, a constraint cannot mention either of the last two type variables. For
<a name="line-117"></a> example, @data Illegal2 a b where I2 :: Ord a => a -> b -> Illegal2 a b@ cannot
<a name="line-118"></a> have a derived 'Bifunctor' instance.
<a name="line-119"></a>
<a name="line-120"></a>* If either of the last two type variables is used within a constructor argument's
<a name="line-121"></a> type, it must only be used in the last two type arguments. For example,
<a name="line-122"></a> @data Legal a b = Legal (Int, Int, a, b)@ can have a derived 'Bifunctor' instance,
<a name="line-123"></a> but @data Illegal a b = Illegal (a, b, a, b)@ cannot.
<a name="line-124"></a>
<a name="line-125"></a>* Data family instances must be able to eta-reduce the last two type variables. In other
<a name="line-126"></a> words, if you have a instance of the form:
<a name="line-127"></a>
<a name="line-128"></a> @
<a name="line-129"></a> data family Family a1 ... an t1 t2
<a name="line-130"></a> data instance Family e1 ... e2 v1 v2 = ...
<a name="line-131"></a> @
<a name="line-132"></a>
<a name="line-133"></a> Then the following conditions must hold:
<a name="line-134"></a>
<a name="line-135"></a> 1. @v1@ and @v2@ must be distinct type variables.
<a name="line-136"></a> 2. Neither @v1@ not @v2@ must be mentioned in any of @e1@, ..., @e2@.
<a name="line-137"></a>
<a name="line-138"></a>-}</span>
<a name="line-139"></a>
<a name="line-140"></a><span class='hs-comment'>{- $make
<a name="line-141"></a>
<a name="line-142"></a>There may be scenarios in which you want to, say, 'bimap' over an arbitrary data type
<a name="line-143"></a>or data family instance without having to make the type an instance of 'Bifunctor'. For
<a name="line-144"></a>these cases, this module provides several functions (all prefixed with @make@-) that
<a name="line-145"></a>splice the appropriate lambda expression into your source code.
<a name="line-146"></a>
<a name="line-147"></a>This is particularly useful for creating instances for sophisticated data types. For
<a name="line-148"></a>example, 'deriveBifunctor' cannot infer the correct type context for
<a name="line-149"></a>@newtype HigherKinded f a b c = HigherKinded (f a b c)@, since @f@ is of kind
<a name="line-150"></a>@* -> * -> * -> *@. However, it is still possible to create a 'Bifunctor' instance for
<a name="line-151"></a>@HigherKinded@ without too much trouble using 'makeBimap':
<a name="line-152"></a>
<a name="line-153"></a>@
<a name="line-154"></a>&#123;-&#35; LANGUAGE FlexibleContexts, TemplateHaskell &#35;-&#125;
<a name="line-155"></a>import Data.Bifunctor
<a name="line-156"></a>import Data.Bifunctor.TH
<a name="line-157"></a>
<a name="line-158"></a>newtype HigherKinded f a b c = HigherKinded (f a b c)
<a name="line-159"></a>
<a name="line-160"></a>instance Bifunctor (f a) => Bifunctor (HigherKinded f a) where
<a name="line-161"></a> bimap = $(makeBimap ''HigherKinded)
<a name="line-162"></a>@
<a name="line-163"></a>
<a name="line-164"></a>-}</span>
<a name="line-165"></a>
<a name="line-166"></a><a name="deriveBifunctor"></a><span class='hs-comment'>-- | Generates a 'Bifunctor' instance declaration for the given data type or data</span>
<a name="line-167"></a><span class='hs-comment'>-- family instance.</span>
<a name="line-168"></a><span class='hs-definition'>deriveBifunctor</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-169"></a><span class='hs-definition'>deriveBifunctor</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>deriveBiClass</span> <span class='hs-conid'>Bifunctor</span>
<a name="line-170"></a>
<a name="line-171"></a><a name="makeBimap"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bimap' (without requiring a</span>
<a name="line-172"></a><span class='hs-comment'>-- 'Bifunctor' instance).</span>
<a name="line-173"></a><span class='hs-definition'>makeBimap</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-174"></a><span class='hs-definition'>makeBimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>makeBiFun</span> <span class='hs-conid'>Bimap</span>
<a name="line-175"></a>
<a name="line-176"></a><a name="deriveBifoldable"></a><span class='hs-comment'>-- | Generates a 'Bifoldable' instance declaration for the given data type or data</span>
<a name="line-177"></a><span class='hs-comment'>-- family instance.</span>
<a name="line-178"></a><span class='hs-definition'>deriveBifoldable</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-179"></a><span class='hs-definition'>deriveBifoldable</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>deriveBiClass</span> <span class='hs-conid'>Bifoldable</span>
<a name="line-180"></a>
<a name="line-181"></a><a name="makeBifold"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bifold' (without requiring a</span>
<a name="line-182"></a><span class='hs-comment'>-- 'Bifoldable' instance).</span>
<a name="line-183"></a><span class='hs-definition'>makeBifold</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-184"></a><span class='hs-definition'>makeBifold</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>makeBifoldMap</span> <span class='hs-varid'>name</span>
<a name="line-185"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-186"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-187"></a> <span class='hs-keyglyph'>]</span>
<a name="line-188"></a>
<a name="line-189"></a><a name="makeBifoldMap"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bifoldMap' (without requiring a</span>
<a name="line-190"></a><span class='hs-comment'>-- 'Bifoldable' instance).</span>
<a name="line-191"></a><span class='hs-definition'>makeBifoldMap</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-192"></a><span class='hs-definition'>makeBifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>makeBiFun</span> <span class='hs-conid'>BifoldMap</span>
<a name="line-193"></a>
<a name="line-194"></a><a name="makeBifoldr"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bifoldr' (without requiring a</span>
<a name="line-195"></a><span class='hs-comment'>-- 'Bifoldable' instance).</span>
<a name="line-196"></a><span class='hs-definition'>makeBifoldr</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-197"></a><span class='hs-definition'>makeBifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>makeBiFun</span> <span class='hs-conid'>Bifoldr</span>
<a name="line-198"></a>
<a name="line-199"></a><a name="makeBifoldl"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bifoldl' (without requiring a</span>
<a name="line-200"></a><span class='hs-comment'>-- 'Bifoldable' instance).</span>
<a name="line-201"></a><span class='hs-definition'>makeBifoldl</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-202"></a><span class='hs-definition'>makeBifoldl</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-203"></a> <span class='hs-varid'>f</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"f"</span>
<a name="line-204"></a> <span class='hs-varid'>g</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"g"</span>
<a name="line-205"></a> <span class='hs-varid'>z</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"z"</span>
<a name="line-206"></a> <span class='hs-varid'>t</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"t"</span>
<a name="line-207"></a> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>varP</span> <span class='hs-varid'>f</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>g</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>z</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span>
<a name="line-208"></a> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>appEndoValName</span>
<a name="line-209"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>getDualValName</span>
<a name="line-210"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>makeBifoldMap</span> <span class='hs-varid'>name</span><span class='hs-layout'>,</span> <span class='hs-varid'>foldFun</span> <span class='hs-varid'>f</span><span class='hs-layout'>,</span> <span class='hs-varid'>foldFun</span> <span class='hs-varid'>g</span><span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>t</span><span class='hs-keyglyph'>]</span>
<a name="line-211"></a> <span class='hs-keyglyph'>]</span>
<a name="line-212"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>z</span>
<a name="line-213"></a> <span class='hs-keyglyph'>]</span>
<a name="line-214"></a> <span class='hs-keyword'>where</span>
<a name="line-215"></a> <span class='hs-varid'>foldFun</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-216"></a> <span class='hs-varid'>foldFun</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>infixApp</span> <span class='hs-layout'>(</span><span class='hs-varid'>conE</span> <span class='hs-varid'>dualDataName</span><span class='hs-layout'>)</span>
<a name="line-217"></a> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>composeValName</span><span class='hs-layout'>)</span>
<a name="line-218"></a> <span class='hs-layout'>(</span><span class='hs-varid'>infixApp</span> <span class='hs-layout'>(</span><span class='hs-varid'>conE</span> <span class='hs-varid'>endoDataName</span><span class='hs-layout'>)</span>
<a name="line-219"></a> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>composeValName</span><span class='hs-layout'>)</span>
<a name="line-220"></a> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>flipValName</span> <span class='hs-varop'>`appE`</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span>
<a name="line-221"></a> <span class='hs-layout'>)</span>
<a name="line-222"></a>
<a name="line-223"></a><a name="deriveBitraversable"></a><span class='hs-comment'>-- | Generates a 'Bitraversable' instance declaration for the given data type or data</span>
<a name="line-224"></a><span class='hs-comment'>-- family instance.</span>
<a name="line-225"></a><span class='hs-definition'>deriveBitraversable</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-226"></a><span class='hs-definition'>deriveBitraversable</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>deriveBiClass</span> <span class='hs-conid'>Bitraversable</span>
<a name="line-227"></a>
<a name="line-228"></a><a name="makeBitraverse"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bitraverse' (without requiring a</span>
<a name="line-229"></a><span class='hs-comment'>-- 'Bitraversable' instance).</span>
<a name="line-230"></a><span class='hs-definition'>makeBitraverse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-231"></a><span class='hs-definition'>makeBitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>makeBiFun</span> <span class='hs-conid'>Bitraverse</span>
<a name="line-232"></a>
<a name="line-233"></a><a name="makeBisequenceA"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bisequenceA' (without requiring a</span>
<a name="line-234"></a><span class='hs-comment'>-- 'Bitraversable' instance).</span>
<a name="line-235"></a><span class='hs-definition'>makeBisequenceA</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-236"></a><span class='hs-definition'>makeBisequenceA</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>makeBitraverse</span> <span class='hs-varid'>name</span>
<a name="line-237"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-238"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-239"></a> <span class='hs-keyglyph'>]</span>
<a name="line-240"></a>
<a name="line-241"></a><a name="makeBimapM"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bimapM' (without requiring a</span>
<a name="line-242"></a><span class='hs-comment'>-- 'Bitraversable' instance).</span>
<a name="line-243"></a><span class='hs-definition'>makeBimapM</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-244"></a><span class='hs-definition'>makeBimapM</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-245"></a> <span class='hs-varid'>f</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"f"</span>
<a name="line-246"></a> <span class='hs-varid'>g</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"g"</span>
<a name="line-247"></a> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>varP</span> <span class='hs-varid'>f</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>g</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>.</span> <span class='hs-varid'>infixApp</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>unwrapMonadValName</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>composeValName</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-248"></a> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>makeBitraverse</span> <span class='hs-varid'>name</span><span class='hs-layout'>,</span> <span class='hs-varid'>wrapMonadExp</span> <span class='hs-varid'>f</span><span class='hs-layout'>,</span> <span class='hs-varid'>wrapMonadExp</span> <span class='hs-varid'>g</span><span class='hs-keyglyph'>]</span>
<a name="line-249"></a> <span class='hs-keyword'>where</span>
<a name="line-250"></a> <span class='hs-varid'>wrapMonadExp</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-251"></a> <span class='hs-varid'>wrapMonadExp</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>infixApp</span> <span class='hs-layout'>(</span><span class='hs-varid'>conE</span> <span class='hs-varid'>wrapMonadDataName</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>composeValName</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span>
<a name="line-252"></a>
<a name="line-253"></a><a name="makeBisequence"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like 'bisequence' (without requiring a</span>
<a name="line-254"></a><span class='hs-comment'>-- 'Bitraversable' instance).</span>
<a name="line-255"></a><span class='hs-definition'>makeBisequence</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-256"></a><span class='hs-definition'>makeBisequence</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>makeBimapM</span> <span class='hs-varid'>name</span>
<a name="line-257"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-258"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>idValName</span>
<a name="line-259"></a> <span class='hs-keyglyph'>]</span>
<a name="line-260"></a>
<a name="line-261"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-262"></a><span class='hs-comment'>-- Code generation</span>
<a name="line-263"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-264"></a>
<a name="line-265"></a><a name="deriveBiClass"></a><span class='hs-comment'>-- | Derive a class instance declaration (depending on the BiClass argument's value).</span>
<a name="line-266"></a><span class='hs-definition'>deriveBiClass</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-267"></a><span class='hs-definition'>deriveBiClass</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>withType</span> <span class='hs-varid'>name</span> <span class='hs-varid'>fromCons</span> <span class='hs-keyword'>where</span>
<a name="line-268"></a> <span class='hs-varid'>fromCons</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Cxt</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>TyVarBndr</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Con</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-269"></a> <span class='hs-varid'>fromCons</span> <span class='hs-varid'>name'</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>cons</span> <span class='hs-varid'>mbTys</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-conop'>:</span><span class='hs-conid'>[]</span><span class='hs-layout'>)</span> <span class='hs-varop'>`fmap`</span> <span class='hs-keyword'>do</span>
<a name="line-270"></a> <span class='hs-layout'>(</span><span class='hs-varid'>instanceCxt</span><span class='hs-layout'>,</span> <span class='hs-varid'>instanceType</span><span class='hs-layout'>)</span>
<a name="line-271"></a> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>buildTypeInstance</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>name'</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>mbTys</span>
<a name="line-272"></a> <span class='hs-varid'>instanceD</span> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-varid'>instanceCxt</span><span class='hs-layout'>)</span>
<a name="line-273"></a> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-varid'>instanceType</span><span class='hs-layout'>)</span>
<a name="line-274"></a> <span class='hs-layout'>(</span><span class='hs-varid'>biFunDecs</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>cons</span><span class='hs-layout'>)</span>
<a name="line-275"></a>
<a name="line-276"></a><a name="biFunDecs"></a><span class='hs-comment'>-- | Generates a declaration defining the primary function(s) corresponding to a</span>
<a name="line-277"></a><span class='hs-comment'>-- particular class (bimap for Bifunctor, bifoldr and bifoldMap for Bifoldable, and</span>
<a name="line-278"></a><span class='hs-comment'>-- bitraverse for Bitraversable).</span>
<a name="line-279"></a><span class='hs-comment'>--</span>
<a name="line-280"></a><span class='hs-comment'>-- For why both bifoldr and bifoldMap are derived for Bifoldable, see Trac #7436.</span>
<a name="line-281"></a><span class='hs-definition'>biFunDecs</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Con</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Q</span> <span class='hs-conid'>Dec</span><span class='hs-keyglyph'>]</span>
<a name="line-282"></a><span class='hs-definition'>biFunDecs</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>cons</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>makeFunD</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biClassToFuns</span> <span class='hs-varid'>biClass</span> <span class='hs-keyword'>where</span>
<a name="line-283"></a> <span class='hs-varid'>makeFunD</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Dec</span>
<a name="line-284"></a> <span class='hs-varid'>makeFunD</span> <span class='hs-varid'>biFun</span> <span class='hs-keyglyph'>=</span>
<a name="line-285"></a> <span class='hs-varid'>funD</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFunName</span> <span class='hs-varid'>biFun</span><span class='hs-layout'>)</span>
<a name="line-286"></a> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>clause</span> <span class='hs-conid'>[]</span>
<a name="line-287"></a> <span class='hs-layout'>(</span><span class='hs-varid'>normalB</span> <span class='hs-varop'>$</span> <span class='hs-varid'>makeBiFunForCons</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>cons</span><span class='hs-layout'>)</span>
<a name="line-288"></a> <span class='hs-conid'>[]</span>
<a name="line-289"></a> <span class='hs-keyglyph'>]</span>
<a name="line-290"></a>
<a name="line-291"></a><a name="makeBiFun"></a><span class='hs-comment'>-- | Generates a lambda expression which behaves like the BiFun argument.</span>
<a name="line-292"></a><span class='hs-definition'>makeBiFun</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-293"></a><span class='hs-definition'>makeBiFun</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>withType</span> <span class='hs-varid'>name</span> <span class='hs-varid'>fromCons</span> <span class='hs-keyword'>where</span>
<a name="line-294"></a> <span class='hs-varid'>fromCons</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Cxt</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>TyVarBndr</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Con</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-295"></a> <span class='hs-varid'>fromCons</span> <span class='hs-varid'>name'</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>cons</span> <span class='hs-varid'>mbTys</span> <span class='hs-keyglyph'>=</span>
<a name="line-296"></a> <span class='hs-comment'>-- We force buildTypeInstance here since it performs some checks for whether</span>
<a name="line-297"></a> <span class='hs-comment'>-- or not the provided datatype can actually have bimap/bifoldr/bitraverse/etc.</span>
<a name="line-298"></a> <span class='hs-comment'>-- implemented for it, and produces errors if it can't.</span>
<a name="line-299"></a> <span class='hs-varid'>buildTypeInstance</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFunToClass</span> <span class='hs-varid'>biFun</span><span class='hs-layout'>)</span> <span class='hs-varid'>name'</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>mbTys</span>
<a name="line-300"></a> <span class='hs-varop'>`seq`</span> <span class='hs-varid'>makeBiFunForCons</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>cons</span>
<a name="line-301"></a>
<a name="line-302"></a><a name="makeBiFunForCons"></a><span class='hs-comment'>-- | Generates a lambda expression for the given constructors.</span>
<a name="line-303"></a><span class='hs-comment'>-- All constructors must be from the same type.</span>
<a name="line-304"></a><span class='hs-definition'>makeBiFunForCons</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Con</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-305"></a><span class='hs-definition'>makeBiFunForCons</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>cons</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-306"></a> <span class='hs-varid'>argNames</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mapM</span> <span class='hs-varid'>newName</span> <span class='hs-varop'>$</span> <span class='hs-varid'>catMaybes</span> <span class='hs-keyglyph'>[</span> <span class='hs-conid'>Just</span> <span class='hs-str'>"f"</span>
<a name="line-307"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Just</span> <span class='hs-str'>"g"</span>
<a name="line-308"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>guard</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFun</span> <span class='hs-varop'>==</span> <span class='hs-conid'>Bifoldr</span><span class='hs-layout'>)</span> <span class='hs-varop'>>></span> <span class='hs-conid'>Just</span> <span class='hs-str'>"z"</span>
<a name="line-309"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Just</span> <span class='hs-str'>"value"</span>
<a name="line-310"></a> <span class='hs-keyglyph'>]</span>
<a name="line-311"></a> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>map1</span><span class='hs-layout'>,</span> <span class='hs-varid'>map2</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-varid'>others</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>splitAt</span> <span class='hs-num'>2</span> <span class='hs-varid'>argNames</span>
<a name="line-312"></a> <span class='hs-varid'>z</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>head</span> <span class='hs-varid'>others</span> <span class='hs-comment'>-- If we're deriving bifoldr, this will be well defined</span>
<a name="line-313"></a> <span class='hs-comment'>-- and useful. Otherwise, it'll be ignored.</span>
<a name="line-314"></a> <span class='hs-varid'>value</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>last</span> <span class='hs-varid'>others</span>
<a name="line-315"></a> <span class='hs-varid'>lamE</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>argNames</span><span class='hs-layout'>)</span>
<a name="line-316"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>appsE</span>
<a name="line-317"></a> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>varE</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunConstName</span> <span class='hs-varid'>biFun</span>
<a name="line-318"></a> <span class='hs-layout'>,</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>cons</span>
<a name="line-319"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>appE</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>errorValName</span><span class='hs-layout'>)</span>
<a name="line-320"></a> <span class='hs-layout'>(</span><span class='hs-varid'>stringE</span> <span class='hs-varop'>$</span> <span class='hs-str'>"Void "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>nameBase</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFunName</span> <span class='hs-varid'>biFun</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-321"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>caseE</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>value</span><span class='hs-layout'>)</span>
<a name="line-322"></a> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>makeBiFunForCon</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>z</span> <span class='hs-varid'>map1</span> <span class='hs-varid'>map2</span><span class='hs-layout'>)</span> <span class='hs-varid'>cons</span><span class='hs-layout'>)</span>
<a name="line-323"></a> <span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-varid'>map</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>argNames</span>
<a name="line-324"></a>
<a name="line-325"></a><a name="makeBiFunForCon"></a><span class='hs-comment'>-- | Generates a lambda expression for a single constructor.</span>
<a name="line-326"></a><span class='hs-definition'>makeBiFunForCon</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Con</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Match</span>
<a name="line-327"></a><span class='hs-definition'>makeBiFunForCon</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>z</span> <span class='hs-varid'>map1</span> <span class='hs-varid'>map2</span> <span class='hs-varid'>con</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-328"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>conName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>constructorName</span> <span class='hs-varid'>con</span>
<a name="line-329"></a> <span class='hs-layout'>(</span><span class='hs-varid'>ts</span><span class='hs-layout'>,</span> <span class='hs-varid'>tvMap</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>reifyConTys</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>map1</span> <span class='hs-varid'>map2</span>
<a name="line-330"></a> <span class='hs-varid'>argNames</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newNameList</span> <span class='hs-str'>"_arg"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>length</span> <span class='hs-varid'>ts</span>
<a name="line-331"></a> <span class='hs-varid'>makeBiFunForArgs</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>z</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>ts</span> <span class='hs-varid'>argNames</span>
<a name="line-332"></a>
<a name="line-333"></a><a name="makeBiFunForArgs"></a><span class='hs-comment'>-- | Generates a lambda expression for a single constructor's arguments.</span>
<a name="line-334"></a><span class='hs-definition'>makeBiFunForArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span>
<a name="line-335"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-336"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>TyVarMap</span>
<a name="line-337"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-338"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-339"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-340"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Match</span>
<a name="line-341"></a><span class='hs-definition'>makeBiFunForArgs</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>z</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>tys</span> <span class='hs-varid'>args</span> <span class='hs-keyglyph'>=</span>
<a name="line-342"></a> <span class='hs-varid'>match</span> <span class='hs-layout'>(</span><span class='hs-varid'>conP</span> <span class='hs-varid'>conName</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>args</span><span class='hs-layout'>)</span>
<a name="line-343"></a> <span class='hs-layout'>(</span><span class='hs-varid'>normalB</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunCombine</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>z</span> <span class='hs-varid'>args</span> <span class='hs-varid'>mappedArgs</span><span class='hs-layout'>)</span>
<a name="line-344"></a> <span class='hs-conid'>[]</span>
<a name="line-345"></a> <span class='hs-keyword'>where</span>
<a name="line-346"></a> <span class='hs-varid'>mappedArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-347"></a> <span class='hs-varid'>mappedArgs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zipWithM</span> <span class='hs-layout'>(</span><span class='hs-varid'>makeBiFunForArg</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span> <span class='hs-varid'>tys</span> <span class='hs-varid'>args</span>
<a name="line-348"></a>
<a name="line-349"></a><a name="makeBiFunForArg"></a><span class='hs-comment'>-- | Generates a lambda expression for a single argument of a constructor.</span>
<a name="line-350"></a><span class='hs-comment'>-- The returned value is 'Right' if its type mentions one of the last two type</span>
<a name="line-351"></a><span class='hs-comment'>-- parameters. Otherwise, it is 'Left'.</span>
<a name="line-352"></a><span class='hs-definition'>makeBiFunForArg</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span>
<a name="line-353"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>TyVarMap</span>
<a name="line-354"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-355"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span>
<a name="line-356"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-357"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-layout'>)</span>
<a name="line-358"></a><span class='hs-definition'>makeBiFunForArg</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>ty</span> <span class='hs-varid'>tyExpName</span> <span class='hs-keyglyph'>=</span>
<a name="line-359"></a> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-conid'>True</span> <span class='hs-varid'>ty</span> <span class='hs-varop'>`appEitherE`</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>tyExpName</span>
<a name="line-360"></a>
<a name="line-361"></a><a name="makeBiFunForType"></a><span class='hs-comment'>-- | Generates a lambda expression for a specific type. The returned value is</span>
<a name="line-362"></a><span class='hs-comment'>-- 'Right' if its type mentions one of the last two type parameters. Otherwise,</span>
<a name="line-363"></a><span class='hs-comment'>-- it is 'Left'.</span>
<a name="line-364"></a><span class='hs-definition'>makeBiFunForType</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span>
<a name="line-365"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>TyVarMap</span>
<a name="line-366"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-367"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-368"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span>
<a name="line-369"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-layout'>)</span>
<a name="line-370"></a><span class='hs-definition'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-layout'>(</span><span class='hs-conid'>VarT</span> <span class='hs-varid'>tyName</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-371"></a> <span class='hs-keyword'>case</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>tyName</span> <span class='hs-varid'>tvMap</span> <span class='hs-keyword'>of</span>
<a name="line-372"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>mapName</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Right</span> <span class='hs-varop'>.</span> <span class='hs-varid'>varE</span> <span class='hs-varop'>$</span>
<a name="line-373"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>covariant</span>
<a name="line-374"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>mapName</span>
<a name="line-375"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>contravarianceError</span> <span class='hs-varid'>conName</span>
<a name="line-376"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Left</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunTriv</span> <span class='hs-varid'>biFun</span>
<a name="line-377"></a><span class='hs-definition'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-layout'>(</span><span class='hs-conid'>SigT</span> <span class='hs-varid'>ty</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-378"></a> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-varid'>ty</span>
<a name="line-379"></a><span class='hs-definition'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-layout'>(</span><span class='hs-conid'>ForallT</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>ty</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-380"></a> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-varid'>ty</span>
<a name="line-381"></a><span class='hs-definition'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-varid'>ty</span> <span class='hs-keyglyph'>=</span>
<a name="line-382"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>tyCon</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Type</span>
<a name="line-383"></a> <span class='hs-varid'>tyArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-384"></a> <span class='hs-varid'>tyCon</span><span class='hs-conop'>:</span><span class='hs-varid'>tyArgs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unapplyTy</span> <span class='hs-varid'>ty</span>
<a name="line-385"></a>
<a name="line-386"></a> <span class='hs-varid'>numLastArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span>
<a name="line-387"></a> <span class='hs-varid'>numLastArgs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>min</span> <span class='hs-num'>2</span> <span class='hs-varop'>$</span> <span class='hs-varid'>length</span> <span class='hs-varid'>tyArgs</span>
<a name="line-388"></a>
<a name="line-389"></a> <span class='hs-varid'>lhsArgs</span><span class='hs-layout'>,</span> <span class='hs-varid'>rhsArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-390"></a> <span class='hs-layout'>(</span><span class='hs-varid'>lhsArgs</span><span class='hs-layout'>,</span> <span class='hs-varid'>rhsArgs</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'>length</span> <span class='hs-varid'>tyArgs</span> <span class='hs-comment'>-</span> <span class='hs-varid'>numLastArgs</span><span class='hs-layout'>)</span> <span class='hs-varid'>tyArgs</span>
<a name="line-391"></a>
<a name="line-392"></a> <span class='hs-varid'>tyVarNames</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-393"></a> <span class='hs-varid'>tyVarNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>keys</span> <span class='hs-varid'>tvMap</span>
<a name="line-394"></a>
<a name="line-395"></a> <span class='hs-varid'>mentionsTyArgs</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Bool</span>
<a name="line-396"></a> <span class='hs-varid'>mentionsTyArgs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`mentionsName`</span> <span class='hs-varid'>tyVarNames</span><span class='hs-layout'>)</span> <span class='hs-varid'>tyArgs</span>
<a name="line-397"></a>
<a name="line-398"></a> <span class='hs-varid'>makeBiFunTuple</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Q</span> <span class='hs-conid'>Pat</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Pat</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span>
<a name="line-399"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-layout'>)</span>
<a name="line-400"></a> <span class='hs-varid'>makeBiFunTuple</span> <span class='hs-varid'>mkTupP</span> <span class='hs-varid'>mkTupleDataName</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-401"></a> <span class='hs-varid'>args</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mapM</span> <span class='hs-varid'>newName</span> <span class='hs-varop'>$</span> <span class='hs-varid'>catMaybes</span> <span class='hs-keyglyph'>[</span> <span class='hs-conid'>Just</span> <span class='hs-str'>"x"</span>
<a name="line-402"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>guard</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFun</span> <span class='hs-varop'>==</span> <span class='hs-conid'>Bifoldr</span><span class='hs-layout'>)</span> <span class='hs-varop'>>></span> <span class='hs-conid'>Just</span> <span class='hs-str'>"z"</span>
<a name="line-403"></a> <span class='hs-keyglyph'>]</span>
<a name="line-404"></a> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newNameList</span> <span class='hs-str'>"_tup"</span> <span class='hs-varid'>n</span>
<a name="line-405"></a>
<a name="line-406"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>head</span> <span class='hs-varid'>args</span>
<a name="line-407"></a> <span class='hs-varid'>z</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>last</span> <span class='hs-varid'>args</span>
<a name="line-408"></a> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Right</span> <span class='hs-varop'>$</span> <span class='hs-varid'>lamE</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>args</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>caseE</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span>
<a name="line-409"></a> <span class='hs-keyglyph'>[</span> <span class='hs-varid'>match</span> <span class='hs-layout'>(</span><span class='hs-varid'>mkTupP</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-410"></a> <span class='hs-layout'>(</span><span class='hs-varid'>normalB</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunCombine</span> <span class='hs-varid'>biFun</span>
<a name="line-411"></a> <span class='hs-layout'>(</span><span class='hs-varid'>mkTupleDataName</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span>
<a name="line-412"></a> <span class='hs-varid'>z</span>
<a name="line-413"></a> <span class='hs-varid'>xs</span>
<a name="line-414"></a> <span class='hs-layout'>(</span><span class='hs-varid'>zipWithM</span> <span class='hs-varid'>makeBiFunTupleField</span> <span class='hs-varid'>tyArgs</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-415"></a> <span class='hs-layout'>)</span>
<a name="line-416"></a> <span class='hs-conid'>[]</span>
<a name="line-417"></a> <span class='hs-keyglyph'>]</span>
<a name="line-418"></a>
<a name="line-419"></a> <span class='hs-varid'>makeBiFunTupleField</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-layout'>)</span>
<a name="line-420"></a> <span class='hs-varid'>makeBiFunTupleField</span> <span class='hs-varid'>fieldTy</span> <span class='hs-varid'>fieldName</span> <span class='hs-keyglyph'>=</span>
<a name="line-421"></a> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span> <span class='hs-varid'>fieldTy</span>
<a name="line-422"></a> <span class='hs-varop'>`appEitherE`</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>fieldName</span>
<a name="line-423"></a>
<a name="line-424"></a> <span class='hs-keyword'>in</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tyCon</span> <span class='hs-keyword'>of</span>
<a name="line-425"></a> <span class='hs-conid'>ArrowT</span>
<a name="line-426"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>not</span> <span class='hs-layout'>(</span><span class='hs-varid'>allowFunTys</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFunToClass</span> <span class='hs-varid'>biFun</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>noFunctionsError</span> <span class='hs-varid'>conName</span>
<a name="line-427"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>mentionsTyArgs</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>argTy</span><span class='hs-layout'>,</span> <span class='hs-varid'>resTy</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>tyArgs</span> <span class='hs-keyglyph'>-></span>
<a name="line-428"></a> <span class='hs-keyword'>do</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"x"</span>
<a name="line-429"></a> <span class='hs-varid'>b</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"b"</span>
<a name="line-430"></a> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Right</span> <span class='hs-varop'>.</span> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>varP</span> <span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>b</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span>
<a name="line-431"></a> <span class='hs-varid'>covBiFun</span> <span class='hs-varid'>covariant</span> <span class='hs-varid'>resTy</span> <span class='hs-varop'>`appE`</span> <span class='hs-layout'>(</span><span class='hs-varid'>varE</span> <span class='hs-varid'>x</span> <span class='hs-varop'>`appE`</span>
<a name="line-432"></a> <span class='hs-layout'>(</span><span class='hs-varid'>covBiFun</span> <span class='hs-layout'>(</span><span class='hs-varid'>not</span> <span class='hs-varid'>covariant</span><span class='hs-layout'>)</span> <span class='hs-varid'>argTy</span> <span class='hs-varop'>`appE`</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-433"></a> <span class='hs-keyword'>where</span>
<a name="line-434"></a> <span class='hs-varid'>covBiFun</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Bool</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-435"></a> <span class='hs-varid'>covBiFun</span> <span class='hs-varid'>cov</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-varid'>fromEither</span> <span class='hs-varop'>.</span> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>cov</span>
<a name="line-436"></a><span class='hs-cpp'>#if MIN_VERSION_template_haskell(2,6,0)</span>
<a name="line-437"></a> <span class='hs-conid'>UnboxedTupleT</span> <span class='hs-varid'>n</span>
<a name="line-438"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-varop'>></span> <span class='hs-num'>0</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>mentionsTyArgs</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>makeBiFunTuple</span> <span class='hs-varid'>unboxedTupP</span> <span class='hs-varid'>unboxedTupleDataName</span> <span class='hs-varid'>n</span>
<a name="line-439"></a><span class='hs-cpp'>#endif</span>
<a name="line-440"></a> <span class='hs-conid'>TupleT</span> <span class='hs-varid'>n</span>
<a name="line-441"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-varop'>></span> <span class='hs-num'>0</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>mentionsTyArgs</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>makeBiFunTuple</span> <span class='hs-varid'>tupP</span> <span class='hs-varid'>tupleDataName</span> <span class='hs-varid'>n</span>
<a name="line-442"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-443"></a> <span class='hs-varid'>itf</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>isTyFamily</span> <span class='hs-varid'>tyCon</span>
<a name="line-444"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`mentionsName`</span> <span class='hs-varid'>tyVarNames</span><span class='hs-layout'>)</span> <span class='hs-varid'>lhsArgs</span> <span class='hs-varop'>||</span> <span class='hs-layout'>(</span><span class='hs-varid'>itf</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>mentionsTyArgs</span><span class='hs-layout'>)</span>
<a name="line-445"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>outOfPlaceTyVarError</span> <span class='hs-varid'>conName</span>
<a name="line-446"></a> <span class='hs-keyword'>else</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`mentionsName`</span> <span class='hs-varid'>tyVarNames</span><span class='hs-layout'>)</span> <span class='hs-varid'>rhsArgs</span>
<a name="line-447"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Right</span> <span class='hs-varop'>.</span> <span class='hs-varid'>biFunApp</span> <span class='hs-varid'>biFun</span> <span class='hs-varop'>.</span> <span class='hs-varid'>appsE</span> <span class='hs-varop'>$</span>
<a name="line-448"></a> <span class='hs-layout'>(</span> <span class='hs-varid'>varE</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromJust</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunArity</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>numLastArgs</span><span class='hs-layout'>)</span>
<a name="line-449"></a> <span class='hs-conop'>:</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>fmap</span> <span class='hs-varid'>fromEither</span> <span class='hs-varop'>.</span> <span class='hs-varid'>makeBiFunForType</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>tvMap</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>covariant</span><span class='hs-layout'>)</span>
<a name="line-450"></a> <span class='hs-varid'>rhsArgs</span>
<a name="line-451"></a> <span class='hs-layout'>)</span>
<a name="line-452"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>fmap</span> <span class='hs-conid'>Left</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunTriv</span> <span class='hs-varid'>biFun</span>
<a name="line-453"></a>
<a name="line-454"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-455"></a><span class='hs-comment'>-- Template Haskell reifying and AST manipulation</span>
<a name="line-456"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-457"></a>
<a name="line-458"></a><a name="withType"></a><span class='hs-comment'>-- | Boilerplate for top level splices.</span>
<a name="line-459"></a><span class='hs-comment'>--</span>
<a name="line-460"></a><span class='hs-comment'>-- The given Name must meet one of two criteria:</span>
<a name="line-461"></a><span class='hs-comment'>--</span>
<a name="line-462"></a><span class='hs-comment'>-- 1. It must be the name of a type constructor of a plain data type or newtype.</span>
<a name="line-463"></a><span class='hs-comment'>-- 2. It must be the name of a data family instance or newtype instance constructor.</span>
<a name="line-464"></a><span class='hs-comment'>--</span>
<a name="line-465"></a><span class='hs-comment'>-- Any other value will result in an exception.</span>
<a name="line-466"></a><span class='hs-definition'>withType</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-467"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Cxt</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>TyVarBndr</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Con</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-468"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-varid'>a</span>
<a name="line-469"></a><span class='hs-definition'>withType</span> <span class='hs-varid'>name</span> <span class='hs-varid'>f</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-470"></a> <span class='hs-varid'>info</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>reify</span> <span class='hs-varid'>name</span>
<a name="line-471"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>info</span> <span class='hs-keyword'>of</span>
<a name="line-472"></a> <span class='hs-conid'>TyConI</span> <span class='hs-varid'>dec</span> <span class='hs-keyglyph'>-></span>
<a name="line-473"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>dec</span> <span class='hs-keyword'>of</span>
<a name="line-474"></a> <span class='hs-conid'>DataD</span> <span class='hs-varid'>ctxt</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tvbs</span>
<a name="line-475"></a><span class='hs-cpp'>#if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-476"></a> <span class='hs-keyword'>_</span>
<a name="line-477"></a><span class='hs-cpp'>#endif</span>
<a name="line-478"></a> <span class='hs-varid'>cons</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>f</span> <span class='hs-varid'>name</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>cons</span> <span class='hs-conid'>Nothing</span>
<a name="line-479"></a> <span class='hs-conid'>NewtypeD</span> <span class='hs-varid'>ctxt</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tvbs</span>
<a name="line-480"></a><span class='hs-cpp'>#if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-481"></a> <span class='hs-keyword'>_</span>
<a name="line-482"></a><span class='hs-cpp'>#endif</span>
<a name="line-483"></a> <span class='hs-varid'>con</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>f</span> <span class='hs-varid'>name</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>con</span><span class='hs-keyglyph'>]</span> <span class='hs-conid'>Nothing</span>
<a name="line-484"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span> <span class='hs-str'>"Unsupported type: "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>dec</span>
<a name="line-485"></a><span class='hs-cpp'>#if MIN_VERSION_template_haskell(2,7,0)</span>
<a name="line-486"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-487"></a> <span class='hs-conid'>DataConI</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>parentName</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-488"></a><span class='hs-cpp'># else</span>
<a name="line-489"></a> <span class='hs-conid'>DataConI</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>parentName</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-490"></a><span class='hs-cpp'># endif</span>
<a name="line-491"></a> <span class='hs-varid'>parentInfo</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>reify</span> <span class='hs-varid'>parentName</span>
<a name="line-492"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>parentInfo</span> <span class='hs-keyword'>of</span>
<a name="line-493"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-494"></a> <span class='hs-conid'>FamilyI</span> <span class='hs-layout'>(</span><span class='hs-conid'>DataFamilyD</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tvbs</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-varid'>decs</span> <span class='hs-keyglyph'>-></span>
<a name="line-495"></a><span class='hs-cpp'># else</span>
<a name="line-496"></a> <span class='hs-conid'>FamilyI</span> <span class='hs-layout'>(</span><span class='hs-conid'>FamilyD</span> <span class='hs-conid'>DataFam</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tvbs</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-varid'>decs</span> <span class='hs-keyglyph'>-></span>
<a name="line-497"></a><span class='hs-cpp'># endif</span>
<a name="line-498"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>instDec</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>flip</span> <span class='hs-varid'>find</span> <span class='hs-varid'>decs</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>dec</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>dec</span> <span class='hs-keyword'>of</span>
<a name="line-499"></a> <span class='hs-conid'>DataInstD</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span>
<a name="line-500"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-501"></a> <span class='hs-keyword'>_</span>
<a name="line-502"></a><span class='hs-cpp'># endif</span>
<a name="line-503"></a> <span class='hs-varid'>cons</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>name</span> <span class='hs-varop'>==</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>constructorName</span><span class='hs-layout'>)</span> <span class='hs-varid'>cons</span>
<a name="line-504"></a> <span class='hs-conid'>NewtypeInstD</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span>
<a name="line-505"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-506"></a> <span class='hs-keyword'>_</span>
<a name="line-507"></a><span class='hs-cpp'># endif</span>
<a name="line-508"></a> <span class='hs-varid'>con</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>name</span> <span class='hs-varop'>==</span> <span class='hs-varid'>constructorName</span> <span class='hs-varid'>con</span>
<a name="line-509"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span> <span class='hs-str'>"Must be a data or newtype instance."</span>
<a name="line-510"></a> <span class='hs-keyword'>in</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>instDec</span> <span class='hs-keyword'>of</span>
<a name="line-511"></a> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-conid'>DataInstD</span> <span class='hs-varid'>ctxt</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>instTys</span>
<a name="line-512"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-513"></a> <span class='hs-keyword'>_</span>
<a name="line-514"></a><span class='hs-cpp'># endif</span>
<a name="line-515"></a> <span class='hs-varid'>cons</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span>
<a name="line-516"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>f</span> <span class='hs-varid'>parentName</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>cons</span> <span class='hs-varop'>$</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>instTys</span>
<a name="line-517"></a> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-conid'>NewtypeInstD</span> <span class='hs-varid'>ctxt</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>instTys</span>
<a name="line-518"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-519"></a> <span class='hs-keyword'>_</span>
<a name="line-520"></a><span class='hs-cpp'># endif</span>
<a name="line-521"></a> <span class='hs-varid'>con</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span>
<a name="line-522"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>f</span> <span class='hs-varid'>parentName</span> <span class='hs-varid'>ctxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>con</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>instTys</span>
<a name="line-523"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span>
<a name="line-524"></a> <span class='hs-str'>"Could not find data or newtype instance constructor."</span>
<a name="line-525"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span> <span class='hs-str'>"Data constructor "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>name</span> <span class='hs-varop'>++</span>
<a name="line-526"></a> <span class='hs-str'>" is not from a data family instance constructor."</span>
<a name="line-527"></a><span class='hs-cpp'># if MIN_VERSION_template_haskell(2,11,0)</span>
<a name="line-528"></a> <span class='hs-conid'>FamilyI</span> <span class='hs-conid'>DataFamilyD</span><span class='hs-layout'>{</span><span class='hs-layout'>}</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span>
<a name="line-529"></a><span class='hs-cpp'># else</span>
<a name="line-530"></a> <span class='hs-conid'>FamilyI</span> <span class='hs-layout'>(</span><span class='hs-conid'>FamilyD</span> <span class='hs-conid'>DataFam</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span>
<a name="line-531"></a><span class='hs-cpp'># endif</span>
<a name="line-532"></a> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span>
<a name="line-533"></a> <span class='hs-str'>"Cannot use a data family name. Use a data family instance constructor instead."</span>
<a name="line-534"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span> <span class='hs-str'>"The name must be of a plain data type constructor, "</span>
<a name="line-535"></a> <span class='hs-varop'>++</span> <span class='hs-str'>"or a data family instance constructor."</span>
<a name="line-536"></a><span class='hs-cpp'>#else</span>
<a name="line-537"></a> <span class='hs-conid'>DataConI</span><span class='hs-layout'>{</span><span class='hs-layout'>}</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>dataConIError</span>
<a name="line-538"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ns</span> <span class='hs-varop'>++</span> <span class='hs-str'>"The name must be of a plain type constructor."</span>
<a name="line-539"></a><span class='hs-cpp'>#endif</span>
<a name="line-540"></a> <span class='hs-keyword'>where</span>
<a name="line-541"></a> <span class='hs-varid'>ns</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span>
<a name="line-542"></a> <span class='hs-varid'>ns</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"Data.Bifunctor.TH.withType: "</span>
<a name="line-543"></a>
<a name="line-544"></a><a name="buildTypeInstance"></a><span class='hs-comment'>-- | Deduces the instance context and head for an instance.</span>
<a name="line-545"></a><span class='hs-definition'>buildTypeInstance</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span>
<a name="line-546"></a> <span class='hs-comment'>-- ^ Bifunctor, Bifoldable, or Bitraversable</span>
<a name="line-547"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-548"></a> <span class='hs-comment'>-- ^ The type constructor or data family name</span>
<a name="line-549"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Cxt</span>
<a name="line-550"></a> <span class='hs-comment'>-- ^ The datatype context</span>
<a name="line-551"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>TyVarBndr</span><span class='hs-keyglyph'>]</span>
<a name="line-552"></a> <span class='hs-comment'>-- ^ The type variables from the data type/data family declaration</span>
<a name="line-553"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-554"></a> <span class='hs-comment'>-- ^ 'Just' the types used to instantiate a data family instance,</span>
<a name="line-555"></a> <span class='hs-comment'>-- or 'Nothing' if it's a plain data type</span>
<a name="line-556"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Cxt</span><span class='hs-layout'>,</span> <span class='hs-conid'>Type</span><span class='hs-layout'>)</span>
<a name="line-557"></a><span class='hs-comment'>-- Plain data type/newtype case</span>
<a name="line-558"></a><span class='hs-definition'>buildTypeInstance</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span> <span class='hs-varid'>dataCxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>=</span>
<a name="line-559"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>varTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-560"></a> <span class='hs-varid'>varTys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>tvbToType</span> <span class='hs-varid'>tvbs</span>
<a name="line-561"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>buildTypeInstanceFromTys</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span> <span class='hs-varid'>dataCxt</span> <span class='hs-varid'>varTys</span> <span class='hs-conid'>False</span>
<a name="line-562"></a><span class='hs-comment'>-- Data family instance case</span>
<a name="line-563"></a><span class='hs-comment'>--</span>
<a name="line-564"></a><span class='hs-comment'>-- The CPP is present to work around a couple of annoying old GHC bugs.</span>
<a name="line-565"></a><span class='hs-comment'>-- See Note [Polykinded data families in Template Haskell]</span>
<a name="line-566"></a><span class='hs-definition'>buildTypeInstance</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>parentName</span> <span class='hs-varid'>dataCxt</span> <span class='hs-varid'>tvbs</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>instTysAndKinds</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-567"></a><span class='hs-cpp'>#if !(MIN_VERSION_template_haskell(2,8,0)) || MIN_VERSION_template_haskell(2,10,0)</span>
<a name="line-568"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>instTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-569"></a> <span class='hs-varid'>instTys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zipWith</span> <span class='hs-varid'>stealKindForType</span> <span class='hs-varid'>tvbs</span> <span class='hs-varid'>instTysAndKinds</span>
<a name="line-570"></a><span class='hs-cpp'>#else</span>
<a name="line-571"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>kindVarNames</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-572"></a> <span class='hs-varid'>kindVarNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nub</span> <span class='hs-varop'>$</span> <span class='hs-varid'>concatMap</span> <span class='hs-layout'>(</span><span class='hs-varid'>tyVarNamesOfType</span> <span class='hs-varop'>.</span> <span class='hs-varid'>tvbKind</span><span class='hs-layout'>)</span> <span class='hs-varid'>tvbs</span>
<a name="line-573"></a>
<a name="line-574"></a> <span class='hs-varid'>numKindVars</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span>
<a name="line-575"></a> <span class='hs-varid'>numKindVars</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>kindVarNames</span>
<a name="line-576"></a>
<a name="line-577"></a> <span class='hs-varid'>givenKinds</span><span class='hs-layout'>,</span> <span class='hs-varid'>givenKinds'</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Kind</span><span class='hs-keyglyph'>]</span>
<a name="line-578"></a> <span class='hs-varid'>givenTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-579"></a> <span class='hs-layout'>(</span><span class='hs-varid'>givenKinds</span><span class='hs-layout'>,</span> <span class='hs-varid'>givenTys</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>splitAt</span> <span class='hs-varid'>numKindVars</span> <span class='hs-varid'>instTysAndKinds</span>
<a name="line-580"></a> <span class='hs-varid'>givenKinds'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>sanitizeStars</span> <span class='hs-varid'>givenKinds</span>
<a name="line-581"></a>
<a name="line-582"></a> <span class='hs-comment'>-- A GHC 7.6-specific bug requires us to replace all occurrences of</span>
<a name="line-583"></a> <span class='hs-comment'>-- (ConT GHC.Prim.*) with StarT, or else Template Haskell will reject it.</span>
<a name="line-584"></a> <span class='hs-comment'>-- Luckily, (ConT GHC.Prim.*) only seems to occur in this one spot.</span>
<a name="line-585"></a> <span class='hs-varid'>sanitizeStars</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Kind</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Kind</span>
<a name="line-586"></a> <span class='hs-varid'>sanitizeStars</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>go</span>
<a name="line-587"></a> <span class='hs-keyword'>where</span>
<a name="line-588"></a> <span class='hs-varid'>go</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Kind</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Kind</span>
<a name="line-589"></a> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>AppT</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>AppT</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varid'>t1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span>
<a name="line-590"></a> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>SigT</span> <span class='hs-varid'>t</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>SigT</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span>
<a name="line-591"></a> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>ConT</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-varop'>==</span> <span class='hs-varid'>starKindName</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>StarT</span>
<a name="line-592"></a> <span class='hs-varid'>go</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t</span>
<a name="line-593"></a>
<a name="line-594"></a> <span class='hs-comment'>-- If we run this code with GHC 7.8, we might have to generate extra type</span>
<a name="line-595"></a> <span class='hs-comment'>-- variables to compensate for any type variables that Template Haskell</span>
<a name="line-596"></a> <span class='hs-comment'>-- eta-reduced away.</span>
<a name="line-597"></a> <span class='hs-comment'>-- See Note [Polykinded data families in Template Haskell]</span>
<a name="line-598"></a> <span class='hs-varid'>xTypeNames</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newNameList</span> <span class='hs-str'>"tExtra"</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>tvbs</span> <span class='hs-comment'>-</span> <span class='hs-varid'>length</span> <span class='hs-varid'>givenTys</span><span class='hs-layout'>)</span>
<a name="line-599"></a>
<a name="line-600"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>xTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-601"></a> <span class='hs-varid'>xTys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-conid'>VarT</span> <span class='hs-varid'>xTypeNames</span>
<a name="line-602"></a> <span class='hs-comment'>-- ^ Because these type variables were eta-reduced away, we can only</span>
<a name="line-603"></a> <span class='hs-comment'>-- determine their kind by using stealKindForType. Therefore, we mark</span>
<a name="line-604"></a> <span class='hs-comment'>-- them as VarT to ensure they will be given an explicit kind annotation</span>
<a name="line-605"></a> <span class='hs-comment'>-- (and so the kind inference machinery has the right information).</span>
<a name="line-606"></a>
<a name="line-607"></a> <span class='hs-varid'>substNamesWithKinds</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>Name</span><span class='hs-layout'>,</span> <span class='hs-conid'>Kind</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span>
<a name="line-608"></a> <span class='hs-varid'>substNamesWithKinds</span> <span class='hs-varid'>nks</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr'</span> <span class='hs-layout'>(</span><span class='hs-varid'>uncurry</span> <span class='hs-varid'>substNameWithKind</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span> <span class='hs-varid'>nks</span>
<a name="line-609"></a>
<a name="line-610"></a> <span class='hs-comment'>-- The types from the data family instance might not have explicit kind</span>
<a name="line-611"></a> <span class='hs-comment'>-- annotations, which the kind machinery needs to work correctly. To</span>
<a name="line-612"></a> <span class='hs-comment'>-- compensate, we use stealKindForType to explicitly annotate any</span>
<a name="line-613"></a> <span class='hs-comment'>-- types without kind annotations.</span>
<a name="line-614"></a> <span class='hs-varid'>instTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-615"></a> <span class='hs-varid'>instTys</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>substNamesWithKinds</span> <span class='hs-layout'>(</span><span class='hs-varid'>zip</span> <span class='hs-varid'>kindVarNames</span> <span class='hs-varid'>givenKinds'</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-616"></a> <span class='hs-comment'>-- Note that due to a GHC 7.8-specific bug</span>
<a name="line-617"></a> <span class='hs-comment'>-- (see Note [Polykinded data families in Template Haskell]),</span>
<a name="line-618"></a> <span class='hs-comment'>-- there may be more kind variable names than there are kinds</span>
<a name="line-619"></a> <span class='hs-comment'>-- to substitute. But this is OK! If a kind is eta-reduced, it</span>
<a name="line-620"></a> <span class='hs-comment'>-- means that is was not instantiated to something more specific,</span>
<a name="line-621"></a> <span class='hs-comment'>-- so we need not substitute it. Using stealKindForType will</span>
<a name="line-622"></a> <span class='hs-comment'>-- grab the correct kind.</span>
<a name="line-623"></a> <span class='hs-varop'>$</span> <span class='hs-varid'>zipWith</span> <span class='hs-varid'>stealKindForType</span> <span class='hs-varid'>tvbs</span> <span class='hs-layout'>(</span><span class='hs-varid'>givenTys</span> <span class='hs-varop'>++</span> <span class='hs-varid'>xTys</span><span class='hs-layout'>)</span>
<a name="line-624"></a><span class='hs-cpp'>#endif</span>
<a name="line-625"></a> <span class='hs-varid'>buildTypeInstanceFromTys</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>parentName</span> <span class='hs-varid'>dataCxt</span> <span class='hs-varid'>instTys</span> <span class='hs-conid'>True</span>
<a name="line-626"></a>
<a name="line-627"></a><a name="buildTypeInstanceFromTys"></a><span class='hs-comment'>-- For the given Types, generate an instance context and head. Coming up with</span>
<a name="line-628"></a><span class='hs-comment'>-- the instance type isn't as simple as dropping the last types, as you need to</span>
<a name="line-629"></a><span class='hs-comment'>-- be wary of kinds being instantiated with *.</span>
<a name="line-630"></a><span class='hs-comment'>-- See Note [Type inference in derived instances]</span>
<a name="line-631"></a><span class='hs-definition'>buildTypeInstanceFromTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span>
<a name="line-632"></a> <span class='hs-comment'>-- ^ Bifunctor, Bifoldable, or Bitraversable</span>
<a name="line-633"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-634"></a> <span class='hs-comment'>-- ^ The type constructor or data family name</span>
<a name="line-635"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Cxt</span>
<a name="line-636"></a> <span class='hs-comment'>-- ^ The datatype context</span>
<a name="line-637"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-638"></a> <span class='hs-comment'>-- ^ The types to instantiate the instance with</span>
<a name="line-639"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-640"></a> <span class='hs-comment'>-- ^ True if it's a data family, False otherwise</span>
<a name="line-641"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-conid'>Cxt</span><span class='hs-layout'>,</span> <span class='hs-conid'>Type</span><span class='hs-layout'>)</span>
<a name="line-642"></a><span class='hs-definition'>buildTypeInstanceFromTys</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span> <span class='hs-varid'>dataCxt</span> <span class='hs-varid'>varTysOrig</span> <span class='hs-varid'>isDataFamily</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-643"></a> <span class='hs-comment'>-- Make sure to expand through type/kind synonyms! Otherwise, the</span>
<a name="line-644"></a> <span class='hs-comment'>-- eta-reduction check might get tripped up over type variables in a</span>
<a name="line-645"></a> <span class='hs-comment'>-- synonym that are actually dropped.</span>
<a name="line-646"></a> <span class='hs-comment'>-- (See GHC Trac #11416 for a scenario where this actually happened.)</span>
<a name="line-647"></a> <span class='hs-varid'>varTysExp</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mapM</span> <span class='hs-varid'>expandSyn</span> <span class='hs-varid'>varTysOrig</span>
<a name="line-648"></a>
<a name="line-649"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>remainingLength</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span>
<a name="line-650"></a> <span class='hs-varid'>remainingLength</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>varTysOrig</span> <span class='hs-comment'>-</span> <span class='hs-num'>2</span>
<a name="line-651"></a>
<a name="line-652"></a> <span class='hs-varid'>droppedTysExp</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-653"></a> <span class='hs-varid'>droppedTysExp</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>drop</span> <span class='hs-varid'>remainingLength</span> <span class='hs-varid'>varTysExp</span>
<a name="line-654"></a>
<a name="line-655"></a> <span class='hs-varid'>droppedStarKindStati</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>StarKindStatus</span><span class='hs-keyglyph'>]</span>
<a name="line-656"></a> <span class='hs-varid'>droppedStarKindStati</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>canRealizeKindStar</span> <span class='hs-varid'>droppedTysExp</span>
<a name="line-657"></a>
<a name="line-658"></a> <span class='hs-comment'>-- Check there are enough types to drop and that all of them are either of</span>
<a name="line-659"></a> <span class='hs-comment'>-- kind * or kind k (for some kind variable k). If not, throw an error.</span>
<a name="line-660"></a> <span class='hs-varid'>when</span> <span class='hs-layout'>(</span><span class='hs-varid'>remainingLength</span> <span class='hs-varop'><</span> <span class='hs-num'>0</span> <span class='hs-varop'>||</span> <span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-conid'>NotKindStar</span><span class='hs-layout'>)</span> <span class='hs-varid'>droppedStarKindStati</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-661"></a> <span class='hs-varid'>derivingKindError</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span>
<a name="line-662"></a>
<a name="line-663"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>droppedKindVarNames</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-664"></a> <span class='hs-varid'>droppedKindVarNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>catKindVarNames</span> <span class='hs-varid'>droppedStarKindStati</span>
<a name="line-665"></a>
<a name="line-666"></a> <span class='hs-comment'>-- Substitute kind * for any dropped kind variables</span>
<a name="line-667"></a> <span class='hs-varid'>varTysExpSubst</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-668"></a> <span class='hs-varid'>varTysExpSubst</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>substNamesWithKindStar</span> <span class='hs-varid'>droppedKindVarNames</span><span class='hs-layout'>)</span> <span class='hs-varid'>varTysExp</span>
<a name="line-669"></a>
<a name="line-670"></a> <span class='hs-varid'>remainingTysExpSubst</span><span class='hs-layout'>,</span> <span class='hs-varid'>droppedTysExpSubst</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-671"></a> <span class='hs-layout'>(</span><span class='hs-varid'>remainingTysExpSubst</span><span class='hs-layout'>,</span> <span class='hs-varid'>droppedTysExpSubst</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-672"></a> <span class='hs-varid'>splitAt</span> <span class='hs-varid'>remainingLength</span> <span class='hs-varid'>varTysExpSubst</span>
<a name="line-673"></a>
<a name="line-674"></a> <span class='hs-comment'>-- All of the type variables mentioned in the dropped types</span>
<a name="line-675"></a> <span class='hs-comment'>-- (post-synonym expansion)</span>
<a name="line-676"></a> <span class='hs-varid'>droppedTyVarNames</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-677"></a> <span class='hs-varid'>droppedTyVarNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concatMap</span> <span class='hs-varid'>tyVarNamesOfType</span> <span class='hs-varid'>droppedTysExpSubst</span>
<a name="line-678"></a>
<a name="line-679"></a> <span class='hs-comment'>-- If any of the dropped types were polykinded, ensure that they are of kind *</span>
<a name="line-680"></a> <span class='hs-comment'>-- after substituting * for the dropped kind variables. If not, throw an error.</span>
<a name="line-681"></a> <span class='hs-varid'>unless</span> <span class='hs-layout'>(</span><span class='hs-varid'>all</span> <span class='hs-varid'>hasKindStar</span> <span class='hs-varid'>droppedTysExpSubst</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-682"></a> <span class='hs-varid'>derivingKindError</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span>
<a name="line-683"></a>
<a name="line-684"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>preds</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Maybe</span> <span class='hs-conid'>Pred</span><span class='hs-keyglyph'>]</span>
<a name="line-685"></a> <span class='hs-varid'>kvNames</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span><span class='hs-keyglyph'>]</span>
<a name="line-686"></a> <span class='hs-varid'>kvNames'</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-687"></a> <span class='hs-comment'>-- Derive instance constraints (and any kind variables which are specialized</span>
<a name="line-688"></a> <span class='hs-comment'>-- to * in those constraints)</span>
<a name="line-689"></a> <span class='hs-layout'>(</span><span class='hs-varid'>preds</span><span class='hs-layout'>,</span> <span class='hs-varid'>kvNames</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unzip</span> <span class='hs-varop'>$</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>deriveConstraint</span> <span class='hs-varid'>biClass</span><span class='hs-layout'>)</span> <span class='hs-varid'>remainingTysExpSubst</span>
<a name="line-690"></a> <span class='hs-varid'>kvNames'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>concat</span> <span class='hs-varid'>kvNames</span>
<a name="line-691"></a>
<a name="line-692"></a> <span class='hs-comment'>-- Substitute the kind variables specialized in the constraints with *</span>
<a name="line-693"></a> <span class='hs-varid'>remainingTysExpSubst'</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-694"></a> <span class='hs-varid'>remainingTysExpSubst'</span> <span class='hs-keyglyph'>=</span>
<a name="line-695"></a> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>substNamesWithKindStar</span> <span class='hs-varid'>kvNames'</span><span class='hs-layout'>)</span> <span class='hs-varid'>remainingTysExpSubst</span>
<a name="line-696"></a>
<a name="line-697"></a> <span class='hs-comment'>-- We now substitute all of the specialized-to-* kind variable names with</span>
<a name="line-698"></a> <span class='hs-comment'>-- *, but in the original types, not the synonym-expanded types. The reason</span>
<a name="line-699"></a> <span class='hs-comment'>-- we do this is a superficial one: we want the derived instance to resemble</span>
<a name="line-700"></a> <span class='hs-comment'>-- the datatype written in source code as closely as possible. For example,</span>
<a name="line-701"></a> <span class='hs-comment'>-- for the following data family instance:</span>
<a name="line-702"></a> <span class='hs-comment'>--</span>
<a name="line-703"></a> <span class='hs-comment'>-- data family Fam a</span>
<a name="line-704"></a> <span class='hs-comment'>-- newtype instance Fam String = Fam String</span>
<a name="line-705"></a> <span class='hs-comment'>--</span>
<a name="line-706"></a> <span class='hs-comment'>-- We'd want to generate the instance:</span>
<a name="line-707"></a> <span class='hs-comment'>--</span>
<a name="line-708"></a> <span class='hs-comment'>-- instance C (Fam String)</span>
<a name="line-709"></a> <span class='hs-comment'>--</span>
<a name="line-710"></a> <span class='hs-comment'>-- Not:</span>
<a name="line-711"></a> <span class='hs-comment'>--</span>
<a name="line-712"></a> <span class='hs-comment'>-- instance C (Fam [Char])</span>
<a name="line-713"></a> <span class='hs-varid'>remainingTysOrigSubst</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-714"></a> <span class='hs-varid'>remainingTysOrigSubst</span> <span class='hs-keyglyph'>=</span>
<a name="line-715"></a> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>substNamesWithKindStar</span> <span class='hs-layout'>(</span><span class='hs-varid'>union</span> <span class='hs-varid'>droppedKindVarNames</span> <span class='hs-varid'>kvNames'</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-716"></a> <span class='hs-varop'>$</span> <span class='hs-varid'>take</span> <span class='hs-varid'>remainingLength</span> <span class='hs-varid'>varTysOrig</span>
<a name="line-717"></a>
<a name="line-718"></a> <span class='hs-varid'>remainingTysOrigSubst'</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span>
<a name="line-719"></a> <span class='hs-comment'>-- See Note [Kind signatures in derived instances] for an explanation</span>
<a name="line-720"></a> <span class='hs-comment'>-- of the isDataFamily check.</span>
<a name="line-721"></a> <span class='hs-varid'>remainingTysOrigSubst'</span> <span class='hs-keyglyph'>=</span>
<a name="line-722"></a> <span class='hs-keyword'>if</span> <span class='hs-varid'>isDataFamily</span>
<a name="line-723"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>remainingTysOrigSubst</span>
<a name="line-724"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>map</span> <span class='hs-varid'>unSigT</span> <span class='hs-varid'>remainingTysOrigSubst</span>
<a name="line-725"></a>
<a name="line-726"></a> <span class='hs-varid'>instanceCxt</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Cxt</span>
<a name="line-727"></a> <span class='hs-varid'>instanceCxt</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>catMaybes</span> <span class='hs-varid'>preds</span>
<a name="line-728"></a>
<a name="line-729"></a> <span class='hs-varid'>instanceType</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Type</span>
<a name="line-730"></a> <span class='hs-varid'>instanceType</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>AppT</span> <span class='hs-layout'>(</span><span class='hs-conid'>ConT</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biClassName</span> <span class='hs-varid'>biClass</span><span class='hs-layout'>)</span>
<a name="line-731"></a> <span class='hs-varop'>$</span> <span class='hs-varid'>applyTyCon</span> <span class='hs-varid'>tyConName</span> <span class='hs-varid'>remainingTysOrigSubst'</span>
<a name="line-732"></a>
<a name="line-733"></a> <span class='hs-comment'>-- If the datatype context mentions any of the dropped type variables,</span>
<a name="line-734"></a> <span class='hs-comment'>-- we can't derive an instance, so throw an error.</span>
<a name="line-735"></a> <span class='hs-varid'>when</span> <span class='hs-layout'>(</span><span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`predMentionsName`</span> <span class='hs-varid'>droppedTyVarNames</span><span class='hs-layout'>)</span> <span class='hs-varid'>dataCxt</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-736"></a> <span class='hs-varid'>datatypeContextError</span> <span class='hs-varid'>tyConName</span> <span class='hs-varid'>instanceType</span>
<a name="line-737"></a> <span class='hs-comment'>-- Also ensure the dropped types can be safely eta-reduced. Otherwise,</span>
<a name="line-738"></a> <span class='hs-comment'>-- throw an error.</span>
<a name="line-739"></a> <span class='hs-varid'>unless</span> <span class='hs-layout'>(</span><span class='hs-varid'>canEtaReduce</span> <span class='hs-varid'>remainingTysExpSubst'</span> <span class='hs-varid'>droppedTysExpSubst</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span>
<a name="line-740"></a> <span class='hs-varid'>etaReductionError</span> <span class='hs-varid'>instanceType</span>
<a name="line-741"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>instanceCxt</span><span class='hs-layout'>,</span> <span class='hs-varid'>instanceType</span><span class='hs-layout'>)</span>
<a name="line-742"></a>
<a name="line-743"></a><a name="deriveConstraint"></a><span class='hs-comment'>-- | Attempt to derive a constraint on a Type. If successful, return</span>
<a name="line-744"></a><span class='hs-comment'>-- Just the constraint and any kind variable names constrained to *.</span>
<a name="line-745"></a><span class='hs-comment'>-- Otherwise, return Nothing and the empty list.</span>
<a name="line-746"></a><span class='hs-comment'>--</span>
<a name="line-747"></a><span class='hs-comment'>-- See Note [Type inference in derived instances] for the heuristics used to</span>
<a name="line-748"></a><span class='hs-comment'>-- come up with constraints.</span>
<a name="line-749"></a><span class='hs-definition'>deriveConstraint</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-conid'>Pred</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span>
<a name="line-750"></a><span class='hs-definition'>deriveConstraint</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>t</span>
<a name="line-751"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>not</span> <span class='hs-layout'>(</span><span class='hs-varid'>isTyVar</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-conid'>Nothing</span><span class='hs-layout'>,</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-752"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>hasKindVarChain</span> <span class='hs-num'>1</span> <span class='hs-varid'>t</span> <span class='hs-keyword'>of</span>
<a name="line-753"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>ns</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varop'>`applyClass`</span> <span class='hs-varid'>tName</span><span class='hs-layout'>)</span> <span class='hs-varop'>`fmap`</span> <span class='hs-varid'>biClassConstraint</span> <span class='hs-varid'>biClass</span> <span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>ns</span><span class='hs-layout'>)</span>
<a name="line-754"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>hasKindVarChain</span> <span class='hs-num'>2</span> <span class='hs-varid'>t</span> <span class='hs-keyword'>of</span>
<a name="line-755"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>ns</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varop'>`applyClass`</span> <span class='hs-varid'>tName</span><span class='hs-layout'>)</span> <span class='hs-varop'>`fmap`</span> <span class='hs-varid'>biClassConstraint</span> <span class='hs-varid'>biClass</span> <span class='hs-num'>2</span><span class='hs-layout'>,</span> <span class='hs-varid'>ns</span><span class='hs-layout'>)</span>
<a name="line-756"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Nothing</span><span class='hs-layout'>,</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span>
<a name="line-757"></a> <span class='hs-keyword'>where</span>
<a name="line-758"></a> <span class='hs-varid'>tName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-759"></a> <span class='hs-varid'>tName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>varTToName</span> <span class='hs-varid'>t</span>
<a name="line-760"></a>
<a name="line-761"></a><span class='hs-comment'>{-
<a name="line-762"></a>Note [Polykinded data families in Template Haskell]
<a name="line-763"></a>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a name="line-764"></a>
<a name="line-765"></a>In order to come up with the correct instance context and head for an instance, e.g.,
<a name="line-766"></a>
<a name="line-767"></a> instance C a => C (Data a) where ...
<a name="line-768"></a>
<a name="line-769"></a>We need to know the exact types and kinds used to instantiate the instance. For
<a name="line-770"></a>plain old datatypes, this is simple: every type must be a type variable, and
<a name="line-771"></a>Template Haskell reliably tells us the type variables and their kinds.
<a name="line-772"></a>
<a name="line-773"></a>Doing the same for data families proves to be much harder for three reasons:
<a name="line-774"></a>
<a name="line-775"></a>1. On any version of Template Haskell, it may not tell you what an instantiated
<a name="line-776"></a> type's kind is. For instance, in the following data family instance:
<a name="line-777"></a>
<a name="line-778"></a> data family Fam (f :: * -> *) (a :: *)
<a name="line-779"></a> data instance Fam f a
<a name="line-780"></a>
<a name="line-781"></a> Then if we use TH's reify function, it would tell us the TyVarBndrs of the
<a name="line-782"></a> data family declaration are:
<a name="line-783"></a>
<a name="line-784"></a> [KindedTV f (AppT (AppT ArrowT StarT) StarT),KindedTV a StarT]
<a name="line-785"></a>
<a name="line-786"></a> and the instantiated types of the data family instance are:
<a name="line-787"></a>
<a name="line-788"></a> [VarT f1,VarT a1]
<a name="line-789"></a>
<a name="line-790"></a> We can't just pass [VarT f1,VarT a1] to buildTypeInstanceFromTys, since we
<a name="line-791"></a> have no way of knowing their kinds. Luckily, the TyVarBndrs tell us what the
<a name="line-792"></a> kind is in case an instantiated type isn't a SigT, so we use the stealKindForType
<a name="line-793"></a> function to ensure all of the instantiated types are SigTs before passing them
<a name="line-794"></a> to buildTypeInstanceFromTys.
<a name="line-795"></a>2. On GHC 7.6 and 7.8, a bug is present in which Template Haskell lists all of
<a name="line-796"></a> the specified kinds of a data family instance efore any of the instantiated
<a name="line-797"></a> types. Fortunately, this is easy to deal with: you simply count the number of
<a name="line-798"></a> distinct kind variables in the data family declaration, take that many elements
<a name="line-799"></a> from the front of the Types list of the data family instance, substitute the
<a name="line-800"></a> kind variables with their respective instantiated kinds (which you took earlier),
<a name="line-801"></a> and proceed as normal.
<a name="line-802"></a>3. On GHC 7.8, an even uglier bug is present (GHC Trac #9692) in which Template
<a name="line-803"></a> Haskell might not even list all of the Types of a data family instance, since
<a name="line-804"></a> they are eta-reduced away! And yes, kinds can be eta-reduced too.
<a name="line-805"></a>
<a name="line-806"></a> The simplest workaround is to count how many instantiated types are missing from
<a name="line-807"></a> the list and generate extra type variables to use in their place. Luckily, we
<a name="line-808"></a> needn't worry much if its kind was eta-reduced away, since using stealKindForType
<a name="line-809"></a> will get it back.
<a name="line-810"></a>
<a name="line-811"></a>Note [Kind signatures in derived instances]
<a name="line-812"></a>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a name="line-813"></a>
<a name="line-814"></a>It is possible to put explicit kind signatures into the derived instances, e.g.,
<a name="line-815"></a>
<a name="line-816"></a> instance C a => C (Data (f :: * -> *)) where ...
<a name="line-817"></a>
<a name="line-818"></a>But it is preferable to avoid this if possible. If we come up with an incorrect
<a name="line-819"></a>kind signature (which is entirely possible, since our type inferencer is pretty
<a name="line-820"></a>unsophisticated - see Note [Type inference in derived instances]), then GHC will
<a name="line-821"></a>flat-out reject the instance, which is quite unfortunate.
<a name="line-822"></a>
<a name="line-823"></a>Plain old datatypes have the advantage that you can avoid using any kind signatures
<a name="line-824"></a>at all in their instances. This is because a datatype declaration uses all type
<a name="line-825"></a>variables, so the types that we use in a derived instance uniquely determine their
<a name="line-826"></a>kinds. As long as we plug in the right types, the kind inferencer can do the rest
<a name="line-827"></a>of the work. For this reason, we use unSigT to remove all kind signatures before
<a name="line-828"></a>splicing in the instance context and head.
<a name="line-829"></a>
<a name="line-830"></a>Data family instances are trickier, since a data family can have two instances that
<a name="line-831"></a>are distinguished by kind alone, e.g.,
<a name="line-832"></a>
<a name="line-833"></a> data family Fam (a :: k)
<a name="line-834"></a> data instance Fam (a :: * -> *)
<a name="line-835"></a> data instance Fam (a :: *)
<a name="line-836"></a>
<a name="line-837"></a>If we dropped the kind signatures for C (Fam a), then GHC will have no way of
<a name="line-838"></a>knowing which instance we are talking about. To avoid this scenario, we always
<a name="line-839"></a>include explicit kind signatures in data family instances. There is a chance that
<a name="line-840"></a>the inferred kind signatures will be incorrect, but if so, we can always fall back
<a name="line-841"></a>on the make- functions.
<a name="line-842"></a>
<a name="line-843"></a>Note [Type inference in derived instances]
<a name="line-844"></a>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a name="line-845"></a>
<a name="line-846"></a>Type inference is can be tricky to get right, and we want to avoid recreating the
<a name="line-847"></a>entirety of GHC's type inferencer in Template Haskell. For this reason, we will
<a name="line-848"></a>probably never come up with derived instance contexts that are as accurate as
<a name="line-849"></a>GHC's. But that doesn't mean we can't do anything! There are a couple of simple
<a name="line-850"></a>things we can do to make instance contexts that work for 80% of use cases:
<a name="line-851"></a>
<a name="line-852"></a>1. If one of the last type parameters is polykinded, then its kind will be
<a name="line-853"></a> specialized to * in the derived instance. We note what kind variable the type
<a name="line-854"></a> parameter had and substitute it with * in the other types as well. For example,
<a name="line-855"></a> imagine you had
<a name="line-856"></a>
<a name="line-857"></a> data Data (a :: k) (b :: k) (c :: k)
<a name="line-858"></a>
<a name="line-859"></a> Then you'd want to derived instance to be:
<a name="line-860"></a>
<a name="line-861"></a> instance C (Data (a :: *))
<a name="line-862"></a>
<a name="line-863"></a> Not:
<a name="line-864"></a>
<a name="line-865"></a> instance C (Data (a :: k))
<a name="line-866"></a>
<a name="line-867"></a>2. We naïvely come up with instance constraints using the following criteria:
<a name="line-868"></a>
<a name="line-869"></a> (i) If there's a type parameter n of kind k1 -> k2 (where k1/k2 are * or kind
<a name="line-870"></a> variables), then generate a Functor n constraint, and if k1/k2 are kind
<a name="line-871"></a> variables, then substitute k1/k2 with * elsewhere in the types. We must
<a name="line-872"></a> consider the case where they are kind variables because you might have a
<a name="line-873"></a> scenario like this:
<a name="line-874"></a>
<a name="line-875"></a> newtype Compose (f :: k3 -> *) (g :: k1 -> k2 -> k3) (a :: k1) (b :: k2)
<a name="line-876"></a> = Compose (f (g a b))
<a name="line-877"></a>
<a name="line-878"></a> Which would have a derived Bifunctor instance of:
<a name="line-879"></a>
<a name="line-880"></a> instance (Functor f, Bifunctor g) => Bifunctor (Compose f g) where ...
<a name="line-881"></a> (ii) If there's a type parameter n of kind k1 -> k2 -> k3 (where k1/k2/k3 are
<a name="line-882"></a> * or kind variables), then generate a Bifunctor n constraint and perform
<a name="line-883"></a> kind substitution as in the other case.
<a name="line-884"></a>-}</span>
<a name="line-885"></a>
<a name="line-886"></a><a name="reifyConTys"></a><span class='hs-comment'>-- Determines the types of a constructor's arguments as well as the last type</span>
<a name="line-887"></a><span class='hs-comment'>-- parameters (along with their map functions), expanding through any type synonyms.</span>
<a name="line-888"></a><span class='hs-comment'>-- The type parameters are determined on a constructor-by-constructor basis since</span>
<a name="line-889"></a><span class='hs-comment'>-- they may be refined to be particular types in a GADT.</span>
<a name="line-890"></a><span class='hs-definition'>reifyConTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span>
<a name="line-891"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-892"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-893"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-894"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Type</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-conid'>TyVarMap</span><span class='hs-layout'>)</span>
<a name="line-895"></a><span class='hs-definition'>reifyConTys</span> <span class='hs-varid'>biFun</span> <span class='hs-varid'>conName</span> <span class='hs-varid'>map1</span> <span class='hs-varid'>map2</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-896"></a> <span class='hs-varid'>info</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>reify</span> <span class='hs-varid'>conName</span>
<a name="line-897"></a> <span class='hs-layout'>(</span><span class='hs-varid'>ctxt</span><span class='hs-layout'>,</span> <span class='hs-varid'>uncTy</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>info</span> <span class='hs-keyword'>of</span>
<a name="line-898"></a> <span class='hs-conid'>DataConI</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>ty</span> <span class='hs-keyword'>_</span>
<a name="line-899"></a><span class='hs-cpp'>#if !(MIN_VERSION_template_haskell(2,11,0))</span>
<a name="line-900"></a> <span class='hs-keyword'>_</span>
<a name="line-901"></a><span class='hs-cpp'>#endif</span>
<a name="line-902"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>fmap</span> <span class='hs-varid'>uncurryTy</span> <span class='hs-layout'>(</span><span class='hs-varid'>expandSyn</span> <span class='hs-varid'>ty</span><span class='hs-layout'>)</span>
<a name="line-903"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>error</span> <span class='hs-str'>"Must be a data constructor"</span>
<a name="line-904"></a> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>argTys</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>resTy</span><span class='hs-keyglyph'>]</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'>length</span> <span class='hs-varid'>uncTy</span> <span class='hs-comment'>-</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>uncTy</span>
<a name="line-905"></a> <span class='hs-varid'>unapResTy</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unapplyTy</span> <span class='hs-varid'>resTy</span>
<a name="line-906"></a> <span class='hs-comment'>-- If one of the last type variables is refined to a particular type</span>
<a name="line-907"></a> <span class='hs-comment'>-- (i.e., not truly polymorphic), we mark it with Nothing and filter</span>
<a name="line-908"></a> <span class='hs-comment'>-- it out later, since we only apply map functions to arguments of</span>
<a name="line-909"></a> <span class='hs-comment'>-- a type that it (1) one of the last type variables, and (2)</span>
<a name="line-910"></a> <span class='hs-comment'>-- of a truly polymorphic type.</span>
<a name="line-911"></a> <span class='hs-varid'>mbTvNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>varTToName_maybe</span> <span class='hs-varop'>$</span>
<a name="line-912"></a> <span class='hs-varid'>drop</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>unapResTy</span> <span class='hs-comment'>-</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varid'>unapResTy</span>
<a name="line-913"></a> <span class='hs-comment'>-- We use Map.fromList to ensure that if there are any duplicate type</span>
<a name="line-914"></a> <span class='hs-comment'>-- variables (as can happen in a GADT), the rightmost type variable gets</span>
<a name="line-915"></a> <span class='hs-comment'>-- associated with the map function.</span>
<a name="line-916"></a> <span class='hs-comment'>--</span>
<a name="line-917"></a> <span class='hs-comment'>-- See Note [Matching functions with GADT type variables]</span>
<a name="line-918"></a> <span class='hs-varid'>tvMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span>
<a name="line-919"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>catMaybes</span> <span class='hs-comment'>-- Drop refined types</span>
<a name="line-920"></a> <span class='hs-varop'>$</span> <span class='hs-varid'>zipWith</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>mbTvName</span> <span class='hs-varid'>sp</span> <span class='hs-keyglyph'>-></span>
<a name="line-921"></a> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>tvName</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>tvName</span><span class='hs-layout'>,</span> <span class='hs-varid'>sp</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>mbTvName</span><span class='hs-layout'>)</span>
<a name="line-922"></a> <span class='hs-varid'>mbTvNames</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>map1</span><span class='hs-layout'>,</span> <span class='hs-varid'>map2</span><span class='hs-keyglyph'>]</span>
<a name="line-923"></a> <span class='hs-keyword'>if</span> <span class='hs-layout'>(</span><span class='hs-varid'>any</span> <span class='hs-layout'>(</span><span class='hs-varop'>`predMentionsName`</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>keys</span> <span class='hs-varid'>tvMap</span><span class='hs-layout'>)</span> <span class='hs-varid'>ctxt</span>
<a name="line-924"></a> <span class='hs-varop'>||</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>size</span> <span class='hs-varid'>tvMap</span> <span class='hs-varop'><</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span>
<a name="line-925"></a> <span class='hs-varop'>&&</span> <span class='hs-varid'>not</span> <span class='hs-layout'>(</span><span class='hs-varid'>allowExQuant</span> <span class='hs-layout'>(</span><span class='hs-varid'>biFunToClass</span> <span class='hs-varid'>biFun</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-926"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>existentialContextError</span> <span class='hs-varid'>conName</span>
<a name="line-927"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>argTys</span><span class='hs-layout'>,</span> <span class='hs-varid'>tvMap</span><span class='hs-layout'>)</span>
<a name="line-928"></a>
<a name="line-929"></a><span class='hs-comment'>{-
<a name="line-930"></a>Note [Matching functions with GADT type variables]
<a name="line-931"></a>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a name="line-932"></a>
<a name="line-933"></a>When deriving Bifoldable, there is a tricky corner case to consider:
<a name="line-934"></a>
<a name="line-935"></a> data Both a b where
<a name="line-936"></a> BothCon :: x -> x -> Both x x
<a name="line-937"></a>
<a name="line-938"></a>Which fold functions should be applied to which arguments of BothCon? We have a
<a name="line-939"></a>choice, since both the function of type (a -> m) and of type (b -> m) can be
<a name="line-940"></a>applied to either argument. In such a scenario, the second fold function takes
<a name="line-941"></a>precedence over the first fold function, so the derived Bifoldable instance would be:
<a name="line-942"></a>
<a name="line-943"></a> instance Bifoldable Both where
<a name="line-944"></a> bifoldMap _ g (BothCon x1 x2) = g x1 <> g x2
<a name="line-945"></a>
<a name="line-946"></a>This is not an arbitrary choice, as this definition ensures that
<a name="line-947"></a>bifoldMap id = Foldable.foldMap for a derived Bifoldable instance for Both.
<a name="line-948"></a>-}</span>
<a name="line-949"></a>
<a name="line-950"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-951"></a><span class='hs-comment'>-- Error messages</span>
<a name="line-952"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-953"></a>
<a name="line-954"></a><a name="derivingKindError"></a><span class='hs-comment'>-- | Either the given data type doesn't have enough type variables, or one of</span>
<a name="line-955"></a><span class='hs-comment'>-- the type variables to be eta-reduced cannot realize kind *.</span>
<a name="line-956"></a><span class='hs-definition'>derivingKindError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-957"></a><span class='hs-definition'>derivingKindError</span> <span class='hs-varid'>biClass</span> <span class='hs-varid'>tyConName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-958"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Cannot derive well-kinded instance of form ‘"</span>
<a name="line-959"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-varid'>className</span>
<a name="line-960"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showChar</span> <span class='hs-chr'>' '</span>
<a name="line-961"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showParen</span> <span class='hs-conid'>True</span>
<a name="line-962"></a> <span class='hs-layout'>(</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>tyConName</span><span class='hs-layout'>)</span>
<a name="line-963"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>" ..."</span>
<a name="line-964"></a> <span class='hs-layout'>)</span>
<a name="line-965"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘\n\tClass "</span>
<a name="line-966"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-varid'>className</span>
<a name="line-967"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>" expects an argument of kind * -> * -> *"</span>
<a name="line-968"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-969"></a> <span class='hs-keyword'>where</span>
<a name="line-970"></a> <span class='hs-varid'>className</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span>
<a name="line-971"></a> <span class='hs-varid'>className</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>nameBase</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biClassName</span> <span class='hs-varid'>biClass</span>
<a name="line-972"></a>
<a name="line-973"></a><a name="contravarianceError"></a><span class='hs-comment'>-- | One of the last two type variables appeard in a contravariant position</span>
<a name="line-974"></a><span class='hs-comment'>-- when deriving Bifoldable or Bitraversable.</span>
<a name="line-975"></a><span class='hs-definition'>contravarianceError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-976"></a><span class='hs-definition'>contravarianceError</span> <span class='hs-varid'>conName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-977"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Constructor ‘"</span>
<a name="line-978"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span>
<a name="line-979"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘ must not use the last type variable(s) in a function argument"</span>
<a name="line-980"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-981"></a>
<a name="line-982"></a><a name="noFunctionsError"></a><span class='hs-comment'>-- | A constructor has a function argument in a derived Bifoldable or Bitraversable</span>
<a name="line-983"></a><span class='hs-comment'>-- instance.</span>
<a name="line-984"></a><span class='hs-definition'>noFunctionsError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-985"></a><span class='hs-definition'>noFunctionsError</span> <span class='hs-varid'>conName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-986"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Constructor ‘"</span>
<a name="line-987"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span>
<a name="line-988"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘ must not contain function types"</span>
<a name="line-989"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-990"></a>
<a name="line-991"></a><a name="datatypeContextError"></a><span class='hs-comment'>-- | The data type has a DatatypeContext which mentions one of the eta-reduced</span>
<a name="line-992"></a><span class='hs-comment'>-- type variables.</span>
<a name="line-993"></a><span class='hs-definition'>datatypeContextError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-994"></a><span class='hs-definition'>datatypeContextError</span> <span class='hs-varid'>dataName</span> <span class='hs-varid'>instanceType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-995"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Can't make a derived instance of ‘"</span>
<a name="line-996"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>pprint</span> <span class='hs-varid'>instanceType</span><span class='hs-layout'>)</span>
<a name="line-997"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘:\n\tData type ‘"</span>
<a name="line-998"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>dataName</span><span class='hs-layout'>)</span>
<a name="line-999"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘ must not have a class context involving the last type argument(s)"</span>
<a name="line-1000"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-1001"></a>
<a name="line-1002"></a><a name="existentialContextError"></a><span class='hs-comment'>-- | The data type has an existential constraint which mentions one of the</span>
<a name="line-1003"></a><span class='hs-comment'>-- eta-reduced type variables.</span>
<a name="line-1004"></a><span class='hs-definition'>existentialContextError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-1005"></a><span class='hs-definition'>existentialContextError</span> <span class='hs-varid'>conName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-1006"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Constructor ‘"</span>
<a name="line-1007"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span>
<a name="line-1008"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘ must be truly polymorphic in the last argument(s) of the data type"</span>
<a name="line-1009"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-1010"></a>
<a name="line-1011"></a><a name="outOfPlaceTyVarError"></a><span class='hs-comment'>-- | The data type mentions one of the n eta-reduced type variables in a place other</span>
<a name="line-1012"></a><span class='hs-comment'>-- than the last nth positions of a data type in a constructor's field.</span>
<a name="line-1013"></a><span class='hs-definition'>outOfPlaceTyVarError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-1014"></a><span class='hs-definition'>outOfPlaceTyVarError</span> <span class='hs-varid'>conName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-1015"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Constructor ‘"</span>
<a name="line-1016"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-layout'>(</span><span class='hs-varid'>nameBase</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span>
<a name="line-1017"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"‘ must only use its last two type variable(s) within"</span>
<a name="line-1018"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>" the last two argument(s) of a data type"</span>
<a name="line-1019"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-1020"></a>
<a name="line-1021"></a><a name="etaReductionError"></a><span class='hs-comment'>-- | One of the last type variables cannot be eta-reduced (see the canEtaReduce</span>
<a name="line-1022"></a><span class='hs-comment'>-- function for the criteria it would have to meet).</span>
<a name="line-1023"></a><span class='hs-definition'>etaReductionError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Type</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-1024"></a><span class='hs-definition'>etaReductionError</span> <span class='hs-varid'>instanceType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-varop'>$</span>
<a name="line-1025"></a> <span class='hs-str'>"Cannot eta-reduce to an instance of form \n\tinstance (...) => "</span>
<a name="line-1026"></a> <span class='hs-varop'>++</span> <span class='hs-varid'>pprint</span> <span class='hs-varid'>instanceType</span>
<a name="line-1027"></a>
<a name="line-1028"></a><span class='hs-cpp'>#if !(MIN_VERSION_template_haskell(2,7,0))</span>
<a name="line-1029"></a><a name="dataConIError"></a><span class='hs-comment'>-- | Template Haskell didn't list all of a data family's instances upon reification</span>
<a name="line-1030"></a><span class='hs-comment'>-- until template-haskell-2.7.0.0, which is necessary for a derived instance to work.</span>
<a name="line-1031"></a><span class='hs-definition'>dataConIError</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span>
<a name="line-1032"></a><span class='hs-definition'>dataConIError</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span>
<a name="line-1033"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"Cannot use a data constructor."</span>
<a name="line-1034"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"\n\t(Note: if you are trying to derive for a data family instance,"</span>
<a name="line-1035"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>showString</span> <span class='hs-str'>"\n\tuse GHC >= 7.4 instead.)"</span>
<a name="line-1036"></a> <span class='hs-varop'>$</span> <span class='hs-str'>""</span>
<a name="line-1037"></a><span class='hs-cpp'>#endif</span>
<a name="line-1038"></a>
<a name="line-1039"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-1040"></a><span class='hs-comment'>-- Class-specific constants</span>
<a name="line-1041"></a><span class='hs-comment'>-------------------------------------------------------------------------------</span>
<a name="line-1042"></a>
<a name="line-1043"></a><a name="BiClass"></a><span class='hs-comment'>-- | A representation of which class is being derived.</span>
<a name="line-1044"></a><a name="BiClass"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bifunctor</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Bifoldable</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Bitraversable</span>
<a name="line-1045"></a>
<a name="line-1046"></a><a name="BiFun"></a><span class='hs-comment'>-- | A representation of which function is being generated.</span>
<a name="line-1047"></a><a name="BiFun"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Bitraverse</span>
<a name="line-1048"></a> <span class='hs-keyword'>deriving</span> <span class='hs-conid'>Eq</span>
<a name="line-1049"></a>
<a name="line-1050"></a><a name="biFunConstName"></a><span class='hs-definition'>biFunConstName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1051"></a><span class='hs-definition'>biFunConstName</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bimapConstValName</span>
<a name="line-1052"></a><span class='hs-definition'>biFunConstName</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldrConstValName</span>
<a name="line-1053"></a><span class='hs-definition'>biFunConstName</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldMapConstValName</span>
<a name="line-1054"></a><span class='hs-definition'>biFunConstName</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bitraverseConstValName</span>
<a name="line-1055"></a>
<a name="line-1056"></a><a name="biClassName"></a><span class='hs-definition'>biClassName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1057"></a><span class='hs-definition'>biClassName</span> <span class='hs-conid'>Bifunctor</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifunctorTypeName</span>
<a name="line-1058"></a><span class='hs-definition'>biClassName</span> <span class='hs-conid'>Bifoldable</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldableTypeName</span>
<a name="line-1059"></a><span class='hs-definition'>biClassName</span> <span class='hs-conid'>Bitraversable</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bitraversableTypeName</span>
<a name="line-1060"></a>
<a name="line-1061"></a><a name="biFunName"></a><span class='hs-definition'>biFunName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1062"></a><span class='hs-definition'>biFunName</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bimapValName</span>
<a name="line-1063"></a><span class='hs-definition'>biFunName</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldrValName</span>
<a name="line-1064"></a><span class='hs-definition'>biFunName</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldMapValName</span>
<a name="line-1065"></a><span class='hs-definition'>biFunName</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bitraverseValName</span>
<a name="line-1066"></a>
<a name="line-1067"></a><a name="biClassToFuns"></a><span class='hs-definition'>biClassToFuns</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>BiFun</span><span class='hs-keyglyph'>]</span>
<a name="line-1068"></a><span class='hs-definition'>biClassToFuns</span> <span class='hs-conid'>Bifunctor</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Bimap</span><span class='hs-keyglyph'>]</span>
<a name="line-1069"></a><span class='hs-definition'>biClassToFuns</span> <span class='hs-conid'>Bifoldable</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Bifoldr</span><span class='hs-layout'>,</span> <span class='hs-conid'>BifoldMap</span><span class='hs-keyglyph'>]</span>
<a name="line-1070"></a><span class='hs-definition'>biClassToFuns</span> <span class='hs-conid'>Bitraversable</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Bitraverse</span><span class='hs-keyglyph'>]</span>
<a name="line-1071"></a>
<a name="line-1072"></a><a name="biFunToClass"></a><span class='hs-definition'>biFunToClass</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>BiClass</span>
<a name="line-1073"></a><span class='hs-definition'>biFunToClass</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bifunctor</span>
<a name="line-1074"></a><span class='hs-definition'>biFunToClass</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bifoldable</span>
<a name="line-1075"></a><span class='hs-definition'>biFunToClass</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bifoldable</span>
<a name="line-1076"></a><span class='hs-definition'>biFunToClass</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Bitraversable</span>
<a name="line-1077"></a>
<a name="line-1078"></a><a name="biClassConstraint"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Name</span>
<a name="line-1079"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-conid'>Bifunctor</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>functorTypeName</span>
<a name="line-1080"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-conid'>Bifoldable</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>foldableTypeName</span>
<a name="line-1081"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-conid'>Bitraversable</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>traversableTypeName</span>
<a name="line-1082"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-varid'>biClass</span> <span class='hs-num'>2</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biClassName</span> <span class='hs-varid'>biClass</span>
<a name="line-1083"></a><span class='hs-definition'>biClassConstraint</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-1084"></a>
<a name="line-1085"></a><a name="biFunArity"></a><span class='hs-definition'>biFunArity</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Name</span>
<a name="line-1086"></a><span class='hs-definition'>biFunArity</span> <span class='hs-conid'>Bimap</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>fmapValName</span>
<a name="line-1087"></a><span class='hs-definition'>biFunArity</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>foldrValName</span>
<a name="line-1088"></a><span class='hs-definition'>biFunArity</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>foldMapValName</span>
<a name="line-1089"></a><span class='hs-definition'>biFunArity</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>traverseValName</span>
<a name="line-1090"></a><span class='hs-definition'>biFunArity</span> <span class='hs-varid'>biFun</span> <span class='hs-num'>2</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varop'>$</span> <span class='hs-varid'>biFunName</span> <span class='hs-varid'>biFun</span>
<a name="line-1091"></a><span class='hs-definition'>biFunArity</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-1092"></a>
<a name="line-1093"></a><a name="allowFunTys"></a><span class='hs-definition'>allowFunTys</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-1094"></a><span class='hs-definition'>allowFunTys</span> <span class='hs-conid'>Bifunctor</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-1095"></a><span class='hs-definition'>allowFunTys</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-1096"></a>
<a name="line-1097"></a><a name="allowExQuant"></a><span class='hs-definition'>allowExQuant</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiClass</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-1098"></a><span class='hs-definition'>allowExQuant</span> <span class='hs-conid'>Bifoldable</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-1099"></a><span class='hs-definition'>allowExQuant</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-1100"></a>
<a name="line-1101"></a><a name="biFunTriv"></a><span class='hs-comment'>-- See Trac #7436 for why explicit lambdas are used</span>
<a name="line-1102"></a><span class='hs-definition'>biFunTriv</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1103"></a><span class='hs-definition'>biFunTriv</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1104"></a> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"x"</span>
<a name="line-1105"></a> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>varP</span> <span class='hs-varid'>x</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>x</span>
<a name="line-1106"></a><span class='hs-comment'>-- The biFunTriv definitions for bifoldr, bifoldMap, and bitraverse might seem</span>
<a name="line-1107"></a><span class='hs-comment'>-- useless, but they do serve a purpose.</span>
<a name="line-1108"></a><span class='hs-comment'>-- See Note [biFunTriv for Bifoldable and Bitraversable]</span>
<a name="line-1109"></a><span class='hs-definition'>biFunTriv</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1110"></a> <span class='hs-varid'>z</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"z"</span>
<a name="line-1111"></a> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>wildP</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>z</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>z</span>
<a name="line-1112"></a><span class='hs-definition'>biFunTriv</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>wildP</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>memptyValName</span>
<a name="line-1113"></a><span class='hs-definition'>biFunTriv</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>pureValName</span>
<a name="line-1114"></a>
<a name="line-1115"></a><a name="biFunApp"></a><span class='hs-definition'>biFunApp</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1116"></a><span class='hs-definition'>biFunApp</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1117"></a> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"x"</span>
<a name="line-1118"></a> <span class='hs-varid'>z</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newName</span> <span class='hs-str'>"z"</span>
<a name="line-1119"></a> <span class='hs-varid'>lamE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>varP</span> <span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>z</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>$</span> <span class='hs-varid'>appsE</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>e</span><span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>z</span><span class='hs-layout'>,</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>x</span><span class='hs-keyglyph'>]</span>
<a name="line-1120"></a><span class='hs-definition'>biFunApp</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>e</span>
<a name="line-1121"></a>
<a name="line-1122"></a><a name="biFunCombine"></a><span class='hs-definition'>biFunCombine</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>BiFun</span>
<a name="line-1123"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1124"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1125"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1126"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-1127"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1128"></a><span class='hs-definition'>biFunCombine</span> <span class='hs-conid'>Bimap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bimapCombine</span>
<a name="line-1129"></a><span class='hs-definition'>biFunCombine</span> <span class='hs-conid'>Bifoldr</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldrCombine</span>
<a name="line-1130"></a><span class='hs-definition'>biFunCombine</span> <span class='hs-conid'>BifoldMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bifoldMapCombine</span>
<a name="line-1131"></a><span class='hs-definition'>biFunCombine</span> <span class='hs-conid'>Bitraverse</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>bitraverseCombine</span>
<a name="line-1132"></a>
<a name="line-1133"></a><a name="bimapCombine"></a><span class='hs-definition'>bimapCombine</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-1134"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1135"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1136"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-1137"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1138"></a><span class='hs-definition'>bimapCombine</span> <span class='hs-varid'>conName</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-varid'>foldl'</span> <span class='hs-conid'>AppE</span> <span class='hs-layout'>(</span><span class='hs-conid'>ConE</span> <span class='hs-varid'>conName</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fmap</span> <span class='hs-varid'>fromEither</span><span class='hs-layout'>)</span>
<a name="line-1139"></a>
<a name="line-1140"></a><span class='hs-comment'>-- bifoldr, bifoldMap, and bitraverse are handled differently from bimap, since</span>
<a name="line-1141"></a><span class='hs-comment'>-- they filter out subexpressions whose types do not mention one of the last two</span>
<a name="line-1142"></a><span class='hs-comment'>-- type parameters. See</span>
<a name="line-1143"></a><span class='hs-comment'>-- https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/DeriveFunctor#AlternativestrategyforderivingFoldableandTraversable</span>
<a name="line-1144"></a><span class='hs-comment'>-- for further discussion.</span>
<a name="line-1145"></a>
<a name="line-1146"></a><a name="bifoldrCombine"></a><span class='hs-definition'>bifoldrCombine</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-1147"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1148"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1149"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-1150"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1151"></a><span class='hs-definition'>bifoldrCombine</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>zName</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-varid'>foldr</span> <span class='hs-conid'>AppE</span> <span class='hs-layout'>(</span><span class='hs-conid'>VarE</span> <span class='hs-varid'>zName</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>rights</span><span class='hs-layout'>)</span>
<a name="line-1152"></a>
<a name="line-1153"></a><a name="bifoldMapCombine"></a><span class='hs-definition'>bifoldMapCombine</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-1154"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1155"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1156"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-1157"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1158"></a><span class='hs-definition'>bifoldMapCombine</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fmap</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varop'>.</span> <span class='hs-varid'>rights</span><span class='hs-layout'>)</span>
<a name="line-1159"></a> <span class='hs-keyword'>where</span>
<a name="line-1160"></a> <span class='hs-varid'>go</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Exp</span>
<a name="line-1161"></a> <span class='hs-varid'>go</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>VarE</span> <span class='hs-varid'>memptyValName</span>
<a name="line-1162"></a> <span class='hs-varid'>go</span> <span class='hs-varid'>es</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr1</span> <span class='hs-layout'>(</span><span class='hs-conid'>AppE</span> <span class='hs-varop'>.</span> <span class='hs-conid'>AppE</span> <span class='hs-layout'>(</span><span class='hs-conid'>VarE</span> <span class='hs-varid'>mappendValName</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>es</span>
<a name="line-1163"></a>
<a name="line-1164"></a><a name="bitraverseCombine"></a><span class='hs-definition'>bitraverseCombine</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Name</span>
<a name="line-1165"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Name</span>
<a name="line-1166"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1167"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Either</span> <span class='hs-conid'>Exp</span> <span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span>
<a name="line-1168"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1169"></a><span class='hs-definition'>bitraverseCombine</span> <span class='hs-varid'>conName</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>args</span> <span class='hs-varid'>essQ</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1170"></a> <span class='hs-varid'>ess</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>essQ</span>
<a name="line-1171"></a>
<a name="line-1172"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>argTysTyVarInfo</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Bool</span><span class='hs-keyglyph'>]</span>
<a name="line-1173"></a> <span class='hs-varid'>argTysTyVarInfo</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>isRight</span> <span class='hs-varid'>ess</span>
<a name="line-1174"></a>
<a name="line-1175"></a> <span class='hs-varid'>argsWithTyVar</span><span class='hs-layout'>,</span> <span class='hs-varid'>argsWithoutTyVar</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Name</span><span class='hs-keyglyph'>]</span>
<a name="line-1176"></a> <span class='hs-layout'>(</span><span class='hs-varid'>argsWithTyVar</span><span class='hs-layout'>,</span> <span class='hs-varid'>argsWithoutTyVar</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>partitionByList</span> <span class='hs-varid'>argTysTyVarInfo</span> <span class='hs-varid'>args</span>
<a name="line-1177"></a>
<a name="line-1178"></a> <span class='hs-varid'>conExpQ</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Q</span> <span class='hs-conid'>Exp</span>
<a name="line-1179"></a> <span class='hs-varid'>conExpQ</span>
<a name="line-1180"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>null</span> <span class='hs-varid'>argsWithTyVar</span>
<a name="line-1181"></a> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>appsE</span> <span class='hs-layout'>(</span><span class='hs-varid'>conE</span> <span class='hs-varid'>conName</span><span class='hs-conop'>:</span><span class='hs-varid'>map</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>argsWithoutTyVar</span><span class='hs-layout'>)</span>
<a name="line-1182"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1183"></a> <span class='hs-varid'>bs</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newNameList</span> <span class='hs-str'>"b"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>length</span> <span class='hs-varid'>args</span>
<a name="line-1184"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>bs'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>filterByList</span> <span class='hs-varid'>argTysTyVarInfo</span> <span class='hs-varid'>bs</span>
<a name="line-1185"></a> <span class='hs-varid'>vars</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>filterByLists</span> <span class='hs-varid'>argTysTyVarInfo</span>
<a name="line-1186"></a> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>bs</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>varE</span> <span class='hs-varid'>args</span><span class='hs-layout'>)</span>
<a name="line-1187"></a> <span class='hs-varid'>lamE</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>varP</span> <span class='hs-varid'>bs'</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>appsE</span> <span class='hs-layout'>(</span><span class='hs-varid'>conE</span> <span class='hs-varid'>conName</span><span class='hs-conop'>:</span><span class='hs-varid'>vars</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-1188"></a>
<a name="line-1189"></a> <span class='hs-varid'>conExp</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>conExpQ</span>
<a name="line-1190"></a>
<a name="line-1191"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>go</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Exp</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Exp</span>
<a name="line-1192"></a> <span class='hs-varid'>go</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>VarE</span> <span class='hs-varid'>pureValName</span> <span class='hs-varop'>`AppE`</span> <span class='hs-varid'>conExp</span>
<a name="line-1193"></a> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-varid'>e</span><span class='hs-conop'>:</span><span class='hs-varid'>es</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldl'</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>e1</span> <span class='hs-varid'>e2</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>InfixE</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>e1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>VarE</span> <span class='hs-varid'>apValName</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>e2</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-1194"></a> <span class='hs-layout'>(</span><span class='hs-conid'>VarE</span> <span class='hs-varid'>fmapValName</span> <span class='hs-varop'>`AppE`</span> <span class='hs-varid'>conExp</span> <span class='hs-varop'>`AppE`</span> <span class='hs-varid'>e</span><span class='hs-layout'>)</span> <span class='hs-varid'>es</span>
<a name="line-1195"></a>
<a name="line-1196"></a> <span class='hs-varid'>return</span> <span class='hs-varop'>.</span> <span class='hs-varid'>go</span> <span class='hs-varop'>.</span> <span class='hs-varid'>rights</span> <span class='hs-varop'>$</span> <span class='hs-varid'>ess</span>
<a name="line-1197"></a>
<a name="line-1198"></a><span class='hs-comment'>{-
<a name="line-1199"></a>Note [biFunTriv for Bifoldable and Bitraversable]
<a name="line-1200"></a>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a name="line-1201"></a>When deriving Bifoldable and Bitraversable, we filter out any subexpressions whose
<a name="line-1202"></a>type does not mention one of the last two type parameters. From this, you might
<a name="line-1203"></a>think that we don't need to implement biFunTriv for bifoldr, bifoldMap, or
<a name="line-1204"></a>bitraverse at all, but in fact we do need to. Imagine the following data type:
<a name="line-1205"></a>
<a name="line-1206"></a> data T a b = MkT a (T Int b)
<a name="line-1207"></a>
<a name="line-1208"></a>In a derived Bifoldable T instance, you would generate the following bifoldMap
<a name="line-1209"></a>definition:
<a name="line-1210"></a>
<a name="line-1211"></a> bifoldMap f g (MkT a1 a2) = f a1 <> bifoldMap (\_ -> mempty) g arg2
<a name="line-1212"></a>
<a name="line-1213"></a>You need to fill in biFunTriv (\_ -> mempty) as the first argument to the recursive
<a name="line-1214"></a>call to bifoldMap, since that is how the algorithm handles polymorphic recursion.
<a name="line-1215"></a>-}</span>
</pre></body>
</html>
|