Lecture 3: Object Concepts as Categories Nick Rossiter, Computing Science, Newcastle University, England
Introduction Look at use of categories for application: –Modelling of object information systems Introduce further categorical constructions: –Functors –Cartesian Closed Categories –Natural Transformations
Classes as Categories Classes have –identifiers: represented by a categorical object –attributes: represented by an arrow from the identifier (as source) to each attribute (as target) arrows of type assignment arrows can be monic or epic
Classes as Categories -- Methods –methods: represented by arrows from one or more attributes to another attribute. arrows of type function We define the Category Class in the universal category Set to have the above constructions.
Example Class Category Studentd ependency function StudentIdStudentName {Module Module Title Mark} AverageMark Will structure some of this later as pullback (N:M)
Example Class - Observations StudentId is initial object of category Identifiers/keys are usually initial objects No terminal object as it stands StudentId StudentName is not monic nor epic Module ModuleTitle is monic (1:1)
Example Class - Types The class category Student forms one type for use elsewhere in the system Arrows are also typed e.g. –Module ModuleTitle is monic –StudentId is in the domain available for student identifiers in the discrete category S: s: SStudentId (or s:1 S StudentId) There must be a mapping for the instance s from the discrete category S to StudentId
Constraints: Example of Keys Constraints such as normalization tests can be applied e.g. Boyce-Codd Normal Form (BCNF) For a relation, say R(a,b,c,d), can determine the candidate keys from the given (non- trivial) functional dependencies. Can then test whether every determinant (source of non-trivial functional dependency) is a candidate key
Procedure for Normalization Test 1 Take category CLASS Transform CLASS to POS where: –the objects are the powerset of those in CLASS –the arrows are the orderings by projection (trivial functional dependencies) Create a category DEP holding the non- trivial functional dependencies. Embed POS and DEP in a category PRJ
Procedure for Normalization Test 2 Determine limit of singleton attributes in PRJ (greatest lower bound) if it exists If no limit, determine maximal lower bounds. Assign limit or maximal lower bounds to candidate key.
Example of Normalization Test 1 CLASS: objects a,b,c, arrow a b identity arrows for each object POS: objects powerset of a,b,c (2 3 ) arrows trivial dependencies as projections
Example of Normalization Test 2 PRJ: as POS but with addition of non- trivial dependency ab empty {a} {b}{c} {a,b}{a,c}{b,c} trivial {a,b,c} non-trivial
Example of Normalization Test 3 Let Y = {{a}, {b}, {c}} (singleton attributes) Limit Y = {a,c} (greatest lower bound) Candidate key = {a,c} Determinant = {a} So not BCNF as not every determinant is a candidate key.
Further Category Constructions: The Functor Categories are mapped from one to another by a higher-order function called a Functor. A functor F maps from category A to B by: –assigning each F(a) to B –assigning each F(f) to B where a is an object in A and f is an arrow in A
Functors Preserve Composition F(f 1 ) o F(f 2 ) = F(f 1 o f 2 ) F c= F(c) fhF(f)F(h) ba F(b)F(a) g F(g)
Functors Preserve Identities Functors preserve identities: F(1 a ) = 1 F(a) That is: F (1 a : a a) = 1 F(a) : F(a) F(a)
Functors are Arrows They are composable F : A B G: BC CH = G o F G H BA F
Functors are Associative Their evaluation is associative. The order of evaluation of three or more composed functors does not matter
Scope of Functors Can preserve source category structure in target category: F: A B Full and faithful functor: A,B same structure
Free Functors Can assign additional structures For example assign to each set its powerset (all combinations): POWER: CLASSPOS assigns the powerset of the set, say Y, in CLASS to POS. Free functor
Forgetful Functors Can forget structure For example forget set functions in a Set category. REMOVE_FUNCTIONS: Set Discrete Underlying (Forgetful) functor Returns elements only
Functors for Embedding The functors EMBED1: POSPRJ EMBED2: DEPPRJ embed the arrows of POS and DEP in PRJ. Inclusion functor
Identity Functor Can identify a category 1 A : AA 1 A is identity functor for category A
Relationships between Classes Association: by pullback Student pi l f Student x mark ExamMark pi r g Exam
Semantics of Pullbacks Gives constraints of entity-relationship modelling of relationships: –cardinality of 1:1, 1:N, N:M –mandatory or optional participation –can paste together pullbacks to give n-ary relationships –colimit of diagram includes the link object, the name of which appears as the product suffix
Inheritance Through coproducts: as cone Manager+Employee ManagerEmployee
Inheritance as Commuting Diagram More detailed representation as commuting diagram fManager i l UManager+ gi r Employee Employee Gives unqualified union (U is universal object)i l o f = i r o g
Cartesian Closed Categories Widespread use in computing science A Cartesian closed category has: –a terminal object –products –exponentials (connectivity - no discrete objects)
Examples Cartesian Closed The diagrams for products, coproducts, pullbacks and pushouts are examples of Cartesian closed categories Further example: A A X B X C Bemptyset C has products, terminal object (empty set) and connectivity (no discrete objects)
Object Instantiation The Extension -- the category OBJECT representing the objects associated with each category Functors relate intension to extension: Data: CLASSOBJECT and extension to intension: Name: OBJECT CLASS
Constraints with Functors The functors preserve types, composition, identities and limits of the intension in the extension. P o E = ITYPE E = Name P I CLASSOBJECT E
Information System Queries D : CLASS OBJECT D is database with CLASS as intension and OBJECT as extension Query q on D returns subset of database DS with intension CLASSDS and extesnion OBJECTDS: DS : CLASSDS OBJECTDS
Query DS Query is: q: DS D DS is subset of D Both DS and D are functors
Natural Transformation q is natural transformation as it maps from one functor to another Functors must be of similar type Assigns underlying categorical objects and arrows of one functor to another
Simple View D CLASS OBJECT q CLASSDSOBJECTDS DS
Details of Natural Transformation DS(source(f)) q a D(source(f)) DS(f)D(f) DS(target(f))q b D(target(f)) For each f in CLASSDS, the equation: D(f) o q a = q b o DS(f)must hold
Further Reading Categories for the Working Mathematician Mac Lane, Saunders 2nd edition Springer-Verlag (1998). Prototyping a Categorical Database in P/FDM, Nelson, D A, & Rossiter, B N, Proc. 2nd Int. Workshop Advances in Databases and Information Systems (ADBIS'95), Moscow, June 1995, Springer-Verlag Workshops in Computing, ISBN , (1996).