ch01-F13x

hurriedtinkleΤεχνίτη Νοημοσύνη και Ρομποτική

15 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

78 εμφανίσεις

Overview

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Chapter 1

Overview


A good programming language is a conceptual
universe for thinking about programming.













A. Perlis


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

1.1 Principles

1.2 Paradigms

1.3 Special Topics

1.4 A Brief History

1.5 On Language Design


1.5.1 Design Constraints


1.5.2 Outcomes and Goals

1.6 Compilers, Interpreters, and Virtual Machines

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Programming languages have several
properties, including


Syntax


Names & types


Semantics


Language designers define these properties


Language users must understand them

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Syntax describes


How language symbols are combined to create legal
statements


How the statements can be combined into programs


Syntax defines the
form

of a correct program


Programmers: how to write a correct program


Translators: how to recognize a correct program


Syntax
does not
say anything about what the
program means



Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Various kinds of entities in a program have
names:


variables, functions, parameters, classes,
objects, …


Named entities are bound in a running
program to:


Scope


Visibility


Type


Lifetime


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

A
type

is a collection of values and a
collection of operations on those values.


Simple types: numbers, characters, booleans, …


Structured types: Strings, lists, trees, hash tables,


A language’s
type system

can help to:


Determine legal operations


Detect type errors

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

A program’s meaning is called its
semantics
.

In studying semantics, we ask questions like:


When a program is running, what happens to the
values of the variables?


What does each statement mean?


What underlying model governs the run
-
time
behavior of a function call?


How are objects allocated to memory at run
-
time?


Run
-
time stack, heap

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


A programming
paradigm

is a pattern or
framework for problem
-
solving


Languages typically identify with one
paradigm, but may have aspects of several


Common paradigms can be grouped together
as either primarily
imperative

or primarily
declarative

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Imperative languages:


Pure Imperative (von Neumann languages); e.g., C,
Pascal, early versions of Fortran & Ada, …


Object
-
oriented; e.g., C++, Java, Smalltalk


Most of what we’ll study focuses on these
languages


Declarative languages


Functional


Lisp, Scheme, ML, Haskell, …


Logic


Prolog
, SQL, …

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Follows the classic von Neumann
-
Eckert
model:


Program and data are indistinguishable in memory


Program = a sequence of commands


Programs work by changing values of memory
variables


Large programs use
procedural abstraction

as
a way to organize individual imperative
statements.


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


OO languages share many characteristics with
the pure imperative languages


Programs consist of a collection of objects
that communicate via “messages” that modify
object state


Messages are imperative


OO languages are characterized by


Encapsulation


Inheritance


Polymorphism


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Functional programming models a computation
as a function that maps inputs to outputs.


Input = domain


Output = range

Functional languages are characterized by:


Functional composition


Recursion

Example functional languages:


Lisp, Scheme, ML, Haskell, …

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Logic programming declares
what

outcome
the program should accomplish, rather
than
how

it should be accomplished.

Based on predicate logic

Rule
-
based

When studying logic programming we see:


Programs as sets of constraints on a problem


Find values that satisfy all constraints

Example logic programming languages:
Prolog

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Event handling


E.g., GUIs, home security systems, monitoring
systems of various kinds


Concurrency


e.g., Client
-
server programs, rendering (in
computer graphics)


Correctness


Can we prove that a program does what it is
supposed to do under all circumstances?

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


How and when did programming languages
evolve?


What communities have developed and
used them?


Artificial Intelligence


Computer Science Education


Science and Engineering


Information Systems


Systems and Networks


World Wide Web

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

1.5.1


Design Constraints


Computer architecture


Technical setting (domain of applications)


Standards


Legacy systems

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Key characteristics:


Simplicity, readability and writability


Clarity about binding


Reliability


Support


Abstraction


Orthogonality


Efficient implementation

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Early languages focused on efficiency


Today, programs that are easy to read and
easy to write are most likely to be successful


Promoted by simplicity

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Easy to learn


Don’t include too many features


Don’t make it too complex


Simple conceptual model of semantics.


Uniformity:

Similar syntax => similar
semantics.


Lexical and syntactic conventions:



descriptive identifier names, blocking of
compound statements

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


A language element is bound to a property at
the time that property is defined for it.


So a
binding

is the association between an
object and a property of that object; the binding
time should be clearly understood.


Examples:


variable and type: program writing time
or

execution
time?


variable and value: program writing time
and
execution
time


language operator to machine language instruction: ??

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Major binding times


Language definition time


Language implementation time


Program writing time


Compile time


Load time


Execution time


In general,


Early binding takes place at or before compile
-
time


Late binding takes place at load time or run time


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

A language is
reliable

if:


Program behavior is the same on different
platforms


E.g., early versions of Fortran weren’t


Type errors are detected


E.g., C
vs

Haskell


Good exception
-
handling facilities


E.g., C
vs

Java


Memory leaks are prevented


E.g., C/C++
vs

Java

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Good texts and tutorials


Wide community of users


Integrated with development environments
(IDEs)


Accessible compiler/interpreters (public
domain)

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Data


Programmer
-
defined types/classes


Class libraries


Procedural


Programmer
-
defined functions


Standard function libraries


Supports code reuse; simplifies the
programming process.

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


A language is
orthogonal

if it is built on a
small,
mutually independent
set of primitive
operations.


You can use one feature without worrying about
how it affects others


Rules don’t have exceptions;


C
ontext

doesn’t affect the behavior of a language
feature (e.g., a reserved word doesn’t have
different meanings based on where it’s used).


Things that are syntactically similar have similar
meaning.



Copyright © 2006 The McGraw
-
Hill Companies, Inc.



Non
-
orthogonal examples (rule
exceptions):


In C, a function can return a struct, but not an
array. Orthogonality => return value can be
from any type.


Most imperative languages don’t allow function
definitions to be passed as arguments


Orthogonal languages are in general
easier to read and write


Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Embedded systems


Real
-
time responsiveness (e.g., navigation)


Failures of early Ada implementations


real time?


Corporate database applications


Efficient search and updating


Ability to implement the language efficiently


Characteristics of Lisp didn’t match the
architectures of early computers


Characteristics of
Algol

made it difficult to build
efficient translators.


Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Programming language principles


Grammars, syntax, names & types, semantics


What makes a language successful?


Reliable, readable, writeable


Supported by simplicity,
orthogonality
, efficiency, …


Paradigms


Pure imperative, object
-
oriented, functional, logic

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Compiler


produces machine code

Interpreter


executes instructions directly


Example compiled languages:


Fortran, Cobol, C, C++


Example interpreted languages:


Scheme, Haskell, Python


Hybrid compilation/interpretation: Java


The Java Virtual Machine (JVM)


Just
-
in
-
time (JIT) compilation: C#, some Java


Compiled to intermediate code, machine code produced
just before execution

Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Translation/Execution


Compiler

se

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Translate source code program into object
code (machine language).


Object code is linked to libraries, other
modules, to generate an executable object
code module


Object code can be
executed repeatedly
without
repeating the compilation process.


Copyright © 2006 The McGraw
-
Hill Companies, Inc.

Translation/Execution


Interpreter

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


No object code.


Interpretive routines


Examine the program. When an action is
recognized, do it (by calling one of the
routines).


Slower execution (than with compilation):
lexical, syntax, type/semantic analysis
must be performed every time


Better interactive development
environment.

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Some languages (e.g., Java) are compiled to
a machine code (byte code) that runs on a
virtual

machine (the JVM).


To run Java programs install a JVM for your
machine that interprets the byte code.


Benefit: compiler is platform independent;
byte code can run on any computer with a
JVM


Disdadvantage
: slow


Solution: JIT compilers

Copyright © 2006 The McGraw
-
Hill Companies, Inc.


Programming language principles


Grammars, syntax, semantics


What makes a language successful?


Reliable, readable, writeable


Supported by simplicity, orthogonality, efficiency,
support, …


Paradigms


Imperative, object
-
oriented, functional, logic


Language implementation


Compilers & interpreters


SSummaryY