fp-library 0.17.0

A functional programming library for Rust featuring your favourite higher-kinded types and type classes.
Documentation
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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
//! Deferred, non-memoized fallible computation with higher-kinded type support.
//!
//! The fallible counterpart to [`Thunk`](crate::types::Thunk). Each call to [`TryThunk::evaluate`] re-executes the computation and returns a [`Result`]. Supports borrowing and lifetime polymorphism. The corresponding brands are [`TryThunkBrand`](crate::brands::TryThunkBrand) (bifunctor), [`TryThunkErrAppliedBrand`](crate::brands::TryThunkErrAppliedBrand) (functor over the success value), and [`TryThunkOkAppliedBrand`](crate::brands::TryThunkOkAppliedBrand) (functor over the error value).

#[fp_macros::document_module]
mod inner {
	use {
		crate::{
			Apply,
			brands::{
				TryThunkBrand,
				TryThunkErrAppliedBrand,
				TryThunkOkAppliedBrand,
			},
			classes::{
				ApplyFirst,
				ApplySecond,
				Bifoldable,
				Bifunctor,
				CloneFn,
				Deferrable,
				Foldable,
				FoldableWithIndex,
				Functor,
				FunctorWithIndex,
				LazyConfig,
				Lift,
				LiftFn,
				MonadRec,
				Monoid,
				Pointed,
				Semiapplicative,
				Semigroup,
				Semimonad,
				WithIndex,
			},
			impl_kind,
			kinds::*,
			types::{
				ArcTryLazy,
				Lazy,
				RcTryLazy,
				Thunk,
				TryLazy,
				TrySendThunk,
				TryTrampoline,
			},
		},
		core::ops::ControlFlow,
		fp_macros::*,
		std::{
			fmt,
			sync::Arc,
		},
	};

	/// A deferred computation that may fail with error type `E`.
	///
	/// This is [`Thunk<'a, Result<A, E>>`] with ergonomic combinators for error handling.
	/// Like [`Thunk`], this is NOT memoized. Each [`TryThunk::evaluate`] re-executes.
	/// Unlike [`Thunk`], the result is [`Result<A, E>`].
	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the value produced by the computation on success.",
		"The type of the error produced by the computation on failure."
	)]
	///
	/// ### Higher-Kinded Type Representation
	///
	/// This type has multiple higher-kinded representations:
	/// - [`TryThunkBrand`](crate::brands::TryThunkBrand): fully polymorphic over both error and success types (bifunctor).
	/// - [`TryThunkErrAppliedBrand<E>`](crate::brands::TryThunkErrAppliedBrand): the error type is fixed, polymorphic over the success type (functor over `Ok`).
	/// - [`TryThunkOkAppliedBrand<A>`](crate::brands::TryThunkOkAppliedBrand): the success type is fixed, polymorphic over the error type (functor over `Err`).
	///
	/// ### When to Use
	///
	/// Use `TryThunk` for lightweight fallible deferred computation with full HKT support.
	/// It is not stack-safe for deep [`bind`](TryThunk::bind) chains. For stack-safe fallible
	/// recursion, use [`TryTrampoline`](crate::types::TryTrampoline). For memoized fallible
	/// computation, use [`TryLazy`](crate::types::TryLazy).
	///
	/// ### Algebraic Properties
	///
	/// `TryThunk` forms a monad over the success type `A` (with `E` fixed):
	/// - `TryThunk::pure(a).bind(f).evaluate() == f(a).evaluate()` (left identity).
	/// - `thunk.bind(TryThunk::pure).evaluate() == thunk.evaluate()` (right identity).
	/// - `thunk.bind(f).bind(g).evaluate() == thunk.bind(|a| f(a).bind(g)).evaluate()` (associativity).
	///
	/// On the error channel, `bind` short-circuits: if the computation produces `Err(e)`,
	/// the continuation `f` is never called.
	///
	/// ### Stack Safety
	///
	/// `TryThunk::bind` chains are **not** stack-safe. Each nested [`bind`](TryThunk::bind)
	/// adds a frame to the call stack, so sufficiently deep chains will cause a stack overflow.
	/// For stack-safe fallible recursion, use [`TryTrampoline`](crate::types::TryTrampoline).
	///
	/// ### Limitations
	///
	/// **Cannot implement `Traversable`**: `TryThunk` wraps a `FnOnce` closure, which cannot be
	/// cloned because `FnOnce` is consumed when called. The [`Traversable`](crate::classes::Traversable)
	/// trait requires `Clone` bounds on the result type, making it fundamentally incompatible
	/// with `TryThunk`'s design. This mirrors the same limitation on [`Thunk`].
	pub struct TryThunk<'a, A, E>(
		/// The internal `Thunk` wrapping a `Result`.
		Thunk<'a, Result<A, E>>,
	);

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	#[document_parameters("The `TryThunk` instance.")]
	impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E> {
		/// Creates a new `TryThunk` from a thunk.
		#[document_signature]
		///
		#[document_parameters("The thunk to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::new(|| Ok(42));
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn new(f: impl FnOnce() -> Result<A, E> + 'a) -> Self {
			TryThunk(Thunk::new(f))
		}

		/// Returns a pure value (already computed).
		#[document_signature]
		///
		#[document_parameters("The value to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance containing the value.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn pure(a: A) -> Self {
			TryThunk(Thunk::pure(Ok(a)))
		}

		/// Defers a computation that returns a TryThunk.
		#[document_signature]
		///
		#[document_parameters("The thunk that returns a `TryThunk`.")]
		///
		#[document_returns("A new `TryThunk` instance.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::defer(|| TryThunk::ok(42));
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn defer(f: impl FnOnce() -> TryThunk<'a, A, E> + 'a) -> Self {
			TryThunk(Thunk::defer(move || f().0))
		}

		/// Alias for [`pure`](Self::pure), provided for readability.
		///
		/// Both `TryThunk::ok(x)` and `TryThunk::pure(x)` produce the same result: a
		/// deferred computation that succeeds with `x`. The `ok` variant mirrors the
		/// `Result::Ok` constructor name, making intent clearer when working directly
		/// with `TryThunk` values rather than through HKT abstractions.
		#[document_signature]
		///
		#[document_parameters("The value to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance containing the value.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn ok(a: A) -> Self {
			TryThunk(Thunk::pure(Ok(a)))
		}

		/// Returns a pure error.
		#[document_signature]
		///
		#[document_parameters("The error to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance containing the error.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, &str> = TryThunk::err("error");
		/// assert_eq!(try_thunk.evaluate(), Err("error"));
		/// ```
		#[inline]
		pub fn err(e: E) -> Self {
			TryThunk(Thunk::pure(Err(e)))
		}

		/// Monadic bind: chains computations.
		#[document_signature]
		///
		#[document_type_parameters("The type of the result of the new computation.")]
		///
		#[document_parameters("The function to apply to the result of the computation.")]
		///
		#[document_returns("A new `TryThunk` instance representing the chained computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).bind(|x| TryThunk::ok(x * 2));
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn bind<B: 'a>(
			self,
			f: impl FnOnce(A) -> TryThunk<'a, B, E> + 'a,
		) -> TryThunk<'a, B, E> {
			TryThunk(self.0.bind(|result| match result {
				Ok(a) => f(a).0,
				Err(e) => Thunk::pure(Err(e)),
			}))
		}

		/// Functor map: transforms the result.
		#[document_signature]
		///
		#[document_type_parameters("The type of the result of the transformation.")]
		///
		#[document_parameters("The function to apply to the result of the computation.")]
		///
		#[document_returns("A new `TryThunk` instance with the transformed result.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).map(|x| x * 2);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn map<B: 'a>(
			self,
			func: impl FnOnce(A) -> B + 'a,
		) -> TryThunk<'a, B, E> {
			TryThunk(self.0.map(|result| result.map(func)))
		}

		/// Map error: transforms the error.
		#[document_signature]
		///
		#[document_type_parameters("The type of the new error.")]
		///
		#[document_parameters("The function to apply to the error.")]
		///
		#[document_returns("A new `TryThunk` instance with the transformed error.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
		/// assert_eq!(try_thunk.evaluate(), Err(42));
		/// ```
		#[inline]
		pub fn map_err<E2: 'a>(
			self,
			f: impl FnOnce(E) -> E2 + 'a,
		) -> TryThunk<'a, A, E2> {
			TryThunk(self.0.map(|result| result.map_err(f)))
		}

		/// Recovers from an error.
		#[document_signature]
		///
		#[document_parameters("The function to apply to the error value.")]
		///
		#[document_returns("A new `TryThunk` that attempts to recover from failure.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, &str> = TryThunk::err("error").catch(|_| TryThunk::ok(42));
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn catch(
			self,
			f: impl FnOnce(E) -> TryThunk<'a, A, E> + 'a,
		) -> Self {
			TryThunk(self.0.bind(|result| match result {
				Ok(a) => Thunk::pure(Ok(a)),
				Err(e) => f(e).0,
			}))
		}

		/// Recovers from an error using a fallible recovery function that may produce a different error type.
		///
		/// Unlike [`catch`](TryThunk::catch), `catch_with` allows the recovery function to return a
		/// `TryThunk` with a different error type `E2`. On success, the value is passed through
		/// unchanged. On failure, the recovery function is applied to the error value and its result
		/// is evaluated.
		#[document_signature]
		///
		#[document_type_parameters("The error type produced by the recovery computation.")]
		///
		#[document_parameters("The monadic recovery function applied to the error value.")]
		///
		#[document_returns(
			"A new `TryThunk` that either passes through the success value or uses the result of the recovery computation."
		)]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let recovered: TryThunk<i32, i32> =
		/// 	TryThunk::<i32, &str>::err("error").catch_with(|_| TryThunk::err(42));
		/// assert_eq!(recovered.evaluate(), Err(42));
		///
		/// let ok: TryThunk<i32, i32> = TryThunk::<i32, &str>::ok(1).catch_with(|_| TryThunk::err(42));
		/// assert_eq!(ok.evaluate(), Ok(1));
		/// ```
		#[inline]
		pub fn catch_with<E2: 'a>(
			self,
			f: impl FnOnce(E) -> TryThunk<'a, A, E2> + 'a,
		) -> TryThunk<'a, A, E2> {
			TryThunk(Thunk::new(move || match self.evaluate() {
				Ok(a) => Ok(a),
				Err(e) => f(e).evaluate(),
			}))
		}

		/// Maps both the success and error values simultaneously.
		#[document_signature]
		///
		#[document_type_parameters(
			"The type of the new success value.",
			"The type of the new error value."
		)]
		///
		#[document_parameters(
			"The function to apply to the success value.",
			"The function to apply to the error value."
		)]
		///
		#[document_returns("A new `TryThunk` with both sides transformed.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let ok: TryThunk<i32, i32> = TryThunk::pure(5);
		/// assert_eq!(ok.bimap(|x| x * 2, |e| e + 1).evaluate(), Ok(10));
		///
		/// let err: TryThunk<i32, i32> = TryThunk::err(5);
		/// assert_eq!(err.bimap(|x| x * 2, |e| e + 1).evaluate(), Err(6));
		/// ```
		pub fn bimap<B: 'a, E2: 'a>(
			self,
			f: impl FnOnce(A) -> B + 'a,
			g: impl FnOnce(E) -> E2 + 'a,
		) -> TryThunk<'a, B, E2> {
			TryThunk(self.0.map(|result| match result {
				Ok(a) => Ok(f(a)),
				Err(e) => Err(g(e)),
			}))
		}

		/// Forces evaluation and returns the result.
		#[document_signature]
		///
		#[document_returns("The result of the computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		#[inline]
		pub fn evaluate(self) -> Result<A, E> {
			self.0.evaluate()
		}

		/// Combines two `TryThunk`s, running both and combining their results.
		///
		/// Short-circuits on error: if `self` fails, `other` is never evaluated.
		#[document_signature]
		///
		#[document_type_parameters(
			"The type of the second computation's success value.",
			"The type of the combined result."
		)]
		///
		#[document_parameters("The second computation.", "The function to combine the results.")]
		///
		#[document_returns("A new `TryThunk` producing the combined result.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let t1: TryThunk<i32, String> = TryThunk::ok(10);
		/// let t2: TryThunk<i32, String> = TryThunk::ok(20);
		/// let t3 = t1.lift2(t2, |a, b| a + b);
		/// assert_eq!(t3.evaluate(), Ok(30));
		///
		/// let t4: TryThunk<i32, String> = TryThunk::err("fail".to_string());
		/// let t5: TryThunk<i32, String> = TryThunk::ok(20);
		/// let t6 = t4.lift2(t5, |a, b| a + b);
		/// assert_eq!(t6.evaluate(), Err("fail".to_string()));
		/// ```
		#[inline]
		pub fn lift2<B: 'a, C: 'a>(
			self,
			other: TryThunk<'a, B, E>,
			f: impl FnOnce(A, B) -> C + 'a,
		) -> TryThunk<'a, C, E> {
			self.bind(move |a| other.map(move |b| f(a, b)))
		}

		/// Sequences two `TryThunk`s, discarding the first result.
		///
		/// Short-circuits on error: if `self` fails, `other` is never evaluated.
		#[document_signature]
		///
		#[document_type_parameters("The type of the second computation's success value.")]
		///
		#[document_parameters("The second computation.")]
		///
		#[document_returns(
			"A new `TryThunk` that runs both computations and returns the result of the second."
		)]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let t1: TryThunk<i32, String> = TryThunk::ok(10);
		/// let t2: TryThunk<i32, String> = TryThunk::ok(20);
		/// let t3 = t1.then(t2);
		/// assert_eq!(t3.evaluate(), Ok(20));
		///
		/// let t4: TryThunk<i32, String> = TryThunk::err("fail".to_string());
		/// let t5: TryThunk<i32, String> = TryThunk::ok(20);
		/// let t6 = t4.then(t5);
		/// assert_eq!(t6.evaluate(), Err("fail".to_string()));
		/// ```
		#[inline]
		pub fn then<B: 'a>(
			self,
			other: TryThunk<'a, B, E>,
		) -> TryThunk<'a, B, E> {
			self.bind(move |_| other)
		}

		/// Performs tail-recursive monadic computation with error handling.
		///
		/// The step function `f` is called in a loop, avoiding stack growth.
		/// Each iteration evaluates `f(state)` and inspects the resulting
		/// [`Result`] and [`ControlFlow`]: `Ok(ControlFlow::Continue(next))` continues with
		/// `next`, `Ok(ControlFlow::Break(a))` breaks out and returns `Ok(a)`, and
		/// `Err(e)` short-circuits with `Err(e)`.
		///
		/// Unlike the [`MonadRec`] implementation for
		/// [`TryThunkErrAppliedBrand<E>`](crate::brands::TryThunkErrAppliedBrand),
		/// this method does not require `E: 'static`, so it works with
		/// borrowed error types like `&'a str`.
		///
		/// # Step Function
		///
		/// The function `f` is bounded by `Fn`, so it is callable multiple
		/// times by shared reference. Each iteration of the loop calls `f`
		/// without consuming it.
		#[document_signature]
		///
		#[document_type_parameters("The type of the loop state.")]
		///
		#[document_parameters(
			"The step function that produces the next state, the final result, or an error.",
			"The initial state."
		)]
		///
		#[document_returns("A `TryThunk` that, when evaluated, runs the tail-recursive loop.")]
		///
		#[document_examples]
		///
		/// ```
		/// use {
		/// 	core::ops::ControlFlow,
		/// 	fp_library::types::*,
		/// };
		///
		/// let result: TryThunk<i32, &str> = TryThunk::tail_rec_m(
		/// 	|x| {
		/// 		TryThunk::ok(
		/// 			if x < 1000 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
		/// 		)
		/// 	},
		/// 	0,
		/// );
		/// assert_eq!(result.evaluate(), Ok(1000));
		/// ```
		pub fn tail_rec_m<S>(
			f: impl Fn(S) -> TryThunk<'a, ControlFlow<A, S>, E> + 'a,
			initial: S,
		) -> Self
		where
			S: 'a, {
			TryThunk::new(move || {
				let mut state = initial;
				loop {
					match f(state).evaluate() {
						Ok(ControlFlow::Continue(next)) => state = next,
						Ok(ControlFlow::Break(a)) => break Ok(a),
						Err(e) => break Err(e),
					}
				}
			})
		}

		/// Arc-wrapped version of [`tail_rec_m`](TryThunk::tail_rec_m) for
		/// non-Clone closures.
		///
		/// Use this when your closure captures non-Clone state. The closure is
		/// wrapped in [`Arc`] internally, which provides the required `Clone`
		/// implementation. The step function must be `Send + Sync` because
		/// `Arc` requires these bounds.
		#[document_signature]
		///
		#[document_type_parameters("The type of the loop state.")]
		///
		#[document_parameters(
			"The step function that produces the next state, the final result, or an error.",
			"The initial state."
		)]
		///
		#[document_returns("A `TryThunk` that, when evaluated, runs the tail-recursive loop.")]
		///
		#[document_examples]
		///
		/// ```
		/// use {
		/// 	core::ops::ControlFlow,
		/// 	fp_library::types::*,
		/// 	std::sync::{
		/// 		Arc,
		/// 		atomic::{
		/// 			AtomicUsize,
		/// 			Ordering,
		/// 		},
		/// 	},
		/// };
		///
		/// let counter = Arc::new(AtomicUsize::new(0));
		/// let counter_clone = Arc::clone(&counter);
		/// let result: TryThunk<i32, ()> = TryThunk::arc_tail_rec_m(
		/// 	move |x| {
		/// 		counter_clone.fetch_add(1, Ordering::SeqCst);
		/// 		TryThunk::ok(if x < 100 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) })
		/// 	},
		/// 	0,
		/// );
		/// assert_eq!(result.evaluate(), Ok(100));
		/// assert_eq!(counter.load(Ordering::SeqCst), 101);
		/// ```
		pub fn arc_tail_rec_m<S>(
			f: impl Fn(S) -> TryThunk<'a, ControlFlow<A, S>, E> + Send + Sync + 'a,
			initial: S,
		) -> Self
		where
			S: 'a, {
			let f = Arc::new(f);
			let wrapper = move |s: S| {
				let f = Arc::clone(&f);
				f(s)
			};
			Self::tail_rec_m(wrapper, initial)
		}

		/// Converts this `TryThunk` into a memoized [`RcTryLazy`].
		///
		/// The resulting `RcTryLazy` will evaluate the computation on first
		/// access and cache the result for subsequent accesses.
		#[document_signature]
		///
		#[document_returns("A memoized `RcTryLazy` wrapping this computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		/// let lazy: RcTryLazy<i32, ()> = thunk.into_rc_try_lazy();
		/// assert_eq!(lazy.evaluate(), Ok(&42));
		/// ```
		#[inline]
		pub fn into_rc_try_lazy(self) -> RcTryLazy<'a, A, E> {
			RcTryLazy::from(self)
		}

		/// Evaluates this `TryThunk` and wraps the result in a thread-safe [`ArcTryLazy`].
		///
		/// The thunk is evaluated eagerly because its inner closure is not
		/// `Send`. The result is stored in an `ArcTryLazy` for thread-safe sharing.
		#[document_signature]
		///
		#[document_returns("A thread-safe memoized `ArcTryLazy` wrapping this computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		/// let lazy: ArcTryLazy<i32, ()> = thunk.into_arc_try_lazy();
		/// assert_eq!(lazy.evaluate(), Ok(&42));
		/// ```
		#[inline]
		pub fn into_arc_try_lazy(self) -> ArcTryLazy<'a, A, E>
		where
			A: Send + Sync + 'a,
			E: Send + Sync + 'a, {
			let result = self.evaluate();
			ArcTryLazy::new(move || result)
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the computed value.",
		"The type of the error value."
	)]
	#[document_parameters("The `TryThunk` to operate on.")]
	impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E> {
		/// Creates a `TryThunk` that catches unwinds (panics), converting the
		/// panic payload using a custom conversion function.
		///
		/// The closure `f` is executed when the thunk is evaluated. If `f`
		/// panics, the panic payload is passed to `handler` to produce the
		/// error value. If `f` returns normally, the value is wrapped in `Ok`.
		#[document_signature]
		///
		#[document_parameters(
			"The closure that might panic.",
			"The function that converts a panic payload into the error type."
		)]
		///
		#[document_returns(
			"A new `TryThunk` instance where panics are converted to `Err(E)` via the handler."
		)]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let thunk = TryThunk::<i32, i32>::catch_unwind_with(
		/// 	|| {
		/// 		if true {
		/// 			panic!("oops")
		/// 		}
		/// 		42
		/// 	},
		/// 	|_payload| -1,
		/// );
		/// assert_eq!(thunk.evaluate(), Err(-1));
		/// ```
		pub fn catch_unwind_with(
			f: impl FnOnce() -> A + std::panic::UnwindSafe + 'a,
			handler: impl FnOnce(Box<dyn std::any::Any + Send>) -> E + 'a,
		) -> Self {
			TryThunk::new(move || std::panic::catch_unwind(f).map_err(handler))
		}

		/// Unwraps the newtype, returning the inner `Thunk<'a, Result<A, E>>`.
		#[document_signature]
		///
		#[document_returns("The underlying `Thunk` that produces a `Result`.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
		/// let inner = try_thunk.into_inner();
		/// assert_eq!(inner.evaluate(), Ok(42));
		/// ```
		pub fn into_inner(self) -> Thunk<'a, Result<A, E>> {
			self.0
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the computed value."
	)]
	impl<'a, A: 'a> TryThunk<'a, A, String> {
		/// Creates a `TryThunk` that catches unwinds (panics).
		///
		/// The closure is executed when the thunk is evaluated. If the closure
		/// panics, the panic payload is converted to a `String` error. If the
		/// closure returns normally, the value is wrapped in `Ok`.
		///
		/// This is a convenience wrapper around [`catch_unwind_with`](TryThunk::catch_unwind_with)
		/// that uses the default panic payload to string conversion.
		#[document_signature]
		///
		#[document_parameters("The closure that might panic.")]
		///
		#[document_returns(
			"A new `TryThunk` instance where panics are converted to `Err(String)`."
		)]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let thunk = TryThunk::<i32, String>::catch_unwind(|| {
		/// 	if true {
		/// 		panic!("oops")
		/// 	}
		/// 	42
		/// });
		/// assert_eq!(thunk.evaluate(), Err("oops".to_string()));
		/// ```
		pub fn catch_unwind(f: impl FnOnce() -> A + std::panic::UnwindSafe + 'a) -> Self {
			Self::catch_unwind_with(f, crate::utils::panic_payload_to_string)
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value.",
		"The memoization configuration."
	)]
	impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>
	where
		A: Clone + 'a,
		E: 'a,
		Config: LazyConfig,
	{
		#[document_signature]
		#[document_parameters("The lazy value to convert.")]
		#[document_returns("A new `TryThunk` instance that wraps the lazy value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let lazy = Lazy::<_, RcLazyConfig>::pure(42);
		/// let thunk: TryThunk<i32, ()> = TryThunk::from(lazy);
		/// assert_eq!(thunk.evaluate(), Ok(42));
		/// ```
		fn from(memo: Lazy<'a, A, Config>) -> Self {
			TryThunk::new(move || Ok(memo.evaluate().clone()))
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value.",
		"The memoization configuration."
	)]
	impl<'a, A, E, Config> From<TryLazy<'a, A, E, Config>> for TryThunk<'a, A, E>
	where
		A: Clone + 'a,
		E: Clone + 'a,
		Config: LazyConfig,
	{
		/// Converts a [`TryLazy`] value into a [`TryThunk`] by cloning the memoized result.
		///
		/// This conversion clones both the success and error values on each evaluation.
		/// The cost depends on the [`Clone`] implementations of `A` and `E`.
		#[document_signature]
		#[document_parameters("The fallible lazy value to convert.")]
		#[document_returns("A new `TryThunk` instance that wraps the fallible lazy value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let lazy = TryLazy::<_, _, RcLazyConfig>::new(|| Ok::<i32, ()>(42));
		/// let thunk = TryThunk::from(lazy);
		/// assert_eq!(thunk.evaluate(), Ok(42));
		/// ```
		fn from(memo: TryLazy<'a, A, E, Config>) -> Self {
			TryThunk::new(move || memo.evaluate().cloned().map_err(Clone::clone))
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	impl<'a, A: 'a, E: 'a> From<Thunk<'a, A>> for TryThunk<'a, A, E> {
		#[document_signature]
		#[document_parameters("The thunk to convert.")]
		#[document_returns("A new `TryThunk` instance that wraps the thunk.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let thunk = Thunk::new(|| 42);
		/// let try_thunk: TryThunk<i32, ()> = TryThunk::from(thunk);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		fn from(eval: Thunk<'a, A>) -> Self {
			TryThunk(eval.map(Ok))
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	impl<'a, A: 'a, E: 'a> From<Result<A, E>> for TryThunk<'a, A, E> {
		#[document_signature]
		#[document_parameters("The result to convert.")]
		#[document_returns("A new `TryThunk` instance that produces the result.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let ok_thunk: TryThunk<i32, String> = TryThunk::from(Ok(42));
		/// assert_eq!(ok_thunk.evaluate(), Ok(42));
		///
		/// let err_thunk: TryThunk<i32, String> = TryThunk::from(Err("error".to_string()));
		/// assert_eq!(err_thunk.evaluate(), Err("error".to_string()));
		/// ```
		fn from(result: Result<A, E>) -> Self {
			TryThunk(Thunk::pure(result))
		}
	}

	#[document_type_parameters("The type of the success value.", "The type of the error value.")]
	impl<A: 'static, E: 'static> From<TryTrampoline<A, E>> for TryThunk<'static, A, E> {
		/// Converts a [`TryTrampoline`] into a `TryThunk`.
		///
		/// The resulting `TryThunk` will evaluate the trampoline when forced.
		#[document_signature]
		#[document_parameters("The fallible trampoline to convert.")]
		#[document_returns("A new `TryThunk` instance that evaluates the trampoline.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		///
		/// let tramp: TryTrampoline<i32, String> = TryTrampoline::ok(42);
		/// let thunk: TryThunk<i32, String> = TryThunk::from(tramp);
		/// assert_eq!(thunk.evaluate(), Ok(42));
		/// ```
		fn from(tramp: TryTrampoline<A, E>) -> Self {
			TryThunk::new(move || tramp.evaluate())
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	impl<'a, A: 'a, E: 'a> From<TrySendThunk<'a, A, E>> for TryThunk<'a, A, E> {
		/// Converts a [`TrySendThunk`] into a [`TryThunk`] by erasing the `Send` bound.
		///
		/// This delegates to the [`SendThunk`](crate::types::SendThunk) to
		/// [`Thunk`] conversion, which is a zero-cost unsizing coercion: the inner
		/// `Box<dyn FnOnce() -> Result<A, E> + Send + 'a>` is coerced to
		/// `Box<dyn FnOnce() -> Result<A, E> + 'a>`.
		#[document_signature]
		#[document_parameters("The send try-thunk to convert.")]
		#[document_returns("A `TryThunk` wrapping the same deferred computation.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let send_thunk: TrySendThunk<i32, ()> = TrySendThunk::pure(42);
		/// let thunk: TryThunk<i32, ()> = TryThunk::from(send_thunk);
		/// assert_eq!(thunk.evaluate(), Ok(42));
		/// ```
		fn from(send_thunk: TrySendThunk<'a, A, E>) -> Self {
			TryThunk(Thunk::from(send_thunk.into_inner()))
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	impl<'a, A, E> Deferrable<'a> for TryThunk<'a, A, E>
	where
		A: 'a,
		E: 'a,
	{
		/// Creates a `TryThunk` from a computation that produces it.
		#[document_signature]
		///
		#[document_parameters("A thunk that produces the try thunk.")]
		///
		#[document_returns("The deferred try thunk.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::Deferrable,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let task: TryThunk<i32, ()> = Deferrable::defer(|| TryThunk::ok(42));
		/// assert_eq!(task.evaluate(), Ok(42));
		/// ```
		fn defer(f: impl FnOnce() -> Self + 'a) -> Self
		where
			Self: Sized, {
			TryThunk::defer(f)
		}
	}

	impl_kind! {
		#[multi_brand]
		impl<E: 'static> for TryThunkErrAppliedBrand<E> {
			#[document_default]
			type Of<'a, A: 'a>: 'a = TryThunk<'a, A, E>;
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Functor for TryThunkErrAppliedBrand<E> {
		/// Maps a function over the result of a `TryThunk` computation.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the value inside the `TryThunk`.",
			"The type of the result of the transformation."
		)]
		///
		#[document_parameters(
			"The function to apply to the result of the computation.",
			"The `TryThunk` instance."
		)]
		///
		#[document_returns("A new `TryThunk` instance with the transformed result.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let mapped = explicit::map::<TryThunkErrAppliedBrand<()>, _, _, _, _>(|x| x * 2, try_thunk);
		/// assert_eq!(mapped.evaluate(), Ok(20));
		/// ```
		fn map<'a, A: 'a, B: 'a>(
			func: impl Fn(A) -> B + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
			fa.map(func)
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Pointed for TryThunkErrAppliedBrand<E> {
		/// Wraps a value in a `TryThunk` context.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the value to wrap."
		)]
		///
		#[document_parameters("The value to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance containing the value.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(42);
		/// assert_eq!(try_thunk.evaluate(), Ok(42));
		/// ```
		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
			TryThunk::ok(a)
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Lift for TryThunkErrAppliedBrand<E> {
		/// Lifts a binary function into the `TryThunk` context.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the first value.",
			"The type of the second value.",
			"The type of the result."
		)]
		///
		#[document_parameters(
			"The binary function to apply.",
			"The first `TryThunk`.",
			"The second `TryThunk`."
		)]
		///
		#[document_returns(
			"A new `TryThunk` instance containing the result of applying the function."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let eval1: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let eval2: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(20);
		/// let result = explicit::lift2::<TryThunkErrAppliedBrand<()>, _, _, _, _, _, _>(
		/// 	|a, b| a + b,
		/// 	eval1,
		/// 	eval2,
		/// );
		/// assert_eq!(result.evaluate(), Ok(30));
		/// ```
		fn lift2<'a, A, B, C>(
			func: impl Fn(A, B) -> C + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
		where
			A: Clone + 'a,
			B: Clone + 'a,
			C: 'a, {
			fa.bind(move |a| fb.map(move |b| func(a, b)))
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> ApplyFirst for TryThunkErrAppliedBrand<E> {}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> ApplySecond for TryThunkErrAppliedBrand<E> {}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Semiapplicative for TryThunkErrAppliedBrand<E> {
		/// Applies a function wrapped in `TryThunk` to a value wrapped in `TryThunk`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function wrapper.",
			"The type of the input.",
			"The type of the result."
		)]
		///
		#[document_parameters(
			"The `TryThunk` containing the function.",
			"The `TryThunk` containing the value."
		)]
		///
		#[document_returns(
			"A new `TryThunk` instance containing the result of applying the function."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::semiapplicative::apply as explicit_apply,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let func: TryThunk<_, ()> =
		/// 	pure::<TryThunkErrAppliedBrand<()>, _>(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
		/// let val: TryThunk<_, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(21);
		/// let result = explicit_apply::<RcFnBrand, TryThunkErrAppliedBrand<()>, _, _>(func, val);
		/// assert_eq!(result.evaluate(), Ok(42));
		/// ```
		fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
			ff.bind(move |f| {
				fa.map(
					#[expect(clippy::redundant_closure, reason = "Required for move semantics")]
					move |a| f(a),
				)
			})
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Semimonad for TryThunkErrAppliedBrand<E> {
		/// Chains `TryThunk` computations.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the result of the first computation.",
			"The type of the result of the new computation."
		)]
		///
		#[document_parameters(
			"The first `TryThunk`.",
			"The function to apply to the result of the computation."
		)]
		///
		#[document_returns("A new `TryThunk` instance representing the chained computation.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let result = explicit::bind::<TryThunkErrAppliedBrand<()>, _, _, _, _>(try_thunk, |x| {
		/// 	pure::<TryThunkErrAppliedBrand<()>, _>(x * 2)
		/// });
		/// assert_eq!(result.evaluate(), Ok(20));
		/// ```
		fn bind<'a, A: 'a, B: 'a>(
			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
			ma.bind(func)
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> MonadRec for TryThunkErrAppliedBrand<E> {
		/// Performs tail-recursive monadic computation.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the initial value and loop state.",
			"The type of the result."
		)]
		///
		#[document_parameters("The step function.", "The initial value.")]
		///
		#[document_returns("The result of the computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use {
		/// 	core::ops::ControlFlow,
		/// 	fp_library::{
		/// 		brands::*,
		/// 		classes::*,
		/// 		functions::*,
		/// 		types::*,
		/// 	},
		/// };
		///
		/// let result = tail_rec_m::<TryThunkErrAppliedBrand<()>, _, _>(
		/// 	|x| {
		/// 		pure::<TryThunkErrAppliedBrand<()>, _>(
		/// 			if x < 1000 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
		/// 		)
		/// 	},
		/// 	0,
		/// );
		/// assert_eq!(result.evaluate(), Ok(1000));
		/// ```
		fn tail_rec_m<'a, A: 'a, B: 'a>(
			f: impl Fn(
				A,
			)
				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
			+ 'a,
			a: A,
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
			TryThunk::new(move || {
				let mut current = a;
				loop {
					match f(current).evaluate() {
						Ok(ControlFlow::Continue(next)) => current = next,
						Ok(ControlFlow::Break(res)) => break Ok(res),
						Err(e) => break Err(e),
					}
				}
			})
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> Foldable for TryThunkErrAppliedBrand<E> {
		/// Folds the `TryThunk` from the right.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the accumulator."
		)]
		///
		#[document_parameters(
			"The function to apply to each element and the accumulator.",
			"The initial value of the accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("The final accumulator value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let result = explicit::fold_right::<RcFnBrand, TryThunkErrAppliedBrand<()>, _, _, _, _>(
		/// 	|a, b| a + b,
		/// 	5,
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, 15);
		/// ```
		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
			func: impl Fn(A, B) -> B + 'a,
			initial: B,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> B
		where
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Ok(a) => func(a, initial),
				Err(_) => initial,
			}
		}

		/// Folds the `TryThunk` from the left.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the accumulator."
		)]
		///
		#[document_parameters(
			"The function to apply to the accumulator and each element.",
			"The initial value of the accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("The final accumulator value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let result = explicit::fold_left::<RcFnBrand, TryThunkErrAppliedBrand<()>, _, _, _, _>(
		/// 	|b, a| b + a,
		/// 	5,
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, 15);
		/// ```
		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
			func: impl Fn(B, A) -> B + 'a,
			initial: B,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> B
		where
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Ok(a) => func(initial, a),
				Err(_) => initial,
			}
		}

		/// Maps the value to a monoid and returns it.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the monoid."
		)]
		///
		#[document_parameters("The mapping function.", "The TryThunk to fold.")]
		///
		#[document_returns("The monoid value.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(10);
		/// let result = explicit::fold_map::<RcFnBrand, TryThunkErrAppliedBrand<()>, _, _, _, _>(
		/// 	|a: i32| a.to_string(),
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, "10");
		/// ```
		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
			func: impl Fn(A) -> M + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> M
		where
			M: Monoid + 'a,
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Ok(a) => func(a),
				Err(_) => M::empty(),
			}
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The success value type.",
		"The error value type."
	)]
	impl<'a, A: Semigroup + 'a, E: 'a> Semigroup for TryThunk<'a, A, E> {
		/// Combines two `TryThunk`s by combining their results.
		#[document_signature]
		///
		#[document_parameters("The first `TryThunk`.", "The second `TryThunk`.")]
		///
		#[document_returns("A new `TryThunk` containing the combined result.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let t1: TryThunk<String, ()> = pure::<TryThunkErrAppliedBrand<()>, _>("Hello".to_string());
		/// let t2: TryThunk<String, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(" World".to_string());
		/// let t3 = append::<_>(t1, t2);
		/// assert_eq!(t3.evaluate(), Ok("Hello World".to_string()));
		/// ```
		fn append(
			a: Self,
			b: Self,
		) -> Self {
			TryThunk::new(move || {
				let a_val = a.evaluate()?;
				let b_val = b.evaluate()?;
				Ok(Semigroup::append(a_val, b_val))
			})
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The success value type.",
		"The error value type."
	)]
	impl<'a, A: Monoid + 'a, E: 'a> Monoid for TryThunk<'a, A, E> {
		/// Returns the identity `TryThunk`.
		#[document_signature]
		///
		#[document_returns("A `TryThunk` producing the identity value of `A`.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	classes::*,
		/// 	types::*,
		/// };
		///
		/// let t: TryThunk<String, ()> = TryThunk::empty();
		/// assert_eq!(t.evaluate(), Ok("".to_string()));
		/// ```
		fn empty() -> Self {
			TryThunk(Thunk::pure(Ok(Monoid::empty())))
		}
	}

	impl_kind! {
		/// HKT branding for the `TryThunk` type.
		///
		/// The type parameters for `Of` are ordered `E`, then `A` (Error, then Success).
		/// This follows the same convention as `ResultBrand`, matching functional
		/// programming expectations (like Haskell's `Either e a`) where the success
		/// type is the last parameter.
		for TryThunkBrand {
			type Of<'a, E: 'a, A: 'a>: 'a = TryThunk<'a, A, E>;
		}
	}

	impl Bifunctor for TryThunkBrand {
		/// Maps functions over the values in the `TryThunk`.
		///
		/// This method applies one function to the error value and another to the success value.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The type of the error value.",
			"The type of the mapped error value.",
			"The type of the success value.",
			"The type of the mapped success value."
		)]
		///
		#[document_parameters(
			"The function to apply to the error.",
			"The function to apply to the success.",
			"The `TryThunk` to map over."
		)]
		///
		#[document_returns("A new `TryThunk` containing the mapped values.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let x: TryThunk<i32, i32> = TryThunk::ok(5);
		/// assert_eq!(
		/// 	explicit::bimap::<TryThunkBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x).evaluate(),
		/// 	Ok(10)
		/// );
		///
		/// let y: TryThunk<i32, i32> = TryThunk::err(5);
		/// assert_eq!(
		/// 	explicit::bimap::<TryThunkBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y).evaluate(),
		/// 	Err(6)
		/// );
		/// ```
		fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
			f: impl Fn(A) -> B + 'a,
			g: impl Fn(C) -> D + 'a,
			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
			TryThunk(p.0.map(move |result| match result {
				Ok(c) => Ok(g(c)),
				Err(a) => Err(f(a)),
			}))
		}
	}

	impl Bifoldable for TryThunkBrand {
		/// Folds a `TryThunk` using two step functions, right-associatively.
		///
		/// Dispatches to `f` for error values and `g` for success values.
		/// The thunk is evaluated to determine which branch to fold.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The brand of the cloneable function to use.",
			"The error type (first position).",
			"The success type (second position).",
			"The accumulator type."
		)]
		///
		#[document_parameters(
			"The step function applied to the error value.",
			"The step function applied to the success value.",
			"The initial accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("`f(e, z)` for `Err(e)`, or `g(a, z)` for `Ok(a)`.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// assert_eq!(
		/// 	explicit::bi_fold_right::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
		/// 		10,
		/// 		TryThunk::err(3),
		/// 	),
		/// 	7
		/// );
		/// assert_eq!(
		/// 	explicit::bi_fold_right::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
		/// 		10,
		/// 		TryThunk::ok(5),
		/// 	),
		/// 	15
		/// );
		/// ```
		fn bi_fold_right<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
			f: impl Fn(A, C) -> C + 'a,
			g: impl Fn(B, C) -> C + 'a,
			z: C,
			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
		) -> C {
			match p.evaluate() {
				Err(a) => f(a, z),
				Ok(b) => g(b, z),
			}
		}

		/// Folds a `TryThunk` using two step functions, left-associatively.
		///
		/// Dispatches to `f` for error values and `g` for success values.
		/// The thunk is evaluated to determine which branch to fold.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The brand of the cloneable function to use.",
			"The error type (first position).",
			"The success type (second position).",
			"The accumulator type."
		)]
		///
		#[document_parameters(
			"The step function applied to the error value.",
			"The step function applied to the success value.",
			"The initial accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("`f(z, e)` for `Err(e)`, or `g(z, a)` for `Ok(a)`.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// assert_eq!(
		/// 	explicit::bi_fold_left::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
		/// 		10,
		/// 		TryThunk::err(3),
		/// 	),
		/// 	7
		/// );
		/// assert_eq!(
		/// 	explicit::bi_fold_left::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
		/// 		10,
		/// 		TryThunk::ok(5),
		/// 	),
		/// 	15
		/// );
		/// ```
		fn bi_fold_left<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
			f: impl Fn(C, A) -> C + 'a,
			g: impl Fn(C, B) -> C + 'a,
			z: C,
			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
		) -> C {
			match p.evaluate() {
				Err(a) => f(z, a),
				Ok(b) => g(z, b),
			}
		}

		/// Maps a `TryThunk`'s value to a monoid using two functions and returns the result.
		///
		/// Dispatches to `f` for error values and `g` for success values,
		/// returning the monoid value.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the values.",
			"The brand of the cloneable function to use.",
			"The error type (first position).",
			"The success type (second position).",
			"The monoid type."
		)]
		///
		#[document_parameters(
			"The function mapping the error to the monoid.",
			"The function mapping the success to the monoid.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("`f(e)` for `Err(e)`, or `g(a)` for `Ok(a)`.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// assert_eq!(
		/// 	explicit::bi_fold_map::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|e: i32| e.to_string(), |s: i32| s.to_string()),
		/// 		TryThunk::err(3),
		/// 	),
		/// 	"3".to_string()
		/// );
		/// assert_eq!(
		/// 	explicit::bi_fold_map::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
		/// 		(|e: i32| e.to_string(), |s: i32| s.to_string()),
		/// 		TryThunk::ok(5),
		/// 	),
		/// 	"5".to_string()
		/// );
		/// ```
		fn bi_fold_map<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
			f: impl Fn(A) -> M + 'a,
			g: impl Fn(B) -> M + 'a,
			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
		) -> M
		where
			M: Monoid + 'a, {
			match p.evaluate() {
				Err(a) => f(a),
				Ok(b) => g(b),
			}
		}
	}

	impl_kind! {
		#[multi_brand]
		/// HKT branding for `TryThunk` with the success type `A` fixed.
		///
		/// This is the "dual-channel" encoding for `TryThunk`:
		/// - [`TryThunkErrAppliedBrand<E>`] fixes the error type and is polymorphic over `Ok` values,
		///   giving a standard `Functor`/`Monad` that maps and chains success values.
		/// - `TryThunkOkAppliedBrand<A>` fixes the success type and is polymorphic over `Err` values,
		///   giving a `Functor`/`Monad` that maps and chains error values.
		///
		/// Together they allow the same `TryThunk<'a, A, E>` to participate in HKT abstractions
		/// on either channel. For example, `pure::<TryThunkErrAppliedBrand<E>, _>(x)` produces
		/// `Ok(x)`, while `pure::<TryThunkOkAppliedBrand<A>, _>(e)` produces `Err(e)`.
		impl<A: 'static> for TryThunkOkAppliedBrand<A> {
			#[document_default]
			type Of<'a, E: 'a>: 'a = TryThunk<'a, A, E>;
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Functor for TryThunkOkAppliedBrand<A> {
		/// Maps a function over the error value in the `TryThunk`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the error value inside the `TryThunk`.",
			"The type of the result of the transformation."
		)]
		///
		#[document_parameters("The function to apply to the error.", "The `TryThunk` instance.")]
		///
		#[document_returns("A new `TryThunk` instance with the transformed error.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let mapped = explicit::map::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(|x| x * 2, try_thunk);
		/// assert_eq!(mapped.evaluate(), Err(20));
		/// ```
		fn map<'a, E: 'a, E2: 'a>(
			func: impl Fn(E) -> E2 + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) {
			fa.map_err(func)
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Pointed for TryThunkOkAppliedBrand<A> {
		/// Wraps a value in a `TryThunk` context (as error).
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the value to wrap."
		)]
		///
		#[document_parameters("The value to wrap.")]
		///
		#[document_returns("A new `TryThunk` instance containing the value as an error.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(42);
		/// assert_eq!(try_thunk.evaluate(), Err(42));
		/// ```
		fn pure<'a, E: 'a>(e: E) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>) {
			TryThunk::err(e)
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Lift for TryThunkOkAppliedBrand<A> {
		/// Lifts a binary function into the `TryThunk` context (over error).
		///
		/// # Evaluation strategy
		///
		/// This implementation uses **fail-fast** semantics, consistent with
		/// [`bind`](TryThunk::bind): `fa` is evaluated first, and if it is `Ok`,
		/// the result is returned immediately without evaluating `fb`. If `fa` is
		/// `Err`, `fb` is evaluated next; if `fb` is `Ok`, that `Ok` is returned.
		/// The function is only called when both sides are `Err`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the first error value.",
			"The type of the second error value.",
			"The type of the result error value."
		)]
		///
		#[document_parameters(
			"The binary function to apply to the errors.",
			"The first `TryThunk`.",
			"The second `TryThunk`."
		)]
		///
		#[document_returns(
			"A new `TryThunk` instance containing the result of applying the function to the errors."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let eval1: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let eval2: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(20);
		/// let result = explicit::lift2::<TryThunkOkAppliedBrand<i32>, _, _, _, _, _, _>(
		/// 	|a, b| a + b,
		/// 	eval1,
		/// 	eval2,
		/// );
		/// assert_eq!(result.evaluate(), Err(30));
		/// ```
		fn lift2<'a, E1, E2, E3>(
			func: impl Fn(E1, E2) -> E3 + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E1>),
			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E3>)
		where
			E1: Clone + 'a,
			E2: Clone + 'a,
			E3: 'a, {
			TryThunk::new(move || match fa.evaluate() {
				Ok(a) => Ok(a),
				Err(e1) => match fb.evaluate() {
					Ok(a) => Ok(a),
					Err(e2) => Err(func(e1, e2)),
				},
			})
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> ApplyFirst for TryThunkOkAppliedBrand<A> {}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> ApplySecond for TryThunkOkAppliedBrand<A> {}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Semiapplicative for TryThunkOkAppliedBrand<A> {
		/// Applies a function wrapped in `TryThunk` (as error) to a value wrapped in `TryThunk` (as error).
		///
		/// # Evaluation strategy
		///
		/// This implementation uses **fail-fast** semantics, consistent with
		/// [`bind`](TryThunk::bind): `ff` is evaluated first, and if it is `Ok`,
		/// the result is returned immediately without evaluating `fa`. If `ff` is
		/// `Err`, `fa` is evaluated next; if `fa` is `Ok`, that `Ok` is returned.
		/// The function is only applied when both sides are `Err`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function wrapper.",
			"The type of the input error.",
			"The type of the result error."
		)]
		///
		#[document_parameters(
			"The `TryThunk` containing the function (in Err).",
			"The `TryThunk` containing the value (in Err)."
		)]
		///
		#[document_returns(
			"A new `TryThunk` instance containing the result of applying the function."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::semiapplicative::apply as explicit_apply,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let func: TryThunk<i32, _> =
		/// 	pure::<TryThunkOkAppliedBrand<i32>, _>(lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
		/// let val: TryThunk<i32, _> = pure::<TryThunkOkAppliedBrand<i32>, _>(21);
		/// let result = explicit_apply::<RcFnBrand, TryThunkOkAppliedBrand<i32>, _, _>(func, val);
		/// assert_eq!(result.evaluate(), Err(42));
		/// ```
		fn apply<'a, FnBrand: 'a + CloneFn, E1: 'a + Clone, E2: 'a>(
			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, E1, E2>>),
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E1>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) {
			TryThunk::new(move || match ff.evaluate() {
				Ok(a) => Ok(a),
				Err(f) => match fa.evaluate() {
					Ok(a) => Ok(a),
					Err(e) => Err(f(e)),
				},
			})
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Semimonad for TryThunkOkAppliedBrand<A> {
		/// Chains `TryThunk` computations (over error).
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the result of the first computation (error).",
			"The type of the result of the new computation (error)."
		)]
		///
		#[document_parameters(
			"The first `TryThunk`.",
			"The function to apply to the error result of the computation."
		)]
		///
		#[document_returns("A new `TryThunk` instance representing the chained computation.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let result = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(try_thunk, |x| {
		/// 	pure::<TryThunkOkAppliedBrand<i32>, _>(x * 2)
		/// });
		/// assert_eq!(result.evaluate(), Err(20));
		/// ```
		fn bind<'a, E1: 'a, E2: 'a>(
			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E1>),
			func: impl Fn(E1) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) + 'a,
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) {
			TryThunk::new(move || match ma.evaluate() {
				Ok(a) => Ok(a),
				Err(e) => func(e).evaluate(),
			})
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> MonadRec for TryThunkOkAppliedBrand<A> {
		/// Performs tail-recursive monadic computation over the error channel.
		///
		/// The step function returns `TryThunk<A, ControlFlow<E2, E>>`. The loop
		/// continues while the error is `ControlFlow::Continue`, terminates with the
		/// final error on `ControlFlow::Break`, and short-circuits on `Ok`.
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the initial value and loop state.",
			"The type of the result."
		)]
		///
		#[document_parameters("The step function.", "The initial value.")]
		///
		#[document_returns("The result of the computation.")]
		///
		#[document_examples]
		///
		/// ```
		/// use {
		/// 	core::ops::ControlFlow,
		/// 	fp_library::{
		/// 		brands::*,
		/// 		classes::*,
		/// 		functions::*,
		/// 		types::*,
		/// 	},
		/// };
		///
		/// let result = tail_rec_m::<TryThunkOkAppliedBrand<i32>, _, _>(
		/// 	|x| {
		/// 		pure::<TryThunkOkAppliedBrand<i32>, _>(
		/// 			if x < 1000 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
		/// 		)
		/// 	},
		/// 	0,
		/// );
		/// assert_eq!(result.evaluate(), Err(1000));
		/// ```
		fn tail_rec_m<'a, E: 'a, E2: 'a>(
			f: impl Fn(
				E,
			)
				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<E2, E>>)
			+ 'a,
			e: E,
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) {
			TryThunk::new(move || {
				let mut current = e;
				loop {
					match f(current).evaluate() {
						Err(ControlFlow::Continue(next)) => current = next,
						Err(ControlFlow::Break(res)) => break Err(res),
						Ok(a) => break Ok(a),
					}
				}
			})
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> Foldable for TryThunkOkAppliedBrand<A> {
		/// Folds the `TryThunk` from the right (over error).
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the accumulator."
		)]
		///
		#[document_parameters(
			"The function to apply to each element and the accumulator.",
			"The initial value of the accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("The final accumulator value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let result = explicit::fold_right::<RcFnBrand, TryThunkOkAppliedBrand<i32>, _, _, _, _>(
		/// 	|a, b| a + b,
		/// 	5,
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, 15);
		/// ```
		fn fold_right<'a, FnBrand, E: 'a + Clone, B: 'a>(
			func: impl Fn(E, B) -> B + 'a,
			initial: B,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> B
		where
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Err(e) => func(e, initial),
				Ok(_) => initial,
			}
		}

		/// Folds the `TryThunk` from the left (over error).
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the accumulator."
		)]
		///
		#[document_parameters(
			"The function to apply to the accumulator and each element.",
			"The initial value of the accumulator.",
			"The `TryThunk` to fold."
		)]
		///
		#[document_returns("The final accumulator value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let result = explicit::fold_left::<RcFnBrand, TryThunkOkAppliedBrand<i32>, _, _, _, _>(
		/// 	|b, a| b + a,
		/// 	5,
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, 15);
		/// ```
		fn fold_left<'a, FnBrand, E: 'a + Clone, B: 'a>(
			func: impl Fn(B, E) -> B + 'a,
			initial: B,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> B
		where
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Err(e) => func(initial, e),
				Ok(_) => initial,
			}
		}

		/// Maps the value to a monoid and returns it (over error).
		#[document_signature]
		///
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the elements in the structure.",
			"The type of the monoid."
		)]
		///
		#[document_parameters("The mapping function.", "The TryThunk to fold.")]
		///
		#[document_returns("The monoid value.")]
		///
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	functions::*,
		/// 	types::*,
		/// };
		///
		/// let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(10);
		/// let result = explicit::fold_map::<RcFnBrand, TryThunkOkAppliedBrand<i32>, _, _, _, _>(
		/// 	|a: i32| a.to_string(),
		/// 	try_thunk,
		/// );
		/// assert_eq!(result, "10");
		/// ```
		fn fold_map<'a, FnBrand, E: 'a + Clone, M>(
			func: impl Fn(E) -> M + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> M
		where
			M: Monoid + 'a,
			FnBrand: CloneFn + 'a, {
			match fa.evaluate() {
				Err(e) => func(e),
				Ok(_) => M::empty(),
			}
		}
	}

	#[document_type_parameters(
		"The lifetime of the computation.",
		"The type of the success value.",
		"The type of the error value."
	)]
	#[document_parameters("The try-thunk to format.")]
	impl<'a, A, E> fmt::Debug for TryThunk<'a, A, E> {
		/// Formats the try-thunk without evaluating it.
		#[document_signature]
		#[document_parameters("The formatter.")]
		#[document_returns("The formatting result.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::types::*;
		/// let thunk = TryThunk::new(|| Ok::<i32, ()>(42));
		/// assert_eq!(format!("{:?}", thunk), "TryThunk(<unevaluated>)");
		/// ```
		fn fmt(
			&self,
			f: &mut fmt::Formatter<'_>,
		) -> fmt::Result {
			f.write_str("TryThunk(<unevaluated>)")
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> WithIndex for TryThunkErrAppliedBrand<E> {
		type Index = ();
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> FunctorWithIndex for TryThunkErrAppliedBrand<E> {
		/// Maps a function over the success value in the `TryThunk`, providing the index `()`.
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the success value inside the `TryThunk`.",
			"The type of the result of applying the function."
		)]
		#[document_parameters(
			"The function to apply to the value and its index.",
			"The `TryThunk` to map over."
		)]
		#[document_returns(
			"A new `TryThunk` containing the result of applying the function, or the original error."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::TryThunkErrAppliedBrand,
		/// 	classes::functor_with_index::FunctorWithIndex,
		/// 	types::*,
		/// };
		///
		/// let x: TryThunk<i32, ()> = TryThunk::pure(5);
		/// let y = <TryThunkErrAppliedBrand<()> as FunctorWithIndex>::map_with_index(|_, i| i * 2, x);
		/// assert_eq!(y.evaluate(), Ok(10));
		/// ```
		fn map_with_index<'a, A: 'a, B: 'a>(
			f: impl Fn((), A) -> B + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
			fa.map(move |a| f((), a))
		}
	}

	#[document_type_parameters("The error type.")]
	impl<E: 'static> FoldableWithIndex for TryThunkErrAppliedBrand<E> {
		/// Folds the `TryThunk` using a monoid, providing the index `()`.
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the success value inside the `TryThunk`.",
			"The monoid type."
		)]
		#[document_parameters(
			"The function to apply to the value and its index.",
			"The `TryThunk` to fold."
		)]
		#[document_returns("The monoid value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::foldable_with_index::FoldableWithIndex,
		/// 	types::*,
		/// };
		///
		/// let x: TryThunk<i32, ()> = TryThunk::pure(5);
		/// let y = <TryThunkErrAppliedBrand<()> as FoldableWithIndex>::fold_map_with_index::<
		/// 	RcFnBrand,
		/// 	_,
		/// 	_,
		/// >(|_, i: i32| i.to_string(), x);
		/// assert_eq!(y, "5".to_string());
		/// ```
		fn fold_map_with_index<'a, FnBrand, A: 'a + Clone, R: Monoid>(
			f: impl Fn((), A) -> R + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
		) -> R
		where
			FnBrand: LiftFn + 'a, {
			match fa.evaluate() {
				Ok(a) => f((), a),
				Err(_) => R::empty(),
			}
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> WithIndex for TryThunkOkAppliedBrand<A> {
		type Index = ();
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> FunctorWithIndex for TryThunkOkAppliedBrand<A> {
		/// Maps a function over the error value in the `TryThunk`, providing the index `()`.
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The type of the error value inside the `TryThunk`.",
			"The type of the result of applying the function."
		)]
		#[document_parameters(
			"The function to apply to the error and its index.",
			"The `TryThunk` to map over."
		)]
		#[document_returns(
			"A new `TryThunk` containing the original success or the transformed error."
		)]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::TryThunkOkAppliedBrand,
		/// 	classes::functor_with_index::FunctorWithIndex,
		/// 	types::*,
		/// };
		///
		/// let x: TryThunk<i32, i32> = TryThunk::err(5);
		/// let y = <TryThunkOkAppliedBrand<i32> as FunctorWithIndex>::map_with_index(|_, e| e * 2, x);
		/// assert_eq!(y.evaluate(), Err(10));
		/// ```
		fn map_with_index<'a, E: 'a, E2: 'a>(
			f: impl Fn((), E) -> E2 + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E2>) {
			fa.map_err(move |e| f((), e))
		}
	}

	#[document_type_parameters("The success type.")]
	impl<A: 'static> FoldableWithIndex for TryThunkOkAppliedBrand<A> {
		/// Folds the `TryThunk` over the error using a monoid, providing the index `()`.
		#[document_signature]
		#[document_type_parameters(
			"The lifetime of the computation.",
			"The brand of the cloneable function to use.",
			"The type of the error value inside the `TryThunk`.",
			"The monoid type."
		)]
		#[document_parameters(
			"The function to apply to the error and its index.",
			"The `TryThunk` to fold."
		)]
		#[document_returns("The monoid value.")]
		#[document_examples]
		///
		/// ```
		/// use fp_library::{
		/// 	brands::*,
		/// 	classes::foldable_with_index::FoldableWithIndex,
		/// 	types::*,
		/// };
		///
		/// let x: TryThunk<i32, i32> = TryThunk::err(5);
		/// let y = <TryThunkOkAppliedBrand<i32> as FoldableWithIndex>::fold_map_with_index::<
		/// 	RcFnBrand,
		/// 	_,
		/// 	_,
		/// >(|_, e: i32| e.to_string(), x);
		/// assert_eq!(y, "5".to_string());
		/// ```
		fn fold_map_with_index<'a, FnBrand, E: 'a + Clone, R: Monoid>(
			f: impl Fn((), E) -> R + 'a,
			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
		) -> R
		where
			FnBrand: LiftFn + 'a, {
			match fa.evaluate() {
				Err(e) => f((), e),
				Ok(_) => R::empty(),
			}
		}
	}
}
pub use inner::*;

#[cfg(test)]
#[expect(
	clippy::unwrap_used,
	clippy::panic,
	reason = "Tests use panicking operations for brevity and clarity"
)]
mod tests {
	use {
		super::*,
		crate::types::Thunk,
		quickcheck_macros::quickcheck,
	};

	/// Tests success path.
	///
	/// Verifies that `TryThunk::ok` creates a successful computation.
	#[test]
	fn test_success() {
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests that `TryThunk::pure` creates a successful computation.
	#[test]
	fn test_pure() {
		let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests failure path.
	///
	/// Verifies that `TryThunk::err` creates a failed computation.
	#[test]
	fn test_failure() {
		let try_thunk: TryThunk<i32, &str> = TryThunk::err("error");
		assert_eq!(try_thunk.evaluate(), Err("error"));
	}

	/// Tests `TryThunk::map`.
	///
	/// Verifies that `map` transforms the success value.
	#[test]
	fn test_map() {
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).map(|x| x * 2);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `TryThunk::map_err`.
	///
	/// Verifies that `map_err` transforms the error value.
	#[test]
	fn test_map_err() {
		let try_thunk: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
		assert_eq!(try_thunk.evaluate(), Err(42));
	}

	/// Tests `TryThunk::bind`.
	///
	/// Verifies that `bind` chains computations.
	#[test]
	fn test_bind() {
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).bind(|x| TryThunk::ok(x * 2));
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests borrowing in TryThunk.
	///
	/// Verifies that `TryThunk` can capture references.
	#[test]
	fn test_borrowing() {
		let x = 42;
		let try_thunk: TryThunk<&i32, ()> = TryThunk::new(|| Ok(&x));
		assert_eq!(try_thunk.evaluate(), Ok(&42));
	}

	/// Tests `TryThunk::bind` failure propagation.
	///
	/// Verifies that if the first computation fails, the second one is not executed.
	#[test]
	fn test_bind_failure() {
		let try_thunk = TryThunk::<i32, &str>::err("error").bind(|x| TryThunk::ok(x * 2));
		assert_eq!(try_thunk.evaluate(), Err("error"));
	}

	/// Tests `TryThunk::map` failure propagation.
	///
	/// Verifies that `map` is not executed if the computation fails.
	#[test]
	fn test_map_failure() {
		let try_thunk = TryThunk::<i32, &str>::err("error").map(|x| x * 2);
		assert_eq!(try_thunk.evaluate(), Err("error"));
	}

	/// Tests `TryThunk::map_err` success propagation.
	///
	/// Verifies that `map_err` is not executed if the computation succeeds.
	#[test]
	fn test_map_err_success() {
		let try_thunk = TryThunk::<i32, &str>::pure(42).map_err(|_| "new error");
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `From<Lazy>`.
	#[test]
	fn test_try_thunk_from_memo() {
		use crate::types::RcLazy;
		let memo = RcLazy::new(|| 42);
		let try_thunk: TryThunk<i32, ()> = TryThunk::from(memo);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `From<TryLazy>`.
	#[test]
	fn test_try_thunk_from_try_memo() {
		use crate::types::RcTryLazy;
		let memo = RcTryLazy::new(|| Ok(42));
		let try_thunk: TryThunk<i32, ()> = TryThunk::from(memo);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `Thunk::into_try`.
	///
	/// Verifies that `From<Thunk>` converts a `Thunk` into a `TryThunk` that succeeds.
	#[test]
	fn test_try_thunk_from_eval() {
		let eval = Thunk::pure(42);
		let try_thunk: TryThunk<i32, ()> = TryThunk::from(eval);
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `TryThunk::defer`.
	#[test]
	fn test_defer() {
		let try_thunk: TryThunk<i32, ()> = TryThunk::defer(|| TryThunk::ok(42));
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `TryThunk::catch`.
	///
	/// Verifies that `catch` recovers from failure.
	#[test]
	fn test_catch() {
		let try_thunk: TryThunk<i32, &str> = TryThunk::err("error").catch(|_| TryThunk::ok(42));
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `TryThunk::catch_with`.
	///
	/// Verifies that `catch_with` recovers from failure using a different error type.
	#[test]
	fn test_catch_with() {
		let recovered: TryThunk<i32, i32> =
			TryThunk::<i32, &str>::err("error").catch_with(|_| TryThunk::err(42));
		assert_eq!(recovered.evaluate(), Err(42));

		let ok: TryThunk<i32, i32> = TryThunk::<i32, &str>::ok(1).catch_with(|_| TryThunk::err(42));
		assert_eq!(ok.evaluate(), Ok(1));
	}

	/// Tests `TryThunkErrAppliedBrand` (Functor over Success).
	#[test]
	fn test_try_thunk_with_err_brand() {
		use crate::{
			brands::*,
			functions::*,
		};

		// Functor (map over success)
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(10);
		let mapped = explicit::map::<TryThunkErrAppliedBrand<()>, _, _, _, _>(|x| x * 2, try_thunk);
		assert_eq!(mapped.evaluate(), Ok(20));

		// Pointed (pure -> ok)
		let try_thunk: TryThunk<i32, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(42);
		assert_eq!(try_thunk.evaluate(), Ok(42));

		// Semimonad (bind over success)
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(10);
		let bound = explicit::bind::<TryThunkErrAppliedBrand<()>, _, _, _, _>(try_thunk, |x| {
			pure::<TryThunkErrAppliedBrand<()>, _>(x * 2)
		});
		assert_eq!(bound.evaluate(), Ok(20));

		// Foldable (fold over success)
		let try_thunk: TryThunk<i32, ()> = TryThunk::ok(10);
		let folded = explicit::fold_right::<RcFnBrand, TryThunkErrAppliedBrand<()>, _, _, _, _>(
			|x, acc| x + acc,
			5,
			try_thunk,
		);
		assert_eq!(folded, 15);
	}

	/// Tests `Bifunctor` for `TryThunkBrand`.
	#[test]
	fn test_bifunctor() {
		use crate::{
			brands::*,
			classes::bifunctor::*,
		};

		let x: TryThunk<i32, i32> = TryThunk::ok(5);
		assert_eq!(bimap::<TryThunkBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x).evaluate(), Ok(10));

		let y: TryThunk<i32, i32> = TryThunk::err(5);
		assert_eq!(bimap::<TryThunkBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y).evaluate(), Err(6));
	}

	/// Tests `Bifoldable` for `TryThunkBrand` with `bi_fold_right`.
	///
	/// Verifies that error values are folded with `f` and success values with `g`.
	#[test]
	fn test_bifoldable_right() {
		use crate::{
			brands::*,
			functions::*,
		};

		// Error case: f(3, 10) = 10 - 3 = 7
		assert_eq!(
			explicit::bi_fold_right::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
				10,
				TryThunk::err(3),
			),
			7
		);

		// Success case: g(5, 10) = 10 + 5 = 15
		assert_eq!(
			explicit::bi_fold_right::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|e: i32, acc| acc - e, |s: i32, acc| acc + s),
				10,
				TryThunk::ok(5),
			),
			15
		);
	}

	/// Tests `Bifoldable` for `TryThunkBrand` with `bi_fold_left`.
	///
	/// Verifies left-associative folding over both error and success values.
	#[test]
	fn test_bifoldable_left() {
		use crate::{
			brands::*,
			functions::*,
		};

		assert_eq!(
			explicit::bi_fold_left::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
				10,
				TryThunk::err(3),
			),
			7
		);

		assert_eq!(
			explicit::bi_fold_left::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|acc, e: i32| acc - e, |acc, s: i32| acc + s),
				10,
				TryThunk::ok(5),
			),
			15
		);
	}

	/// Tests `Bifoldable` for `TryThunkBrand` with `bi_fold_map`.
	///
	/// Verifies that both error and success values can be mapped to a monoid.
	#[test]
	fn test_bifoldable_map() {
		use crate::{
			brands::*,
			functions::*,
		};

		assert_eq!(
			explicit::bi_fold_map::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|e: i32| e.to_string(), |s: i32| s.to_string()),
				TryThunk::err(3),
			),
			"3".to_string()
		);

		assert_eq!(
			explicit::bi_fold_map::<RcFnBrand, TryThunkBrand, _, _, _, _, _>(
				(|e: i32| e.to_string(), |s: i32| s.to_string()),
				TryThunk::ok(5),
			),
			"5".to_string()
		);
	}

	/// Tests `MonadRec` for `TryThunkOkAppliedBrand` (tail recursion over error).
	///
	/// Verifies that the loop continues on `ControlFlow::Continue` and terminates on `ControlFlow::Break`.
	#[test]
	fn test_monad_rec_ok_applied() {
		use {
			crate::{
				brands::*,
				functions::*,
			},
			core::ops::ControlFlow,
		};

		let result = tail_rec_m::<TryThunkOkAppliedBrand<i32>, _, _>(
			|x| {
				pure::<TryThunkOkAppliedBrand<i32>, _>(
					if x < 100 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
				)
			},
			0,
		);
		assert_eq!(result.evaluate(), Err(100));
	}

	/// Tests `MonadRec` for `TryThunkOkAppliedBrand` short-circuits on `Ok`.
	///
	/// Verifies that encountering an `Ok` value terminates the loop immediately.
	#[test]
	fn test_monad_rec_ok_applied_short_circuit() {
		use {
			crate::{
				brands::*,
				functions::*,
			},
			core::ops::ControlFlow,
		};

		let result = tail_rec_m::<TryThunkOkAppliedBrand<i32>, _, _>(
			|x: i32| {
				if x == 5 {
					TryThunk::ok(42)
				} else {
					pure::<TryThunkOkAppliedBrand<i32>, _>(ControlFlow::<i32, i32>::Continue(x + 1))
				}
			},
			0,
		);
		assert_eq!(result.evaluate(), Ok(42));
	}

	/// Tests `catch_unwind` on `TryThunk`.
	///
	/// Verifies that panics are caught and converted to `Err(String)`.
	#[test]
	fn test_catch_unwind() {
		let thunk = TryThunk::<i32, String>::catch_unwind(|| {
			if true {
				panic!("oops")
			}
			42
		});
		assert_eq!(thunk.evaluate(), Err("oops".to_string()));
	}

	/// Tests `catch_unwind` on `TryThunk` with a non-panicking closure.
	///
	/// Verifies that a successful closure wraps the value in `Ok`.
	#[test]
	fn test_catch_unwind_success() {
		let thunk = TryThunk::<i32, String>::catch_unwind(|| 42);
		assert_eq!(thunk.evaluate(), Ok(42));
	}

	/// Tests `TryThunkOkAppliedBrand` (Functor over Error).
	#[test]
	fn test_try_thunk_with_ok_brand() {
		use crate::{
			brands::*,
			functions::*,
		};

		// Functor (map over error)
		let try_thunk: TryThunk<i32, i32> = TryThunk::err(10);
		let mapped = explicit::map::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(|x| x * 2, try_thunk);
		assert_eq!(mapped.evaluate(), Err(20));

		// Pointed (pure -> err)
		let try_thunk: TryThunk<i32, i32> = pure::<TryThunkOkAppliedBrand<i32>, _>(42);
		assert_eq!(try_thunk.evaluate(), Err(42));

		// Semimonad (bind over error)
		let try_thunk: TryThunk<i32, i32> = TryThunk::err(10);
		let bound = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(try_thunk, |x| {
			pure::<TryThunkOkAppliedBrand<i32>, _>(x * 2)
		});
		assert_eq!(bound.evaluate(), Err(20));

		// Foldable (fold over error)
		let try_thunk: TryThunk<i32, i32> = TryThunk::err(10);
		let folded = explicit::fold_right::<RcFnBrand, TryThunkOkAppliedBrand<i32>, _, _, _, _>(
			|x, acc| x + acc,
			5,
			try_thunk,
		);
		assert_eq!(folded, 15);
	}

	/// Tests `From<Result>` with `Ok`.
	///
	/// Verifies that converting an `Ok` result produces a successful `TryThunk`.
	#[test]
	fn test_try_thunk_from_result_ok() {
		let try_thunk: TryThunk<i32, String> = TryThunk::from(Ok(42));
		assert_eq!(try_thunk.evaluate(), Ok(42));
	}

	/// Tests `From<Result>` with `Err`.
	///
	/// Verifies that converting an `Err` result produces a failed `TryThunk`.
	#[test]
	fn test_try_thunk_from_result_err() {
		let try_thunk: TryThunk<i32, String> = TryThunk::from(Err("error".to_string()));
		assert_eq!(try_thunk.evaluate(), Err("error".to_string()));
	}

	/// Tests `From<TrySendThunk>` with `Ok`.
	///
	/// Verifies that converting a successful `TrySendThunk` produces a successful `TryThunk`.
	#[test]
	fn test_try_thunk_from_try_send_thunk_ok() {
		use crate::types::TrySendThunk;
		let send: TrySendThunk<i32, ()> = TrySendThunk::pure(42);
		let thunk: TryThunk<i32, ()> = TryThunk::from(send);
		assert_eq!(thunk.evaluate(), Ok(42));
	}

	/// Tests `From<TrySendThunk>` with `Err`.
	///
	/// Verifies that converting a failed `TrySendThunk` produces a failed `TryThunk`.
	#[test]
	fn test_try_thunk_from_try_send_thunk_err() {
		use crate::types::TrySendThunk;
		let send: TrySendThunk<i32, String> = TrySendThunk::err("fail".to_string());
		let thunk: TryThunk<i32, String> = TryThunk::from(send);
		assert_eq!(thunk.evaluate(), Err("fail".to_string()));
	}

	// QuickCheck Law Tests

	// Functor Laws (via HKT, TryThunkErrAppliedBrand)

	/// Functor identity: `map(id, t).evaluate() == t.evaluate()`.
	#[quickcheck]
	fn functor_identity(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let t: TryThunk<i32, i32> = TryThunk::ok(x);
		explicit::map::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(|a| a, t).evaluate() == Ok(x)
	}

	/// Functor composition: `map(f . g, t) == map(f, map(g, t))`.
	#[quickcheck]
	fn functor_composition(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let f = |a: i32| a.wrapping_add(1);
		let g = |a: i32| a.wrapping_mul(2);
		let lhs = explicit::map::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(
			move |a| f(g(a)),
			TryThunk::ok(x),
		)
		.evaluate();
		let rhs = explicit::map::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(
			f,
			explicit::map::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(g, TryThunk::ok(x)),
		)
		.evaluate();
		lhs == rhs
	}

	// Monad Laws (via HKT, TryThunkErrAppliedBrand)

	/// Monad left identity: `pure(a).bind(f) == f(a)` (for Ok values).
	#[quickcheck]
	fn monad_left_identity(a: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let f = |x: i32| pure::<TryThunkErrAppliedBrand<i32>, _>(x.wrapping_mul(2));
		let lhs = explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(
			pure::<TryThunkErrAppliedBrand<i32>, _>(a),
			f,
		)
		.evaluate();
		let rhs = f(a).evaluate();
		lhs == rhs
	}

	/// Monad right identity: `t.bind(pure) == t`.
	#[quickcheck]
	fn monad_right_identity(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let lhs = explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(
			pure::<TryThunkErrAppliedBrand<i32>, _>(x),
			pure::<TryThunkErrAppliedBrand<i32>, _>,
		)
		.evaluate();
		lhs == Ok(x)
	}

	/// Monad associativity: `m.bind(f).bind(g) == m.bind(|a| f(a).bind(g))`.
	#[quickcheck]
	fn monad_associativity(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let f = |a: i32| pure::<TryThunkErrAppliedBrand<i32>, _>(a.wrapping_add(1));
		let g = |a: i32| pure::<TryThunkErrAppliedBrand<i32>, _>(a.wrapping_mul(3));
		let m: TryThunk<i32, i32> = TryThunk::ok(x);
		let m2: TryThunk<i32, i32> = TryThunk::ok(x);
		let lhs = explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(
			explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(m, f),
			g,
		)
		.evaluate();
		let rhs = explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(m2, move |a| {
			explicit::bind::<TryThunkErrAppliedBrand<i32>, _, _, _, _>(f(a), g)
		})
		.evaluate();
		lhs == rhs
	}

	/// Error short-circuit: `TryThunk::err(e).bind(f).evaluate() == Err(e)`.
	#[quickcheck]
	fn error_short_circuit(e: i32) -> bool {
		let t: TryThunk<i32, i32> = TryThunk::err(e);
		t.bind(|x| TryThunk::ok(x.wrapping_add(1))).evaluate() == Err(e)
	}

	/// Tests `TryThunk::lift2` with two successful values.
	///
	/// Verifies that `lift2` combines results from both computations.
	#[test]
	fn test_lift2_ok_ok() {
		let t1: TryThunk<i32, String> = TryThunk::ok(10);
		let t2: TryThunk<i32, String> = TryThunk::ok(20);
		let t3 = t1.lift2(t2, |a, b| a + b);
		assert_eq!(t3.evaluate(), Ok(30));
	}

	/// Tests `TryThunk::lift2` short-circuits on first error.
	///
	/// Verifies that if the first computation fails, the second is not evaluated.
	#[test]
	fn test_lift2_err_ok() {
		let t1: TryThunk<i32, String> = TryThunk::err("first".to_string());
		let t2: TryThunk<i32, String> = TryThunk::ok(20);
		let t3 = t1.lift2(t2, |a, b| a + b);
		assert_eq!(t3.evaluate(), Err("first".to_string()));
	}

	/// Tests `TryThunk::lift2` propagates second error.
	///
	/// Verifies that if the second computation fails, the error is propagated.
	#[test]
	fn test_lift2_ok_err() {
		let t1: TryThunk<i32, String> = TryThunk::ok(10);
		let t2: TryThunk<i32, String> = TryThunk::err("second".to_string());
		let t3 = t1.lift2(t2, |a, b| a + b);
		assert_eq!(t3.evaluate(), Err("second".to_string()));
	}

	/// Tests `TryThunk::then` with two successful values.
	///
	/// Verifies that `then` discards the first result and returns the second.
	#[test]
	fn test_then_ok_ok() {
		let t1: TryThunk<i32, String> = TryThunk::ok(10);
		let t2: TryThunk<i32, String> = TryThunk::ok(20);
		let t3 = t1.then(t2);
		assert_eq!(t3.evaluate(), Ok(20));
	}

	/// Tests `TryThunk::then` short-circuits on first error.
	///
	/// Verifies that if the first computation fails, the second is not evaluated.
	#[test]
	fn test_then_err_ok() {
		let t1: TryThunk<i32, String> = TryThunk::err("first".to_string());
		let t2: TryThunk<i32, String> = TryThunk::ok(20);
		let t3 = t1.then(t2);
		assert_eq!(t3.evaluate(), Err("first".to_string()));
	}

	/// Tests `TryThunk::then` propagates second error.
	///
	/// Verifies that if the second computation fails, the error is propagated.
	#[test]
	fn test_then_ok_err() {
		let t1: TryThunk<i32, String> = TryThunk::ok(10);
		let t2: TryThunk<i32, String> = TryThunk::err("second".to_string());
		let t3 = t1.then(t2);
		assert_eq!(t3.evaluate(), Err("second".to_string()));
	}

	/// Tests `TryThunk::into_rc_try_lazy` basic usage.
	///
	/// Verifies that converting a thunk produces a lazy value with the same result.
	#[test]
	fn test_into_rc_try_lazy() {
		let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		let lazy = thunk.into_rc_try_lazy();
		assert_eq!(lazy.evaluate(), Ok(&42));
	}

	/// Tests `TryThunk::into_rc_try_lazy` caching behavior.
	///
	/// Verifies that the memoized value is computed only once.
	#[test]
	fn test_into_rc_try_lazy_caching() {
		use std::{
			cell::RefCell,
			rc::Rc,
		};

		let counter = Rc::new(RefCell::new(0));
		let counter_clone = counter.clone();
		let thunk: TryThunk<i32, ()> = TryThunk::new(move || {
			*counter_clone.borrow_mut() += 1;
			Ok(42)
		});
		let lazy = thunk.into_rc_try_lazy();

		assert_eq!(*counter.borrow(), 0);
		assert_eq!(lazy.evaluate(), Ok(&42));
		assert_eq!(*counter.borrow(), 1);
		assert_eq!(lazy.evaluate(), Ok(&42));
		assert_eq!(*counter.borrow(), 1);
	}

	/// Tests `TryThunk::into_arc_try_lazy` basic usage.
	///
	/// Verifies that converting a thunk produces a thread-safe lazy value.
	#[test]
	fn test_into_arc_try_lazy() {
		let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
		let lazy = thunk.into_arc_try_lazy();
		assert_eq!(lazy.evaluate(), Ok(&42));
	}

	/// Tests `TryThunk::into_arc_try_lazy` is Send and Sync.
	///
	/// Verifies that the resulting `ArcTryLazy` can be shared across threads.
	#[test]
	fn test_into_arc_try_lazy_send_sync() {
		use std::thread;

		let thunk: TryThunk<i32, String> = TryThunk::ok(42);
		let lazy = thunk.into_arc_try_lazy();
		let lazy_clone = lazy.clone();

		let handle = thread::spawn(move || {
			assert_eq!(lazy_clone.evaluate(), Ok(&42));
		});

		assert_eq!(lazy.evaluate(), Ok(&42));
		handle.join().unwrap();
	}

	/// Tests `TryThunk::catch_unwind_with` with a panicking closure.
	///
	/// Verifies that the custom handler converts the panic payload.
	#[test]
	fn test_catch_unwind_with_panic() {
		let thunk = TryThunk::<i32, i32>::catch_unwind_with(
			|| {
				if true {
					panic!("oops")
				}
				42
			},
			|_payload| -1,
		);
		assert_eq!(thunk.evaluate(), Err(-1));
	}

	/// Tests `TryThunk::catch_unwind_with` with a non-panicking closure.
	///
	/// Verifies that a successful closure wraps the value in `Ok`.
	#[test]
	fn test_catch_unwind_with_success() {
		let thunk = TryThunk::<i32, i32>::catch_unwind_with(|| 42, |_payload| -1);
		assert_eq!(thunk.evaluate(), Ok(42));
	}

	// 7.3: Bifunctor law QuickCheck tests for TryThunkBrand

	/// Bifunctor identity: `bimap(id, id, t) == t`.
	#[quickcheck]
	fn bifunctor_identity_ok(x: i32) -> bool {
		use crate::{
			brands::*,
			classes::bifunctor::*,
		};
		let t: TryThunk<i32, i32> = TryThunk::ok(x);
		bimap::<TryThunkBrand, _, _, _, _>(|e| e, |a| a, t).evaluate() == Ok(x)
	}

	/// Bifunctor identity on error path: `bimap(id, id, err(e)) == err(e)`.
	#[quickcheck]
	fn bifunctor_identity_err(e: i32) -> bool {
		use crate::{
			brands::*,
			classes::bifunctor::*,
		};
		let t: TryThunk<i32, i32> = TryThunk::err(e);
		bimap::<TryThunkBrand, _, _, _, _>(|e| e, |a| a, t).evaluate() == Err(e)
	}

	/// Bifunctor composition: `bimap(f1 . f2, g1 . g2, t) == bimap(f1, g1, bimap(f2, g2, t))`.
	#[quickcheck]
	fn bifunctor_composition_ok(x: i32) -> bool {
		use crate::{
			brands::*,
			classes::bifunctor::*,
		};
		let f1 = |a: i32| a.wrapping_add(1);
		let f2 = |a: i32| a.wrapping_mul(2);
		let g1 = |a: i32| a.wrapping_add(10);
		let g2 = |a: i32| a.wrapping_mul(3);

		let lhs = bimap::<TryThunkBrand, _, _, _, _>(
			move |e| f1(f2(e)),
			move |a| g1(g2(a)),
			TryThunk::ok(x),
		)
		.evaluate();
		let rhs = bimap::<TryThunkBrand, _, _, _, _>(
			f1,
			g1,
			bimap::<TryThunkBrand, _, _, _, _>(f2, g2, TryThunk::ok(x)),
		)
		.evaluate();
		lhs == rhs
	}

	/// Bifunctor composition on error path.
	#[quickcheck]
	fn bifunctor_composition_err(e: i32) -> bool {
		use crate::{
			brands::*,
			classes::bifunctor::*,
		};
		let f1 = |a: i32| a.wrapping_add(1);
		let f2 = |a: i32| a.wrapping_mul(2);
		let g1 = |a: i32| a.wrapping_add(10);
		let g2 = |a: i32| a.wrapping_mul(3);

		let lhs = bimap::<TryThunkBrand, _, _, _, _>(
			move |e| f1(f2(e)),
			move |a| g1(g2(a)),
			TryThunk::err(e),
		)
		.evaluate();
		let rhs = bimap::<TryThunkBrand, _, _, _, _>(
			f1,
			g1,
			bimap::<TryThunkBrand, _, _, _, _>(f2, g2, TryThunk::err(e)),
		)
		.evaluate();
		lhs == rhs
	}

	// 7.4: Error-channel monad law QuickCheck tests via TryThunkOkAppliedBrand

	/// Error-channel monad left identity: `pure(a).bind(f) == f(a)`.
	#[quickcheck]
	fn error_monad_left_identity(a: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let f = |x: i32| pure::<TryThunkOkAppliedBrand<i32>, _>(x.wrapping_mul(2));
		let lhs = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(
			pure::<TryThunkOkAppliedBrand<i32>, _>(a),
			f,
		)
		.evaluate();
		let rhs = f(a).evaluate();
		lhs == rhs
	}

	/// Error-channel monad right identity: `m.bind(pure) == m`.
	#[quickcheck]
	fn error_monad_right_identity(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let lhs = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(
			pure::<TryThunkOkAppliedBrand<i32>, _>(x),
			pure::<TryThunkOkAppliedBrand<i32>, _>,
		)
		.evaluate();
		lhs == Err(x)
	}

	/// Error-channel monad associativity: `m.bind(f).bind(g) == m.bind(|a| f(a).bind(g))`.
	#[quickcheck]
	fn error_monad_associativity(x: i32) -> bool {
		use crate::{
			brands::*,
			functions::*,
		};
		let f = |a: i32| pure::<TryThunkOkAppliedBrand<i32>, _>(a.wrapping_add(1));
		let g = |a: i32| pure::<TryThunkOkAppliedBrand<i32>, _>(a.wrapping_mul(3));
		let m: TryThunk<i32, i32> = TryThunk::err(x);
		let m2: TryThunk<i32, i32> = TryThunk::err(x);
		let lhs = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(
			explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(m, f),
			g,
		)
		.evaluate();
		let rhs = explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(m2, move |a| {
			explicit::bind::<TryThunkOkAppliedBrand<i32>, _, _, _, _>(f(a), g)
		})
		.evaluate();
		lhs == rhs
	}

	// 7.5: Semigroup/Monoid law QuickCheck tests for TryThunk

	/// Semigroup associativity for TryThunk: `append(append(a, b), c) == append(a, append(b, c))`.
	#[quickcheck]
	fn try_thunk_semigroup_associativity(
		a: String,
		b: String,
		c: String,
	) -> bool {
		use crate::classes::semigroup::append;

		let ta: TryThunk<String, ()> = TryThunk::ok(a.clone());
		let tb: TryThunk<String, ()> = TryThunk::ok(b.clone());
		let tc: TryThunk<String, ()> = TryThunk::ok(c.clone());
		let ta2: TryThunk<String, ()> = TryThunk::ok(a);
		let tb2: TryThunk<String, ()> = TryThunk::ok(b);
		let tc2: TryThunk<String, ()> = TryThunk::ok(c);
		let lhs = append(append(ta, tb), tc).evaluate();
		let rhs = append(ta2, append(tb2, tc2)).evaluate();
		lhs == rhs
	}

	/// Monoid left identity for TryThunk: `append(empty(), a) == a`.
	#[quickcheck]
	fn try_thunk_monoid_left_identity(x: String) -> bool {
		use crate::classes::{
			monoid::empty,
			semigroup::append,
		};

		let a: TryThunk<String, ()> = TryThunk::ok(x.clone());
		let lhs: TryThunk<String, ()> = append(empty(), a);
		lhs.evaluate() == Ok(x)
	}

	/// Monoid right identity for TryThunk: `append(a, empty()) == a`.
	#[quickcheck]
	fn try_thunk_monoid_right_identity(x: String) -> bool {
		use crate::classes::{
			monoid::empty,
			semigroup::append,
		};

		let a: TryThunk<String, ()> = TryThunk::ok(x.clone());
		let rhs: TryThunk<String, ()> = append(a, empty());
		rhs.evaluate() == Ok(x)
	}

	// 7.6: Thread safety test for TryThunk::into_arc_try_lazy

	/// Tests that `TryThunk::into_arc_try_lazy` produces a thread-safe value
	/// and all threads see the same cached result.
	#[test]
	fn test_into_arc_try_lazy_thread_safety() {
		use std::{
			sync::{
				Arc,
				atomic::{
					AtomicUsize,
					Ordering,
				},
			},
			thread,
		};

		let counter = Arc::new(AtomicUsize::new(0));
		let counter_clone = Arc::clone(&counter);
		let thunk: TryThunk<i32, String> = TryThunk::new(move || {
			counter_clone.fetch_add(1, Ordering::SeqCst);
			Ok(42)
		});
		let lazy = thunk.into_arc_try_lazy();

		let handles: Vec<_> = (0 .. 8)
			.map(|_| {
				let lazy_clone = lazy.clone();
				thread::spawn(move || {
					assert_eq!(lazy_clone.evaluate(), Ok(&42));
				})
			})
			.collect();

		assert_eq!(lazy.evaluate(), Ok(&42));
		for h in handles {
			h.join().unwrap();
		}

		// The closure should have been invoked exactly once.
		assert_eq!(counter.load(Ordering::SeqCst), 1);
	}

	/// Tests `Semigroup::append` short-circuits when the first operand is `Err`.
	///
	/// Verifies that the second operand is never evaluated when the first fails.
	#[test]
	fn test_semigroup_append_first_err_short_circuits() {
		use {
			crate::classes::semigroup::append,
			std::cell::Cell,
		};

		let counter = Cell::new(0u32);
		let t1: TryThunk<String, &str> = TryThunk::err("first failed");
		let t2: TryThunk<String, &str> = TryThunk::new(|| {
			counter.set(counter.get() + 1);
			Ok("second".to_string())
		});
		let result = append(t1, t2);
		assert_eq!(result.evaluate(), Err("first failed"));
		assert_eq!(counter.get(), 0, "second operand should not have been evaluated");
	}

	/// Tests `Semigroup::append` propagates the error when the second operand fails.
	///
	/// Verifies that when the first operand succeeds but the second fails, the error
	/// from the second operand is returned.
	#[test]
	fn test_semigroup_append_second_err_propagates() {
		use crate::classes::semigroup::append;

		let t1: TryThunk<String, &str> = TryThunk::pure("hello".to_string());
		let t2: TryThunk<String, &str> = TryThunk::err("second failed");
		let result = append(t1, t2);
		assert_eq!(result.evaluate(), Err("second failed"));
	}

	/// Tests `TryThunk::tail_rec_m` computes a simple recursive sum.
	///
	/// Verifies that the loop accumulates correctly and terminates with `Break`.
	#[test]
	fn test_tail_rec_m_success() {
		use core::ops::ControlFlow;
		let result: TryThunk<i32, ()> = TryThunk::tail_rec_m(
			|x| {
				TryThunk::ok(
					if x < 1000 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
				)
			},
			0,
		);
		assert_eq!(result.evaluate(), Ok(1000));
	}

	/// Tests `TryThunk::tail_rec_m` short-circuits on error.
	///
	/// Verifies that when the step function returns `Err`, the loop terminates
	/// immediately and propagates the error.
	#[test]
	fn test_tail_rec_m_early_error() {
		use core::ops::ControlFlow;
		let result: TryThunk<i32, &str> = TryThunk::tail_rec_m(
			|x| {
				if x == 5 {
					TryThunk::err("stopped at 5")
				} else {
					TryThunk::ok(ControlFlow::Continue(x + 1))
				}
			},
			0,
		);
		assert_eq!(result.evaluate(), Err("stopped at 5"));
	}

	/// Tests `TryThunk::tail_rec_m` stack safety with many iterations.
	///
	/// Verifies that the loop does not overflow the stack with 100,000 iterations.
	#[test]
	fn test_tail_rec_m_stack_safety() {
		use core::ops::ControlFlow;
		let iterations: i64 = 100_000;
		let result: TryThunk<i64, ()> = TryThunk::tail_rec_m(
			|acc| {
				TryThunk::ok(
					if acc < iterations {
						ControlFlow::Continue(acc + 1)
					} else {
						ControlFlow::Break(acc)
					},
				)
			},
			0i64,
		);
		assert_eq!(result.evaluate(), Ok(iterations));
	}

	/// Tests `TryThunk::arc_tail_rec_m` computes correctly and tracks calls.
	///
	/// Verifies that the Arc-wrapped variant produces the same result as
	/// `tail_rec_m` and that the step function is called the expected number
	/// of times.
	#[test]
	fn test_arc_tail_rec_m_success() {
		use {
			core::ops::ControlFlow,
			std::sync::{
				Arc,
				atomic::{
					AtomicUsize,
					Ordering,
				},
			},
		};
		let counter = Arc::new(AtomicUsize::new(0));
		let counter_clone = Arc::clone(&counter);
		let result: TryThunk<i32, ()> = TryThunk::arc_tail_rec_m(
			move |x| {
				counter_clone.fetch_add(1, Ordering::SeqCst);
				TryThunk::ok(
					if x < 100 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
				)
			},
			0,
		);
		assert_eq!(result.evaluate(), Ok(100));
		assert_eq!(counter.load(Ordering::SeqCst), 101);
	}

	/// Tests `TryThunk::arc_tail_rec_m` short-circuits on error.
	///
	/// Verifies that the Arc-wrapped variant propagates errors immediately.
	#[test]
	fn test_arc_tail_rec_m_early_error() {
		use core::ops::ControlFlow;
		let result: TryThunk<i32, &str> = TryThunk::arc_tail_rec_m(
			|x| {
				if x == 5 {
					TryThunk::err("stopped at 5")
				} else {
					TryThunk::ok(ControlFlow::Continue(x + 1))
				}
			},
			0,
		);
		assert_eq!(result.evaluate(), Err("stopped at 5"));
	}

	/// Tests `TryThunk::arc_tail_rec_m` stack safety with many iterations.
	///
	/// Verifies that the Arc-wrapped variant does not overflow the stack.
	#[test]
	fn test_arc_tail_rec_m_stack_safety() {
		use core::ops::ControlFlow;
		let iterations: i64 = 100_000;
		let result: TryThunk<i64, ()> = TryThunk::arc_tail_rec_m(
			|acc| {
				TryThunk::ok(
					if acc < iterations {
						ControlFlow::Continue(acc + 1)
					} else {
						ControlFlow::Break(acc)
					},
				)
			},
			0i64,
		);
		assert_eq!(result.evaluate(), Ok(iterations));
	}
}