CSC324 { Introductionafsaneh/csc324w13/intro.pdf · CSC324 { Introduction Afsaneh ... nd out unused...
Transcript of CSC324 { Introductionafsaneh/csc324w13/intro.pdf · CSC324 { Introduction Afsaneh ... nd out unused...
CSC324 – Introduction
Afsaneh Fazly1
January 7, 2013
1Thanks to A.Tafliovich, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton
1
A few administrative notes
You must carefully read the course information sheet, posted onthe course webpage:
www.cs.toronto.edu/~afsaneh/csc324.html
• Communication media: course forum.
• Readings: recommended textbooks, lecture and tutorial notes,and online material.
• Course grading scheme (assignments, midterm, final exam).
• Policy on plagiarism, lateness, and emergencies.
• Course prerequisites: please drop if you do not have them.
• If you intend to take the course, attend the first two weeks.
• Drop on ROSI as soon as you decide you do not want to takethe course, so students on waiting list can enroll.
2
Resources
Office Hours:Monday 3–4pm BA 4261Wednesday 1–2pm BA 4261
please drop by even if it is just to introduce yourself!
DCS Help Centre: a study space with help from experienced TAs.web.cs.toronto.edu/program/ugrad/ug_helpcentre.htm
UofT Writing Centre:http://www.writing.utoronto.ca
3
Why are you here?
• Because I am keen to learn about all the cool conceptsunderlying the design and implementation of differentprogramming languages, so I can · · ·
5
Course Objectives
• choose the right language for a given task.
• design a new language.
• learn new languages quickly.
• find out unused features of languages I already know.
• increase my vocabulary of programming constructs.
• be a better programmer.
And of course:
• find a good job!
6
Course Objectives
Computer programming is an art · · · Programmers whosubconsciously view themselves as artists will enjoy whatthey do and will do it better.
— Donald E. Knuth
7
What is Covered?
Principles of programming languages:
• issues in designining data type systems.
• issues in disigning procedures.
Programming paradigms:
• functional programming (exemplified by Scheme and ML)
• logic programming (exemplified by Prolog)
8
Today
Course title: Programming languages.
Levels of programming languages.
Translation.
Programming paradigms.
A few historical notes.
9
On Programming
The main idea is to treat a program as a piece ofliterature, addressed to human beings rather than to acomputer.
— Donald E. Knuth
10
On Language (as a general concept)
a tool for communication.
a formal symbolic system that (arbitrarily) associates a collectionof forms with their meanings.
• Components:• specification of the forms, a.k.a., Syntax.• specification of the meanings, a.k.a., Semantics.
• Kinds:• natural languages (e.g., English) for human–human
communication.• artificial languages, including programming languages (e.g.,
Python) for human–machine communication.
11
On Programming Languages
“an artificial language designed to communicate instructions to amachine.” [Wikipedia]
“a set of conventions for communicating an algorithm.” [Horowitz]
• Components• formal syntax.• formal semantics.
(we will see more of these later)
• Target of communication• computer/machine.• other programmers.
12
On Programming Languages
Let us change our traditional attitude to the constructionof programs. Instead of imagining that our main task isto instruct a computer what to to, let us concentraterather on explaining to human beings what we want acomputer to do.
— Donald E. Knuth
13
Levels of Programming Languages
• Machine languages.
• Assembly languages.
• High-level languages.
14
Levels: Machine Languages
move content of mem location 08125 to register 7
add content of mem location 08125 to register 7
shift content of register 3 left one bit
skip next instruction if register 1 is zero
15
Levels: Machine Languages
move content of mem location 08125 to register 7
add content of mem location 08125 to register 7
shift content of register 3 left one bit
skip next instruction if register 1 is zero
• Instructions are encoded as numbers.
• No variables; operands are memory addresses or registernumbers.
• Programming requires deep understanding of the machinearchitecture.
• Programs are not portable because instructions and theirencoding are machine-specific.
• Programs are extremely hard to write, debug, and read.
16
Levels: Assembly Languages
1 sub 3 5 ;sub 3 from reg 5
2 skp 5 ;skip if reg 5 is 0
3 jmp 10 ;jump to line 10
4 <some instrns>
10 <other instrns>
17
Levels: Assembly Languages
1 sub 3 5 ;sub 3 from reg 5
2 skp 5 ;skip if reg 5 is 0
3 jmp 10 ;jump to line 10
4 <some instrns>
10 <other instrns>
• Operations and operands have symbolic names.
• Can use macros as shorthand for common sequences of code.
• An assembler translates the program into machine code.
• Still machine dependent.
• Almost as hard to write as machine code.
18
Levels: High-level Languages
if (x == 3) 1 sub 3 5 ;sub 3 from reg 5
<some instrns> 2 skp 5 ;skip if reg 5 is 0
else 3 jmp 10 ;jump to line 10
<other instrns> 4 <some instrns>
10 <other instrns>
19
Levels: High-level Languages
if (x == 3) 1 sub 3 5 ;sub 3 from reg 5
<some instrns> 2 skp 5 ;skip if reg 5 is 0
else 3 jmp 10 ;jump to line 10
<other instrns> 4 <some instrns>
10 <other instrns>
• Include higher level constructs.
• Programs are much easier to write, debug, and read.
• Programs are now machine independent.
• Programs may still be “language-implementation dependent”.
• Usually support type checking and other checks that helpdetect bugs (we will see more of this later).
• Examples: Fortran, C, Scheme, Java, Python, . . .
20
Quiz (1)
How many different programming languages have been developedin the past 60 years?
• thousands?
according to Wikipeida
• hundreds?
223 according to TIOBE index [www.tiobe.com]
Why the big difference?
• Depends on the definition of programming language, e.g.,TIOBE does not include XML and HTML.
21
Quiz (1)
How many different programming languages have been developedin the past 60 years?
• thousands? according to Wikipeida
• hundreds? 223 according to TIOBE index [www.tiobe.com]
Why the big difference?
• Depends on the definition of programming language, e.g.,TIOBE does not include XML and HTML.
21
Quiz (1)
How many different programming languages have been developedin the past 60 years?
• thousands? according to Wikipeida
• hundreds? 223 according to TIOBE index [www.tiobe.com]
Why the big difference?
• Depends on the definition of programming language, e.g.,TIOBE does not include XML and HTML.
21
Quiz (1)
How many different programming languages have been developedin the past 60 years?
• thousands? according to Wikipeida
• hundreds? 223 according to TIOBE index [www.tiobe.com]
Why the big difference?
• Depends on the definition of programming language, e.g.,TIOBE does not include XML and HTML.
21
Quiz (2)
Which languages are the “most popular” today?
• C, Java, Objective-C, C++, C#[ based on TIOBE ratings, calculated by counting hits of the most
popular search engines ]
• top 10 in list are imperative, why?
22
Quiz (2)
Which languages are the “most popular” today?
• C, Java, Objective-C, C++, C#[ based on TIOBE ratings, calculated by counting hits of the most
popular search engines ]
• top 10 in list are imperative, why?
22
Summary
Want computer programs to:
• be performed by a machine.
• be understandable (i.e., easy to read and write) by humanprogrammers.
Need to:
• write code in a high-level language.
• write another program to translate high-level code tomachine instructions.
23
Summary
Want computer programs to:
• be performed by a machine.
• be understandable (i.e., easy to read and write) by humanprogrammers.
Need to:
• write code in a high-level language.
• write another program to translate high-level code tomachine instructions.
23
Translation
The process of converting a program written in a high-levellanguage into machine language. Two general methods:
source code
target code
compiler
input output
Compilation
source code
input output
Interpretation
interpreter
• Compilation: the whole program is translated beforeexecution.
• Interpretation: translate and execute, one statement at a time.
24
Translation
Compilation:
• Can execute translated program many times because theentire translation is produced.
• Program execution is much faster because the translator canperform optimization.
• Harder to provide useful source-level feedback whendebugging because executing the target code.
Interpretation:
• Must re-translate for every execution.
• Program execution is 10 to 100 times slower!.
• Easier to provide source-level feedback when debugging.
• Flexibility supports rapid prototyping.
25
Translation
Compilation:
• Can execute translated program many times because theentire translation is produced.
• Program execution is much faster because the translator canperform optimization.
• Harder to provide useful source-level feedback whendebugging because executing the target code.
Interpretation:
• Must re-translate for every execution.
• Program execution is 10 to 100 times slower!.
• Easier to provide source-level feedback when debugging.
• Flexibility supports rapid prototyping.
25
Pseudo-compilation: Hybird
source code
compiler
intermed. code
interpreter outputinput
• A compiler translates the whole program before execution, butonly into intermediate code.
• An interpreter translates and executes the intermediate codeone statement at a time.
• The intermediate code can be executed on any machine thathas an interpreter for the intermediate code.
Java uses this hybrid strategy: Intermediate code called bytecode.
26
Determinants of Programming Language (PL) Design
Computer architecture: the von Neumann architecture.
Application/domain for which a language is originally designed.
27
The von Neumann Architecture
the fetch-execute cycle
[image from faculty.cs.wwu.edu]
28
Application Affecting PL Design
Historical examples:
• Fortran: first high-level PL, invented for scientific andmathematical computation in the 1950s.
• Lisp: invented to provide features for processing symbolic datain lists, needed for research in artificial intelligence.
• COBOL: designed for business computing in the 1950s.
• C: originally designed in the 1960s for developing systemsoftware (e.g., the Unix OS).
Note: PL designs have in turn affected practices in programdevelopment.
29
Programming Paradigms
Imperative (e.g., Fortran, Algol, C, Pascal)
• program statements are commands.Example: “Add 17 to x”
• key operations are assignment and iteration.
• closely fit the von Neumann architecture.
Functional (e.g., Lisp, Scheme, ML, Haskell)
• program statements are noun phrases describing values ofexpressions.Example: “reverse of a list is last element followed by thereverse of the rest of the list”
• key operations are expression evaluation by applying afunction.
30
Programming Paradigms
Logic-based (e.g., Prolog)
• program statements describe facts and rules.Example: “fact: Doug is Tom’s father.
rule: If x is y’s father and y is z’s father, then x is z’s grandfather.”
• key operation is unification.
Object-oriented (e.g., Simula, C++, Java, Python)
• program statements describe communication amongobjects.
• key operations are message passing and inheritance.
• can be imperative (e.g., Java) or functional (e.g., Scala).
31
Declarative vs. Imperative Programming
Declarative programming:
• consist of declarations, specifying what to compute.
• variables are logical variables, not nicknames for memorylocations or machine registers.
• logic-based (and to some extent functional) languages supportthis programming style.
Imperative programming:
• consist of assignments and control-flow statements, specifyinghow to perform a computation.
• most programming today is imperative.
Note: many “hows” can be subsumed by a single “what”.
32
Why are Imperative Languages more Popular?
• Inertia: lots of code out there to maintain.
• Lack of competent programmers: a lot of programmersfinished their education a long time ago.
• Lack of good Programming Environments: there’s beenprogress, largely due to open-source collaboration.
• Efficiency: the biggest problem, but there’s been progress.
Things are changing:
• Twitter and Linkedin use Scala: a hybrid of functional andobject-oriented language.To learn more, see http://www.scala-lang.org/
33
What Makes a Good Language
Language should be easy to learn:
• Simple syntax.
• Simple and appropriate semantics.
• Small number of primitive concepts.• Orthogonality: concepts combine systematically, with no
exceptions.• No ambiguity: Meaning of a construct does not depend on
context.• Low synonymy: Not many different ways to perform a certain
task.
Programs should be easy to read and write:
• Code should reflect intuition of the programmer.
• Programs should be portable.
... and more technical principles that we will see later.
34
What Makes a Good Language
Language should be easy to learn:
• Simple syntax.
• Simple and appropriate semantics.
• Small number of primitive concepts.• Orthogonality: concepts combine systematically, with no
exceptions.• No ambiguity: Meaning of a construct does not depend on
context.• Low synonymy: Not many different ways to perform a certain
task.
Programs should be easy to read and write:
• Code should reflect intuition of the programmer.
• Programs should be portable.
... and more technical principles that we will see later.
34
What Makes a Good Language
Language should be easy to learn:
• Simple syntax.
• Simple and appropriate semantics.• Small number of primitive concepts.• Orthogonality: concepts combine systematically, with no
exceptions.• No ambiguity: Meaning of a construct does not depend on
context.• Low synonymy: Not many different ways to perform a certain
task.
Programs should be easy to read and write:
• Code should reflect intuition of the programmer.
• Programs should be portable.
... and more technical principles that we will see later.
34
What Makes a Good Language
Language should be easy to learn:
• Simple syntax.
• Simple and appropriate semantics.• Small number of primitive concepts.• Orthogonality: concepts combine systematically, with no
exceptions.• No ambiguity: Meaning of a construct does not depend on
context.• Low synonymy: Not many different ways to perform a certain
task.
Programs should be easy to read and write:
• Code should reflect intuition of the programmer.
• Programs should be portable.
... and more technical principles that we will see later.
34
Quiz (3)
Which factors contribute to the popularity of a programminglanguage?
• how easy it is to read and write programs in the language.
• how efficiently programs written in the language run.
• luck!
35