MELJUN CORTES Programming Paradigms

539 views

Published on

MELJUN CORTES Programming Paradigms

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
539
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • meljun cortes, mba,mpa,bscs,acs
  • Computation : Implementation of an I/O relation meljun cortes, mba,mpa,bscs,acs
  • Recursion = Iteration + Stack Tail-recursion = Iteration meljun cortes, mba,mpa,bscs,acs
  • Reflexive transitive closure meljun cortes, mba,mpa,bscs,acs
  • In OOP style, the interface between suppliers and clients is standardized, so that by requiring the suppliers to conform to this standard, the clients can safely assume the form of messages. This enables clients to choose from a number of suppliers facilitating interchangeability, upgradability, reuse, etc and enables clients to be extendable by being able to accommodate future suppliers that conform to the standard (open universe context). E.g, Double-click on DeskTop icons for view, Displaying info : XML approach, etc meljun cortes, mba,mpa,bscs,acs
  • When a new collection is added, in the procedural style, the entire client needs to be recompiled because of the additional clause in the COND. Furthermore, it is the client’s responsibility to write the glue code to get the appropriate functionality from what the supplier provides. These restrictions make it suitable for closed world applications. meljun cortes, mba,mpa,bscs,acs
  • Standard interfaces are a minimum requirement for flexibility, and for supporting reuse and interchangeability. The supplier is responsible for conforming to the interface. Interfaces and Dynamic Binding ensure that the client not need to be aware of the implementation details of the supplier and can comfortably handle different suppliers at run-time. meljun cortes, mba,mpa,bscs,acs
  • MELJUN CORTES Programming Paradigms

    1. 1. Programming Paradigms
    2. 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. • Imperative : Machine-model based • Functional : Equations; Expression Evaluation • Logical : First-order Logic Deduction • Object-Oriented : Programming with Data Typescs784(Prasad) L5Pdm 2
    3. 3. 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, Imperative programs describe the details of HOW the results are to be obtained, in terms of the underlying machine model.cs784(Prasad) L5Pdm 3
    4. 4. Illustrative Example• Expression (to be computed) : a + b + c• 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; Addcs784(Prasad) L5Pdm 4
    5. 5. Imperative vs Non-Imperative• Functional/Logic style clearly separates WHAT aspects of a program (programmers’ responsibility) from the HOW aspects (implementation decisions).• An Imperative program contains both the specification and the implementation details, inseparably inter-twined.cs784(Prasad) L5Pdm 5
    6. 6. Procedural vs Functional• Program: a sequence of • Program: a collection instructions for a von of function definitions Neumann m/c. (m/c independent).• Computation by • Computation by term instruction execution. rewriting.• Iteration. • Recursion.• Modifiable or • Assign-only-once updateable variables. variables.cs784(Prasad) L5Pdm 6
    7. 7. Functional Style : Illustration• Definition : Equations sum(0) = 0 sum(n) = n + sum(n-1)• Computation : Substituition and Replacement sum(2) = 2 + sum (2-1) = … = 3cs784(Prasad) L5Pdm 7
    8. 8. Paradigm vs Language• Imperative Style • Functional Style i := 0; sum := 0; func sum(i:int) : int; while (i < n) do if i = 0 i := i + 1; then 0 sum := sum + i else i + sum(i-1) end; end; – Storage efficient – No Side-effectcs784(Prasad) L5Pdm 8
    9. 9. Role of Variables• Imperative (read/write) i 0 1 2 3 ...sum 0 1 3 6 ...• Functional (read only) i1 sum1 i2 sum2 i3 sum3 ... 6 3 2 3 1 1cs784(Prasad) L5Pdm 9
    10. 10. 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; if i = 0 then r else sum(i-1, n+r) end – Scheme does not have loops.cs784(Prasad) L5Pdm 10
    11. 11. Analogy: Styles vs Formalisms• Iteration • Regular Expression• Tail-Recursion • Regular Grammar• General Recursion • Context-free Grammarcs784(Prasad) L5Pdm 11
    12. 12. 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
    13. 13. 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 languages make explicit HOW the “declarative knowledge” is used to solve the query.cs784(Prasad) L5Pdm 13
    14. 14. Append in Prolog 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. • Uses pattern matching. • “[]” and “|” stand for empty list and cons operation.cs784(Prasad) L5Pdm 14
    15. 15. 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
    16. 16. 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
    17. 17. Trading expressiveness for efficiency : Executable specification Problem Solving in AI Problem Solving in AI Knowledge Knowledge Theorem Theorem (i)Search (i)Search Representation Representation Proving Proving (ii)Divide and Conquer (ii)Divide and Conquer efficiency unification Logic Programming Paradigmmechanization expressiveness declarativenessAttribute GrammarsAttribute Grammars Relational Relational Programming Programming //Compilers (DCGs) Compilers (DCGs) Databases Databases Languages Languagescs774 (Prasad) L1LP 17
    18. 18. Object-Oriented Style• Programming with Abstract Data Types – ADTs specify/describe behaviors.• Basic Program Unit: Class – Implementation of an ADT. • Abstraction enforced by encapsulation.• Basic Run-time Unit: Object – Instance of a class. • Has an associated state.cs784(Prasad) L5Pdm 18
    19. 19. Procedural vs Object-Oriented• Emphasis on procedural • Emphasis on data abstraction. abstraction.• Top-down design; • Bottom-up design; Step-wise refinement. Reusable libraries.• Suited for programming • Suited for programming in the small. in the large.cs784(Prasad) L5Pdm 19
    20. 20. 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 Bindingcs784(Prasad) L5Pdm 20
    21. 21. Comparison : Figures example• Data • Classes – Square – Square • side • side – Circle • area • radius (= side * side)• Operation (area) – Circle • radius – Square • area • side * side (= PI*radius*radius) – Circle • PI * radius * radiuscs784(Prasad) L5Pdm 21
    22. 22. Adding a new operation• Data • Classes ... – Square • ...• Operation (area) • perimeter• Operation (perimeter) (= 4 * side) – Square – Circle • 4 * side • ... – Circle • perimeter • 2 * PI * radius (= 2 * PI * radius)cs784(Prasad) L5Pdm 22
    23. 23. Adding a new data representation• Data • Classes – ... – ... – rectangle – rectangle • length • length • width • width• Operation (area) • area (= length * width) – ... – rectangle • length * widthcs784(Prasad) L5Pdm 23
    24. 24. 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 additive changes in object-oriented style, but require modifications to all “procedure modules”.cs784(Prasad) L5Pdm 24
    25. 25. 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
    26. 26. 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
    27. 27. 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
    28. 28. 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

    ×