Slide 1 Vitaly Shmatikov CS 345 Programming Paradigms.

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
High-Level Programming Languages
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Computability (Chapter 2) ICS 535 Design and Implementation.
Programming Languages Structure
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Introduction and Syntax. Course objectives Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
CS 355 – Programming Languages
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
High-Level Programming Languages: C++
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
COP4020 Programming Languages
Chapter 1. Introduction.
CS 363 Comparative Programming Languages
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages –14 David Watt (Glasgow) Steven Wong (Singapore) Moodle : Computing Science → Level 3 → Programming Languages 3 © 2012 David.
Chapter 1 - Introduction
Programming Languages
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
1 Programming Language History and Evolution In Text: Chapter 2.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Programming Languages
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Programming Languages John Mitchell CS 242 Course web site:
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Computable Functions John Mitchell CS 242 Reading: Chapter 2.
1-1 1 Introduction  Programming linguistics: concepts and paradigms syntax, semantics, and pragmatics language processors.  Historical development of.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Programming Languages John Mitchell CS 242 Course web site:
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Language Paradigms CS655.
Programming Language History and Evolution
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages 2nd edition Tucker and Noonan
Why study programming languages?
Types for Programs and Proofs
PROGRAMMING LANGUAGES
Representation, Syntax, Paradigms, Types
Programming Language History and Evolution
Representation, Syntax, Paradigms, Types
Programming Languages 2nd edition Tucker and Noonan
High Level Programming Languages
Representation, Syntax, Paradigms, Types
Principles of Programming Languages
Representation, Syntax, Paradigms, Types
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Programming Paradigms

slide 2 Reading Assignment uMitchell, Chapter 2.1

slide 3 What Is a Programming Language? uFormal notation for specifying computations, independent of a specific machine Example: a factorial function takes a single non- negative integer argument and computes a positive integer result –Mathematically, written as fact: nat  nat uSet of imperative commands used to direct computer to do something useful Print to an output device: printf(“hello world\n”); –What mathematical function is “computed” by printf?

slide 4 Partial and Total Functions uValue of an expression may be undefined Undefined operation, e.g., division by zero –3/0 has no value –Implementation may halt with error condition Nontermination –f(x) = if x=0 then 1 else f(x-2) –This is a partial function: not defined on all arguments –Cannot be detected by inspecting expression (why?) uThese two cases are “mathematically” equivalent, but operationally different (why?) Subtle: “undefined” is not the name of a function value …

slide 5 Partial and Total: Definitions uTotal function f:A  B is a subset f  A  B with  x  A, there is some y  B with  x,y   f (total) If  x,y   f and  x,z   f then y=z (single-valued) uPartial function f:A  B is a subset f  A  B with If  x,y   f and  x,z   f then y=z (single-valued) uPrograms define partial functions for two reasons What are these reasons?

slide 6 Computability uFunction f is computable if some program P computes it For any input x, the computation P(x) halts with output f(x) Partial recursive functions: partial functions (int to int) that are computable

slide 7 Halting Problem Ettore Bugatti: "I make my cars to go, not to stop"

slide 8 Halting Function uDecide whether program halts on input Given program P and input x to P, Halt(P,x) = Fact: There is no program for Halt yes if P(x) halts no otherwise Clarifications Assume program P requires one string input x Write P(x) for output of P when run in input x Program P is string input to Halt

slide 9 Unsolvability of the Halting Problem uSuppose P solves variant of halting problem On input Q, assume P(Q) = uBuild program D D(Q) = uIf D(D) halts, then D(D) runs forever uIf D(D) runs forever, then D(D) halts uContradiction! Thus P cannot exist. yes if Q(Q) halts no otherwise run forever if Q(Q) halts halt if Q(Q) runs forever

slide 10 Main Points About Computability uSome functions are computable, some are not Example: halting problem uProgramming language implementation Can report error if program result is undefined due to an undefined basic operation (e.g., division by zero) Cannot report error if program will not terminate

slide 11 Computation Rules uThe factorial function type declaration does not convey how the computation is to proceed uWe also need a computation rule fact (0) = 1 fact (n) = n * fact(n-1) uThis notation is more computationally oriented and can almost be executed by a machine

slide 12 Factorial Functions uC, C++, Java: int fact (int n) { return (n == 0) ? 1 : n * fact (n-1); } uScheme: (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) uML: fun fact n = if n=0 then 1 else n*fact(n-1); uHaskell: fact :: Integer->Integer fact 0 = 1 fact n = n*fact(n-1)

slide 13 Principal Paradigms uImperative / Procedural uFunctional / Applicative uObject-Oriented uConcurrent uLogic uScripting uIn reality, very few languages are “pure” Most combine features of different paradigms

slide 14 Where Do Paradigms Come From? uParadigms emerge as the result of social processes in which people develop ideas and create principles and practices that embody those ideas Thomas Kuhn. “The Structure of Scientific Revolutions.” uProgramming paradigms are the result of people’s ideas about how programs should be constructed … and formal linguistic mechanisms for expressing them … and software engineering principles and practices for using the resulting programming language to solve problems

slide 15 Imperative Paradigm uImperative (procedural) programs consists of actions to effect state change, principally through assignment operations or side effects Fortran, Algol, Cobol, PL/I, Pascal, Modula-2, Ada, C Why does imperative programming dominate in practice? uOO programming is not always imperative, but most OO languages have been imperative Simula, Smalltalk, C++, Modula-3, Java Notable exception: CLOS (Common Lisp Object System)

slide 16 Functional and Logic Paradigms uFocuses on function evaluation; avoids updates, assignment, mutable state, side effects uNot all functional languages are “pure” In practice, rely on non-pure functions for input/output and some permit assignment-like operators –E.g., (set! x 1) in Scheme uLogic programming is based on predicate logic Targeted at theorem-proving languages, automated reasoning, database applications Recent trend: declarative programming

slide 17 Concurrent and Scripting Languages uConcurrent programming cuts across imperative, object-oriented, and functional paradigms uScripting is a very “high” level of programming Rapid development; glue together different programs Often dynamically typed, with only int, float, string, and array as the data types; no user-defined types Weakly typed: a variable ‘x’ can be assigned a value of any type at any time during execution uVery popular in Web development Especially scripting active Web pages

slide 18 Unifying Concepts uUnifying language concepts Types (both built-in and user-defined) –Specify constraints on functions and data –Static vs. dynamic typing Expressions (e.g., arithmetic, boolean, strings) Functions/procedures Commands uWe will study how these are defined syntactically, used semantically, and implemented pragmatically

slide 19 Design Choices uC: Efficient imperative programming with static types uC++: Object-oriented programming with static types and ad hoc, subtype and parametric polymorphism uJava: Imperative, object-oriented, and concurrent programming with static types and garbage collection uScheme: Lexically scoped, applicative-style recursive programming with dynamic types uStandard ML: Practical functional programming with strict (eager) evaluation and polymorphic type inference uHaskell: Pure functional programming with non-strict (lazy) evaluation.

slide 20 Abstraction and Modularization uRe-use, sharing, extension of code are critically important in software engineering uBig idea: detect errors at compile-time, not when program is executed uType definitions and declarations Define intent for both functions/procedures and data uAbstract data types (ADT) Access to local data only via a well-defined interface uLexical scope

slide 21 Static vs. Dynamic Typing uStatic typing Common in compiled languages, considered “safer” Type of each variable determined at compile-time; constrains the set of values it can hold at run-time uDynamic typing Common in interpreted languages Types are associated with a variable at run-time; may change dynamically to conform to the type of the value currently referenced by the variable Type errors not detected until a piece of code is executed

slide 22 Billion-Dollar Mistake Failed launch of Ariane 5 rocket (1996) $500 million payload; $7 billion spent on development Cause: software error in inertial reference system Re-used Ariane 4 code, but flight path was different 64-bit floating point number related to horizontal velocity converted to 16-bit signed integer; the number was larger than 32,767; inertial guidance crashed

slide 23 Program Correctness uAssert formal correctness statements about critical parts of a program and reason effectively A program is intended to carry out a specific computation, but a programmer can fail to adequately address all data value ranges, input conditions, system resource constraints, memory limitations, etc. uLanguage features and their interaction should be clearly specified and understandable If you do not or can not clearly understand the semantics of the language, your ability to accurately predict the behavior of your program is limited

slide 24 uNative-code compiler: produces machine code Compiled languages: Fortran, C, C++, SML … uInterpreter: translates into internal form and immediately executes (read-eval-print loop) Interpreted languages: Scheme, Haskell, Python … uByte-code compiler: produces portable bytecode, which is executed on virtual machine (e.g., Java) uHybrid approaches Source-to-source translation (early C++  C  compile) Just-in-time Java compilers convert bytecode into native machine code when first executed Language Translation

slide 25 Language Compilation uCompiler: program that translates a source language into a target language Target language is often, but not always, the assembly language for a particular machine C compiler C source code x86 assembler x86 ASMPentium op codes

slide 26 Checks During Compilation uSyntactically invalid constructs uInvalid type conversions A value is used in the “wrong” context, e.g., assigning a float to an int uStatic determination of type information is also used to generate more efficient code Know what kind of values will be stored in a given memory region during program execution uSome programmer logic errors Can be subtle: if (a = b) … instead of if (a == b) …

slide 27 Compilation Process Lexical analyzer raw source code text Syntax analyzer + type checker tokensASTs Intermediate code gen Optimizer ICIC opt Final code gen ASM Assembler Machine code Syntax and static type errors Preprocessor Source code with preprocessor directives

slide 28 Phases of Compilation uPreprocessing: conditional macro text substitution uLexical analysis: convert keywords, identifiers, constants into a sequence of tokens uSyntactic analysis: check that token sequence is syntactically correct Generate abstract syntax trees (AST), check types uIntermediate code generation: “walk” the ASTs and generate intermediate code Apply optimizations to produce efficient code uFinal code generation: produce machine code

slide 29 Language Interpretation uRead-eval-print loop Read in an expression, translate into internal form Evaluate internal form –This requires an abstract machine and a “run-time” component (usually a compiled program that runs on the native machine) Print the result of evaluation Loop back to read the next expression REPL interpreter input expression Interpreter runtime result

slide 30 Virtual machine runtime Bytecode Compilation uCombine compilation with interpretation Idea: remove inefficiencies of read-eval-print loop uBytecodes are conceptually similar to real machine opcodes, but they represent compiled instructions to a virtual machine instead of a real machine Source code statically compiled into a set of bytecodes Bytecode interpreter implements the virtual machine In what way are bytecodes “better” then real opcodes? Bytecode compiler source program bytecodes Bytecode interpreter result

slide 31 Binding uBinding = association between an object and a property of that object Example: a variable and its type Example: a variable and its value uA language element is bound to a property at the time that property is defined for it Early binding takes place at compile-time Late binding takes place at run-time