0% found this document useful (0 votes)
42 views

Experiment No 1 - Logic Programming

This document discusses implementing logical rules in Python using logic programming. It includes 5 programs: 1) Using logic programming to compare expressions and find unknown values. 2) Checking for prime numbers using logic programming. 3) Using logic programming to solve puzzles like Zebra and 8-puzzles. 4) Implementing the FIND-S algorithm to find the most specific hypothesis from training data in a CSV file. 5) Open for a program of the reader's choice about logic programming in Python.

Uploaded by

Salil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Experiment No 1 - Logic Programming

This document discusses implementing logical rules in Python using logic programming. It includes 5 programs: 1) Using logic programming to compare expressions and find unknown values. 2) Checking for prime numbers using logic programming. 3) Using logic programming to solve puzzles like Zebra and 8-puzzles. 4) Implementing the FIND-S algorithm to find the most specific hypothesis from training data in a CSV file. 5) Open for a program of the reader's choice about logic programming in Python.

Uploaded by

Salil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AIM 1: Implementation of logical rules in Python.

Requirements:

Theoretical Description:

Program 1: Compare expressions and find out unknown values.

>>> from kanren import run,var,fact


>>> from kanren.assoccomm import eq_assoccomm as eq
>>> from kanren.assoccomm import commutative,associative
>>> add='add' #Defining operations
>>> mul='mul'
>>> fact(commutative,mul) #Addition and multiplication are commutative and associative
>>> fact(commutative,add)
>>> fact(associative,mul)
>>> fact(associative,add)
>>> a,b,c=var('a'),var('b'),var('c') #Defining variables
>>> #2ab+b+3c is the expression we have'
>>> expression=(add, (mul, 2, a, b), b, (mul, 3, c))
>>> expression=(add,(mul,3,-2),(mul,(add,1,(mul,2,3)),-1)) #Expression
>>> expr1=(add,(mul,(add,1,(mul,2,a)),b),(mul,3,c)) #Expressions to match
>>> expr2=(add,(mul,c,3),(mul,b,(add,(mul,2,a),1)))
>>> expr3=(add,(add,(mul,(mul,2,a),b),b),(mul,3,c))

Program 2: Checking for Prime Numbers in Python Logic Programming

>>> from kanren import isvar,run,membero

>>> from kanren.core import success,fail,goaleval,condeseq,eq,var

>>> from sympy.ntheory.generate import prime,isprime

>>> import itertools as it

>>> def prime_test(n): #Function to test for prime


if isvar(n):

return condeseq([(eq,n,p)] for p in map(prime,it.count(1)))

else:

return success if isprime(n) else fail

>>> n=var() #Variable to use

>>>
ret(run(0,n,(membero,n,(12,14,15,19,21,20,22,29,23,30,41,44,62,52,65,85)),(prime_test,n)))

Program 3: Logic programming can be used to solve many problems like 8-puzzles,
Zebra puzzle

rules_zebraproblem = lall(
(eq, (var(), var(), var(), var(), var()), houses),

(membero,('Englishman', var(), var(), var(), 'red'), houses),


(membero,('Swede', var(), var(), 'dog', var()), houses),
(membero,('Dane', var(), 'tea', var(), var()), houses),
(left,(var(), var(), var(), var(), 'green'),
(var(), var(), var(), var(), 'white'), houses),
(membero,(var(), var(), 'coffee', var(), 'green'), houses),
(membero,(var(), 'Pall Mall', var(), 'birds', var()), houses),
(membero,(var(), 'Dunhill', var(), var(), 'yellow'), houses),
(eq,(var(), var(), (var(), var(), 'milk', var(), var()),
var(), var()), houses),
(eq,(('Norwegian', var(), var(), var(), var()), var(), var(),
var(), var()), houses),
(next,(var(), 'Blend', var(), var(), var()),
(var(), var(), var(), 'cats', var()), houses),
(next,(var(), 'Dunhill', var(), var(), var()),
(var(), var(), var(), 'horse', var()), houses),

Program 4: Implement and demonstratethe FIND-Salgorithm for finding the most


specific hypothesis based on a given set of training data samples. Read the training data
from a
.CSV file.

import csv
with open('tennis.csv', 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
h = [['0', '0', '0', '0', '0', '0']]
for i in your_list:
print(i)
if i[-1] == "True":
j=0
for x in i:
if x != "True":
if x != h[0][j] and h[0][j] == '0':
h[0][j] = x
elif x != h[0][j] and h[0][j] != '0':
h[0][j] = '?'
else:
pass
j=j+1
print("Most specific hypothesis is")
print(h)
Output
'Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same',True
'Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same',True
'Rainy', 'Cold', 'High', 'Strong', 'Warm', 'Change',False
'Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change',True
Maximally Specific set
[['Sunny', 'Warm', '?', 'Strong', '?', '?']]

Program 5: your choice about Logic Programming in Python.

You might also like