Programming Paradigms Procedural Functjonal Logic Object-Oriented - - PowerPoint PPT Presentation

programming paradigms
SMART_READER_LITE
LIVE PREVIEW

Programming Paradigms Procedural Functjonal Logic Object-Oriented - - PowerPoint PPT Presentation

Programming Paradigms Procedural Functjonal Logic Object-Oriented Specifying the WHAT Describe the Inputs Specifjc values Propertjes Describe the Outputs (as above) Describe the Relatjonships Between I x O As


slide-1
SLIDE 1

Programming Paradigms

  • Procedural
  • Functjonal
  • Logic
  • Object-Oriented
slide-2
SLIDE 2

CS784 2

Specifying the WHAT

  • Describe the Inputs

– Specifjc values – Propertjes

  • Describe the Outputs (as above)
  • Describe the Relatjonships Between I x O

– As a possibly infjnite table – Equatjons and other predicates between input and

  • utput expressions

– For a given input, output may not be unique

slide-3
SLIDE 3

CS784 3

Specifying the HOW

  • Describe the Inputs

– Specifjc values – Propertjes

  • Describe HOW the Outputs are produced
  • Models of existjng computers

– Program State – Control Flow

  • A Few Abstractjons

– Block Structure – Recursion via a Stack

slide-4
SLIDE 4

CS784 4

Procedural programming

  • Describes the details of HOW the results are to be obtained, in terms of

the underlying machine model.

  • Describes computatjon in terms of

– Statements that change a program state – Explicit control fmow

  • Synonyms

– Imperatjve programming – Operatjonal

  • Fortran, C, …

– Abstractjons of typical machines – Control Flow Encapsulatjon

  • Control Structures
  • Procedures

– No return values

  • Functjons

– Return one or more values

  • Recursion via stack
slide-5
SLIDE 5

CS784 5

Procedural Programming: State

  • Program State

– Collectjon of Variables and their values – Contents of variables change

  • Expressions

– Not expected to change Program State

  • Assignment Statements
  • Other Statements
  • Side Efgects
slide-6
SLIDE 6

CS784 6

C, C++, C#, Java

  • Abstractjons of typical machines
  • Control Flow Encapsulatjon

– Control Structures – Procedures

  • No return values

– Functjons

  • Return one or more values

– Recursion via stack

  • Betuer Data Type support
slide-7
SLIDE 7

CS784 7

Illustratjve Example

  • Expression (to be computed) : a + b + c
  • Recipe for Computatjon

– Account for machine limitatjons – Intermediate Locatjon

  • T := a + b; T := T + c;

– Accumulator Machine

  • Load a; Add b; Add c

– Stack Machine

  • Push a; Push b; Add; Push c; Add
slide-8
SLIDE 8

CS784 8

Declaratjve Programming

  • Specifjes WHAT is to be computed abstractly
  • Expresses the logic of a computatjon without

describing its control fmow

  • Declaratjve languages include

– logic programming, and – functjonal programming.

  • ofuen defjned as any style of programming

that is not imperatjve.

slide-9
SLIDE 9

CS784 9

Imperatjve vs Non-Imperatjve

  • Functjonal/Logic style clearly separates WHAT

aspects of a program (programmers’ responsibility) from the HOW aspects (implementatjon decisions).

  • An Imperatjve program contains both the

specifjcatjon and the implementatjon details, inseparably inter-twined.

slide-10
SLIDE 10

CS784 10

Procedural vs Functjonal

  • Program: a sequence of

instructjons for a von Neumann m/c.

  • Computatjon by

instructjon executjon.

  • Iteratjon.
  • Modifjable or updatable

variables..

  • Program: a collectjon of

functjon defjnitjons (m/c independent).

  • Computatjon by term

rewritjng.

  • Recursion.
  • Assign-only-once

variables.

slide-11
SLIDE 11

CS784 11

Functjonal Style : Illustratjon

  • Defjnitjon: Equatjons

sumto(0) = 0 sumto(n) = n + sumto(n-1)

  • Computatjon: Substjtutjon and Replacement

sumto(2) = 2 + sumto (2-1) = 2 + sumto(1) = 2 + 1 + sumto(1-1) = 2 + 1 + sumto(0) = 2 + 1 + 0 = … = 3

slide-12
SLIDE 12

CS784 12

Paradigm vs Language

Imperatjve Style tsum := 0; i := 0; while (i < n) do i := i + 1; tsum := tsum + I

  • d

Storage effjcient Functjonal Style func sumto(n: int): int; if n = 0 then else n + sumto(n-1) fj endfunc; No Side-efgect

slide-13
SLIDE 13

CS784 13

Bridging the Gap

  • Imperatjve is not always faster, or more memory

effjcient than functjonal.

  • E.g., tail recursive programs can be automatjcally

translated into equivalent while-loops. func xyz(n : int, r : int) : int; if n = 0 then r else xyz(n-1, n+r) fj endfunc

slide-14
SLIDE 14

CS784 14

Analogy: Styles vs Formalisms

  • Iteratjon
  • Tail-Recursion
  • General Recursion
  • Regular Expression
  • Regular Grammar
  • Context-free Grammar
slide-15
SLIDE 15

CS784 15

Logic Programming Paradigm

  • 1. edge(a,b).
  • 2. edge(a,c).
  • 3. edge(c,a).
  • 4. path(X,X).
  • 5. path(X,Y) :- edge(X,Y).
  • 6. path(X,Y) :- edge(X,Z), path(Z,Y).
slide-16
SLIDE 16

CS784 16

Logic Programming

  • A logic program defjnes a set of relatjons.
  • This “knowledge” can be used in various ways

by the interpreter to solve difgerent “queries”.

  • In contrast, the programs in other languages
  • Make explicit HOW the “declaratjve

knowledge” is used to solve the query.

slide-17
SLIDE 17

CS784 17

Append in Prolog

  • append([], L, L).
  • append([ H | T ], X, [ H | Y ]) :-
  • append(T, X, Y).
  • True statements about append relatjon.
  • Uses patuern matching.

– “[]” and “|” stand for empty list and cons

  • peratjon.
slide-18
SLIDE 18

CS784 18

Difgerent Kinds of Queries

  • Verifjcatjon

– append: list x list x list

  • append([1], [2,3], [1,2,3]).
  • Concatenatjon

– append: list x list -> list

  • append([1], [2,3], R).
slide-19
SLIDE 19

CS784 19

More Queries

  • Constraint solving

– append: list x list -> list

  • append( R, [2,3], [1,2,3]).

– append: list -> list x list

  • append(A, B, [1,2,3]).
  • Generatjon

– append: -> list x list x list

  • append(X, Y, Z).
slide-20
SLIDE 20

CS784 20

Object-Oriented Style

  • Programming with Abstract Data Types

– ADTs specify/describe behaviors.

  • Basic Program Unit: Class

– Implementatjon of an ADT.

  • Abstractjon enforced by encapsulatjon..
  • Basic Run-tjme Unit: Object

– Instance of a class.

  • Has an associated state.
slide-21
SLIDE 21

CS784 21

Procedural vs Object-Oriented

  • Emphasis on procedural

abstractjon.

  • Top-down design; Step-

wise refjnement.

  • Suited for programming

in the small.

  • Emphasis on data

abstractjon.

  • Botuom-up design;

Reusable libraries.

  • Suited for programming

in the large.

slide-22
SLIDE 22

CS784 22

Integratjng Heterogeneous Data

  • In C, Pascal, etc., use
  • Union Type / Switch Statement
  • Variant Record Type / Case Statement
  • In C++, Java, Eifgel, etc., use
  • Abstract Classes / Virtual Functjons
  • Interfaces and Classes / Dynamic Binding
slide-23
SLIDE 23

CS784 23

Comparison : Figures example

  • Data

– Square

  • side

– Circle

  • radius
  • Operatjon (area)

– Square

  • side * side

– Circle

  • PI * radius * radius
  • Classes

– Square

  • side
  • area
  • (= side * side)

– Circle

  • radius
  • area
  • (= PI*radius*radius)
slide-24
SLIDE 24

CS784 24

Adding a new operatjon

  • Data
  • ...
  • Operatjon (area)
  • Operatjon (perimeter)

– Square

  • 4 * side

– Circle

  • 2 * PI * radius
  • Classes

– Square

  • ...
  • perimeter
  • (= 4 * side)

– Circle

  • ...
  • perimeter
  • (= 2 * PI * radius)
slide-25
SLIDE 25

CS784 25

Adding a new data representatjon

  • Data

– ... – rectangle

  • length
  • width
  • Operatjon (area)

– ... – rectangle

  • length * width
  • Classes

– ... – rectangle

  • length
  • width
  • area
  • (= length * width)
slide-26
SLIDE 26

CS784 26

Procedural vs Object-Oriented

  • New operatjons cause additjve changes in

procedural style, but require modifjcatjons to all existjng “class modules” in object-oriented style.

  • New data representatjons cause additjve

changes in object-oriented style, but require modifjcatjons to all “procedure modules”.

slide-27
SLIDE 27

CS784 27

Object-Oriented Concepts

  • Data Abstractjon (specifjes behavior)
  • Encapsulatjon (controls visibility of names)
  • Polymorphism (accommodates various

implementatjons)

  • Inheritance (facilitates code reuse)
  • Modularity (relates to unit of compilatjon)
slide-28
SLIDE 28

CS784 28

Example : Role of interface in decoupling

  • Client

– Determine the number of elements in a collectjon.

  • Suppliers

– Collectjons : Vector, String, List, Set, Array, etc

  • Procedural Style

– A client is responsible for invoking appropriate supplier functjon for determining the size.

  • OOP Style

– Suppliers are responsible for conforming to the standard interface required for exportjng the size functjonality to a client.

slide-29
SLIDE 29

CS784 29

Client in Scheme

  • (defjne (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))
slide-30
SLIDE 30

CS784 30

Suppliers and Client in Java

Interface Collectjon {int size(); } class myVector extends Vector implements Collectjon { } class myString extends String implements Collectjon { public int size() { return length();} } class myArray implements Collectjon { int[] array; public int size() {return array.length;} } Collectjon c = new myVector(); c.size();