MELJUN CORTES Programming Paradigms
-
Upload
meljun-cortes -
Category
Technology
-
view
165 -
download
1
description
Transcript of MELJUN CORTES Programming Paradigms
![Page 1: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/1.jpg)
Programming Paradigms
![Page 2: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/2.jpg)
Programming Paradigm
A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized.
• Imperative : Machine-model based• Functional : Equations; Expression Evaluation• Logical : First-order Logic Deduction• Object-Oriented : Programming with Data Types
cs784(Prasad) L5Pdm 2
![Page 3: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/3.jpg)
Imperative vs Non-Imperative
• Functional/Logic programs specify WHAT is to be computed abstractly, leaving the details of data organization and instruction sequencing to the interpreter.
• In constrast, In constrast, Imperative programsImperative programs describe describe the details of the details of HOWHOW the results are to be the results are to be
obtained, in terms of the underlying machine obtained, in terms of the underlying machine model.model.
cs784(Prasad) L5Pdm 3
![Page 4: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/4.jpg)
Illustrative Example
• Expression (to be computed) : a + b + c• Recipe for Computation:– Intermediate Code Intermediate Code • T := a + b; T := T + c;
– Accumulator MachineAccumulator Machine• Load a; Add b; Add c
– Stack MachineStack Machine• Push a; Push b; Add; Push c; Add
cs784(Prasad) L5Pdm 4
![Page 5: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/5.jpg)
Imperative vs Non-Imperative
• Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementation decisions).
• An An Imperative programImperative program contains both the contains both the specification and the implementation details, specification and the implementation details, inseparably inter-twined.inseparably inter-twined.
cs784(Prasad) L5Pdm 5
![Page 6: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/6.jpg)
Procedural vs Functional
• Program: a sequence of instructions for a von Neumann m/c.
• Computation by instruction execution.
• Iteration.• Modifiable or
updateable variables.
• Program: a collection of function definitions (m/c independent).
• Computation by term rewriting.
• Recursion.• Assign-only-once
variables.
cs784(Prasad) L5Pdm 6
![Page 7: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/7.jpg)
Functional Style : Illustration
• Definition : Equations sum(0) = 0sum(n) = n + sum(n-1)
• Computation : Substituition and Replacement sum(2)
= 2 + sum (2-1)= …= 3
cs784(Prasad) L5Pdm 7
![Page 8: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/8.jpg)
Paradigm vs Language
• Imperative StyleImperative Style i := 0; sum := 0; while (i < n) do i := i + 1; sum := sum + i end;– Storage efficient
• Functional StyleFunctional Style func sum(i:int) : int; if i = 0 then 0 else i + sum(i-1) end;– No Side-effect
cs784(Prasad) L5Pdm 8
![Page 9: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/9.jpg)
Role of Variables• Imperative (read/write)Imperative (read/write) i 0 1 2 3 ...sum 0 1 3 6 ...• Functional (read only)Functional (read only) i1 sum1i1 sum1 i2 sum2i2 sum2 i3 sum3i3 sum3 ......
cs784(Prasad) L5Pdm 9
3
2
1
6
3
1
![Page 10: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/10.jpg)
Bridging the Gap
• Tail recursive programs can be auomatically optimized for space by translating them into equivalent while-loops.
func sum(i : int, r : int) : int;func sum(i : int, r : int) : int; if i = 0 then rif i = 0 then r else sum(i-1, n+r)else sum(i-1, n+r) endend– Scheme does not have loops.
cs784(Prasad) L5Pdm 10
![Page 11: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/11.jpg)
Analogy: Styles vs Formalisms
• Iteration
• Tail-Recursion
• General Recursion
• Regular Expression
• Regular Grammar
• Context-free Grammar
cs784(Prasad) L5Pdm 11
![Page 12: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/12.jpg)
Logic Programming Paradigm
• Integrates Data and Control Structures edge(a,b). edge(a,c). edge(c,a). path(X,X). path(X,Y) :- edge(X,Y). path(X,Y) :- edge(X,Z), path(Z,Y).
cs784(Prasad) L5Pdm 12
![Page 13: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/13.jpg)
Declarative Programming
• A logic program defines a set of relations. This “knowledge” can be used in various ways
by the interpreter to solve different queries.• In contrast, the programs in other languagesIn contrast, the programs in other languages make explicit make explicit HOWHOW the “declarative the “declarative
knowledge” is used to solve the query.knowledge” is used to solve the query.
cs784(Prasad) L5Pdm 13
![Page 14: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/14.jpg)
AppendAppend in Prolog
append([], L, L).append([], L, L). append([ H | T ], L, [ H | R ]) :-append([ H | T ], L, [ H | R ]) :- append(T, L, R).append(T, L, R).• True statements about appendappend relation.
• “.” and “:-” are logical connectives that stand for “and” and “if” respectively.
• Uses pattern matching.• “[]” and “|” stand for empty list and cons operation.
cs784(Prasad) L5Pdm 14
![Page 15: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/15.jpg)
Different Kinds of Queries
• Verification– sig: list x list x list • append([1], [2,3], [1,2,3]).
• Concatenation– sig: list x list -> list • append([1], [2,3], R).
cs784(Prasad) L5Pdm 15
![Page 16: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/16.jpg)
More Queries
• Constraint solving– sig: list x list -> list • append( R, [2,3], [1,2,3]).
– sig: list -> list x list• append(A, B, [1,2,3]).
• Generation– sig: -> list x list x list• append(X, Y, Z).
cs784(Prasad) L5Pdm 16
![Page 17: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/17.jpg)
cs774 (Prasad) L1LP 17
expressivenessmechanization
Logic Programming Paradigm
Knowledge Representation
Knowledge Representation
Theorem Proving
Theorem Proving
Attribute Grammars / Compilers (DCGs)
Attribute Grammars / Compilers (DCGs)
Relational DatabasesRelational Databases
Programming Languages
Programming Languages
Problem Solving in AI(i)Search
(ii)Divide and Conquer
Problem Solving in AI(i)Search
(ii)Divide and Conquer
unification
declarativeness
efficiency
Trading expressiveness for efficiency :Executable specification
![Page 18: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/18.jpg)
Object-Oriented Style
• Programming with Abstract Data Types– ADTs specify/describe behaviors.ADTs specify/describe behaviors.
• Basic Program Unit: Class– Implementation of an ADT.Implementation of an ADT.• Abstraction enforced by encapsulation.
• Basic Run-time Unit: Object– Instance of a class.Instance of a class.• Has an associated state.
cs784(Prasad) L5Pdm 18
![Page 19: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/19.jpg)
Procedural vs Object-Oriented
• Emphasis on procedural abstraction.
• Top-down design; Step-wise refinement.• Suited for programming
in the small.
• Emphasis on data abstraction.
• Bottom-up design; Reusable libraries.• Suited for programming
in the large.
cs784(Prasad) L5Pdm 19
![Page 20: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/20.jpg)
Integrating Heterogeneous Data
• In C, Pascal, etc., use Union Type / Switch Statement Variant Record Type / Case Statement
• In C++, Java, Eiffel, etc., use Abstract Classes / Virtual Functions Interfaces and Classes / Dynamic Binding
cs784(Prasad) L5Pdm 20
![Page 21: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/21.jpg)
Comparison : Figures example
• Data– Square
• side
– Circle• radius
• Operation (area)– Square
• side * side
– Circle• PI * radius * radius
• Classes– Square
• side• area (= side * side)
– Circle• radius• area (= PI*radius*radius)
cs784(Prasad) L5Pdm 21
![Page 22: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/22.jpg)
Adding a new operation
• Data ...• Operation (area)• Operation (perimeter)– Square
• 4 * side
– Circle• 2 * PI * radius
• Classes– Square
• ...• perimeter (= 4 * side)
– Circle• ...• perimeter (= 2 * PI * radius)
cs784(Prasad) L5Pdm 22
![Page 23: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/23.jpg)
Adding a new data representation
• Data– ...– rectangle
• length• width
• Operation (area)– ...– rectangle
• length * width
• Classes– ...– rectangle
• length• width• area (= length * width)
cs784(Prasad) L5Pdm 23
![Page 24: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/24.jpg)
Procedural vs Object-Oriented
• New operations cause additive changes in procedural style, but require modifications to all existing “class modules” in object-oriented style.
• New data representations cause New data representations cause additive additive changes in object-oriented style, but require changes in object-oriented style, but require modifications to all “procedure modules”.modifications to all “procedure modules”.
cs784(Prasad) L5Pdm 24
![Page 25: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/25.jpg)
Object-Oriented Concepts
• Data Abstraction (specifies behavior)• Encapsulation (controls visibility of names)• Polymorphism (accommodates various
implementations)• Inheritance (facilitates code reuse)• Modularity (relates to unit of compilation)
cs784(Prasad) L5Pdm 25
![Page 26: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/26.jpg)
Example : Role of interface in decoupling
Client• Determine the number of elements in a collection.
Suppliers• Collections : Vector, String, List, Set, Array, etc
Procedual Style• A client is responsible for invoking appropriate
supplier function for determining the size. OOP Style• Suppliers are responsible for conforming to the
standard interface required for exporting the size functionality to a client.
cs784(Prasad) L5Pdm 26
![Page 27: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/27.jpg)
Client in Scheme
(define (size C) (cond ( (vector? C) (vector-length C) ) ( (pair? C) (length C) ) ( (string? C) (string-length C) ) ( else “size not supported”) )))
(size (vector 1 2 (+ 1 2)))(size ‘(one “two” 3))
cs784(Prasad) L5Pdm 27
![Page 28: MELJUN CORTES Programming Paradigms](https://reader035.fdocuments.net/reader035/viewer/2022062418/554ea08fb4c9055f7b8b46ba/html5/thumbnails/28.jpg)
Suppliers and Client in Java
interface Collection { int size(); }class myVector extends Vector
implements Collection {}class myString extends String
implements Collection { public int size() { return length();}}class myArray implements Collection { int[] array; public int size() {return
array.length;}}
Collection c = new myVector(); c.size();cs784(Prasad) L5Pdm 28