1 package JSci.maths.categories; 2 3 8 public final class Preorder extends Object implements Category { 9 private final int N; 10 13 public Preorder(int n) { 14 N=n; 15 } 16 20 public Category.Morphism identity(Object a) { 21 return new Relation((Integer )a, (Integer )a); 22 } 23 28 public Object cardinality(Object a) { 29 return a; 30 } 31 34 public Category.HomSet hom(Object a,Object b) { 35 final Integer i=(Integer )a; 36 final Integer j=(Integer )b; 37 if(i.compareTo(j)<=0) 38 return new RelationSet(i,j); 39 else 40 return new RelationSet(); 41 } 42 public Object initial() { 43 return new Integer (0); 44 } 45 public Object terminal() { 46 return new Integer (N-1); 47 } 48 51 public int ordinal() { 52 return N; 53 } 54 public class RelationSet implements Category.HomSet { 55 public final Relation morphism; 56 public RelationSet() { 57 morphism=null; 58 } 59 public RelationSet(Integer a,Integer b) { 60 morphism=new Relation(a,b); 61 } 62 } 63 public class Relation implements Category.Morphism { 64 private final Integer from,to; 65 public Relation(Integer a,Integer b) { 66 from=a; 67 to=b; 68 } 69 public Object domain() { 70 return from; 71 } 72 public Object codomain() { 73 return to; 74 } 75 public Object map(Object o) { 76 return to; 77 } 78 public Category.Morphism compose(Category.Morphism m) { 79 if(m instanceof Relation) { 80 Relation r=(Relation)m; 81 if(to.equals(r.from)) 82 return new Relation(from,r.to); 83 else 84 throw new UndefinedCompositionException(); 85 } else 86 throw new IllegalArgumentException ("Morphism is not a Relation."); 87 } 88 } 89 } 90 91 | Popular Tags |