450 likes | 531 Views
Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Modules: Ada. Modules called packages Two components: specification and body (both with same name)
E N D
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635
Modules: Ada • Modules called packages • Two components: specification and body (both with same name) • Components of module that are not exported are included in specification, in section called private • No restriction on what types may be exported
Running Example – Stacks ADA : package STACKPACK is type STACKTYPE is limited private; MAX_SIZE : constant := 100; function EMPTY (STK : in STACKTYPE) return BOOLEAN;
Running Example – Stacks procedure PUSH (STK : in out STACKTYPE; ELEMENT : in INTEGER); procedure POP (STK : in out STACKTYPE); function TOP (STK : in STACKTYPE) return INTEGER;
Running Example – Stacks private type LIST_TYPE is array (1..MAX_SIZE) of INTEGER; type STACKTYPE is record LIST : LIST_TYPE; TOPSUB : INTEGER range 0..MAX_SIZE := 0; end record; end STACKPACK;
Running Example – Stacks with TEXT_IO; use TEXT_IO; package body STACKPACK is function EMPTY (STK : in STACKTYPE) return BOOLEAN is begin return STK.TOPSUB = 0 end EMPTY;
Running Example – Stacks procedure PUSH (STK : in out STACKTYPE; ELEMENT : in INTEGER) is begin if STK.TOPSUB >= MAX_SIZE then PUT_LINE ("ERROR - Stack overflow");
Running Example – Stacks else STK.TOPSUB := STK.TOPSUB + 1; STK.LIST(TOPSUB) := ELEMENT; end if; end PUSH;
Running Example – Stacks procedure POP (STK : in out STACKTYPE) is begin if STK.TOPSUB = 0 then PUT_LINE ("ERROR - Stack underflow) else STK.TOPSUB := STK.TOPSUB - 1; end if; end POP;
Running Example – Stacks function TOP (STK : in STACKTYPE) return INTEGER is begin if STK.TOPSUB = 0 then PUT_LINE ("ERROR - Stack is empty"); else return STK.LIST(STK.TOPSUB); end if; end TOP; end STACKPACK;
Running Example – Stacks with STACKPACK, TEXT_IO; use STACKPACK, TEXT_IO; procedure USE_STACKS is TOPONE : INTEGER; STACK : STACKTYPE; begin
Running Example – Stacks ... PUSH (STACK, 42); PUSH (STACK, 27); POP (STACK) TOPONE := TOP (STACK); ... end USE_SATCKS;
Parameterized Modules • In above examples we have stacks of integers of size 100 • Would like to have module for use with any type • Would like to have module for use with any size • Want to pass type and value as arguments to module
Running Example – Stacks Ada: generic MAX_SIZE : POSITIVE; type ELEMENT_TYPE is private; package GENERIC_STACK is
Running Example – Stacks type STACKTYPE is limited private; function EMPTY (STK : in STACKTYPE) return BOOLEAN; … function TOP (STK : in STACKTYPE) return ELEMENT_TPYE;
Running Example – Stacks private type LIST_TYPE is array (1..MAX_SIZE) of ELEMENT_TYPE; type STACKTYPE is record LIST : LIST_TYPE; TOPSUB : INTEGER range 0..MAX_SIZE := 0; end record; end GENERIC_STACK;
Running Example – Stacks with TEXT_IO; use TEXT_IO; package body GENERIC_STACK is function EMPTY (STK : in STACKTYPE) return BOOLEAN is begin return STK.TOPSUB = 0 end EMPTY;
Running Example – Stacks procedure PUSH (STK : in out STACKTYPE; ELEMENT : in ELEMENT_TYPE) is begin if STK.TOPSUB >= MAX_SIZE then PUT_LINE ("ERROR - Stack overflow");
Running Example – Stacks else STK.TOPSUB := STK.TOPSUB + 1; STK.LIST(TOPSUB) := ELEMENT; end if; end PUSH; … end STACKPACK;
Running Example – Stacks package INTEGER_STACK is new GENERIC_STACK (100, INTEGER); package FLOAT_STACK is new GENERIC_STACK (500, FLOAT);
Modules: C++ • Modules called classes • Class contains data members and member functions • Uses private clause for entities hidden outside modules • Uses public clause for entities exported • User defined constructor function to initialize data member values • User defined destructor functions called by delete operator
Running Example – Stacks C++ :#include <iostream.h> class stack {private: int *stack_ptr; int max_len; int top_ptr;
Running Example – Stacks public: stack () { stack_ptr = new int [100]; max_len = 99; top_ptr = -1;}; ~stack () {delete stack_ptr;}; void push (int number) { if (top_ptr == max_len) cout << "Error in push--stack is full\n"; else stack_ptr[++top_ptr] = number; }
Running Example - Stacks void pop () { if (top_ptr == -1) cout << "Error in pop--stack is empty\n"; else top_ptr--; } int top () {return (stack_ptr[top_ptr]);} int empty () {return (top_ptr == -1);} };
Running Example - Stacks main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }
Running Example - Stacks To make the class stack parameterized by size, make the function stack be parameterized by size: stack (int size) { stack_ptr = new int [size]; max_len = size - 1; top_ptr = -1;};
Running Example – Stacks C++ templates: #include <iostreamn.h> template <class Type> // Type parameter class stack { private: Type *stack_ptr; int max_len; int top_ptr;
Running Example – Stacks public: stack () { stack_ptr = new Type [100]; max_len = 99; top_ptr = -1}; ~stack () {delete stack_ptr;}; void push (Type elt) { if (top_ptr == max_len) cout << "Error in push--stack is full\n"; else stack_ptr{++top_ptr} = elt; }
Running Example - Stacks void pop () { if (top_ptr == -1) cout << "Error in pop--stack is empty\n"; else top_ptr--; } Type top () {return (stack_ptr[top_ptr]);} int empty () {return (top_ptr == -1);} } // End of template
Running Example - Stacks main () {int top_one; stack stk; ... stk.push (42); stk.push (27); stk.pop (); top_one = stk.top (); ... }
Problem with Classes as Modules • Each class defines exactly one new type • May need to have abstraction which jointly encapsulates two or more types • Example: designing class matrix {…} and class vector {…}
Problem with Classes as Modules • Where to put function for multiplying a vector by a matrix? • May need to have function which needs access to internals • C++ solution: define functions as friend of each class
Modules: SML • Module called structure; interface called signature • Interface specifies what is exported • Interface and structure may have different names • If no structure has no signature, everything exported • Module system quite expressive; we will consider only limited uses
Running Example - Stacks SML: signature Stack_sig = sig type stack val stack_size : int exception STACK_LIMITS
Running Example - Stacks val create : unit -> stack val empty : stack -> bool val push : stack * int -> unit val pop : stack -> unit val top : stack -> int end
Running Example - Stacks structure Stack :> Stack_sig = struct type stack {stack : int Array.array, top_sub : int ref} val stack_size = 100 exception STACK_LIMITS
Running Example - Stacks fun create () = {stack = Array.array(stack_size, 0), top_sub = ref 0} fun empty ({top_sub,...}) = !top_sub < 0
Running Example – Stacks fun push ({stack, top_sub}, element) = if !top_sub < stack_size - 1 then (print "Error--stack overflow"; raise STACK_LIMITS) else (top_sub := !top_sub + 1; Array.update(stack,!top_sub,element))
Running Example – Stacks fun pop (stk as {stack, top_sub}) = if empty stk then (print "Error--stack underflow") else top_sub := !top_sub – 1 fun top (stk as {stack, top_sub}) = Array.sub(stack,!top_sub) end; (* structure *)
Running Example – Stacks SML parameterized modules: signature Stack_input_sig = sig val stack_size : int type element val null_element : element end
Running Example - Stacks signature Generic_Stack_sig = sig type stack type element val stack_size : int exception STACK_LIMITS
Running Example - Stacks val create : unit -> stack val empty : stack -> bool val push : stack * int -> unit val pop : stack -> unit val top : stack -> element end
Running Example - Stacks functor STACK (Stack_input : Stack_input_sig) :> Generic_Stack_sig = struct open Stack_input type stack = {stack : element Array.array, top_sub : int ref} exception STACK_LIMITS
Running Example - Stacks fun create () = {stack=Array.array(stack_size, null_element), top_sub = ref 0} fun empty (Stack {top_sub,...}) = !top_sub < 0 … end; (* functor *)
Running Example - Stacks structure Int_Stack_input = struct val stack_size = 100 type element = int val null_element = 0 end structure IntStack = STACK(Int_Stack_input);