Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PLG 4 Presentation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

ICS 2304:PROGRAMMING PARADIGMS

PLG-4 PRESENTATION
Using operational semantics for functional programming, give
examples for arithmetic and Boolean evaluations
The evaluation of arithmetic expressions
Recap:
Rules for Arithmetic Evaluations
• Rule CR =____________
𝑛⥤𝑛
𝑒⥤𝑉
𝑒⥤𝑣𝐼
• Rule OpR=
𝑒 𝑜𝑝 𝑒1⥤𝐴𝑝(𝑜𝑝,𝑣,𝑣1)
Example 1:
Calculate the value of the expression (7 + 5)
The rules are provided. We need to view this in terms of the abstract
syntax of Exp in order to apply them. Express as a parse tree rather
than a sequence of symbols

e op e1
• There e is 7 and e1 is 5. In this case the only rule applicable is Rule OpR and in order to
apply it we need to know:
7 + 5⥤ ??

Then an application of the rule CR would lead to the partial proof:


1. 7⥤7
2. 5⥤5
We obtain a more complete proof:
1. 7⥤7 by Rule CR
2. 5⥤5 by Rule CR
3. 7 + 5 ⥤ 12 by Rule OpR to 1,2
So the result is 12
Example 2:
Calculate the value of the expression 2*3 + 10div(8-6)
We apply this as a parse tree:

e op e1
Where e is the expression 2*3 and e1 the expression 10 div (8-6). So we
use Rule OpR and we need to know:
2*3 ⥤ ?
10 div (8-6) ⥤ ??
When we apply rule OpR it will lead to the proof:
1. If 2*3 ⥤ ?
2. And 10 div(8-6) ⥤ ??
3. Then (2*3) + 10 div(8-6) ⥤ Ap(+num, ?, ??)
We apply rule OpR to resolve 2*3
First we solve:
2 ⥤ ?`
3 ⥤??``
Using indirect application of rule CR the proof for 2*3 ⥤?`:
1. 2 ⥤2
2. 3 ⥤3
3. 2*3 ⥤6 by OpR
• To continue to reduce the (2*3) + 10 div (8-6) we do the second part 10 div (8-6) ⥤??
• e is 10 and e1 is 8-6 and we use OpR rule
• So we evaluate
10⥤ ?`
8-6 ⥤ ??``
The first we use rule 1 and the second we use rule 2
1. 10 ⥤10 by Rule CR
2. 8 ⥤8 by Rule CR
3. 6 ⥤6 by Rule CR
4. 8-6 ⥤ 2 by Rule OpR to 2,3
5. 10 div (8-6) ⥤5 by Rule OpR to 1,4
• We obtain the following complete proof
1. 10 ⥤10 by Rule CR
2. 8 ⥤8 by Rule CR
3. 6 ⥤6 by Rule CR
4. 8-6 ⥤ 2 by Rule OpR to 2,3
5. 10 div (8-6) ⥤5 by Rule OpR to 1,4
6. 2 ⥤ 2 by Rule CR
7. 3 ⥤3 by Rule CR
8. 2 * 3 ⥤ 6 by Rule OpR to 6,7
9. 2*3 + 10 div (8-6) ⥤ 11 by Rule OpR to 8,5
The result is 11.
The evaluation of boolean expressions
The evaluation of boolean expressions
Recap of basic rules:

• 1. Value Rule:
𝑣⇒𝑣

Example:

,
𝑇𝑅𝑈𝐸 ⇒ 𝑇𝑅𝑈𝐸 𝐹𝐴𝐿𝑆𝐸 ⇒ 𝐹𝐴𝐿𝑆𝐸
(𝑒⇒𝑣)
• 2. NOT Rule:
𝑁𝑂𝑇 𝑒⇒𝑛𝑒𝑔𝑎𝑡𝑖𝑜𝑛 𝑜𝑓 𝑣

Example:
𝑣 𝑖𝑠 𝑇𝑟𝑢𝑒
𝑇𝑅𝑈𝐸⇒𝑇𝑅𝑈𝐸 𝐹𝐴𝐿𝑆𝐸⇒𝐹𝐴𝐿𝑆𝐸
𝑁𝑂𝑇 𝑇𝑅𝑈𝐸 ⇒ 𝐹𝐴𝐿𝑆𝐸 𝑁𝑂𝑇 𝐹𝐴𝐿𝑆𝐸 ⇒ 𝑇𝑅𝑈𝐸
𝑒1 ⇒𝑣1 , 𝑒2 ⇒ 𝑣2 ,
• 3. AND Rule:
𝑒1 𝐴𝑁𝐷 𝑒2 ⇒𝑙𝑜𝑔𝑖𝑐𝑎𝑙 𝐴𝑁𝐷 𝑜𝑓 𝑣1 𝑎𝑛𝑑 𝑣2

Example:
True and True(𝑣1 𝑖𝑠 𝑇𝑟𝑢𝑒 𝑎𝑛𝑑 𝑣2 𝑖𝑠 𝑇𝑟𝑢𝑒):
𝑇𝑅𝑈𝐸⇒𝑇𝑅𝑈𝐸 𝑇𝑅𝑈𝐸⇒𝑇𝑅𝑈𝐸
𝑇𝑅𝑈𝐸 𝐴𝑁𝐷 𝑇𝑅𝑈𝐸 ⇒ 𝑇𝑅𝑈𝐸
Example 2:

True and False(𝑣1 𝑖𝑠 𝑇𝑟𝑢𝑒 𝑎𝑛𝑑 𝑣2 𝑖𝑠 𝐹𝑎𝑙𝑠𝑒):

𝑇𝑅𝑈𝐸⇒𝑇𝑅𝑈𝐸 𝐹𝐴𝐿𝑆𝐸⇒𝐹𝐴𝐿𝑆𝐸
𝑇𝑅𝑈𝐸 𝐴𝑁𝐷 𝐹𝐴𝐿𝑆𝐸 ⇒ 𝐹𝐴𝐿𝑆𝐸
𝑒1 ⇒𝑣1 , 𝑒2 ⇒ 𝑣2 ,
• 3. OR Rule:
𝑒1 𝑂𝑅 𝑒2 ⇒𝑙𝑜𝑔𝑖𝑐𝑎𝑙 𝑂𝑅 𝑜𝑓 𝑣1 𝑎𝑛𝑑 𝑣2

Example:
True OR False (𝑣1 𝑖𝑠 𝑇𝑟𝑢𝑒 𝑎𝑛𝑑 𝑣2 𝑖𝑠 𝐹𝑎𝑙𝑠𝑒):
𝑇𝑅𝑈𝐸⇒𝑇𝑅𝑈𝐸 𝐹𝐴𝐿𝑆𝐸⇒𝐹𝐴𝐿𝑆𝐸
𝑇𝑅𝑈𝐸 𝑂𝑅 𝐹𝐴𝐿𝑆𝐸 ⇒ 𝑇𝑅𝑈𝐸
Example 2:

Example:
False OR False (𝑣1 𝑖𝑠 𝐹𝑎𝑙𝑠𝑒 𝑎𝑛𝑑 𝑣2 𝑖𝑠 𝐹𝑎𝑙𝑠𝑒):

𝐹𝐴𝐿𝑆𝐸⇒𝐹𝐴𝐿𝑆𝐸 𝐹𝐴𝐿𝑆𝐸⇒𝐹𝐴𝐿𝑆𝐸
𝐹𝐴𝐿𝑆𝐸 𝑂𝑅 𝐹𝐴𝐿𝑆𝐸 ⇒ 𝐹𝐴𝐿𝑆𝐸
Further Examples:
Example 2:
Example 3:
Discuss Lambda Calculus and its use for functions in functional
programing paradigm giving examples
Then we understand what Lambda Calculus is:
• The Lambda Calculus is a model of computation developed in the 1930s by
the mathematician Alonzo Church. You are probably aware of the more
famous model for computation developed around the same time by Alan
Turing: the Turing Machine. However, while the Turing Machine is based on
a hypothetical physical machine (involving tapes from which instructions
are read and written) the Lambda Calculus was conceived as a set of rules
and operations for function abstraction and application. It has been proven
that, as a model of computation, the Lambda Calculus is just as powerful as
Turing Machines, that is, any computation that can be modelled with a
Turing Machine can also be modeled with the Lambda Calculus.
• The Lambda Calculus is also important to study as it is the basis of
functional programming. The operations we can apply to Lambda Calculus
expressions to simplify (or reduce) them, or to prove equivalence, can also
be applied to pure functions in a programming language that
supports higher-order functions.
Lambda Expressions
• Lambda Calculus expressions are written with a standard system of
notation. It is worth looking at this notation before studying haskell-like
languages because it was the inspiration for Haskell syntax. Here is a simple
Lambda Abstraction of a function:
• λx. x
• The λ (Greek letter Lambda) simply denotes the start of a function
expression. Then follows a list of parameters (in this case we have only a
single parameter called x) terminated by “.”
• Then follows the function body, an expression returned by the function
when it is applied.
• The variable x is said to be bound to the parameter. A bound variable:
appears in the function body as well as the parameter list.
• Variables that appear in the function body but not in the parameter list are
said to be free.
Things to note about Lambda Expressions
1. A lambda expression has no name, it is anonymous. Note that anonymous
functions in languages like JavaScript and Python are also frequently called lambda
expressions, or just lambdas. Now you know why.
2. The only values that Lambda Calculus variables can take on is other functions (i.e.
lambda expressions). It’s lambdas all the way down! However, to actually model
and perform useful computations we say that certain expressions represent values.
See the discussion of Church Encodings, below, to see how this is done.
3. The names of variables bound to parameters in a lambda expression are only
meaningful within the context of that expression. Thus, λx.x is semantically
equivalent (or alpha equivalent) to λy.y or any other possible renaming of the
variable.
4. Lambda functions can have multiple parameters in the parameter list, e.g.: λxy. x y,
but they are implicitly curried (e.g. a sequence of nested univariate functions).
Thus the following are all equivalent:
λxy. x y
= λx. λy. x y
= λx. (λy. x y)
Combinators

A combinator is a lambda expression (function) with no free variables.


Thus, the expression λx. x is a combinator because the variable x is
bound to the parameter. The expression λx. x y is not a combinator,
because y is not bound to any parameter, it is free.
Evaluating Lambda Calculus
(λx. x) y

We can reduce this expression to a simpler form by a substitution, indicated


by a bit of intermediate notation. Two types of annotations are commonly
seen, you can use either (or both!):
(λx. x) y [x:=y] -- an annotation on the right showing the substitution
that will be applied to the expression on the left
(λx [x:=y].x) -- an annotation inside the parameter list showing the
substitution that will be performed inside the body (arguments have already
been removed)
Now we perform the substitution in the body of the expression and throw
away the head, since all the bound variables are substituted, leaving only:
y
Alpha Equivalence
• Alpha Equivalence variables can be arbitrarily renamed as long as the
names remain consistent within the scope of the expression.

• λxy.yx = λwv.vw
Beta Reduction
This first reduction rule, substituting the arguments of a function application
to all occurrences of that parameter inside the function body, is called beta
reduction.
Beta Reduction functions are applied to their arguments by substituting the
text of the argument in the body of the function

(λx. x) y
= (λx [x:=y]. x) - we indicate the substitution that is going to occur inside []
= x [x:=y] - an alternative way to show the substitution
=y
Further Example
• A simple way for substitution:
• (λ param . output)input => output [param := input] => result

• Here’s a fully worked example of applying the different rules to


reduce an expression until no more Beta reduction is possible, at
which time we say it is in beta normal form:
.
• (λz.z) (λa.a a) (λz.z b)
• ⇒
• ((λz.z) (λa.a a)) (λz.z b) => Function application is left-associative
• ⇒
• (z [z:=λa.a a]) (λz.z b) => BETA Reduction
• ⇒
• (λa.a a) (λz.z b)
• ⇒
• a a [a:=λz.z b] => BETA Reduction
• ⇒
• (λz.z b) (λz.z b)
• ⇒
• z b [z:=(λz.z b)] => BETA Reduction
• ⇒
• (λz.z b) b
• ⇒
• z b [z:=b] => BETA Reduction
• ⇒
• bb => Beta normal form, cannot be reduced again.
Group Members:
• Kennedy Karira
• Brenda Makena
• Adriana Helga
• Samuel Nduati
• Naomi Gitau

You might also like