140 likes | 199 Views
CIA2326: WEEK ??. LECTURE: Introduction to Algebras SUPPORTING NOTES: See chapters 8,9,10 of my ‘online book’ on my homepage TUTORIAL: hand out exercises “ Telescopes are to Astronomy what Computers are to Computer Science” (Edgar Dijstra). Algebras. Q1. What is an algebra?
E N D
CIA2326: WEEK ?? • LECTURE: • Introduction to Algebras • SUPPORTING NOTES: • See chapters 8,9,10 of my ‘online book’ on my homepage • TUTORIAL: hand out exercises • “Telescopes are to Astronomy what Computers are to Computer Science” (Edgar Dijstra)
Algebras • Q1. What is an algebra? • Q2.What has it to do with computing?
Algebras • A1. Roughly, an algebra is A SET OF VALUES + the specification of some OPERATIONS on those values • A2. Roughly, a data type is A SET OF VALUES + the implementation of some OPERATIONS on those values. • Hence we can give a computer-independent meaning to data types using algebras.
Abstraction in Programming • Algebras inform us in both the THEORY and PRACTICE of programming: • Good modularity of software is ensured to a large degree by procedural and data abstractions • Object technologies owe much of their success to the fact that their structure results in a high degree of procedural and data abstraction. • Data abstraction or “abstract data types” abstract away the implementation of data types’ values and operations - just like ALGEBRAS!
Abstract Data Types • A very abstract way of defining data types is as follows: abstract away the implementation of data types’ values and operations in two ways: • define the operations in terms of each other; • don’t represent values AT ALL except in terms of the operations that construct them This give us an ‘abstract algebra’…….
Homogenous algebras: Formal Definition • (A,O) is a Homogenous algebra if • A is a non-empty set which contains values of the algebra and is known as the carrier set. • O is a set of closed, totaloperations defined over the carrier set which may include nullary operations (constants). • EXAMPLE: (Natural Numbers, {“+”}) , • EXAMPLE: ({True,False}, {and,not}) • COUNTER-EX : (Natural Numbers, {“-”}) • COUNTER-EX : (Real Numbers, {“/”})
heterogeneous algebras • (A,O) is a heterogeneous algebra if • A is a SET of carrier sets and • O is a set of closed and total operations over these sets. • The semantics of data types are often given by heterogeneous algebras as we shall see...
Equational Specification of Algebras • The most abstract form of “Presentation” (ie how to define them) is NOT TO GIVE ANY NAMES to their values. • A very abstract way to specify a (family of) Algebras/Data Types is to give an Equational Specification.
Example - Equational Presentation of a Homogenous Algebra • SPEC Boolean • SORT bool • OPS • true : -> bool • false : -> bool • not : bool -> bool • and : bool bool -> bool • AXIOMS: FORALL b : bool • (1) not(true) = false (2) not(false) = true (3) and(true,b) = b • (4) and(b,true) = b (5) and(false,b) = false (6) and(b,false) = false • ENDSPEC
Syntax for Equational Specs of Algebras (ADTs) • SPEC %% Name of Specification • SORT %% ‘Type of interest’ - algebra being defined • OPS %% Signature of operations • FORALL %% Universally defined variables • AXIOMS %% Equations defining MEANING of operations • ENDSPEC
Another Example • SPEC Natural • SORT nat • OPS • zero : -> nat • succ : nat -> nat • add : nat nat -> nat • AXIOMS: FORALL m, n : nat • (1) add(zero, n) = n (2) add(succ(m), n) = succ(add(m, n)) • ENDSPEC
SPEC A_State_Machine • SORT state • OPS • S1 : -> state S2 : -> state • S3 : -> state • a : state -> state b : state -> state • c : state -> state • AXIOMS: FORALL m, n : nat • (1) a(S1) = S3 (6) c(S2) = S1 • (2) b(S1) = S1 (7) a(S3) = S3 • (3) c(S1) = S2 (8) b(S3) = S1 • (4) a(S2) = S2 (9) c(S3) = S3 • (5) b(S2) = S2 • ENDSPEC
Another Example... • SPEC Stack USING Natural + Boolean % Carriers are Stack, Natural • SORT stack % and Boolean • OPS % Type of Interest = Stack • init : -> stack % Signature of each Operation • push : stack nat -> stack • pop : stack -> stack • top : stack -> nat is-empty? : stack -> bool • stack-error : -> stack • nat-error : -> nat • FORALL s : stack, n : nat % universally quantified vars • AXIOMS for is-empty?: • (1) is-empty?(init) = true % this part gives `meaning' • (2) is-empty?(push(s,n)) = false % to each operation AXIOMS for pop: • (3) pop(init) = stack-error • (4) pop(push(s,n)) = s • AXIOMS for top: • (5) top(init) = nat-error • (6) top(push(s,n)) = n ENDSPEC
Conclusions • ALGEBRA gives us an ABSTRACT way to specify DATA TYPES • NEXT week we will examine how to REASON with algebraic expressions.