# closure property of datatype “tuple” in python

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,

1. Like addition in case of real numbers, what is the operation on which `tuple` hold closure property?

2. 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.

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.

• Is `both` and `counts` hierarchical in this code? – overexchange Mar 13 '15 at 13:07
• @overexchange: well, `both` is 'hierarchical' because its elements are not atomic, they are also tuples. – 9000 Mar 13 '15 at 16:08
• @9000 so that mean, closure property of tuples permit hierarchical structure. – overexchange Mar 13 '15 at 16:21
• @overexchange: yes, closure property of tuples under the element substitution operation, that is, using a tuple as an element of another tuple, permits hierarchical structure. – 9000 Mar 13 '15 at 18:09
• @overexchange: yes, this is the idea. – 9000 Mar 14 '15 at 13:52

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.

• How could asking what operation is involved be an odd question? Closure is an idea which is only defined on an operation over a given set. He knows the set is 'tuples', but doesn't know the operation. Why give him a hard time over asking the obvious question? – Michael Shaw Mar 12 '15 at 21:50
• @MichaelShaw - the way it is phrased implies that there is a singular operation, or a special operation that has the property. I want to make it clear that such an assumption is incorrect since that would greatly impair understanding. – Telastyn Mar 12 '15 at 22:23

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.