Course Goal CMSC 330: Organization of Programming Languages. All Languages Are (Kind of) Equivalent. Studying Programming Languages.

Similar documents
CMSC 330: Organization of Programming Languages

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

CMSC 330: Organization of Programming Languages. Administrivia

CMSC 330: Organization of Programming Languages. Administrivia

Concepts of Programming Languages

Programming Languages 2nd edition Tucker and Noonan"

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Introduction. A. Bellaachia Page: 1

Programming Languages, Summary CSC419; Odelia Schwartz

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

What is a programming language?

What Is Computer Science? The Scientific Study of Computation. Expressing or Describing

Concepts in Programming Languages

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Com S 541. Programming Languages I

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

CSc 372 Comparative Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Principles of Programming Languages. Lecture Outline

Language Translation, History. CS152. Chris Pollett. Sep. 3, 2008.

CS 6371: Advanced Programming Languages

Lecture 1. Introduction to course, Welcome to Engineering, What is Programming and Why is this the first thing being covered in Engineering?

CMSC 330: Organization of Programming Languages

Chapter 1. Preliminaries

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

1DL321: Kompilatorteknik I (Compiler Design 1)

NOTE: Answer ANY FOUR of the following 6 sections:

CS Exam #1-100 points Spring 2011

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE : Fall 2017

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2013

Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

MIDTERM EXAMINATION - CS130 - Spring 2005

Why study Programming Language Concepts? Chapter One. Language Evaluation Criteria. Programming Domains. Readability Writability Reliability Cost

CS A331 Programming Language Concepts

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Why are there so many programming languages?

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Chapter 1. Preliminaries

Programmiersprachen (Programming Languages)

Seminar in Programming Languages

More Lambda Calculus and Intro to Type Systems

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

CS 3304 Comparative Languages. Lecture 1: Introduction

Programming Paradigms

Computer Science Seminar. Whats the next big thing? Ruby? Python? Neither?

Programming Paradigms

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

MIDTERM EXAMINATION - CS130 - Spring 2003

CS 3110 Lecture 1 Course Overview

Programming Languages

CS558 Programming Languages

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2014

CSCI 3136 Principles of Programming Languages

INSTITUTE OF AERONAUTICAL ENGINEERING

G Programming Languages - Fall 2012

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS 3360 Design and Implementation of Programming Languages. Exam 1

INTRODUCTION PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/10

CS101 Introduction to Programming Languages and Compilers

Chapter 1 Preliminaries

Overview. Rationale Division of labour between script and C++ Choice of language(s) Interfacing to C++ Performance, memory

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

Chapter 2 Preview. Preview. History of Programming Languages. History of Programming Languages. History of Programming Languages

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

MIDTERM EXAM (Solutions)

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Simply-Typed Lambda Calculus

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

G Programming Languages Spring 2010 Lecture 1. Robert Grimm, New York University

CS 240 Fall Mike Lam, Professor. Just-for-fun survey:

Programming Languages

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

Compilers. Prerequisites

Comp 333: Concepts of Programming Languages Fall 2016

CS 3360 Design and Implementation of Programming Languages. Exam 1

Administration Computers Software Algorithms Programming Languages

IA010: Principles of Programming Languages

CS 242. Fundamentals. Reading: See last slide

Course Syllabus. Programming Language Paradigms. Spring - DIS Copenhagen. Semester & Location: Elective Course - 3 credits.

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Introduction CMSC 330: Organization of Programming Languages. Books on Ruby. Applications of Scripting Languages

CS 406/534 Compiler Construction Putting It All Together

CSCI-GA Scripting Languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

CS 314 Principles of Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

CSc 372. Comparative Programming Languages. 1 : Introduction. Department of Computer Science University of Arizona

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

LECTURE 1. Overview and History

Transcription:

Course Goal CMSC 330: Organization of Programming Languages Introduction Dr. Anwar Mamat Learn how programming languages work Broaden your language horizons Different programming languages Different language features and tradeoffs Ø Useful programming patterns Study how languages are described / specified Mathematical formalisms Study how languages are implemented What really happens when I write x.foo( )? CMSC 330 1 2 All Languages Are (Kind of) Equivalent A language is Turing complete if it can compute any function computable by a Turing Machine Essentially all general-purpose programming languages are Turing complete I.e., any program can be written in any programming language Studying Programming Languages Helps you to choose between languages Programming is a human activity Ø Features of a language make it easier or harder to program for a specific application Using the right programming language for a problem may make programming Ø Easier, faster, less error-prone Therefore this course is useless?! Learn only 1 programming language, always use it 3 4 1

Studying Programming Languages Become better at learning new languages A language not only allows you to express an idea, it also shapes how you think when conceiving it Ø There are some fundamental computational paradigms underlying language designs that take getting used to You may need to learn a new (or old) language Ø Paradigms and fads change quickly in CS Ø Also, may need to support or extend legacy systems Why Study Programming Languages? To make you better at learning new languages You may need to add code to a legacy system Ø E.g., FORTRAN (1954), COBOL (1959), You may need to write code in a new language Ø Your boss says, From now on, all software will be written in {C++/Java/C#/Python } You may think Java is the ultimate language Ø But if you are still programming or managing programmers in 20 years, they probably won t be programming in Java! 5 6 Studying Programming Languages Improve your understanding of languages you are already familiar with Many design patterns in Java are functional programming techniques Understanding what a language is good for will help you know when it is appropriate to use The deeper your understanding of a language, the better you will be at using it appropriately Course Subgoals Learn some fundamental programminglanguage concepts Regular expressions Automata theory Context free grammars Computer security Improve programming skills Practice learning new programming languages Learn how to program in a new style 7 8 2

Syllabus Scripting languages (Ruby) Regular expressions & finite automata Context-free grammars & parsing Functional programming (OCaml) Scoping, type systems, parameter passing Logic programming (Prolog) Secure programming Comparing language styles;; other topics Calendar / Course Overview Tests 4 quizzes, 2 midterms, final exam Projects Project 1 Ruby Project 2-4 OCaml Project 5 Prolog Project 6 Secure programming (Ruby) Meet your professor! 1% extra credit : come to chat with your professor during office hours or at a mutually agreed-upon time Conversation need not be long, or technical but we would like to get to know you! 9 10 Discussion Sections Lectures introduce the course content Discussion sections will deepen understanding These are smaller, and thus can be more interactive Oftentimes discussion section will consist of programming exercises Bring your laptop to discussion Be preparedto program: install the language in question on your laptop, or remote shell into Grace There will also be be quizzes, and some lecture material in discussion sections Project Grading You have accounts on the Grace cluster Projects will be graded using the submit server Software versions on these machines are canonical Develop programs on your own machine Generally results will be identical on dept machines Your responsibilityto ensure programs run correctly on the grace cluster See web page for Ruby, Ocaml, SWI-Prolog versions we use, if you want to install at home CMSC 330 - Spring 2011 11 12 3

Rules and Reminders Use lecture notes as your text Supplement with readings, Internet You will be responsible for everything in the notes, even if it is not directly covered in class! Keep ahead of your work Get help as soon as you need it Ø Office hours, Piazza (email as a last resort) Don t disturb other students in class Keep cell phones quiet No laptops / tablets in class Ø Except for taking notes (please sit in back of class) Academic Integrity All written work (including projects) must be done on your own Do not copy code from other students Do not copy code from the web Do not post your code on the web We re using Moss;; cheaters will be caught Work together on high-level project questions Do not look at/describe another student s code If unsure, ask an instructor! Work together on practice exam questions 13 14 Changing Language Goals 1950s-60s Compile programs to execute efficiently Language features based on hardware concepts Ø Integers, reals, goto statements Programmers cheap;; machines expensive Ø Computation was the primary constrained resource Ø Programs had to be efficient because machines weren t Note: this still happens today, just not as pervasively Changing Language Goals Today Language features based on design concepts Ø Encapsulation, records, inheritance, functionality, assertions Machines cheap;; programmers expensive Ø Scripting languages are slow(er), but run on fast machines Ø They ve become very popular because they ease the programming process The constrained resource changes frequently Ø Communication, effort, power, privacy, Ø Future systems and developers will have to be nimble 15 16 4

Language Attributes to Consider Syntax What a program looks like Semantics What a program means (mathematically) Implementation How a program executes (on a real machine) Imperative Languages Also called procedural or von Neumann Building blocks are procedures and statements Programs that write to memory are the norm int x = 0; while (x < y) x = x + 1; FORTRAN (1954) Pascal (1970) C (1971) 17 18 Functional Languages Also called applicative languages Less explicit map to underlying memory Functions are higher-order let rec map f = function [] -> [] LISP (1958) ML (1973) Scheme (1975) Haskell (1987) OCaml (1987) x::l -> (f x)::(map f l) OCaml A mostly-functional language Has objects, but won t discuss (much) Developed in 1987 at INRIA in France Dialect of ML (1973) Natural support for pattern matching Generalizes switch/if-then-else very elegant Has full featured module system Much richer than interfaces in Java or headers in C Includes type inference Ensures compile-time type safety, no annotations 19 20 5

A Small OCaml Example intro.ml: $ ocaml let greet s = List.iter (fun x -> print_string x) [ hello, ; s; "!\n ] Objective Caml version 3.12.1 # #use "intro.ml";; val greet : string -> unit = <fun> # greet "world";; Hello, world! - : unit = () Logic-Programming Languages Also called rule-based or constraint-based Program rules constrain possible results Evaluation = constraint satisfaction = search A :- B If B holds, then A holds ( B implies A ) Ø append([], L2, L2). Ø append([x Xs],Ys,[X Zs]) :- append(xs,ys,zs). PROLOG (1970) Datalog (1977) Various expert systems 21 22 Prolog A logic programming language 1972, University of Aix-Marseille Original goal: Natural language processing Rule based Rules resemble pattern matching and recursive functions in Ocaml, but more general Execution = search Rules specify relationships among data Ø Lists, records, atoms, integers, etc. Programs are queries over these relationships Ø The query will fill in the blanks CMSC 330 - Spring 2011 23 A Small Prolog Example /* A small Prolog program */ female(alice). male(bob). male(charlie). father(bob, charlie). mother(alice, charlie). % X is a son of Y son(x, Y) :- father(y, X), male(x). son(x, Y) :- mother(y, X), male(x). Query?- son(x,y). X = charlie, Y = bob; X = charlie, Y = alice. Multiple answers Lowercase logically terminates Program consists of facts and rules Uppercase denotes variables User types ;; to request additional answer User types return to complete request CMSC 330 - Spring 2011 24 6

Object-Oriented Languages Programs are built from objects Objects combine functions and data Ø Often into classes which can inherit Base may be either imperative or functional class C { int x; int getx() {return x;} } class D extends C { } Smalltalk (1969) C++ (1986) OCaml (1987) Ruby (1993) Java (1995) 25 Scripting Languages Rapid prototyping languages for common tasks Traditionally: text processing and system interaction Scripting is a broad genre of languages Base may be imperative, functional, OO Increasing use due to higher-layer abstractions Not just for text processing anymore sh (1971) perl (1987) Python (1991) Ruby (1993) #!/usr/bin/ruby while line = gets do csvs = line.split /,/ if(csvs[0] == 330 ) then... 26 Ruby A Small Ruby Example An imperative, object-oriented scripting language Created in 1993 by Yukihiro Matsumoto (Matz) Ruby is designed to make programmers happy Core of Ruby on Rails web programming framework (a key to its popularity) Similar in flavor to many other scripting languages Much cleaner than perl Full object-orientation (even primitives are objects!) intro.rb: def greet(s) 3.times { print Hello, } print #{s}!\n end % irb # you ll usually use ruby instead irb(main):001:0> require "intro.rb" => true irb(main):002:0> greet("world") Hello, Hello, Hello, world! => nil 27 28 7

Concurrent / Parallel Languages Traditional languages had one thread of control Processor executes one instruction at a time Newer languages support many threads Thread execution conceptually independent Means to create and communicate among threads Concurrency may help/harm Readability, performance, expressiveness Won t cover in this class Threads covered in 132 and 216;; more in 412, 433 Supporting secure execution Security is a big issue today Features of the language can help (or hurt) C/C++ lack of memory safety leaves them open for many vulnerabilities: buffer overruns, use-after-free errors, data races, etc. Type safety is a big help, but so are abstraction and isolation facilities, to help enforce security policies, and limit the damage of possible attacks Additional ecosystem support also useful Fuzz testing, static analysis, dynamic analysis (e.g., taint tracking) 29 CMSC 330 - Spring 2011 30 Other Languages There are lots of other languages w/ various features COBOL (1959) Business applications Ø Imperative, rich file structure BASIC (1964) MS Visual Basic Ø Originally designed for simplicity (as the name implies) Ø Now it is object-oriented and event-driven, widely used for UIs Logo (1968) Introduction to programming Forth (1969) Mac Open Firmware Ø Extremely simple stack-based language for PDP-8 Ada (1979) The DoD language Ø Real-time Postscript (1982) Printers- Based on Forth Program Execution Suppose we have a program P written in a high-level language (i.e., not machine code) There are two main ways to run P 1. Compilation 2. Interpretation 31 32 8

Compilation Interpretation def greet(s) print("hello, ) print(s) print("!\n ) end 11230452 23230456 01200312 def greet(s) print("hello, ) print(s) print("!\n ) end Hello, world! world world Hello, world! Source program translated ( compiled ) to another language Traditionally: directly executable machine code Generating code from a higher level interface is also common (e.g., JSON, RPC IDL) Interpreter executes each instruction in source program one step at a time No separate executable 33 34 Architecture of Compilers, Interpreters Front Ends and Back Ends Source Parser Static Analyzer Front End Intermediate Representation Compiler / Interpreter Back End Front ends handle syntactic analysis Parser converts source code into intermediate format ( parse tree ) reflecting program structure Static analyzer checks parse tree for errors (e.g. types), may also modify it What goes into static analyzer is languagedependent! Back ends handle semantics Compiler: back end ( code generator ) translates intermediate representation into object language Interpreter: back end executes intermediate representation directly 35 36 9

Compiler or Intepreter? gcc Compiler C code translated to object code, executed directly on hardware (as a separate step) javac Compiler Java source code translated to Java byte code java Interpreter Java byte code executed by virtual machine sh/csh/tcsh/bash Interpreter commands executed by shell program Compilers vs. Interpreters Compilers Generated code more efficient Heavy Interpreters Great for debugging Slow In practice General-purpose programming languages (e.g. C, Java) are often compiled, although debuggers provide interpreter support Scripting languages and other special-purpose languages are interpreted, even if general purpose 37 38 Formal (Mathematical) Semantics What do my programs mean? let rec fact n = if n = 0 then 1 else n * (fact n-1) let fact n = let rec aux i j = if i = 0 then j else aux (i-1) (j*i) in aux n 1 Both OCaml functions implement the factorial function. How do I know this? Can I prove it? Key ingredient: a mathematical way of specifying what programs do, i.e., their semantics Doing so depends on the semantics of the language 39 Semantic styles Textual language definitions are often incomplete and ambiguous A formal semantics is basically a mathematical definition of what programs do. Two flavors: Denotational semantics (compiler/translator) Ø Meaning defined in terms of another language (incl. math) Ø If we know what C means, then we can define Ruby by translation to C Operational semantics (interpreter) Ø Meaning defined as rules that simulate program execution Ø Show what Ruby programs do directly, using an abstract machine, more high-level than real hardware 40 10

Attributes of a Good Language Cost of use Program execution (run time), program translation, program creation, and program maintenance Portability of programs Develop on one computer system, run on another Programming environment External support for the language Libraries, documentation, community, IDEs, Attributes of a Good Language Clarity, simplicity, and unity Provides both a framework for thinking about algorithms and a means of expressing those algorithms Orthogonality Every combination of features is meaningful Features work independently Naturalness for the application Program structure reflects the logical structure of algorithm 41 42 Attributes of a Good Language What Programmers Want In a PL Support for abstraction Hide details where you don t need them Program data reflects the problem you re solving Security & safety Should be very difficult to write unsafe programs Ease of program verification Does a program correctly perform its required function? 43 Meyerovitch & Rabin, Empirical analysis of programming language adoption, OOPSLA 13 44 11

Summary Many types of programming languages Imperative, functional, logical, OO, scripting, Many programming language attributes Clear, natural, low cost, verifiable, secure, Programming language implementation Compiled, interpreted Programming language semantics Proving your program operates correctly 45 12