PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ06C - Polymorphism Programming Language Design and.

Slides:



Advertisements
Similar presentations
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Advertisements

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Chapter 7:: Data Types Programming Language Pragmatics
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Type Checking.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10A - Heap storage Programming Language Design and.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
PZ06A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06A - Inheritance Programming Language Design and Implementation.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Approaches to Typing Programming Languages Robert Dewar.
Elementary Data Types Scalar Data Types Numerical Data Types Other
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
SE424 Languages with Context A Block Structured Language.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Names, Scope, and Bindings Programming Languages and Paradigms.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ11A Programming Language design and Implementation -4th Edition
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML Programming Language Design and Implementation (4th Edition)
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
PZ05A - Abstract data types
PZ09A - Activation records
PZ04A - Scalar and composite data
Abstract data types Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Names and Binding In Text: Chapter 5.
Polymorphism Programming Language Design and Implementation
Abstract data types Programming Language Design and Implementation
Parameter transmission
Polymorphism Programming Language Design and Implementation
PZ09B - Parameter transmission
Abstract data types Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Parameter transmission
Polymorphism Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Type equality Programming Language Design and Implementation
Types and Related Issues
Type equality Programming Language Design and Implementation
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
Polymorphism Programming Language Design and Implementation
Presentation transcript:

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Polymorphism The use of parameters to subprograms is one of the oldest characteristics of programming languages But they have an l-value. That is, they are a data object that requires storage. Polymorphism means a single operator or subprogram name can to refer to any of a number of function definitions depending on the data types of the arguments and results. Similar to overloading It is generally applied to functions where a type is one of the arguments.

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Polymorphism in ML reverse([1,2,3]); val it = [3,2,1]: int list reverse([1.0, 2.0, 3.0]); val it = [3.0,2.0,1.0]: real list; reverse([1,2,3], [4,5,6], [7,8,9]]; val it = [[7,8,9],[4,5,6],[1,2,3]] : int list list; Arguments to reverse are different list data types fun identity(x) = x; fn: 'a -> 'a identity(1); val it = 1: int; identity(1.5); val it = 1.5: real; But why is the following not polymorphic (or legal) in ML? fun add(x,y) = x+y?

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Type inference in ML ML is strongly typed, but if it can infer a type, it does not need an explicit type declaration: fun add(x:int, y:int):int = x+y; add is fully qualified. Any one declaration defines the operation. All are equivalent: fun add(x:int, y) = x+y; fun add(x, y:int) = x+y; fun add(x, y):int = x+y; But fun add(x, y) = x+y; is ambiguous At runtime, function is well defined, but ML uses static type inferencing to determine function to invoke before execution.

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Type inference examples ML does not allow automatic coersions between int and real, so what are signatures (if any) to the following? fun add(x,y) = real(x) + y; fun thing(x::y) = x+ hd(y); fun thing(x::y) = truncate(x)+ hd(y); fun thing(x::y:int list) = x+ hd(y);

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Polymorphism in arrays Note: Implementation of arrays differs in most languages: Pascal - arrays are explicit and bounds are part of type. Not polymorphic. type matrix10 = array[1..10] of integer; var x: matrix10; Ada - polymorphic array bounds type matrix is array (integer range <>) of integer; y: matrix(1..30); C arrays don't really exist - shorthand for pointer variables. int C[10]; C[5] means *C+5

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Create polymorphism Can often build parameterized objects for true polymorphism, e.g., stack of objects: Y: stack(int, 10)  up to 10 ints Z: stack(float, 20)  up to 20 reals Can do this in ML and Ada for any data types that don't involve calculations (i.e., only for operations that use pointer manipulation (stacking and unstacking) Why? Macros can be used to simulate this for languages without polymorphism, e.g. In Pascal: sum(int,A,B)  can be implemented as a macro with 4 sequences depending upon types of arguments: trunc(A)+B A+trunc(B) A+B trunc(A)+trunc(B)

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Implementation For statically typed languages (ML, C++), polymorphism is easy: Keep track of function argument types in symbol table of compiler. For dynamically typed languages: Two forms of arguments can be passed to a polymorphic function: 1. An immediate descriptor - when the value to a function is smaller than the size of the fixed field. For example, passing a Boolean, character, or small integer to a function uses less space than the fixed- size object permits. The actual value is placed in the argument location, and the extra bits in the field are used to tell the function what the type actually is. 2. A boxed descriptor occurs in all other cases.

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Boxed descriptors The argument field will contain a type indicator stating the argument is boxed. The rest of the field will be the address of the actual object (which will be elsewhere, such as in heap storage). At this address, the complete type information will be given, such as giving the entire structure of the composite data object.

PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Example of dynamic polymorphism Assume argument descriptor field is 5 bytes. Byte 1 is a data type indicator and Bytes 2 to 5 are the data. The following arguments can be passed to a polymorphic function: bit Integer data. Byte 1=0 signifying integer, bytes 2 to 5 are the 32-bit integer value bit character data. Byte 1=1 signifying character, byte 2= actual character argument, bytes 3 to 5 unused bit Boolean data. Byte 1=2 and byte 2= 0 or Complex record structure. Byte 1=3 and bytes 2 to 5 are pointer to structure. The r-value at this pointer address contains more information about the actual argument.