acsets
Version:
An implementation of acsets in typescript, compatible with ACSets.jl and pyacsets
148 lines (147 loc) • 3.86 kB
JavaScript
/**
This class represents objects in schemas. In an acset, there is a table for
each object in the schema.
*/
export class Ob {
name;
constructor(object) {
this.name = object.name;
}
export() {
return {
name: this.name,
};
}
}
/**
This class represents morphisms in schemas. In an acset, the table corresponding
to an object `x` has a foreign key column for every morphism in the schema that
has a domain (`dom`) of `x`, that has ids that reference rows in the table for
the codomain (`codom`).
*/
export class Hom {
name;
dom;
codom;
constructor(object) {
this.name = object.name;
this.dom = object.dom;
this.codom = object.codom;
}
export() {
return {
name: this.name,
dom: this.dom,
codom: this.codom,
};
}
}
/**
This class represents attribute types in schemas. An attribute type is the "codomain"
of attributes. In an acset, each attrtype is associated with a type. But in general,
acsets are "polymorphic" over the types of their attributes.
*/
export class AttrType {
name;
constructor(object) {
this.name = object.name;
}
export() {
return {
name: this.name,
};
}
}
/**
This class represents attributes in schemas. An attribute corresponds to a
non-foreign-key column in the table for its domain (`dom`).
*/
export class Attr {
name;
dom;
codom;
constructor(object) {
this.name = object.name;
this.dom = object.dom;
this.codom = object.codom;
}
export() {
return {
name: this.name,
dom: this.dom,
codom: this.codom,
};
}
}
/**
We use this version spec to version the serialization format, so that if we
change the serialization format, we can migrate old serializations into new
ones.
*/
export class VersionSpec {
ACSetSchema;
Catlab;
constructor(object) {
this.ACSetSchema = object.ACSetSchema;
this.Catlab = object.Catlab;
}
export() {
return {
ACSetSchema: this.ACSetSchema,
Catlab: this.Catlab,
};
}
}
export class CatlabSchema {
obs;
homs;
attrtypes;
attrs;
version_spec;
constructor(...args) {
if (args.length == 1) {
const object = args[0];
this.obs = object.Ob.map((object) => new Ob(object));
this.homs = object.Hom.map((object) => new Hom(object));
this.attrtypes = object.AttrType.map((object) => new AttrType(object));
this.attrs = object.Attr.map((object) => new Attr(object));
this.version_spec = object.version && new VersionSpec(object.version);
}
else {
this.obs = args[0];
this.homs = args[1];
this.attrtypes = args[2];
this.attrs = args[3];
this.version_spec = args[4];
}
}
export() {
return {
Ob: this.obs.map((x) => x.export()),
Hom: this.homs.map((x) => x.export()),
AttrType: this.attrtypes.map((x) => x.export()),
Attr: this.attrs.map((x) => x.export()),
version: this.version_spec && this.version_spec.export(),
};
}
outgoingHoms(ob) {
return this.homs.filter((f) => f.dom == ob.name);
}
outgoingAttrs(ob) {
return this.attrs.filter((f) => f.dom == ob.name);
}
}
/**
This is a schema for an acset. Every acset needs a schema, to restrict the allowed
operations to ensure consistency.
*/
export class Schema {
name;
schema;
ob_by_name;
constructor(name, schema) {
this.name = name;
this.schema = schema;
this.ob_by_name = new Map(schema.obs.map((ob) => [ob.name, ob]));
}
}