Sie sind auf Seite 1von 6

Assignment No A-04

Aim
Implementation of Unification algorithm.

Pre-requisite
1. Discrete Structures.
2. Programming language basics.

Objective
1. To understand idea of unification.
2. To implement sample application using concept of unification.

Problem Statement
Write a program to implement the unification algorithm.

Hardware / Software Used


1. Python Programming

Mathematical Model
M = { s, e, X, Y, Fme, DD, NDD, success, failure, Memshared, CPUCoreCount }

1. s = Initial state - Define some variables and expressions.


2. e = End state - List which represent compositions of substitutions.
3. X = Input- List which are to be unified.
4. Y = Output- List which represent compositions of substitutions.
5. Fme = unify(x, y, s) where x and y are variables , constants, lists or Expressions and s
is a substitution that would make x and y equal or None if x and y can not unify.
6. DD = Deterministic data- List which are to be unified.
7. NDD = Non-deterministic data- Substitute variables.
8. Success = List is unified.
9. Failure = Proper substitutions are not done.
10. Memshared = Shared memory not used.
11. CPUCoreCount = 1.

Theory
Unification Algorithm
1. It is a process of finding substitutions for lifted inference rules, which can make different
logical expression to look similar (identical).
2. Unification is an procedure for determining substitutions needed to make two first order
logic expressions match.
3. Unification is important component of all first order logic inference algorithms.
4. The unification algorithm takes two sentences and returns a unifier for them, if one exists.
THE ALGORITHM STATEMENT IS AS FOLLOWS :Unify (p,q) = a where,
SUBST (a,p) = SUBST (a,q).
Input : Two lists, L1 and L2 which are to be unified.
Output : A list representing the composition of the substitutions that were performed during
the match.

Procedure
Execute command
Python A4 unification.py

Conclusion
Hence,Unification algorithm is implemented successfully.

Steps in Unification Algorithm


=================================================
GROUP A
Assignment No : A4
Title : Implementation of Unification algorithm.
Roll No :
Batch : B
Class : BE ( Computer )
=================================================
def unify(x, y, s):
if s is None:
return None
elif x == y:
return s
elif is variable(x):
return unify var(x, y, s)
elif is variable(y):
return unify var(y, x, s)
elif isinstance(x, Expr) and isinstance(y, Expr):
return unify(x.args, y.args, unify(x.op, y.op, s))
elif isinstance(x, str) or isinstance(y, str):
return None
elif issequence(x) and issequence(y) and len(x) == len(y):
if not x: return s
return unify(x[1:], y[1:], unify(x[0], y[0], s))
else:
return None
def is variable(x):
return isinstance(x, Expr) and not x.args and is var symbol(x.op)
def unify (var, x, s):
if var in s:
return unify(s[var], x, s)
elif occur check(var, x, s):
return None
else:
return extend(s, var, x)
def occur check(var, x, s):
if var == x:
return True
elif is v ariable(x) and x in s:
return occur check(var, s[x], s)
elif isinstance(x, Expr):
return (occur check(var, x.op, s) or
occur check(var, x.args, s))
elif isinstance(x, (list, tuple)):
return some(lambda element: occur check(var, element, s), x)
else:
return False
def extend(s, var, val):
4

s2 = s.copy()
s2[var] = val
return s2
def subst(s, x):
if isinstance(x, list):
return [subst(s, xi) for xi in x]
elif isinstance(x, tuple):
return tuple([subst(s, xi) for xi in x])
elif not isinstance(x, Expr):
return x
elif is var symbol(x.op):
ret urn s.get(x, x)
else:
return Expr(x.op, *[subst(s, arg) for arg in x.args])
def fol fc ask(KB, alpha):
while True:
new =
for r in KB.clauses:
ps, q = parse definite clause(standardize variables(r))
raise NotImplementedError
def standardize variables(sentence, dic=None):
if dic is None: dic =
if not isinstance(sentence, Expr):
return sentence
elif is var symbol(sentence.op):
if sentence in dic:
return dic[sentence]
else:
v = Expr(v % d % standardize variables.counter.next())
dic[sentence] = v
return v
else:
return
Expr(sentence.op,*[standardize variables(a, dic) for a in sentence.args])
standardize variables.counter = itertools.count()

Plagiarism Score

Das könnte Ihnen auch gefallen