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 electric cars = 18 $\implies$ Electric SUVs = $18 - 5 = 13$
In Prolog, we write facts and rules, and then ask Prolog to satisfy a goal.
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:
Share your code
Show a friend, family member, or teacher what you've done!
Here is a share link to your code:
Need a hint?
💡 What now?
This Prolog code has ???? in each arithmetic rule. Replace the ???? with the appropriate arithmetic expressions: T - S, Sed - Gs, and Et - Es. Then hit Run to let Prolog deduce the number of electric SUVs!