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 module LambdaLift where 153 154 155 import Utilities 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 type Name = [Char] 189 190 191 192 193 data Constant = CNum Integer | CBool Bool | CFun Name 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 type IsRec = Bool 210 recursive = True 211 nonRecursive = False 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 data Expr binder 296 = EConst Constant 297 | EVar Name 298 | EAp (Expr binder) (Expr binder) 299 | ELam [binder] (Expr binder) 300 | ELet IsRec [Defn binder] (Expr binder) 301 302 type Defn binder = (binder, Expr binder) 303 304 305 306 307 308 309 310 311 type Expression = Expr Name 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 type AnnExpr binder annot = (annot, AnnExpr' binder annot) 333 334 data AnnExpr' binder annot 335 = AConst Constant 336 | AVar Name 337 | AAp (AnnExpr binder annot) (AnnExpr binder annot) 338 | ALam [binder] (AnnExpr binder annot) 339 | ALet IsRec [AnnDefn binder annot] (AnnExpr binder annot) 340 341 type AnnDefn binder annot = (binder, AnnExpr binder annot) 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 bindersOf :: [(binder,rhs)] -> [binder] 369 bindersOf defns = [name | (name, rhs) <- defns] 370 371 rhssOf :: [(binder,rhs)] -> [rhs] 372 rhssOf defns = [rhs | (name,rhs) <- defns] 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 lambdaLift :: Expression -> [SCDefn] 490 491 492 493 494 type SCDefn = (Name, [Name], Expression) 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 freeVars :: Expression -> AnnExpr Name (Set Name) 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 abstract :: AnnExpr Name (Set Name) -> Expression 529 530 531 532 533 534 535 536 537 538 collectSCs :: Expression -> [SCDefn] 539 540 541 542 543 544 545 lambdaLift = collectSCs . abstract . freeVars 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 freeVars (EConst k) = (setEmpty, AConst k) 582 freeVars (EVar v) = (setSingleton v, AVar v) 583 584 freeVars (EAp e1 e2) = 585 (setUnion (freeVarsOf e1') (freeVarsOf e2'), AAp e1' e2') 586 where 587 e1' = freeVars e1 588 e2' = freeVars e2 589 590 freeVars (ELam args body) = 591 (setDifference (freeVarsOf body') (setFromList args), ALam args body') 592 where 593 body' = freeVars body 594 595 freeVars (ELet isRec defns body) = 596 (setUnion defnsFree bodyFree, ALet isRec (zip binders rhss') body') 597 where 598 binders = bindersOf defns 599 binderSet = setFromList binders 600 rhss' = map freeVars (rhssOf defns) 601 freeInRhss = setUnionList (map freeVarsOf rhss') 602 defnsFree | isRec = setDifference freeInRhss binderSet 603 | not isRec = freeInRhss 604 body' = freeVars body 605 bodyFree = setDifference (freeVarsOf body') binderSet 606 607 freeVarsOf :: AnnExpr Name (Set Name) -> Set Name 608 freeVarsOf (free_vars, expr) = free_vars 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 collectSCs_e :: NameSupply -> Expression 671 -> (NameSupply, Bag SCDefn, Expression) 672 673 collectSCs e = [("$main", [], e')] ++ bagToList scs 674 where 675 (_, scs, e') = collectSCs_e initialNameSupply e 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 collectSCs_e ns (EConst k) = (ns, bagEmpty, EConst k) 692 collectSCs_e ns (EVar v) = (ns, bagEmpty, EVar v) 693 collectSCs_e ns (EAp e1 e2) = 694 (ns2, bagUnion scs1 scs2, EAp e1' e2') 695 where 696 (ns1, scs1, e1') = collectSCs_e ns e1 697 (ns2, scs2, e2') = collectSCs_e ns1 e2 698 699 700 701 702 collectSCs_e ns (ELam args body) = 703 (ns2, bagInsert (name, args, body') bodySCs, EConst (CFun name)) 704 where 705 (ns1, bodySCs, body') = collectSCs_e ns body 706 (ns2, name) = newName ns1 "SC" 707 708 709 710 collectSCs_e ns (ELet isRec defns body) = 711 (ns2, scs, ELet isRec defns' body') 712 where 713 (ns1, bodySCs, body') = collectSCs_e ns body 714 ((ns2, scs), defns') = mapAccuml collectSCs_d (ns1, bodySCs) defns 715 716 collectSCs_d (ns, scs) (name, rhs) = 717 ((ns1, bagUnion scs scs'), (name, rhs')) 718 where 719 (ns1, scs', rhs') = collectSCs_e ns rhs 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 separateLams :: Expression -> Expression 998 999 1000 1001 1002 1003 type Level = Int 1004 addLevels :: Expression -> AnnExpr (Name, Level) Level 1005 1006 1007 1008 1009 1010 1011 identifyMFEs :: AnnExpr (Name, Level) Level -> Expr (Name, Level) 1012 1013 1014 1015 1016 1017 1018 rename :: Expr (Name, a) -> Expr (Name, a) 1019 1020 1021 1022 1023 1024 float :: Expr (Name, Level) -> Expression 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 fullyLazyLift = lambdaLift . float . rename . 1035 identifyMFEs . addLevels . separateLams 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 freeSetToLevel :: Set Name -> Assn Name Level -> Level 1070 freeSetToLevel free_vars env = 1071 maximum (0 : map (assLookup env) (setToList free_vars)) 1072 -- If there are no free variables, return level zero 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 addLevels = freeToLevel . freeVars 1089 1090 1091 1092 1093 1094 1095 1096 freeToLevel_e :: Level -- Level of context 1097 -> Assn Name Level -- Level of in-scope names 1098 -> AnnExpr Name (Set Name) -- Input expression 1099 -> AnnExpr (Name, Level) Level -- Result expression 1100 1101 freeToLevel e = freeToLevel_e 0 [] e 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 freeToLevel_e level env (_, AConst k) = (0, AConst k) 1118 freeToLevel_e level env (_, AVar v) = (assLookup env v, AVar v) 1119 freeToLevel_e level env (_, AAp e1 e2) = 1120 (max (levelOf e1') (levelOf e2'), AAp e1' e2') 1121 where 1122 e1' = freeToLevel_e level env e1 1123 e2' = freeToLevel_e level env e2 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 freeToLevel_e level env (free, ALam args body) = 1137 (freeSetToLevel free env, ALam args' body') 1138 where 1139 body' = freeToLevel_e (level + 1) (args' ++ env) body 1140 args' = zip args (repeat (level+1)) 1141 1142 1143 1144 1145 freeToLevel_e level env (free, ALet isRec defns body) = 1146 (levelOf body', ALet isRec defns' body') 1147 where 1148 binders = bindersOf defns 1149 freeRhsVars = setUnionList [free | (free, _) <- rhssOf defns] 1150 maxRhsLevel = freeSetToLevel freeRhsVars 1151 ([(name,0) | name <- binders] ++ env) 1152 defns' = map freeToLevel_d defns 1153 body' = freeToLevel_e level (bindersOf defns' ++ env) body 1154 1155 freeToLevel_d (name, rhs) = ((name, levelOf rhs'), rhs') 1156 where rhs' = freeToLevel_e level envRhs rhs 1157 envRhs | isRec = [(name,maxRhsLevel) | name <- binders] ++ env 1158 | not isRec = env 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 levelOf :: AnnExpr a Level -> Level 1174 levelOf (level, e) = level 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 identifyMFEs_e :: Level -> AnnExpr (Name, Level) Level -> Expr (Name, Level) 1191 1192 identifyMFEs e = identifyMFEs_e 0 e 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 notMFECandidate (AConst k) = True 1205 notMFECandidate (AVar v) = True 1206 notMFECandidate _ = False -- For now, everything else is a candidate 1207 1208 1209 1210 1211 1212 1213 1214 1215 identifyMFEs_e cxt (level, e) = 1216 if (level == cxt || notMFECandidate e) 1217 then e' 1218 else transformMFE level e' 1219 where 1220 e' = identifyMFEs_e1 level e 1221 1222 transformMFE level e = ELet nonRecursive [(("v",level), e)] (EVar "v") 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 abstract (_, AConst k) = EConst k 1404 abstract (_, AVar v) = EVar v 1405 abstract (_, AAp e1 e2) = EAp (abstract e1) (abstract e2) 1406 1407 abstract (free, ALam args body) = 1408 foldl EAp sc (map EVar fvList) 1409 where 1410 fvList = setToList free 1411 sc = ELam (fvList ++ args) (abstract body) 1412 1413 abstract (_, ALet isRec defns body) = 1414 ELet isRec [(name, abstract body) | (name, body) <- defns] (abstract body) 1415 1416 1417 1418 1419 1420 separateLams (EConst k) = EConst k 1421 separateLams (EVar v) = EVar v 1422 separateLams (EAp e1 e2) = EAp (separateLams e1) (separateLams e2) 1423 separateLams (ELam args body) = foldr mkLam (separateLams body) args 1424 where 1425 mkLam arg body = ELam [arg] body 1426 separateLams (ELet isRec defns body) = 1427 ELet isRec [(name, separateLams rhs) | (name,rhs) <- defns] 1428 (separateLams body) 1429 1430 1431 1432 1433 1434 1435 identifyMFEs_e1 :: Level -> AnnExpr' (Name, Level) Level -> Expr (Name, Level) 1436 identifyMFEs_e1 level (AConst k) = EConst k 1437 identifyMFEs_e1 level (AVar v) = EVar v 1438 identifyMFEs_e1 level (AAp e1 e2) = 1439 EAp (identifyMFEs_e level e1) (identifyMFEs_e level e2) 1440 1441 1442 1443 1444 identifyMFEs_e1 level (ALam args body) = 1445 ELam args (identifyMFEs_e argLevel body) 1446 where 1447 (_, argLevel) = head args 1448 1449 identifyMFEs_e1 level (ALet isRec defns body) = 1450 ELet isRec defns' body' 1451 where 1452 body' = identifyMFEs_e level body 1453 defns' = [ ((name,rhsLevel),identifyMFEs_e rhsLevel rhs) 1454 | ((name,rhsLevel),rhs) <- defns] 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 rename e = e' where (_, e') = rename_e [] initialNameSupply e 1465 1466 rename_e :: Assn Name Name -> NameSupply -> Expr (Name,a) 1467 -> (NameSupply, Expr (Name,a)) 1468 rename_e env ns (EConst k) = (ns, EConst k) 1469 rename_e env ns (EVar v) = (ns, EVar (assLookup env v)) 1470 rename_e env ns (EAp e1 e2) = 1471 (ns2, EAp e1' e2') 1472 where 1473 (ns1, e1') = rename_e env ns e1 1474 (ns2, e2') = rename_e env ns1 e2 1475 rename_e env ns (ELam args body) = 1476 (ns1, ELam args' body') 1477 where 1478 (ns1, args') = mapAccuml newBinder ns args 1479 (ns2, body') = rename_e (assocBinders args args' ++ env) ns1 body 1480 1481 rename_e env ns (ELet isRec defns body) = 1482 (ns3, ELet isRec (zip binders' rhss') body') 1483 where 1484 (ns1, body') = rename_e env' ns body 1485 binders = bindersOf defns 1486 (ns2, binders') = mapAccuml newBinder ns1 binders 1487 env' = assocBinders binders binders' ++ env 1488 (ns3, rhss') = mapAccuml (rename_e rhsEnv) ns2 (rhssOf defns) 1489 rhsEnv | isRec = env' 1490 | not isRec = env 1491 1492 1493 1494 newBinder ns (name, info) = 1495 (ns1, (name', info)) where (ns1, name') = newName ns name 1496 1497 1498 1499 1500 assocBinders :: [(Name,a)] -> [(Name,a)] -> Assn Name Name 1501 assocBinders binders binders' = zip (map fst binders) (map fst binders') 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 float_e :: Expr (Name, Level) -> (FloatedDefns, Expression) 1512 1513 1514 1515 1516 1517 float e = install floatedDefns e' where (floatedDefns, e') = float_e e 1518 1519 1520 1521 1522 1523 1524 type FloatedDefns = [(Level, IsRec, [Defn Name])] 1525 1526 1527 1528 1529 1530 1531 1532 1533 install :: FloatedDefns -> Expression -> Expression 1534 install defnGroups e = 1535 foldr installGroup e defnGroups 1536 where 1537 installGroup (level, isRec, defns) e = ELet isRec defns e 1538 1539 1540 1541 1542 float_e (EConst k) = ([], EConst k) 1543 float_e (EVar v) = ([], EVar v) 1544 float_e (EAp e1 e2) = (fd1 ++ fd2, EAp e1' e2') 1545 where 1546 (fd1, e1') = float_e e1 1547 (fd2, e2') = float_e e2 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 float_e (ELam args body) = 1559 (outerLevelDefns, ELam args' (install thisLevelDefns body')) 1560 where 1561 args' = [arg | (arg,level) <- args] 1562 (_,thisLevel) = head args -- Extract level of abstraction 1563 (floatedDefns, body') = float_e body 1564 thisLevelDefns = filter groupIsThisLevel floatedDefns 1565 outerLevelDefns = filter (not.groupIsThisLevel) floatedDefns 1566 groupIsThisLevel (level,isRec,defns) = level >= thisLevel 1567 1568 1569 1570 1571 1572 1573 float_e (ELet isRec defns body) = 1574 (rhsFloatDefns ++ [thisGroup] ++ bodyFloatDefns, body') 1575 where 1576 (bodyFloatDefns, body') = float_e body 1577 (rhsFloatDefns, defns') = mapAccuml float_defn [] defns 1578 thisGroup = (thisLevel, isRec, defns') 1579 (_,thisLevel) = head (bindersOf defns) 1580 1581 float_defn floatedDefns ((name,level), rhs) = 1582 (rhsFloatDefns ++ floatedDefns, (name, rhs')) 1583 where 1584 (rhsFloatDefns, rhs') = float_e rhs 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