Download - Formal methods 6 - elements of algebra

Transcript
Page 1: Formal methods   6 - elements of algebra

Formal Methods in Software

Lecture 6. Elements of Algebra

Vlad PatryshevSCU2014

Page 2: Formal methods   6 - elements of algebra

In This Lecture● monoid● group● groupoid● category

Page 3: Formal methods   6 - elements of algebra

MonoidAn object X (e.g. a set, a type in programming language),binary operation Op,nullary operation Zero;Laws:● Op is associative● Zero is neutral re: Op

Page 4: Formal methods   6 - elements of algebra

Monoid: examples● Real numbers ℝ and multiplication; 1. is neutral element● Natural numbers ℕ, max, 0● Sets and union; ∅ is neutral● Lists, concatenation, empty list● Predicates, conjunction, TRUE● Given an X, functions: X → X, their composition, idX as neutral

Page 5: Formal methods   6 - elements of algebra

Mappings of MonoidsGiven monoids (A,opa,za) and (B,opb,zb), define a mapping f that preserves structure:● f: A → B - that is, defined on elements of A, mapping them to B● f(x opa y) = f(x) opb f(y)● f(za) = zb

E.g. ● exp: (ℝ,+,0) → (ℝ,*,1)● sum: List[Int] → (Int,+,0)

Page 6: Formal methods   6 - elements of algebra

Monoid of EndomorphismsDefinition. Endomorphism is a function f:X → X

Endomorphisms form a monoid, ({f:X → X}, ∘, idx)

If X is a finite set, the size is |X||X|, so we denote this monoid XX.

Example: id ā b̄ swap

a a a b b

b b a b a

Page 7: Formal methods   6 - elements of algebra

GroupGroup is a monoid (A,op,0) where each element has an inverse:

∀x∈A ∃y∈A ((x op y) = 0) ∧ ((y op x) = 0)

Notation: y = x-1, or y = inv(x)

E.g.● (ℤ,+,0); inv(x) = -x

Page 8: Formal methods   6 - elements of algebra

Group of IsomorphismsIn monoid ({f:X → X}, ∘, idx) take only such functions that have an inversethey are called isomorphisms.

f is an isomorphism if ∃g:X→X (f∘g = idx) ∧ (g∘f = idx)

It is also known a bijection in the case when X is a set.

Examples: (_+7):ℝ→ℝ; (-3.4*_):ℝ→ℝ

Page 9: Formal methods   6 - elements of algebra

Group of PermutationsTake a set of n elements, {0,1,2,...,n}, and its isomorphic endomorphismsThey are called permutations.

The group of all permutations on n elements is called An.|An| = n!

Sorting n elements amounts to finding the right one out of n! elements.With no extra knowledge, binary search gives an estimate O(log(n!))=O(n log(n))

0 1 2

po p1 p2

(p0,p1,p2)

Page 10: Formal methods   6 - elements of algebra

What if there’s more than one X?Have a bunch of objects, (X1,X2,...,Xn)take all isomorphisms between them:invertible functions of kind f:Xi→XjIt is not a group, and not a monoid: 1. composition is only allowed between f:Xi→Xj and g:Xj→Xk2. no common neutral element, but idi:Xi→Xi

Still, have associativity, neutrality, inverses.It is called Groupoid

Page 11: Formal methods   6 - elements of algebra

Examples of Groupoids● a group is a groupoid● X and Y are objects. Iso(X,X) ∪ Iso(Y,Y) is a

groupoid●● Set A, and an identity on each element - this makes a

discrete groupoid

Page 12: Formal methods   6 - elements of algebra

What If Not Only IsomorphismsHave a bunch of objects, (X1,X2,...,Xn)take functions between them, so that:1. idi:Xi→Xi included, for each i;2. if f:Xi→Xj and g:Xj→Xk are present, so is g∘f:Xi→Xk

Have associativity, have neutrality; inverses optional.It is called Category

Page 13: Formal methods   6 - elements of algebra

Examples of Categories● Every monoid is a category (with just one object)● Every groupoid is a category (all functions invertible)● Category of all sets and their functions● Category of all monoids and their functions● Tiny things, like

○ Category 1 =

○ Category 1+1 =

○ Category 2 =

○ Category 3 =

Page 14: Formal methods   6 - elements of algebra

Java as a CategoryObjects (Xi): all types and classes (Integer, String, java.util.Date, Map<X,Y>)Functions: all imaginable static functions, plus methods, plus identities

E.g.toString: Integer → String

It is not an isomorphism: some strings are not results of toString.But it is an injection; injection is called monomorphism in Category Theory.

Integer.parseInt: String → Integer - not even a function.Either have to restrict to representations of integers, or redefine “function”.

Page 16: Formal methods   6 - elements of algebra