libpetri 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -635,69 +635,235 @@ function findForwardInputs(out) {
635
635
  }
636
636
  }
637
637
 
638
- // src/core/compose-bindings.ts
639
- var COMPOSE_BINDINGS_KEY = /* @__PURE__ */ Symbol("ComposeBindings.internal");
640
- var ComposeBindings = class {
641
- _portBindings = /* @__PURE__ */ new Map();
642
- _channelBindings = /* @__PURE__ */ new Map();
638
+ // src/core/interface.ts
639
+ var INTERFACE_KEY = /* @__PURE__ */ Symbol("Interface.internal");
640
+ var Interface = class {
641
+ ports;
642
+ channels;
643
+ /** @internal Use {@link Interface.builder} to create instances. */
644
+ constructor(key, ports, channels) {
645
+ if (key !== INTERFACE_KEY) throw new Error("Use Interface.builder() to create instances");
646
+ this.ports = ports;
647
+ this.channels = channels;
648
+ }
649
+ /** Looks up a port by name. Returns undefined when absent. */
650
+ port(name) {
651
+ return this.ports.get(name);
652
+ }
653
+ /** Looks up a channel by name. Returns undefined when absent. */
654
+ channel(name) {
655
+ return this.channels.get(name);
656
+ }
643
657
  /**
644
- * @internal Use {@link PetriNetBuilder.compose} instances are produced
645
- * by the host builder and supplied to the caller's callback.
658
+ * Looks up a port by name and returns its underlying place, narrowed to
659
+ * `Place<T>`. Returns undefined when the port does not exist.
660
+ *
661
+ * Note: TypeScript erases generics at runtime, so unlike Java's
662
+ * `portPlaceAs(name, Class<T>)` this method cannot verify the token type at
663
+ * runtime — the caller is trusted to supply the correct `T`. Per **MOD-022**,
664
+ * type compatibility is enforced at compile time only in TypeScript.
646
665
  */
647
- constructor(key) {
648
- if (key !== COMPOSE_BINDINGS_KEY) {
649
- throw new Error(
650
- "Use PetriNetBuilder.compose(instance, b => ...) \u2014 ComposeBindings is not directly constructible"
651
- );
666
+ placeAs(name) {
667
+ const p = this.ports.get(name);
668
+ if (p === void 0) return void 0;
669
+ return p.place;
670
+ }
671
+ static builder() {
672
+ return new InterfaceBuilder();
673
+ }
674
+ };
675
+ var InterfaceBuilder = class {
676
+ _ports = /* @__PURE__ */ new Map();
677
+ _channels = /* @__PURE__ */ new Map();
678
+ /** Add a pre-built port (rejects duplicate names). */
679
+ port(port) {
680
+ if (this._ports.has(port.name)) {
681
+ throw new Error(`Duplicate port name: '${port.name}'`);
682
+ }
683
+ this._ports.set(port.name, port);
684
+ return this;
685
+ }
686
+ /** Add an input port (advisory direction). */
687
+ inputPort(name, place2) {
688
+ return this.port({ name, direction: "input", place: place2 });
689
+ }
690
+ /** Add an output port (advisory direction). */
691
+ outputPort(name, place2) {
692
+ return this.port({ name, direction: "output", place: place2 });
693
+ }
694
+ /** Add an in-out port (advisory direction). */
695
+ inoutPort(name, place2) {
696
+ return this.port({ name, direction: "inout", place: place2 });
697
+ }
698
+ channel(channelOrName, transition) {
699
+ const ch = typeof channelOrName === "string" ? { name: channelOrName, transition } : channelOrName;
700
+ if (this._channels.has(ch.name)) {
701
+ throw new Error(`Duplicate channel name: '${ch.name}'`);
702
+ }
703
+ this._channels.set(ch.name, ch);
704
+ return this;
705
+ }
706
+ /** @internal Bulk-add ports already validated by the caller. */
707
+ portsAll(ports) {
708
+ for (const p of ports) this.port(p);
709
+ return this;
710
+ }
711
+ /** @internal Bulk-add channels already validated by the caller. */
712
+ channelsAll(channels) {
713
+ for (const c of channels) this.channel(c);
714
+ return this;
715
+ }
716
+ build() {
717
+ return new Interface(
718
+ INTERFACE_KEY,
719
+ new Map(this._ports),
720
+ new Map(this._channels)
721
+ );
722
+ }
723
+ };
724
+
725
+ // src/core/instance.ts
726
+ var INSTANCE_KEY = /* @__PURE__ */ Symbol("Instance.internal");
727
+ var Instance = class {
728
+ prefix;
729
+ def;
730
+ renamedBody;
731
+ portHandles;
732
+ channelHandles;
733
+ params;
734
+ /**
735
+ * @internal Use {@link SubnetDef.instantiate} (or the internal factory
736
+ * {@link __createInstance}) to create instances.
737
+ */
738
+ constructor(key, prefix, def, renamedBody, portHandles, channelHandles, params) {
739
+ if (key !== INSTANCE_KEY) {
740
+ throw new Error("Use SubnetDef.instantiate() to create Instance values");
652
741
  }
742
+ this.prefix = prefix;
743
+ this.def = def;
744
+ this.renamedBody = renamedBody;
745
+ this.portHandles = portHandles;
746
+ this.channelHandles = channelHandles;
747
+ this.params = params;
653
748
  }
654
749
  /**
655
- * Binds the named interface port to the given caller place per **MOD-020**.
750
+ * Returns the renamed {@link Place} corresponding to the named port.
656
751
  *
657
- * The port name is the **original** (pre-prefix) name declared in the
658
- * subnet's `Interface`. The typed `<T>` parameter ensures the caller
659
- * place's token type matches the interface port's token type at compile
660
- * time per [MOD-022]; TypeScript does not validate the type at runtime
661
- * because generics are erased.
752
+ * The port name is the **original** (pre-prefix) name as declared in the
753
+ * subnet's `Interface`. Per **MOD-022**, TypeScript enforces token-type
754
+ * compatibility at compile time only; this method does not validate `T`
755
+ * at runtime. A missing name raises an `Error`.
662
756
  *
663
- * @throws when `portName` is already bound on this builder
757
+ * @throws when the port name is unknown
664
758
  */
665
- bindPort(portName, callerPlace) {
666
- if (this._portBindings.has(portName)) {
667
- throw new Error(`Port '${portName}' is already bound`);
759
+ port(name) {
760
+ const p = this.portHandles.get(name);
761
+ if (p === void 0) {
762
+ throw new Error(`No port named '${name}' in instance '${this.prefix}'`);
668
763
  }
669
- this._portBindings.set(portName, callerPlace);
670
- return this;
764
+ return p;
671
765
  }
672
766
  /**
673
- * Records a synchronous channel binding per **MOD-021**: at compose time,
674
- * the instance-side renamed channel transition is merged with
675
- * `callerTransition` into a single transition in the resulting flat net.
676
- *
677
- * **Status**: channel composition is implemented in task #13. Recording a
678
- * channel binding here is permitted, but `compose(...)` currently raises
679
- * an `Error` when any channel binding is present.
767
+ * Returns the renamed {@link Transition} corresponding to the named channel.
680
768
  *
681
- * @throws when `channelName` is already bound on this builder
769
+ * @throws when the channel name is unknown
682
770
  */
683
- bindChannel(channelName, callerTransition) {
684
- if (this._channelBindings.has(channelName)) {
685
- throw new Error(`Channel '${channelName}' is already bound`);
771
+ channel(name) {
772
+ const t = this.channelHandles.get(name);
773
+ if (t === void 0) {
774
+ throw new Error(`No channel named '${name}' in instance '${this.prefix}'`);
686
775
  }
687
- this._channelBindings.set(channelName, callerTransition);
688
- return this;
776
+ return t;
689
777
  }
690
- /** Returns an unmodifiable view of the recorded port bindings. */
691
- portBindings() {
692
- return this._portBindings;
778
+ /**
779
+ * Returns the debug-UI descriptor for this instance per **MOD-041**.
780
+ */
781
+ descriptor() {
782
+ const transitions = [];
783
+ for (const t of this.renamedBody.transitions) transitions.push(t.name);
784
+ const exposedPlaces = [];
785
+ for (const p of this.portHandles.values()) exposedPlaces.push(p.name);
786
+ return {
787
+ prefix: this.prefix,
788
+ defName: this.def.name,
789
+ transitions,
790
+ exposedPlaces,
791
+ params: this.params,
792
+ parentPrefix: null
793
+ };
693
794
  }
694
- /** Returns an unmodifiable view of the recorded channel bindings. */
695
- channelBindings() {
696
- return this._channelBindings;
795
+ /**
796
+ * Produces a derived instance whose specified transitions (named by their
797
+ * **original**, pre-prefix names) carry the supplied actions per **MOD-030**.
798
+ *
799
+ * For each entry `[originalName, action]`, the renamed body is searched for
800
+ * the transition whose name equals `prefix + "/" + originalName`. The
801
+ * resulting instance shares the original `def`, `prefix`, `params`, and
802
+ * port/channel handle topology — only the renamed body is rebuilt with new
803
+ * actions. Per **MOD-030**, calling `bindActions` on one instance does NOT
804
+ * affect the actions held by other instances of the same `def`.
805
+ *
806
+ * Unrecognised original names raise an `Error` so typos surface eagerly.
807
+ */
808
+ bindActions(actionsByOriginalName) {
809
+ const prefixedByName = /* @__PURE__ */ new Map();
810
+ const renamedNames = /* @__PURE__ */ new Set();
811
+ for (const t of this.renamedBody.transitions) {
812
+ renamedNames.add(t.name);
813
+ }
814
+ for (const originalName of Object.keys(actionsByOriginalName)) {
815
+ const prefixed = this.prefix + "/" + originalName;
816
+ if (!renamedNames.has(prefixed)) {
817
+ throw new Error(
818
+ `Instance.bindActions: no transition '${originalName}' (resolved as '${prefixed}') in instance '${this.prefix}' of subnet '${this.def.name}'`
819
+ );
820
+ }
821
+ prefixedByName.set(prefixed, actionsByOriginalName[originalName]);
822
+ }
823
+ const reboundBody = this.renamedBody.bindActionsWithResolver((name) => {
824
+ const action = prefixedByName.get(name);
825
+ if (action !== void 0) return action;
826
+ for (const t of this.renamedBody.transitions) {
827
+ if (t.name === name) return t.action;
828
+ }
829
+ throw new Error(`Instance.bindActions: resolver invoked with unknown name '${name}'`);
830
+ });
831
+ const reboundChannelHandles = /* @__PURE__ */ new Map();
832
+ if (this.channelHandles.size > 0) {
833
+ const byName = /* @__PURE__ */ new Map();
834
+ for (const t of reboundBody.transitions) {
835
+ byName.set(t.name, t);
836
+ }
837
+ for (const [name, oldT] of this.channelHandles) {
838
+ const refreshed = byName.get(oldT.name);
839
+ if (refreshed === void 0) {
840
+ throw new Error(
841
+ `Instance.bindActions: channel '${name}' transition '${oldT.name}' not found in rebound body`
842
+ );
843
+ }
844
+ reboundChannelHandles.set(name, refreshed);
845
+ }
846
+ }
847
+ return __createInstance(
848
+ this.prefix,
849
+ this.def,
850
+ reboundBody,
851
+ this.portHandles,
852
+ reboundChannelHandles,
853
+ this.params
854
+ );
697
855
  }
698
856
  };
699
- function __createComposeBindings() {
700
- return new ComposeBindings(COMPOSE_BINDINGS_KEY);
857
+ function __createInstance(prefix, def, renamedBody, portHandles, channelHandles, params) {
858
+ return new Instance(
859
+ INSTANCE_KEY,
860
+ prefix,
861
+ def,
862
+ renamedBody,
863
+ portHandles,
864
+ channelHandles,
865
+ params
866
+ );
701
867
  }
702
868
 
703
869
  // src/core/internal/subnet-rewriter.ts
@@ -958,1043 +1124,946 @@ function applyFusion(transitions, fusionMap) {
958
1124
  return rewritten;
959
1125
  }
960
1126
 
961
- // src/core/fusion-set.ts
962
- var FUSION_SET_KEY = /* @__PURE__ */ Symbol("FusionSet.internal");
963
- var FusionSet = class _FusionSet {
1127
+ // src/verification/verification-harness.ts
1128
+ function buildVerificationResult(syntheticNet, perProperty) {
1129
+ const frozen = new Map(perProperty);
1130
+ return {
1131
+ syntheticNet,
1132
+ perProperty: frozen,
1133
+ allProven() {
1134
+ for (const r of frozen.values()) {
1135
+ if (!isProven(r)) return false;
1136
+ }
1137
+ return true;
1138
+ },
1139
+ anyViolated() {
1140
+ for (const r of frozen.values()) {
1141
+ if (isViolated(r)) return true;
1142
+ }
1143
+ return false;
1144
+ }
1145
+ };
1146
+ }
1147
+ function normaliseGenerators(generators) {
1148
+ if (generators instanceof Map) return new Map(generators);
1149
+ return new Map(Object.entries(generators));
1150
+ }
1151
+ function normaliseProperties(properties) {
1152
+ if (Array.isArray(properties)) return [...properties];
1153
+ return [...properties];
1154
+ }
1155
+
1156
+ // src/core/subnet-def.ts
1157
+ var SUBNET_DEF_KEY = /* @__PURE__ */ Symbol("SubnetDef.internal");
1158
+ var SubnetDef = class _SubnetDef {
964
1159
  name;
965
- members;
966
- /** @internal Use {@link FusionSet.builder} or {@link FusionSet.of} to create instances. */
967
- constructor(key, name, members) {
968
- if (key !== FUSION_SET_KEY) {
969
- throw new Error("Use FusionSet.builder() or FusionSet.of() to create instances");
1160
+ body;
1161
+ iface;
1162
+ /** @internal Use {@link SubnetDef.builder} or {@link SubnetDef.fromNet} to create instances. */
1163
+ constructor(key, name, body, iface) {
1164
+ if (key !== SUBNET_DEF_KEY) {
1165
+ throw new Error("Use SubnetDef.builder() or SubnetDef.fromNet() to create instances");
970
1166
  }
971
1167
  this.name = name;
972
- this.members = members;
973
- }
974
- /**
975
- * Returns the canonical member — by convention, the first declared member.
976
- * The canonical place's identity survives into the resulting flat net.
977
- */
978
- get canonical() {
979
- return this.members[0];
1168
+ this.body = body;
1169
+ this.iface = iface;
980
1170
  }
981
1171
  /**
982
- * Returns all members **except** the canonical member, in declaration order.
983
- * These are the places that get substituted away at
984
- * {@link import('./petri-net.js').PetriNetBuilder.build} time.
1172
+ * Produces a renamed module instance per **MOD-010**, **MOD-011**, **MOD-012**,
1173
+ * and **MOD-030**.
985
1174
  *
986
- * For a single-member (degenerate) set, returns an empty array.
987
- */
988
- nonCanonical() {
989
- if (this.members.length <= 1) return [];
990
- return this.members.slice(1);
991
- }
992
- toString() {
993
- return `FusionSet[${this.name}, canonical=${this.canonical.name}, members=${this.members.length}]`;
994
- }
995
- // ============================================================
996
- // Static factories
997
- // ============================================================
998
- /** Returns a fresh {@link FusionSetBuilder} with the given human-readable name. */
999
- static builder(name) {
1000
- return new FusionSetBuilder(name);
1001
- }
1002
- /**
1003
- * Convenience factory: builds a fusion set whose first member is `first`
1004
- * (the canonical) and whose remaining members are `rest`, all sharing the
1005
- * type `<T>`.
1175
+ * The rename pass walks every place and transition of the body net,
1176
+ * substituting each name with `prefix + "/" + originalName`, and rebuilds
1177
+ * every arc with rewritten place references. Transition timing, priority,
1178
+ * and action are carried through by reference (action sharing per
1179
+ * [MOD-030]). The renamed body is itself a structurally valid `PetriNet`
1180
+ * per [CORE-040]; per-instance state isolation per [MOD-012] is a
1181
+ * structural consequence of distinct prefixed names.
1006
1182
  *
1007
- * The varargs form ensures static-type homogeneity at the call site (the
1008
- * TypeScript compiler checks every `rest` entry is a `Place<T>`).
1009
- */
1010
- static of(name, first, ...rest) {
1011
- const members = [first];
1012
- for (const p of rest) members.push(p);
1013
- return new _FusionSet(FUSION_SET_KEY, name, Object.freeze(members));
1014
- }
1015
- };
1016
- var FusionSetBuilder = class {
1017
- _name;
1018
- _members = [];
1019
- constructor(name) {
1020
- if (typeof name !== "string" || name.length === 0) {
1021
- throw new Error("FusionSet.builder: name must be a non-empty string");
1022
- }
1023
- this._name = name;
1024
- }
1025
- /**
1026
- * Appends a member to the fusion set. The first member becomes the canonical
1027
- * place per the convention documented on {@link FusionSet}.
1183
+ * ## Prefix validation
1028
1184
  *
1029
- * The `<T>` parameter is for compile-time guidance only: callers writing
1030
- * typed code at the same call site benefit from the compiler checking
1031
- * `Place<T>` at each member.
1032
- */
1033
- member(place2) {
1034
- if (place2 === void 0 || place2 === null) {
1035
- throw new Error(`FusionSet '${this._name}': member place must not be null`);
1036
- }
1037
- this._members.push(place2);
1038
- return this;
1039
- }
1040
- /**
1041
- * Builds the immutable {@link FusionSet}. Validates that the set has at
1042
- * least one member; the empty set is rejected as malformed per **MOD-060**.
1043
- * Single-member sets are accepted as a structurally degenerate no-op.
1044
- */
1045
- build() {
1046
- if (this._members.length === 0) {
1047
- throw new Error(
1048
- `FusionSet '${this._name}': must have at least one member (MOD-060)`
1049
- );
1050
- }
1051
- return new FusionSet(FUSION_SET_KEY, this._name, Object.freeze(this._members.slice()));
1052
- }
1053
- };
1054
-
1055
- // src/core/petri-net.ts
1056
- var PETRI_NET_KEY = /* @__PURE__ */ Symbol("PetriNet.internal");
1057
- var PetriNet = class _PetriNet {
1058
- name;
1059
- places;
1060
- transitions;
1061
- /** @internal Use {@link PetriNet.builder} to create instances. */
1062
- constructor(key, name, places, transitions) {
1063
- if (key !== PETRI_NET_KEY) throw new Error("Use PetriNet.builder() to create instances");
1064
- this.name = name;
1065
- this.places = places;
1066
- this.transitions = transitions;
1067
- }
1068
- /**
1069
- * Creates a new PetriNet with actions bound to transitions by name.
1070
- * Unbound transitions keep passthrough action.
1071
- */
1072
- bindActions(actionBindings) {
1073
- const bindings = actionBindings instanceof Map ? actionBindings : new Map(Object.entries(actionBindings));
1074
- return this.bindActionsWithResolver(
1075
- (name) => bindings.get(name) ?? passthrough()
1076
- );
1077
- }
1078
- /**
1079
- * Creates a new PetriNet with actions bound via a resolver function.
1185
+ * The `"/"` character is reserved as the prefix separator (per [MOD-010]).
1186
+ * User-supplied prefixes MUST NOT contain `"/"`; nested instantiation is
1187
+ * performed by the future `PetriNetBuilder.compose(...)` mechanism (per
1188
+ * [MOD-013]). A prefix containing `"/"` raises an `Error`.
1189
+ *
1190
+ * @param prefix the rename prefix (non-empty, must not contain `"/"`)
1191
+ * @param params the parameter value carried through to `Instance.params`
1192
+ * (may be omitted when `P` is `void`)
1193
+ * @throws when `prefix` is empty or contains `"/"`
1080
1194
  */
1081
- bindActionsWithResolver(actionResolver) {
1082
- const boundTransitions = /* @__PURE__ */ new Set();
1083
- for (const t of this.transitions) {
1084
- const action = actionResolver(t.name);
1085
- if (action !== null && action !== t.action) {
1086
- boundTransitions.add(rebuildWithAction(t, action));
1087
- } else {
1088
- boundTransitions.add(t);
1195
+ instantiate(prefix, params) {
1196
+ validatePrefix(prefix);
1197
+ const placeRemap = /* @__PURE__ */ new Map();
1198
+ const transitionRemap = /* @__PURE__ */ new Map();
1199
+ const renamedBody = renameNet(this.body, prefix, placeRemap, transitionRemap);
1200
+ const portHandles = /* @__PURE__ */ new Map();
1201
+ for (const port of this.iface.ports.values()) {
1202
+ const renamed = placeRemap.get(port.place.name);
1203
+ if (renamed === void 0) {
1204
+ throw new Error(
1205
+ `Port '${port.name}' references place '${port.place.name}' that was not found in the renamed body. This indicates a SubnetDef invariant violation.`
1206
+ );
1089
1207
  }
1208
+ portHandles.set(port.name, renamed);
1090
1209
  }
1091
- return new _PetriNet(PETRI_NET_KEY, this.name, this.places, boundTransitions);
1092
- }
1093
- static builder(name) {
1094
- return new PetriNetBuilder(name);
1095
- }
1096
- };
1097
- var PetriNetBuilder = class {
1098
- _name;
1099
- _places = /* @__PURE__ */ new Set();
1100
- _transitions = /* @__PURE__ */ new Set();
1101
- _fusionSets = [];
1102
- constructor(name) {
1103
- this._name = name;
1104
- }
1105
- /** Add an explicit place. */
1106
- place(place2) {
1107
- this._places.add(place2);
1108
- return this;
1109
- }
1110
- /** Add explicit places. */
1111
- places(...places) {
1112
- for (const p of places) this._places.add(p);
1113
- return this;
1114
- }
1115
- /** Add a transition (auto-collects places from arcs). */
1116
- transition(transition) {
1117
- this._transitions.add(transition);
1118
- for (const spec of transition.inputSpecs) {
1119
- this._places.add(spec.place);
1120
- }
1121
- for (const p of transition.outputPlaces()) {
1122
- this._places.add(p);
1123
- }
1124
- for (const inh of transition.inhibitors) {
1125
- this._places.add(inh.place);
1126
- }
1127
- for (const r of transition.reads) {
1128
- this._places.add(r.place);
1129
- }
1130
- for (const r of transition.resets) {
1131
- this._places.add(r.place);
1132
- }
1133
- return this;
1134
- }
1135
- /** Add transitions (auto-collects places from arcs). */
1136
- transitions(...transitions) {
1137
- for (const t of transitions) this.transition(t);
1138
- return this;
1139
- }
1140
- compose(instance, arg) {
1141
- if (arg === void 0) {
1142
- return this.composeAuto(instance);
1143
- }
1144
- if (typeof arg === "function") {
1145
- const bindings = __createComposeBindings();
1146
- arg(bindings);
1147
- return this.composeInternal(instance, bindings.portBindings(), bindings.channelBindings());
1210
+ const channelHandles = /* @__PURE__ */ new Map();
1211
+ for (const channel of this.iface.channels.values()) {
1212
+ const renamed = transitionRemap.get(channel.transition.name);
1213
+ if (renamed === void 0) {
1214
+ throw new Error(
1215
+ `Channel '${channel.name}' references transition '${channel.transition.name}' that was not found in the renamed body. This indicates a SubnetDef invariant violation.`
1216
+ );
1217
+ }
1218
+ channelHandles.set(channel.name, renamed);
1148
1219
  }
1149
- const portMappings = arg instanceof Map ? arg : new Map(Object.entries(arg));
1150
- return this.composeInternal(instance, portMappings, /* @__PURE__ */ new Map());
1220
+ return __createInstance(
1221
+ prefix,
1222
+ this,
1223
+ renamedBody,
1224
+ portHandles,
1225
+ channelHandles,
1226
+ params
1227
+ );
1151
1228
  }
1152
1229
  /**
1153
- * Identity-default auto-compose per **MOD-024**.
1230
+ * Verifies safety properties of this subnet definition **in isolation** per
1231
+ * **MOD-051**, by wrapping it in a synthetic enclosing net where each input
1232
+ * port is fed by an {@link EnvironmentPlace} (token-source per the harness
1233
+ * generator) and each output port is observed via a synthetic place. The
1234
+ * standard {@link SmtVerifier} (per [MOD-050]) is invoked once per property
1235
+ * declared in the harness; the resulting per-property outcomes are
1236
+ * aggregated into a {@link VerificationResult}.
1154
1237
  *
1155
- * Each declared interface port auto-binds to its own `port.place` — the
1156
- * Place the SubnetDef builder declared via `.inputPort(name, hostPlace)`
1157
- * (or `outputPort` / `inoutPort`). If the host builder already declares
1158
- * the equal place, the two merge; if not, the place arrives implicitly
1159
- * via the rewritten transitions' arcs (same flow as explicit `bindPort`).
1238
+ * ## Synthetic-net construction
1160
1239
  *
1161
- * If the subnet declares no interface ports at all, body places are
1162
- * checked against this builder's place set **by name** (matching the
1163
- * existing TS Place equality semantics; see [CORE-002] note in
1164
- * `spec/11-modular-composition.md` MOD-024). Body places that don't match
1165
- * stay private under their prefixed names per [MOD-010].
1240
+ * The synthetic enclosing net is built by:
1241
+ * 1. Instantiating this `SubnetDef` with the prefix `"sut"` (system-under-test)
1242
+ * and `harness.params`.
1243
+ * 2. For each input or in-out port on the interface, looking up the
1244
+ * harness generator by port name, allocating a synthetic
1245
+ * {@link Place}`<unknown>` of the same conceptual token type as the
1246
+ * port, wrapping it in an {@link EnvironmentPlace}, and binding the
1247
+ * port to that synthetic place via
1248
+ * {@link import('./petri-net.js').PetriNetBuilder.compose}. The supplier
1249
+ * is invoked once at construction time to materialize the seed token —
1250
+ * its presence is what bounds the input behavior under analysis. **If
1251
+ * the harness map is missing a generator for a required input or in-out
1252
+ * port, an `Error` is thrown.**
1253
+ * 3. For each output or in-out port, allocating a synthetic observation
1254
+ * {@link Place}`<unknown>` and binding the port to it via the same
1255
+ * `compose(...)` call. The verifier inspects this place's reachability
1256
+ * / marking through the standard property APIs ({@link SmtProperty}).
1257
+ * 4. Building the resulting flat {@link PetriNet} per [MOD-023] (the
1258
+ * verifier is composition-unaware per [MOD-050]).
1166
1259
  *
1167
- * Channels are NOT auto-bound — transition identity is too delicate for
1168
- * inference. If the subnet declares any channel, this overload throws.
1260
+ * ## Per-property invocation
1261
+ *
1262
+ * Each {@link SmtProperty} in the harness is verified independently against
1263
+ * the same synthetic net. The synthetic environment places are passed
1264
+ * through to the verifier so that places driven by the harness generators
1265
+ * are treated under the verifier's environment-analysis semantics rather
1266
+ * than as ordinary sink places.
1267
+ *
1268
+ * @param harness the verification harness — supplies parameters, input-port
1269
+ * token generators, and the property set
1270
+ * @returns a {@link VerificationResult} aggregating per-property
1271
+ * {@link SmtVerificationResult}s
1272
+ * @throws when an input or in-out port is missing a harness generator
1169
1273
  */
1170
- composeAuto(instance) {
1171
- const iface = instance.def.iface;
1172
- if (iface.channels.size > 0) {
1173
- const channelNames = [];
1174
- for (const c of iface.channels.values()) channelNames.push(c.name);
1175
- channelNames.sort();
1176
- throw new Error(
1177
- `compose(Instance): subnet '${instance.def.name}' (instance prefix '${instance.prefix}') declares channels [${channelNames.join(", ")}]; auto-compose does not bind channels. Use compose(instance, bind => bind.bindChannel(...)) with explicit channel bindings.`
1178
- );
1274
+ async verify(harness) {
1275
+ if (harness === null || harness === void 0) {
1276
+ throw new Error("SubnetDef.verify: harness must not be null/undefined");
1179
1277
  }
1180
- const hostByName = /* @__PURE__ */ new Map();
1181
- for (const p of this._places) hostByName.set(p.name, p);
1182
- if (iface.ports.size > 0) {
1183
- const portMappings = /* @__PURE__ */ new Map();
1184
- for (const port of iface.ports.values()) {
1185
- const hostMatch = hostByName.get(port.place.name);
1186
- portMappings.set(port.name, hostMatch ?? port.place);
1278
+ const sut = this.instantiate("sut", harness.params);
1279
+ const generators = normaliseGenerators(harness.portInputGenerators);
1280
+ const properties = normaliseProperties(harness.properties);
1281
+ const envPlaces = [];
1282
+ const portMappings = /* @__PURE__ */ new Map();
1283
+ for (const port of this.iface.ports.values()) {
1284
+ const portName = port.name;
1285
+ switch (port.direction) {
1286
+ case "input": {
1287
+ const generator = generators.get(portName);
1288
+ if (generator === void 0) {
1289
+ throw new Error(
1290
+ `verify: harness is missing an input generator for port '${portName}' on subnet '${this.name}' (MOD-051)`
1291
+ );
1292
+ }
1293
+ const seed = generator();
1294
+ if (seed === null || seed === void 0) {
1295
+ throw new Error(
1296
+ `verify: input generator for port '${portName}' on subnet '${this.name}' produced null/undefined`
1297
+ );
1298
+ }
1299
+ const synth = place(`harness_in_${portName}`);
1300
+ envPlaces.push(environmentPlace(synth.name));
1301
+ portMappings.set(portName, synth);
1302
+ break;
1303
+ }
1304
+ case "output": {
1305
+ const synth = place(`harness_out_${portName}`);
1306
+ portMappings.set(portName, synth);
1307
+ break;
1308
+ }
1309
+ case "inout": {
1310
+ const generator = generators.get(portName);
1311
+ if (generator === void 0) {
1312
+ throw new Error(
1313
+ `verify: harness is missing an input generator for in-out port '${portName}' on subnet '${this.name}' (MOD-051)`
1314
+ );
1315
+ }
1316
+ const seed = generator();
1317
+ if (seed === null || seed === void 0) {
1318
+ throw new Error(
1319
+ `verify: input generator for in-out port '${portName}' on subnet '${this.name}' produced null/undefined`
1320
+ );
1321
+ }
1322
+ const synth = place(`harness_io_${portName}`);
1323
+ envPlaces.push(environmentPlace(synth.name));
1324
+ portMappings.set(portName, synth);
1325
+ break;
1326
+ }
1187
1327
  }
1188
- return this.composeInternal(instance, portMappings, /* @__PURE__ */ new Map());
1189
1328
  }
1190
- const mergeMap = /* @__PURE__ */ new Map();
1191
- const prefix = instance.prefix + "/";
1192
- for (const renamed of instance.renamedBody.places) {
1193
- if (!renamed.name.startsWith(prefix)) continue;
1194
- const originalName = renamed.name.substring(prefix.length);
1195
- const hostMatch = hostByName.get(originalName);
1196
- if (hostMatch !== void 0) {
1197
- mergeMap.set(renamed.name, hostMatch);
1329
+ const syntheticNet = PetriNet.builder("verify_" + this.name).compose(sut, portMappings).build();
1330
+ const perProperty = /* @__PURE__ */ new Map();
1331
+ for (const property of properties) {
1332
+ const verifier = SmtVerifier.forNet(syntheticNet).property(property);
1333
+ if (envPlaces.length > 0) {
1334
+ verifier.environmentPlaces(...envPlaces);
1198
1335
  }
1336
+ const result = await verifier.verify();
1337
+ perProperty.set(property, result);
1199
1338
  }
1200
- return this.applyComposition(instance, mergeMap, /* @__PURE__ */ new Map());
1339
+ return buildVerificationResult(syntheticNet, perProperty);
1340
+ }
1341
+ // ============================================================
1342
+ // Static factories
1343
+ // ============================================================
1344
+ static builder(name) {
1345
+ return new SubnetDefBuilder(name);
1201
1346
  }
1202
1347
  /**
1203
- * @internal Shared compose implementation: validates port and channel
1204
- * bindings, builds the place-substitution map, walks every renamed-body
1205
- * transition through {@link substitutePlaces}, applies channel merges per
1206
- * [MOD-021], and adds the resulting transitions to this builder.
1207
- *
1208
- * ## Channel-merge flow ([MOD-021])
1348
+ * Retrofit utility per **MOD-014**: wraps an existing closed {@link PetriNet}
1349
+ * plus an {@link Interface} into an unparameterised `SubnetDef<void>`.
1209
1350
  *
1210
- * 1. Collect rewritten instance transitions into a working `Map<string,
1211
- * Transition>` keyed by prefixed transition name (deferred — not yet
1212
- * added to the builder's transition set).
1213
- * 2. For each channel binding, resolve the renamed instance-side
1214
- * transition through `instance.channel(channelName)`, then look up its
1215
- * rewritten counterpart in the working map by name.
1216
- * 3. Replace the working-map entry with a {@link mergeTransitions} result
1217
- * that fuses caller-side + instance-side; remove the rewritten
1218
- * instance-side entry. Also replace (or add) the caller-side
1219
- * transition in this builder's transition set with the same merged
1220
- * result, indexed under the caller's name slot.
1221
- * 4. Add the surviving (un-merged) entries to this builder.
1351
+ * Validation per **MOD-014** / **MOD-006** is enforced before the result is
1352
+ * constructed:
1353
+ * - Every port's underlying `Place` must be present in `net.places`.
1354
+ * - Every channel's underlying `Transition` must be present in `net.transitions`.
1355
+ * - Port and channel name uniqueness is re-validated defensively (the
1356
+ * `Interface` builder already enforces this; hand-built `Interface`
1357
+ * values bypass that path).
1222
1358
  *
1223
- * The deferral matters: writing the rewritten instance transitions to the
1224
- * builder eagerly would force a second "remove-then-replace" pass to
1225
- * apply the channel merges, complicating the place-collection invariants.
1226
- * Collecting first and merging second keeps the builder's transition set
1227
- * finalized exactly once.
1359
+ * The resulting subnet definition is unparameterised (parameter type is `void`).
1228
1360
  *
1229
- * Keying the working map by prefixed transition name (rather than by
1230
- * Transition reference) is also robust against a prior
1231
- * {@link Instance.bindActions} call that may have rebuilt the renamed-body
1232
- * transitions, breaking identity equality between the body and the
1233
- * channel-handle map — but the prefixed names remain stable.
1361
+ * @throws when a port place is not in `net.places`, a channel transition is
1362
+ * not in `net.transitions`, or port/channel names are not unique.
1234
1363
  */
1235
- composeInternal(instance, portMappings, channelBindings) {
1236
- const iface = instance.def.iface;
1237
- const mergeMap = /* @__PURE__ */ new Map();
1238
- for (const [portName, callerPlace] of portMappings) {
1239
- const port = iface.port(portName);
1240
- if (port === void 0) {
1241
- const knownPorts = [];
1242
- for (const p of iface.ports.values()) knownPorts.push(p.name);
1364
+ static fromNet(net, iface) {
1365
+ const bodyPlaces = net.places;
1366
+ const bodyTransitions = net.transitions;
1367
+ const seenPortNames = /* @__PURE__ */ new Set();
1368
+ for (const port of iface.ports.values()) {
1369
+ if (seenPortNames.has(port.name)) {
1243
1370
  throw new Error(
1244
- `compose: no port named '${portName}' on subnet '${instance.def.name}' (instance prefix '${instance.prefix}'). Known ports: [${knownPorts.join(", ")}]`
1371
+ `fromNet: duplicate port name '${port.name}' on interface for net '${net.name}' (MOD-006)`
1372
+ );
1373
+ }
1374
+ seenPortNames.add(port.name);
1375
+ if (!bodyPlaces.has(port.place)) {
1376
+ throw new Error(
1377
+ `fromNet: port '${port.name}' references place '${port.place.name}' which is not in net '${net.name}' (MOD-014/MOD-006)`
1245
1378
  );
1246
1379
  }
1247
- const ifacePlace = instance.port(portName);
1248
- mergeMap.set(ifacePlace.name, callerPlace);
1249
- }
1250
- return this.applyComposition(instance, mergeMap, channelBindings);
1251
- }
1252
- /**
1253
- * Shared post-mergeMap pipeline: rewrites renamed-body transitions
1254
- * through `mergeMap`, applies channel merges per **MOD-021**, and adds
1255
- * the surviving transitions to the builder.
1256
- *
1257
- * Used by both the explicit-binding path (`composeInternal`) and the
1258
- * auto-compose path (`composeAuto` per **MOD-024**). The two paths differ
1259
- * only in how the (renamed Place name → host Place) `mergeMap` is built.
1260
- */
1261
- applyComposition(instance, mergeMap, channelBindings) {
1262
- const iface = instance.def.iface;
1263
- const rewrittenByName = /* @__PURE__ */ new Map();
1264
- for (const t of instance.renamedBody.transitions) {
1265
- rewrittenByName.set(t.name, substitutePlaces(t, mergeMap));
1266
1380
  }
1267
- for (const [channelName, callerTrans] of channelBindings) {
1268
- let instanceRenamedChannel;
1269
- try {
1270
- instanceRenamedChannel = instance.channel(channelName);
1271
- } catch (cause) {
1272
- const knownChannels = [];
1273
- for (const c of iface.channels.values()) knownChannels.push(c.name);
1274
- const err = new Error(
1275
- `compose: no channel named '${channelName}' on subnet '${instance.def.name}' (instance prefix '${instance.prefix}'). Known channels: [${knownChannels.join(", ")}]`
1381
+ const seenChannelNames = /* @__PURE__ */ new Set();
1382
+ for (const channel of iface.channels.values()) {
1383
+ if (seenChannelNames.has(channel.name)) {
1384
+ throw new Error(
1385
+ `fromNet: duplicate channel name '${channel.name}' on interface for net '${net.name}' (MOD-006)`
1276
1386
  );
1277
- err.cause = cause;
1278
- throw err;
1279
1387
  }
1280
- const rewrittenInstanceChannel = rewrittenByName.get(instanceRenamedChannel.name);
1281
- if (rewrittenInstanceChannel === void 0) {
1388
+ seenChannelNames.add(channel.name);
1389
+ if (!bodyTransitions.has(channel.transition)) {
1282
1390
  throw new Error(
1283
- `compose: channel '${channelName}' resolved to a transition '${instanceRenamedChannel.name}' that is not present in the renamed body. This indicates a SubnetDef invariant violation.`
1391
+ `fromNet: channel '${channel.name}' references transition '${channel.transition.name}' which is not in net '${net.name}' (MOD-014/MOD-006)`
1284
1392
  );
1285
1393
  }
1286
- const merged = mergeTransitions(callerTrans, rewrittenInstanceChannel, callerTrans.name);
1287
- rewrittenByName.delete(instanceRenamedChannel.name);
1288
- this._transitions.delete(callerTrans);
1289
- this.transition(merged);
1290
- }
1291
- for (const rewritten of rewrittenByName.values()) {
1292
- this.transition(rewritten);
1293
1394
  }
1395
+ return new _SubnetDef(SUBNET_DEF_KEY, net.name, net, iface);
1396
+ }
1397
+ };
1398
+ var SubnetDefBuilder = class {
1399
+ _name;
1400
+ _bodyBuilder;
1401
+ _ports = [];
1402
+ _channels = [];
1403
+ constructor(name) {
1404
+ this._name = name;
1405
+ this._bodyBuilder = PetriNet.builder(name);
1406
+ }
1407
+ // -------- body construction (delegates to PetriNet.Builder) --------
1408
+ transition(transition) {
1409
+ this._bodyBuilder.transition(transition);
1294
1410
  return this;
1295
1411
  }
1296
- fuse(...args) {
1297
- if (args.length === 1 && typeof args[0] === "function") {
1298
- const declarer = args[0];
1299
- const fb = FusionSet.builder(this._name + "-fusion");
1300
- declarer(fb);
1301
- this._fusionSets.push(fb.build());
1302
- return this;
1303
- }
1304
- for (const s of args) {
1305
- if (s === void 0 || s === null) {
1306
- throw new Error("fuse: fusion set must not be null");
1307
- }
1308
- this._fusionSets.push(s);
1309
- }
1412
+ transitions(...transitions) {
1413
+ this._bodyBuilder.transitions(...transitions);
1310
1414
  return this;
1311
1415
  }
1312
- /**
1313
- * Builds the immutable {@link PetriNet}, applying fusion resolution (per
1314
- * **MOD-061**) AFTER all transition/composition accumulation:
1315
- *
1316
- * 1. Detect overlapping fusion sets — a single place declared in two sets
1317
- * is rejected with an `Error`.
1318
- * 2. Build the `non-canonical → canonical` substitution map across all
1319
- * sets, keyed by non-canonical place name (matching the rewriter's
1320
- * Map<string, Place<unknown>> convention — TypeScript Place identity is
1321
- * name-based per `runtime/compiled-net.ts`).
1322
- * 3. Walk every transition through {@link applyFusion} to rewrite arc place
1323
- * references.
1324
- * 4. Re-derive the place set from the rewritten transitions plus any
1325
- * caller-declared standalone places, dropping non-canonical members.
1326
- * Caller-declared standalone places that happen to be non-canonical
1327
- * members are also dropped.
1328
- *
1329
- * If no fusion sets were registered, the build is the trivial
1330
- * `new PetriNet(...)` — the fusion machinery has no per-build cost when
1331
- * unused.
1332
- *
1333
- * @throws when two fusion sets share a place
1334
- */
1335
- build() {
1336
- if (this._fusionSets.length === 0) {
1337
- return new PetriNet(PETRI_NET_KEY, this._name, this._places, this._transitions);
1338
- }
1339
- return this.buildWithFusion();
1416
+ place(place2) {
1417
+ this._bodyBuilder.place(place2);
1418
+ return this;
1340
1419
  }
1341
- /**
1342
- * @internal Fusion-resolution pass per **MOD-061**. Split out from
1343
- * {@link build} so the no-fusion fast path stays trivial.
1344
- */
1345
- buildWithFusion() {
1346
- const ownership = /* @__PURE__ */ new Map();
1347
- for (const set of this._fusionSets) {
1348
- for (const member of set.members) {
1349
- const prior = ownership.get(member.name);
1350
- if (prior !== void 0 && prior !== set) {
1351
- throw new Error(
1352
- `Fusion overlap: place '${member.name}' appears in two fusion sets ('${prior.name}' and '${set.name}'). A place may appear in at most one fusion set (MOD-060).`
1353
- );
1354
- }
1355
- ownership.set(member.name, set);
1420
+ // -------- interface declarations --------
1421
+ inputPort(name, place2) {
1422
+ this._ports.push({ name, direction: "input", place: place2 });
1423
+ return this;
1424
+ }
1425
+ outputPort(name, place2) {
1426
+ this._ports.push({ name, direction: "output", place: place2 });
1427
+ return this;
1428
+ }
1429
+ inoutPort(name, place2) {
1430
+ this._ports.push({ name, direction: "inout", place: place2 });
1431
+ return this;
1432
+ }
1433
+ channel(name, transition) {
1434
+ this._channels.push({ name, transition });
1435
+ return this;
1436
+ }
1437
+ // -------- build & validate (MOD-006) --------
1438
+ build() {
1439
+ const built = this._bodyBuilder.build();
1440
+ const bodyPlaces = built.places;
1441
+ const bodyTransitions = built.transitions;
1442
+ const portNames = /* @__PURE__ */ new Set();
1443
+ for (const port of this._ports) {
1444
+ if (portNames.has(port.name)) {
1445
+ throw new Error(`Subnet '${this._name}': duplicate port name '${port.name}'`);
1356
1446
  }
1357
- }
1358
- const fusionMap = /* @__PURE__ */ new Map();
1359
- const nonCanonicalNames = /* @__PURE__ */ new Set();
1360
- for (const set of this._fusionSets) {
1361
- const canonical = set.canonical;
1362
- for (const nc of set.nonCanonical()) {
1363
- fusionMap.set(nc.name, canonical);
1364
- nonCanonicalNames.add(nc.name);
1447
+ portNames.add(port.name);
1448
+ if (!bodyPlaces.has(port.place)) {
1449
+ throw new Error(
1450
+ `Subnet '${this._name}': port '${port.name}' references place '${port.place.name}' which is not in the body`
1451
+ );
1365
1452
  }
1366
1453
  }
1367
- const rewrittenTransitions = applyFusion(this._transitions, fusionMap);
1368
- const rebuiltPlaces = /* @__PURE__ */ new Set();
1369
- for (const p of this._places) {
1370
- if (!nonCanonicalNames.has(p.name)) {
1371
- rebuiltPlaces.add(p);
1454
+ const channelNames = /* @__PURE__ */ new Set();
1455
+ for (const channel of this._channels) {
1456
+ if (channelNames.has(channel.name)) {
1457
+ throw new Error(`Subnet '${this._name}': duplicate channel name '${channel.name}'`);
1458
+ }
1459
+ channelNames.add(channel.name);
1460
+ if (!bodyTransitions.has(channel.transition)) {
1461
+ throw new Error(
1462
+ `Subnet '${this._name}': channel '${channel.name}' references transition '${channel.transition.name}' which is not in the body`
1463
+ );
1372
1464
  }
1373
1465
  }
1374
- for (const t of rewrittenTransitions) {
1375
- for (const spec of t.inputSpecs) rebuiltPlaces.add(spec.place);
1376
- for (const p of t.outputPlaces()) rebuiltPlaces.add(p);
1377
- for (const inh of t.inhibitors) rebuiltPlaces.add(inh.place);
1378
- for (const r of t.reads) rebuiltPlaces.add(r.place);
1379
- for (const r of t.resets) rebuiltPlaces.add(r.place);
1380
- }
1381
- return new PetriNet(PETRI_NET_KEY, this._name, rebuiltPlaces, rewrittenTransitions);
1466
+ const ifaceBuilder = Interface.builder();
1467
+ ifaceBuilder.portsAll(this._ports);
1468
+ ifaceBuilder.channelsAll(this._channels);
1469
+ const iface = ifaceBuilder.build();
1470
+ return new SubnetDef(SUBNET_DEF_KEY, this._name, built, iface);
1382
1471
  }
1383
1472
  };
1384
- function rebuildWithAction(t, action) {
1385
- const builder = Transition.builder(t.name).timing(t.timing).priority(t.priority).action(action);
1386
- if (t.inputSpecs.length > 0) {
1387
- builder.inputs(...t.inputSpecs);
1388
- }
1389
- if (t.outputSpec !== null) {
1390
- builder.outputs(t.outputSpec);
1391
- }
1392
- for (const inh of t.inhibitors) {
1393
- builder.inhibitor(inh.place);
1394
- }
1395
- for (const r of t.reads) {
1396
- builder.read(r.place);
1473
+ function validatePrefix(prefix) {
1474
+ if (typeof prefix !== "string" || prefix.length === 0) {
1475
+ throw new Error("SubnetDef.instantiate: prefix must be a non-empty string");
1397
1476
  }
1398
- for (const r of t.resets) {
1399
- builder.reset(r.place);
1477
+ if (prefix.indexOf("/") >= 0) {
1478
+ throw new Error(
1479
+ `SubnetDef.instantiate: prefix must not contain '/' (reserved as the prefix separator per MOD-010); use compose(...) for nested instantiation. Got: '${prefix}'`
1480
+ );
1400
1481
  }
1401
- return builder.build();
1402
- }
1403
-
1404
- // src/core/subnet.ts
1405
- function closedSubnet(net) {
1406
- return { kind: "closed", net };
1407
- }
1408
- function openSubnet(def) {
1409
- return { kind: "open", def };
1410
1482
  }
1411
1483
 
1412
- // src/core/interface.ts
1413
- var INTERFACE_KEY = /* @__PURE__ */ Symbol("Interface.internal");
1414
- var Interface = class {
1415
- ports;
1416
- channels;
1417
- /** @internal Use {@link Interface.builder} to create instances. */
1418
- constructor(key, ports, channels) {
1419
- if (key !== INTERFACE_KEY) throw new Error("Use Interface.builder() to create instances");
1420
- this.ports = ports;
1421
- this.channels = channels;
1422
- }
1423
- /** Looks up a port by name. Returns undefined when absent. */
1424
- port(name) {
1425
- return this.ports.get(name);
1426
- }
1427
- /** Looks up a channel by name. Returns undefined when absent. */
1428
- channel(name) {
1429
- return this.channels.get(name);
1484
+ // src/core/compose-bindings.ts
1485
+ var COMPOSE_BINDINGS_KEY = /* @__PURE__ */ Symbol("ComposeBindings.internal");
1486
+ var ComposeBindings = class {
1487
+ _portBindings = /* @__PURE__ */ new Map();
1488
+ _channelBindings = /* @__PURE__ */ new Map();
1489
+ /**
1490
+ * @internal Use {@link PetriNetBuilder.compose} — instances are produced
1491
+ * by the host builder and supplied to the caller's callback.
1492
+ */
1493
+ constructor(key) {
1494
+ if (key !== COMPOSE_BINDINGS_KEY) {
1495
+ throw new Error(
1496
+ "Use PetriNetBuilder.compose(instance, b => ...) \u2014 ComposeBindings is not directly constructible"
1497
+ );
1498
+ }
1430
1499
  }
1431
1500
  /**
1432
- * Looks up a port by name and returns its underlying place, narrowed to
1433
- * `Place<T>`. Returns undefined when the port does not exist.
1501
+ * Binds the named interface port to the given caller place per **MOD-020**.
1434
1502
  *
1435
- * Note: TypeScript erases generics at runtime, so unlike Java's
1436
- * `portPlaceAs(name, Class<T>)` this method cannot verify the token type at
1437
- * runtime the caller is trusted to supply the correct `T`. Per **MOD-022**,
1438
- * type compatibility is enforced at compile time only in TypeScript.
1503
+ * The port name is the **original** (pre-prefix) name declared in the
1504
+ * subnet's `Interface`. The typed `<T>` parameter ensures the caller
1505
+ * place's token type matches the interface port's token type at compile
1506
+ * time per [MOD-022]; TypeScript does not validate the type at runtime
1507
+ * because generics are erased.
1508
+ *
1509
+ * @throws when `portName` is already bound on this builder
1439
1510
  */
1440
- placeAs(name) {
1441
- const p = this.ports.get(name);
1442
- if (p === void 0) return void 0;
1443
- return p.place;
1444
- }
1445
- static builder() {
1446
- return new InterfaceBuilder();
1447
- }
1448
- };
1449
- var InterfaceBuilder = class {
1450
- _ports = /* @__PURE__ */ new Map();
1451
- _channels = /* @__PURE__ */ new Map();
1452
- /** Add a pre-built port (rejects duplicate names). */
1453
- port(port) {
1454
- if (this._ports.has(port.name)) {
1455
- throw new Error(`Duplicate port name: '${port.name}'`);
1511
+ bindPort(portName, callerPlace) {
1512
+ if (this._portBindings.has(portName)) {
1513
+ throw new Error(`Port '${portName}' is already bound`);
1456
1514
  }
1457
- this._ports.set(port.name, port);
1515
+ this._portBindings.set(portName, callerPlace);
1458
1516
  return this;
1459
1517
  }
1460
- /** Add an input port (advisory direction). */
1461
- inputPort(name, place2) {
1462
- return this.port({ name, direction: "input", place: place2 });
1518
+ /**
1519
+ * Records a synchronous channel binding per **MOD-021**: at compose time,
1520
+ * the instance-side renamed channel transition is merged with
1521
+ * `callerTransition` into a single transition in the resulting flat net.
1522
+ *
1523
+ * **Status**: channel composition is implemented in task #13. Recording a
1524
+ * channel binding here is permitted, but `compose(...)` currently raises
1525
+ * an `Error` when any channel binding is present.
1526
+ *
1527
+ * @throws when `channelName` is already bound on this builder
1528
+ */
1529
+ bindChannel(channelName, callerTransition) {
1530
+ if (this._channelBindings.has(channelName)) {
1531
+ throw new Error(`Channel '${channelName}' is already bound`);
1532
+ }
1533
+ this._channelBindings.set(channelName, callerTransition);
1534
+ return this;
1463
1535
  }
1464
- /** Add an output port (advisory direction). */
1465
- outputPort(name, place2) {
1466
- return this.port({ name, direction: "output", place: place2 });
1536
+ /** Returns an unmodifiable view of the recorded port bindings. */
1537
+ portBindings() {
1538
+ return this._portBindings;
1467
1539
  }
1468
- /** Add an in-out port (advisory direction). */
1469
- inoutPort(name, place2) {
1470
- return this.port({ name, direction: "inout", place: place2 });
1540
+ /** Returns an unmodifiable view of the recorded channel bindings. */
1541
+ channelBindings() {
1542
+ return this._channelBindings;
1471
1543
  }
1472
- channel(channelOrName, transition) {
1473
- const ch = typeof channelOrName === "string" ? { name: channelOrName, transition } : channelOrName;
1474
- if (this._channels.has(ch.name)) {
1475
- throw new Error(`Duplicate channel name: '${ch.name}'`);
1544
+ };
1545
+ function __createComposeBindings() {
1546
+ return new ComposeBindings(COMPOSE_BINDINGS_KEY);
1547
+ }
1548
+
1549
+ // src/core/fusion-set.ts
1550
+ var FUSION_SET_KEY = /* @__PURE__ */ Symbol("FusionSet.internal");
1551
+ var FusionSet = class _FusionSet {
1552
+ name;
1553
+ members;
1554
+ /** @internal Use {@link FusionSet.builder} or {@link FusionSet.of} to create instances. */
1555
+ constructor(key, name, members) {
1556
+ if (key !== FUSION_SET_KEY) {
1557
+ throw new Error("Use FusionSet.builder() or FusionSet.of() to create instances");
1476
1558
  }
1477
- this._channels.set(ch.name, ch);
1478
- return this;
1559
+ this.name = name;
1560
+ this.members = members;
1479
1561
  }
1480
- /** @internal Bulk-add ports already validated by the caller. */
1481
- portsAll(ports) {
1482
- for (const p of ports) this.port(p);
1483
- return this;
1562
+ /**
1563
+ * Returns the canonical member — by convention, the first declared member.
1564
+ * The canonical place's identity survives into the resulting flat net.
1565
+ */
1566
+ get canonical() {
1567
+ return this.members[0];
1484
1568
  }
1485
- /** @internal Bulk-add channels already validated by the caller. */
1486
- channelsAll(channels) {
1487
- for (const c of channels) this.channel(c);
1488
- return this;
1569
+ /**
1570
+ * Returns all members **except** the canonical member, in declaration order.
1571
+ * These are the places that get substituted away at
1572
+ * {@link import('./petri-net.js').PetriNetBuilder.build} time.
1573
+ *
1574
+ * For a single-member (degenerate) set, returns an empty array.
1575
+ */
1576
+ nonCanonical() {
1577
+ if (this.members.length <= 1) return [];
1578
+ return this.members.slice(1);
1489
1579
  }
1490
- build() {
1491
- return new Interface(
1492
- INTERFACE_KEY,
1493
- new Map(this._ports),
1494
- new Map(this._channels)
1495
- );
1580
+ toString() {
1581
+ return `FusionSet[${this.name}, canonical=${this.canonical.name}, members=${this.members.length}]`;
1582
+ }
1583
+ // ============================================================
1584
+ // Static factories
1585
+ // ============================================================
1586
+ /** Returns a fresh {@link FusionSetBuilder} with the given human-readable name. */
1587
+ static builder(name) {
1588
+ return new FusionSetBuilder(name);
1496
1589
  }
1497
- };
1498
-
1499
- // src/core/instance.ts
1500
- var INSTANCE_KEY = /* @__PURE__ */ Symbol("Instance.internal");
1501
- var Instance = class {
1502
- prefix;
1503
- def;
1504
- renamedBody;
1505
- portHandles;
1506
- channelHandles;
1507
- params;
1508
1590
  /**
1509
- * @internal Use {@link SubnetDef.instantiate} (or the internal factory
1510
- * {@link __createInstance}) to create instances.
1591
+ * Convenience factory: builds a fusion set whose first member is `first`
1592
+ * (the canonical) and whose remaining members are `rest`, all sharing the
1593
+ * type `<T>`.
1594
+ *
1595
+ * The varargs form ensures static-type homogeneity at the call site (the
1596
+ * TypeScript compiler checks every `rest` entry is a `Place<T>`).
1511
1597
  */
1512
- constructor(key, prefix, def, renamedBody, portHandles, channelHandles, params) {
1513
- if (key !== INSTANCE_KEY) {
1514
- throw new Error("Use SubnetDef.instantiate() to create Instance values");
1598
+ static of(name, first, ...rest) {
1599
+ const members = [first];
1600
+ for (const p of rest) members.push(p);
1601
+ return new _FusionSet(FUSION_SET_KEY, name, Object.freeze(members));
1602
+ }
1603
+ };
1604
+ var FusionSetBuilder = class {
1605
+ _name;
1606
+ _members = [];
1607
+ constructor(name) {
1608
+ if (typeof name !== "string" || name.length === 0) {
1609
+ throw new Error("FusionSet.builder: name must be a non-empty string");
1515
1610
  }
1516
- this.prefix = prefix;
1517
- this.def = def;
1518
- this.renamedBody = renamedBody;
1519
- this.portHandles = portHandles;
1520
- this.channelHandles = channelHandles;
1521
- this.params = params;
1611
+ this._name = name;
1522
1612
  }
1523
1613
  /**
1524
- * Returns the renamed {@link Place} corresponding to the named port.
1525
- *
1526
- * The port name is the **original** (pre-prefix) name as declared in the
1527
- * subnet's `Interface`. Per **MOD-022**, TypeScript enforces token-type
1528
- * compatibility at compile time only; this method does not validate `T`
1529
- * at runtime. A missing name raises an `Error`.
1614
+ * Appends a member to the fusion set. The first member becomes the canonical
1615
+ * place per the convention documented on {@link FusionSet}.
1530
1616
  *
1531
- * @throws when the port name is unknown
1617
+ * The `<T>` parameter is for compile-time guidance only: callers writing
1618
+ * typed code at the same call site benefit from the compiler checking
1619
+ * `Place<T>` at each member.
1532
1620
  */
1533
- port(name) {
1534
- const p = this.portHandles.get(name);
1535
- if (p === void 0) {
1536
- throw new Error(`No port named '${name}' in instance '${this.prefix}'`);
1621
+ member(place2) {
1622
+ if (place2 === void 0 || place2 === null) {
1623
+ throw new Error(`FusionSet '${this._name}': member place must not be null`);
1537
1624
  }
1538
- return p;
1625
+ this._members.push(place2);
1626
+ return this;
1539
1627
  }
1540
1628
  /**
1541
- * Returns the renamed {@link Transition} corresponding to the named channel.
1542
- *
1543
- * @throws when the channel name is unknown
1629
+ * Builds the immutable {@link FusionSet}. Validates that the set has at
1630
+ * least one member; the empty set is rejected as malformed per **MOD-060**.
1631
+ * Single-member sets are accepted as a structurally degenerate no-op.
1544
1632
  */
1545
- channel(name) {
1546
- const t = this.channelHandles.get(name);
1547
- if (t === void 0) {
1548
- throw new Error(`No channel named '${name}' in instance '${this.prefix}'`);
1633
+ build() {
1634
+ if (this._members.length === 0) {
1635
+ throw new Error(
1636
+ `FusionSet '${this._name}': must have at least one member (MOD-060)`
1637
+ );
1549
1638
  }
1550
- return t;
1639
+ return new FusionSet(FUSION_SET_KEY, this._name, Object.freeze(this._members.slice()));
1640
+ }
1641
+ };
1642
+
1643
+ // src/core/petri-net.ts
1644
+ var PETRI_NET_KEY = /* @__PURE__ */ Symbol("PetriNet.internal");
1645
+ var PetriNet = class _PetriNet {
1646
+ name;
1647
+ places;
1648
+ transitions;
1649
+ /** @internal Use {@link PetriNet.builder} to create instances. */
1650
+ constructor(key, name, places, transitions) {
1651
+ if (key !== PETRI_NET_KEY) throw new Error("Use PetriNet.builder() to create instances");
1652
+ this.name = name;
1653
+ this.places = places;
1654
+ this.transitions = transitions;
1551
1655
  }
1552
1656
  /**
1553
- * Returns the debug-UI descriptor for this instance per **MOD-041**.
1657
+ * Creates a new PetriNet with actions bound to transitions by name.
1658
+ * Unbound transitions keep passthrough action.
1554
1659
  */
1555
- descriptor() {
1556
- const transitions = [];
1557
- for (const t of this.renamedBody.transitions) transitions.push(t.name);
1558
- const exposedPlaces = [];
1559
- for (const p of this.portHandles.values()) exposedPlaces.push(p.name);
1560
- return {
1561
- prefix: this.prefix,
1562
- defName: this.def.name,
1563
- transitions,
1564
- exposedPlaces,
1565
- params: this.params,
1566
- parentPrefix: null
1567
- };
1660
+ bindActions(actionBindings) {
1661
+ const bindings = actionBindings instanceof Map ? actionBindings : new Map(Object.entries(actionBindings));
1662
+ return this.bindActionsWithResolver(
1663
+ (name) => bindings.get(name) ?? passthrough()
1664
+ );
1568
1665
  }
1569
1666
  /**
1570
- * Produces a derived instance whose specified transitions (named by their
1571
- * **original**, pre-prefix names) carry the supplied actions per **MOD-030**.
1572
- *
1573
- * For each entry `[originalName, action]`, the renamed body is searched for
1574
- * the transition whose name equals `prefix + "/" + originalName`. The
1575
- * resulting instance shares the original `def`, `prefix`, `params`, and
1576
- * port/channel handle topology — only the renamed body is rebuilt with new
1577
- * actions. Per **MOD-030**, calling `bindActions` on one instance does NOT
1578
- * affect the actions held by other instances of the same `def`.
1579
- *
1580
- * Unrecognised original names raise an `Error` so typos surface eagerly.
1667
+ * Creates a new PetriNet with actions bound via a resolver function.
1581
1668
  */
1582
- bindActions(actionsByOriginalName) {
1583
- const prefixedByName = /* @__PURE__ */ new Map();
1584
- const renamedNames = /* @__PURE__ */ new Set();
1585
- for (const t of this.renamedBody.transitions) {
1586
- renamedNames.add(t.name);
1587
- }
1588
- for (const originalName of Object.keys(actionsByOriginalName)) {
1589
- const prefixed = this.prefix + "/" + originalName;
1590
- if (!renamedNames.has(prefixed)) {
1591
- throw new Error(
1592
- `Instance.bindActions: no transition '${originalName}' (resolved as '${prefixed}') in instance '${this.prefix}' of subnet '${this.def.name}'`
1593
- );
1594
- }
1595
- prefixedByName.set(prefixed, actionsByOriginalName[originalName]);
1596
- }
1597
- const reboundBody = this.renamedBody.bindActionsWithResolver((name) => {
1598
- const action = prefixedByName.get(name);
1599
- if (action !== void 0) return action;
1600
- for (const t of this.renamedBody.transitions) {
1601
- if (t.name === name) return t.action;
1602
- }
1603
- throw new Error(`Instance.bindActions: resolver invoked with unknown name '${name}'`);
1604
- });
1605
- const reboundChannelHandles = /* @__PURE__ */ new Map();
1606
- if (this.channelHandles.size > 0) {
1607
- const byName = /* @__PURE__ */ new Map();
1608
- for (const t of reboundBody.transitions) {
1609
- byName.set(t.name, t);
1610
- }
1611
- for (const [name, oldT] of this.channelHandles) {
1612
- const refreshed = byName.get(oldT.name);
1613
- if (refreshed === void 0) {
1614
- throw new Error(
1615
- `Instance.bindActions: channel '${name}' transition '${oldT.name}' not found in rebound body`
1616
- );
1617
- }
1618
- reboundChannelHandles.set(name, refreshed);
1669
+ bindActionsWithResolver(actionResolver) {
1670
+ const boundTransitions = /* @__PURE__ */ new Set();
1671
+ for (const t of this.transitions) {
1672
+ const action = actionResolver(t.name);
1673
+ if (action !== null && action !== t.action) {
1674
+ boundTransitions.add(rebuildWithAction(t, action));
1675
+ } else {
1676
+ boundTransitions.add(t);
1619
1677
  }
1620
1678
  }
1621
- return __createInstance(
1622
- this.prefix,
1623
- this.def,
1624
- reboundBody,
1625
- this.portHandles,
1626
- reboundChannelHandles,
1627
- this.params
1628
- );
1679
+ return new _PetriNet(PETRI_NET_KEY, this.name, this.places, boundTransitions);
1680
+ }
1681
+ static builder(name) {
1682
+ return new PetriNetBuilder(name);
1629
1683
  }
1630
1684
  };
1631
- function __createInstance(prefix, def, renamedBody, portHandles, channelHandles, params) {
1632
- return new Instance(
1633
- INSTANCE_KEY,
1634
- prefix,
1635
- def,
1636
- renamedBody,
1637
- portHandles,
1638
- channelHandles,
1639
- params
1640
- );
1641
- }
1642
-
1643
- // src/verification/verification-harness.ts
1644
- function buildVerificationResult(syntheticNet, perProperty) {
1645
- const frozen = new Map(perProperty);
1646
- return {
1647
- syntheticNet,
1648
- perProperty: frozen,
1649
- allProven() {
1650
- for (const r of frozen.values()) {
1651
- if (!isProven(r)) return false;
1652
- }
1653
- return true;
1654
- },
1655
- anyViolated() {
1656
- for (const r of frozen.values()) {
1657
- if (isViolated(r)) return true;
1658
- }
1659
- return false;
1685
+ var PetriNetBuilder = class {
1686
+ _name;
1687
+ _places = /* @__PURE__ */ new Set();
1688
+ _transitions = /* @__PURE__ */ new Set();
1689
+ _fusionSets = [];
1690
+ constructor(name) {
1691
+ this._name = name;
1692
+ }
1693
+ /** Add an explicit place. */
1694
+ place(place2) {
1695
+ this._places.add(place2);
1696
+ return this;
1697
+ }
1698
+ /** Add explicit places. */
1699
+ places(...places) {
1700
+ for (const p of places) this._places.add(p);
1701
+ return this;
1702
+ }
1703
+ /** Add a transition (auto-collects places from arcs). */
1704
+ transition(transition) {
1705
+ this._transitions.add(transition);
1706
+ for (const spec of transition.inputSpecs) {
1707
+ this._places.add(spec.place);
1660
1708
  }
1661
- };
1662
- }
1663
- function normaliseGenerators(generators) {
1664
- if (generators instanceof Map) return new Map(generators);
1665
- return new Map(Object.entries(generators));
1666
- }
1667
- function normaliseProperties(properties) {
1668
- if (Array.isArray(properties)) return [...properties];
1669
- return [...properties];
1670
- }
1671
-
1672
- // src/core/subnet-def.ts
1673
- var SUBNET_DEF_KEY = /* @__PURE__ */ Symbol("SubnetDef.internal");
1674
- var SubnetDef = class _SubnetDef {
1675
- name;
1676
- body;
1677
- iface;
1678
- /** @internal Use {@link SubnetDef.builder} or {@link SubnetDef.fromNet} to create instances. */
1679
- constructor(key, name, body, iface) {
1680
- if (key !== SUBNET_DEF_KEY) {
1681
- throw new Error("Use SubnetDef.builder() or SubnetDef.fromNet() to create instances");
1709
+ for (const p of transition.outputPlaces()) {
1710
+ this._places.add(p);
1711
+ }
1712
+ for (const inh of transition.inhibitors) {
1713
+ this._places.add(inh.place);
1714
+ }
1715
+ for (const r of transition.reads) {
1716
+ this._places.add(r.place);
1717
+ }
1718
+ for (const r of transition.resets) {
1719
+ this._places.add(r.place);
1682
1720
  }
1683
- this.name = name;
1684
- this.body = body;
1685
- this.iface = iface;
1721
+ return this;
1722
+ }
1723
+ /** Add transitions (auto-collects places from arcs). */
1724
+ transitions(...transitions) {
1725
+ for (const t of transitions) this.transition(t);
1726
+ return this;
1727
+ }
1728
+ compose(instanceOrDef, arg) {
1729
+ if (instanceOrDef instanceof SubnetDef) {
1730
+ return this.composeDirect(instanceOrDef);
1731
+ }
1732
+ const instance = instanceOrDef;
1733
+ if (arg === void 0) {
1734
+ return this.composeAuto(instance);
1735
+ }
1736
+ if (typeof arg === "function") {
1737
+ const bindings = __createComposeBindings();
1738
+ arg(bindings);
1739
+ return this.composeInternal(instance, bindings.portBindings(), bindings.channelBindings());
1740
+ }
1741
+ const portMappings = arg instanceof Map ? arg : new Map(Object.entries(arg));
1742
+ return this.composeInternal(instance, portMappings, /* @__PURE__ */ new Map());
1686
1743
  }
1687
1744
  /**
1688
- * Produces a renamed module instance per **MOD-010**, **MOD-011**, **MOD-012**,
1689
- * and **MOD-030**.
1745
+ * Composes a subnet {@link SubnetDef} **directly** into this builder per
1746
+ * **MOD-025** — **without instantiation**, and without the prefix-renaming
1747
+ * of {@link SubnetDef.instantiate}.
1690
1748
  *
1691
- * The rename pass walks every place and transition of the body net,
1692
- * substituting each name with `prefix + "/" + originalName`, and rebuilds
1693
- * every arc with rewritten place references. Transition timing, priority,
1694
- * and action are carried through by reference (action sharing per
1695
- * [MOD-030]). The renamed body is itself a structurally valid `PetriNet`
1696
- * per [CORE-040]; per-instance state isolation per [MOD-012] is a
1697
- * structural consequence of distinct prefixed names.
1749
+ * Every body place and transition is added under its **original**
1750
+ * (un-prefixed) name. Places merge into this builder by name: a body place
1751
+ * whose name equals an enclosing-net place *is* that place in the composed
1752
+ * flat net. This is the mode for wiring a subnet in as a single shared copy.
1698
1753
  *
1699
- * ## Prefix validation
1754
+ * Direct composition is **order-independent**: composing the same set of
1755
+ * subnets in any order yields the same flat net, because merging is by
1756
+ * place name and not by a probe of the builder's place set at call time —
1757
+ * contrast the no-interface body-inference branch of {@link composeAuto}
1758
+ * (MOD-024), which is order-sensitive.
1700
1759
  *
1701
- * The `"/"` character is reserved as the prefix separator (per [MOD-010]).
1702
- * User-supplied prefixes MUST NOT contain `"/"`; nested instantiation is
1703
- * performed by the future `PetriNetBuilder.compose(...)` mechanism (per
1704
- * [MOD-013]). A prefix containing `"/"` raises an `Error`.
1760
+ * For multiple *independent* copies each with isolated per-instance state
1761
+ * per [MOD-012] use {@link SubnetDef.instantiate} + the `compose(instance)`
1762
+ * overload instead.
1705
1763
  *
1706
- * @param prefix the rename prefix (non-empty, must not contain `"/"`)
1707
- * @param params the parameter value carried through to `Instance.params`
1708
- * (may be omitted when `P` is `void`)
1709
- * @throws when `prefix` is empty or contains `"/"`
1764
+ * Rejections: a body transition whose name already exists in this builder
1765
+ * (use `instantiate(prefix)` for independent copies); a subnet whose
1766
+ * interface declares any channel (direct composition does not bind
1767
+ * channels use `instantiate` + the channel-binding `compose` overload).
1768
+ *
1769
+ * Token-type conflicts on a same-named place cannot be detected: TS
1770
+ * {@link Place} equality is name-only at runtime (the documented carve-out,
1771
+ * same as MOD-024).
1772
+ *
1773
+ * @throws when the subnet declares channels, or a body transition name
1774
+ * collides with a transition already in this builder.
1710
1775
  */
1711
- instantiate(prefix, params) {
1712
- validatePrefix(prefix);
1713
- const placeRemap = /* @__PURE__ */ new Map();
1714
- const transitionRemap = /* @__PURE__ */ new Map();
1715
- const renamedBody = renameNet(this.body, prefix, placeRemap, transitionRemap);
1716
- const portHandles = /* @__PURE__ */ new Map();
1717
- for (const port of this.iface.ports.values()) {
1718
- const renamed = placeRemap.get(port.place.name);
1719
- if (renamed === void 0) {
1720
- throw new Error(
1721
- `Port '${port.name}' references place '${port.place.name}' that was not found in the renamed body. This indicates a SubnetDef invariant violation.`
1722
- );
1723
- }
1724
- portHandles.set(port.name, renamed);
1776
+ composeDirect(def) {
1777
+ const iface = def.iface;
1778
+ if (iface.channels.size > 0) {
1779
+ const channelNames = [];
1780
+ for (const c of iface.channels.values()) channelNames.push(c.name);
1781
+ channelNames.sort();
1782
+ throw new Error(
1783
+ `compose(SubnetDef): subnet '${def.name}' declares channels [${channelNames.join(", ")}]; direct composition does not bind channels. Use def.instantiate(prefix) + compose(instance, bind => bind.bindChannel(...)).`
1784
+ );
1725
1785
  }
1726
- const channelHandles = /* @__PURE__ */ new Map();
1727
- for (const channel of this.iface.channels.values()) {
1728
- const renamed = transitionRemap.get(channel.transition.name);
1729
- if (renamed === void 0) {
1786
+ const body = def.body;
1787
+ const hostTransitionNames = /* @__PURE__ */ new Set();
1788
+ for (const t of this._transitions) hostTransitionNames.add(t.name);
1789
+ for (const t of body.transitions) {
1790
+ if (hostTransitionNames.has(t.name)) {
1730
1791
  throw new Error(
1731
- `Channel '${channel.name}' references transition '${channel.transition.name}' that was not found in the renamed body. This indicates a SubnetDef invariant violation.`
1792
+ `compose(SubnetDef): transition '${t.name}' from subnet '${def.name}' collides with a transition already in net '${this._name}'. Direct composition merges by name; for independent copies use def.instantiate(prefix) + compose(instance).`
1732
1793
  );
1733
1794
  }
1734
- channelHandles.set(channel.name, renamed);
1735
1795
  }
1736
- return __createInstance(
1737
- prefix,
1738
- this,
1739
- renamedBody,
1740
- portHandles,
1741
- channelHandles,
1742
- params
1743
- );
1796
+ const hostByName = /* @__PURE__ */ new Map();
1797
+ for (const p of this._places) hostByName.set(p.name, p);
1798
+ const mergeMap = /* @__PURE__ */ new Map();
1799
+ for (const p of body.places) {
1800
+ const host = hostByName.get(p.name);
1801
+ this.place(host ?? p);
1802
+ if (host !== void 0) mergeMap.set(p.name, host);
1803
+ }
1804
+ for (const t of body.transitions) {
1805
+ this.transition(substitutePlaces(t, mergeMap));
1806
+ }
1807
+ return this;
1744
1808
  }
1745
1809
  /**
1746
- * Verifies safety properties of this subnet definition **in isolation** per
1747
- * **MOD-051**, by wrapping it in a synthetic enclosing net where each input
1748
- * port is fed by an {@link EnvironmentPlace} (token-source per the harness
1749
- * generator) and each output port is observed via a synthetic place. The
1750
- * standard {@link SmtVerifier} (per [MOD-050]) is invoked once per property
1751
- * declared in the harness; the resulting per-property outcomes are
1752
- * aggregated into a {@link VerificationResult}.
1753
- *
1754
- * ## Synthetic-net construction
1755
- *
1756
- * The synthetic enclosing net is built by:
1757
- * 1. Instantiating this `SubnetDef` with the prefix `"sut"` (system-under-test)
1758
- * and `harness.params`.
1759
- * 2. For each input or in-out port on the interface, looking up the
1760
- * harness generator by port name, allocating a synthetic
1761
- * {@link Place}`<unknown>` of the same conceptual token type as the
1762
- * port, wrapping it in an {@link EnvironmentPlace}, and binding the
1763
- * port to that synthetic place via
1764
- * {@link import('./petri-net.js').PetriNetBuilder.compose}. The supplier
1765
- * is invoked once at construction time to materialize the seed token —
1766
- * its presence is what bounds the input behavior under analysis. **If
1767
- * the harness map is missing a generator for a required input or in-out
1768
- * port, an `Error` is thrown.**
1769
- * 3. For each output or in-out port, allocating a synthetic observation
1770
- * {@link Place}`<unknown>` and binding the port to it via the same
1771
- * `compose(...)` call. The verifier inspects this place's reachability
1772
- * / marking through the standard property APIs ({@link SmtProperty}).
1773
- * 4. Building the resulting flat {@link PetriNet} per [MOD-023] (the
1774
- * verifier is composition-unaware per [MOD-050]).
1810
+ * Identity-default auto-compose per **MOD-024**.
1775
1811
  *
1776
- * ## Per-property invocation
1812
+ * Each declared interface port auto-binds to its own `port.place` — the
1813
+ * Place the SubnetDef builder declared via `.inputPort(name, hostPlace)`
1814
+ * (or `outputPort` / `inoutPort`). If the host builder already declares
1815
+ * the equal place, the two merge; if not, the place arrives implicitly
1816
+ * via the rewritten transitions' arcs (same flow as explicit `bindPort`).
1777
1817
  *
1778
- * Each {@link SmtProperty} in the harness is verified independently against
1779
- * the same synthetic net. The synthetic environment places are passed
1780
- * through to the verifier so that places driven by the harness generators
1781
- * are treated under the verifier's environment-analysis semantics rather
1782
- * than as ordinary sink places.
1818
+ * If the subnet declares no interface ports at all, body places are
1819
+ * checked against this builder's place set **by name** (matching the
1820
+ * existing TS Place equality semantics; see [CORE-002] note in
1821
+ * `spec/11-modular-composition.md` MOD-024). Body places that don't match
1822
+ * stay private under their prefixed names per [MOD-010].
1783
1823
  *
1784
- * @param harness the verification harness supplies parameters, input-port
1785
- * token generators, and the property set
1786
- * @returns a {@link VerificationResult} aggregating per-property
1787
- * {@link SmtVerificationResult}s
1788
- * @throws when an input or in-out port is missing a harness generator
1824
+ * Channels are NOT auto-boundtransition identity is too delicate for
1825
+ * inference. If the subnet declares any channel, this overload throws.
1789
1826
  */
1790
- async verify(harness) {
1791
- if (harness === null || harness === void 0) {
1792
- throw new Error("SubnetDef.verify: harness must not be null/undefined");
1827
+ composeAuto(instance) {
1828
+ const iface = instance.def.iface;
1829
+ if (iface.channels.size > 0) {
1830
+ const channelNames = [];
1831
+ for (const c of iface.channels.values()) channelNames.push(c.name);
1832
+ channelNames.sort();
1833
+ throw new Error(
1834
+ `compose(Instance): subnet '${instance.def.name}' (instance prefix '${instance.prefix}') declares channels [${channelNames.join(", ")}]; auto-compose does not bind channels. Use compose(instance, bind => bind.bindChannel(...)) with explicit channel bindings.`
1835
+ );
1793
1836
  }
1794
- const sut = this.instantiate("sut", harness.params);
1795
- const generators = normaliseGenerators(harness.portInputGenerators);
1796
- const properties = normaliseProperties(harness.properties);
1797
- const envPlaces = [];
1798
- const portMappings = /* @__PURE__ */ new Map();
1799
- for (const port of this.iface.ports.values()) {
1800
- const portName = port.name;
1801
- switch (port.direction) {
1802
- case "input": {
1803
- const generator = generators.get(portName);
1804
- if (generator === void 0) {
1805
- throw new Error(
1806
- `verify: harness is missing an input generator for port '${portName}' on subnet '${this.name}' (MOD-051)`
1807
- );
1808
- }
1809
- const seed = generator();
1810
- if (seed === null || seed === void 0) {
1811
- throw new Error(
1812
- `verify: input generator for port '${portName}' on subnet '${this.name}' produced null/undefined`
1813
- );
1814
- }
1815
- const synth = place(`harness_in_${portName}`);
1816
- envPlaces.push(environmentPlace(synth.name));
1817
- portMappings.set(portName, synth);
1818
- break;
1819
- }
1820
- case "output": {
1821
- const synth = place(`harness_out_${portName}`);
1822
- portMappings.set(portName, synth);
1823
- break;
1824
- }
1825
- case "inout": {
1826
- const generator = generators.get(portName);
1827
- if (generator === void 0) {
1828
- throw new Error(
1829
- `verify: harness is missing an input generator for in-out port '${portName}' on subnet '${this.name}' (MOD-051)`
1830
- );
1831
- }
1832
- const seed = generator();
1833
- if (seed === null || seed === void 0) {
1834
- throw new Error(
1835
- `verify: input generator for in-out port '${portName}' on subnet '${this.name}' produced null/undefined`
1836
- );
1837
- }
1838
- const synth = place(`harness_io_${portName}`);
1839
- envPlaces.push(environmentPlace(synth.name));
1840
- portMappings.set(portName, synth);
1841
- break;
1842
- }
1837
+ const hostByName = /* @__PURE__ */ new Map();
1838
+ for (const p of this._places) hostByName.set(p.name, p);
1839
+ if (iface.ports.size > 0) {
1840
+ const portMappings = /* @__PURE__ */ new Map();
1841
+ for (const port of iface.ports.values()) {
1842
+ const hostMatch = hostByName.get(port.place.name);
1843
+ portMappings.set(port.name, hostMatch ?? port.place);
1843
1844
  }
1845
+ return this.composeInternal(instance, portMappings, /* @__PURE__ */ new Map());
1844
1846
  }
1845
- const syntheticNet = PetriNet.builder("verify_" + this.name).compose(sut, portMappings).build();
1846
- const perProperty = /* @__PURE__ */ new Map();
1847
- for (const property of properties) {
1848
- const verifier = SmtVerifier.forNet(syntheticNet).property(property);
1849
- if (envPlaces.length > 0) {
1850
- verifier.environmentPlaces(...envPlaces);
1847
+ const mergeMap = /* @__PURE__ */ new Map();
1848
+ const prefix = instance.prefix + "/";
1849
+ for (const renamed of instance.renamedBody.places) {
1850
+ if (!renamed.name.startsWith(prefix)) continue;
1851
+ const originalName = renamed.name.substring(prefix.length);
1852
+ const hostMatch = hostByName.get(originalName);
1853
+ if (hostMatch !== void 0) {
1854
+ mergeMap.set(renamed.name, hostMatch);
1851
1855
  }
1852
- const result = await verifier.verify();
1853
- perProperty.set(property, result);
1854
1856
  }
1855
- return buildVerificationResult(syntheticNet, perProperty);
1856
- }
1857
- // ============================================================
1858
- // Static factories
1859
- // ============================================================
1860
- static builder(name) {
1861
- return new SubnetDefBuilder(name);
1857
+ return this.applyComposition(instance, mergeMap, /* @__PURE__ */ new Map());
1862
1858
  }
1863
1859
  /**
1864
- * Retrofit utility per **MOD-014**: wraps an existing closed {@link PetriNet}
1865
- * plus an {@link Interface} into an unparameterised `SubnetDef<void>`.
1860
+ * @internal Shared compose implementation: validates port and channel
1861
+ * bindings, builds the place-substitution map, walks every renamed-body
1862
+ * transition through {@link substitutePlaces}, applies channel merges per
1863
+ * [MOD-021], and adds the resulting transitions to this builder.
1866
1864
  *
1867
- * Validation per **MOD-014** / **MOD-006** is enforced before the result is
1868
- * constructed:
1869
- * - Every port's underlying `Place` must be present in `net.places`.
1870
- * - Every channel's underlying `Transition` must be present in `net.transitions`.
1871
- * - Port and channel name uniqueness is re-validated defensively (the
1872
- * `Interface` builder already enforces this; hand-built `Interface`
1873
- * values bypass that path).
1865
+ * ## Channel-merge flow ([MOD-021])
1874
1866
  *
1875
- * The resulting subnet definition is unparameterised (parameter type is `void`).
1867
+ * 1. Collect rewritten instance transitions into a working `Map<string,
1868
+ * Transition>` keyed by prefixed transition name (deferred — not yet
1869
+ * added to the builder's transition set).
1870
+ * 2. For each channel binding, resolve the renamed instance-side
1871
+ * transition through `instance.channel(channelName)`, then look up its
1872
+ * rewritten counterpart in the working map by name.
1873
+ * 3. Replace the working-map entry with a {@link mergeTransitions} result
1874
+ * that fuses caller-side + instance-side; remove the rewritten
1875
+ * instance-side entry. Also replace (or add) the caller-side
1876
+ * transition in this builder's transition set with the same merged
1877
+ * result, indexed under the caller's name slot.
1878
+ * 4. Add the surviving (un-merged) entries to this builder.
1879
+ *
1880
+ * The deferral matters: writing the rewritten instance transitions to the
1881
+ * builder eagerly would force a second "remove-then-replace" pass to
1882
+ * apply the channel merges, complicating the place-collection invariants.
1883
+ * Collecting first and merging second keeps the builder's transition set
1884
+ * finalized exactly once.
1876
1885
  *
1877
- * @throws when a port place is not in `net.places`, a channel transition is
1878
- * not in `net.transitions`, or port/channel names are not unique.
1886
+ * Keying the working map by prefixed transition name (rather than by
1887
+ * Transition reference) is also robust against a prior
1888
+ * {@link Instance.bindActions} call that may have rebuilt the renamed-body
1889
+ * transitions, breaking identity equality between the body and the
1890
+ * channel-handle map — but the prefixed names remain stable.
1879
1891
  */
1880
- static fromNet(net, iface) {
1881
- const bodyPlaces = net.places;
1882
- const bodyTransitions = net.transitions;
1883
- const seenPortNames = /* @__PURE__ */ new Set();
1884
- for (const port of iface.ports.values()) {
1885
- if (seenPortNames.has(port.name)) {
1886
- throw new Error(
1887
- `fromNet: duplicate port name '${port.name}' on interface for net '${net.name}' (MOD-006)`
1888
- );
1889
- }
1890
- seenPortNames.add(port.name);
1891
- if (!bodyPlaces.has(port.place)) {
1892
+ composeInternal(instance, portMappings, channelBindings) {
1893
+ const iface = instance.def.iface;
1894
+ const mergeMap = /* @__PURE__ */ new Map();
1895
+ for (const [portName, callerPlace] of portMappings) {
1896
+ const port = iface.port(portName);
1897
+ if (port === void 0) {
1898
+ const knownPorts = [];
1899
+ for (const p of iface.ports.values()) knownPorts.push(p.name);
1892
1900
  throw new Error(
1893
- `fromNet: port '${port.name}' references place '${port.place.name}' which is not in net '${net.name}' (MOD-014/MOD-006)`
1901
+ `compose: no port named '${portName}' on subnet '${instance.def.name}' (instance prefix '${instance.prefix}'). Known ports: [${knownPorts.join(", ")}]`
1894
1902
  );
1895
1903
  }
1904
+ const ifacePlace = instance.port(portName);
1905
+ mergeMap.set(ifacePlace.name, callerPlace);
1896
1906
  }
1897
- const seenChannelNames = /* @__PURE__ */ new Set();
1898
- for (const channel of iface.channels.values()) {
1899
- if (seenChannelNames.has(channel.name)) {
1900
- throw new Error(
1901
- `fromNet: duplicate channel name '${channel.name}' on interface for net '${net.name}' (MOD-006)`
1907
+ return this.applyComposition(instance, mergeMap, channelBindings);
1908
+ }
1909
+ /**
1910
+ * Shared post-mergeMap pipeline: rewrites renamed-body transitions
1911
+ * through `mergeMap`, applies channel merges per **MOD-021**, and adds
1912
+ * the surviving transitions to the builder.
1913
+ *
1914
+ * Used by both the explicit-binding path (`composeInternal`) and the
1915
+ * auto-compose path (`composeAuto` per **MOD-024**). The two paths differ
1916
+ * only in how the (renamed Place name → host Place) `mergeMap` is built.
1917
+ */
1918
+ applyComposition(instance, mergeMap, channelBindings) {
1919
+ const iface = instance.def.iface;
1920
+ const rewrittenByName = /* @__PURE__ */ new Map();
1921
+ for (const t of instance.renamedBody.transitions) {
1922
+ rewrittenByName.set(t.name, substitutePlaces(t, mergeMap));
1923
+ }
1924
+ for (const [channelName, callerTrans] of channelBindings) {
1925
+ let instanceRenamedChannel;
1926
+ try {
1927
+ instanceRenamedChannel = instance.channel(channelName);
1928
+ } catch (cause) {
1929
+ const knownChannels = [];
1930
+ for (const c of iface.channels.values()) knownChannels.push(c.name);
1931
+ const err = new Error(
1932
+ `compose: no channel named '${channelName}' on subnet '${instance.def.name}' (instance prefix '${instance.prefix}'). Known channels: [${knownChannels.join(", ")}]`
1902
1933
  );
1934
+ err.cause = cause;
1935
+ throw err;
1903
1936
  }
1904
- seenChannelNames.add(channel.name);
1905
- if (!bodyTransitions.has(channel.transition)) {
1937
+ const rewrittenInstanceChannel = rewrittenByName.get(instanceRenamedChannel.name);
1938
+ if (rewrittenInstanceChannel === void 0) {
1906
1939
  throw new Error(
1907
- `fromNet: channel '${channel.name}' references transition '${channel.transition.name}' which is not in net '${net.name}' (MOD-014/MOD-006)`
1940
+ `compose: channel '${channelName}' resolved to a transition '${instanceRenamedChannel.name}' that is not present in the renamed body. This indicates a SubnetDef invariant violation.`
1908
1941
  );
1909
1942
  }
1943
+ const merged = mergeTransitions(callerTrans, rewrittenInstanceChannel, callerTrans.name);
1944
+ rewrittenByName.delete(instanceRenamedChannel.name);
1945
+ this._transitions.delete(callerTrans);
1946
+ this.transition(merged);
1947
+ }
1948
+ for (const rewritten of rewrittenByName.values()) {
1949
+ this.transition(rewritten);
1910
1950
  }
1911
- return new _SubnetDef(SUBNET_DEF_KEY, net.name, net, iface);
1912
- }
1913
- };
1914
- var SubnetDefBuilder = class {
1915
- _name;
1916
- _bodyBuilder;
1917
- _ports = [];
1918
- _channels = [];
1919
- constructor(name) {
1920
- this._name = name;
1921
- this._bodyBuilder = PetriNet.builder(name);
1922
- }
1923
- // -------- body construction (delegates to PetriNet.Builder) --------
1924
- transition(transition) {
1925
- this._bodyBuilder.transition(transition);
1926
- return this;
1927
- }
1928
- transitions(...transitions) {
1929
- this._bodyBuilder.transitions(...transitions);
1930
- return this;
1931
- }
1932
- place(place2) {
1933
- this._bodyBuilder.place(place2);
1934
- return this;
1935
- }
1936
- // -------- interface declarations --------
1937
- inputPort(name, place2) {
1938
- this._ports.push({ name, direction: "input", place: place2 });
1939
- return this;
1940
- }
1941
- outputPort(name, place2) {
1942
- this._ports.push({ name, direction: "output", place: place2 });
1943
- return this;
1944
- }
1945
- inoutPort(name, place2) {
1946
- this._ports.push({ name, direction: "inout", place: place2 });
1947
1951
  return this;
1948
1952
  }
1949
- channel(name, transition) {
1950
- this._channels.push({ name, transition });
1953
+ fuse(...args) {
1954
+ if (args.length === 1 && typeof args[0] === "function") {
1955
+ const declarer = args[0];
1956
+ const fb = FusionSet.builder(this._name + "-fusion");
1957
+ declarer(fb);
1958
+ this._fusionSets.push(fb.build());
1959
+ return this;
1960
+ }
1961
+ for (const s of args) {
1962
+ if (s === void 0 || s === null) {
1963
+ throw new Error("fuse: fusion set must not be null");
1964
+ }
1965
+ this._fusionSets.push(s);
1966
+ }
1951
1967
  return this;
1952
1968
  }
1953
- // -------- build & validate (MOD-006) --------
1969
+ /**
1970
+ * Builds the immutable {@link PetriNet}, applying fusion resolution (per
1971
+ * **MOD-061**) AFTER all transition/composition accumulation:
1972
+ *
1973
+ * 1. Detect overlapping fusion sets — a single place declared in two sets
1974
+ * is rejected with an `Error`.
1975
+ * 2. Build the `non-canonical → canonical` substitution map across all
1976
+ * sets, keyed by non-canonical place name (matching the rewriter's
1977
+ * Map<string, Place<unknown>> convention — TypeScript Place identity is
1978
+ * name-based per `runtime/compiled-net.ts`).
1979
+ * 3. Walk every transition through {@link applyFusion} to rewrite arc place
1980
+ * references.
1981
+ * 4. Re-derive the place set from the rewritten transitions plus any
1982
+ * caller-declared standalone places, dropping non-canonical members.
1983
+ * Caller-declared standalone places that happen to be non-canonical
1984
+ * members are also dropped.
1985
+ *
1986
+ * If no fusion sets were registered, the build is the trivial
1987
+ * `new PetriNet(...)` — the fusion machinery has no per-build cost when
1988
+ * unused.
1989
+ *
1990
+ * @throws when two fusion sets share a place
1991
+ */
1954
1992
  build() {
1955
- const built = this._bodyBuilder.build();
1956
- const bodyPlaces = built.places;
1957
- const bodyTransitions = built.transitions;
1958
- const portNames = /* @__PURE__ */ new Set();
1959
- for (const port of this._ports) {
1960
- if (portNames.has(port.name)) {
1961
- throw new Error(`Subnet '${this._name}': duplicate port name '${port.name}'`);
1962
- }
1963
- portNames.add(port.name);
1964
- if (!bodyPlaces.has(port.place)) {
1965
- throw new Error(
1966
- `Subnet '${this._name}': port '${port.name}' references place '${port.place.name}' which is not in the body`
1967
- );
1993
+ if (this._fusionSets.length === 0) {
1994
+ return new PetriNet(PETRI_NET_KEY, this._name, this._places, this._transitions);
1995
+ }
1996
+ return this.buildWithFusion();
1997
+ }
1998
+ /**
1999
+ * @internal Fusion-resolution pass per **MOD-061**. Split out from
2000
+ * {@link build} so the no-fusion fast path stays trivial.
2001
+ */
2002
+ buildWithFusion() {
2003
+ const ownership = /* @__PURE__ */ new Map();
2004
+ for (const set of this._fusionSets) {
2005
+ for (const member of set.members) {
2006
+ const prior = ownership.get(member.name);
2007
+ if (prior !== void 0 && prior !== set) {
2008
+ throw new Error(
2009
+ `Fusion overlap: place '${member.name}' appears in two fusion sets ('${prior.name}' and '${set.name}'). A place may appear in at most one fusion set (MOD-060).`
2010
+ );
2011
+ }
2012
+ ownership.set(member.name, set);
1968
2013
  }
1969
2014
  }
1970
- const channelNames = /* @__PURE__ */ new Set();
1971
- for (const channel of this._channels) {
1972
- if (channelNames.has(channel.name)) {
1973
- throw new Error(`Subnet '${this._name}': duplicate channel name '${channel.name}'`);
2015
+ const fusionMap = /* @__PURE__ */ new Map();
2016
+ const nonCanonicalNames = /* @__PURE__ */ new Set();
2017
+ for (const set of this._fusionSets) {
2018
+ const canonical = set.canonical;
2019
+ for (const nc of set.nonCanonical()) {
2020
+ fusionMap.set(nc.name, canonical);
2021
+ nonCanonicalNames.add(nc.name);
1974
2022
  }
1975
- channelNames.add(channel.name);
1976
- if (!bodyTransitions.has(channel.transition)) {
1977
- throw new Error(
1978
- `Subnet '${this._name}': channel '${channel.name}' references transition '${channel.transition.name}' which is not in the body`
1979
- );
2023
+ }
2024
+ const rewrittenTransitions = applyFusion(this._transitions, fusionMap);
2025
+ const rebuiltPlaces = /* @__PURE__ */ new Set();
2026
+ for (const p of this._places) {
2027
+ if (!nonCanonicalNames.has(p.name)) {
2028
+ rebuiltPlaces.add(p);
1980
2029
  }
1981
2030
  }
1982
- const ifaceBuilder = Interface.builder();
1983
- ifaceBuilder.portsAll(this._ports);
1984
- ifaceBuilder.channelsAll(this._channels);
1985
- const iface = ifaceBuilder.build();
1986
- return new SubnetDef(SUBNET_DEF_KEY, this._name, built, iface);
2031
+ for (const t of rewrittenTransitions) {
2032
+ for (const spec of t.inputSpecs) rebuiltPlaces.add(spec.place);
2033
+ for (const p of t.outputPlaces()) rebuiltPlaces.add(p);
2034
+ for (const inh of t.inhibitors) rebuiltPlaces.add(inh.place);
2035
+ for (const r of t.reads) rebuiltPlaces.add(r.place);
2036
+ for (const r of t.resets) rebuiltPlaces.add(r.place);
2037
+ }
2038
+ return new PetriNet(PETRI_NET_KEY, this._name, rebuiltPlaces, rewrittenTransitions);
1987
2039
  }
1988
2040
  };
1989
- function validatePrefix(prefix) {
1990
- if (typeof prefix !== "string" || prefix.length === 0) {
1991
- throw new Error("SubnetDef.instantiate: prefix must be a non-empty string");
2041
+ function rebuildWithAction(t, action) {
2042
+ const builder = Transition.builder(t.name).timing(t.timing).priority(t.priority).action(action);
2043
+ if (t.inputSpecs.length > 0) {
2044
+ builder.inputs(...t.inputSpecs);
1992
2045
  }
1993
- if (prefix.indexOf("/") >= 0) {
1994
- throw new Error(
1995
- `SubnetDef.instantiate: prefix must not contain '/' (reserved as the prefix separator per MOD-010); use compose(...) for nested instantiation. Got: '${prefix}'`
1996
- );
2046
+ if (t.outputSpec !== null) {
2047
+ builder.outputs(t.outputSpec);
2048
+ }
2049
+ for (const inh of t.inhibitors) {
2050
+ builder.inhibitor(inh.place);
2051
+ }
2052
+ for (const r of t.reads) {
2053
+ builder.read(r.place);
1997
2054
  }
2055
+ for (const r of t.resets) {
2056
+ builder.reset(r.place);
2057
+ }
2058
+ return builder.build();
2059
+ }
2060
+
2061
+ // src/core/subnet.ts
2062
+ function closedSubnet(net) {
2063
+ return { kind: "closed", net };
2064
+ }
2065
+ function openSubnet(def) {
2066
+ return { kind: "open", def };
1998
2067
  }
1999
2068
 
2000
2069
  // src/runtime/marking.ts