App SRM Unit 4 Notes
App SRM Unit 4 Notes
• Actually , every logic program needs facts to work with so that it can
achieve the given goal.
• Facts basically are true statements about the program and data.
• For example, Delhi is the capital of India.
Rules
• Rules are the constraints which allow us to make conclusions about
the problem domain. Rules basically written as logical clauses to
express various facts. For example, if we are building any game then
all the rules must be defined.
• Rules are very important to solve any problem in Logic Programming.
Rules are basically logical conclusion which can express the facts.
Following is the syntax of rule −
• A∶− B1,B2,...,Bn. Here, A is the head and B1, B2, ... Bn is the body.
• For example − ancestor(A,B) :- father(A,B).
ancestor(A,C) :- father(A,B), ancestor(B,C).
• This can be read as, for every A and B, if A is the father of B and B is
an ancestor of C, Ais the ancestor of C. For every A and B, A is the
ancestor of C, if A is the father of B and B is an ancestor of C.
Logic programming in python
• For starting logic programming in Python, we need to install the
following two packages −
• Kanren:It provides us a way to simplify the way we made code for
business logic. It lets us express the logic in terms of rules and facts.
The following command will help you install kanren −pip install
kanren
• SymPy:SymPy is a Python library for symbolic mathematics. It aims
to become a full-featured computer algebra system (CAS) while
keeping the code as simple as possible in order to be comprehensible
and easily extensible. The following command will help you install
SymPy −pip install sympy
>>>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))
>>> run(0,(a,b,c),eq(expr1,expression)) #Calls to run()
First class function
• If a function can be assigned to a variable or passed as object/variable to other function, that function is
called as first class function.
def square(x): return x * x
def cube(x): return x * x * x
>>> run(0, x,
... neq(x, 1), # Not "equal" to 1
... neq(x, 3), # Not "equal" to 3
... membero(x, (1, 2, 3)))
(2,)
context.assumePredicate("is a fruit(banana)")
context.assumePredicate("is a fruit(apple)")
context.setTermDomain(range(10))
# tell the context about our predicate function, with arity 2
context.setPredicateFunction(greater_than,2)