From this link, below is the slide that I would like to understand:
The Closure Property of Data Types
- A method for combining data values satisfies the closure property if:
- The result of combination can itself be combined using the same method.
- Closure is the key to power in any means of combination because it permits us to create hierarchical structures.
- Hierarchical structures are made up of parts, which themselves are made up of parts, and so on.
Tuples can contain tuples as elements
In math, we know that,
The closure property of real number addition states that when we add real number to other real number the result is also real. For example, 3 + 6 = 9 .
I think the above slide also talks about tuple
on these same lines.
My question:
As per above slide,
-
Like addition in case of real numbers, what is the operation on which
tuple
hold closure property? -
Can you explain how the closure property(only) of
tuple
permits hierarchical structures? Because as per this code, it is “closure property + recursive property” of a data model permits a data model to be hierarchical.
2
Mathematical definition: Closure is defined for a particular operation over a particular set. Let’s say we had a set S
and we have 2 elements from S
, call them a
and b
, and we have an operation, call it @
that combines 2 elements of S
. Then @
is closed over S
if a@b
is always in S
, no matter which particular elements we picked.
So you have to specify both an operation and a set if you want to be perfectly clear about what is meant by ‘closure’. (Sometimes people omit one or both if they think context makes it clear enough what they mean.)
In programming, types often show up where in math you’d expect a set. You can think of a type as the set of all possible values of that type, and a value being of a particular type as that value belonging to the corresponding set.
With that background: the operation in this case is tupling (making a tuple) and the set/type over which we have closure is the set of tuples. For instance, (a,b)
and (c,d)
are tuples, and combining them makes another tuple: ((a,b), (c,d))
.
The important thing to get from that slide is not the word ‘closure’ or even its mathematical definition, but that nestable data structures are powerful.
11
At the risk of repeating the previous solid, deleted answer…
Like addition in case of real numbers, what is the operation on which tuple hold closure property?
This is an odd question. There are infinitely many operations that work on tuple
(or any type for that matter), which will return some type that the operation can be applied to again. It’s like asking which sentence ends in the letter L
.
Can you explain how the closure property(only) of tuple permits hierarchical structures? Because as per this code, it is “closure property + recursive property” of a data model permits a data model to be hierarchical.
You’re taking things too literally.
Recursion is not necessary for closure. Closure does not necessarily provide hierarchical structures. Consider this simple function:
T Identity(){ return this; }
Hopefully it is clear that this provides the necessary property that you can take the result of the function and perform the function on it again. But it is not recursive, and it is not hierarchical. It isn’t a “combination”, but it’s trivial to add a parameter which is then discarded.
What the slide is trying to convey is that the closure property is a very useful pattern. Having an object that is itself the same type as its parts is well known in the OO world as the composite pattern. In functional languages, they are more often known as algebraic data types (though they’re so common there, that they often aren’t given a special name).
And it is powerful because it provides a concise way to define infinite, fractal-like structures like lists and trees that are the foundation of data structures; and by extension, a foundation of computing.
2
There’s no just ‘closure propery’, there’s a ‘closure under operation X‘. For instance, integers are closed both under addition and multiplication.
Tuples are closed under many operations, one of them being ‘element composition’, that is, you can use a tuple as an element of another tuple. This particular property obviously, by construction, allows to build recursive structures. Compare it to nodes in a tree.
There are other operations under which tuples are also closed, e.g. ‘merging’ of tuples (expressed by +
) that do not allow for arbitrary recursive structures, but allow theoretically arbitrary length, etc.