Computing Fundamentals 2 Lecture 3 Modern Algebra

# Computing Fundamentals 2 Lecture 3 Modern Algebra

Télécharger la présentation

## Computing Fundamentals 2 Lecture 3 Modern Algebra

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Computing Fundamentals 2Lecture 3Modern Algebra Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ patrick.browne@dit.ie Lecture Room K408, Labs A117 Based on Chapter 18. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

2. From the syllabus • Algebraic Structures and Techniques: algebras, theories, models, composition, abstract data types (ADTs), languages for algebraic specification and programming, applications to lists, strings, queues (FIFO), stacks (LIFO), trees, etc. • Algebraic structured need to be specified and implemented. • The CafeOBJ language can be used for specification and implementation .

3. Modern Algebra • In this lecture we look at: • Structure of Algebra • Algebraic Specifications • Algebraic/functional programs • Morphisms: how algebras are related

4. Algebraic Specifications • Algebraic specification is a formal process of defining and refining specifications to develop correct information systems. Traditionally AS was used for the rather narrow task of ‘program specification’ newer AS systems can specify an entire information systems from ontology to database and code. Algebraic specifications can: • 1. Represent mathematical structures and functions over those structures. • 2. Provide an abstraction mechanism free from implementation details such as the size of representations (in memory) and the computational efficiency. • 3. Permit automation due to a limited set of rules ( not over-specified). • 4. Sets the scene for implementation of a prototype.

5. Modern Algebra • Modern Algebra is the study of the structure of sets along with operations on those sets. • An algebra is a model of a theory . • The formulas of a logic are often intended to be statements about some domain of discourse (DOD).Formulas may be interpreted as having a meaning with respect to this DOD by defining which formulas are true statements and which statements are false about DOD. • An interpretation assigns meaning to the operators, constants, and variables of a logic.

6. Model Theory • We distinguish between: • Syntax : of a formal logic/system • Semantics : the interpretation of a formal logic/system • Last year we used truth tables to define operations and to evaluate expressions. • Truth tables provide an interpretation (or meaning or model) for the Boolean expressions. • An interpretation can have two parts: • A fixed meaning, based on operators and constants • A state based meaning, based on values of the variables. • We say a model (an algebra or program) satisfies a theory (a set of axioms). Model Theory

7. Model Theory • Models of computation versus models of the world. Common model assumed Mapping between informal and formal domains

8. Model Theory (Sowa1)

9. Sound and complete. • A logic is sound iff every theorem is valid • A logic is complete iff every valid formula is a theorem.

10. Abstraction Process In model theory, a model must satisfy the axioms of the theory. In general the word model is overloaded (i.e. means more than one thing). It can mean a toy system. A miniature representation of an object. A pattern on which something not yet produced will be based. A design or type. Something serving as an example to be emulated or imitated. In model theory, a model must satisfy the axioms of the theory.

11. What is a model? Jackson(Jackson 1995) (page 121) advises against the mathematical use of the term model, where it is not applicable. Some mathematicians, logicians and computer scientists consider the real world as a model for their theories. But their conceptual framework is a world of Platonic abstraction removed from the world of real systems. He states: “How much faith would you place in a cartographer who told you that the world is a model for his map?”. If we contrast this with Frank (Frank 2003) statement: "Technically, the world and the information systems are then models for the abstract behaviour described algebraically".

12. Modern Algebra • Modern Algebra is the study of the structure of sets along with operations on those sets. • An algebra is a model of a theory. • One Boolean algebra is the standard model (?) for the propositional calculus. To see CafeOBJ’s BOOL module, type: show module BOOL • We have studied quantification for commutative operations. (Chapter 8 last year). Another name for these operations and their sets is commutative or Abelian monoids. We also studied associative operations.

13. Structure of algebras • An algebra consists of two components: • A set S of elements, called the carrier of the algebra. • Operators defined on the carrier set. • Each operator is a total function Sm->S, where m is the arity of the operator, e.g. from River-Crossing op change : Side -> Side . • Constants are considered as operators with an arity of null (nullary operators), e.g. from River-Crossing op right : -> Side

14. Structure of algebras • An algebra has more structure than a set, i.e. the behaviour of the operations. • A function has an arity • Number and sort* of arguments • Zero arity represent a constant • The rank is the number of elements in the arity. • Co-arity: Sort of result • Profile: the arity and coarity

15. Structure of algebras • Some example operations from CafeOBJ: • if condition then c else d fi • Is a ternaryfunction can be used in CafeOBJ as follows1: • eq {X} \ S = if (X in S) then {} else {X} fi . • Operators can be written in various forms prefix +(a,c) or infix a + b., or mixfix as above if_then_else_fi. In CafeOBJ underbars act as place holders.

16. BOOL + NAT example • open BOOL + NAT • red if true then true else false fi . • red if true then 6 else 7 fi . • red if true then false else 7 fi . ERROR?

17. Specification of Natural Numbers module SIMPLE-NAT { [ NzNat Zero < Nat ] op 0 : -> Zero op s_ : Nat -> NzNat op _+_ : Nat Nat -> Nat {assoc } vars M N : Nat eq 0 + N = N . eq N + s M = s(N + M) . }

18. Specification of Natural Numbers

19. Using the reduce (red) command • At operational level CafeOBJ's pattern matching modulo associativity or commutativity. For example, if we define op _+_ : Nat Nat -> Nat {comm} vars M N : Nat eq M + 0 = M . eq M + s N = s(M + N). • The first equation can match 0 + (s s 0) and the second can match (s x) + y, returning s s 0 and s(x + y), respectively. Matching modulo associativity and commutativity provides very powerful pattern matching.

20. Structure of algebras • Some example operations from CafeOBJ: • 1 is function that takes no arguments and always returns the value 1. • ops 1 2 3 4 5 6 7 8 9 : -> NzNat • Algebras are models and are related by means of homomorphisms, that is, maps that preserve the algebra structure. A homomorphism between algebras is analogous to a function between sets.

21. Examples of Algebras • The set of even numbers and the operator +. As the carrier set is infinite the algebra is called and infinite algebra. • The set of even numbers and the operations multiplication and division is not an algebra because division is not a total function (division by 0) . • In general <S, > denotes an algebra with carrier S and a list of operations ,  (PHI). • The set {true,false} and operators ,, and ⌐ is a finite algebra. Written <Bool,,, ⌐ >

22. Algebras & CafeOBJ • An algebra consists of two components: • 1) A setS of elements, called the carrier of the algebra. These are called sorts in CafeOBJ. The syntax is [SortList]. The sorts in CafeOBJ are ordered by a subsort relation (<). Hence CafeOBJ can represent Order Sorted Algebras (OSA).

23. Algebras & CafeOBJ • An algebra consists of two components: • 2)Operators defined on the carrier set S. Each operator is a total function Sm->S, where m is the arity of the operator. Constants are considered as operators with an arity of null (nullary operators). Operations and constants in CafeOBJ are represented with the op keyword. The operations are defined in the axiom section with the keywords eq or ceq.

24. Order Sorted Algebra (OSA)1 • There are many examples where all items of one sort are necessarily also items of some other sort. Every natural number () is an integer (), and every integer is a rational (). We may write this symbolically • Natural ≤ Integer ≤ Rational • In CafeOBJ this is written < but means≤. • [ Natural <Integer < Rational ] • Associated with each sort name is a meaning, i.e. semantic denotation, the sub-sort relations appear as set-theoretic inclusion. • 

25. Order Sorted Algebra (OSA)1 • An OSA provides rigorous model theoretic semantics for all the following features: • Allows a partial ordering relation on the set of sorts. • Supports abstract data types with multiple inheritance roughly in the sense of object oriented programming • Allows several forms of polymorphism sub-sort, parametric, and ad-hoc (overloading e.g. +) • Allows partial operations as total on equationally defined “error supersorts”2.

26. Polymorphism in OSA • Ad hoc in its strongest sense indicates semantically unrelated uses, such as + for both integer addition and Boolean disjunction. Even in this case, both instances of + are associative, commutative, and have an identity element. The operations have similar properties. Ad-hoc polymorphism allows the same function name to be used for different operations on different types.

27. Polymorphism in OSA • Multiple representation. In this cases the uses are related semantically, but their representations may be different. Multiple representation polymorphism is less ad-hoc than ad-hoc polymorphism, for example Polar and Cartesian coordinates.

28. Polymorphism in OSA • Parametric polymorphism, such as the map function. Map must behave the same regardless the types or operations used. Another example is Stack. • Ad hoc polymorphism allows different semantic functions and structures to use the same symbol name in different ways. • Subsort polymorphism where the different instances of an operation symbol are related by inheritance (interpreted as subset inclusion) such that the result does not depend on the instance used, as with + for natural, integer, and rational numbers.

29. Parametric polymorphism • Parametric polymorphism allows a set of different sorts to use a single definition of a function or a structure. -- Single definition of list mod! LIST(T :: TRIV) { [ Elt.T < List ] op nil : -> List op __ : Elt.T List -> List } -- List can be used by different sorts red in LIST(INT) : 1 2 3 . red in LIST(CHARACTER) : 'A' 'B' 'C' .

30. Ad hoc polymorphism • Ad hoc polymorphism (AHP), or overloading, allows different unrelated functions to share the same name. The operations are disambiguated through the use of the sorts in each declaration. AHP is a mechanism for name reuse independently of any semantic relationship among names, e.g. “push a door” is a different use of “push” than “push element on stack”. Example on next slide.

31. Ad hoc polymorphism -- The operation ++ doubles both arguments then adds mod! DOUBLE-ADD { Pr(INT) op _++_ : Int Int -> Int vars X Y : Int eq X ++ Y = (X * 2) + (Y * 2) } -- CafeOBJ uses ++ to concatenate strings -- to check this type show module STRING in CafeOBJ -- ++ can be used by different unrelated sorts red in DOUBLE-ADD : 1 ++ 3 . red in STRING : “Ellen” ++ “Pat” .

32. Sub-sort Polymorphism • Sub-sort polymorphism is a form of operator overloading that is consistent under sub-sort restriction. Operations defined in a super-sort can be used or redefined in the sub-sort. See example in next slide.

33. Sub-sort Polymorphism mod ADD2 { pr(FLOAT) pr(INT) -- Construct a sort hierarchy -- This will identify the correct arguments for untyped digits and variables [Int < Float] vars i1 i2 : Int vars f1 f2 : Float op _++_ : Float Float -> Float op _++_ : Int Int -> Int op _++_ : Int Float -> Float op _++_ : Float Int -> Float } open ADD2 -- If we use a sort hierarchy then OSA will ensure the correct sort on un-annotated digits red 1 ++ 2 . **> gives Int red 1 ++ 2.8 . **> gives Float red 1.1 ++ 2 . **> gives Float red 1.1 ++ 2.8 . **> gives Float

34. OSA in CafeOBJ Can be writen as: [ s4 < s2 s3 < s1] [ s5 < s1 ]

35. Basic CafeOBJ module <ModId> { signature { -- start of signature section [ <SortId> ] -- sort declaration op <OpForm> : <SortList> -> <Sort> -- operation declaration op <ConstName> : -> <Sort> -- constants are nullary operations. } axioms { var <VarId> : <Sort> -- variable declaration eq <Term> = <Term> . -- ordinary equation ceq <Term> = <Term> if <Condition> . -- conditional equation -- Or transitions }}

36. CafeOBJ evaluation module SIMPLE-NAT { [ Zero NzNat < Nat ] signature { op 0 : -> Zero op s : Nat -> NzNat op _+_ : Nat Nat -> Nat { comm, assoc } } axioms { var N : Nat var M : Nat eq [0] : 0 + N = N . eq [1] : N + s(M) = s(N + M) . }} red s(s(0)) + s(s(s(0))) Result: s(s(s(s(s(0)))))

37. CafeOBJ evaluation

38. CafeOBJ evaluation eq [0] : 0 + N = N . eq [1] : N + s(M) = s(N + M) s(s(0)) + s(s(s(0))) s(s(s(s(s(0))))) start eq[1] eq[1] eq [0] end Shows the process of term rewriting when the goal term is “s(s(0)) + s(s(s(0)))“ giving “s(s(s(s(s(0)))))" (2+3=5) Authors T. Ogawa and J. Tanaka

39. Signature • The signature of an algebra consists of the name of its carrier set and a list of types of its operations. signature { [ NzNat Zero < Nat ] op 0 : -> Zero op s_ : Nat -> NzNat op _+_ : Nat Nat -> Nat }

40. Signature Maths Notation: <B, B x B -> B, B x B -> B, B -> B> CafeOBJ notation. signature { [ true false ] op _ and _ : Bool Bool -> Bool op _ or _ : Bool Bool -> Bool op not _ : Bool -> Bool }

41. Signature • Two algebras have the same signature if they have the same number of operators and the corresponding operators have the same types modulo the name of the carrier. If we consider a signature as a sequence then: • <Bool,,, ⌐> : Boolean Algebra • <S,,,  > : Set Theory • Have the same signature. While • <S,,,> • Does not have the same sig. (look at arities)

42. Properties (attributes) of operations • Properties of certain constants wrt. some operation  : • Identity (Unit): 1  b = b, b  1 = b • Zero: 0  b = 0 , b  0 = 0 • Inverse: b  RightInv = 1, LeftInv  b = 1 • (18.5) Theorem: Let 1 be the identity of binary operator  on S. Then b has a right inverse c wrt.  and c has a left inverse b wrt. if b  c = 1. • Elements b and c are called inverses of each other if b  c = c  b = 1 • E.g. b=3, c=1: 1x3=3, 3x1=3. identity 0x3=0, 3x0=0, zero 3x(1/3) = 1, (1/3)x3 = 1, inverse

43. Examples of inverses • In algebra of integers <,+>, 0 is an identity, for every b and there exists an inverse –b (where  means +-Int). • In the algebra of reals <,>, 1 is an identity, every element b (except 0) has an inverse ( real numbers). • In general every one-to-one and onto function has an inverse. (18.6) Theorem: Let li be a left inverse and ri be a right inverse of element b with respect to operator  . Then li=ri and b has unique inverse. In other words, if an operation has both a left and a right inverse then they are the same. We can check left and right inverse by showing • li  b = unit and b  ri = unit.

44. Subalgebras • A is a carrier set of some algebra. B is a subset of A. We say B is closedwrt an operation () if the operation returns a value in B when its arguments are from B. A B Operation 

45. Subalgebras • Algebras are differentiated by their structure. Structure refers to properties like the existence of an identity, but also refers to the kinds of sub-algebras an algebra has. • (18.7) Definition: A subset T of a set S is closed under an operator if applying the operator to elements of T always produces a result in T

46. Example: Closed sub-algebras • The set of even integers under + isclosedbecause the sum of two even integers is an even integer. • Subset T={0,1} of the integers is not closed under + because 1+1 is not in T. • Subset T={0,1} of the integers is closed under max(x,y) because the max of any two is one of them.

47. Sub-algebra • A sub-algebra satisfies all the properties of an algebra: T is non-empty and T is closed under operations. Note we can ‘overload’ function names e.g. f over T may be distinct from f over S (e.g. + for  and + for ). • Algebra <,+> is a sub-algebra of <,+> because   and  is closed under + • Algebra <{1,0},+> is not a sub-algebra of <,+> because {0,1} is not closed under + • Any algebra is a sub-algebra of itself.

48. Mappings (morphisms) between algebras • Consider the function h:->+that assigns every a to its absolute value |a| in +, the set of non-negative real numbers. Note, the function h relates carrier sets (sorts in CafeOBJ, types in C), h has the property that it is compatible with (or preserves) the multiplicative structure of  because: [eq1] |a * b| = |a| * |b| where a,b  • We get the same result on either side of [eq1]. h is a mapping between the carrier sets of two algebras, while * is an operation defined onboth algebras. LHS * must handle a,  b RHS does not.

49. Morphisms • Previously we looked at the internal structure of algebras. Now we consider relating and structuring algebras (e.g. CafeOBJ’s imports and parameters, MDA1). When combing algebras, we need ways to characterize structural similarities between algebras. • Do they look the ‘same’? • Could they be mapped to each other? • Three types of morphism: • Isomorphism (equal shape) • Automorphism (self shape, not covered here) • Homomorphism (same shape, structure-preserving)