disclaimer
play

Disclaimer The lecture slides for this semester are based upon the - PDF document

Disclaimer The lecture slides for this semester are based upon the slides used by Dr. Adam Webber. Credit Programming Paradigms and thanks are due to Dr. Webber; mistakes are mine. Primary Language Classification 01 Introduction to Language


  1. Disclaimer The lecture slides for this semester are based upon the slides used by Dr. Adam Webber. Credit Programming Paradigms and thanks are due to Dr. Webber; mistakes are mine. Primary Language Classification 01 Introduction to Language Paradigms CS631 Fall 2000 1 01 Introduction to Language Paradigms CS631 Fall 2000 2 Related Reading Overview � Why bother studying programming languages? Chapter 1 � Classification of programming languages Programming Languages Concepts and � Strengths and weaknesses of each paradigm Constructs, Ravi Sethi � Best uses for each of the paradigms “Don’t fight the paradigm” “Use the right tool for the job” 01 Introduction to Language Paradigms CS631 Fall 2000 3 01 Introduction to Language Paradigms CS631 Fall 2000 4 Why Study Programming Classification: Languages? Four Language Families � Computer science is a young, evolving field. � Imperative � Today’s cool language is tomorrow’s ‘has been’ � Object-oriented � “Everything old is new again” � Functional � The more you know about programming � Logic languages, the easier it will be to learn the “next great language.” � It is an active research area. 01 Introduction to Language Paradigms CS631 Fall 2000 5 01 Introduction to Language Paradigms CS631 Fall 2000 6 1

  2. The Imperative Language Paradigm Imperative Example: Factorial in C � An imperative program is a sequence of void main(int argc, char *argv[]) { statements. int n = atoi(argv[1]) ; � Each statement is a directive to change the int sofar = 1; while (n > 1) “environment” (memory, devices, etc.). sofar *= n--; � Changes are cumulative, so the sequence must printf (“%d\n”, sofar); } be done in the order specified. Imperative characteristics: � Imperative languages are tightly modeled to the assignment statements ( = and *= ) computer hardware loop depends on side-effects sequence of statements with implied ordering 01 Introduction to Language Paradigms CS631 Fall 2000 7 01 Introduction to Language Paradigms CS631 Fall 2000 8 Imperative Languages Imperative Paradigm Strengths � All the most popular languages � People seem to be able to program in this mode most easily. – COBOL � Since it is derived from the way computer – BASIC hardware works, it is easy to translate into – C efficient machine language. – Fortran – Ada – Pascal – and many others 01 Introduction to Language Paradigms CS631 Fall 2000 9 01 Introduction to Language Paradigms CS631 Fall 2000 10 Imperative Language Drawback #1: Example a = f(x,y); Difficult to Reason About b = f(x,y); // Does a equal b? � Informally, programmers try to prove to f might not be referentially transparent: themselves that the program works. int f(int x, int y) � Formally, the holy grail for many researchers is { return system_clock; to find a way to formally prove that a program is } correct. Or a and x might be aliased: Or f might change a : � But imperative languages make it difficult: you int a; int f(int x, int y) int& x = a; { can’t reason about any part of the program ... a = 0; independently of the other parts. . . a = f(x,y); return x + y; b = f(x,y); } 01 Introduction to Language Paradigms CS631 Fall 2000 11 2

  3. Imperative Lang. Drawback #2 (1): Imperative Lang. Drawback #2 (2): Hard to Parallelize Parallelization, Example 1 � If a computer has multiple processors, you can For each group of C statements below, is it possible for a non-standard order of execution to give different results speed up your program if you can make use of from the standard order? more than one processor. � This is only possible if we can identify parts that a = 0; a = 0; b = a; a = 1; do not depend on being executed in the standard order. � Side effects and implied sequencing in b = a; a = 0; a = 0; b = 0; imperative languages make this difficult to do automatically. 01 Introduction to Language Paradigms CS631 Fall 2000 13 01 Introduction to Language Paradigms CS631 Fall 2000 14 Imperative Lang. Drawback #2 (3): Imperative Lang. Drawback #2 (4): Parallelization, Example 2 Parallelization, Example 3 For each group of C statements below, is it possible for a For each group of C statements below, is it possible for a non-standard order of execution to give different results non-standard order of execution to give different results from the standard order? from the standard order? *p = 1; for (int i = 0; i < 100; i++) *q = 2; a[i] = i * 2; *p = 0; for (int i = 1; i < 100; i++) *q = 0; a[i] = a[i-1] * 2; 01 Introduction to Language Paradigms CS631 Fall 2000 15 01 Introduction to Language Paradigms CS631 Fall 2000 16 OO Example class MyInt “ I am a MyInt object” The Object-Oriented Paradigm Factorial in Java { private int value; public MyInt(int value) “ I can be created ” � A object oriented program is a set of objects. { “ I hold an int value ” this.value = value; } � Each object has private state, and presents a public int getValue() “ I can tell you my value ” { return value; strictly-enforced interface to be used by other } public MyInt getFactorial() objects. “ I can return a MyInt object { � A computation is the interaction of a collection return new MyInt(fact(value)); for my factorial... ” } private int fact(int n) of such objects. { int sofar = 1; “ …but how I calculate my while (n > 1) factorial is my own business ” sofar *= n--; return sofar; } } 01 Introduction to Language Paradigms CS631 Fall 2000 17 3

  4. The Object-Oriented Paradigm Object-oriented Languages Is it really just imperative? � Simula � An object has a state that is altered via method � Smalltalk calls (side effects). � C++ � Objects execute code that looks like ordinary � Java imperative code � Eiffel Object-oriented programming is really a variation on the theme of imperative programming. 01 Introduction to Language Paradigms CS631 Fall 2000 19 01 Introduction to Language Paradigms CS631 Fall 2000 20 Object Oriented Benefits (1) Object Oriented Benefits (2) Better Organization of Large Programs Mitigate Drawbacks of Imperative � Encapsulation: objects hide implementation � Easier to reason about: details from other objects – OOP helps since private properties of objects can be � Inheritance: objects can inherit functionality reasoned about in isolation from the rest of the program. from other objects (makes code reuse simpler) � Easier to parallelize: – OOP helps since the strict interfaces among objects Encapsulation and inheritance also help moderate make good places at which to divide up the the drawbacks of imperative programs... computation among machines. 01 Introduction to Language Paradigms CS631 Fall 2000 21 01 Introduction to Language Paradigms CS631 Fall 2000 22 OO Drawbacks The Functional Paradigm � OOP adds an extra discipline applied to � A functional program is a collection of function imperative programs, but at a cost: definitions. – Programs can be longer � Each function is free of side-effects: it has no – Languages can be more complex detectable permanent effect on memory, – Paradigm can take longer to master devices, etc. � Modeled on functions in mathematics. Bad OOP code is often worse than bad imperative code 01 Introduction to Language Paradigms CS631 Fall 2000 23 01 Introduction to Language Paradigms CS631 Fall 2000 24 4

  5. Functional Example Functional Languages Factorial in ML � Lisp-y languages fun fact x = if x = 0 – Common Lisp, Scheme, T then � More recent functional languages 1 else – Haskell, Gofer, ML x * fact(x-1); � “Pure” functional Functional characteristics: – FP No assignment statements Recursion (not necessarily present) Referential transparency 01 Introduction to Language Paradigms CS631 Fall 2000 25 01 Introduction to Language Paradigms CS631 Fall 2000 26 Functional Benefits (1) Functional Benefits (2) Easier to Parallelize Easier to Reason About � Side-effects are not allowed. � When all functions exhibit referential � Any execution order is safe. transparency we know that calling the same function with the same parameters always f( g(a), h(a) ) produces the same result. We know these can be evaluated ( ) ( ) ( ) = ∧ = ⇒ , = , x x y y f x y f x y simultaneously, without even 1 2 1 2 1 1 2 2 seeing the definitions of g and h . 01 Introduction to Language Paradigms CS631 Fall 2000 27 01 Introduction to Language Paradigms CS631 Fall 2000 28 Functional Paradigm Drawbacks The Logic Paradigm � No side effects is both a blessing and a � A logic program is a collection of formal logical curse assertions and rules of inference. � Execution means proving a new assertion (the – Pro: You can reason about the program – Con: I/O is technically not allowed “goal”) from the part already given. � Paradigm can be difficult to learn � Modeled on mathematical first-order logic. – Recursion can be a very difficult concept – No assignment statements 01 Introduction to Language Paradigms CS631 Fall 2000 29 01 Introduction to Language Paradigms CS631 Fall 2000 30 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend