CSC 530 Lecture Notes Week 3, Part 2
Discussion of Assignments 1 and 2
More on Type Theory
( var-name data-value )
( function-name formal-parms function-body )
where the stack-store is further subdivided into sub-alists, one per each active function invocation.( ( state-store ) ( environment ) ( stack-store ) )
(setq x 1) (setq y 2) (defun f(y) (g)) (defun g() (+ x y))
the type binding (x int) would be put in the environment and the value binding (x 10) would be put in the state-store.(xdefvar x int 10)
Now on to Further Discussion of Type Theory
definition module Stack; type Stack; procedure Push(var s: Stack; elem: integer); procedure Pop(var s: Stack): integer; procedure Peek(s: Stack): integer; end Stack. implementation module Stack; const Size = 100; type Stack = array[1..Size] of integer; var curtop: integer; (* ... implementations of Push, Pop, and Peek *) end Stack. (* program *) module TestIntStack; import Stack; var s: Stack; i: integer; begin Stack.Push(s, 1); i := Stack.Pop(s); end TestIntStack.
wherein the module name is used to access the function, and an explicit value of the abstract type is the first actual parameter.Stack.Push(s, 1); i := Stack.Pop(s);
class Stack { public: void Push(int elem); int Pop(); int Peek(); protected: const int Size = 100; int curtop; int body[Size]; }; /* ... implementations of Push, Pop, and Peek */ main() { Stack s; int i; s.Push(1); i = s.Pop(); }
wherein a value (a.k.a. object) of the class type is used to access the function, which value serves also as an (implicit) actual parameter.s.Push(1); i := s.Pop();
class Stack { public: Stack(); void push(int elem); void pop(); int top(); equations: pop(Stack()) = null; pop(s.push(e)) = s; top(Stack()) = null; top(s.push(e)) = e; }; /* NO implementations of Push, Pop, and Peek */ main() { Stack s; int i; s = s.Push(1); i = s.Pop(); }
s = s.Push(1); i = s.Pop();
(* A generic stack module. *) definition module Stack(Size: integer, ElemType: type); type Stack; procedure Push(var S: Stack; Elem: ElemType); procedure Pop(var S: Stack): ElemType: procedure Peek(S: Stack): ElemType; end Stack. implementation module Stack; type Stack = array[1..Size] of ElemType; var curtop: integer; (* ... implementations of Push, Pop, and Peek *) end Stack; (* program *) module TestIntStack; import Stack(100, integer); var S: Stack; i: integer; begin Stack.Push(S, 1); i := Stack.Pop(S); end TestIntStack; (* program *) module TestThreeStacks; import Stack(100, integer) as IntStack100; import Stack(200, integer) as IntStack200; import Stack(200, real) as RealStack200;
var SI100: IntStack100.Stack; (* A 100-elem integer stack *) var SI200: IntStack200.Stack; (* A 200-elem integer stack *) var SR200: RealStack200.Stack; (* A 200-elem real stack *)
begin IntStack100.Push(SI100, 1); (* push a value on SI100 *) IntStack200.Push(SI200, 2); (* push a value on SI200 *) RealStack200.Push(SR200, 2.5); (* push a value on SR200 *) (* etc. ... *) end TestThreeStacks;