Unit 4
Unit 4
• Functional programming is a programming paradigm in which it is tried to bind each and everything in pure mathematical
functions. It is a declarative type of programming style that focuses on what to solve rather than how to solve.
• Functional programming paradigm is based on lambda calculus.
• Instead of statements, functional programming makes use of expressions. Unlike a statement, which is executed to assign
variables, evaluation of an expression produces a value.
• Functional programming is a declarative paradigm because it relies on expressions and declarations rather than statements.
Unlike procedures that depend on a local or global state, value outputs in FP depend only on the arguments passed to the
function.
• Functional programming consists only of PURE functions.
• In functional programming, control flow is expressed by combining function calls, rather than by assigning values to variables.
Example:
Characteristics of Functional Programming
• Functional programming method focuses on results, not the process
• Emphasis is on what is to be computed
• Data is immutable
• Functional programming Decompose the problem into 'functions
• It is built on the concept of mathematical functions which uses conditional expressions and recursion to do perform the
calculation
• It does not support iteration like loop statements and conditional statements like If-Else
• Functional programming languages are designed on the concept of mathematical functions that use conditional expressions
and recursion to perform computation.
• Functional programming supports higher-order functions and lazy evaluation features.
• Functional programming languages don’t support flow Controls like loop statements and conditional statements like If-Else and
Switch Statements. They directly use the functions and functional calls.
Concepts of FP
• Pure functions
• Recursion
• Referential transparency
• Functions are First-Class and can be Higher-Order
• Immutability
Functional Programming vs Procedure Programming
1. Pure functions
• Pure functions always return the same results when given the same inputs. Consequently, they have no side effects.
• Properties of Pure functions are:
• First, they always produce the same output for same arguments irrespective of anything else.
• Secondly, they have no side-effects i.e. they do modify any argument or global variables or output something.
• A simple example would be a function to receive a collection of numbers and expect it to increment each element of this
collection.
• We receive the numbers collection, use map with the inc function to increment each number, and return a new list of
incremented numbers.
Example:
def inc(x): Note : Function involving Reading files,
return x+1 using global data, random numbers are impure functions
list=[8,3,7,5,2,6]
x=map(inc,list) #print(list)
print(x)
Note: if a function relies on the global variable or class member’s data, then it is not pure. And in such cases, the return value of that
function is not entirely dependent on the list of arguments received as input and can also have side effects.
A side effect is a change in the state of an application that is observable outside the called function other than its return value
2. Recursion
• In the functional programming paradigm, there is no for and while loops. Instead, functional programming languages rely on
recursion for iteration. Recursion is implemented using recursive functions, which repetitively call themselves until the base
case is reached.
• The above code performs recursion task as the loop by calling itself with a new start and a new accumulator.
Referential transparency
• An expression is said to be referentially transparent if it can be replaced with its corresponding value without changing the
program's behaviour. As a result, evaluating a referentially transparent function gives the same value for fixed arguments. If a
function consistently yields the same result for the same input, it is referentially transparent.
• Functional programs don’t have any assignment statements. For storing additional values in a program developed using
functional programming, new variables must be defined. State of a variable in such a program is constant at any moment in
time
• In imperative programming, this means “take the current value of x, add 1 and put the result back into x.” In functional
programming, however, x = x + 1 is illegal. That’s because there are technically no variables in functional programming.
• Using immutable data structures, you can make single or multi-valued changes by copying the variables and calculating new
values,
• Since FP doesn’t depend on shared states, all data in functional code must be immutable, or incapable of changing
Functions are First-Class and can be Higher-Order
• A programming language is said to have First-class functions when functions in that language are treated like any other
variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by
another function and can be assigned as a value to a variable.
• Higher-order functions are functions that take at least one first-class function as a parameter
• Examples:
• name_lengths = map(len, ["Bob", "Rob", "Bobby"])
• Higher Order functions are map, reduce, filter
Functional style of getting a sum of a list: # or the pure functional way in python using higher order
function
new_lst = [1, 2, 3, 4]
def sum_list(lst):
if len(lst) == 1: import functools
return lst[0] print(functools.reduce(lambda x, y: x + y, new_lst))
else:
return lst[0] + sum_list(lst[1:])
print(sum_list(new_lst))
Functions are First-Class and can be Higher-Order
Map
map() can run the function on each item and insert the return values into the new collection. Example:
squares = map(lambda x: x * x, [0, 1, 2, 3, 4])
import random
names = ['Seth', 'Ann', 'Morganna']
team_names = map(lambda x: random.choice(['A Team','B Team']),names)
print names
// ['A Team', 'B Team', 'B Team']
reduce()
• reduce() is another higher order function for performing iterations. It takes functions and collections of items, and then it
returns the value of combining the items
Example:
sum = reduce(lambda a, x: a + x, [0, 1, 2, 3, 4])
print sum // 10
Functions are First-Class and can be Higher-Order ..cont
filter()
• filter function expects a true or false value to determine if the element should or should not be included in the result collection.
Basically, if the call-back expression is true, the filter function will include the element in the result collection. Otherwise, it will
not.
Example:
def f(x):
return x%2 != 0 and x%3 ==0
filter(f, range(2,25))
Functional Programming – Non Strict Evaluation
• In programming language theory, lazy evaluation, or call-by-need[1] is an evaluation strategy which delays the evaluation of an
expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations
• Allows Functions having variables that have not yet been computed
In Python, the logical expression operators and, or, and if-then-else are all non-strict. We sometimes call them short-circuit
operators because they don't need to evaluate all arguments to determine the resulting value.
The following command snippet shows the and operator's non-strict feature:
Syntax:
lambda argument(s): expression
Example:
remainder = lambda num: num % 2 [(lambda x: x*x)(x) for x in [2,6,9,3,6,4,8]]
print(remainder(5))
Functional Programming – Closure
Basically, the method of binding data to a function without actually passing them as parameters is called closure. It is a function
object that remembers values in enclosing scopes even if they are not present in memory.
Example:.
def counter(start=0, step=1):
x = [start]
def _inc():
x[0] += step
return x[0]
return _inc
c1 = counter()
c2 = counter(100, -10)
c1()
//1
c2()
90
Pure Functions in Python
• If a function uses an object from a higher scope or random numbers, communicates with files and so on, it might be impure
Built-in Higher Order Functions
Map
• The map function allows us to apply a function to every element in an iterable object
Filter
• The filter function tests every element in an iterable object with a function that returns either True or False, only keeping those
which evaluates to True.
Combining map and filter
• As each function returns an iterator, and they both accept iterable objects, we can use them together for some really
expressive data manipulations!
List Comprehensions
• A popular Python feature that appears prominently in Functional Programming Languages is list comprehensions. Like the map
and filter functions, list comprehensions allow us to modify data in a concise, expressive way.
Anonymous Function
• In Python, anonymous function is a function that is defined without a name.
• While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda
keyword.
Characteristics of Python lambda functions:
• A lambda function can take any number of arguments, but they contain only a single expression. An expression is a piece of code
executed by the lambda function, which may or may not return any value.
• Lambda functions can be used to return function objects.
• Syntactically, lambda functions are restricted to only a single expression.
Syntax of Lambda Function in python
lambda arguments: expression
Example:
double = lambda x: x * 2 product = lambda x, y : x * y
print(double(5)) print(product(2, 3))
# Output: 10
Note: you want to pass a function as an argument to higher-order functions, that is, functions that take other functions as their
arguments.
map() Function
Example Map with lambda Example with Map
tup= (5, 7, 22, 97, 54, 62, 77, 23, 73, 61) from math import sqrt
list_a = [1, 2, 3] #splitting the input and convert to int using map
Syntax:
map(function, iterables)
s = functools.reduce(lambda x,y:x+y,even)
Examples
//Closure
def twice(x):
def square():
return x*x
return square()*square()
def quad(x):
return twice(x)
print(quad(3))
Examples
//partial to make some of the parameters as fixed
import functools
def s_total(a,b,c):
return a*b+c #a=5,b=15, c=from list
s = functools.partial(s_total,5,15)
l= [13,54,76,89,10]
for i in l:
print(s(i))
Function vs Procedure
S.No Functional Paradigms Procedural Paradigm
1 Treats computation as the evaluation of mathematical Derived from structured programming, based on the
functions avoiding state and mutable data concept of modular programming or the procedure call
2 Main traits are Lambda Main traits are Local variables, sequence,
alculus, compositionality, formula, recursion, referential selection, iteration, and modularization
transparency
3 Functional programming focuses on expressions Procedural programming focuses on statements
4 Often recursive. Always returns the same output for a The output of a routine does not always have a direct
given input. correlation with the input.
6 Must be stateless. i.e. No operation can have side Execution of a routine may have side effects.
effects.
7 Good fit for parallel execution, Tends to emphasize a Tends to emphasize implementing solutions in a linear
divide and conquer approach. fashion.
Function vs Object Oriented
3 What it focuses is on: "What you are doing. in the What it focuses is on "How you are doing your
programme." programming."
4 Supports Parallel Programming. No supports for Parallel Programming.
5 Its functions have no-side effects. Method can produce many side effects.
6 Flow Control is performed using function calls & function Flow control process is conducted using loops and
calls with recursion. conditional statements.
7 Execution order of statements is not very important. Execution order of statements is important.
8 Supports both "Abstraction over Data" and "Abstraction Supports only "Abstraction over Data".
over Behavior."
Logical Programming
Paradigm
Logical Programming Paradigm
Logic programming is a paradigm where computation arises from proof search in a logic according to a fixed, predictable strategy. A logic is a
language. It has syntax and semantics. A logic is a language. It has syntax and semantics. More than a language, it has inference rules.
Syntax:
this is the rules about how to form formulas; this is usually the easy part of a logic.
Semantics:
About the meaning carried by the formulas, mainly in terms of logical consequences.
Inference rules:
Inference rules describe correct ways to derive conclusions
Logical Programming Paradigm
Logic :
A Logic program is a set of predicates.
Predicates :
Define relations between their arguments. Logically, a Logic program states what holds. Each predicate has a name, and zero or more
arguments. The predicate name is a atom. Each argument is an arbitrary Logic term. A predicate is defined by a collection of clauses.
Clause :
A clause is either a rule or a fact. The clauses that constitute a predicate denote logical alternatives: If any clause is true, then the whole
predicate is true.
Logical Programming Paradigm
Fact
A fact must start with a predicate (which is an atom). The predicate may be followed by one or more arguments which are enclosed by
parentheses. The arguments can be atoms (in this case, these atoms are treated as constants), numbers, variables or lists.
Facts are axioms; relations between terms that are assumed to be true.
Example facts:
+big('bear')
+big('elephant')
+small('cat')
+brown('bear')
+black('cat')
+grey('elephant')
Consider the 3 fact saying ‘cat’ is a smallest animal and fact 6 saying the elephant is grey in color
Logical Programming Paradigm
Rule
• Rules are theorems that allow new inferences to be made.
• Describe Relationships Using other Relationships.
Example
Two people are sisters if they are both female and have the same parents.
Gives a definition of one relationship given other relationships.
Both must be females.
Both must have the same parents.
If two people satisfy these rules, then they are sisters (according to our simplified relationship)
dark(X)<=black(X)
dark(X)<=brown(X)
Consider rule 1 saying the animal color is black its consider to be dark color animal
Logical Programming Paradigm
Queries
A query is a statement starting with a predicate and followed by its arguments, some of which are variables. Similar to goals, the predicate
of a valid query must have appeared in at least one fact or rule in the consulted program, and the number of arguments in the query must be
the same as that appears in the consulted program.
print(pyDatalog.ask('father_of(X,jess)'))
Output:
{('jack',)}
X
print(father_of(X,'jess'))
Output:
jack
X
Anatomy Logical Programming Paradigm
Fact
X +male('adam')
Predicat Clause
e
son(X,Y)<= male(X) & parent(Y,X)
Query
print(pyDatalog.ask('son(adam,Y)'))
Logical Programming Paradigm
from pyDatalog import pyDatalog
pyDatalog.create_atoms('parent,male,female,son,daughter,X,Y,Z')
+male('adam')
+female('anne')
+female('barney')
+male('james')
+parent('barney','adam')
+parent('james','anne')
#The first rule is read as follows: for all X and Y, X is the son of Y if there exists X and Y such that Y is the parent of X and X is male.
#The second rule is read as follows: for all X and Y, X is the daughter of Y if there exists X and Y such that Y is the parent of X and X is
female.
son(X,Y)<= male(X) & parent(Y,X)
daughter(X,Y)<= parent(Y,X) & female(X)
print(pyDatalog.ask('son(adam,Y)'))
print(pyDatalog.ask('daughter(anne,Y)'))
print(son('adam',X))
Logical Programming Paradigm
pyDatalog.create_terms('factorial, N')
factorial[N] = N*factorial[N-1]
factorial[1] = 1
print(factorial[3]==N)
Logical Programming Paradigm
Logical Programming Paradigm
from pyDatalog import pyDatalog
pyDatalog.create_terms('X,Y,Z, works_in, department_size, manager, indirect_manager, count_of_indirect_reports')
# Mary works in Production
+works_in('Mary', 'Production')
+works_in('Sam', 'Marketing')
+works_in('John', 'Production')
+works_in('John', 'Marketing')
+(manager['Mary'] == 'John')
+(manager['Sam'] == 'Mary')
+(manager['Tom'] == 'Mary')
Soln
from pyDatalog import pyDatalog
pyDatalog.create_terms('X,Y,Z,professor,lecturer, dean')
+professor('lucy')
+professor('danny')
+lecturer('james')
dean(X)<=professor(X)
print(dean(X))
Logical Programming Paradigm
likes(john, susie). /* John likes Susie */
likes(X, susie). /* Everyone likes Susie */
likes(john, Y). /* John likes everybody */
likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes John */
likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */
not(likes(john,pizza)). /* John does not like pizza */
likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.
rules
friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends if they like each other */
hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does not like Y. */
enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies if they don't like each other */
Dependent Types Paradigms
Dependent Types Paradigms
Unit-IV (15 Session)
Session 6-10 cover the following topics:-
TextBook:
1) Amit Saha, Doing Math with Python: Use Programming to Explore Algebra,
Statistics,Calculus and More, Kindle Edition, 2015
URL :
• https://tech.peoplefund.co.kr/2018/11/28/programming-paradigm-and-pyth
on-eng.html
• https://freecontent.manning.com/a-first-example-of-dependent-data-types/
Introduction
• A constant problem:
• Writing a correct computer program is hard
• Proving that a program is correct is even harder
• Dependent Types allow us to write programs and know they are correct before
running them.
What is correctness?
• What does it mean to be “correct”?
• Depends on the application domain, but could mean one or
more of:
• Functionally correct (e.g. arithmetic operations on a CPU)
• Resource safe (e.g. runs within memory bounds, no memory
leaks, no accessing unallocated memory, no deadlock. . . )
• Secure (e.g. not allowing access to another user’s data)
What is Type?
from typing import Union def return_int_or_str(flag: bool) -> Union[str, int]:
if flag:
return 'I am a string!‘
return 0
Dependent Type
• » pip install mypy typing_extensions
• from typing import overload
• from typing_extension import Literal
Literal
Literal type represents a specific value of the specific type.
x :: Iterator[T1]
y :: Iterator[T2]
z :: Iterator[T3]
product(x, y) :: Iterator[Tuple[T1, T2]]
product(x, y, z) :: Iterator[Tuple[T1, T2, T3]]
product(x, x, x, x) :: Iterator[Tuple[T1, T1, T1, T1]]
❶Use a type variable, power, because this function works for all
possible vehicle types.
❷ Refueling only makes sense for vehicles that carry fuel.
Restrict the input and output type to Vehicle Petrol.
References
• http://www.cs.ru.nl/dtp11/slides/brady.pdf
• https://freecontent.manning.com/a-first-example-of-dependent-data-types/
• https://en.wikipedia.org/wiki/Dependent_type
• https://livebook.manning.com/book/type-driven-development-with-idris/chapter-1
/13
• https://github.com/python/mypy/issues/366