Ti1220 Lecture 1
-
Upload
eelco-visser -
Category
Education
-
view
7 -
download
1
description
Transcript of Ti1220 Lecture 1
Challenge the future
DelftUniversity ofTechnology
TI1220Concepts of Programming Languages
Eelco Visser
Concepts of Programming Languages
today’s lecture
TI1220 - Introduction
Overview
A new course
• from IN1605-B to TI1220
What is this course about?
• why study concepts of programming languages?
How is this course organized?
• lectures and assignments
Introduction to Scala
• writing and running Scala programs
2
TI 1220 - Lecture 1: Introduction
A New Course
I
3
the only constant is change
TI1220 - Introduction
A New Course
IN1605-B: Algoritmen en Programmeertalen: Programmeertalen
• 4 ECTS
• Haskell + C
• Peter Kluijt
TI1220: Concepten van Programmeertalen
• 6 ECTS
• Scala + C + JavaScript
• Eelco Visser
4
Eelco Visser
TI1220 - Introduction
Under New Management
Lecturer
• Dr. Eelco Visser
• UHD in Software Engineering Research Group
• http://eelcovisser.org
• Contact: [email protected]
• first ask student assistants
• send questions by email
• appointment if necessary
5
software language design & engineering
TI1220 - Introduction
My Research
6
Spoofax Language Workbench
• design and implement languages & IDEs
• from high-level declarative specification
Domain-specific language design
• WebDSL: a DSL for web programming
• Mobl: a DSL for mobile web applications
Interaction design
• researchr.org: building a digital library
TI1220 - Introduction
Examination Old Course
You can still sit for exams of IN1605-B
• Provided you have finished the lab (practicum)
Sittings
• Together with exam for this course (TI1220)
• April 11, 2010
• June 22, 2010
7
TI1220 - Introduction
Feedback Appreciated
An invitation to Murphy
• New content
• New software
• New lectures, assignments, exams
• New lecturer
• New assistants (mostly)
Please let us know how it goes
• report problems early
• also let us know what goes right :)
8
TI 1220 - Lecture 1: Introduction
Concepts of Programming Languages
II
9
TI1220 - Introduction
Computers Process Data
10
computerdata data
TI1220 - Introduction
Programmable Computers
11
computerdata data
program
TI1220 - Introduction
Programming Language
12
computerdata data
L program
computability
TI1220 - Introduction
Turing Machines
Turing/Church Thesis
• Every effective computation can be carried out by a Turing machine*
Corollary
• All (Turing Complete) programming languages can express all effective computations
• Why bother with new programming languages?
(*) see IN2505
13
timeline
TI1220 - Introduction 14
1986 1990 1990 1991 1991 1993 1994 1995 1996 1996 1997 1997 2000 2001 2001 2003 2003 2004
History of Programming Languages
©2004 O’Reilly Media, Inc. O’Reilly logo is a registered trademark of O’Reilly Media, Inc. All other trademarks are property of their respective owners. part#30417
19601954 1965 1970 1975 1980 1985 1990 1995 2000 20022001 2003 2004
For more than half of the fifty years computer programmers have beenwriting code, O’Reilly has provided developers with comprehensive,in-depth technical information. We’ve kept pace with rapidly changingtechnologies as new languages have emerged, developed, andmatured. Whether you want to learn something new or needanswers to tough technical questions, you’ll find what you need in O’Reilly books and on the O’Reilly Network.
This timeline includes fifty of the more than 2500 documented programming languages. It is based on an original diagram createdby Éric Lévénez (www.levenez.com), augmented with suggestionsfrom O’Reilly authors, friends, and conference attendees.
For information and discussion on this poster, go to www.oreilly.com/go/languageposter.
www.oreilly.com
This timeline includes 50 of the more than 2500 documented programming languages.
History of Programming Languages
TI1220 - Introduction
Programming Languages in TI
15
TI1200: Object-Oriented Programming
• Java
TI1400: Computer Systems
• Assembly
TI1500: Web- and Database Technology
• HTML, PHP, SQL, JavaScript
TI1600: Multi-Agent Systems
• Prolog, GOAL
TI1220 - Introduction 16
“A programming language is low level when its programs require attention to the irrelevant”
Alan J. Perlis. Epigrams on Programming. SIGPLAN Notices, 17(9):7-13, 1982.
purpose of programming languages
TI1220 - Introduction
Understanding Computation
17
Encoding computation
• Computer needs list of instructions to move its parts
Understanding computation
• Discuss concepts
• Formulate algorithms
• Reason about problem solutions
Programming languages are for people
concepts of programming languages
TI1220 - Introduction
Increasing the Level of Abstraction
18
f(e1)
calc: push eBP ; save old frame pointer mov eBP,eSP ; get new frame pointer sub eSP,localsize ; reserve place for locals . . ; perform calculations, leave result in AX . mov eSP,eBP ; free space for locals pop eBP ; restore old frame pointer ret paramsize ; free parameter space and return
push eAX ; pass some register resultpush byte[eBP+20] ; pass some memory variable (FASM/TASM syntax)push 3 ; pass some constantcall calc ; the returned result is now in eAX
def f(x)={ ... }
http://en.wikipedia.org/wiki/Calling_convention
concepts of programming languages
TI1220 - Introduction
Programming Models
Language provides model of computation
• data & operations, composition & abstraction
Language targets application domain
• Systems programming
• Embedded systems
• Web programming
• Enterprise software
19
concepts of programming languages
TI1220 - Introduction
Programming Language DNA
Scope and binding
• declaration and binding of names, free and bound occurrences, renaming, substitution
Data structures
• product and sum types, records, recursive types
Modularization and data abstraction
• modules, classes, higher-order functions, class-based vs prototype inheritance
Syntax and interpretation
20
concepts of programming languages
TI1220 - Introduction
Why Study Concepts?
Design abstractions for new domains
• from library to language feature
• apply PL techniques (MapReduce)
Learn new computational models
• understand fundamental ideas underlying PLs
• experience with variety of models
• learn new languages in future
Choosing the right language
21
concepts of programming languages
TI1220 - Introduction
Goals of this Course
• Understand programming languages and programs in terms of concepts that transcend individual languages
• Consider advantages and disadvantages in the use of particular languages and concepts
• Analyze programs in terms of concepts
• Application of concepts in designing programs
22
concepts of programming languages
TI1220 - Introduction
Approach
Study three concrete languages
• Scala + C + JavaScript
• Learn to program in these languages
• Learn concepts by using them in programming
• Programming assignments about languages
Generalize
• What are the underlying ideas?
• Learn to learn new languages
23
functional programming
TI1220 - Introduction
Scala
• Lecture 1: Getting Started with Scala
• Lecture 2: Functional Objects
• Lecture 3: Functions & Closures
• Lecture 4: List Programming (Recursion)
• Lecture 5: Trees & XML
• Lecture 6: Composition, Inheritance, Traits
• Lecture 7: Modular Programming
• Assignment: interpreter for tiny functional language
24
systems programming
TI1220 - Introduction
C
• Lecture 8: Memory Management
• stack vs heap
• pointers
• Lecture 9: Implementing Objects
• function pointers
• simulating dynamic dispatch
• Assignment: implementing OO in C
25
web programming
TI1220 - Introduction
JavaScript
Prototype-Based Object-Oriented Language
• Lecture 10: Prototypal Inheritance
• History: Smalltalk, Self (Squeak)
• Lecture 11: Asynchronous functions
• Lecture 12: DOM manipulation
• Lecture 13: Regular expressions
• Lecture 14: mobl: a domain-specific language for the mobile web
• Assignment: playing with prototypes
26
implementation of programming languages
TI1220 - Introduction 27
Meta-Programming
Other courses consider internals of languages
• IN2505: Fundamental Computer Science 2: Theory of Computation
• grammars and automata
• IN4303: Compiler Construction
• translation of high-level source to machine language
• IN4308: Model-Driven Software Development
• design of domain-specific software languages
TI 1220 - Lecture 1: Introduction
Course Organization
III
28
course organization
TI1220 - Introduction
Ingredients
This is a semester course
• Q3 + Q4
Lectures
• Tuesday morning
Practicum
• on Tuesday or Thursday afternoon
• Exercises + Graded Assignments
Exams
29
course organization
TI1220 - Introduction
Exams
Exam 1
• Monday 11 April, 2011, 9:00-12:00
• E1: about material of Q3 (Scala)
Exam 2
• Wednesday, 22 June, 2011, 9:00-12:00
• If Exam 1 >= 6
• then E2a: material of Q4 (C+JavaScript)
• else E2b: material Q3+Q4 (Scala+C+JavaScript)
30
course organization
TI1220 - Introduction
Exams
Exam 3 (resit)
• If Exam 2 < 6, even if Exam 1 >= 6
• Wednesday, 24 August, 2011, 9:00-12:00
• E3: material Q3+Q4 (Scala+C+JavaScript)
Note
• next year programming exams
31
course organization
TI1220 - Introduction
Practicum
Scala (Q3)
• Exercises: hand in on paper to assistants at end of lab!
• Assignment 1 (graded)
C (Q4)
• Assignment 2 (graded)
JavaScript (Q4)
• Assignment 3 (graded)
Graded assignments should be submitted as code
32
course organization
TI1220 - Introduction
Final Grade
33
val H = all exercises handed in
val A = 0.4*A1 + 0.3*A2 + 0.3*A3
val E2 = if(E1 >= 6) (E1 + E2a)/2
else E2b
val E = if(E2 >= 6) E2 else E3
val G = if(H && Ai >= 6 && E >= 6)
0.4*A + 0.6E
else min(E, 5)
course organization
TI1220 - Introduction
Practicum Groups
34
Tuesday: Group 1
• mentor groups 1 to 6
Thursday: Group 2
• mentor groups 7 to 12
course organization
TI1220 - Introduction
Practicum Assistants
35
Tuesday Thursday
Maartje de Jonge Sander van der Burg
Laurent Verweijen Bart van Vuuren
Vlad Vergu Peter Pul
Joost Heijkoop
TI1220 - Introduction 36
coffee break
Term Rewriting
The Scala Programming Language
IV
37
imperative vs functional programming
TI1220 - Introduction
Programming Language Schools
Imperative Programming (Algol, C, Pascal)
• programming with side effects
• structured control-flow
Object-Oriented Programming (C++, Java)
• imperative programming with data abstraction
Functional Programming (ML, Haskell)
• functions as first-class citizens
• immutable data (pure)
38
history
TI1220 - Introduction 39
Pizza
POPL 1997
• "Pizza into Java: Translating theory into practice", Martin Odersky and Philip Wadler. Proc. 24th ACM Symposium on Principles of Programming Languages, Paris, France, January 1997.
Extending Java with Functional Concepts
• Parametric polymorphism (generics)
• First-class functions (function pointers)
• Algebraic data types (class cases and pattern matching)
Continued in GJ and Java Generics
Philip Wadler, Martin Odersky, Gilad Bracha, Dave Stoutamire (source: http://lamp.epfl.ch/pizza/gj/)
history
TI1220 - Introduction 40
Scala
A Scalable Language
• object-oriented + functional programming
Properties
• compatible (combines with Java)
• concise (sugar + abstractions)
• high-level
• statically typed
Designed by Martin Odersky et al. at EPFL
history
TI1220 - Introduction
Scala in Industry
Scala at Twitter
• Ruby-on-Rails did not scale
• Replaced back-end processing (API) with Scala
BankSimple
• Back-end services in Scala or Clojure
Alex Payne
• Programming Scala by Wampler & Payne
41
Book
TI1220 - Introduction
Programming in Scala
42
Programming in ScalaA comprehensive step-by-step guide
by Martin Odersky, Lex Spoon, and Bill Venners
http://www.artima.com/shop/programming_in_scala
Available as e-book (PDF)
Lectures are based on book; not everything in book is on slides; reading book is required; see Literature slides at end of each lecture for required reading!
B:2.1 TI1220 - Introduction
Installing Scala
Download Interpreter & Compiler
• Make your own installation (assignment 1)
• http://www.scala-lang.org/downloads
Editors
• Emacs, Vi, <your favorite editor>
Integrated Development Environments
• Plugins for Eclipse, IntelliJ, NetBeans
43
B:2.1
$ scalaWelcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).Type in expressions to have them evaluated.Type :help for more information..
TI1220 - Introduction
The Scala Interpreter
44
B:2 Term Rewriting
Variables and Functions
V
45
B:2.1
scala> 1 + 2res0: Int = 3
scala> res0 * 3res1: Int = 9
scala> println("Hello, world!")Hello, world!
repl: read, eval, print loop
TI1220 - Introduction
Evaluating Expressions
46
B:2.2
scala> val msg = "Hello, world!"msg: java.lang.String = Hello, world!
scala> val msg2: java.lang.String = | "Hello again, world!"msg2: java.lang.String = Hello again, world!
scala> val msg3: String = "Hello yet again, world!"msg3: String = Hello yet again, world!
scala> println(msg)Hello, world!
binding values to names
TI1220 - Introduction
Variable Declaration and Use
47
B:2.3
scala> msg = "Goodbye cruel world!"<console>:6: error: reassignment to val msg = "Goodbye cruel world!"
scala> var greeting = "Hello, world!"greeting: java.lang.String = Hello, world!
scala> greeting = "Leave me alone, world!"greeting: java.lang.String = Leave me alone, world!
changing value bound to name
TI1220 - Introduction
Immutable and Mutable Variables
48
B:2.3
scala> def max(x: Int, y: Int): Int = { if (x > y) x else y }max: (x: Int,y: Int)Int
scala> max(3, 5)res2: Int = 5
binding functions to names
TI1220 - Introduction
Function Definition and Call
49
scala> def max2(x: Int, y: Int) = if (x > y) x else y max: (x: Int,y: Int)Int
B:2.3
scala> def greet() = println("Hello, world!")greet: ()Unit
scala> greet()Hello, world!
the type of side effects
TI1220 - Introduction
Unit
50
B:2.4
$ scala hello.scala Hello, world, from a script!
TI1220 - Introduction
Scala Scripts
51
//file: hello.scalaprintln("Hello, world, from a script!")
B:2.4
$ scala helloarg.scala delftHello, delft!
with arguments
TI1220 - Introduction
Scala Scripts
52
//file: helloarg.scala// Say hello to the first argumentprintln("Hello, "+ args(0) +"!")
B:2.5
// file: printargs.scalavar i = 0while (i < args.length) { if (i != 0) print(" ") print(args(i)) i += 1}println()
imperative style
TI1220 - Introduction
Control-Flow
53
$ scala printargs.scala imperative styleimperative style
B:2.5
// file: printargsfun.scalaargs.foreach(arg => println(arg))
functional style
TI1220 - Introduction
Control-Flow
54
$ scala printargsfun.scala first class functionsfirstclassfunctions
B:2.5
for (arg <- args) println(arg)
functional style
TI1220 - Introduction
Control-Flow
55
B:3.11 TI1220 - Introduction
Functional Style
56
Functional style
• functions are values
• operations map input values to output values
• instead of changing values in place (side effects)
A balanced attitude
• Prefer vals, immutable objects, and methods without side effects.
• Use vars, mutable objects, and methods with side effects when you have a specific need and justification for them.
B:3.12
$ scala linecounts.scala linecounts.scala 22 | import scala.io.Source 0 | 55 | def widthOfLength(s: String) = s.length.toString.length 0 | 22 | if (args.length > 0) {54 | val lines = Source.fromFile(args(0)).getLines.toList37 | val longestLine = lines.reduceLeft(49 | (a, b) => if (a.length > b.length) a else b 3 | )43 | val maxWidth = widthOfLength(longestLine)23 | for (line <- lines) {50 | val numSpaces = maxWidth - widthOfLength(line)33 | val padding = " " * numSpaces47 | println(padding + line.length +" | "+ line) 3 | } 1 | } 4 | else46 | Console.err.println("Please enter filename")
TI1220 - Introduction 57
B:2.3
import scala.io.Sourceif (args.length > 0) { for (line <- Source.fromFile(args(0)).getLines) println(line.length + " | " + line)} else Console.err.println("Please enter filename")
charcounts
TI1220 - Introduction 58
$ scala countchars1.scala countchars1.scala 22 | import scala.io.Source22 | if (args.length > 0) {49 | for (line <- Source.fromFile(args(0)).getLines)37 | println(line.length + " " + line)6 | } else46 | Console.err.println("Please enter filename")
Print Lines
B:3.12
def widthOfLength(s: String) = s.length.toString.length
val lines = Source.fromFile(args(0)).getLines.toList
val longestLine = lines.reduceLeft( (a, b) => if (a.length > b.length) a else b)
val maxWidth = widthOfLength(longestLine)
charcounts
TI1220 - Introduction 59
Width of Longest Line
B:3.12
for (line <- lines) { val numSpaces = maxWidth - widthOfLength(line)
val padding = " " * numSpaces
println(padding + line.length +" | "+ line)}
charcounts
TI1220 - Introduction 60
Print Lines with Length
B:3.12
import scala.io.Source
def widthOfLength(s: String) = s.length.toString.length
if (args.length > 0) { val lines = Source.fromFile(args(0)).getLines.toList val longestLine = lines.reduceLeft( (a, b) => if (a.length > b.length) a else b ) val maxWidth = widthOfLength(longestLine) for (line <- lines) { val numSpaces = maxWidth - widthOfLength(line) val padding = " " * numSpaces println(padding + line.length +" | "+ line) }}else Console.err.println("Please enter filename")
TI1220 - Introduction 61
B:2.3 Term Rewriting
Classes and Objects
VI
62
B:4.1
class ChecksumAccumulator {}
val acc = new ChecksumAccumulatorval csa = new ChecksumAccumulator
declaration and instantiation
TI1220 - Introduction
Class: Object Blueprint
63
B:4.1
class ChecksumAccumulator { var sum = 0}
val acc = new ChecksumAccumulatorval csa = new ChecksumAccumulator
acc.sum = 3
object state
TI1220 - Introduction
Fields (Instance Variables)
64
B:4.1
class ChecksumAccumulator { var sum = 0}
val acc = new ChecksumAccumulatorval csa = new ChecksumAccumulator
acc.sum = 3
// Won’t compile, because acc is a valacc = new ChecksumAccumulator
object state
TI1220 - Introduction
Reference vs Content
65
B:4.1
class ChecksumAccumulator { private var sum = 0}
val acc = new ChecksumAccumulatoracc.sum = 5 // Won’t compile, because sum is private
object state
TI1220 - Introduction
Data Hiding
66
B:4.1
class ChecksumAccumulator { private var sum = 0 def add(b: Byte): Unit = { sum += b } def checksum(): Int = { return ~(sum & 0xFF) + 1 }}
object behavior
TI1220 - Introduction
Methods
67
B:4.1
def add(b: Byte): Unit = { b = 1 // This won’t compile, because b is a val sum += b}
parameters are vals
TI1220 - Introduction
Methods
68
B:4.1
// file: ChecksumAccumulator.scalaclass ChecksumAccumulator { private var sum = 0 def add(b: Byte) { sum += b } def checksum(): Int = ~(sum & 0xFF) + 1}
sugar
TI1220 - Introduction
Methods
69
B:4.3
import scala.collection.mutable.Mapobject ChecksumAccumulator { private val cache = Map[String, Int]() def calculate(s: String): Int = if (cache.contains(s)) cache(s) else { val acc = new ChecksumAccumulator for (c <- s) acc.add(c.toByte) val cs = acc.checksum() cache += (s -> cs) cs }}
defining ‘static’ methods
TI1220 - Introduction
Singleton Objects
70
B:4.3
ChecksumAccumulator.calculate("Every value is an object.")
methods and state
TI1220 - Introduction
Using Singleton Objects
71
B:4.4
// file: Summer.scalaimport ChecksumAccumulator.calculateobject Summer { def main(args: Array[String]) { for (arg <- args) println(arg + ": " + calculate(arg)) }}
TI1220 - Introduction
Scala Application
72
$ scalac ChecksumAccumulator.scala Summer.scala$ scala Summer of loveof: -213love: -182
Term Rewriting
Summary
VII
73
B:2.3
lessons learned
TI1220 - Introduction
Summary
Programming languages are for people
• to reason about solutions for problems
Declaration and use
• binding values to names, referring to values using names
Immutable values
• cannot be changed once created (no side effects)
Objects
• data structures with methods
74
B:2.3 TI1220 - Introduction
Exam Question
75
What happens when we execute the following code:
val greetStrings = new Array[String](3)greetStrings(0) = "Hello"greetStrings(1) = ", "greetStrings(2) = "world!\n"
(a) Error: greetStrings is an immutable variable that cannot be assigned to
(b) Error: Array is a immutable data structure that cannot be assigned to
(c) No error: greetStrings is a mutable variable that can be assigned to
(d) No error: Array is a mutable data structure that can be assigned to
B:2.3
getting started with Scala
TI1220 - Introduction 76
Exercises Week 1
Set up your Scala environment
• download and install Scala interpreter and compiler
Scala basics
• evaluating expressions
• defining functions
• running scripts
• making applications
• hand in on paper to student assistants!
required reading
TI1220 - Introduction
Literature
Programming in Scala
• Chapter 1: A Scalable Language
• Chapter 2: First Steps in Scala
• Chapter 3: Next Steps in Scala
• Chapter 4: Classes and Objects
• Chapter 5: Basic Types and Operations
77
http://researchr.org/bibliography/ti1220
TI1220 - Introduction
Literature
78
B:2.3
coming next
TI1220 - Introduction
Outlook
Functional programming
• Lecture 2: Functional Objects
• Chapters 6, 7
• Lecture 3: Functions & Closures
• Chapters 8, 9
• Lecture 4: List Programming
• Chapters 15, 16, 17
Lab Week 1
• Getting started with Scala
79
Term Rewriting
Notes
IX
80
B:2.3
copyrights
TI1220 - Introduction
Pictures
Slide 1: Kinetica by Paul Downey, Some rights reservedSlide 2: McPhillips’ Map of the City of Winnipeg by Manitoba Historical Maps, some rights reservedSlide 4: The Blue Hour by Andreas Wonisch, Some rights reservedSlide 5: by Eelco Visser, Some rights reservedSlide 8: I’m listening by Melvin Gaal, some rights reservedSlide 10: Bombe detail by Garret Coakley, Some rights reservedSlide 13: Alan Turing by oalfonso, some rights reservedSlide 14: History of Programming Languages by O’Reilly Slide: ABZ-DNA by dulhunk, some rights reservedSlide 32, 34: Envelopes by benchilada, some rights reservedSlide 33: Report card by Carosaurus, some rights reservedSlide 36: paper coffee cup by Shutterstock, some rights reserved
81
B:2.3
copyrights
TI1220 - Introduction
Pictures
Slide 40: Dinner at Roussillon (Martin Odersky) by Miles Sabin, some rights reservedSlide 41: Portrait: Alex Payne by Dave Fayram, some rights reservedSlide 43: “Plumbing Nightmare” by Natalie Wilkie, some rights reservedSlide 72: HIV: The Moleskine Summary by Niels OlsonSlide 75, 76: remember to thank all the books you haven’t read over the past three years by Natalia Osiatynska, Some rights reservedSlide 7, 73: Stupid Exam by Remi Carreiro, Some rights reservedSlide 74: Practice makes perfect by Simon ProbertSlide 10: Bombe detail by Garret Coakley, Some rights reservedSlide 29: 1.12.2011 <recipes> 362/365 by Phil Roeder, Some rights reserved
Slide 22,23: China Tibet Himalaya by Bernhard Goldbach, Some rights reservedSlide 17: Thinking at Hell’s gate by innoxiuss, Some rights reserved
82