<<

Functional Languages Object-Oriented Languages Lisp, Scheme and ML are ++, Java, , Pizza and functional in nature. Python are object-oriented. Programs are expressions to be Data and functions are evaluated. encapsulated into Objects. Language design aims to Objects are active, have minimize side-effects, including persistent state, and uniform assignment. interfaces (messages or Alternative evaluation methods). mechanisms are possible, Notions of inheritance and including common interfaces are central. Lazy (Demand Driven) All objects that provide the same interface are treated uniformly. In Java you can print Eager (Data Driven or any object that provides the Speculative) toString method. Iteration through the elements of any object that implements the Enumeration interface is possible.

© © CS 538 Spring 2008 37 CS 538 Spring 2008 38

Subclassing allows to you Logic Programming extend or redefine part of an Languages object’ behavior without Prolog notes that most reprogramming all of the programming languages object’s definition. Thus in Java, address both the logic of a you can take a Hashtable class program (what is to be done) (which is fairly elaborate) and and its control flow (how you create a subclass in which an do what you want). existing method (like toString) is redefined, or new operations A logic , are added. like Prolog, lets programmers focus on a program’s logic without concern for control issue. These languages have no real control structures, and little notion of “flow of control.” What results are programs that are unusually succinct and focused.

© © CS 538 Spring 2008 39 CS 538 Spring 2008 40 Example: Review of Concepts from inOrder( [] ). inOrder( [ _ ] ). inOrder([a,b|c]) :- (a

© © CS 538 Spring 2008 41 CS 538 Spring 2008 42

• Data objects have a lifetime—the span Properties of an identifier (and the of time, during program execution, object it represents) may be set at during which the object exists and • Compile-time may be used. • Lifetimes of data objects are often tied These are static properties as to the scope of the identifier that they do not change during denotes them. The objects are created execution. Examples include when its identifier’s scope is entered, the type of a variable, the value and they may be deleted when the of a constant, the initial value identifier’s scope is exited. For of a variable, or the body of a example, memory for local variables function. within a function is usually allocated when the function is called (activated) • Run-time and released when the call terminates. These are dynamic properties. In Java, a method may be loaded into Examples include the value of a memory when the object it is a variable, the lifetime of a heap member of is first accessed. object, the value of a function’s parameter, the number of times a while loop iterates, etc.

© © CS 538 Spring 2008 43 CS 538 Spring 2008 44 Example: Structured Languages

In • Include Algol 60, Pascal, C and Java. • The scope of an identifier is the whole • Identifiers may have a non-global program or subprogram. scope. Declarations may be local to a class, subprogram or block. • Each identifier may be declared only once. • Scopes may nest, with declarations propagating to inner (contained) • Variable declarations may be implicit. scopes. (Using an identifier implicitly declares it as a variable.) • The lexically nearest declaration of an identifier is bound to uses of that • The lifetime of data objects is the whole program. identifier.

© © CS 538 Spring 2008 45 CS 538 Spring 2008 46

Binding of an identifier to its Block Structure Concepts corresponding declaration is usually static (also called • Nested Visibility lexical), though dynamic No access to identifiers outside binding is also possible. their scope. Static binding is done prior to • Nearest Declaration Applies execution—at compile-time. Static name scoping. Example (drawn from C): • Automatic Allocation and Deallocation of Locals int x,z; Lifetime of data objects is void A() { bound to the scope of the float x,y; print(x,y,z); Identifiers that denote them. int } float void B() { float print (x,y,z) int } undeclared int

© © CS 538 Spring 2008 47 CS 538 Spring 2008 48 Variations in these rules of Dynamic Scoping name scoping are possible. An alternative to static scoping For example, in Java, the is dynamic scoping, which was lifetime of all class objects is used in early Lisp dialects (but from the time of their creation not in Scheme, which is (via new) to the last visible reference to them. statically scoped). Thus Under dynamic scoping, identifiers are bound to the ... Object O;... dynamically closest declaration creates an object reference but of the identifier. Thus if an does not allocate any memory identifier is not locally space for O. declared, the call chain You need (sequence of callers) is examined to find a matching ... Object O = new Object(); ... declaration. to actually create memory space for O.

© © CS 538 Spring 2008 49 CS 538 Spring 2008 50

Example: Dynamic scoping makes type checking and variable access int x; harder and more costly than void print() { static scoping. (Why?) write(x); } However, dynamic scoping main () { does allow a notion of an bool x; “extended scope” in which print(); declarations extend to } subprograms called within that Under static scoping the x scope. written in print is the lexically Though dynamic scoping may closest declaration of x, which seen a bit bizarre, it is closely is as an int. related to virtual functions Under dynamic scoping, since used in C++ and Java. print has no local declaration of x, print’s caller is examined. Since main calls print, and it has a declaration of x as a bool, that declaration is used.

© © CS 538 Spring 2008 51 CS 538 Spring 2008 52 Virtual Functions A function declared in a class, C, may be redeclared in a class derived from C. Moreover, for uniformity of redeclaration, it is important that all calls, including those in methods within C, use the new declaration. Example: class C { void DoIt()(PrintIt();} void PrintIt() {println(“C rules!”);} } class D extends C { void PrintIt() {println(“D rules!”);} void TestIt() {DoIt();} } D dvar = new D(); dvar.TestIt(); D rules! is printed.

© CS 538 Spring 2008 53