0% found this document useful (0 votes)
58 views

Lecture 1&2 PPL

Here are the steps to solve this problem using logic programming: 1. Define facts and relationships as predicates (e.g. owns(Spanish, dog), drinks(green, coffee), etc.) 2. Write rules to relate predicates (e.g. owns(X, dog) -> nationality(X, Spanish)) 3. Write a query to find the answer (e.g. nationality(X, Ukrainian)) 4. The Prolog interpreter will use backward chaining to try and satisfy the query by applying rules until it finds a solution. So in Prolog this problem could be modeled logically and solved through goal-directed searching of the rule space. The key aspects are defining relationships as predicates
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Lecture 1&2 PPL

Here are the steps to solve this problem using logic programming: 1. Define facts and relationships as predicates (e.g. owns(Spanish, dog), drinks(green, coffee), etc.) 2. Write rules to relate predicates (e.g. owns(X, dog) -> nationality(X, Spanish)) 3. Write a query to find the answer (e.g. nationality(X, Ukrainian)) 4. The Prolog interpreter will use backward chaining to try and satisfy the query by applying rules until it finds a solution. So in Prolog this problem could be modeled logically and solved through goal-directed searching of the rule space. The key aspects are defining relationships as predicates
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Principles of Programming

Languages
TextBook

Concepts of Programming
Languages, Robert W.
Sebesta, (10th edition),

Addison-Wesley Publishing
Company

Principles of Programming languages


Contents
• Programming languages: structure and
semantics
• Some language implementation
considerations
• See the Compilers course for more details!
• How to evaluate and compare languages

Principles of Programming languages


What we will not be covering
• Assembly language
• Concurrency
• Software tools
• How to build a compiler

Principles of Programming languages


Chapter 1 : Topics
• Reasons for Studying Concepts of Programming
Languages
• Programming Domains
• Language Evaluation Criteria
• Influences on Language Design
• Language Categories
• Language Design Trade-Offs
• Implementation Methods
• Programming Environments

Principles of Programming languages


Evolution of Programming Languages

Principles of Programming languages


Reasons for Studying Concepts of
Programming Languages
• Increased ability to express ideas
• Improved background for choosing
appropriate languages
• Increased ability to learn new languages
• Better understanding of significance of
implementation
• Better use of languages that are already
known
• Overall advancement of computing
Principles of Programming languages
Programming Domains
1. Scientific applications
2. Business applications
3. Artificial intelligence
4. Systems programming
5. Scripting languages
6. Special purpose languages

Principles of Programming languages


Programming Domains
• Scientific Applications
– Large numbers of floating point computations; use of arrays.
– Example: Fortran.
• Business Applications
– Produce reports, use decimal numbers and characters.
– Example: COBOL.
• Artificial intelligence
– Symbols rather than numbers manipulated; use of linked
lists.
– Example: LISP.

Principles of Programming languages


Programming Domains
• System programming
- Need effieciency because of continous use.
- Example:C
• Web Software
-Eclectic collection of languages:
markup(example:XHTML),scripting
(example:PHP),
• general-purpose(example:JAVA).
Principles of Programming languages
Characteristics impact Criteria impact
Software Design

Principles of Programming languages


Language Evaluation Criteria
● Readability:
➢ The ease with which programs can be read and
understood.
● Writability:
➢ The ease with which a language can be used to create
programs.
● Reliability:
➢ Conformance to specifications (i.e., performs to its
specifications).
● Cost:
➢ The ultimate total cost.

Principles of Programming languages


Evaluation Criteria: Readability
➔ Overall simplicity
◆ A manageable set of features and constructs.
◆ Minimal feature multiplicity .
– (Feature multiplicity: having more than one way to
accomplish an operation, reduces multiplicity. For
example, there are four ways to increment a simple
variable in C:

count=count+1
count+=1
count++
++count
)
◆ Minimal operator overloading.
Principles of Programming languages
Evaluation Criteria: Readability
➔ Orthogonality
◆ A relatively small set of primitive constructs
can be
combined in a relatively small number of ways
◆ Every possible combination is legal
➔ Data types
◆ Adequate predefined data types.

Principles of Programming languages


Orthogonality Example
– Following is an example of orthogonality in VAX as compared to
IBM Mainframe:
In IBM main frame has two different instructions for adding the contents of
a register to a memory cell or another register.
A Reg1, memory_cell
AR Reg1, Reg2

In contrast, VAX has only one statement for addition (hence greater
orthogonality):
ADDL operand1, operand2

– How does Orthogonality affect readability?

Principles of Programming languages


Here are some examples of non-orthogonality in C:

– C has two kinds of built-in data structures, arrays and records (structs). Records can
be returned from functions, but arrays cannot.

– A member of a struct can have any type except void or a structure of the same
type.

– An array element can be any data type except void or a function.

– Parameters are passed by value, unless they are arrays, in which case they are
passed by reference.

Reading
Chapter on Orthogonality from “The Art of Unix
Programming”
Principles of Programming languages
Evaluation Criteria: Readability
➔Syntax considerations
-Identifier forms: flexible composition.
-Special words and methods of forming compound statements.
-Form and meaning: self-descriptive constructs, meaningful
keywords

Principles of Programming languages


Evaluation Criteria: Readability
Which of the following loops is more readable?

COBOL

Principles of Programming languages


Evaluation Criteria: Readability
• Briefness and readability are (usually)
inversely proportional?
– Simple division in C++ and COBOL
C++: k = i / 10
COBOL: divide i by 10 giving k
– Simple addition in C++ and COBOL
C++: k = i + 10
COBOL: add i to 10 giving k

Principles of Programming languages


Evaluation Criteria: Writability
• Simplicity and orthogonality
– Few constructs, a small number of primitives, a small
set of rules for combining them.
Example – Which would you use as a C++ programmer? See:
Obfuscated Code

Principles of Programming languages


Evaluation Criteria: Writability
● Support for abstraction
-The ability to define and use complex structures or
operations in ways that allow details to be ignored.
● Expressivity
– A set of relatively convenient ways of specifying
operations.
– Strength and number of operators and predefined
functions.

Principles of Programming languages


EVALUATION CRITERIA: RELIABILITY
Reliability – What does it mean, and what
affects it?
• A program is said to be reliable if it performs
to its specifications under all conditions.

• Language features directly affect reliability –


and though we would like to increase
reliability at all costs, there are tradeoffs.

Principles of Programming languages


Evaluation Criteria: Reliability
• Type checking
– Testing for type errors.
• Exception handling
– Intercept run-time errors and take corrective measures.
• Aliasing
– Presence of two or more distinct referencing methods for
the same memory location.
• Readability and writability
– A language that does not support “natural” ways of
expressing an algorithm will require the use of
“unnatural” approaches, and hence reduced reliability

Principles of Programming languages


Question
• Find a case in C++ and JAVA – where an algorithm has a more
natural expression in one and less so in the other. Argue for
you choice.

Principles of Programming languages


Evaluation Criteria: Cost
• Training programmers to use the language
• Writing programs (closeness to particular
applications)
• Compiling programs
• Executing programs
• Language implementation system:
availability of free compilers
• Reliability: poor reliability leads to high costs
• Maintaining programs

Principles of Programming languages


Evaluation Criteria: Others
• Portability
– The ease with which programs can be moved
from one implementation to another.
• Generality
– The applicability to a wide range of
applications.
• Well-definedness
– The completeness and precision of the
language’s official definition

Principles of Programming languages


The programming language spectrum
Languages may be classified based on the model
of computation

Top Level Division of languages


Declarative Languages: What the computer has
to do…
Imperative Languages: How the computer
should do what it is supposed to do…

Principles of Programming languages


Language Categories

Principles of Programming languages


The programming language spectrum

Dataflow

Principles of Programming languages


The programming language spectrum
Functional languages employ a computational model
based on the recursive definition of functions. They
take their inspiration from the lambda calculus, a
formal computational model developed by Alonzo
Church in the 1930s. In essence, a program is
considered a function from inputs to outputs,
defined in terms of simpler functions through a
process of refinement. Languages in this category
include Lisp, ML, and Haskell.

Principles of Programming languages


The programming language spectrum
Logic or constraint-based languages take their
inspiration from predicate logic. They model
computation as an attempt to find values that satisfy
certain specified relationships, using a goal-directed
a search through a list of logical rules.

Prolog is the best-known logic language. The term can


also be applied to the programmable aspects of
spreadsheet systems such as Excel, VisiCalc, or Lotus
1-2-3.

Principles of Programming languages


The programming language spectrum
Typical Problem for a logic program
1) There are 5 colored houses in a row, each having an owner, which has an animal,
a favorite cigarette, a favorite drink.
2) The English lives in the red house.
3) The Spanish has a dog.
4) They drink coffee in the green house.
5) The Ukrainian drinks tea.
6) The green house is next to the white house.
7) The Winston smoker has a serpent.
8) In the yellow house they smoke Kool.
9) In the middle house they drink milk.
10) The Norwegian lives in the first house from the left.
11) The Chesterfield smoker lives near the man with the fox.
12) In the house near the house with the horse they smoke Kool.
13) The Lucky Strike smoker drinks juice.
14) The Japanese smokes Kent.
15) The Norwegian lives near the blue house.
Who has a zebra and who drinks water?
Principles of Programming languages
The programming language spectrum
Dataflow languages model computation as the flow of
information (tokens) among primitive functional
nodes.
They provide an inherently parallel model: nodes are
triggered by the arrival of input tokens, and can
operate concurrently.

Id and Val are examples of dataflow languages. Sisal,


a descendant of Val, is more often described as a
functional language.

Principles of Programming languages


Influences on Language Design
• Computer Architecture
– Languages are developed around the prevalent
computer architecture, known as the von
Neumann architecture
• Programming Methodologies
– New software development methodologies (e.g.,
object-oriented software development) led to
new programming paradigms and by extension,
new programming languages

Principles of Programming languages


Computer Architecture Influence
• Well-known computer architecture: Von Neumann

• Imperative languages, most dominant, because of von


Neumann computers
– Data and programs stored in memory
– Memory is separate from CPU
– Instructions and data are piped from memory to CPU
– Basis for imperative languages

• Variables model memory cells


• Assignment statements model piping
• Iteration is efficient

Principles of Programming languages


The Von Neumann Architecture

Principles of Programming languages


The Von Neumann Architecture

Principles of Programming languages


Programming Methodologies Influences
• 1950s and early 1960s: Simple applications; worry about
machine efficiency
• Late 1960s: People efficiency became important; readability,
better control structures
– structured programming
– top-down design and step-wise refinement
• Late 1970s: Process-oriented to data-oriented
– data abstraction
• Middle 1980s: Object-oriented programming
– Data abstraction + inheritance + polymorphism

Principles of Programming languages


Languages vs. Language Implementations

Principles of Programming languages


Implementation Methods

Principles of Programming languages


Layered View of Computer
• The operating system
and language
implementation are
layered over
machine interface of
a computer

Principles of Programming languages


Program Execution

Principles of Programming languages


Interpretation

Principles of Programming languages


Example: CPython (‘normal’ Python)

Principles of Programming languages


Python execution (simplified)

Principles of Programming languages


Compilation
THE COMPILATION PROCESS
Compilation

Principles of Programming languages


Compiling and Linking in C

Principles of Programming languages


Principles of Programming languages
Pure Interpretation Process

Principles of Programming languages


Comparison: Compilation vs
Interpretation

Principles of Programming languages


Dynamic Compilation

Principles of Programming languages


Hybrid Implementation Systems

Principles of Programming languages


Hybrid Implementation Process

Principles of Programming languages Results


Just-in-Time Implementation Systems

Principles of Programming languages

You might also like