final Flashcards
functional languages vs imperative languages
imperative:
- based directly on the von Neumann
architecture:
- Efficiency is the primary concern, rather than the suitability of the language for software development
functional:
- based on mathematical functions:
- A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run
mathematical function
a mapping of members of one set, called the domain set, to another set, called the range set
Lambda expressions
*Lambda expressions describe nameless functions
*Lambda expressions are applied to parameter(s) by placing the parameter(s)
after the expression,
Function Composition
A functional form that takes two functions as parameters and yields a function
whose value is the first actual parameter function applied to the application of
the second:
Form: h =f∘g
which means: h(x) =f(g(x)
objective of the design of an FPL
mimic mathematical functions to the
greatest extent possible
basic process of computation in FPL vs imperative languages
§ imperative language: operations are done and the results are stored in variables for later use
§Management of variables is a constant concern and source of complexity for
imperative programming
* FPL: variables are not necessary, as is the case in mathematics, the evaluation of a function always produces the same result given the same parameters (referential transparency)
Tail recursive function in Scheme
- A function is tail recursive if its recursive call is the last operation in
the function - A tail recursive function can be automatically converted by a compiler to use iteration, making it faster
- Scheme language definition requires that Scheme language systems convert all tail recursive functions to use iteration
common lisp features
§records
§arrays
§complex numbers
§character strings
§powerful I/O capabilities
§packages with access control
§iterative control statements
Symbol Data Type
*Similar to that of Ruby
*The reserved words are symbols that evaluate to themselves
*Symbols are either bound or unbound:
§Parameter symbols are bound while the function is being evaluated
§Symbols that are the names of imperative style variables that have been assigned
values are bound
§All other symbols are unbound
ML
- static-scoped
- syntax that is closer to Pascal than to
Lisp - type declarations + type inferencing
- strongly typed and has no type coercions
- Does not have imperative-style variables
- identifiers are untyped
- exception handling and a module facility for implementing abstract data types
- lists and list operations
how is Haskell similar and different from ML?
*Similar to ML (syntax, static scoped, strongly typed, type inferencing, pattern
matching)
*Different from ML (and most other functional languages) in that it is purely
functional (e.g., no variables, no assignment statements, and no side effects of
any kind)
F#
- imperative features and supports
OOP - tuples, lists, discriminated unions, records, and both mutable and immutable arrays
Trends in imperative languages
Support for functional programming is increasingly creeping into imperative languages
Comparing Functional and Imperative Languages
*Imperative Languages:
§Efficient execution
§Complex semantics
§Complex syntax
§Concurrency is programmer designed
*Functional Languages:
§Simple semantics
§Simple syntax
§Less efficient execution
§Programs can automatically be made concurrent
abstraction
view or representation of an entity that includes only the most significant attributes
* fundamental in programming (and computer science)
*Nearly all programming languages support process abstraction with subprograms
*Nearly all programming languages designed since 1980 support data
abstraction
abstract data type (ADT)
a user-defined data type that satisfies the
following two conditions
1. the only operations possible are those provided in the type’s definition
2. declarations and the protocols of the operations on objects of the type are contained in a single syntactic unit
Advantages of Data Abstraction
*Advantages the first condition:
§Reliability: by hiding the data representations, user code cannot directly access objects of the type or depend on the representation, allowing the representation to
be changed without affecting user code
§Reduces the range of code and variables of which the programmer must be aware
§Name conflicts are less likely
*Advantages of the second condition:
§Provides a method of program organization
§Aids modifiability (everything associated with a data structure is together)
§Separate compilation
Language Requirements for ADTs
*A syntactic unit in which to encapsulate the type definition
*A method of making type names and subprogram headers visible to clients,
while hiding actual definitions
*Some primitive operations must be built into the language processor
Parameterized ADTs
Parameterized ADTs allow designing an ADT that can store any type elements:
only an issue for static typed languages
Encapsulation Constructs
a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units)
Nested Subprograms
*Organizing programs by nesting subprogram definitions inside the logically
larger subprograms that use them
*Nested subprograms are supported in Python, JavaScript, and Ruby
Object-Oriented Programming (OOP) Languages
*Some support procedural and data-oriented programming (e.g., C++)
*Some support functional program (e.g., CLOS)
*Newer languages do not support other paradigms but use their imperative
structures (e.g., Java and C#)
*Some are pure OOP language (e.g., Smalltalk & Ruby)
*Some functional languages support OOP, but they are not discussed in this chapter
Three major OOP language features:
§Abstract data types (Chapter 11)
§(Class) Inheritance:
o Inheritance is the central theme in OOP and languages that support it
§Polymorphism
Design Issues for OOP Languages
*The Exclusivity of Objects
*Are Subclasses Subtypes?
*Single and Multiple Inheritance
*Object Allocation and Deallocation
*Dynamic and Static Binding
*Nested Classes
*Initialization of Objects
Single and Multiple Inheritance
*Multiple inheritance allows a new class to inherit from two or more classes
*Disadvantages of multiple inheritance:
§Language and implementation complexity (in part due to name collisions)
§Potential inefficiency: dynamic binding costs more with multiple inheritance (but not
much)
*Advantage:
§Sometimes it is quite convenient and valuable
Allocation and DeAllocation of Objects
§If they behave line the ADTs, they can be allocated from anywhere:
o Allocated from the run-time stack
o Explicitly create on the heap (via new)
§If they are all heap-dynamic, references can be uniform thru a pointer or reference
variable:
o Simplifies assignment: dereferencing can be implicit
§If objects are stack dynamic, there is a problem with regard to subtypes: object
slicing
Two interesting and challenging parts of implementing OO constructs:
§Storage structures for instance variables
§Dynamic binding of messages to methods
Instance Data Storage
*Class instance records (CIRs) store the state of an object:
§Static (built at compile time)
*If a class has a parent, the subclass instance variables are added to the parent
CIR
*Because CIR is static, access to all instance variables is done as it is in records:
§Efficient
Dynamic Binding of Methods Calls
Methods in a class that are statically bound need not be involved in the CIR;
methods that will be dynamically bound must have entries in the CIR:
§Calls to dynamically bound methods can be connected to the corresponding code
thru a pointer in the CIR
§The storage structure is sometimes called virtual method tables (vtable)
§Method calls can be represented as offsets from the beginning of the vtable
Reflection
A programming language that supports reflection allows its programs to have runtime access to their types and structure and to be able to dynamically modify their behavior
metadata
The types and structure of a program
introspection
The process of a program examining its metadata
intercession
Interceding in the execution of a program
Uses of reflection for software tools:
§Class browsers need to enumerate the classes of a program
§Visual IDEs use type information to assist the developer in building type correct code
§Debuggers need to examine private fields and methods of classes
§Test systems need to know all of the methods of a class
Concurrency can occur at four levels:
§Machine instruction level
§High-level language statement level
§Unit level
§Program level
Categories of Concurrency:
§Physical concurrency: Multiple independent processors ( multiple threads of control)
§Logical concurrency: The appearance of physical concurrency is presented by time-
sharing one processor (software can be designed as if there were multiple threads of
control)
Coroutines
have a single thread of control
thread of control in a program
the sequence of program points reached as control flows through the program
task or process or thread
a program unit that can be in concurrent execution
with other program units
Tasks differ from ordinary subprograms in that:
§A task may be implicitly started
§When a program unit starts the execution of a task, it is not necessarily suspended
§When a task’s execution is completed, control may not return to the caller
Tasks dont usually work together
FALSE
Two General Categories of Tasks
*Heavyweight tasks execute in their own address space
*Lightweight tasks all run in the same address space, more efficient
*A task is disjoint if it does not communicate with or affect the execution of any
other task in the program in any way
Task Synchronization
A mechanism that controls the order in which tasks execute
Two kinds of synchronization:
§Cooperation synchronization
§Competition synchronization
Task communication is necessary for synchronization, provided by:
§Shared nonlocal variables
§Parameters
§Message passing
Task Execution States
*New: created but not yet started
*Ready: ready to run but not currently running (no available processor)
*Running
*Blocked: has been running, but cannot now continue (usually waiting for some
event to occur)
*Dead: no longer active in any sense
Design Issues for Concurrency
*Competition and cooperation synchronization: the most important issue
*Controlling task scheduling
*How can an application influence task scheduling
*How and when tasks start and end execution
*How and when are tasks created
Semaphores
*A semaphore is a data structure consisting of a counter and a queue for storing
task descriptors:
§A task descriptor is a data structure that stores all of the relevant information about
the execution state of the task
*Semaphores can be used to implement guards on the code that accesses
shared data structures
*Semaphores have only two operations, wait and release (originally called Pand
Vby Dijkstra)
*Semaphores can be used to provide both competition and cooperation
synchronization
Monitors
*The idea: encapsulate the shared data and its operations to restrict access
*A monitor is an abstract data type for shared data
Message Passing
*Message passing is a general model for concurrency:
§It can model both semaphores and monitors
§It is not just for competition synchronization
*Central idea: task communication is like seeing a doctor, most of the time the
doctor waits for you or you wait for the doctor, but when you are both ready, you
get together, or rendezvous
Ada Evaluation
*Message passing model of concurrency is powerful and general
*Protected objects are a better way to provide synchronized shared data
*In the absence of distributed processors, the choice between monitors and tasks
with message passing is somewhat a matter of taste
*For distributed systems, message passing is a better model for concurrency
Java’s Thread Evaluation
*Java’s support for concurrency is relatively simple but effective
*Not as powerful as Ada’s tasks
C#’s Concurrency Evaluation
*An advance over Java threads, e.g., any method can run its own thread
*Thread termination is cleaner than in Java
*Synchronization is more sophisticated
Statement-Level Concurrency
*Objective: Provide a mechanism that the programmer can use to inform compiler of ways it can map the program onto multiprocessor architecture
*Minimize communication among processors and the memories of the other processors
High-Performance Fortran (HPF)
*A collection of extensions that allow the programmer to provide information to
the compiler to help it optimize code for multiprocessor computers
*Specify the number of processors, the distribution of data over the memories of
those processors, and the alignment of data
exception
any unusual event, either erroneous or not, detectable by either
hardware or software, that may require special processing
exception handling
The special processing that may be required after detection of an exception
exception handler
exception handling code unit
exceptions: Design Issues
*How and where are exception handlers specified and what is their scope?
*How is an exception occurrence bound to an exception handler?
*Can information about the exception be passed to the handler?
*Where does execution continue, if at all, after an exception handler completes
its execution? (continuation vs. resumption)
*Is some form of finalization provided?
*How are user-defined exceptions specified?
*Should there be default exception handlers for programs that do not provide
their own?
*Can predefined exceptions be explicitly raised?
*Are hardware-detectable errors treated as exceptions that can be handled?
*Are there any predefined exceptions?
*How can exceptions be disabled, if at all?
Exceptions: C++ Evaluation
*There are no predefined exceptions
*It is odd that exceptions are not named and that hardware- and system
software-detectable exceptions cannot be handled
*Binding exceptions to handlers through the type of the parameter certainly does
not promote readability
Exceptions: Java Evaluation
*The types of exceptions makes more sense than in the case of C++
*The throws clause is better than that of C++ (The throw clause in C++ says little to the programmer)
*The finally clause is often useful
*The Java interpreter throws a variety of exceptions that can be handled by user programs
Python Exception Handling
*Exceptions are objects: the base class is BaseException
*All predefined and user-defined exceptions are derived from Exception
*Predefined subclasses of Exception are:
§ArithmeticError: subclasses are OverflowError, ZeroDivisionError, and
FloatingPointError
§LookupError: subclasses are IndexError and KeyError