# Understanding Logic Programming

List of states that are adjacent to Arkansas and Kentucky:') for item in output: print(item)The full code is given in states.

py.

If you run the code, you will see the following output:You can cross-check the output with the US map to verify if the answers are right.

You can also add more questions to the program to see if it can answer them.

Building a puzzle solverAnother interesting application of logic programming is in solving puzzles.

We can specify the conditions of a puzzle and the program will come up with a solution.

In this section, we will specify various bits and pieces of information about four people and ask for the missing piece of information.

In the logic program, we specify the puzzle as follows:Steve has a blue carThe person who owns the cat lives in CanadaMatthew lives in USAThe person with the black car lives in AustraliaJack has a catAlfred lives in AustraliaThe person who has a dog lives in FranceWho has a rabbit?The goal is the find the person who has a rabbit.

Here are the full details about the four people:Create a new Python file and import the following packages:from logpy import * from logpy.

core import lallDeclare the variable people:# Declare the variable people = var()Define all the rules using lall.

The first rule is that there are four people:# Define the rules rules = lall( # There are 4 people (eq, (var(), var(), var(), var()), people),The person named Steve has a blue car:# Steve's car is blue (membero, ('Steve', var(), 'blue', var()), people),The person who has a cat lives in Canada:# Person who has a cat lives in Canada (membero, (var(), 'cat', var(), 'Canada'), people),The person named Matthew lives in USA:# Matthew lives in USA (membero, ('Matthew', var(), var(), 'USA'), people),The person who has a black car lives in Australia:# The person who has a black car lives in Australia (membero, (var(), var(), 'black', 'Australia'), people),The person named Jack has a cat:# Jack has a cat (membero, ('Jack', 'cat', var(), var()), people),The person named Alfred lives in Australia:# Alfred lives in Australia (membero, ('Alfred', var(), var(), 'Australia'), people),The person who has a dog lives in France:# Person who owns the dog lives in France (membero, (var(), 'dog', var(), 'France'), people),One of the people in this group has a rabbit.

Who is that person?# Who has a rabbit.(membero, (var(), 'rabbit', var(), var()), people) )Run the solver with the preceding constraints:# Run the solver solutions = run(0, people, rules)Extract the output from the solution:# Extract the output output = [house for house in solutions[0] if 'rabbit' in house][0][0]Print the full matrix obtained from the solver:# Print the output print('.' + output + ' is the owner of the rabbit') print('.Here are all the details:') attribs = ['Name', 'Pet', 'Color', 'Country'] print('.' + ' '.

join(attribs)) print('=' * 57) for item in solutions[0]: print('') print(' '.

join([str(x) for x in item]))The full code is given in puzzle.

py.

If you run the code, you will see the following output:The preceding figure shows all the values obtained using the solver.

Some of them are still unknown as indicated by numbered names.

Even though the information was incomplete, our solver was able to answer our question.

But in order to answer every single question, you may need to add more rules.

This program was to demonstrate how to solve a puzzle with incomplete information.

You can play around with it and see how you can build puzzle solvers for various scenarios.

.