Programming Paradigms cs 784Prasad L 5 Pdm 1

  • Slides: 28
Download presentation
Programming Paradigms cs 784(Prasad) L 5 Pdm 1

Programming Paradigms cs 784(Prasad) L 5 Pdm 1

Programming Paradigm A way of conceptualizing what it means to perform computation and how

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 cs 784(Prasad) L 5 Pdm 2

Imperative vs Non-Imperative Functional/Logic programs specify WHAT is to be computed abstractly, leaving the

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. n In constrast, Imperative programs describe the details of HOW the results are to be obtained, in terms of the underlying machine model. n cs 784(Prasad) L 5 Pdm 3

Illustrative Example Expression (to be computed) : a + b + c n Recipe

Illustrative Example Expression (to be computed) : a + b + c n Recipe for Computation: n – Intermediate Code » T : = a + b; T : = T + c; – Accumulator Machine » Load a; Add b; Add c – Stack Machine » Push a; Push b; Add; Push c; Add cs 784(Prasad) L 5 Pdm 4

Imperative vs Non-Imperative Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility)

Imperative vs Non-Imperative Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementation decisions). n An Imperative program contains both the specification and the implementation details, inseparably inter-twined. n cs 784(Prasad) L 5 Pdm 5

Procedural vs Functional n n Program: a sequence of instructions for a von Neumann

Procedural vs Functional n n Program: a sequence of instructions for a von Neumann m/c. Computation by instruction execution. Iteration. Modifiable or updateable variables. cs 784(Prasad) n n L 5 Pdm Program: a collection of function definitions (m/c independent). Computation by term rewriting. Recursion. Assign-only-once variables. 6

Functional Style : Illustration Definition : Equations sum(0) = 0 sum(n) = n +

Functional Style : Illustration Definition : Equations sum(0) = 0 sum(n) = n + sum(n-1) n Computation : Substituition and Replacement sum(2) = 2 + sum (2 -1) = … = 3 n cs 784(Prasad) L 5 Pdm 7

Paradigm vs Language n Imperative Style i : = 0; sum : = 0;

Paradigm vs Language n Imperative Style i : = 0; sum : = 0; while (i < n) do i : = i + 1; sum : = sum + i end; n – Storage efficient cs 784(Prasad) Functional Style func sum(i: int) : int; if i = 0 then 0 else i + sum(i-1) end; – No Side-effect L 5 Pdm 8

Role of Variables Imperative (read/write) i 0 1 2 3. . . sum 0

Role of Variables Imperative (read/write) i 0 1 2 3. . . sum 0 1 3 6. . . n Functional (read only) i 1 6 3 i 2 2 3 i 3 1 1. . . L 5 Pdm cs 784(Prasad) n sum 1 sum 2 sum 3 9

Bridging the Gap n Tail recursive programs can be auomatically optimized for space by

Bridging the Gap n Tail recursive programs can be auomatically optimized for space by translating them into equivalent while-loops. func sum(i : int, r : int) : int; if i = 0 then r else sum(i-1, n+r) end – Scheme does not have loops. cs 784(Prasad) L 5 Pdm 10

Analogy: Styles vs Formalisms n Iteration n Regular Expression n Tail-Recursion n Regular Grammar

Analogy: Styles vs Formalisms n Iteration n Regular Expression n Tail-Recursion n Regular Grammar n General Recursion n Context-free Grammar cs 784(Prasad) L 5 Pdm 11

Logic Programming Paradigm n Integrates Data and Control Structures edge(a, b). edge(a, c). edge(c,

Logic Programming Paradigm n 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). cs 784(Prasad) L 5 Pdm 12

Declarative Programming A logic program defines a set of relations. This “knowledge” can be

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. n In contrast, the programs in other languages make explicit HOW the “declarative knowledge” is used to solve the query. n cs 784(Prasad) L 5 Pdm 13

Append in Prolog n append([], L, L). append([ H | T ], L, [

Append in Prolog n append([], L, L). append([ H | T ], L, [ H | R ]) : append(T, L, R). True statements about append relation. » “. ” and “: -” are logical connectives that stand for “and” and “if” respectively. n Uses pattern matching. » “[]” and “|” stand for empty list and cons operation. cs 784(Prasad) L 5 Pdm 14

Different Kinds of Queries n Verification – sig: list x list » append([1], [2,

Different Kinds of Queries n Verification – sig: list x list » append([1], [2, 3], [1, 2, 3]). n Concatenation – sig: list x list -> list » append([1], [2, 3], R). cs 784(Prasad) L 5 Pdm 15

More Queries n Constraint solving – sig: list x list -> list » append(

More Queries n 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]). n Generation – sig: -> list x list » append(X, Y, Z). cs 784(Prasad) L 5 Pdm 16

Trading expressiveness for efficiency : Executable specification Knowledge Representation Problem Solving in AI (i)

Trading expressiveness for efficiency : Executable specification Knowledge Representation Problem Solving in AI (i) Search (ii) Divide and Conquer Theorem Proving efficiency unification Logic Programming Paradigm mechanization Attribute Grammars / Compilers (DCGs) cs 774 (Prasad) expressiveness Relational Databases L 1 LP declarativeness Programming Languages 17

Object-Oriented Style n Programming with Abstract Data Types – ADTs specify/describe behaviors. n Basic

Object-Oriented Style n Programming with Abstract Data Types – ADTs specify/describe behaviors. n Basic Program Unit: Class – Implementation of an ADT. » Abstraction enforced by encapsulation. n Basic Run-time Unit: Object – Instance of a class. » Has an associated state. cs 784(Prasad) L 5 Pdm 18

Procedural vs Object-Oriented n n n Emphasis on procedural abstraction. Top-down design; Step-wise refinement.

Procedural vs Object-Oriented n n n Emphasis on procedural abstraction. Top-down design; Step-wise refinement. Suited for programming in the small. cs 784(Prasad) n n n L 5 Pdm Emphasis on data abstraction. Bottom-up design; Reusable libraries. Suited for programming in the large. 19

Integrating Heterogeneous Data n In C, Pascal, etc. , use Union Type / Switch

Integrating Heterogeneous Data n In C, Pascal, etc. , use Union Type / Switch Statement Variant Record Type / Case Statement n In C++, Java, Eiffel, etc. , use Abstract Classes / Virtual Functions Interfaces and Classes / Dynamic Binding cs 784(Prasad) L 5 Pdm 20

Comparison : Figures example n Data n – Square » side » area (=

Comparison : Figures example n Data n – Square » side » area (= side * side) – Circle » radius n Classes – Circle Operation (area) » radius » area (= PI*radius) – Square » side * side – Circle » PI * radius cs 784(Prasad) L 5 Pdm 21

Adding a new operation n Data. . . Operation (area) Operation (perimeter) n –

Adding a new operation n Data. . . Operation (area) Operation (perimeter) n – Square » . . . » perimeter (= 4 * side) – Square – Circle » 4 * side » . . . » perimeter (= 2 * PI * radius) – Circle » 2 * PI * radius cs 784(Prasad) Classes L 5 Pdm 22

Adding a new data representation n Data n –. . . – rectangle »

Adding a new data representation n Data n –. . . – rectangle » length » width n Classes » » » Operation (area) –. . . – rectangle length width area (= length * width) » length * width cs 784(Prasad) L 5 Pdm 23

Procedural vs Object-Oriented New operations cause additive changes in procedural style, but require modifications

Procedural vs Object-Oriented New operations cause additive changes in procedural style, but require modifications to all existing “class modules” in objectoriented style. n New data representations cause additive changes in object-oriented style, but require modifications to all “procedure modules”. n cs 784(Prasad) L 5 Pdm 24

Object-Oriented Concepts Data Abstraction (specifies behavior) n Encapsulation (controls visibility of names) n Polymorphism

Object-Oriented Concepts Data Abstraction (specifies behavior) n Encapsulation (controls visibility of names) n Polymorphism (accommodates various implementations) n Inheritance (facilitates code reuse) n Modularity (relates to unit of compilation) n cs 784(Prasad) L 5 Pdm 25

Example : Role of interface in decoupling 8 Client » Determine the number of

Example : Role of interface in decoupling 8 Client » Determine the number of elements in a collection. 8 Suppliers » Collections : Vector, String, List, Set, Array, etc 8 Procedual Style » A client is responsible for invoking appropriate supplier function for determining the size. 8 OOP Style » Suppliers are responsible for conforming to the standard interface required for exporting the size functionality to a client. cs 784(Prasad) L 5 Pdm 26

Client in Scheme (define (size C) (cond ( (vector? C) ( (pair? C) (

Client in Scheme (define (size C) (cond ( (vector? C) ( (pair? C) ( (string? C) ( else )) (size cs 784(Prasad) (vector-length C) ) (string-length C) ) “size not supported”) ) (vector 1 2 (+ 1 2))) ‘(one “two” 3)) L 5 Pdm 27

Suppliers and Client in Java interface Collection { int size(); } class my. Vector

Suppliers and Client in Java interface Collection { int size(); } class my. Vector extends Vector implements Collection { } class my. String extends String implements Collection { public int size() { return length(); } } class my. Array implements Collection { int[] array; public int size() {return array. length; } } Collection c = new cs 784(Prasad) my. Vector(); L 5 Pdm c. size(); 28