40
Programming and Paradigms Course overview Edward (Ned) Blurock Lecture: Programming Paradigm CIS: Edward Blruock

Programming Languages

Embed Size (px)

Citation preview

Page 1: Programming Languages

Programming and Paradigms

Course overviewEdward (Ned) Blurock

Lecture: Programming Paradigm CIS: Edward Blruock

Page 2: Programming Languages

LANGUAGESWhy so many programming Languages?

Lecture: Programming Paradigm CIS: Edward Blruock

Page 3: Programming Languages

Programming Language

Lecture: Programming Paradigm CIS: Edward Blruock

The process of developing and implementing various sets of instructions to enable a computer to do a certain task.

The language used to program computers is not understood by an untrained eye. Computer programming continues to be a necessary process as the Internet continues to expand.

Read more: http://www.businessdictionary.com/definition/computer-programming.html#ixzz3jLVEiKeK

Page 4: Programming Languages

Top Ten Programming Languages

Lecture: Programming Paradigm CIS: Edward Blruock

Page 6: Programming Languages

Wierdest

Lecture: Programming Paradigm CIS: Edward Blruock

http://tutorialzine.com/2013/12/the-10-weirdest-programming-languages/

Page 7: Programming Languages

High-level programming language

Lecture: Programming Paradigm CIS: Edward Blruock

"High-level language" refers to the higher level of abstraction from machine language.

Abstraction penalty:

Execution modes• Interpreted• Compiled

• Machine code generation: assembly language• Intermediate code: byte code

• Source to Source: trans-compiled

https://en.wikipedia.org/wiki/High-level_programming_language

Page 8: Programming Languages

AbstractionPenalty

Lecture: Programming Paradigm CIS: Edward Blruock

While high-level languages are intended to make complex programming simpler, low-level languages often produce more efficient code. Abstraction penalty is the border that prevents high-level programming techniques from being applied in situations where computational resources are limited. High-level programming exhibits features like more generic data structures, run-time interpretation, and intermediate code files; which often result in slower execution speed, higher memory consumption, and larger binary program size

Page 9: Programming Languages

Abstraction

Lecture: Programming Paradigm CIS: Edward Blruock

Abstraction can apply to control or to data: Control abstraction is the abstraction of actions while Data abstraction is that of data structures.

Control abstraction involves the use of subprograms and related concepts control flows

Data abstraction allows handling data bits in meaningful ways.

For example, it is the basic motivation behind datatype.One can view the notion of an object as a way to combine abstractions of data and code.

The same abstract definition can be used as a common interface for a family of objects with different implementations and behaviors but which share the same meaning.

The inheritance mechanism in object-oriented programming can be used to define an abstract class as the common interface.

Page 10: Programming Languages

History

Lecture: Programming Paradigm CIS: Edward Blruock

http://www.paulgraham.com/fix.html

What languages fix

A view of the development of languages:

The NEXT language fixes short-comings

of the PREVIOUS language

Page 11: Programming Languages

Power of a Language

Lecture: Programming Paradigm CIS: Edward Blruock

All languages are equally powerful in the sense of being Turing equivalent, but that's not the sense of the word programmers care about. (No one wants to program a Turing machine.)

The kind of power programmers care about may not be formally definable,

but one way to explain it would be to say that it refers to features you could only get in the less powerful language by writing an interpreter for the more powerful language in it.

If language A has an operator for removing spaces from strings and language B doesn't, that probably doesn't make A more

powerful, because you can probably write a subroutine to do it in B. But if A supports, say, recursion, and B doesn't,

that's not likely to be something you can fix by writing library functions.

Paul Graham: http://www.paulgraham.com/avg.html

Page 12: Programming Languages

Blub Paradox

Lecture: Programming Paradigm CIS: Edward Blruock

Programmers get very attached to their favorite languages,

and I don't want to hurt anyone's feelings, so to explain this point I'm going to use a hypothetical language called Blub.

Blub falls right in the middle of the abstractness continuum.

It is not the most powerful language, but it is more powerful than Cobol or machine

language.

Paul Graham: http://www.paulgraham.com/avg.html

Page 13: Programming Languages

The Blub Paradox

Lecture: Programming Paradigm CIS: Edward Blruock

As long as our hypothetical Blub programmer is looking down the power continuum, he knows he's looking down. Languages less powerful than Blub are obviously less powerful,

because they're missing some feature he's used to.

But when our hypothetical Blub programmer looks in the other direction, up the power continuum,

he doesn't realize he's looking up. What he sees are merely weird languages.

He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well.

Blub is good enough for him, because he thinks in Blub.

Paul Graham: http://www.paulgraham.com/avg.html

Page 14: Programming Languages

Blub Paradox

Lecture: Programming Paradigm CIS: Edward Blruock

You can't trust the opinions of the others, because of the Blub paradox:

they're satisfied with whatever language they happen to use, because it dictates the way they think about programs.

I know this from my own experience, as a high school kid writing programs in Basic.

That language didn't even support recursion.

It's hard to imagine writing programs without using recursion, but I didn't miss it at the time. I thought in Basic. And I was a whiz at it. Master of all I surveyed.

Paul Graham: http://www.paulgraham.com/avg.html

Page 15: Programming Languages

A reason to take this course

Lecture: Programming Paradigm CIS: Edward Blruock

Learn at least one new [programming] language every year. Different languages solve the same problems in different ways.

By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut.

The Pragmatic Programmer: From Journeyman to MasterAndrew Hunthttp://www.amazon.com/The-Pragmatic-Programmer-Journeyman-Master/dp/020161622X

Page 16: Programming Languages

PS — Denotational Semantics© O. Nierstrasz 8.16

Defining Programming Languages

There are three main characteristics of programming languages:

1. Syntax: What is the appearance and structure of its programs?

2. Semantics: What is the meaning of programs?The static semantics tells us which (syntactically valid) programs are semantically valid (i.e., which are type correct) and the dynamic semantics tells us how to interpret the meaning of valid programs.

3. Pragmatics: What is the usability of the language? How easy is it to implement? What kinds of applications does it suit?

Page 17: Programming Languages

PS — Denotational Semantics

Programming Languages

© O. Nierstrasz 8.17

1. Syntactic Abstraction Mechanism: to reduce repetitive "boilerplate" code that cannot be abstracted from using another language's built-in abstraction mechanisms.

2. Thought shaper: to induce a paradigm shift in how one should structure software (changing the "path of least resistance").

3. Simplifier: to boil down an existing paradigm to just its essential parts, often to increase understanding and insight.

4. Law Enforcer: to enforce important properties or invariants, possibly to make it easier to infer more useful properties from programs.

Tom Van Cutsam: http://soft.vub.ac.be/~tvcutsem/whypls.html

Page 18: Programming Languages

syntactic abstraction mechanism

Lecture: Programming Paradigm CIS: Edward Blruock

Purpose: Lightweight syntax that lets programmers do the same things as before, but with less code

Example: Cprovides multiple looping constructs, multiple conditional constructs,multiple constructs for incrementing/updating the value of a variable.

loop through the elements of arraysincrement or decrement the values of variablesperform multiway conditionals based on numeric or character values.

Programming language designers provide special purpose syntactic constructs that handle the most common patterns.

Page 19: Programming Languages

The opposite: no syntactic abstraction

Lecture: Programming Paradigm CIS: Edward Blruock

Language: Chicken http://torso.me/chickenhttps://isotropic.org/papers/chicken.pdf

https://www.youtube.com/watch?v=yL_-1d9OSdk

Page 20: Programming Languages

Language: Chickens

Lecture: Programming Paradigm CIS: Edward Blruock

1. A chicken program consists of the tokens "chicken", " " and "\n”.2. Every line has a number of chickens separated by spaces. 3. The number of chickens corresponds to an opcode. 4. Trailing newlines are significant, as an empty line will produce a "0" opcode.5. Instructions are loaded onto the stack and executed there directly, which allows for

injecting arbitrary code and executing it through a jump. 6. Self-modifying code is also possible because the program stack is not bounded.7. The user is able to supply an input value before executing the program. This input is

stored in one of the two registers.

The operations:http://esolangs.org/wiki/Chicken

Page 21: Programming Languages

thought shaper

Lecture: Programming Paradigm CIS: Edward Blruock

The goal of a thought shaper language is to change the way a programmer thinks about structuring his or her program.

The basic building blocks provided by a programming language, as well as the ways in which they can (or cannot) be combined, will tend to lead programmers down a "path of least resistance",

for some unit of resistance.

For example, an imperative programming style is definitely the path of least resistance in C. It's possible to write functional C programs, but as C does not make it the path of least resistance, most C programs will not be functional.

Page 22: Programming Languages

Thought Shaper

Lecture: Programming Paradigm CIS: Edward Blruock

Experienced programmers carry quite a number of patterns around in their heads.

They may have learned these patterns from books or by hard won experience.

The patterns enable a fluid style of programming where attention can be maintained on the unknown parts of the task at hand and work feels productive.

Programmers tend to like programming languages that have good support for the patterns they use.

Programmers don't like languages that disallow or inconvenience the patterns they use.

They are also rarely impressed by language features that support patterns they don't know or use.

http://c2.com/cgi/wiki?BlubParadox

Page 23: Programming Languages

Thought Shaper

Lecture: Programming Paradigm CIS: Edward Blruock

(pure) Functional Programming

Functional programming languages, by the way, are a good example of thought shaper languages.

By taking away assignment from the programmer's basic toolbox,

the language really forces programmers coming from an imperative language to change their coding habits.

Page 24: Programming Languages

Thought Shaper

Lecture: Programming Paradigm CIS: Edward Blruock

linguistic relativity

holds that cognitive processes, such as thought and experience, may be influenced by the categories and patterns of the language a person speaks.

From the field of linquistics

https://en.wikipedia.org/wiki/Linguistic_relativity

the structure of a language affects the ways in which its respective speakers conceptualize their world, i.e. their world view, or otherwise influences their cognitive processes.

Page 25: Programming Languages

a simplifier

Lecture: Programming Paradigm CIS: Edward Blruock

A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all.

Bentley, Jon, The most beautiful code I never wrote, Beautiful Code, O'Reilly Media, Inc.,2007

Page 26: Programming Languages

law enforcer

Lecture: Programming Paradigm CIS: Edward Blruock

http://www.cs.cornell.edu/courses/cs1130/2012sp/1130selfpaced/module1/module1part4/strongtyping.html

One property that is sometimes looked for in a programming language is safety

Safety: Any attempt to misinterpret data is caught at compile time or generates a well-specified error at runtime.

definition used in the year 2000 by an ad hoc committee that recommended that the Advanced Placement (AP) test in programming be based on the programming language Java

Page 27: Programming Languages

Strong Typing

Lecture: Programming Paradigm CIS: Edward Blruock

Some errors arise from lack of understanding; others are logical errors caused by inadequate thinking and design; and some are simply typos.

In all cases, finding errors early, at compile time, can save immense amounts of time.

Safety and strong typing make possible the early detection of

many errors.

A strongly typed language has one simple feature: knowledge of a value’s type at runtime.

Page 28: Programming Languages

Strong vs. Weak Typing

Lecture: Programming Paradigm CIS: Edward Blruock

String foo = "Hello, world!";Object obj = foo;

String bar = (String) obj;Date baz = (Date) obj;

The above example will run perfectly fine until the last line, which will raise a ClassCastException. This is because Java is indeed strongly typed. Even though the obj variable is of the type Object, its value is still a String. Casting it to a String works fine, but trying to cast it to a Date fails.

char* foo = "Hello, world!";int x = foo * 10;printf("%i\n", x);

The above block is valid C code, but the output is not well-defined.…. This is because C does not store anything in memory other than the raw values—it would be impossible at runtime to know that foo was a char* and x was an int.

Strong

Weak

Page 29: Programming Languages

Program Verification:Bugs

Lecture: Programming Paradigm CIS: Edward Blruock

Here are some famous ones:

ESA Ariane 5 Flight 501 self-destruction 40 seconds after takeoff (June 4, 1996). A conversion from 64-bit floating point to 16 bit integer with a value larger than possible with Arian 4. The overflow caused a hardware trap

The Pentium bugIncorrect floating-point division.Cost Intel ~ $400,000,000

The 2003 North America blackout was triggered by a local outage that went undetetected. A race condition in General Electric’s monitoring software prevented an alarm

Page 30: Programming Languages

Program Verification:Testing

Lecture: Programming Paradigm CIS: Edward Blruock

When you look at a big commercial software company like Microsoft, there's actually as much testing that goes in as development. We have as many testers as we have developers. Testers basically test all the time, and developers basically are involved in the testing process about half the time…

Bill Gates:

The test cases are unbelievably expensive; in fact, there's more lines of code in the test harness than there is in the program itself. Often that's a ratio of about three to one

Page 31: Programming Languages

Program Verification

Lecture: Programming Paradigm CIS: Edward Blruock

Formal = based on rigorous mathematical logic concepts.

Once we formally specify what we expect from the program, we can try to prove that the program satisfies the specification.

Page 32: Programming Languages

Styles of formal semantics

Lecture: Programming Paradigm CIS: Edward Blruock

Operational.Meanings for program phrases defined in terms of the stepsof computation they can take during program execution.

Axiomatic.Meanings for program phrases defined indirectly via the axiomsand rules of some logic of program properties.

Denotational.Concerned with giving mathematical models of programminglanguages. Meanings for program phrases defined abstractlyas elements of some suitable mathematical structure.

Page 33: Programming Languages

Denotational Semantics Example

Lecture: Programming Paradigm CIS: Edward Blruock

<expr> → number| ( <expr> )| <expri>+

<expr>| <expri>−

<expr>| <expri> ∗

<expr>|

<expr>/<expr>

[[n1 + n2]] = rep(n1) + rep(n2)

[[e1+e2]] = [[e1]] + [[e2]]

Page 34: Programming Languages

Denotational Semantics Example

Lecture: Programming Paradigm CIS: Edward Blruock

Page 35: Programming Languages

Operational Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

A category of formal programming language semanticsin which certain desired properties of a program, such as correctness, safety or security,

are verified by constructing proofs from

logical statements about its execution, rather than by attaching mathematical meanings to its terms

(denotational semantics).

The operational semantics for a programming language describes how a valid program is interpreted as sequences of computational steps

Page 36: Programming Languages

Operational Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

Two categories:

structural operational semantics (or small-step semantics) formally describe how the individual steps of a

computation take place in a computer-based system. natural semantics

(or big-step semantics) describe how the overall results of the executions are

obtained.

Page 37: Programming Languages

Operational Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

The process:1. Identify a virtual machine (an idealized computer)2. Build a translator (translates source code to the machine code

of an idealized computer)3. Build a simulator for the idealized computerOperational semantics is sometimes called translational semantics, if an existing PL is used in place of the virtual machine

http://courses.cs.vt.edu/~cs3304/Spring00/notes/Chapter-3b/index.htm

Page 38: Programming Languages

Axiomatic Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

• An assertion before a statement is called a precondition• An assertion following a statement is a postcondition

Based on formal logic (first order predicate calculus)Original purpose: formal program verification

Approach: Define axioms or inference rules for each statement type in the language

Inference rule allows one to transform expressions to other expressions

assertionsstate the relationships and constraints among variables that are true at a specific point in execution

Page 39: Programming Languages

Axiomatic Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

Logic Programming (declarative) view of programTranslation to logical statements means you can construct a proof

For example:Can verify the correctness of the program automatically through automatic theorem provers

If the precondition on the first statement is the same as the program spec, the program is correct

Program proof process:The postcondition for the whole program is the desired results

Work back through the program to the first statement

Page 40: Programming Languages

Denotational Semantics

Lecture: Programming Paradigm CIS: Edward Blruock

denotational semantics (initially known as mathematical semantics or Scott–Strachey semantics) is an approach of formalizing the meanings of programming languages by constructing mathematical objects (called denotations) that describe the meanings of expressions from the languages. Other approaches to providing formal semantics of programming languages include axiomatic semantics and operational semantics.

https://en.wikipedia.org/wiki/Denotational_semantics

With formal semantics we give programs meaning by mapping them into some abstract but precise domain of objects. Using denotational semantics, we provide meaning in terms of mathematical objects, such as integers, truth values, tuples of values, and functions. For this reason, denotational semantics was originally called mathematical semantics.

http://homepage.cs.uiowa.edu/~slonnegr/plf/Book/Chapter9.pdf