Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Lecture 4 Functional Programming Paradigms - Operational Semantics

Functional programming is a programming paradigm that treats computations as mathematical functions and avoids changing state and mutable data. It is based on lambda calculus and focuses on declaring what to solve rather than how. Some key characteristics include: treating functions as first-class citizens, avoiding side effects or state, using recursion instead of loops, and supporting higher-order functions. Operational semantics define the computation relation for a functional language using rules like value, NOT, AND, and OR rules. Lambda calculus forms the basis of functional programming and uses lambda abstraction and beta reduction in function application and evaluation.

Uploaded by

christine wambui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Lecture 4 Functional Programming Paradigms - Operational Semantics

Functional programming is a programming paradigm that treats computations as mathematical functions and avoids changing state and mutable data. It is based on lambda calculus and focuses on declaring what to solve rather than how. Some key characteristics include: treating functions as first-class citizens, avoiding side effects or state, using recursion instead of loops, and supporting higher-order functions. Operational semantics define the computation relation for a functional language using rules like value, NOT, AND, and OR rules. Lambda calculus forms the basis of functional programming and uses lambda abstraction and beta reduction in function application and evaluation.

Uploaded by

christine wambui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Functional programming Paradigm

What is a functional programming language?


• By examples: OCaml, Haskell, Scheme, SML,Python( not pure
functional but incorporates the concepts), Erlang, Lisp Clojure,
Scala, ML
• A language that takes functions seriously.
• A language that manipulates functions with free variables as
first-class values, following (more or less) the model of the Lamda
-calculus.
FUNCTIONAL PROGRAMMING
PARADIGM
1. No State
• Having a state normally refers to storing and changing values
such that when a function is called, it may leave things differently
from how it was before the function was called and calling the
function a second time might not get you the same result.

• Functional Languages do not have state, and as such, a function


called will produce the same result for the same data.
FUNCTIONAL PROGRAMMING
PARADIGM
• Referential Transparency
Referential transparency means that given a function and an input value, you will
always receive the same output. That is to say there is no external state used in the
function.
• First Class Functions
• Beyond the pure function ideal, in actual coding practice functional programming
hinges on first class functions. A first class function is a function that is treated as a
“thing in itself,” capable of standing alone and being treated independently.
Functional programming seeks to take advantage of language support in using
functions as variables, arguments, and return values to create elegant code.
FUNCTIONAL PROGRAMMING
PARADIGM
• Immutability:
• "Immutable" means exactly that: it doesn't change.

• The way functional programs do updates is by passing around


new things. An existing value never changes: you just build a new
value and pass that instead
FUNCTIONAL PROGRAMMING
PARADIGM
• Recursion (No flow of Control )
• Since functional languages do not support iterative methods (for loops, while loops
and the like), the only way to repetitively perform a task is through recursion.
• Higher Order Functions
These are functions that have other functions as their parameters and return a
function as their result
• Pure Functions
• The ideal in functional programming is what is known as pure functions. A
pure function is one whose results are dependent only upon the input
parameters, and whose operation initiates no side effect, that is, makes no
external impact besides the return value.
OPERATIONAL SEMANTICS FOR
FUNCTIONAL PROGRAMMING PARADIGM
Functional programming paradigm…what is it?
A programming paradigm that treats computations as the
evaluation of mathematical functions and avoids changing
state and mutable data.
Functional programming is based on Lambda Calculus
OPERATIONAL SEMANTICS FOR
FUNCTIONAL PROGRAMMING
PARADIGM
•Operations
  semantics
Operational semantics for a programming language is defined as the
mathematical definition of its computation relation, , where is the
programming language.
is mathematically a 2-place relation between expressions of the
language , , and values of the language, . Integers and Booleans are
values. Functions are also values because they do not compute to
anything. and are metavariables, meaning they denote an arbitrary
expression or value, and should not be confused with the regular
variable that are part of the program.
OPERATIONAL SEMANTICS FOR
FUNCTIONAL PROGRAMMING
PARADIGM
Functional programming is a programming paradigm in
which we try to bind everything in pure mathematical
functions style. It is a declarative type of programming
style. Its main focus is on “what to solve” in contrast to
an imperative style where the main focus is “how to
solve”. It uses expressions instead of statements. An
expression is evaluated to produce a value whereas a
statement is executed to assign variables.
OPERATIONAL SEMANTICS FOR BOOLEAN VALUES
IN THE FUNCTIONAL PROGRAMMING PARADIGM
•  *Recall this example from induction
• The set of Natural numbers satisfies the following conditions

• If k then
• Both of which can be represented in the following form:

• The general form is then:


OPERATIONAL SEMANTICS FOR BOOLEAN VALUES
IN THE FUNCTIONAL PROGRAMMING PARADIGM

•  1. Value Rule:

 
Value Rule examples:
OPERATIONAL SEMANTICS FOR BOOLEAN VALUES
IN THE FUNCTIONAL PROGRAMMING PARADIGM

•  2. NOT Rule:

 
NOT Rule examples:
OPERATIONAL SEMANTICS FOR BOOLEAN VALUES
IN THE FUNCTIONAL PROGRAMMING PARADIGM

•  3. AND Rule:

 
AND Rule example:
True and True():
OPERATIONAL SEMANTICS FOR BOOLEAN VALUES
IN THE FUNCTIONAL PROGRAMMING PARADIGM

•  3. OR Rule:

 
OR Rule example:
True OR False ():
OPERATION SEMANTICS FOR
FUNCTIONS IN THE FUNCTIONAL
PROGRAMMING PARADIGM
• The basis of functional programming is lambda calculus. Two
languages that are ideal for functional programming are Haskell and
Python. You can create lambda functions in Haskell. You can create
specially defined operators. The following code creates a new
operator, +=: For example
• (+=) = \x y -> x + y
• To test this code, you type 1+=2 and press Enter. The output is 3, as
you might expect.
Syntax Of Lambda Calculus

In Lambda Calculus there are 3 different types of expressions, namely:


• a) E :: = x (variables)
• b) | E1 E2 (function application)
• c) | λx.E (function creation)
• Where λx.E is called Lambda abstraction
• E is known as λ-expressions.
Syntax Of Lambda Calculus
Evaluating Lambda Calculus
• Example 1
• (+ (* 4 3) (* 7 2))
• We can’t start with '+' because it only operates on numbers. There are two reducible expressions: (* 4 3) and
(* 7 2).
(+ (* 4 3) (* 7 2))
(+ (* 4 3) (* 7 2))
(+ 12 (* 7 2))
(+ 12 14)
= 26
Beta-Reduction

• Beta reduction (also written β\betaβ-reduction) is the


replacement of a bound variable in a function body with a
function argument.
• Bound Variable: A bound variable never changes in the
expression after being bound to an actual parameter value at
the time evaluation of the lambda expression begins.
• The purpose of beta-reduction is to compute the result of a
function by function application using specific rules.
Beta-Reduction
( λx.xy ( λy . + y )) x
• λx , λy are the bound variables
• .xy , y are the free variables.
Beta-Reduction
• Beta-reductions in the Lamda -calculus can occur anywhere
and in any order. This can affect termination and algorithmic
eficiency of programs. A fixed reduction strategy enables the
programmer to reason about termination and algorithmic
complexity.
Evaluating Lambda Calculus
•  Example 1:

• Let be
• be
• be

The solution is then:


(a) ii and iii are passed as arguments to i
(b) ii is substituted as f while iii is substituted as x in the function
(c) In the function body, f(fx), the inner part is f, ii, given iii which will evaluate to 3,
thus the inner part becomes f(3), which then evaluates to 2.
Evaluating Lambda Calculus
Example 1
(λx . * 2 x) 4
• (* 2 4)
•=8
Example 2
• (λx . + x x) 4
• (+ 4 4)
•=8
Evaluating Lambda Calculus
Example 3
(λx . (λx . + (− x 1)) x 3) 9
• The inner x belongs to the inner λ and the outer x belongs to the outer one.
• (λx . + (− x 1)) 9 3
• + (− 9 1) 3
• + 8 3 = 11

You might also like