Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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.

share|improve this question
2  
consider cs.stackexchange.com as another place to ask this question –  A Red Herring Mar 11 at 10:17
2  
In general, please avoid including images of text - it makes it inaccessible to people using screen-readers, etc., and impossible to copy-paste for use in answers/searches. See e.g. meta.stackoverflow.com/q/284577/3001761 –  jonrsharpe Mar 11 at 11:07

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
Is both and counts hierarchical in this code? –  overexchange Mar 13 at 13:07
    
@overexchange: well, both is 'hierarchical' because its elements are not atomic, they are also tuples. –  9000 Mar 13 at 16:08
    
@9000 so that mean, closure property of tuples permit hierarchical structure. –  overexchange Mar 13 at 16:21
1  
@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 at 18:09
1  
@overexchange: yes, this is the idea. –  9000 Mar 14 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.

share|improve this answer
    
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 at 21:50
1  
@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 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.