Lesson goal: Reasoning about Sets and Venn Diagrams with AI

Previous: Looking for rectangles | Home | Next: Euclid's GCD and reducing fractions

In logic and set theory, Venn diagram problems involve breaking down a collection of items into overlapping categories. Instead of computing formulas on a calculator, we can use Prolog — a declarative programming language widely used in Artificial Intelligence — to state facts about the categories and let the computer deduce the answer through logical inference! Consider a puzzle inspired by Problem #10 from the 1971 MAA High School Mathematics Contest: A car dealership has 50 cars on its lot. Each car is either an SUV or a Sedan, and each car is either Electric or Gas-powered. If 31 are SUVs, 18 are electric, and 14 are gas-powered sedans, then how many electric SUVs are on the lot? Let's represent the given information logically:
  • Total cars = 50
  • SUVs = 31 $\implies$ Sedans = $50 - 31 = 19$
  • Gas-powered sedans = 14 $\implies$ Electric sedans = $19 - 14 = 5$
  • Total electric cars = 18 $\implies$ Electric SUVs = $18 - 5 = 13$
In Prolog, we write facts and rules, and then ask Prolog to satisfy a goal.
total_cars(50).
sedans(N) :- total_cars(T), suvs(S), N is T - S.

electric_suvs(Ans) :- electric_total(Et), electric_sedans(Es), Ans is Et - Es.

goal: electric_suvs(Ans).
Move the mouse over a dotted box for more information.

In Prolog, the symbol :- means "if", and uppercase letters (like Ans, T, S) denote variables that Prolog will solve for automatically.

Now you try. Replace ???? in the three rules with T - S, Sed - Gs, and Et - Es. Run the goal to see Prolog solve for Ans = 13!

Type your code here:


See your results here: