Source

htsql / src / htsql / core / tr / space.py

Full commit
   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
#
# Copyright (c) 2006-2013, Prometheus Research, LLC
#


from ..util import (maybe, listof, tupleof, Clonable, Hashable, Printable,
        cachedproperty)
from ..entity import TableEntity, ColumnEntity, Join
from ..domain import Domain, BooleanDomain, ListDomain, IdentityDomain
from ..error import point
from .flow import Flow
from .signature import Signature, Bag, Formula


class Expression(Hashable, Clonable, Printable):
    """
    Represents an expression node.

    This is an abstract class; most of its subclasses belong to one of the
    two categories: *space* and *code* nodes (see :class:`Space` and
    :class:`Code`).

    A space graph is an intermediate phase of the HTSQL translator.  It is
    translated from the flow graph by the *encoding* process.  The space
    graph is used to *compile* the term tree and then *assemble* the frame
    structure.

    A space graph reflects the space structure of the HTSQL query: each
    expression node represents either a data space or an expression over
    a data space.

    Expression nodes support equality by value: that is, two expression
    nodes are equal if they are of the same type and all their (essential)
    attributes are equal.  Some attributes (e.g. `flow`) are not
    considered essential and do not participate in comparison.  By-value
    semantics is respected when expression nodes are used as dictionary
    keys.

    The constructor arguments:

    `flow` (:class:`htsql.core.tr.flow.Flow`)
        The flow node that gave rise to the expression; should be used
        only for presentation or error reporting.

    Other attributes:

    `syntax` (:class:`htsql.core.tr.syntax.Syntax`)
        The syntax node that gave rise to the expression; for debugging
        purposes only.

    `mark` (:class:`htsql.core.mark.Mark`)
        The location of the node in the original query; for error reporting.

    `hash` (an integer)
        The node hash; if two nodes are considered equal, their hashes
        must be equal too.
    """

    def __init__(self, flow):
        assert isinstance(flow, Flow)
        self.flow = flow
        self.binding = flow.binding
        self.syntax = flow.binding.syntax
        point(self, flow)

    def __str__(self):
        # Display the syntex node that gave rise to the expression.
        return str(self.syntax)


class SegmentExpr(Expression):

    def __init__(self, root, space, codes, dependents, flow):
        assert isinstance(root, Space)
        assert isinstance(space, Space)
        assert isinstance(codes, listof(Code))
        assert isinstance(dependents, listof(SegmentExpr))
        assert isinstance(flow, Flow)
        super(SegmentExpr, self).__init__(flow)
        self.root = root
        self.space = space
        self.codes = codes
        self.dependents = dependents

    def __basis__(self):
        return (self.root, self.space, tuple(self.codes),
                tuple(self.dependents))


class Family(object):
    """
    Represents the target class of a space.

    The space family specifies the type of values produced by
    a space.  There are three distinct space families:

    - *scalar*, which indicates that the space produces
      scalar values;
    - *table*, which indicates that the space produces
      records from a database table;
    - *quotient*, which indicates that the space produces
      records from a derived *quotient* class.

    Class attributes:

    `is_scalar` (Boolean)
        Set for a scalar family.

    `is_table` (Boolean)
        Set for a table family.

    `is_quotient` (Boolean)
        Set for a quotient family.
    """

    is_scalar = False
    is_table = False
    is_quotient = False


class ScalarFamily(Family):
    """
    Represents a scalar space family.

    A scalar space produces values of a primitive type.
    """

    is_scalar = True


class TableFamily(Family):
    """
    Represents a table space family.

    A table space produces records from a database table.

    `table` (:class:`htsql.core.entity.TableEntity`)
        The table.
    """

    is_table = True

    def __init__(self, table):
        assert isinstance(table, TableEntity)
        self.table = table


class QuotientFamily(Family):
    """
    Represents a quotient space family.

    A quotient space produces records from a derived quotient class.

    The quotient class contains records formed from the kernel expressions
    as they run over the `seed` space.

    `seed` (:class:`Space`)
        The dividend space.

    `ground` (:class:`Space`)
        The ground space of the dividend.

    `kernels` (list of :class:`Code`)
        The kernel expressions of the quotient.
    """

    is_quotient = True

    def __init__(self, seed, ground, kernels):
        assert isinstance(seed, Space)
        assert isinstance(ground, Space)
        assert ground.is_axis and seed.concludes(ground)
        assert isinstance(kernels, listof(Code))
        self.seed = seed
        self.ground = ground
        self.kernels = kernels


class Space(Expression):
    """
    Represents a space node.

    A data space is a sequence of homogeneous values.  A space is generated
    by a series of space operations applied sequentially to the root space.

    Each space operation takes an input space as an argument and produces
    an output space as a result.  The operation transforms each element
    from the input row into zero, one, or more elements of the output
    space; the generating element is called *the origin* of the generated
    elements.  Thus, with every element of a space, we could associate
    a sequence of origin elements, one per each elementary space operation
    that together produce the space.

    Each instance of :class:`Space` represents a single space operation
    applied to some input space.  The `base` attribute of the instance
    represents the input space while the type of the instance and the
    other attributes reflect the properies of the operation.  The root
    space is denoted by an instance of:class:`RootSpace`, different
    subclasses of :class:`Space` correspond to different types of
    space operations.

    The type of values produced by a space is indicated by the `family`
    attribute.  We distinguish three space families: *scalar*, *table*
    and *quotient*.  A scalar space produces values of an elementary data
    type; a table space produces records of some table; a quotient space
    produces elements of a derived quotient class.

    Among others, we consider the following space operations:

    *The root space* `I`
        The initial space that contains one empty record.

    *A direct product* `A * T`
        Given a scalar space `A` and a table `T`, the direct product
        `A * T` generates all records of `T` for each element of `A`.

    *A fiber product* `A . T`
        Given an input space `A` that produces records of some table `S`
        and a table `T` linked to `S`, for each element of `A`,
        the fiber product `A . T` generates all associated records
        from `T`.

    *Filtering* `A ? p`
        Given a space `A` and a predicate `p` defined on `A`,
        the filtered space `A ? p` consists of all elements of `A`
        satisfying condition `p`.

    *Ordering* `A [e,...]`
        Given a space `A` and a list of expressions `e,...`, the
        ordered space `A [e,...]` consists of elements of `A` reordered
        by the values of `e,...`.

    *Quotient* `A ^ k`
        Given a space `A` and a kernel expression `k` defined on `A`,
        a quotient `A ^ k` produces all unique values of the kernel
        as it runs over `A`.

    Space operations for which the output space does not consist of
    elements of the input space are called *axial*.  If we take an
    arbitrary space `A`, disassemble it into individual operations,
    and then reapply only axial operations, we get the new space `A'`,
    which we call *the inflation* of `A`.  Note that elements of `A`
    form a subset of elements of `A'`.

    Now we can establish how different spaces are related to each other.
    Formally, for each pair of spaces `A` and `B`, we define a relation
    `<->` ("converges to") on elements from `A` and `B`, that is,
    a subset of the Cartesian product `A x B`, by the following rules:

    (1) For any space `A`, `<->` is the identity relation on `A`,
        that is, each element converges only to itself.

        For a space `A` and its inflation `A'`, each element from `A`
        converges to an equal element from `A'`.

    (2) Suppose `A` and `B` are spaces such that `A` is produced
        from `B` as a result of some axial space operation.  Then
        each element from `A` converges to its origin element
        from `B`.

        By transitivity, we could extend `<->` on `A` and any of its
        *ancestor spaces*, that is, the parent space of `A`, the
        parent of the parent of `A` and so on.

        In particular, this defines `<->` on an arbitrary space `A`
        and the root space `I` since `I` is an ancestor of any space.
        By the above definition, any element of `A` converges to
        the (only) record of `I`.

    (3) Finally, we are ready to define `<->` on an arbitrary pair
        of spaces `A` and `B`.  First, suppose that `A` and `B`
        share the same inflated space: `A' = B'`.  Then we could
        define `<->` on `A` and `B` transitively via `A'`: `a` from `A`
        converges to `b` from `B` if there exists `a'` from `A'` such
        that `a <-> a'` and `a' <-> b`.

        In the general case, find the closest ancestors `C` of `A`
        and `D` of `B` such that `C` and `D` have the same
        inflated space: `C' = D'`.  Rules `(1)` and `(2)` establish
        `<->` for the pairs `A` and `C`, `C` and `C' = D'`,
        `C' = D'` and `D`, and `D` and `B`.  We define `<->`
        on `A` and `B` transitively: `a` from `A` converges to
        `b` from `B` if there exist elements `c` from `C`,
        `c'` from `C' = D'`, `d` from `D` such that
        `a <-> c <-> c' <-> d <-> b`.

        Note that it is important that we take among the common inflated
        ancestors the closest one.  Any two spaces have a common inflated
        ancestor: the root space.  If the root space is, indeed, the closest
        common inflated ancestor of `A` and `B`, then each element of `A`
        converges to every element of `B`.

    Now we are ready to introduce several important relations between
    spaces:

    `A` *spans* `B`
        A space `A` spans a space `B` if for every element `a` from `A`:

            `card { b` from `B | a <-> b } <= 1`.

        Informally, it means that the statement::

            SELECT * FROM A

        and the statement::

            SELECT * FROM A LEFT OUTER JOIN B ON (A <-> B)

        produce the same number of rows.

    `A` *dominates* `B`
        A space `A` dominates a space `B` if `A` spans `B` and
        for every element `b` from `B`:

            `card { a` from `A | a <-> b } >= 1`.

        Informally, it implies that the statement::

            SELECT * FROM B INNER JOIN A ON (A <-> B)

        and the statement::

            SELECT * FROM B LEFT OUTER JOIN A ON (A <-> B)

        produce the same number of rows.

    `A` *conforms* `B`
        A space `A` conforms a space `B` if `A` dominates `B`
        and `B` dominates `A`.  Alternatively, we could say
        `A` conforms `B` if the `<->` relation establishes
        a bijection between `A` and `B`.

        Informally, it means that the statement::

            SELECT * FROM A

        and the statement::

            SELECT * FROM B

        produce the same number of rows.

        Note that `A` conforming `B` is not the same as `A` being equal
        to `B`; even if `A` conforms `B`,  elements of `A` and `B` may
        be of different types, therefore as sets, they are different.

    Now take an arbitrary space `A` and its parent space `B`.  We say:

    `A` *contracts* `B`
        A space `A` contracts its parent `B` if for any element from `B`
        there is no more than one converging element from `A`.

        Typically, it is non-axis spaces that contract their bases,
        although in some cases, an axis space could do it too.

    `A` *expands* `B`
        A space `A` expands its parent `B` if for any element from `B`
        there is at least one converging element from `A`.

        Note that it is possible that a space `A` both contracts and
        expands its base `B`, and also that `A` neither contracts
        nor expands `B`.  The former means that `A` conforms `B`.
        The latter holds, in particular, for the direct table space
        `A * T`.  `A * T` violates the contraction condition when
        `T` contains more than one record and violates the expansion
        condition when `T` has no records.

    A few words about how elements of a space are ordered.  The default
    (also called *weak*) ordering rules are:

    - a table space `T = I * T` is sorted by the lexicographic order
      of the table primary key;

    - a non-axial space keeps the order of its base;

    - an axial table space `A * T` or `A . T` respects the order its
      base `A`; records with the same origin are sorted by the table order.

    An alternative sort order could be specified explicitly (also called
    *strong* ordering).  Whenever strong ordering is  specified, it
    overrides the weak ordering.  Thus, elements of an ordered space `A [e]`
    are sorted first by expression `e`, and then elements which are not
    differentiated by `e` are sorted using the weak ordering of `A`.
    However, if `A` already has a strong ordering, it must be respected.
    Therefore, the general rule for sorting `A [e]` is:

    - first, sort the space by the strong ordering of `A`;

    - then, by `e`;

    - finally, by the weak ordering of `A`.

    Class attributes:

    `is_axis` (Boolean)
        Indicates whether the space is axial, that is, the elements
        of the space do not necessarily coincide with their origins.

    `is_root` (Boolean)
        Indicates that the space is the root space.

    The constructor arguments:

    `base` (:class:`Space` or ``None``)
        The parent input space; ``None`` for the root space.

    `family` (:class:`Family`)
        Specifies the type of the elements produced by the space.

    `is_contracting` (Boolean)
        Indicates if the space contracts its base space.

    `is_expanding` (Boolean)
        Indicates if the space expands its base space.

    Other attributes:

    `is_inflated` (Boolean)
        Indicates if the space is an inflation, that is, this space
        operation and all its ancestors are axial.
    """

    is_axis = False
    is_root = False
    is_commutative = True

    def __init__(self, base, family, is_contracting, is_expanding, flow):
        assert isinstance(base, maybe(Space))
        assert isinstance(family, Family)
        assert isinstance(is_contracting, bool)
        assert isinstance(is_expanding, bool)
        super(Space, self).__init__(flow)
        self.base = base
        self.family = family
        self.is_contracting = is_contracting
        self.is_expanding = is_expanding
        # Indicates that the space itself and all its ancestors are axes.
        self.is_inflated = (self.is_root or
                            (base.is_inflated and self.is_axis))

    def root(self):
        root = self
        while root.base is not None:
            root = root.base
        return root

    def unfold(self):
        """
        Produces a list of ancestor spaces.

        The method returns a list composed of the space itself,
        its base, the base of its base and so on.
        """
        ancestors = []
        ancestor = self
        while ancestor is not None:
            ancestors.append(ancestor)
            # Note: `ancestor.base` is None for the root space.
            ancestor = ancestor.base
        return ancestors

    def resembles(self, other):
        """
        Verifies if the spaces represent the same operation.

        Typically, it means that `self` and `other` have the same type
        and equal attributes, but may have different bases.
        """
        # We rely upon an assumption that the equality vector of a space node
        # is a tuple of all its essential attributes and the first element
        # of the tuple is the space base.  So we skip the base space and
        # compare the remaining attributes.
        if not isinstance(other, self.__class__):
            return False
        try:
            _basis = self._basis
        except AttributeError:
            self._rehash()
            _basis = self._basis
        try:
            _other_basis = other._basis
        except AttributeError:
            other._rehash()
            _other_basis = other._basis
        return (_basis[1:] == _other_basis[1:])

    def inflate(self):
        """
        Produces the inflation of the space.

        If we represent a space as a series of operations sequentially
        applied to the scalar space, the inflation of the space is obtained
        by ignoring any non-axial operations and applying axial operations
        only.
        """
        # Shortcut: check if the space is already an inflation.
        if self.is_inflated:
            return self
        # This is going to become a new inflated space.
        space = None
        # Iterate over all ancestors starting from the scalar space.
        for ancestor in reversed(self.unfold()):
            # Skip non-axial operations, reapply axial operations to
            # a new base.
            if ancestor.is_axis:
                space = ancestor.clone(base=space)
        # This is the inflated space now.
        return space

    def prune(self, other):
        """
        Prunes shared non-axial operations.

        Given spaces `A` and `B`, this function produces a new space
        `A'` such that `A` is a subset of `A'` and the convergence
        of `A` and `B` coincides with the convergence of `A'` and `B`.
        This is done by pruning any non-axial operations of `A` that
        also occur in `B`.
        """
        # Sanity check on the argument.
        assert isinstance(other, Space)
        # Shortcut: we cannot further prune an inflated space.
        if self.is_inflated:
            return self
        # Unfold the spaces into individual operations.
        my_ancestors = self.unfold()
        their_ancestors = other.unfold()
        # This is going to become the pruned space.
        space = None
        # Iterate until the ancestors are exhausted or diverged.
        while my_ancestors and their_ancestors:
            # Get the next operation.
            my_ancestor = my_ancestors[-1]
            their_ancestor = their_ancestors[-1]
            # Compare the ancestors.
            if my_ancestor.resembles(their_ancestor):
                # So both ancestors represent the same operation.
                # If it is an axis operation, apply it; otherwise,
                # discard it.
                # FIXME: may break if the space contains a non-matching
                # `limit/offset` operation?
                if not (my_ancestor.is_commutative or
                        my_ancestor == their_ancestor):
                    return self
                if my_ancestor.is_axis:
                    space = my_ancestor.clone(base=space)
                my_ancestors.pop()
                their_ancestors.pop()
            elif not their_ancestor.is_axis:
                # The ancestors represent different operations and `B`'s
                # ancestor is not an axis.  Discard it, we will try the
                # next ancestor.
                # FIXME: we may miss an opportunity to compare `B`'s ancestor
                # with other `A`'s ancestors.  It is not a big deal though,
                # we do not need to generate an optimal result here.
                their_ancestors.pop()
            elif not my_ancestor.is_axis:
                # The ancestors represent different operations, `B`'s ancestor
                # is an axis, and `A`'s ancestor is not.  Here we apply the
                # `A`'s ancestor.
                if not my_ancestor.is_commutative:
                    return self
                space = my_ancestor.clone(base=space)
                my_ancestors.pop()
            else:
                # The ancestors are both axial and differ from each other.
                # At this point, the ancestors diverge and are not
                # comparable anymore.  Break from the loop.
                break
        # Reapply the unprocessed ancestors.
        while my_ancestors:
            my_ancestor = my_ancestors.pop()
            if not my_ancestor.is_commutative:
                return self
            space = my_ancestor.clone(base=space)
        # We have a pruned space here.
        return space

    def spans(self, other):
        """
        Verifies if the space spans another space.
        """
        # Sanity check on the argument.
        assert isinstance(other, Space)
        # Shortcut: any space spans itself.
        if self == other:
            return True
        # Extract axial ancestors from both spaces.
        my_axes = [ancestor for ancestor in self.unfold()
                            if ancestor.is_axis]
        their_axes = [ancestor for ancestor in other.unfold()
                               if ancestor.is_axis]
        # Iterate until the axes are exhausted or diverged.
        while my_axes and their_axes:
            # Check if the next pair of axes represent the same operation.
            if my_axes[-1].resembles(their_axes[-1]):
                # If so, go compare the next pair of axes.
                my_axes.pop()
                their_axes.pop()
            else:
                # Otherwise, the axes diverge.
                break
        # At this point, all processed pairs of axes converge by identity.
        # If the other space has no more axes left, it is spanned.  Otherwise,
        # it is spanned only if its remaining unprocessed axes represent
        # contracting operations.
        for their_axis in their_axes:
            if not their_axis.is_contracting:
                return False
        return True

    def conforms(self, other):
        """
        Verifies if the space conforms another space.
        """
        # Sanity check on the argument.
        assert isinstance(other, Space)
        # Shortcut: any space conforms itself.
        if self == other:
            return True
        # Unfold the spaces into individual operations.
        my_ancestors = self.unfold()
        their_ancestors = other.unfold()
        # Iterate until the ancestors are exhausted or diverged.
        while my_ancestors and their_ancestors:
            # Get the next pair of ancestors.
            my_ancestor = my_ancestors[-1]
            their_ancestor = their_ancestors[-1]
            # Compare the ancestors.
            if my_ancestor.resembles(their_ancestor):
                # If the ancestors represent the same operation, we could
                # proceed to the next pair of ancestors.
                my_ancestors.pop()
                their_ancestors.pop()
            elif (my_ancestor.is_contracting and
                  my_ancestor.is_expanding and
                  not my_ancestor.is_axis):
                # Ok, the ancestors represent different operations, but
                # one of them is not an axis and does not change the
                # cardinality of its base.  We could skip this ancestor
                # and proceed further.
                my_ancestors.pop()
            elif (their_ancestor.is_contracting and
                  their_ancestor.is_expanding and
                  not their_ancestor.is_axis):
                # Same with the other ancestor.
                their_ancestors.pop()
            else:
                # The ancestors start to diverge; break from the loop.
                break
        # If all ancestors are processed, the spaces conform each other.
        # Otherwise, they conform each other only if the remaining unprocessed
        # ancestors do not change the cardinality of their bases.
        for ancestor in my_ancestors + their_ancestors:
            if not (ancestor.is_contracting and ancestor.is_expanding):
                return False
        return True

    def dominates(self, other):
        """
        Verifies if the space dominates another space.
        """
        # Sanity check on the argument.
        assert isinstance(other, Space)
        # Shortcut: any space dominates itself.
        if self == other:
            return True
        # Unfold the spaces into individual operations.
        my_ancestors = self.unfold()
        their_ancestors = other.unfold()
        # Iterate until the ancestors are exhausted or diverged.
        while my_ancestors and their_ancestors:
            # Get the next pair of ancestors.
            my_ancestor = my_ancestors[-1]
            their_ancestor = their_ancestors[-1]
            # Compare the ancestors.
            if my_ancestor.resembles(their_ancestor):
                # If the ancestors represent the same operation, we could
                # proceed to the next pair of ancestors.
                my_ancestors.pop()
                their_ancestors.pop()
            elif their_ancestor.is_contracting and not their_ancestor.is_axis:
                # We got ancestors representing different operations; however
                # the dominated ancestor represents a non-axis operation that
                # does not increase the cardinality of its base.  Therefore
                # we could ignore this ancestor and proceed further.
                their_ancestors.pop()
            else:
                # The ancestors start to diverge; break from the loop.
                break
        # If all ancestors are processed, the space dominates the other.
        # Otherwise, it is only possible if the remaining ancestors of
        # the space do not decrease the base cardinality while the
        # remaining ancestors of the other space do not increase the
        # base cardinality.
        for my_ancestor in my_ancestors:
            if not my_ancestor.is_expanding:
                return False
        for their_ancestor in their_ancestors:
            if not their_ancestor.is_contracting:
                return False
        return True

    def concludes(self, other):
        """
        Verifies if the other space is a ancestor of the space.
        """
        # Sanity check on the argument.
        assert isinstance(other, Space)
        # Iterate over all ancestors of the space comparing them with
        # the given other space.
        space = self
        while space is not None:
            if space == other:
                return True
            space = space.base
        # None of the ancestors matched, the spaces must be unrelated.
        return False


class RootSpace(Space):
    """
    Represents a root scalar space.

    A root space `I` contains one record ``()``.  Any other space is generated
    by applying a sequence of elementary space operations to `I`.

    `base` (always ``None``)
        The root space (and only the root space) has no parent space.
    """

    # Scalar space is an axial space.
    is_axis = True
    is_root = True

    def __init__(self, base, flow):
        # We keep `base` among constructor arguments despite it always being
        # equal to `None` to make
        #   space = space.clone(base=new_base)
        # work for all types of spaces.
        assert base is None
        # Note that we must satisfy the assumption that the first element
        # of the equality vector is the space base (used by `Space.resembles`).
        super(RootSpace, self).__init__(
                    base=None,
                    family=ScalarFamily(),
                    is_contracting=False,
                    is_expanding=False,
                    flow=flow)

    def __basis__(self):
        return (self.base,)

    def __str__(self):
        # Display a table expression in an algebraic form.
        return "I"


class ScalarSpace(Space):
    """
    Represents a link to the scalar class.

    Traversing a link to the scalar class produces an empty record ``()``
    for each element of the input space.
    """

    is_axis = True

    def __init__(self, base, flow):
        super(ScalarSpace, self).__init__(
                    base=base,
                    family=ScalarFamily(),
                    is_contracting=True,
                    is_expanding=True,
                    flow=flow)

    def __basis__(self):
        return (self.base,)

    def __str__(self):
        # Display:
        #   (<base> * I)
        return "(%s * I)" % self.base


class TableSpace(Space):
    """
    Represents a product of an input space to a table.

    A product operation generates a subset of a Cartesian product
    between the base space and records of a table.  This is an abstract
    class, see concrete subclasses :class:`DirectTableSpace` and
    :class:`FiberTableSpace`.

    `table` (:class:`htsql.core.entity.TableEntity`)
        The table.
    """

    # All subclasses of `TableSpace` are axial spaces.
    is_axis = True


class DirectTableSpace(TableSpace):
    """
    Represents a direct product between a scalar space and a table.

    A direct product `A * T` produces all records of the table `T`
    for each element of the input space `A`.

    `base` (:class:`Space`)
        The base space.

    `table` (:class:`htsql.core.entity.TableEntity`)
        The table.
    """

    def __init__(self, base, table, flow):
        assert isinstance(base, Space) and base.family.is_scalar
        super(DirectTableSpace, self).__init__(
                    base=base,
                    family=TableFamily(table),
                    is_contracting=False,
                    is_expanding=False,
                    flow=flow)
        self.table = table

    def __basis__(self):
        return (self.base, self.table)

    def __str__(self):
        # Display:
        #   (<base> * <schema>.<table>)
        return "(%s * %s)" % (self.base, self.family.table)


class FiberTableSpace(TableSpace):
    """
    Represents a fiber product between a table space and a linked table.

    Let `A` be a space producing records of table `S`, `j` be a join
    condition between tables `S` and `T`.  A fiber product `A .j T`
    (or `A . T` when the join condition is implied) of the space `A`
    and the table `T` is a sequence of records of `T` that for each
    record of `A` generates all records of `T` satisfying the join
    condition `j`.

    `base` (:class:`Space`)
        The base space.

    `join` (:class:`htsql.core.entity.Join`)
        The join condition.
    """

    def __init__(self, base, join, flow):
        assert isinstance(join, Join)
        # Check that the join origin is the table of the base space.
        assert isinstance(base, Space) and base.family.is_table
        assert base.family.table is join.origin
        super(FiberTableSpace, self).__init__(
                    base=base,
                    family=TableFamily(join.target),
                    is_contracting=join.is_contracting,
                    is_expanding=join.is_expanding,
                    flow=flow)
        self.join = join

    def __basis__(self):
        return (self.base, self.join)

    def __str__(self):
        # Display:
        #   (<base> . <schema>.<table>)
        return "(%s . %s)" % (self.base, self.family.table)


class QuotientSpace(Space):
    """
    Represents a quotient operation.

    A quotient operation takes three arguments: an input space `A`,
    a seed space `S`, which should be a descendant of the input space,
    and a kernel expression `k` on the seed space.  For each element
    of the input space, the output space `A . (S ^ k)` generates unique
    values of `k` as it runs over convergent elements of `S`.

    `base` (:class:`Space`)
        The base space.

    `seed` (:class:`Space`)
        The seed space of the quotient; must be a descendant
        of the base space.

    `kernels` (a list of :class:`Code`)
        Kernel expressions of the quotient.

    `companions` (a list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        representing the space needs to export extra aggregate units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.

    Other attributes:

    `ground` (:class:`Space`)
        The closest axial ancestor of `seed` that is spanned
        by the `base` space.
    """

    is_axis = True

    def __init__(self, base, seed, kernels, flow,
                 companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        # Check that `seed` is a plural descendant of `base`.
        assert seed.spans(base)
        assert not base.spans(seed)
        assert isinstance(kernels, listof(Code))
        assert isinstance(companions, listof(Code))
        # Find an ancestor of `seed` that is spanned by `base`.
        ground = seed
        while not base.spans(ground.base):
            ground = ground.base
        # The quotient space conforms its base space only when
        # the kernel expression is constant.
        is_contracting = (not kernels)
        # FIXME: this is wrong, but the assembler relies on it
        # to collapse `GROUP BY` to the segment frame.
        is_expanding = (base.is_root and not kernels)
        super(QuotientSpace, self).__init__(
                    base=base,
                    family=QuotientFamily(seed, ground, kernels),
                    is_contracting=is_contracting,
                    is_expanding=is_expanding,
                    flow=flow)
        self.seed = seed
        self.ground = ground
        self.kernels = kernels
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed, tuple(self.kernels))

    def __str__(self):
        # Display:
        #   (<base> . (<seed> ^ {<kernels>}))
        return "(%s . (%s ^ {%s}))" % (self.base, self.seed,
                        ", ".join(str(kernel) for kernel in self.kernels))


class ComplementSpace(Space):
    """
    Represents a complement to a quotient.

    A complement takes a quotient as an input space and generates
    elements of the quotient seed.

    `base` (:class:`Space`)
        The base space.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        representing the space needs to export extra covering units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.

    Other attributes:

    `seed` (:class:`Space`)
        The seed space of the quotient.

    `ground` (:class:`Space`)
        The grond space of the quotient.

    `kernels` (list of :class:`Code`)
        Kernel expressions of the quotient.
    """

    is_axis = True

    def __init__(self, base, flow, companions=[]):
        assert isinstance(base, Space)
        assert base.family.is_quotient
        assert isinstance(companions, listof(Code))
        super(ComplementSpace, self).__init__(
                    base=base,
                    family=base.family.seed.family,
                    is_contracting=False,
                    is_expanding=True,
                    flow=flow)
        self.seed = base.family.seed
        self.ground = base.family.ground
        self.kernels = base.family.kernels
        self.companions = companions

    def __basis__(self):
        return (self.base,)

    def __str__(self):
        # Display:
        #   (<base> . ^)
        return "(%s . ^)" % self.base


class MonikerSpace(Space):
    """
    Represents an moniker operation.

    A moniker masks an arbitrary sequence of operations
    as a single axial space operation.

    `base` (:class:`Space`)
        The base space.

    `seed` (:class:`Space`)
        The seed space.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        representing the space must export extra covering units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.

    Other attributes:

    `ground` (:class:`Space`)
        The closest axial ancestor of `seed` spanned by `base`.
    """

    is_axis = True

    def __init__(self, base, seed, flow, companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        assert seed.spans(base)
        # We don't need `seed` to be plural or even axial against `base`.
        #assert not base.spans(seed)
        assert isinstance(companions, listof(Code))
        # Determine an axial ancestor of `seed` spanned by `base`
        # (could be `seed` itself).
        ground = seed
        while not ground.is_axis:
            ground = ground.base
        if not base.spans(ground):
            while not base.spans(ground.base):
                ground = ground.base
        super(MonikerSpace, self).__init__(
                    base=base,
                    family=seed.family,
                    is_contracting=base.spans(seed),
                    is_expanding=seed.dominates(base),
                    flow=flow)
        self.seed = seed
        self.ground = ground
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed)

    def __str__(self):
        # Display:
        #   (<base> . (<seed>))
        return "(%s . (%s))" % (self.base, self.seed)


class ForkedSpace(Space):
    """
    Represents a fork expression.

    A fork expression associated each element of the input space
    with every element of the input space sharing the same origin
    and values of the kernel expression.

    `base` (:class:`Space`)
        The base space.

    `seed` (:class:`Space`)
        The space to fork (typically coincides with the base space).

    `kernels` (list of :class:`Code`)
        The kernel expressions.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        representing the space must export extra covering units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.

    Other attributes:

    `ground` (:class:`Space`)
        The closest axial ancestor of the seed space.
    """

    is_axis = True

    def __init__(self, base, seed, kernels, flow, companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        assert isinstance(kernels, listof(Code))
        assert base.spans(seed) and seed.spans(base)
        # FIXME: this condition could be violated after the rewrite step
        # (also, equal-by-value is not implemented for `Family`):
        #assert base.family == seed.family
        # FIXME: we don't check for this constraint in the encoder anymore.
        #assert all(base.spans(unit.space) for code in kernels
        #                                  for unit in code.units)
        assert isinstance(companions, listof(Code))
        ground = seed
        while not ground.is_axis:
            ground = ground.base
        is_contracting = ground.is_contracting
        is_expanding = (not kernels and seed.dominates(base))
        super(ForkedSpace, self).__init__(
                    base=base,
                    family=base.family,
                    is_contracting=is_contracting,
                    is_expanding=is_expanding,
                    flow=flow)
        self.seed = seed
        self.ground = ground
        self.kernels = kernels
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed, tuple(self.kernels))

    def __str__(self):
        # Display:
        #   (<base> . fork({<kernels>}))
        return "(%s . fork({%s}))" \
                % (self.base, ", ".join(str(code) for code in self.kernels))


class AttachSpace(Space):
    """
    Represents a linking operation.

    A linking operation generates, for every element of the input space,
    convergent elements from the seed space with the same image value.

    `base` (:class:`Space`)
        The base space.

    `seed` (:class:`Space`)
        The seed space.

    `images` (list of pairs of :class:`Code`)
        Pairs of expressions forming a fiber join condition.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        representing the space must export extra covering units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.

    Other attributes:

    `ground` (:class:`Space`)
        The closest axial ancestor of `seed` spanned by `base`.
    """

    is_axis = True

    def __init__(self, base, seed, images, filter, flow, companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        assert seed.spans(base)
        assert not base.spans(seed)
        assert isinstance(images, listof(tupleof(Code, Code)))
        assert isinstance(filter, maybe(Code))
        if filter is not None:
            assert isinstance(filter.domain, BooleanDomain)
        assert isinstance(companions, listof(Code))
        ground = seed
        while not base.spans(ground.base):
            ground = ground.base
        super(AttachSpace, self).__init__(
                    base=base,
                    family=seed.family,
                    is_contracting=False,
                    is_expanding=False,
                    flow=flow)
        self.seed = seed
        self.ground = ground
        self.images = images
        self.filter = filter
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed, tuple(self.images), self.filter)

    def __str__(self):
        # Display:
        #   (<base> . ({<limages>} -> <seed>{<rimages>}))
        return "(%s . ({%s} -> %s{%s}))" \
                % (self.base, ", ".join(str(lop) for lop, rop in self.images),
                   self.seed, ", ".join(str(rop) for lop, rop in self.images))


class ClippedSpace(Space):

    is_axis = True

    def __init__(self, base, seed, limit, offset, flow, companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        assert seed.spans(base)
        assert not base.spans(seed)
        assert isinstance(limit, maybe(int))
        assert isinstance(offset, maybe(int))
        assert isinstance(companions, listof(Code))
        # Determine an axial ancestor of `seed` spanned by `base`.
        ground = seed
        while not ground.is_axis:
            ground = ground.base
        assert not base.spans(ground)
        while not base.spans(ground.base):
            ground = ground.base
        is_contracting = (limit is None)
        is_expanding = (seed.dominates(base) and offset is None
                        and (limit is None or limit > 0))
        super(ClippedSpace, self).__init__(
                    base=base,
                    family=seed.family,
                    is_contracting=is_contracting,
                    is_expanding=is_expanding,
                    flow=flow)
        self.seed = seed
        self.ground = ground
        self.limit = limit
        self.offset = offset
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed, self.limit, self.offset)

    def __str__(self):
        # Display:
        #   (<base> . (<seed>) [<offset>:<offset>+<limit>])
        return "(%s . (%s) [%s:%s+%s])" \
                % (self.base, self.seed,
                   self.offset if self.offset is not None else 0,
                   self.offset if self.offset is not None else 0,
                   self.limit if self.limit is not None else 1)


class LocatorSpace(AttachSpace):

    is_axis = True

    def __init__(self, base, seed, images, filter, flow, companions=[]):
        assert isinstance(base, Space)
        assert isinstance(seed, Space)
        assert isinstance(images, listof(tupleof(Code, Code)))
        assert isinstance(filter, maybe(Code))
        if filter is not None:
            assert isinstance(filter.domain, BooleanDomain)
        assert seed.spans(base)
        # We don't need `seed` to be plural or even axial against `base`.
        #assert not base.spans(seed)
        assert isinstance(companions, listof(Code))
        # Determine an axial ancestor of `seed` spanned by `base`
        # (could be `seed` itself).
        ground = seed
        while not ground.is_axis:
            ground = ground.base
        if not base.spans(ground):
            while not base.spans(ground.base):
                ground = ground.base
        axis = seed
        while not axis.is_axis:
            axis = axis.base
        is_contracting = (axis.base is None or base.spans(axis.base))
        # Note: skip Attach constructor.
        super(AttachSpace, self).__init__(
                    base=base,
                    family=seed.family,
                    is_contracting=is_contracting,
                    is_expanding=False,
                    flow=flow)
        self.seed = seed
        self.images = images
        self.filter = filter
        self.ground = ground
        self.companions = companions

    def __basis__(self):
        return (self.base, self.seed, tuple(self.images), self.filter)


class FilteredSpace(Space):
    """
    Represents a filtering operation.

    A filtered space `A ? f`, where `A` is the input space and `f` is
    a predicate expression on `A`, consists of rows of `A` satisfying
    the condition `f`.

    `base` (:class:`Space`)
        The base space.

    `filter` (:class:`Code`)
        The predicate expression.
    """

    def __init__(self, base, filter, flow):
        assert isinstance(filter, Code)
        assert isinstance(filter.domain, BooleanDomain)
        super(FilteredSpace, self).__init__(
                    base=base,
                    family=base.family,
                    is_contracting=True,
                    is_expanding=False,
                    flow=flow)
        self.filter = filter

    def __basis__(self):
        return (self.base, self.filter)

    def __str__(self):
        # Display:
        #   (<base> ? <filter>)
        return "(%s ? %s)" % (self.base, self.filter)


class OrderedSpace(Space):
    """
    Represents an ordered space.

    An ordered space `A [e,...;p:q]` is a space with explicitly specified
    strong ordering.  It also may extract a slice of the input space.

    `base` (:class:`Space`)
        The base space.

    `order` (a list of pairs `(code, direction)`)
        Expressions to sort the space by.

        Here `code` is a :class:`Code` instance, `direction` is either
        ``+1`` (indicates ascending order) or ``-1`` (indicates descending
        order).

    `limit` (a non-negative integer or ``None``)
        If set, the space extracts the first `limit` rows from the base
        space (with respect to the space ordering).  The remaining rows
        are discarded.

    `offset` (a non-negative integer or ``None``)
        If set, indicates that when extracting rows from the base space,
        the first `offset` rows should be skipped.
    """

    # FIXME: Non-commutativity of the ordered space may affect `prune`
    # and other functions.  Add class attribute `is_commutative`?
    # Or override `resembles` to return `True` only for equal nodes?

    def __init__(self, base, order, limit, offset, flow):
        assert isinstance(order, listof(tupleof(Code, int)))
        assert isinstance(limit, maybe(int))
        assert isinstance(offset, maybe(int))
        assert limit is None or limit >= 0
        assert offset is None or offset >= 0
        super(OrderedSpace, self).__init__(
                    base=base,
                    family=base.family,
                    is_contracting=True,
                    is_expanding=(limit is None and offset is None),
                    flow=flow)
        self.order = order
        self.limit = limit
        self.offset = offset
        self.is_commutative = (limit is None and offset is None)

    def __basis__(self):
        return (self.base, tuple(self.order))

    def __str__(self):
        # Display:
        #   <base> [<code>,...;<offset>:<limit>+<offset>]
        indicators = []
        if self.order:
            indicator = ",".join(str(code) for code, dir in self.order)
            indicators.append(indicator)
        if self.limit is not None and self.offset is not None:
            indicator = "%s:%s+%s" % (self.offset, self.offset, self.limit)
            indicators.append(indicator)
        elif self.limit is not None:
            indicator = ":%s" % self.limit
            indicators.append(indicator)
        elif self.offset is not None:
            indicator = "%s:" % self.offset
            indicators.append(indicator)
        indicators = ";".join(indicators)
        return "%s [%s]" % (self.base, indicators)


class Code(Expression):
    """
    Represents a code expression.

    A code expression is a function on spaces.  Specifically, it is a
    functional (possibly of several variables) that maps a space
    (or a Cartesian product of several spaces) to some scalar domain.
    :class:`Code` is an abstract base class for all code expressions;
    see its subclasses for concrete types of expressions.

    Among all code expressions, we distinguish *unit expressions*:
    elementary functions on spaces.  There are several kinds of units:
    among them are table columns and aggregate functions (see :class:`Unit`
    for more detail).  A non-unit code could be expressed as
    a composition of a scalar function and one or several units:

        `f = f(a,b,...) = F(u(a),v(b),...)`,

    where

    - `f` is a code expression;
    - `F` is a scalar function;
    - `a`, `b`, ... are elements of spaces `A`, `B`, ...;
    - `u`, `v`, ... are unit expressions on `A`, `B`, ....

    Note: special forms like `COUNT` or `EXISTS` are also expressed
    as code nodes.  Since they are not regular functions, special care
    must be taken to properly wrap them with appropriate
    :class:`ScalarUnit` and/or :class:`AggregateUnit` instances.

    `domain` (:class:`htsql.core.domain.Domain`)
        The co-domain of the code expression.

    `units` (a list of :class:`Unit`)
        The unit expressions of which the code is composed.
    """

    def __init__(self, domain, flow):
        assert isinstance(domain, Domain)
        super(Code, self).__init__(flow)
        self.domain = domain

    @cachedproperty
    def units(self):
        return self.get_units()

    def get_units(self):
        return []


class LiteralCode(Code):
    """
    Represents a literal value.

    `value` (valid type depends on the domain)
        The value.

    `domain` (:class:`htsql.core.domain.Domain`)
        The value type.
    """

    def __init__(self, value, domain, flow):
        super(LiteralCode, self).__init__(
                    domain=domain,
                    flow=flow)
        self.value = value

    def __basis__(self):
        return (self.value, self.domain)

    def __str__(self):
        # The actual value is often more helpful than the expression
        # that generated it.
        return repr(self.value)


class CastCode(Code):
    """
    Represents a type conversion operator.

    `base` (:class:`Code`)
        The expression to convert.

    `domain` (:class:`htsql.core.domain.Domain`)
        The target domain.
    """

    def __init__(self, base, domain, flow):
        super(CastCode, self).__init__(
                    domain=domain,
                    flow=flow)
        self.base = base

    def __basis__(self):
        return (self.base, self.domain)

    def get_units(self):
        return self.base.units


class FormulaCode(Formula, Code):
    """
    Represents a formula code.

    A formula code represents a function or an operator call as a code node.

    `signature` (:class:`htsql.core.tr.signature.Signature`)
        The signature of the formula.

    `domain` (:class:`Domain`)
        The co-domain of the formula.

    `arguments` (a dictionary)
        The arguments of the formula.

        Note that all the arguments become attributes of the node object.
    """

    def __init__(self, signature, domain, flow, **arguments):
        assert isinstance(signature, Signature)
        # Check that the arguments match the formula signature.
        arguments = Bag(**arguments)
        assert arguments.admits(Code, signature)
        # The first two arguments are processed by the `Formula`
        # constructor, the rest of them go to the `Flow` constructor.
        super(FormulaCode, self).__init__(
                    signature, arguments,
                    domain=domain,
                    flow=flow)

    def __basis__(self):
        return (self.signature, self.domain, self.arguments.freeze())

    def get_units(self):
        units = []
        for cell in self.arguments.cells():
            units.extend(cell.units)
        return units


class CorrelationCode(Code):

    def __init__(self, code):
        super(CorrelationCode, self).__init__(code.domain, code.flow)
        self.code = code

    def __basis__(self):
        return (self.code,)


class Unit(Code):
    """
    Represents a unit expression.

    A unit is an elementary function on a space.  There are several kinds
    of units; see subclasses :class:`ColumnUnit`, :class:`ScalarUnit`,
    :class:`AggregateUnit`, and :class:`CorrelatedUnit` for more detail.

    Units are divided into two categories: *primitive* and *compound*.

    A primitive unit is an intrinsic function of its space; no additional
    calculations are required to generate a primitive unit.  Currently,
    the only example of a primitive unit is :class:`ColumnUnit`.

    A compound unit requires calculating some non-intrinsic function
    on the target space.  Among compound units there are :class:`ScalarUnit`
    and :class:`AggregateUnit`, which correspond respectively to
    scalar and aggregate functions on a space.

    Note that it is easy to *lift* a unit code from one space to another.
    Specifically, suppose a unit `u` is defined on a space `A` and `B`
    is another space such that `B` spans `A`.  Then for each row `b`
    from `B` there is no more than one row `a` from `A` such that `a <-> b`.
    Therefore we could define `u` on `B` as follows:

    - `u(b) = u(a)` if there exists `a` from `A` such that `a <-> b`;
    - `u(b) =` ``NULL`` if there is no rows in `A` convergent to `b`.

    When a space `B` spans the space `A` of a unit `u`, we say that
    `u` is *singular* on `B`.  By the previous argument, `u` could be
    lifted to `B`.  Thus any unit is well-defined not only on the
    space where it is originally defined, but also on any space where
    it is singular.

    Attributes:

    `space` (:class:`Space`)
        The space on which the unit is defined.

    `domain` (:class:`htsql.core.domain.Domain`)
        The unit co-domain.

    Class attributes:

    `is_primitive` (Boolean)
        If set, indicates that the unit is primitive.

    `is_compound` (Boolean)
        If set, indicates that the unit is compound.
    """

    is_primitive = False
    is_compound = False

    def __init__(self, space, domain, flow):
        assert isinstance(space, Space)
        super(Unit, self).__init__(
                    domain=domain,
                    flow=flow)
        self.space = space

    @property
    def units(self):
        # Use `property` instead of `cachedproperty` to avoid
        # creating a reference cycle.
        return [self]

    def singular(self, space):
        """
        Verifies if the unit is singular (well-defined) on the given space.
        """
        return space.spans(self.space)


class PrimitiveUnit(Unit):
    """
    Represents a primitive unit.

    A primitive unit is an intrinsic function on a space.

    This is an abstract class; for the (only) concrete subclass, see
    :class:`ColumnUnit`.
    """

    is_primitive = True


class CompoundUnit(Unit):
    """
    Represents a compound unit.

    A compound unit is some non-intrinsic function on a space.

    This is an abstract class; for concrete subclasses, see
    :class:`ScalarUnit`, :class:`AggregateUnit`, etc.

    `code` (:class:`Code`)
        The expression to evaluate on the unit space.
    """

    is_compound = True

    def __init__(self, code, space, domain, flow):
        assert isinstance(code, Code)
        super(CompoundUnit, self).__init__(
                    space=space,
                    domain=domain,
                    flow=flow)
        self.code = code


class ColumnUnit(PrimitiveUnit):
    """
    Represents a column unit.

    A column unit is a function on a space that returns a column of the
    prominent table of the space.

    `column` (:class:`htsql.core.entity.ColumnEntity`)
        The column produced by the unit.

    `space` (:class:`Space`)
        The unit space.  The space must be of a table family and the space
        table must coincide with the column table.
    """

    def __init__(self, column, space, flow):
        assert isinstance(column, ColumnEntity)
        assert (space.family.is_table and
                space.family.table == column.table)
        super(ColumnUnit, self).__init__(
                    space=space,
                    domain=column.domain,
                    flow=flow)
        self.column = column

    def __basis__(self):
        return (self.column, self.space)


class ScalarUnit(CompoundUnit):
    """
    Represents a scalar unit.

    A scalar unit is an expression evaluated in the specified space.

    Recall that any expression has the following form:

        `F(u(a),v(b),...)`,

    where

    - `F` is a scalar function;
    - `a`, `b`, ... are elements of spaces `A`, `B`, ...;
    - `u`, `v`, ... are unit expressions on `A`, `B`, ....

    We require that the units of the expression are singular on the given
    space.  If so, the expression units `u`, `v`, ... could be lifted to
    the given slace (see :class:`Unit`).  The scalar unit is defined as

        `F(u(x),v(x),...)`,

    where `x` is an element of the space where the scalar unit is defined.

    `code` (:class:`Code`)
        The expression to evaluate.

    `space` (:class:`Space`)
        The space on which the unit is defined.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        exporting the unit must also export extra scalar units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.
    """

    def __init__(self, code, space, flow, companions=[]):
        assert isinstance(companions, listof(Code))
        super(ScalarUnit, self).__init__(
                    code=code,
                    space=space,
                    domain=code.domain,
                    flow=flow)
        self.companions = companions

    def __basis__(self):
        return (self.code, self.space)


class AggregateUnitBase(CompoundUnit):
    """
    Represents an aggregate unit.

    Aggregate units express functions on sets.  Specifically, let `A` and `B`
    be spaces such that `B` spans `A`, but  `A` does not span `B`, and
    let `g` be a function that takes subsets of `B` as an argument.  Then
    we could define an aggregate unit `u` on `A` as follows:

        `u(a) = g({b | a <-> b})`

    Here, for each row `a` from `A`, we take the subset of convergent
    rows from `B` and apply `g` to it; the result is the value of `u(a)`.

    The space `A` is the unit space, the space `B` is called *the plural
    space* of an aggregate unit, and `g` is called *the composite expression*
    of an aggregate unit.

    `code` (:class:`Code`)
        The composite expression of the aggregate unit.

    `plural_space` (:class:`Space`)
        The plural space of the aggregate unit, that is, the space
        which subsets form the argument of the composite expression.

    `space` (:class:`Space`)
        The space on which the unit is defined.
    """

    def __init__(self, code, plural_space, space, flow):
        assert isinstance(code, Code)
        assert isinstance(plural_space, Space)
        # FIXME: consider lifting the requirement that the plural
        # space spans the unit space.  Is it really necessary?
        assert plural_space.spans(space)
        assert not space.spans(plural_space)
        super(AggregateUnitBase, self).__init__(
                    code=code,
                    space=space,
                    domain=code.domain,
                    flow=flow)
        self.plural_space = plural_space

    def __basis__(self):
        return (self.code, self.plural_space, self.space)


class AggregateUnit(AggregateUnitBase):
    """
    Represents a regular aggregate unit.

    A regular aggregate unit is expressed in SQL using an aggregate
    expression with ``GROUP BY`` clause.

    `companions` (list of :class:`Code`)
        An auxiliary hint to the compiler indicating that the term
        exporting the unit must also export extra aggregate units.
        The value of this attribute has no effect on the semantics of
        the space graph and node comparison.
    """

    def __init__(self, code, plural_space, space, flow, companions=[]):
        assert isinstance(companions, listof(Code))
        super(AggregateUnit, self).__init__(code, plural_space, space, flow)
        self.companions = companions


class CorrelatedUnit(AggregateUnitBase):
    """
    Represents a correlated aggregate unit.

    A correlated aggregate unit is expressed in SQL using a correlated
    subquery.
    """


class KernelUnit(CompoundUnit):
    """
    Represents a value generated by a quotient space.

    A value generated by a quotient is either a part of a kernel
    expression or a unit from a ground space.

    `code` (:class:`Code`)
        An expression (calculated against the seed space of the quotient).

    `space` (:class:`Space`)
        The space of the quotient family on which the unit is defined.
    """

    def __init__(self, code, space, flow):
        assert space.family.is_quotient
        super(KernelUnit, self).__init__(
                    code=code,
                    space=space,
                    domain=code.domain,
                    flow=flow)

    def __basis__(self):
        return (self.code, self.space)


class CoveringUnit(CompoundUnit):
    """
    Represents a value generated by a covering space.

    A covering space represents another space expression as
    a single axial space operation.

    `code` (:class:`Code`)
        An expression (calculated against the seed space of
        the covering space).

    `space` (:class:`Space`)
        The space on which the unit is defined.
    """

    def __init__(self, code, space, flow):
        assert isinstance(space, (ComplementSpace,
                                 MonikerSpace,
                                 ForkedSpace,
                                 AttachSpace,
                                 ClippedSpace,
                                 LocatorSpace))
        super(CoveringUnit, self).__init__(
                    code=code,
                    space=space,
                    domain=code.domain,
                    flow=flow)

    def __basis__(self):
        return (self.code, self.space)