{{ message }}

Instantly share code, notes, and snippets.

# philzook58/intorder.py

Last active May 2, 2020
python category theory monoids, groups, preorders
 class IntOrderCat(): def __init__(self, dom, cod): assert(dom <= cod) self.cod = cod self.dom = dom self.f = () def idd(n): return IntOrderCat(n,n) def compose(f,g): assert( f.dom == g.cod ) return IntOrderCat( g.dom, f.cod ) def __repr__(self): return f"[{self.dom} <= {self.cod}]" # our convention for the order of composition feels counterintuitive here. IntOrderCat(3,5).compose(IntOrderCat(2,3)) # [2 <= 5] IntOrderCat.idd(3) # [3 <= 3]
 class PlusIntMonoid(int): def mplus(self,b): return self + b def mzero(): return 0 class TimesIntMonoid(int): def mplus(self,b): return self * b def mzero(): return 1 class ListMonoid(list): def mplus(self,b): return self + b def mzero(): return [] class UnionMonoid(set): def mplus(self,b): return self.union(b) def mzero(): return set() ListMonoid([1,2]).mplus(ListMonoid([1,2])) # [1,2,1,2] UnionMonoid({1,2}).mplus(UnionMonoid({1,4})) # {1,2,4} TimesIntMonoid(3).mplus(TimesIntMonoid.mzero()) # 3
 class PlusIntCat(int): def compose(self,b): return self + b def idd(): return 0 def dom(self): return () # always return (), the only object def cod(self): return () class TimesIntCat(int): def compose(self,b): return self * b def idd(): return 1 def dom(self): return () def cod(self): return () class ListCat(int): def compose(self,b): return self + b def idd(): return [] def dom(self): return () def cod(self): return () class UnionSetCat(set): def compose(self,b): return self.union(b) def idd(self,b): return set() def dom(self): return () def cod(self): return () PlusIntCat(3).compose(PlusIntCat.idd()) # 3
 class SubSetCat(): def __init__(self,dom,cod): assert( dom.issubset(cod)) self.cod = cod self.dom = dom def compose(f,g): assert(f.dom == g.cod) return SubSetCat(g.dom, f.cod) def idd(s): return SubSetCat(s,s) def __repr__(self): return f"[{self.dom} <= {self.cod}]" SubSetCat( {1,2,3} , {1,2,3,7} ).compose(SubSetCat( {1,2} , {1,2,3} )) # [{1, 2} <= {1, 2, 3, 7}]
 from sympy.combinatorics.free_groups import free_group, vfree_group, xfree_group from sympy.combinatorics.fp_groups import FpGroup, CosetTable, coset_enumeration_r def fp_group_cat(G, catname): # A Category generator that turns a finitely presented group into categorical python class Cat = type(catname, (), vars(G)) def cat_init(self,a): self.f = a Cat.__init__ = cat_init Cat.compose = lambda self,b : G.reduce(self.f * b.f) Cat.dom = lambda : () Cat.cod = lambda : () Cat.idd = lambda : Cat(G.identity) return Cat F, a, b = free_group("a, b") G = FpGroup(F, [a**2, b**3, (a*b)**4]) MyCat = fp_group_cat(G, "MyCat") MyCat(a*a).compose(MyCat.idd()) MyCat.dom()