Download presentation
Presentation is loading. Please wait.
Published byCamron Blair Modified over 9 years ago
1
Programming Paradigms cs784(Prasad)L5Pdm1
2
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. 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)L5Pdm2
3
cs784(Prasad)L5Pdm3 Imperative vs Non-Imperative n 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. the details of HOW the results are to be obtained, in terms of the underlying machine model.
4
cs784(Prasad)L5Pdm4 Illustrative Example n Expression (to be computed) : a + b + c n Recipe for Computation: –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
5
cs784(Prasad)L5Pdm5 Imperative vs Non-Imperative n 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.
6
cs784(Prasad)L5Pdm6 Procedural vs Functional n Program: a sequence of instructions for a von Neumann m/c. n Computation by instruction execution. n Iteration. n Modifiable or updateable variables. n Program: a collection of function definitions (m/c independent). n Computation by term rewriting. n Recursion. n Assign-only-once variables.
7
cs784(Prasad)L5Pdm7 Functional Style : Illustration n Definition : Equations sum(0) = 0 sum(n) = n + sum(n-1) n Computation : Substituition and Replacement sum(2) sum(2) = 2 + sum (2-1) =… = 3
8
cs784(Prasad)L5Pdm8 Paradigm vs Language n Imperative Style i := 0; sum := 0; i := 0; sum := 0; while (i < n) do while (i < n) do i := i + 1; i := i + 1; sum := sum + i sum := sum + i end; end; –Storage efficient n Functional Style func sum(i:int) : int; if i = 0 then 0 else i + sum(i-1) end; –No Side-effect
9
cs784(Prasad)L5Pdm9 Role of Variables n Imperative (read/write) i 0 1 2 3... i 0 1 2 3... sum 0 1 3 6... n Functional (read only) i1 sum1 i1 sum1 i2 sum2 i2 sum2 i3 sum3 i3 sum3...... 3 2 1 6 3 1
10
cs784(Prasad)L5Pdm10 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; func sum(i : int, r : int) : int; if i = 0 then r if i = 0 then r else sum(i-1, n+r) else sum(i-1, n+r) end end –Scheme does not have loops.
11
cs784(Prasad)L5Pdm11 Analogy: Styles vs Formalisms n Iteration n Tail-Recursion n General Recursion n Regular Expression n Regular Grammar n Context-free Grammar
12
cs784(Prasad)L5Pdm12 Logic Programming Paradigm n Integrates Data and Control Structures edge(a,b). edge(a,b). edge(a,c). edge(a,c). edge(c,a). edge(c,a). path(X,X). path(X,X). path(X,Y) :- edge(X,Y). path(X,Y) :- edge(X,Y). path(X,Y) :- edge(X,Z), path(Z,Y). path(X,Y) :- edge(X,Z), path(Z,Y).
13
cs784(Prasad)L5Pdm13 Declarative Programming n A logic program defines a set of relations. This “knowledge” can be used in various ways by the interpreter to solve different queries. 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. make explicit HOW the “declarative knowledge” is used to solve the query.
14
cs784(Prasad)L5Pdm14 Append in Prolog Append 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). n 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.
15
cs784(Prasad)L5Pdm15 Different Kinds of Queries n Verification –sig: list x list x list »append([1], [2,3], [1,2,3]). n Concatenation –sig: list x list -> list »append([1], [2,3], R).
16
cs784(Prasad)L5Pdm16 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 x list »append(X, Y, Z).
17
cs774 (Prasad)L1LP17 expressiveness mechanization Logic Programming Paradigm Knowledge Representation Knowledge Representation Theorem Proving Theorem Proving Attribute Grammars / Compilers (DCGs) Attribute Grammars / Compilers (DCGs) Relational Databases Relational 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
18
cs784(Prasad)L5Pdm18 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.
19
cs784(Prasad)L5Pdm19 Procedural vs Object-Oriented n Emphasis on procedural abstraction. n Top-down design; Step-wise refinement. Step-wise refinement. n Suited for programming in the small. n Emphasis on data abstraction. n Bottom-up design; Reusable libraries. n Suited for programming in the large.
20
cs784(Prasad)L5Pdm20 Integrating Heterogeneous Data n In C, Pascal, etc., use Union Type / Switch Statement Union Type / Switch Statement Variant Record Type / Case Statement Variant Record Type / Case Statement n In C++, Java, Eiffel, etc., use Abstract Classes / Virtual Functions Abstract Classes / Virtual Functions Interfaces and Classes / Dynamic Binding Interfaces and Classes / Dynamic Binding
21
cs784(Prasad)L5Pdm21 Comparison : Figures example n Data –Square »side –Circle »radius n Operation (area) –Square »side * side –Circle »PI * radius * radius n Classes –Square »side »area (= side * side) –Circle »radius »area (= PI*radius*radius)
22
cs784(Prasad)L5Pdm22 Adding a new operation n Data...... n Operation (area) n Operation (perimeter) –Square »4 * side –Circle »2 * PI * radius n Classes –Square »... »perimeter (= 4 * side) –Circle »... »perimeter (= 2 * PI * radius)
23
cs784(Prasad)L5Pdm23 Adding a new data representation n Data –... –rectangle »length »width n Operation (area) –... –rectangle »length * width n Classes –... –rectangle »length »width »area (= length * width)
24
cs784(Prasad)L5Pdm24 Procedural vs Object-Oriented n New operations cause additive changes in procedural style, but require modifications to all existing “class modules” in object- oriented style. n New data representations cause additive changes in object-oriented style, but require modifications to all “procedure modules”.
25
cs784(Prasad)L5Pdm25 Object-Oriented Concepts n 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)
26
cs784(Prasad)L5Pdm26 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.
27
cs784(Prasad)L5Pdm27 Client in Scheme (define (size C) (cond (cond ( (vector? C) (vector-length C) ) ( (vector? C) (vector-length C) ) ( (pair? C) (length C) ) ( (pair? C) (length C) ) ( (string? C) (string-length C) ) ( (string? C) (string-length C) ) ( else “ size not supported ”) ) ( else “ size not supported ”) ))) (size (vector 1 2 (+ 1 2))) (size ‘(one “two” 3))
28
cs784(Prasad)L5Pdm28 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();} public int size() { return length();}} class myArray implements Collection { int[] array; int[] array; public int size() {return array.length;} public int size() {return array.length;}} Collection c = new myVector(); c.size();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.