Closure Properties of Formal Languages: Worked Examples and Exam Traps

Learn what closure means at the language-class level. Build a product DFA, disprove CFL intersection closure, and use known results without falling into common traps.

KnowledgeGate Team

Exam prep & CS education

Updated 20 Sep 20265 min read

Students often memorise a closure table, then lose the answer when a question changes the language class, operation, or alphabet. A safer method is to define closure precisely, prove one positive result by construction, disprove one claim by counterexample, and turn those ideas into a decision process.

Related reading: regular language closure and context-free languages.

Closure property: what "closed" actually means

A language class C\mathcal C is closed under a binary operation ∘\circ when

L1,L2∈CL_1,L_2\in\mathcal C always implies L1∘L2∈CL_1\circ L_2\in\mathcal C. For a unary operation such as complement or reversal, L∈CL\in\mathcal C must imply that the resulting language is also in C\mathcal C.

The word always carries the definition. One selected language may produce a regular-looking result, and one selected pair may survive an operation, but neither proves closure of the class. One counterexample disproves closure. A positive closure claim needs a general construction or theorem.

The operations used here are union, intersection, complement relative to a fixed alphabet Σ\Sigma, difference, concatenation, Kleene star, reversal, homomorphism, and inverse homomorphism. Place these within the wider subject through this Theory of Computation guide.

The exam-safe closure map

For the six core operations below, regular and recursive languages are closed in every column. CFLs are not closed under arbitrary intersection or complement. Recursively enumerable languages are not closed under complement.

Class

Union

Intersection

Complement

Concatenation

Kleene star

Reversal

Regular

✓

✓

✓

✓

✓

✓

Context-free (CFL)

✓

✗

✗

✓

✓

✓

Recursive/decidable

✓

✓

✓

✓

✓

✓

Recursively enumerable/recognisable

✓

✓

✗

✓

✓

✓

Keep the qualifications beside the table. A CFL intersected with a regular language is a CFL. Deterministic CFLs are closed under complement and under intersection with a regular language, but not under arbitrary union or intersection. Context-sensitive languages require care around erasing homomorphisms, so do not copy an unqualified rule into that class.

Regular languages and CFLs are closed under both homomorphism and inverse homomorphism. For stronger classes, check the theorem's exact conditions, especially whether erasing is allowed.

Worked example 1: construct closure with a product DFA

Let Σ={0,1}\Sigma=\{0,1\}. Language L1L_1 contains strings with an even number of 1s. Its DFA has states EE and OO, starts and accepts at EE, keeps its state on 0, and toggles on 1. Language L2L_2 contains strings ending in 0. Its states are NN and ZZ, with start state NN, accept state ZZ, every 0 moving to ZZ, and every 1 moving to NN.

The reachable product states and all transitions are:

Product state

On 0

On 1

(E,N)(E,N)

(E,Z)(E,Z)

(O,N)(O,N)

(E,Z)(E,Z)

(E,Z)(E,Z)

(O,N)(O,N)

(O,N)(O,N)

(O,Z)(O,Z)

(E,N)(E,N)

(O,Z)(O,Z)

(O,Z)(O,Z)

(E,N)(E,N)

The start state is (E,N)(E,N). For L1∩L2L_1\cap L_2, only (E,Z)(E,Z) accepts. For L1∪L2L_1\cup L_2, the accepting states are (E,N),(E,Z),(O,Z)(E,N),(E,Z),(O,Z).

Trace 1010:

\[

(E,N)\xrightarrow{1}(O,N)\xrightarrow{0}(O,Z)\xrightarrow{1}(E,N)\xrightarrow{0}(E,Z).

\]

It has two 1s and ends in 0, so the intersection DFA accepts it. By contrast, 10 finishes at (O,Z)(O,Z). It ends in 0 but has one 1, so the union accepts it and the intersection rejects it.

Product DFA makes regular-language closure concrete. Draw four states labelled (E,N), (E,Z), (O,N), and (O,Z), with (E,N) as the incoming-arrow start state and (E,Z) double-circled for the intersection. Show every transition exactly: (E,N) --0--> (E,Z), (E,N) --1--> (O,N); (E,Z) --0--> (E,Z), (E,Z) --1--> (O,N); (O,N) --0--> (O,Z), (O,N) --1--> (E,N); (O,Z) --0--> (O,Z), (O,Z) --1--> (E,N). Beside the graph, show the trace 1010 as (E,N) --1--> (O,N) --0--> (O,Z) --1--> (E,N) --0--> (E,Z), ending with “accept: even two 1s and final symbol 0”. Add a small union legend listing its accepting states as {(E,N), (E,Z), (O,Z)}; do not change the graph’s double circle, which represents intersection.

Worked example 2: disprove CFL closure with one intersection

Define

L1={aibicj∣i,j≥0}L_1=\{a^i b^i c^j\mid i,j\geq0\}. The grammar S→ACS\to AC, A→aAb∣εA\to aAb\mid\varepsilon, C→cC∣εC\to cC\mid\varepsilon generates it, so L1L_1 is context-free.

Define

L2={aibjcj∣i,j≥0}L_2=\{a^i b^j c^j\mid i,j\geq0\}. The grammar T→DBT\to DB, D→aD∣εD\to aD\mid\varepsilon, B→bBc∣εB\to bBc\mid\varepsilon establishes that L2L_2 is also context-free.

Membership in L1L_1 forces the number of a's to equal the number of b's. Membership in L2L_2 forces the number of b's to equal the number of c's. Therefore

\[

L_1\cap L_2=\{a^n b^n c^n\mid n\geq0\}.

\]

The string aabbcc is in the intersection for n=2n=2. The string aaabbbcc is only in L1L_1, while aabbbccc is only in L2L_2. The language {anbncn∣n≥0}\{a^n b^n c^n\mid n\geq0\} is not context-free. Thus, two CFL inputs have produced a non-CFL intersection, which disproves closure under arbitrary intersection. Review the machine connection in Context-Free Grammars and Pushdown Automata.

One counterexample disproves CFL intersection closure. Draw two overlapping sets labelled L1 = {a^i b^i c^j | i,j >= 0} and L2 = {a^i b^j c^j | i,j >= 0}. Put aaabbbcc in the L1-only region and annotate “3 a, 3 b, 2 c”; put aabbbccc in the L2-only region and annotate “2 a, 3 b, 3 c”. Label the overlap L1 ∩ L2 = {a^n b^n c^n | n >= 0}, list epsilon, abc, and aabbcc inside it, and add “not context-free” immediately below the overlap label. Do not add any other sample strings or alter their counts.

Turn known closures into new answers

Suppose CFLs were closed under complement. They are already closed under union, so De Morgan's law would give

\[

L_1\cap L_2=\overline{\overline{L_1}\cup\overline{L_2}}

\]

as another CFL. The counterexample above contradicts that result. Therefore CFLs cannot be closed under complement.

Now let RR be regular and CC be context-free. Then C∖R=C∩R‾C\setminus R=C\cap\overline R is context-free: R‾\overline R is regular, and intersecting a CFL with a regular language preserves context-freeness. Swapping the roles does not support the same reasoning. For R∖C=R∩C‾R\setminus C=R\cap\overline C, the needed CFL complement closure is unavailable.

Two boundary checks prevent easy mistakes. Complement is taken over a stated alphabet. If \(L=\{w\in\{0,1\}^:w\text{ ends in }0\}\), then L‾\overline L contains ε\varepsilon and every binary string ending in 1. Also, \(L^\) always contains ε\varepsilon, even when LL does not.

How questions disguise the operation

Consider this four-statement mini-question:

  • (a) Regular languages are closed under difference.

  • (b) CFLs are closed under arbitrary intersection.

  • (c) The intersection of a CFL and a regular language is a CFL.

  • (d) Recursively enumerable languages are closed under complement.

Statement (a) is true because L1−L2=L1∩L2‾L_1-L_2=L_1\cap\overline{L_2}. Statement (b) is false by the counterexample above. Statement (c) is true, and statement (d) is false. The answer is (a) and (c) only.

Use three passes. First, identify the exact input classes. Second, rewrite difference or symmetric difference using primitive operations. Third, check that every step uses an available closure property. For a "not closed" claim, seek one counterexample pair. For a "closed" claim, seek a general machine, grammar, or theorem. Common forms include direct table recall, product-automaton construction, a counterexample, and a De Morgan deduction.

Traps, recall card, and the next study step

Do not fall for these shortcuts:

  • "Not closed" does not mean every pair fails.

  • Closure belongs to a class, not to one language.

  • CFL intersection failure does not cancel CFL-with-regular closure.

  • DCFL complement closure must not be copied to all CFLs.

  • Complement needs a fixed universe Σ∗\Sigma^*.

  • A homomorphism theorem may require an erasing or non-erasing condition.

Keep this six-line recall card:

  • Regular is closed under every core operation in the table.

  • CFL is closed under union, concatenation, star, and reversal, but not arbitrary intersection or complement.

  • CFL intersected with regular stays context-free.

  • Recursive languages are closed under complement.

  • Recursively enumerable languages are not closed under complement.

  • A product construction proves positive regular closure; anbncna^n b^n c^n supplies the standard negative CFL-intersection witness.

Reconstruct both worked examples without looking. Then use the Theory of Computation / Automata Theory course for a focused subject pass. The ZERO TO HERO complete CS course is the broader multi-subject route, while the CS Fundamentals category is the comparison page.