Note that some items actually talked about in Lecture 14 are in the lecture 13 notes and are not repeated here!
Advantage to external rep. is that can define array of stack, etc.
generic length : Natural := 100; type element is private; -- generic parameters package stack is type stack is private; procedure make_empty (S : out stack); procedure push (S : in out stack; X : in element); procedure pop (S : in out stack; X: out element); function empty (S : stack) return boolean; stack_error : exception; private type stack is record space : array(1..length) of element; top : integer range 0..length := 0; end record; end stack; package body stack is procedure make_empty (S : out stack); begin S.top := 0; end make_empty ; procedure push (S : in out stack; X : element) is begin if full(S) then raise stack_error; else S.top := S.top + 1; S.space(S.top) := X; end if; end push; procedure pop (S : in out stack; X: out element) is begin if empty(S) then raise stack_error; else X := S.space(S.top); S.top := S.top - 1; end if; end pop; function empty(S : in out stack) return boolean is begin return (top = 0); end; function full(S : in out stack) return boolean is begin return (S.top = length); end; end stack;
Biggest problem is exposure of representation of ADT in private part of package spec.
Not available to user, but must recompile if change representation.
Modula 2 is 1980 revision (influenced by Mesa at Xerox PARC) intended to synthesize systems programming with general purpose language supporting modern software engineering.
(operating system for Lilith microcomputer written in Modula 2)
Sample program
DEFINITION MODULE stackMod; IMPORT element FROM elementMod; TYPE stack; PROCEDURE make_empty (VAR S : stack); PROCEDURE push (VAR S : stack; X : element); PROCEDURE pop (VAR S : stack; X: element); PROCEDURE empty (S : stack): BOOLEAN; END stackMod. IMPLEMENTATION MODULE stackMod; TYPE stack = POINTER TO RECORD space : array[1..length] of element; top : INTEGER; END; PROCEDURE make_empty (VAR S : stack); BEGIN S^.top := 0; END make_empty ; ... END stackMod;
Opaque types (those declared without definition in Definition module) must be pointers or take no more space than pointers.
Compare and contrast Modula-2 and Ada on supporting abstract data types.
Representations fairly similar - both can import from other units (modules or packages) and export items to other units.
For external representations not much difference.
Private types in Ada vs opaque types in Modula.
Opaque types require Pointer (or other data type of same length or less)
-Use of opaque types does not force recompilation of user programs if representation changes - does force recompilation in Ada.
Internal representations of ADT's almost identical.
Ada more flexible via generic routines - can parameterize over types as well as sizes of objects. Can also use this to create new instances of packages - especially helpful for internal representations of data types.
Provides both packaging and hiding of representation
(cvt used to go back and forth btn external abstract type and internal representation).
May have parameterized clusters where specify needed properties of type paramenter. E.g.,
sorted_bag = cluster [t : type] is create, insert, ... where t has lt, equal : proctype (t,t) returns (bool);Abstraction facilities described in Liskov et al. paper in collection on reserve shelf.
Biggest difference from Ada and Modula-2 is that cluster is a type. Therefore can create multiple copies. Elements of cluster types are held as implicit references.
Provides for encapsulation and information hiding
Example:
abstype intstack = mkstack of (int list) with exception stackUnderflow val emptyStk = mkstack [] fun push (e:int) (mkstack(s):intstack) = mkstack(e::s) fun pop (mkstack([])) = raise stackUnderflow | pop (mkstack(e::s)) = mkstack(s) fun top (mkstack([])) = raise stackUnderflow | top (mkstack(e::s)) = e fun IsEmpty (mkstack([])) = true | IsEmpty (mkstack(e::s)) = false end;
Generic stacks ADT:
abstype 'a stack = mkstack of ('a list) with exception stackUnderflow val emptyStk : 'a stack = mkstack [] fun push (e:'a) (mkstack(s):'a stack) = mkstack(e::s) fun pop (mkstack([]):'a stack) = raise stackUnderflow | pop (mkstack(e::s):'a stack) = mkstack(s):'a stack fun top (mkstack([]):'a stack) = raise stackUnderflow | top (mkstack(e::s):'a stack) = e fun IsEmpty (mkstack([]):'a stack) = true | IsEmpty (mkstack(e::s):'a stack) = false end;
Cannot get at representation of stack
Reference to mkstack(l) will generate an error message.
Only access through constants and op's.
More sophisticated support through modules, which also support separate
compilation.
See Ullman text or Harper's notes.
In Pascal get extreme position that procedures only take arrays w/exactly same dimensions as well as types.
Modula-2 starts opening up, by allowing different sizes of arrays as parameters, but still require same base type.
Restrictive since sort procedures will work with any ordered type.
Rather work with "polymorphic" functions and procedures.
Two main flavors of polymorphism: ad hoc and parametric (talk about subtype later).
Ad hoc also known as overloading. Done well it can make code easier to understand. E.g., "+" for real and integer addition.
Done poorly it can confuse and lead to errors, e.g. "+" for string concatenation.
Most languages provide some overloading (e.g., arith. operators, I/O), a few (e.g., Ada and C++) allow user to define overloaded operations (either infix or prefix).
E.g. in Ada, write
function "+"(m,n:Complex) return Complex is begin ... end;
Must provide a mechanism to support resolution of overloading - i.e. which actual operation does an instance refer to.
Two major ways of resolving:
Overloading is not essential and is easily abused. Useful when matches expectations from external notation. Confusing and unhelpful otherwise.
Parametric polymorphism usually considered a good thing.
These are almost always related to parameterized types.
E.g., sorting with array of T where T is ordered
Push, pop with stack of T.
Search of Binary search tree of T.
In ML, direct support for polymorphic functions on lists (parameterized).
Can define parameterized types through datatype definitions.
E.g.,
datatype 'a stack = Empty | Push of 'a * ('a stack)
Languages support two different forms of parametric polymorphism:
implicit
and explicit.
Differ on whether must supply type parameter.
ML is example of implicit polymorphism. Needn't supply type of any expression.
System derives most general type, often polymorphic.
E.g., reverse: 'a
list -> 'a list.
Very clever type inference algorithm invented independently by Hindley and
Milner.
Collects all information about types of terms in expression
(assigning a variable if no info) and then tries to "unify" all information so
mutually consistent.
E.g.
fun map f nil = nil | map f (hd::tl) = (f hd):: (map f tl) map: 'a -> 'b -> 'c'b = 'd list (since second arg is "nil" or "hd::tl")
f: 'a = 'd -> 'e (since "f hd" is defined)
'c = 'e list (since result is a list, and, in 2nd clause, first element is "f hd")
Therefore, map: ('d -> 'e) -> ('d list) -> ('e list)
Overloading does not interact well with implicit polymorphism because often don't have type info necessary to disambiguate expressions.
Some ML designers (e.g., Harper) feel that it would have been better to omit overloading of built-in functions.