001: package JSci.maths.categories;
002:
003: /**
004: * The Preorder class encapsulates preorders as categories.
005: * @version 1.0
006: * @author Mark Hale
007: */
008: public final class Preorder extends Object implements Category {
009: private final int N;
010:
011: /**
012: * Constructs a preorder category.
013: */
014: public Preorder(int n) {
015: N = n;
016: }
017:
018: /**
019: * Returns the identity morphism for an object.
020: * @param a an Integer.
021: */
022: public Category.Morphism identity(Object a) {
023: return new Relation((Integer) a, (Integer) a);
024: }
025:
026: /**
027: * Returns the cardinality of an object.
028: * @param a an Integer.
029: * @return an Integer.
030: */
031: public Object cardinality(Object a) {
032: return a;
033: }
034:
035: /**
036: * Returns a hom-set.
037: */
038: public Category.HomSet hom(Object a, Object b) {
039: final Integer i = (Integer) a;
040: final Integer j = (Integer) b;
041: if (i.compareTo(j) <= 0)
042: return new RelationSet(i, j);
043: else
044: return new RelationSet();
045: }
046:
047: public Object initial() {
048: return new Integer(0);
049: }
050:
051: public Object terminal() {
052: return new Integer(N - 1);
053: }
054:
055: /**
056: * Returns the ordinal that this category represents.
057: */
058: public int ordinal() {
059: return N;
060: }
061:
062: public class RelationSet implements Category.HomSet {
063: public final Relation morphism;
064:
065: public RelationSet() {
066: morphism = null;
067: }
068:
069: public RelationSet(Integer a, Integer b) {
070: morphism = new Relation(a, b);
071: }
072: }
073:
074: public class Relation implements Category.Morphism {
075: private final Integer from, to;
076:
077: public Relation(Integer a, Integer b) {
078: from = a;
079: to = b;
080: }
081:
082: public Object domain() {
083: return from;
084: }
085:
086: public Object codomain() {
087: return to;
088: }
089:
090: public Object map(Object o) {
091: return to;
092: }
093:
094: public Category.Morphism compose(Category.Morphism m) {
095: if (m instanceof Relation) {
096: Relation r = (Relation) m;
097: if (to.equals(r.from))
098: return new Relation(from, r.to);
099: else
100: throw new UndefinedCompositionException();
101: } else
102: throw new IllegalArgumentException(
103: "Morphism is not a Relation.");
104: }
105: }
106: }
|