6.12.2.1.4. Mutators

Start python section to interscript/felix/model/fincat.py[5 /5 ] Prev
    78: #line 92 "felix_fincat.ipk"
    79:   def add_arrow(self, arrow, src, dst):
    80:     """add new arrow to pre-category,
    81:        raise exception if duplicate or
    82:        domain or codomain not extant objects
    83:     """
    84:     if arrow in self.get_arrows():
    85:       raise 'duplicate arrow in "add arrow"'
    86:     if src not in self.get_objects():
    87:       raise 'src not defined in "add arrow"'
    88:     if dst not in self.get_objects():
    89:       raise 'dst not defined in "add arrow"'
    90: 
    91:     self.arrows[arrow] = (src, dst)
    92:     self.objects[src][0].append(arrow)
    93:     self.objects[dst][1].append(arrow)
    94: 
    95:   def add_object(self, object):
    96:     """add new object to pre-category,
    97:        raise exception iof duplicate
    98:     """
    99:     if object in self.get_arrows():
   100:       raise 'duplicate arrow in "add object"'
   101:     self.objects[object]=([],[])
   102: 
   103:   def add_rule(self, left, right, result):
   104:     """add new rule to composition rules,
   105:        raise exception if arrows not defined or
   106:        cannot be composed or
   107:        the rule is already known
   108: 
   109:     """
   110:     msg = ' in add_rule('+str(left)+', '+str(right)+', '+str(result)+')'
   111:     if left not in self.get_arrows():
   112:       raise 'first argument not an arrow'+msg
   113:     if right not in self.get_arrows():
   114:       raise 'second argument not an arrow'+msg
   115:     if result not in self.get_arrows():
   116:       raise 'result not an arrow'+msg
   117: 
   118:     if self.codomain(left) != self.domain(right):
   119:       raise 'arguments cannot be composed'+msg
   120:     if self.domain(left) != self.domain(result):
   121:       raise 'first argument and result require same domain'+msg
   122:     if self.codomain(right) != self.codomain(result):
   123:       raise 'second argument and result require same codomain'+msg
   124: 
   125:     if left in self.get_objects() or right in self.get_objects():
   126:       raise 'attempted to add (duplicate or conflicting) identity rule'
   127:     pair = (left, right)
   128:     if self.rules.has_key(pair):
   129:       raise 'duplicate composition rule'
   130:     self.rules[pair] = result
   131: 
   132:   def copy(self):
   133:     cat = precategory()
   134:     cat.objects = self.objects.copy()
   135:     cat.arrows = self.arrows.copy()
   136:     cat.rules = self.rules.copy()
   137:     return cat
   138: 
   139:   def extend(self, cat):
   140:     # add all the arrows and rules from another category
   141:     # it is OK to add an arrow that already exists, provided
   142:     # it obeys the same rules
   143: 
   144:     for object in cat.objects:
   145:       if object not in self.objects:
   146:         self.add_object(object)
   147:     for arrow in cat.arrows.keys():
   148:       if arrow not in self.arrows:
   149:         self.add_arrow(arrow)
   150:       elif self.arrows[arrow] != cat.arrow[arrow]:
   151:         raise 'incompatible domain or codomain for arrow in "extend category"'
   152:     for rule in cat.rules.keys():
   153:       if not self.rules.has_key(rule):
   154:         self.rules[rule] = cat.rules[rule]
   155:       elif self.rules[rule] != cat.rules[rule]:
   156:         raise 'incompatible composition rules in "extend category"'
   157: 
   158:   def check_complete(self):
   159:     # check that every composible pair has a composition rule
   160:     # skip identities because they're always correct implicitly
   161:     nonobjects = self.get_nonobjects()
   162:     for arrow in nonobjects:
   163:       dom = self.arrows[arrow][0]
   164:       cod = self.arrows[arrow][1]
   165:       for left in self.objects[dom][1]: # arrows into the domain
   166:         if not self.rules.has_key((left, arrow)):
   167:           raise 'incomplete composition set ('+str(left)+', '+str(arrow)+')'
   168:       for right in self.objects[cod][0]: # arrows out of the codomain
   169:         if not self.rules.has_key((arrow, right)):
   170:           raise 'incomplete composition set ('+str(arrow)+', '+str(right)+')'
   171: 
   172: 
   173:   def check_associative(self):
   174:     for a in self.get_nonobjects():
   175:       for b in self.get_nonobjects():
   176:         for c in self.get_nonobjects():
   177:           if self.can_compose(a,b) and self.can_compose(b,c):
   178:             ab = self.compose(a,b)
   179:             bc = self.compose(b,c)
   180:             if self.compose(ab,c) != self.compose(a,bc):
   181:               raise 'Nonassociative precategory'
   182: 
   183:   def print_everything(self, offset=0, compose_sym = '.'):
   184:     p = ' ' * offset
   185:     print p + 'objects:',self.objects.keys()
   186:     print p + 'arrows:'
   187:     for arrow in self.get_nonobjects():
   188:       dom = self.domain(arrow)
   189:       cod = self.codomain(arrow)
   190:       print p + '  '+str(arrow)+':'+str(dom)+'-->'+str(cod)
   191:     print p+'rules:'
   192:     for pair in self.rules.keys():
   193:       result = self.rules[pair]
   194:       print (p + '  '+str(pair[0])+' '+compose_sym+' '+str(pair[1])+
   195:         ' = '+str(result))
   196: