We will describe a set just as a list. Each item is a name of item or a name of another set.
all_sets[] is a dictionary that stores all sets currently known in our world. A repository of all sets, so to say.
#!/usr/bin/env python3 # all sets known/registered in our world all_sets={} # some settings for a primitive toy graphics editor: # 3 elements: all_sets["colors"]=["red", "green", "blue"] all_sets["shapes"]=["circle", "square", "line"] # 4 elements: all_sets["line_styles"]=["solid", "dotted", "dashed", "dashdot"] # dashdot: -.-.-.-.-.- all_sets["fonts"]=["italic", "bold", "slant", "serif"] # self-reference. # all_sets_of_3_elements also contains 3 elements: all_sets["all_sets_of_3_elements"]=["colors", "shapes", "all_sets_of_3_elements"] # self-reference. # directly or transitively: all_sets["all_sets_containing_red"]=["colors","all_sets_containing_red"] # no self-reference: all_sets["all_sets_of_4_elements"]=["line_styles","fonts"] # self-reference. all_sets["all_sets_with_names_starting_with_a"]=["all_sets_of_3_elements", "all_sets_of_4_elements", "all_sets_containing_red", "all_sets_with_names_starting_with_a"] def set_has_self_reference(set_name): return set_name in all_sets[set_name] def enum_all_sets_with_no_self_reference(): tmp=[] for set_name in all_sets.keys(): if set_has_self_reference(set_name)==False: tmp.append(set_name) return tmp all_sets["set_of_sets_with_no_self_reference"]=enum_all_sets_with_no_self_reference() print (all_sets["set_of_sets_with_no_self_reference"]) # ['colors', 'shapes', 'line_styles', 'fonts', 'all_sets_of_4_elements'] all_sets["set_of_sets_with_no_self_reference"]=enum_all_sets_with_no_self_reference() print (all_sets["set_of_sets_with_no_self_reference"]) # ['colors', 'shapes', 'line_styles', 'fonts', 'all_sets_of_4_elements', 'set_of_sets_with_no_self_reference'] all_sets["set_of_sets_with_no_self_reference"]=enum_all_sets_with_no_self_reference() print (all_sets["set_of_sets_with_no_self_reference"]) # ['colors', 'shapes', 'line_styles', 'fonts', 'all_sets_of_4_elements']
When a set has an element of itself, you can think of recursion here.
You see -- it's impossible to construct a set_of_sets_with_no_self_reference (try this manually). At each iteration, a link to itself is added, then removed, then added, etc. This is a fundamental flaw of naive set theory. As well as a bug in this code -- it's impossible to write such a code that will produce such a set.
If you read a Wikipedia article about paradox, you can find: Let us call a set "normal" if it is not a member of itself, and "abnormal" if it is a member of itself. Clearly every set must be either normal or abnormal. In our terms, "normal" set is a set with no self-references. "Abnormal" sets are sets with self-references.
If you're familiar with the Barber paradox, ones who shaves everyone else except himself/herself is the analogy of "normal" set with no self-references. Ones who shaves themselves is the analogy of "abnormal" set with self-references. Now you can't classify a barber. (In real life, of course, no one forces you to be a member of either first or the second group.)
Yes, I know about these lousy Disqus ads. Please use adblocker. I would consider to subscribe to 'pro' version of Disqus if the signal/noise ratio in comments would be good enough.