tla-ts 0.0.3-dev.e9d8347 → 0.0.3-dev.f362080

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/transfer.cjs CHANGED
@@ -20,7 +20,7 @@ The exception was a java.lang.RuntimeException
20
20
  `).length-1,n.index+=i.length;continue}if(r.startsWith(`\\*`)){let e=r.indexOf(`
21
21
  `);n.index+=e===-1?r.length:e;continue}if(r.startsWith(`(*`)){let e=r.indexOf(`*)`);if(e===-1)throw new l(`Unterminated comment at line ${n.line}`);n.line+=r.slice(0,e).split(`
22
22
  `).length-1,n.index+=e+2;continue}let a=/^\d+(?![A-Za-z_])/.exec(r)?.[0];if(a){t.push({kind:`Number`,text:a,line:n.line}),n.index+=a.length;continue}let o=/^[A-Za-z0-9_!]+/.exec(r)?.[0];if(o){t.push({kind:`Word`,text:o,line:n.line}),n.index+=o.length;continue}let s=/^"((?:[^"\\]|\\.)*)"/.exec(r);if(s){t.push({kind:`String`,text:s[1],line:n.line}),n.index+=s[0].length;continue}let c=E.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let u=ie(e,n.index);if(!u)throw new l(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:u.text,line:n.line}),n.index+=u.length}return t.push({kind:`End`,text:``,line:n.line}),t},ce=e=>{let t=D(e),n={index:0},r={init:void 0,next:void 0,specification:void 0,invariants:[],properties:[],constraints:[],actionConstraints:[],constants:[],substitutions:[],checkDeadlock:!0,symmetry:void 0,view:void 0,alias:void 0,postconditions:[],possible:[],periodic:void 0,rlReward:void 0},i={isSet:!1},a=()=>t[n.index],o=()=>t[n.index++],s=e=>e.kind===`Word`&&T.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new l(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},u=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new l(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},d=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;d();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new l(`The keyword ${t} appears twice`);r[e]=u()},m=()=>{let e=o();if(e.kind===`Number`)return{kind:`Number`,value:Number(e.text)};if(e.kind===`String`)return{kind:`String`,value:e.text};if(e.text===`TRUE`||e.text===`FALSE`)return{kind:`Boolean`,value:e.text===`TRUE`};if(e.text===`{`){let e=[];for(a().text!==`}`&&e.push(m());a().text===`,`;)o(),e.push(m());return c(`}`),{kind:`Set`,elements:e}}if(e.kind===`Word`&&!s(e))return{kind:`ModelValue`,name:e.text};throw new l(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=u();return c(`]`),e},g=()=>{for(;d();){let e=o().text;if(a().text===`(`){o();let t=[];for(;a().text!==`)`;)t.push(m()),a().text!==`)`&&c(`,`);o(),c(`=`);let n=h();r.constants.push({name:e,value:m(),args:t,...n&&{module:n}});continue}if(a().text===`<-`){o();let t=h();r.substitutions.push({name:e,target:u(),...t&&{module:t}});continue}c(`=`);let t=h();r.constants.push({name:e,value:m(),...t&&{module:t}})}};for(;a().kind!==`End`;){let e=o();switch(e.text){case`CONSTANT`:case`CONSTANTS`:g();break;case`INIT`:p(`init`,e.text);break;case`NEXT`:p(`next`,e.text);break;case`SPECIFICATION`:p(`specification`,e.text);break;case`SYMMETRY`:p(`symmetry`,e.text);break;case`VIEW`:p(`view`,e.text);break;case`INVARIANT`:case`INVARIANTS`:r.invariants.push(...f());break;case`PROPERTY`:case`PROPERTIES`:r.properties.push(...f());break;case`CONSTRAINT`:case`CONSTRAINTS`:r.constraints.push(...f());break;case`ACTION_CONSTRAINT`:case`ACTION_CONSTRAINTS`:r.actionConstraints.push(...f());break;case`ALIAS`:p(`alias`,e.text);break;case`POSTCONDITION`:case`POSTCONDITIONS`:r.postconditions.push(...f());break;case`_POSSIBLE`:r.possible.push(...f());break;case`_PERIODIC`:p(`periodic`,e.text);break;case`_RL_REWARD`:p(`rlReward`,e.text);break;case`CHECK_DEADLOCK`:{if(i.isSet)throw new l(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new l(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new l(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new l(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},O={Set:{open:`{ `,close:` }`,pad:` `,separator:`,`,divider:``,dividerPad:``},Sequence:{open:`<< `,close:` >>`,pad:` `,separator:`,`,divider:``,dividerPad:``},Subset:{open:`SUBSET `,close:` `,pad:` `,separator:`,`,divider:``,dividerPad:``},Function:{open:`( `,close:` )`,pad:` `,separator:` @@`,divider:` :>`,dividerPad:` `},Record:{open:`[ `,close:` ]`,pad:` `,separator:`,`,divider:` |->`,dividerPad:` `}},k=e=>{if(e===void 0)return!1;let t=e.charCodeAt(0);return t>=9&&t<=13||t>=28&&t<=32||t===5760||t>=8192&&t<=8202&&t!==8199||t===8232||t===8233||t===8287||t===12288},le=e=>e!==void 0&&/^\p{Nd}$/u.test(e),ue=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),de=e=>{let t=t=>{let n=oe(e,t,k);return e[n]===void 0?void 0:n},n=(n,r,i,a,o,s)=>{let c={kind:n,first:r,last:r,children:[]},l={index:t(i)};for(;l.index!==void 0&&e[l.index]!==a;){let n=o(l.index);if(!n)return;c.children.push(n);let r=t(n.last+1);if(r===void 0)return;let i=e.startsWith(s,r)?r+s.length:r;l.index=t(i)}if(l.index!==void 0)return c.last=l.index,c},r=(n,r)=>{let i=o(n);if(!i)return;let a=t(i.last+1);if(a===void 0||!e.startsWith(r,a))return;let s=t(a+r.length),c=s===void 0?void 0:o(s);if(c)return{kind:`Pair`,first:n,last:c.last,children:[i,c]}},i=t=>{let n=oe(e,t,le);if(e.startsWith(`..`,n)&&le(e[n+2]))return{kind:`Constant`,first:t,last:oe(e,n+2,le)-1,children:[]}},a=t=>{let n=e[t];if(n===`"`){let n=e.indexOf(`"`,t+1);return n===-1?void 0:{kind:`Constant`,first:t,last:n,children:[]}}let r=le(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:oe(e,t+1,le)-1,children:[]};if(ue(n))return{kind:`Constant`,first:t,last:oe(e,t,ue)-1,children:[]}},o=i=>{let s=t(i);if(s!==void 0)switch(e[s]){case`{`:return n(`Set`,s,s+1,`}`,o,`,`);case`<`:{let t=n(`Sequence`,s,s+2,`>`,o,`,`);return!t||t.last+1>=e.length?void 0:{...t,last:t.last+1}}case`[`:return n(`Record`,s,s+1,`]`,e=>r(e,`|->`),`,`);case`(`:return n(`Function`,s,s+1,`)`,e=>r(e,`:>`),`@@`);default:{if(!e.startsWith(`SUBSET`,s))return a(s);let n=t(s+6),r=n===void 0?void 0:o(n);return r?{kind:`Subset`,first:s,last:r.last,children:[r]}:void 0}}};return o(0)},fe=e=>{let t=e.length===0?void 0:de(e);if(!t||t.last<e.length-1)return e;let n=t=>e.slice(t.first,t.last+1),r=e=>e.last-e.first+1,i=(e,t,i,s)=>{if(r(e)<=t-i||e.kind===`Constant`)return n(e);if(e.kind===`Pair`)return;let c=O[e.kind],l=t-Math.max(c.open.length,c.pad.length),u=i+c.close.length,d=s+c.pad,f=e.kind===`Function`||e.kind===`Record`?o(e.children,l,u,d,c):a(e.children,l,u,d,c.separator);return f===void 0?void 0:c.open+f+c.close},a=(e,t,n,r,a)=>{let o=e.map((o,s)=>{let c=s===e.length-1,l=i(o,t,c?n:a.length,r);return l===void 0||c?l:`${l}${a}\n${r}`});return o.includes(void 0)?void 0:o.join(``)},o=(e,t,a,o,s)=>{let c=e.map((c,l)=>{let u=l===e.length-1,d=c.children[0],f=c.children[1],p=u?``:`${s.separator}\n${o}`,m=r(d)+s.divider.length+1+r(f);if(u?m<=t-a:m+s.separator.length<=t)return`${n(d)}${s.divider} ${n(f)}${p}`;let h=r(d)+s.divider.length<=t?n(d):i(d,t,a+s.divider.length,o),g=i(f,t-s.dividerPad.length,u?a:s.separator.length,o+s.dividerPad);return h===void 0||g===void 0?void 0:`${h}${s.divider}\n${o}${s.dividerPad}${g}${p}`});return c.includes(void 0)?void 0:c.join(``)};return i(t,80,0,``)??e},A=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,pe=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`||e.kind===`Interval`),j=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),M=e=>typeof e==`object`&&e.kind===`ModelValue`,me=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:A(e)?`set`:`function`,he=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new d(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},ge={isUsed:!1},_e=(e,t)=>(ge.isUsed=!0,e.writtenKeys=t,e),ve=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Yt(e.domain,t)]]),ye=(e,t,n)=>_e(Le(e,t),n),be=e=>{if(!(typeof e!=`object`||e.kind!==`Function`&&e.kind!==`Set`)&&e.hash===0&&e.fingerprint===0){if(e.kind===`Set`)return e.elements.forEach(be);e.writtenKeys=void 0,e.range.forEach(be)}},xe=e=>e.every(e=>typeof e==`string`),Se=(e,t)=>e.length>0&&t.length>0&&xe(e)!==xe(t),Ce=(e,t)=>{ge.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Se(n,r)?rt(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=L(n[e],r[e]);if(t!==0)return t}let i=e.range,a=t.range;if(i===a)return 0;for(let e=0;e<i.length;e++){let t=L(i[e],a[e]);if(t!==0)return t}return 0},N=(e,t)=>{let n=e.elements,r=t.elements;if(n.length!==r.length)return n.length-r.length;if(n===r)return 0;for(let e=0;e<n.length;e++){let t=L(n[e],r[e]);if(t!==0)return t}return 0},we=e=>M(e)?e.type:void 0,Te=(e,t,n)=>{let r=we(e),i=we(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(M(e)&&M(t)){if(r===void 0||i===void 0||r===i)return;throw new u(`Attempted to ${a} the differently-typed model values ${U(e)} and ${U(t)}`)}throw r===void 0?new u(`Attempted to ${a} the typed model value ${U(t)} and the non-model value\n${U(e)}`):new u(`Attempted to ${a}${n?``:` the`} typed model value ${U(e)} and non-model value\n${U(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0,Pe.clear();for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,P=e=>Ee.ranks[Oe(e)],Ae=(e,t)=>{if(e===t)return 0;let n=Ee.ranks,r=n[e],i=n[t];return r!==void 0&&i!==void 0?r-i:r===void 0?i===void 0&&e<t?-1:1:-1},F=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),je=(e,t)=>(F(e)||F(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Me=(e,t,n)=>{if(!F(e))throw new u(`Attempted to ${n} the set ${U(z(e)?B(e):e,!0)} with the value:\n${U(t,!0)}`);if(!F(t))throw new u(`Attempted to compare overridden value ${U(e,!0)} with non-overridden value:\n${U(t,!0)}`);let r=e.kind===`InfiniteSet`?e.name:void 0,i=t.kind===`InfiniteSet`?t.name:void 0;if(r!==void 0&&r===i)return 0;if(r===`Nat`&&i===`Int`||r===`Int`&&i===`Nat`)return r===`Nat`?-1:1;throw new u(`Attempted to compare ${U(e,!0)} with the value\n${U(t,!0)}`)},I={kind:`InfiniteSet`,name:`Any`},Ne=e=>{throw new u(`Attempted to compare ANY with the value\n${U(e)}`)},L=(e,t)=>{if(e===t)return ge.isUsed&&be(e),e===I?Ne(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return Ce(e,t);if(n===`Set`&&r===`Set`)return N(e,t);if(n===`Interval`&&r===`Interval`){let n=Math.max(0,e.high-e.low+1),r=Math.max(0,t.high-t.low+1);return n!==r||n===0?n-r:e.low-t.low}if(n===`ModelValue`&&r===`ModelValue`)return e.type!==void 0&&t.type!==void 0&&Te(e,t,!1),e.rank-t.rank;if(n===`ModelValue`||r===`ModelValue`)return Te(e,t,!1),n===`ModelValue`?-1:1;if(e===I||t===I)return Ne(e===I?t:e);if(A(e)&&A(t)&&je(e,t))return Me(e,t,`compare`);let i=A(e);if(i!==A(t))throw new u(`Attempted to compare ${me(e)} ${U(e)} with ${me(t)} ${U(t)}`);return i?N(B(e),Ht(t)):Ce(H(e),H(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return Ae(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(Te(e,t,!1),M(e))return-1;if(M(t))return 1;throw new u(`Attempted to compare ${me(e)} ${U(e)} with ${me(t)} ${U(t)}`)},Pe=/* @__PURE__ */ new Map,Fe=e=>{let t=Pe.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0,rank:P(e)};return Pe.set(e,n),n},Ie=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),Le=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Re=(e,t)=>{if(e===t)return ge.isUsed&&be(e),e!==I||Ne(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&Te(e,t,!0),L(e,t)===0);if((e.kind===`Set`||e.kind===`Function`)&&(t.kind===`Set`||t.kind===`Function`)&&e.hash!==0&&t.hash!==0&&e.hash!==t.hash)return!1;if(e.kind===`Function`&&t.kind===`Function`)return Ce(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return N(e,t)===0;if(M(e)||M(t))return Te(e,t,!0),M(e)&&M(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Re(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=V(e.domain),r=V(t.domain);if(n||r)return n===r;let i=V(e.range),a=V(t.range);return i||a?i===a:Re(e.domain,t.domain)&&Re(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(V(e))return V(t);if(V(t))return!1;let n=e=>e.names.map((t,n)=>[t,e.sets[n]]).sort((e,t)=>e[0]<t[0]?-1:1),r=n(t);return e.names.length===t.names.length&&n(e).every((e,t)=>e[0]===r[t][0]&&Re(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return V(e)?V(t):!V(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Re(e,t.factors[n]));let n=A(e)&&A(t)&&!(z(e)&&z(t));if(n&&je(e,t))return Me(e,t,`check equality of`)===0;if(n){if(!F(e)){let n=B(e);if(!F(t))return N(n,B(t))===0}return Mn(e)===Mn(t)}return L(e,t)===0},ze=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>Be(e,t[n])),Be=(e,t)=>e===t?!0:typeof e!=`object`||typeof t!=`object`?!1:e.kind===`Set`&&t.kind===`Set`?(e.hash===0||t.hash===0||e.hash===t.hash)&&ze(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&ze(e.domain,t.domain)&&ze(e.range,t.range):Mn(e)===Mn(t),Ve=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},He=e=>{if(!j(e))return;let t=H(e);return{domain:t.domain,range:t.range,low:Ve(t.domain)}},Ue=e=>{if(!j(e))return;let t=H(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},We=(e,t)=>{if(we(e)!==void 0)throw new u(`Attempted to compare the typed model value ${U(e)} and the non-model value\n${U(t)}`);return 1},Ge=(e,t)=>{if(we(e)!==void 0)throw new u(`Attempted to check equality of the typed model value ${U(e)} and the non-model value\n${U(t)}`);return!1},Ke=(e,t)=>{let n=we(e);if(n===void 0)return M(t)?e.rank-t.rank:-1;if(!M(t))throw new u(`Attempted to compare the typed model value ${U(e)} and non-model value\n${U(t)}`);let r=we(t);if(r!==void 0&&r!==n)throw new u(`Attempted to compare the differently-typed model values ${U(e)} and ${U(t)}`);return e.rank-t.rank},qe=(e,t)=>{let n=we(e);if(n===void 0)return M(t)&&e.name===t.name;if(!M(t))throw new u(`Attempted to check equality of typed model value ${U(e)} and non-model value\n${U(t)}`);let r=we(t);if(r!==void 0&&r!==n)throw new u(`Attempted to check equality of the differently-typed model values ${U(e)} and ${U(t)}`);return e.name===t.name},Je={number:{name:`integer`,other:`non-integer`,equality:`check equality of`},string:{name:`string`,other:`non-string`,equality:`check equality of`},boolean:{name:`boolean`,other:`non-boolean`,equality:`compare equality of`}},Ye=(e,t,n)=>{let r=Je[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${U(e)} with ${r.other}:\n${U(t)}`},Xe=(e,t,n)=>{let r=He(n);if(!r){if(M(n))return We(n,t);throw new u(`Attempted to compare the function ${U(t)} with the value:\n${U(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>Ze(e.range,r.range);if(e.low!==void 0&&e.range.length===0)return 0;if(e.low!==void 0&&r.low!==void 0)return e.low===r.low?i():e.low-r.low;let a=e.low??r.low;if(a===void 0)return Ze(e.domain,r.domain)||i();let o=e.low===void 0?e.domain:r.domain;for(let t=0;t<o.length;t++){let n=o[t];if(typeof n!=`number`)throw new u(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${U(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},Ze=(e,t)=>{for(let n=0;n<e.length;n++){let r=rt(e[n],t[n]);if(r!==0)return r}return 0},Qe=(e,t)=>{let n=H(e);if(e.kind===`Function`&&Xt(n)!==void 0){let r=j(t)?Xt(t):void 0;return r?n.range.length-r.length||Ze(n.range,r):Xe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ue(t);if(!r){if(M(t))return We(t,e);throw new u(`Attempted to compare record:\n${U(e)}\nwith non-record\n${U(t)}`)}return n.domain.length-r.domain.length||Ze(n.domain,r.domain)||Ze(n.range,r.range)}return Xe({...n,low:Ve(n.domain)},e,t)},$e=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),et=(e,t)=>F(e)||F(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),tt=(e,t)=>{let n=$e(t)?t:B(t);return{elements:B(e).elements,otherElements:B(n).elements}},nt=(e,t)=>{if(et(e,t))return L(e,t);if(!$e(e))return nt(B(e),t);if(!A(t)){if(M(t))return We(t,e);throw new u(`Attempted to compare the set ${U(e)} with the value:\n${U(t)}`)}let n=tt(e,t);return n.elements.length-n.otherElements.length||Ze(n.elements,n.otherElements)},rt=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return L(e,t);if(M(t))return We(t,e);throw new u(Ye(e,t,!1))}return e.kind===`ModelValue`?Ke(e,t):j(e)?Qe(e,t):nt(e,t)},it=(e,t)=>e.every((e,n)=>ct(e,t[n])),at=(e,t,n)=>{let r=He(n);if(!r){if(M(n))return Ge(n,t);throw new u(`Attempted to check equality of the function ${U(t)} with the value:\n${U(n)}`)}if(e.range.length!==r.range.length)return!1;if(e.low!==void 0&&r.low!==void 0)return(e.range.length===0||e.low===r.low)&&it(e.range,r.range);let i=e.low??r.low;return i===void 0?it(e.domain,r.domain)&&it(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new u(`Attempted to compare an integer with non-integer:\n${U(e)}.`);return e===i+t})&&it(e.range,r.range)},ot=(e,t)=>{let n=H(e);if(e.kind===`Function`&&Xt(n)!==void 0){let r=j(t)?Xt(t):void 0;return r?n.range.length===r.length&&it(n.range,r):at({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ue(t);if(!r){if(M(t))return Ge(t,e);throw new u(`Attempted to check equality of record:\n${U(e)}\nwith non-record\n${U(t)}`)}return n.domain.length===r.domain.length&&it(n.domain,r.domain)&&it(n.range,r.range)}return at({...n,low:Ve(n.domain)},e,t)},st=(e,t)=>{if(et(e,t))return Re(e,t);if(!$e(e))return st(B(e),t);if(!A(t)){if(M(t))return Ge(t,e);throw new u(`Attempted to check equality of the set ${U(e)} with the value:\n${U(t)}`)}let n=tt(e,t);return n.elements.length===n.otherElements.length&&it(n.elements,n.otherElements)},ct=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(M(t))return Ge(t,e);throw new u(Ye(e,t,!0))}return e.kind===`ModelValue`?qe(e,t):j(e)?ot(e,t):st(e,t)},lt=e=>{let t=[];for(let n of e){let e={low:0,high:t.length,comparison:1};for(;e.low<e.high;){let r=e.low+(e.high-e.low>>1);if(e.comparison=rt(n,t[r]),e.comparison===0)break;e.comparison<0?e.high=r:e.low=r+1}e.comparison!==0&&t.splice(e.low,0,n)}return t},ut=(e,t)=>t.some(t=>ct(t,e)),dt=(e,t)=>{try{return Re(e,t)}catch(n){if(!(n instanceof u))throw n;return ct(e,t)}},ft=e=>{for(let t=1;t<e.length;t++)if(L(e[t],e[t-1])<=0)return!1;return!0},pt=e=>{if(e.length<2||ft(e))return Ie(e);let t=[...e].sort(L),n=[t[0]];for(let e=1;e<t.length;e++)L(n[n.length-1],t[e])!==0&&n.push(t[e]);return Ie(n)},mt=e=>{try{return pt(e)}catch(t){if(!(t instanceof u))throw t;return{kind:`MixedSet`,elements:e}}},ht=Ie([]),gt=(e,t)=>{if(e.elements.length===0||t.elements.length===0)return e;let n=e.elements,r=t.elements,i=[],a=0;for(let e=0;e<n.length;e++){let t=n[e];for(;a<r.length&&L(r[a],t)<0;)a++;a<r.length&&L(r[a],t)===0||i.push(t)}return i.length===n.length?e:Ie(i)},_t=(e,t)=>{let n=e.elements,r=t.elements,i=[],a=0;for(let e=0;e<n.length;e++){let t=n[e];for(;a<r.length&&L(r[a],t)<0;)a++;a<r.length&&L(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Ie(i)},vt=(e,t)=>{if(e.elements.length===0)return t;if(t.elements.length===0)return e;let n=e.elements,r=t.elements,i=[],a=[],o=0,s=0;for(;o<n.length&&s<r.length;){let e=L(n[o],r[s]);e<0?i.push(n[o++]):e>0?(a.push(r[s]),i.push(r[s++])):(i.push(n[o++]),s++)}for(;o<n.length;)i.push(n[o++]);for(;s<r.length;)a.push(r[s]),i.push(r[s++]);if(a.length===0)return e;let c=Ie(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+bn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+Dn(a[e])|0;c.fingerprint=t}return c},yt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>L(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&L(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return Le(n,r)},bt=e=>{let t=yt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:_e(t,e.map(e=>e[0]))},xt=[],St=[],Ct=/* @__PURE__ */ new WeakMap,wt=e=>{let t=xt[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return xt[e]=n,n},Tt=e=>{if(Kt(e),e.low===1&&e.high>=1)return wt(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Et=e=>e.length>0&&(e===xt[e.length]||Ct.has(e)),Dt=e=>{let t=B(e).elements;return Et(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},Ot=e=>{let t=St[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return St[e]=n,n},R=e=>Le(wt(e.length),e),kt=e=>yt(e),At=e=>A(e)&&!F(e),jt=(e,t=!1)=>`UNION(${U(e.base,t)})`,Mt=(e,t=e=>B(e).elements)=>{if(!At(e.base))throw new u(`Attempted to enumerate the nonenumerable set:\n${jt(e)}`);return t(e.base)},Nt=(e,t,n)=>{if(At(e))return e;throw new u(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${U(e)}\nwhen enumerating:\n${jt(n)}`)},z=e=>{if(typeof e!=`object`)return!1;switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:return!0;case`InfiniteSet`:case`SequenceSet`:return!1;case`Subset`:case`FilteredSet`:return z(e.base);case`FunctionSet`:return z(e.domain)&&z(e.range)||Pt(e.domain)===!0||Pt(e)===!0;case`RecordSet`:return e.sets.every(z)||Pt(e)===!0;case`ProductSet`:return e.factors.every(z)||Pt(e)===!0;case`Union`:return z(e.base)&&Ut(e.base).every(z);case`SetOperation`:return e.operator===`\\cup`?z(e.left)&&z(e.right):e.operator===`\\cap`?z(e.left)||z(e.right):z(e.left);default:return!1}},Pt=e=>{if(typeof e==`object`)switch(e.kind){case`Set`:case`MixedSet`:return e.elements.length===0;case`Interval`:return e.low>e.high;case`InfiniteSet`:return e.name===`Any`&&void 0;case`Subset`:case`SequenceSet`:return!1;case`FunctionSet`:{let t=Pt(e.domain);return t===void 0?void 0:!t&&Pt(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=Pt(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return z(e)?B(e).elements.length===0:void 0;default:return}},Ft=e=>{let t=e.reduce((e,t)=>e*t.length,1),n=[],r=e.map(()=>0);for(let i=0;i<t;i++){n.push(e.map((e,t)=>e[r[t]]));for(let t=e.length-1;t>=0&&(r[t]++,!(r[t]<e[t].length));t--)r[t]=0}return n},It=(e,t)=>{for(let n=e.length-1;n>=0;n--)if(e[n]<t-e.length+n){e[n]++;for(let t=n+1;t<e.length;t++)e[t]=e[t-1]+1;return!0}return!1},Lt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Ie(r.map(t=>e[t])));while(It(r,e.length))}return t},Rt=e=>{if(typeof e!=`object`)throw new u(`Expected a set but found ${U(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return lt(e.elements);case`Interval`:{let t=Tt(e);return t!==xt[t.length]&&Ct.set(t,e),t}case`Subset`:{F(e.base)&&Kt(e.base);let t=B(e.base).elements;if(t.length>30)throw new u(Gt(e));return Lt(t)}case`FunctionSet`:{if(V(e))return[];if(F(e.domain))throw new u(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${U(e.domain)}\ncannot be enumerated.`);let t=Dt(e.domain);if(t.length>0&&F(e.range))throw new u(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${U(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:B(e.range).elements;return Ft(t.map(()=>n)).map(e=>Le(t,e))}case`RecordSet`:{if(V(e))return[];let t=e.sets.map((t,n)=>{if(F(t))throw new u(`Attempted to enumerate a set of the form [l1 : v1, ..., ln : vn],\nbut can't enumerate the value of the \`${e.names[n]}' field:\n${U(t)}`);return B(t).elements});return Ft(t).map(t=>kt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(V(e))return[];let t=e.factors.map((e,t)=>{if(F(e))throw new u(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${U(e)}`);return B(e).elements});return Ft(t).map(R)}case`FilteredSet`:if(F(e.base))throw new u(`Attempted to enumerate { x \\in S : p(x) } when S:\n${U(e.base)}\nis not enumerable`);return B(e.base).elements.filter(e.test);case`Union`:{let t=Mt(e).flatMap((t,n)=>B(Nt(t,n,e)).elements);return B(mt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(F(e.left)||F(e.right)))throw new u(`Attempted to enumerate S \\cup T when S:\n${U(e.left)}\nand T:\n${U(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&F(e.left)&&F(e.right))throw new u(`Attempted to enumerate S \\cap T when neither S:\n${U(e.left)}\nnor T:\n${U(e.right)}\nis enumerable`);if(e.operator===`\\`&&F(e.left))throw new u(`Attempted to enumerate S \\ T when S:\n${U(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return vt(B(e.left),B(e.right)).elements;let t=e.operator===`\\`||z(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return B(n).elements.filter(t=>$t(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new u(`Attempted to enumerate the non-enumerable set ${U(e)}`);default:throw new u(`Expected a set but found ${U(e)}`)}},zt=/* @__PURE__ */ new WeakMap,Bt=/* @__PURE__ */ new WeakMap,B=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Ie(Rt(e));let t=typeof e==`object`?zt.get(e):void 0;if(t)return t;let n=(typeof e==`object`?Bt.get(e):void 0)??Vt(e);return typeof e==`object`&&zt.set(e,n),n},Vt=e=>{let t=Rt(e);return typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Ie(t):pt(t)},Ht=e=>{if(typeof e!=`object`||e.kind!==`Union`&&e.kind!==`SetOperation`&&e.kind!==`FilteredSet`||zt.has(e))return B(e);let t=Bt.get(e);if(t)return t;let n=Vt(e);return Bt.set(e,n),n},Ut=e=>{if(typeof e!=`object`||zt.has(e))return B(e).elements;if(e.kind===`Union`)return Mt(e,Ut).flatMap((t,n)=>Ut(Nt(t,n,e)));if(e.kind===`FilteredSet`&&!F(e.base))return Ut(e.base).filter(e.test);if(e.kind!==`SetOperation`)return B(e).elements;if(e.operator===`\\cup`)return F(e.left)||F(e.right)?B(e).elements:[...Ut(e.left),...Ut(e.right)];let t=e.operator===`\\`||z(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return F(n)?B(e).elements:Ut(n).filter(t=>$t(t,r)===(e.operator===`\\cap`))},V=e=>{if(typeof e!=`object`||!A(e))throw new u(`Shouldn't call isEmpty() on value ${U(e)}`);switch(e.kind){case`Set`:case`MixedSet`:return e.elements.length===0;case`Interval`:return e.low>e.high;case`InfiniteSet`:if(e.name===`Any`)throw new u(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(V);case`RecordSet`:return e.sets.some(V);case`FunctionSet`:return!V(e.domain)&&V(e.range);case`Union`:return Mt(e).every((t,n)=>V(Nt(t,n,e)));default:return B(e).elements.length===0}},Wt=(e,t)=>e.reduce((e,n)=>{let r=e*Kt(n);if(r>2147483647)throw new u(t());return r},1),Gt=e=>`Overflow when computing the number of elements in:\n${U(e)}`,Kt=e=>{if(typeof e!=`object`)return B(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new u(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new u(`Attempted to compute the number of elements in the overridden value ${U(e)}.`);case`Subset`:{let t=Kt(e.base);if(t>=31)throw new u(Gt(e));return 2**t}case`FunctionSet`:{if(V(e.domain))return 1;if(V(e.range))return 0;let t=Kt(e.range);if(t===1)return 1;let n=Kt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new u(Gt(e));return r.value}case`RecordSet`:return V(e)?0:Wt(e.sets,()=>Gt(e));case`ProductSet`:return V(e)?0:Wt(e.factors,()=>`Overflow when computing the number of elements in ${U(e)}`);default:return B(e).elements.length}},qt=e=>`Attempted to check if the set ${U(e)}is finite.`,Jt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new u(`Attempted to check if the ${me(e)} ${U(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new u(`Attempted to check if the function:\n${U(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return Jt(e.base)&&V(e.base);case`Subset`:return Jt(e.base);case`FunctionSet`:{let t=Jt(e.domain);if(t&&V(e.domain))return!0;let n=Jt(e.range);return n&&V(e.range)?!0:n&&(t||Kt(e.range)===1)}case`RecordSet`:case`ProductSet`:{let t=e.kind===`RecordSet`?e.sets:e.factors,n={isAllFinite:!0};for(let e of t)if(!Jt(e))n.isAllFinite=!1;else if(V(e))return!0;return n.isAllFinite}case`Union`:if(!At(e.base))throw new u(`Attempted to check if the nonenumerable set:\n${jt(e)}\nis a finite set.`);return B(e.base).elements.every(Jt);case`SetOperation`:if(e.operator===`\\cup`)return Jt(e.left)&&Jt(e.right);if(e.operator===`\\cap`){if(!Jt(e.left)&&!Jt(e.right))throw new u(qt(e));return!0}if(Jt(e.left))return!0;if(!Jt(e.right))throw new u(qt(e));return!1;case`FilteredSet`:if(!Jt(e.base))throw new u(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${U(e.base)}\nis finite.`);return!0}},Yt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=L(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Xt=e=>{if(typeof e!=`object`||!j(e))return;let t=H(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Zt=(e,t)=>`Attempted to check if the value ${U(e)} is an element of ${U(t)}`,Qt=(e,t)=>{let n=`Attempted to check if the value:\n${U(e)}\nis an element of the`;if(!j(t))return`${n} ${me(t)} ${U(t)}`;let r=t.kind===`Function`||z(t.domain)?H(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${U(e)}\nis in the record:\n${U(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${U(t)}`},$t=(e,t)=>{if(!A(t))throw new u(Qt(e,t));switch(t.kind){case`Set`:try{return Yt(t.elements,e)!==-1}catch(n){if(!(n instanceof u))throw n;return ut(e,t.elements)}case`MixedSet`:return ut(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!M(e))throw new u(`Attempted to check if the value:\n${U(e)}\nis in the integer interval ${U(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(M(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new u(`Attempted to check if the value:\n${U(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!A(e))throw new u(Zt(e,t));return B(e).elements.every(e=>$t(e,t.base));case`FunctionSet`:{if(!j(e)){if(M(e))return!1;throw new u(Zt(e,t))}let n=H(e),r=Ve(n.domain);return(r===void 0?dt(t.domain,Ie(n.domain)):dt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>$t(e,t.range))}case`RecordSet`:{let n=j(e)?H(e):void 0;if(!n||!xe(n.domain)){if(M(e))return!1;throw new u(`Attempted to check if non-record\n${U(e)}\nis in the set of records:\n${U(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Yt(n.domain,e);return i!==-1&&$t(n.range[i],t.sets[r])})}case`ProductSet`:{let n=j(e)?Xt(e):void 0;if(n===void 0){if(j(e)&&H(e).domain.every(e=>typeof e==`number`)||M(e))return!1;throw new u(`Attempted to check if non-tuple\n${U(e)}\nis in the set of tuples:\n${U(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>$t(e,t.factors[n]))}case`FilteredSet`:return $t(e,t.base)&&t.test(e);case`Union`:if(!At(t.base))throw new u(`Attempted to check if:\n ${U(e)}\nis an element of the non-enumerable set:\n ${jt(t)}`);return B(t.base).elements.some(t=>$t(e,t));case`SetOperation`:return t.operator===`\\cup`?$t(e,t.left)||$t(e,t.right):$t(e,t.left)&&$t(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Xt(e);if(n===void 0){if(M(e))return!1;throw new u(Zt(e,t))}return n.every(e=>$t(e,t.base))}default:throw new u(Qt(e,t))}},en=(e,t,n)=>e===t?!0:typeof e!=`object`||typeof t!=`object`||typeof n!=`object`||e.kind!==`Function`||t.kind!==`Function`||n.kind!==`FunctionSet`||e.domain!==t.domain?$t(e,n):e.range.every((e,r)=>en(e,t.range[r],n.range)),H=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){z(e.domain)||Kt(e.domain);let t=Dt(e.domain);return Le(t,t.map(t=>e.apply(t)))}throw new u(`Expected a function but found ${U(e)}`)},tn=/* @__PURE__ */ new WeakSet,nn=e=>(tn.add(e),e),rn=e=>{let t=e.domain;return t.length===0?tn.has(e)?`FcnRcdValue`:`TupleValue`:t===xt[t.length]?`TupleValue`:Ct.has(t)?`IntervalFcnRcdValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},an=(e,t)=>`Attempted to apply function:\n${fe(U(e))}\nto argument ${fe(U(t))}, which is not in the domain of the function.`,on=e=>`Attempted to apply function with integer domain to the non-integer argument ${fe(U(e))}`,sn=(e,t)=>{if(tn.has(e)||e.kind===`LazyFunction`)return`In applying the function\n${fe(U(e))},\nthe first argument is:\n${fe(U(t))}\nwhich is not in its domain.\n`;let n=rn(e);return n===`TupleValue`?typeof t==`number`?`Attempted to access index ${t} of tuple\n${fe(U(e))}\nwhich is out of bounds.`:`Attempted to access tuple at a non integral index: ${fe(U(t))}`:n===`RecordValue`?typeof t==`string`?`Attempted to access nonexistent field '${t}' of record\n${fe(U(e))}`:`Attempted to access record by a non-string argument: ${fe(U(t))}`:n===`IntervalFcnRcdValue`&&typeof t!=`number`?on(t):an(e,t)},cn=(e,t)=>rn(e)===`FcnRcdValue`?an(e,t):on(t),ln=e=>rn(e)===`RecordValue`,un=(e,t)=>{let n=e[0];return typeof n==`number`?typeof t!=`number`&&(e===xt[e.length]||Ct.has(e)):typeof n==`string`&&typeof t!=`string`},dn=(e,t)=>{let n=e[0];if(typeof t==`number`&&typeof n==`number`){let r=t-n;if(e[r]===t)return r}if(e.length<=8){for(let n=0;n<e.length;n++)if(e[n]===t)return n}return Yt(e,t)},fn=(e,t,n,r)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!$t(t,e.domain))throw new u(tn.has(e)?sn(e,t):`Attempted to apply a function to ${U(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new u(r?.(e)??`Attempted to apply ${U(e)}, which is not a function`);let i=un(e.domain,t)?-1:dn(e.domain,t);if(i===-1)throw new u(n?.(e)??sn(e,t));return e.range[i]},pn=e=>Et(e)?Ct.get(e)??Ot(e.length):void 0,mn=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=H(e).domain;return pn(t)??Ie(t)},hn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!z(e.domain))){if(!$t(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>dt(t,n)?r:e.apply(n),source:e.source}}let i=H(e),a=dn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=Le(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-xn(e,s)+xn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-On(e,s)+On(e,c)|0}return l},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},vn=(e,t)=>Math.imul(e^t,16777619),yn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return _n(t)},bn=e=>_n(Cn(e)+2654435769|0),xn=(e,t)=>_n(Math.imul(Cn(e),668265261)+Cn(t)|0),Sn=e=>{if(e.kind===`ModelValue`)return _n(vn(yn(e.name),109));if(A(e)){if(!z(e))return yn(Mn(e));let t=B(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+bn(t[e])|0;return n}let t=H(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+xn(t.domain[e],t.range[e])|0;return n},Cn=e=>{if(typeof e==`number`)return _n(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return yn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=Sn(e)),e.hash;let t=gn.get(e);if(t!==void 0)return t;let n=Sn(e);return gn.set(e,n),n},wn=/* @__PURE__ */ new WeakMap,Tn=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},En=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return Tn(t)},Dn=e=>Tn(An(e)+2135587861|0),On=(e,t)=>Tn(Math.imul(An(e),374761393)+An(t)|0),kn=e=>{if(e.kind===`ModelValue`)return Tn(En(e.name)^60);if(A(e)){if(!z(e))return En(Mn(e));let t=B(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+Dn(t[e])|0;return n}let t=H(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+On(t.domain[e],t.range[e])|0;return n},An=e=>{if(typeof e==`number`)return Tn(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return En(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=kn(e)),e.fingerprint;let t=wn.get(e);if(t!==void 0)return t;let n=kn(e);return wn.set(e,n),n},jn=/* @__PURE__ */ new WeakMap,Mn=e=>{if(typeof e==`boolean`)return e?`T`:`F`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return JSON.stringify(e);let t=jn.get(e);if(t!==void 0)return t;let n=Nn(e);return jn.set(e,n),n},Nn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${Mn(e.base)}>`;if(e.kind===`FilteredSet`&&!z(e))throw new u(`Attempted to compare the non-enumerable set ${U(e)} with another value`);if(A(e))return z(e)?`{${B(e).elements.map(Mn).join(`,`)}}`:`<${e.kind} ${U(e)}>`;let t=H(e);return`(${t.domain.map((e,n)=>`${Mn(e)}:${Mn(t.range[n])}`).join(`,`)})`},Pn=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Fn=e=>e.reduce((e,t)=>e===0?0:e*Kt(t),1),In=e=>{if(e.kind===`Subset`)return Kt(e.base)<=6?B(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return B(e);if(e.kind===`FunctionSet`&&z(e.domain)&&Kt(e.domain)===0)return Ie([Le([],[])]);let t=e.kind===`FunctionSet`?Kt(e.range)**Kt(e.domain):Fn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?ht:t<=1999?B(e):void 0},Ln=(e,t)=>{if(t)return In(e);if(z(e))try{return In(e)}catch(e){if(e instanceof u)return;throw e}},Rn=(e,t)=>{let n=e=>U(e,t);switch(e.kind){case`Subset`:return typeof e.base==`object`&&e.base.kind===`Interval`?`SUBSET (${n(e.base)})`:`SUBSET ${n(e.base)}`;case`FunctionSet`:return`[${n(e.domain)} -> ${n(e.range)}]`;case`RecordSet`:return`[${e.names.map((t,r)=>`${t}: ${n(e.sets[r])}`).join(`, `)}]`;case`ProductSet`:return`(${e.factors.map(n).join(` \\X `)})`;case`SetOperation`:return`${n(e.left)} ${e.operator} ${n(e.right)}`;case`FilteredSet`:return`{x \\in ${n(e.base)} : ...}`;case`Union`:return jt(e,t)}},zn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),U=(e,t=!1,n=!1)=>{let r=e=>U(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return Pn(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(B(e));case`Interval`:return e.low<=e.high?`${e.low}..${e.high}`:`{}`;case`InfiniteSet`:return e.name===`Any`?`ANY`:e.name;case`Subset`:case`FunctionSet`:case`RecordSet`:case`ProductSet`:case`SetOperation`:case`FilteredSet`:case`Union`:{let n=Ln(e,t);return n?r(n):Rn(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!z(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${U(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let i=H(e),a=r=>U(r,t,n&&e.kind===`Function`);return i.domain.every((e,t)=>e===t+1)?`<<${i.range.map(a).join(`, `)}>>`:i.domain.every(e=>typeof e==`string`&&zn(e))?`[${(n||i.hash!==0||i.fingerprint!==0?i.domain.map((e,t)=>[e,i.range[t]]):ve(i)).map(e=>`${String(e[0])} |-> ${a(e[1])}`).join(`, `)}]`:`(${i.domain.map((e,t)=>`${r(e)} :> ${a(i.range[t])}`).join(` @@ `)})`}}},Bn=e=>{let t=Object.entries(e);return t.map(([e,n])=>`${t.length===1?``:`/\\ `}${e} = ${fe(U(n))}\n`).join(``)},Vn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Hn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=Cn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||Be(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||Be(i[e],n))return i[e]}return n.kind===`Set`?Vn(n.elements,t):(Vn(n.domain,t),Vn(n.range,t)),i===void 0?e.set(r,n):Array.isArray(i)?i.push(n):e.set(r,[i,n]),n};return e=>{for(let n=0;n<e.length;n++)e[n]=t(e[n]);return e}},Un=e=>{switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return[];case`Apply`:return e.args;case`Lambda`:return[e.body];case`And`:case`Or`:return e.operands;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return[e.expression];case`If`:return[e.condition,e.consequent,e.alternative];case`Case`:return[...e.arms.flatMap(e=>[e.condition,e.body]),...e.other?[e.other]:[]];case`Let`:return[...e.definitions.flatMap(e=>e.kind===`Function`?[...e.bounds.map(e=>e.set),e.body]:[e.body]),e.body];case`Forall`:case`Exists`:return[...e.bounds.map(e=>e.set),e.body];case`UnboundedQuantifier`:return[e.body];case`Choose`:return[...e.set?[e.set]:[],e.body];case`SetEnumeration`:case`Tuple`:return e.elements;case`SetFilter`:return[e.bound.set,e.predicate];case`SetMap`:return[e.expression,...e.bounds.map(e=>e.set)];case`FunctionConstruction`:return[...e.bounds.map(e=>e.set),e.body];case`FunctionSet`:return[e.domain,e.range];case`Record`:case`RecordSet`:return e.fields.map(e=>e.value);case`Except`:return[e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])];case`FunctionApplication`:return[e.function,...e.args];case`FieldAccess`:return[e.record];case`Product`:return e.factors;case`BoxAction`:case`AngleAction`:return[e.action,e.subscript];case`WeakFairness`:case`StrongFairness`:return[e.subscript,e.action];case`Label`:return[e.body];case`Subexpression`:return e.selectors.flatMap(e=>`args`in e?e.args:[])}},W=e=>(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`,Wn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Gn=(e,t)=>Wn.has(e)?new u(t):new d(t),Kn=e=>typeof e==`object`&&e.kind===`Operator`,G=(e,t,n)=>{let r=e[t];if(r===void 0||Kn(r))throw Gn(n,`${n} expects a value as argument ${t+1}`);return r},qn=(e,t,n)=>{let r=G(e,t,n);if(typeof r!=`number`)throw Gn(n,`${n} expects an integer but received ${U(r)}`);return r},Jn=(e,t,n)=>{let r=G(e,t,n);if(typeof r!=`boolean`)throw Gn(n,`${n} expects a boolean but received ${U(r)}`);return r},Yn=(e,t,n)=>{let r=G(e,t,n);if(!A(r))throw Gn(n,`${n} expects a set but received ${U(r)}`);return r},Xn=(e,t,n)=>{let r=Yn(e,t,n);if(!z(r))throw Gn(n,`${n} cannot enumerate the non-enumerable set ${U(r)}`);return r},Zn=(e,t,n)=>B(Xn(e,t,n)),Qn=(e,t,n)=>Zn(e,t,n).elements,$n=(e,t,n)=>{let r=G(e,t,n),i=Xt(r);if(!i)throw Gn(n,`${n} expects a sequence but received ${U(r)}`);return i},er=(e,t,n)=>{let r=G(e,t,n);if(!j(r))throw Gn(n,`${n} expects a function but received ${U(r)}`);return H(r)},tr=(e,t,n)=>{let r=e[t];if(r===void 0||!Kn(r))throw Gn(n,`${n} expects an operator as argument ${t+1}`);return r},nr=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),K=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:t===1?e=>n([e]):void 0,evaluateBinary:t===2?(e,t)=>n([e,t]):void 0}),rr=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),ir=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),ar=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(G(n,0,e),G(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),or=e=>e>2147483647||e<-2147483648,sr=e=>new d(`Overflow when computing ${e}`),cr={ModelValue:`ModelValue`,Set:`SetEnumValue`,Interval:`IntervalValue`,InfiniteSet:`UserValue`,SequenceSet:`UserValue`,Subset:`SubsetValue`,FunctionSet:`SetOfFcnsValue`,RecordSet:`SetOfRcdsValue`,ProductSet:`SetOfTuplesValue`,FilteredSet:`SetPredValue`,LazyFunction:`FcnLambdaValue`,Union:`UnionValue`,MixedSet:`SetEnumValue`},lr={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},ur={BoolValue:`a Boolean value`,IntValue:`an integer`,StringValue:`a string`,RecordValue:`a record`,SetEnumValue:`a set of the form {e1, ... ,eN}`,SetPredValue:`a set of the form {x \\in S : expr}`,TupleValue:`a tuple`,FcnLambdaValue:`a function of the form [x \\in S |-> expr]`,FcnRcdValue:`a function of the form (d1 :> e1 @@ ... @@ dN :> eN)`,SetOfFcnsValue:`a set of the form [S -> T]`,SetOfRcdsValue:`a set of the form [d1 : S1, ... , dN : SN]`,SetOfTuplesValue:`a cartesian product`,SubsetValue:`a set of the form SUBSET S`,SetDiffValue:`a set of the form S \\ T`,SetCapValue:`a set of the form S \\cap T`,SetCupValue:`a set of the form S \\cup T`,ModelValue:`a model value`,UserValue:`a special set constant`,IntervalValue:`a set of the form i..j`,UnionValue:`a set of the form UNION S`},dr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return lr[e.operator];if(e.kind!==`Function`)return cr[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},fr=e=>ur[dr(e)],pr=(e,t)=>new u(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),mr=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof u&&(e.isLazy===!0||!(n instanceof d))?pr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},hr=(e,t)=>{if(typeof e!=`number`)throw pr(t,`Cannot cast tlc2.value.impl.${dr(e)} to tlc2.value.impl.IntValue`);return e},gr=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,_r=(e,t,n)=>{let r=gr(t,`IntValue`,2);return ar(e,(t,i)=>{let a=hr(t,r),o=hr(i,r),s=n(a,o);if(or(s))throw sr(`${a}${e}${o}`);return s})},vr=(e,t,n)=>{if(typeof e!=`number`)throw new d(`The ${t} argument of ${n} should be an integer, but instead it is:\n${U(e)}`);return e},yr=(e,t)=>ar(e,(n,r)=>t(vr(n,`first`,e),vr(r,`second`,e))),br=(e,t,n)=>({...ir(e,t,n),isLazy:!0}),xr=(e,t,n)=>{let r=tr(e,t,n);return()=>r.call([])},Sr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),Cr=()=>({bool:so,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ja(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw Ya(t,n,e);return e},apply:fn,tuple:R,set:mt,record:Le,writtenRecord:ye,equal:Re,enumerate:po,unassigned:(e,t)=>{throw Rs(e,t)},primeOutsideAction:e=>{throw zs(e)},prime:e=>({environment:e.environment,scope:e.scope,state:e.next,next:void 0}),extend:(e,t,n)=>({environment:e.environment,scope:t.reduce((e,t,r)=>io(e,t,n[r]),e.scope),state:e.state,next:e.next})}),wr={value:void 0},Tr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),Er=e=>`local${e.counter++}`,Dr=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Un(e).forEach(e=>Dr(e,t)),t),Or=(e,t,n)=>{let r=Tr(e,Y(t,e.environment)),i=Dr(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=Tr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},kr=(e,t,n)=>{let r=t.kind===`Forall`,i=JSON.stringify(r?`the body of \\A`:`the body of \\E`),a=`quantifier${e.counter++}`,o=[...n.locals.values()],s={context:`context`,state:`state`,next:`next`,isPrimed:!1,locals:new Map(n.locals)},c=t.bounds.map(()=>Er(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${jr(e,t.set,s)}, ${Tr(e,t.set)});`),u=t.bounds.map(t=>{let n=Er(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${jr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=Er(e);return`for (let ${i} = 0; ${i} < ${c[r]}.length; ${i}++) { const ${n} = ${c[r]}[${i}]; ${t} }`},d),p=o.map(e=>`, ${e}`).join(``);return e.helpers.push(`function ${a}(context, state, next${p}) { let primed; ${l.join(` `)} ${f} return ${r}; }`),`${a}(${n.context}, ${n.state}, ${n.isPrimed?`undefined`:n.next}${p})`},Ar=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${jr(e,r[0],n)}, "the left side of =>") ? bool(${jr(e,r[1],n)}, "the right side of =>") : true)`;if(W(t))return Or(e,t,n);let i=Do(t.operator,e.environment);if(i?.kind!==`Builtin`)return Or(e,t,n);if(i.name===`DOMAIN`)return`${Tr(e,$a(t))}(${jr(e,r[0],n)})`;if(i.name===`TLCEval`)return Or(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${Tr(e,eo(t,i))}(${jr(e,r[0],n)}, ${jr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&vs(r[0],a))return`${Tr(e,i.evaluateUnary)}(${jr(e,r[0],n)})`;let o=r.length===2&&vs(r[0],a)&&vs(r[1],a);return i.evaluateBinary&&o?`${Tr(e,i.evaluateBinary)}(${jr(e,r[0],n)}, ${jr(e,r[1],n)})`:Or(e,t,n)},jr=(e,t,n)=>{let r=(t,r=n)=>jr(e,t,r);if(cs(t)&&rs(t,e.environment))return Or(e,t,n);switch(t.kind){case`Number`:return Go(t.value)?String(t.value):Or(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(W(t))return Or(e,t,n);let i=Lo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=Tr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?Tr(e,i.value):Or(e,t,n)}case`Apply`:return Ar(e,t,n);case`And`:{let n=Tr(e,t);return`(${t.operands.map((e,t)=>`conjunct(${r(e)}, ${n}, ${t})`).join(` && `)})`}case`Or`:return`(${t.operands.map(e=>`bool(${r(e)}, "a disjunct")`).join(` || `)})`;case`If`:return`(condition(${r(t.condition)}, ${Tr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{Ms(t.function);let n=r(t.function),i=Tr(e,Za(t));return t.args.length===1?`apply(${n}, ${r(t.args[0])}, undefined, ${i})`:`apply(${n}, tuple([${t.args.map(e=>r(e)).join(`, `)}]), undefined, ${i})`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${Tr(e,Xa(t))}, ${Tr(e,Qa(t))})`;case`Record`:{let i=Is(t.fields);if(Ls(i))return Or(e,t,n);let a=Tr(e,i.map(e=>e.name)),o=`[${i.map(e=>r(e.value)).join(`, `)}]`,s=t.fields.map(e=>e.name);return s.every((e,t)=>e===i[t].name)?`record(${a}, ${o})`:`writtenRecord(${a}, ${o}, ${Tr(e,s)})`}case`Tuple`:return`tuple([${t.elements.map(e=>r(e)).join(`, `)}])`;case`SetEnumeration`:return`set([${t.elements.map(e=>r(e)).join(`, `)}])`;case`Prime`:{let i=Tr(e,t);if(n.isPrimed)return`primeOutsideAction(${i})`;let a={context:`(primed ??= prime(${n.context}))`,state:n.next,next:`undefined`,isPrimed:!0,locals:n.locals};return`(${n.next} === undefined ? primeOutsideAction(${i}) : ${r(t.expression,a)})`}case`Unchanged`:return`equal(${r({kind:`Prime`,expression:t.expression,location:t.location})}, ${r(t.expression)})`;case`Forall`:case`Exists`:return t.bounds.every(e=>e.pattern.kind===`Name`)?kr(e,t,n):Or(e,t,n);default:return Or(e,t,n)}},Mr=(e,t)=>{if(!Sr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=jr(n,e,{context:`context`,state:`state`,next:`next`,isPrimed:!1,locals:/* @__PURE__ */ new Map}),i=n.references.map((e,t)=>`reference${t}`),a=`${n.helpers.join(`
23
- `)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;wr.value??=Cr();let o=wr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Nr={kind:`Local`},Pr=/* @__PURE__ */ new WeakMap,Fr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Ir=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Lr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Rr=e=>e.kind===`Operator`?e.params.length:0,zr=e=>e.name.includes(`!`),Br=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},Vr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Hr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Ur=e=>e.kind===`Name`?[e.name]:e.names,Wr=e=>e.flatMap(e=>Ur(e.pattern)),Gr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Lr(n.location,t.location))?n:void 0},Kr=(e,t)=>{let n=Gr(e,t);return n?{key:t.getKey(n),arity:Rr(n)}:t.imported.get(e)},qr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Jr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),Yr=(e,t,n,r)=>{let i=e.location,a=Pr.get(e);if(a!==void 0)return a;if(Fr(t))return e.kind===`Apply`?e.args.length:0;if(Jr(t))return 0;let o=Br(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Nr),o)return o.arity;let s=Kr(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,Pr.set(e,s.arity),s.arity;let l=t.slice(0,t.indexOf(`!`));if(l!==``&&(Br(n,l)!==void 0||Kr(l,r)!==void 0||r.facts.has(l)))return e.kind===`Apply`?e.args.length:0;let u=`at line ${i.line}, column ${i.column}`;throw new c(qr(t)?`Could not find declaration or definition of symbol '${t}' ${u}`:`Unknown operator: \`${t}' ${u}`)},Xr=(e,t,n)=>(e.forEach(e=>q(e.set,t,n)),e.reduce((e,t)=>Vr(e,Ur(t.pattern)),t)),Zr=(e,t)=>e.kind===`Identifier`&&e.name===t&&W(e)||Un(e).some(e=>Zr(e,t)),Qr=(e,t)=>{e.kind===`FunctionConstruction`&&Zr(e.body,t)&&(e.isRecursive=!0),Un(e).forEach(e=>Qr(e,t))},$r=(e,t,n)=>{let r=Vr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(q(e.body,Xr(e.bounds,r,n),n),Qr(e.body,e.name)):q(e.body,Hr(r,e.params),n)},ei=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Rr(t),parent:e}),t);return e.reduce((e,t)=>(zr(t)||$r(t,e,n),{name:t.name,arity:Rr(t),parent:e}),r)},ti=(e,t,n,r,i)=>{let a=e.kind===`Operator`?e.params[t]:void 0,o=n.location;if(a?.arity===0&&r>0)throw new c(`Operator used in argument number ${t+1} has incorrect number of arguments at line ${o.line}, column ${o.column}`);let s=i?.kind===`Operator`&&i.params.some(e=>e.arity>0);if(a!==void 0&&a.arity>0&&(r!==a.arity||s))throw new c(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},ni=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,ri=(e,t,n)=>{let r=Br(t,e.operator)?void 0:Gr(e.operator,n),i=Yr(e,e.operator,t,n);if(i!==e.args.length)throw new c(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=ni(e),o=a===void 0||Br(t,a)?void 0:Gr(a,n),s=ii(e,t,n);r&&ti(r,i,e,s,o)})},ii=(e,t,n)=>e.kind===`Identifier`?Yr(e,e.name,t,n):e.kind===`OperatorReference`?Yr(e,e.operator,t,n):(q(e,t,n),e.kind===`Lambda`?e.params.length:0),q=(e,t,n)=>{let r=e=>e.forEach(e=>q(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=Yr(e,e.name,t,n);if(r>0)throw new c(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:Yr(e,e.operator,t,n);return;case`Apply`:ri(e,t,n);return;case`Lambda`:q(e.body,Vr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:q(e.expression,t,n);return;case`If`:r([e.condition,e.consequent,e.alternative]);return;case`Case`:r(e.arms.flatMap(e=>[e.condition,e.body])),e.other&&q(e.other,t,n);return;case`Let`:q(e.body,ei(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:q(e.body,Xr(e.bounds,t,n),n);return;case`UnboundedQuantifier`:q(e.body,Vr(t,e.names),n);return;case`Choose`:e.set&&q(e.set,t,n),q(e.body,Vr(t,Ur(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:q(e.predicate,Xr([e.bound],t,n),n);return;case`SetMap`:q(e.expression,Xr(e.bounds,t,n),n);return;case`FunctionSet`:r([e.domain,e.range]);return;case`Record`:case`RecordSet`:if(new Set(e.fields.map(e=>e.name)).size<e.fields.length)throw new c(`Non-unique fields in constructor at line ${e.location.line}, column ${e.location.column}`);r(e.fields.map(e=>e.value));return;case`Except`:r([e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])]);return;case`FunctionApplication`:r([e.function,...e.args]);return;case`FieldAccess`:q(e.record,t,n);return;case`Product`:r(e.factors);return;case`BoxAction`:case`AngleAction`:r([e.action,e.subscript]);return;case`WeakFairness`:case`StrongFairness`:r([e.subscript,e.action]);return;case`Label`:q(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>ii(e,t,n))}},ai=(e,t,n)=>(e.set&&q(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),oi=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?ai(t,e,n):(t.kind===`AssumeProve`?oi(t,e,n):q(t,e,n),e),t);return q(e.conclusion,r,n),r},si=(e,t,n)=>e.kind===`AssumeProve`?oi(e,t,n):(q(e,t,n),t),ci=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>li(t,e,n),t)},li=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=si(r.statement,t,n);return ci(e.proof,i,n),r.isSuffices?i:t}case`Define`:return ei(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return q(r.expression,t,n),ci(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>q(e,t,n)),t;case`Take`:return Vr(Xr(r.bounds,t,n),r.names);case`Pick`:{let i=Vr(Xr(r.bounds,t,n),r.names);return q(r.body,i,n),ci(e.proof,t,n),i}default:return ci(e.proof,t,n),t}},ui=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:si(e.statement,void 0,t);ci(e.proof,n,t)},di=(e,t)=>new c(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),fi=(e,t,n,r)=>t.reduce((e,t)=>{if(Br(e,t)||n(t))throw di(t,r);return{name:t,arity:0,parent:e}},e),pi=(e,t,n,r)=>(e.forEach(e=>hi(e.set,t,n)),e.reduce((e,t)=>fi(e,Ur(t.pattern),n,r),t)),mi=(e,t,n)=>{if(e.kind===`Function`){let r=Vr(t,[e.name]),i=pi(e.bounds,r,n,e.location);hi(e.body,i,n);return}let r=fi(t,e.params.map(e=>e.name),n,e.location);hi(e.body,r,n)},hi=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:hi(e.body,fi(t,e.params,n,r),n);return;case`Let`:{let i=e.definitions.filter(e=>e.isRecursive).map(e=>e.name),a=e.definitions.reduce((e,t)=>{if(Br(e,t.name)||n(t.name))throw di(t.name,t.location);return mi(t,Vr(e,i),n),Vr(e,[t.name])},t);hi(e.body,fi(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:hi(e.body,pi(e.bounds,t,n,r),n);return;case`SetFilter`:hi(e.predicate,pi([e.bound],t,n,r),n);return;case`SetMap`:hi(e.expression,pi(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:hi(e.body,fi(t,e.names,n,r),n);return;case`Choose`:e.set&&hi(e.set,t,n),hi(e.body,fi(t,Ur(e.pattern),n,r),n);return;default:Un(e).forEach(e=>hi(e,t,n))}},gi=e=>[...e.definitions.map(e=>({name:e.name,kind:`Definition`,location:e.location,isRecursive:e.isRecursive})),...e.instances.flatMap(e=>e.name===void 0?[]:[{name:e.name,kind:`Definition`,location:e.location,isRecursive:!1}]),...e.constants.map(e=>({name:e.name,kind:`Constant`,location:e.location,isRecursive:!1})),...e.variables.map(e=>({name:e.name,kind:`Variable`,location:e.location,isRecursive:!1}))],_i=(e,t)=>{let n=/* @__PURE__ */ new Map;gi(e).sort((e,t)=>Lr(e.location,t.location)?-1:1).forEach(e=>n.set(e.name,[...n.get(e.name)??[],e]));let r=(e,n)=>{if(!t.imported.has(e)&&!Ir.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Lr(r,n)};n.forEach(e=>e.forEach((n,i)=>{let a=e.slice(0,i).some(e=>e.kind===`Definition`||n.kind===`Definition`||e.kind!==n.kind),o=t.imported.get(n.name)?.declaration===n.kind;if(a||!o&&r(n.name,n.location))throw di(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Lr(t.location,e))||r(t,e);e.definitions.forEach(e=>mi(e,void 0,i(e.location))),e.assumptions.forEach(e=>hi(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=fi(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>hi(e.expression,n,t))})},vi=(e,t)=>{let n=new Map([...Ir,...t]);return e.constants.forEach(e=>n.set(e.name,{key:e.name,arity:e.arity})),e.variables.forEach(e=>n.set(e.name,{key:e.name,arity:0})),n},yi=(e,t,n,r)=>{let i=Hr(void 0,e.params),a={imported:vi(n,r.imported),definitions:r.definitions,getKey:r.getKey,location:e.location,self:void 0,facts:/* @__PURE__ */ new Set},o=new Map(e.substitutions.map(e=>[e.name,e.expression])),s=`in the instance of ${e.module} at line ${e.location.line}, column ${e.location.column}`;return o.forEach((e,n)=>{if(t.every(e=>e.name!==n))throw new c(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Br(i,n)&&!Kr(n,a))throw new c(`Substitution missing for symbol ${n} ${s}`);let r=o.get(n)??{kind:`Identifier`,name:n,location:e.location};if(t.arity===0||r.kind!==`Identifier`)return q(r,i,a),[n,r];if(Yr(r,r.name,i,a)!==t.arity)throw new c(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},bi=(e,t)=>{let n=vi(e,t.imported),r=new Set([...e.theorems,...e.assumptions].flatMap(e=>e.name??[])),i=(e,i)=>({imported:n,definitions:t.definitions,getKey:t.getKey,location:e,self:i,facts:r});e.definitions.forEach(e=>$r(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>q(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ui(e,i(e.location,void 0)))},xi=/* @__PURE__ */ new WeakSet,Si=/* @__PURE__ */ new WeakSet,Ci=(e,t,n=/* @__PURE__ */ new Set)=>{let r=ki(e,(e,r)=>Ci(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&W(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&W(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},wi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),Ti=/* @__PURE__ */ new WeakMap,Ei=e=>Ti.get(e)??e.name,Di=(e,t)=>(Ei(t)!==e.name&&Ti.set(e,Ei(t)),e),Oi=(e,t)=>Di(e.kind===`Function`?{...e,bounds:wi(e.bounds,e=>t(e,[])),body:t(e.body,Wr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),ki=(e,t)=>{let n=e=>t(e,[]);switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return e;case`Apply`:return{...e,args:e.args.map(n)};case`Lambda`:return{...e,body:t(e.body,e.params)};case`And`:case`Or`:return{...e,operands:e.operands.map(n)};case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return{...e,expression:n(e.expression)};case`If`:return{...e,condition:n(e.condition),consequent:n(e.consequent),alternative:n(e.alternative)};case`Case`:return{...e,arms:e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),other:e.other?n(e.other):void 0};case`Let`:{let n=e.definitions.map(e=>e.name);return{...e,definitions:e.definitions.map(e=>Oi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:wi(e.bounds,n),body:t(e.body,Wr(e.bounds))};case`UnboundedQuantifier`:return{...e,body:t(e.body,e.names)};case`Choose`:return{...e,set:e.set?n(e.set):void 0,body:t(e.body,Ur(e.pattern))};case`SetEnumeration`:case`Tuple`:return{...e,elements:e.elements.map(n)};case`SetFilter`:return{...e,bound:{pattern:e.bound.pattern,set:n(e.bound.set)},predicate:t(e.predicate,Ur(e.bound.pattern))};case`SetMap`:return{...e,bounds:wi(e.bounds,n),expression:t(e.expression,Wr(e.bounds))};case`FunctionSet`:return{...e,domain:n(e.domain),range:n(e.range)};case`Record`:case`RecordSet`:return{...e,fields:e.fields.map(e=>({name:e.name,value:n(e.value)}))};case`Except`:return{...e,function:n(e.function),updates:e.updates.map(e=>({path:e.path.map(e=>e.kind===`Index`?{kind:`Index`,args:e.args.map(n)}:e),value:n(e.value)}))};case`FunctionApplication`:return{...e,function:n(e.function),args:e.args.map(n)};case`FieldAccess`:return{...e,record:n(e.record)};case`Product`:return{...e,factors:e.factors.map(n)};case`BoxAction`:case`AngleAction`:return{...e,action:n(e.action),subscript:n(e.subscript)};case`WeakFairness`:case`StrongFairness`:return{...e,subscript:n(e.subscript),action:n(e.action)};case`Label`:return{...e,body:n(e.body)};case`Subexpression`:return{...e,selectors:e.selectors.map(e=>`args`in e?{...e,args:e.args.map(n)}:e)}}},Ai=`LAMBDA`,ji=(e,t=/* @__PURE__ */ new Set)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0,r=n===void 0||t.has(n)?[]:[n];return ki(e,(e,n)=>(r.push(...ji(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Mi=e=>{let t=/* @__PURE__ */ new Map;return n=>{if(!e.substitutedNames.has(n))return n;let r=t.get(n)??e.instantiation.createName(n);return t.set(n,r),r}},Ni=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),Pi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Fi=(e,t)=>e.map(e=>({pattern:Pi(e.pattern,t),set:e.set})),Ii=(e,t)=>e.kind===`Function`?{...e,bounds:Fi(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Li=(e,t,n)=>{let r=e=>t.get(e)??e;switch(e.kind){case`Identifier`:return{...e,name:r(e.name)};case`Apply`:case`OperatorReference`:return{...e,operator:r(e.operator)};case`Label`:return{...e,params:e.params.map(r)};case`Lambda`:return{...e,params:e.params.map(n)};case`Let`:return{...e,definitions:e.definitions.map(e=>Di({...Ii(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Fi(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:Pi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:Pi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Ri=e=>`${e}@ENABLED`,zi=(e,t)=>e.modes.get(t)??e,Bi=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!W(e)&&t.instantiation.variables.has(e.name),Vi=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>Vi(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Bi(e,t,n))return[e.name];let r=W(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?Vi(r.body,t,/* @__PURE__ */ new Map):void 0},Hi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Bi(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Ri(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:qi(e.expression,zi(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[qi(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:qi(e.subscript,zi(t,`Primed`),n),location:r},qi(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=Vi(e.expression,t,n);if(r!==void 0)return{kind:`And`,operands:r.map(r=>({kind:`Apply`,operator:`=`,args:[{kind:`Prime`,expression:{kind:`Identifier`,name:Ri(r),location:e.location},location:e.location},qi({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ui=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Wi=(e,t)=>e.map(e=>Ui(e.name,t)),Gi=(e,t,n,r)=>{let i=Array.from({length:n},(e,t)=>`argument${t}`);return{kind:`Lambda`,params:i,body:{kind:`Apply`,operator:e,args:[...Wi(t,r),...i.map(e=>Ui(e,r))],location:r},location:r}},Ki=(e,t)=>e.instantiation.isParameterized(t)?Yi(e,t):void 0,qi=(e,t,n)=>{let r=e=>qi(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!W(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:qi(e.expression,zi(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:qi(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},zi(t,`Enabled`),n)};let s=Hi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return xi.add(n),n;let r=Ki(t,e.name);if(r===void 0)return e;if(a.length===0)return{...e,name:r};let o=i.getDefinition(e.name);return o&&Rr(o)>0?Gi(r,a,Rr(o),e.location):{kind:`Apply`,operator:r,args:Wi(a,e.location),location:e.location}}if(e.kind===`Apply`&&o(e.operator)){let n=e.args.map(r),o=i.substitutions.get(e.operator),s=i.substitutions.size>0||a.length>0||Si.has(e),l=e=>(s&&Si.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return l({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return l({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:Ai,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:Ai,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new c(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let u=Ki(t,e.operator);return u===void 0?l({...e,args:n}):{...e,operator:u,args:[...Wi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ki(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Gi(n,a,Rr(r),e.location):{...e,operator:n}}let l=Mi(t),u=ki(e,(e,i)=>i.length===0?r(e):qi(e,t,Ni(n,i,l)));return Li(u,n,l)},Ji=(e,t,n)=>t.length===0?e:e.kind===`Operator`?{...e,params:[...t,...e.params]}:{kind:`Operator`,name:e.name,params:t,body:{kind:`Let`,definitions:[e],body:Ui(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},Yi=(e,t)=>{let n=e.copies.get(t);if(n!==void 0)return n;let r=e.instantiation,i=r.getDefinition(t);return i?r.addDefinition(t,i.name,n=>{e.copies.set(t,n);let a=Mi(e),o=Ii(Oi(i,(t,n)=>qi(t,e,Ni(/* @__PURE__ */ new Map,n,a))),a);return Ji(o,r.params,i.location)}):t},Xi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>ji(e))),r=r=>{let i={instantiation:e,substitutedNames:n,copies:/* @__PURE__ */ new Map,mode:r,modes:t};return t.set(r,i),i},i=r(`Normal`);return r(`Enabled`),r(`Primed`),{getKey:e=>Ki(i,e)??e,getQualifiedKey:t=>{let n=Ki(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(t,r.name,()=>Ji(r,e.params,r.location)):t}}},Zi=e=>e.kind===`Let`&&e.instances!==void 0||Un(e).some(Zi),Qi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=ki(e,e=>Qi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Lr(n.location,e.location)),a=i===-1?e.length:i;return[...e.slice(0,a),...t(n,r),...e.slice(a)]},i.definitions),body:i.body,location:i.location}},$i=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,ea=(e,t)=>e.definitions.some(e=>Zi(e.body))||e.assumptions.some(e=>Zi(e.expression))||e.theorems.some(e=>{let t=$i(e);return t!==void 0&&Zi(t)})?{...e,definitions:e.definitions.map(e=>Oi(e,e=>Qi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Qi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=$i(e);return n===void 0?e:{...e,statement:Qi(n,t,[])}})}:e,ta=/* @__PURE__ */ new WeakMap,na=/* @__PURE__ */ new WeakMap,J=(e,t,n)=>e.isInit?{environment:e.environment,scope:t,state:n,next:void 0}:{environment:e.environment,scope:t,state:e.current,next:n},ra=(e,t,n)=>{let r=e.slice();return r[t]=n,r},ia=(t,r,i=X(t,r))=>{if(typeof i==`boolean`)return i;try{return so(i,`the action at ${Ga(t)}`)}catch(t){throw r.next===void 0?t:n(t,e.error)}},aa=e=>e===`=`||e===`\\in`||e===`\\subseteq`,oa=(e,t,n)=>{let r=n?ta:na,i=r.get(e);if(i)return i;let a=Fa(e,t,n);return r.set(e,a),a},sa=(e,t)=>{if(t.constants.has(e))return;let n=co(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},ca=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return ca(e.expression,t,!1,r);if(e.kind!==`Identifier`||W(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=sa(e.name,t);if(a&&!r.has(a))return r.add(a),ca(a.body,t,n,r)},la=(e,t,n)=>ca(e,t,!n),ua=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ua(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Uo(e.definitions,J(r,t,n));return ua(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=ro(t,e.name);if(a?.kind===`Thunk`)return ua(a.expression,a.scope,n,r,i);if(a)return;let o=r.environment.variableIndex.get(e.name);if(o!==void 0)return i?void 0:o;let s=sa(e.name,r.environment);if(s)return ua(s.body,void 0,n,r,i)},da=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||W(e))||!t&&e.kind===`Prime`,fa=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>fa(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`||n.has(e.name))return;let r=t.variableIndex.get(e.name);if(r!==void 0)return[r];let i=sa(e.name,t);if(i)return n.add(e.name),fa(i.body,t,n)},pa=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ma=(e,t,n)=>pa.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||aa(e.operator)&&(da(e.args[0],n)||la(e.args[0],t,n)!==void 0)||W(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,ha=/* @__PURE__ */ new Set,ga=(e,t)=>{if(W(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return ki(e,(e,i)=>{let a=r.isReadOnly?ga(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},_a=(e,t,n)=>{if(e.kind===`Or`||e.kind===`Exists`)return!0;let r=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0,i=r===void 0||W(e)?void 0:co(r,t);return i&&!n.has(i)&&(n.add(i),_a(i.body,t,n))?!0:Un(e).some(e=>_a(e,t,n))},va=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=la(e.args[0],t,n);if(r!==void 0)return{kind:`Assign`,expression:e,index:r,value:e.args[1]}}if(!n&&e.kind===`Unchanged`){let n=fa(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ma(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:oa(e,t,n)}},ya=(e,t)=>{if(e.bounds.length<2||e.body.kind!==`And`)return;let n=e.bounds.flatMap(e=>e.pattern.kind===`Name`?[e.pattern.name]:[]);if(n.length!==e.bounds.length)return;let r=new Set(n.slice(0,-1)),i=e.body.operands,a=i.findIndex(e=>{if(Ws(e,t)===void 0||_a(e,t,/* @__PURE__ */ new Set))return!0;let n=ga(e,ha);return!n||n.some(e=>r.has(e))}),o=a===-1?i.length:a;if(o!==0)return{names:n,guards:i.slice(0,o),rest:i.slice(o)}},ba=(e,t,n)=>{let r=e.map(e=>va(e,t,n)),i=r.map(()=>void 0),a=(e,n,o,s,c,l)=>{let u=s,d;for(let s=e;s<r.length;s++){if(s>0&&n.isInit&&za(u)){d??=J(n,o,u);for(let e=s;e<r.length;e++)if(!xa(r[e].expression,d))return;l(u,c);return}let e=r[s];if(e.kind===`Guard`){d??=J(n,o,u);let r=i[s]??=ps(e.expression,t);if(!ia(e.expression,d,r(d)))return;continue}if(e.kind===`Assign`){d??=J(n,o,u);let r=(i[s]??=ps(e.value,t))(d),a=u[e.index];if(a===void 0)u=ra(u,e.index,r),d=void 0;else if(!Re(a,r))return;continue}if(e.kind===`Unchanged`){let t=n.current;if(!t)return;for(let n of e.indices){let e=u[n];if(e===void 0)u=ra(u,n,t[n]),d=void 0;else if(!Re(e,t[n]))return}continue}let f=s+1;e.runner(n,o,u,c,!0,(e,t)=>a(f,n,o,e,t,l));return}l(u,c)};return(e,t,n,r,i,o)=>a(0,e,t,n,r,o)},xa=(e,t)=>so(X(e,t),`the initial predicate at ${Ga(e)}`),Sa=e=>(t,n,r,i,a,o)=>{ia(e,J(t,n,r))&&o(r,i)},Ca=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),d=J(r,i,a);if(a[l]!==void 0){ia(e,d)&&c(a,o);return}let f=X(n,d),p=e.operator===`\\subseteq`?{kind:`Subset`,base:f}:f;if(!z(p))throw new u(`In computing ${r.isInit?`initial`:`next`} states, the right side of \\IN is not enumerable.\n${qa(e)}`);let m=Ut(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(ra(a,l,m[e]),o)}},wa=/* @__PURE__ */ new WeakMap,Ta=e=>{let t=wa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(Ta),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return wa.set(e,n),n},Ea=(e,t)=>{let n=e.args[0],r=Sa(e),i=(e,r,i)=>ua(n,r,i,e,!t)??-1,a=Ca(e,i);return(t,n,o,s,c,l)=>{let u=i(t,n,o);if(u===-1||o[u]!==void 0){r(t,n,o,s,c,l);return}if(e.operator!==`=`){a(t,n,o,s,c,l);return}let d=X(e.args[1],J(t,n,o));l(ra(o,u,d),s)}},Da=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=ro(t,e.name);if(i?.kind===`Thunk`)return Da(i.expression,i.scope,n,r);let a=i?void 0:co(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,J(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:io(void 0,a.name,o)}},Oa=(e,t,n)=>{let r=Sa(e);return(i,a,o,s,c,l)=>{let d=Da(e.function,a,o,i);if(!d){r(i,a,o,s,c,l);return}let f=J(i,a,o),p=e.args.map(e=>X(e,f)),m=p.length===1?p[0]:R(p),h=ho(d.bounds,J(i,d.scope,o));if(!$t(m,h))throw new u(`Attempted to apply a function to ${U(m)}, which is not in its domain at ${Ga(e)}`);oa(d.body,t,n)(i,go(d.bounds,d.scope,m),o,s,c,l)}},ka=(e,t)=>{if(fa(e,t))return ba([Ta(e)],t,!1);if(e.kind===`Tuple`)return oa(Ta(e),t,!1);let n=Ta(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?ro(i,e.name):void 0;l?.kind===`Thunk`?oa(Ta(l.expression),t,!1)(r,l.scope,a,o,s,c):ia(n,J(r,i,a))&&c(a,o)}},Aa=(e,t,n)=>{let r=e.operator,i=Sa(e),a=W(e)?void 0:Do(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=xs(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=oa(a.body,t,n),o.body(e,i(J(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=J(a,o,s),f=ko(e,d);f?.kind===`Closure`?oa(f.body,t,n)(a,Vo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ja=(e,t,n)=>{let r=Sa(e),i=W(e)?void 0:sa(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=ro(s,e.name);f?.kind===`Thunk`?oa(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=oa(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},Ma=(e,t,n)=>{if(n.has(e))return;if(n.add(e),e.kind===`And`){let n=e.operands[0];if(n.kind!==`Apply`||n.operator!==`=`||W(n))return;let r=n.args[0],i=n.args[1];if(r.kind!==`FunctionApplication`||i.kind!==`String`)return;let a=r.args.length===1?r.args[0]:void 0;if(a?.kind!==`Identifier`||!W(a))return;let o=ca(r.function,t,!1);return o===void 0?void 0:{variable:o,argument:a.name,labels:/* @__PURE__ */ new Set([i.value])}}if(e.kind===`Or`){let r=e.operands.map(e=>Ma(e,t,n)),i=r[0],a=r.every(e=>e!==void 0&&e.variable===i?.variable&&e.argument===i.argument);if(!i||!a)return;let o=new Set(r.flatMap(e=>[...e?.labels??[]]));return{variable:i.variable,argument:i.argument,labels:o}}if(e.kind!==`Apply`||W(e))return;let r=Do(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=Ma(r.body,t,n),a=r.params.findIndex(e=>e.arity===0&&e.name===i?.argument),o=e.args[a];if(i&&o?.kind===`Identifier`&&W(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Na=(e,t,n)=>{let r=ro(t,n.argument),i=e.current?.[n.variable];if(r?.kind!==`Value`||typeof i!=`object`||i.kind!==`Function`)return;let a=i.domain.indexOf(r.value),o=a===-1?void 0:i.range[a];return typeof o==`string`?o:void 0},Pa=(e,t,n)=>{let r=e.map(e=>oa(e,t,n)),i=n?[]:e.map(e=>Ma(e,t,/* @__PURE__ */ new Set)),a=i.find(e=>e!==void 0),o=i.map(e=>e?.variable===a?.variable&&e?.argument===a?.argument?e?.labels:void 0);return(e,t,n,i,s,c)=>{let l=a&&Na(e,t,a);for(let a=0;a<r.length&&!e.isStopped;a++)(l===void 0||o[a]?.has(l)!==!1)&&r[a](e,t,n,i,s,c)}},Fa=(e,t,n)=>{let r=e=>oa(e,t,n);switch(e.kind){case`And`:return ba(e.operands,t,n);case`Or`:return Pa(e.operands,t,n);case`Exists`:{let i=r(e.body),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name;return(t,n,r,o,s,c)=>{let l=po(X(a.set,J(t,n,r)),a.set);for(let a=0;a<l.length&&!t.isStopped;a++)i(t,io(n,e,l[a]),r,o,s,c)}}let o=n?void 0:ya(e,t);if(o){let r=ba(o.rest,t,n);return(t,n,i,a,s,c)=>{let l=J(t,n,i),u=e.bounds.map(e=>po(X(e.set,l),e.set));if(u.some(e=>e.length===0))return;let d=(e,n)=>{if(e<0)r(t,n,i,a,s,c);else for(let r of u[e]){if(t.isStopped)return;let a=io(n,o.names[e],r);if(e===u.length-1){let e=J(t,a,i);if(!o.guards.every(t=>ia(t,e)))continue}d(e-1,a)}};d(u.length-1,n)}}return(t,n,r,a,o,s)=>{mo(e.bounds,J(t,n,r),e=>(i(t,e,r,a,o,s),t.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ws(e,t)!==void 0&&!_a(e.body,t,/* @__PURE__ */ new Set)?ga(e,ha):void 0;return(t,n,r,o,s,c)=>{let l=[];mo(e.bounds,J(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>ro(n,e)?.kind===`Value`)){u.every(n=>ia(e.body,J(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&za(r)?u.slice(n).every(n=>xa(e.body,J(t,n,r)))&&c(r,a):i(t,u[n],r,a,!0,(e,t)=>d(n+1,e,t))};d(0,r,o)}}case`If`:{let t=r(e.consequent),n=r(e.alternative);return(r,i,a,o,s,c)=>{let l=X(e.condition,J(r,i,a));(so(l,`an IF condition`)?t:n)(r,i,a,o,s,c)}}case`Case`:{let t=e.arms.map(e=>({condition:e.condition,body:r(e.body)})),n=e.other?r(e.other):void 0;return(r,i,a,o,s,c)=>{let l=J(r,i,a),d=t.find(e=>so(X(e.condition,l),`a CASE guard`))?.body??n;if(!d)throw new u(`In computing ${r.isInit?`initial`:`next`} states, TLC encountered a CASE with no conditions true.\n${qa(e)}`);d(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=$o(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Uo(e.definitions,J(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new u(`UNCHANGED appears in an initial predicate at ${Ga(e)}`)}:ka(e.expression,t);case`AngleAction`:{let t=r(e.action),n=Ta(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{ia(n,J(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,Ta(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ja(e,t,n);case`FunctionApplication`:return Oa(e,t,n);case`Apply`:{let i=e.args;if(aa(e.operator)){let r=la(i[0],t,n);if(r!==void 0)return e.operator===`=`?ba([e],t,n):Ca(e,()=>r);if(da(i[0],n))return Ea(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],J(t,n,r));(typeof c==`boolean`?c:so(c,`the left side of => at ${Ga(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return Aa(e,t,n)}default:return Sa(e)}},Ia=/* @__PURE__ */ new WeakMap,La=e=>{let t=Ia.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ia.set(e,n),n},Ra=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),za=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},Ba=(e,t)=>{let n=t.state;if(!n||!za(n))throw new u(`ENABLED needs a complete state at ${Ga(e)}`);let r=t.environment,i={environment:r,isInit:!1,current:n,isStopped:!1};return oa(e,r,!1)(i,t.scope,La(r),void 0,!1,()=>{i.isStopped=!0}),i.isStopped},Va=(t,r,i,a)=>{let o={environment:r,isInit:!0,current:void 0,isStopped:!1};oa(t,r,!0)(o,a,La(r),void 0,!1,t=>{if(!o.isStopped){if(!za(t))throw n(new u(`The initial predicate does not assign ${Ra(t,r).join(`, `)}`),e.error);o.isStopped=i(t)}})},Ha=(e,t,n)=>{let r=[];return Va(e,t,e=>(r.push(e),!1),n),r},Ua=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return oa(e,n,!1)(a,i,La(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!za(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Ra(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Wa=(e,t,n,r)=>{let i=[];return Ua(e,t,n,e=>(i.push(e),!1),r),i},Ga=e=>`line ${e.location.line}, column ${e.location.column}`,Ka=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,qa=e=>{let t=e.location.range;return t?Ka(t):Ga(e)},Ja=(e,t)=>new u(`A non-boolean expression (${fr(t)}) was used as the condition of an IF.\n${qa(e)}`),Ya=(e,t,n)=>{let r=e.location.range;if(r?.start.line===e.location.line&&r.start.column===e.location.column||!r){let r=e.operands[t],i=r.kind===`Number`?String(r.value):qa(r);return new u(`A non-boolean expression (${fr(n)}) was used as a formula in a conjunction.\n${i}`)}let i=e.operands[Math.max(t,1)].location.range?.end??r.end;return new u(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${fr(n)}.\n${qa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Xa=e=>t=>ln(t)?`Attempted to select nonexistent field "${e.field}" from the record\n${U(t)}\n${qa(e)}`:cn(t,e.field),Za=e=>t=>`A non-function (${fr(t)}) was applied as a function.\n${qa(e)}`,Qa=e=>t=>`Attempted to select field "${e.field}" from a non-record value ${U(t)}\n${qa(e)}`,$a=e=>t=>{if(!j(t))throw new u(`Attempted to apply the operator DOMAIN to a non-function\n(${fr(t)})\n${qa(e)}`);return mn(t)},eo=(e,t)=>(n,r)=>{if(!A(n)||F(n))throw new u(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${qa(e)}`);return t.evaluate([n,r])},to=t=>n(new u(`TLC can't handle real numbers: ${t.text} at ${Ga(t)}`),e.configParseError),no=t=>n(new u(`TLC can't handle a number this big: ${t.text??t.value} at ${Ga(t)}`),e.error),ro=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},io=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),ao=(e,t,n)=>{if(t.kind===`Name`)return io(e,t.name,n);let r=Xt(n);if(!r||r.length!==t.names.length)throw new u(`Cannot bind <<${t.names.join(`, `)}>> to ${U(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>io(e,t,r[n]),e)},oo=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),so=(e,t)=>{if(typeof e!=`boolean`)throw new u(`Expected ${t} to be a boolean but found ${U(e)}`);return e},co=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),lo=(e,t)=>{if(e.kind===`Prime`||e.kind===`Unchanged`||e.kind===`Enabled`)return!0;let n=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0;if(n!==void 0){if(t.variableIndex.has(n)||t.builtins.get(n)?.isStateLevel)return!0;let e=co(n,t);if(e&&!t.constants.has(n)&&!uo(e,t))return!0}return Un(e).some(e=>lo(e,t))},uo=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!lo(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>lo(e.set,t)));return t.constantLevels.set(e,r),r},fo=(e,t)=>{let n=`universally (\\A) or existentially (\\E) quantify over ${t}, because this would require checking an\ninfinite number of cases.`;if(typeof e==`object`&&e.kind===`SequenceSet`)return`In TLA+, Seq(S) represents the set of all finite sequences whose elements come from the set S. Even when S\nis a finite set, the number of possible sequences in Seq(S) is unbounded because sequences can have any\nfinite length (e.g., length 0, 1, 2, and so on). As a result, TLC cannot evaluate expressions that\n${n} Note that for a finite set of sequences s, TLC handles s \\subseteq Seq(S).\nSee https://explain.tlapl.us/seq-unenumerable for additional details.`;if(typeof e==`object`&&e.kind===`InfiniteSet`&&e.name!==`STRING`&&e.name!==`Any`)return`The set ${e.name} contains infinitely many elements. As a result, TLC cannot evaluate expressions that\n${n} Note that TLC handles set membership like T \\subseteq ${e.name} for any finite set T.`},po=(e,t)=>{if(!A(e)||F(e)){let n=U(e),r=fo(e,n);throw new u(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${qa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${qa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Tt(e):B(e).elements},mo=(e,t,n)=>{let r=e.map(e=>po(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,ao(a,e[t].pattern,n)));return i(e.length-1,t.scope)},ho=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},go=(e,t,n)=>{if(e.length===1)return ao(t,e[0].pattern,n);let r=Xt(n)??[];return e.reduce((e,t,n)=>ao(e,t.pattern,r[n]),t)},_o=/* @__PURE__ */ new WeakMap,vo=(e,t)=>{let n=_o.get(t);if(n)return n;let r=t.location.range;if(!r)return;let i={parameters:e.flatMap((t,n)=>n>0&&e[n-1].set===t.set?[]:[{index:n,name:t.pattern.kind===`Name`?t.pattern.name:`<<${t.pattern.names.join(`, `)}>>`}]),body:Ka(r)};return _o.set(t,i),i},yo=(e,t,n)=>{let r=vo(e,t);if(!r)return;let i=e.length>1&&typeof n==`object`&&n.kind===`ProductSet`?n.factors:[n];return{parameters:r.parameters.map(e=>({name:e.name,domain:i[e.index]})),body:r.body}},bo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=Cn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(Be(a[t],e))return a[t+1]}let o=t(e,i),s=r.get(n);return s===void 0?r.set(n,[e,o]):s.push(e,o),o}};return i},xo=(e,t)=>{let n=ho(e.bounds,t),r=bo(n,(n,r)=>X(e.body,oo(t,go(e.bounds,io(void 0,e.name,r),n))),yo(e.bounds,e.body,n));return e.bounds.length===1?nn(r):r},So=(e,t)=>e.kind===`Function`?xo(e,t):X(e.body,t),Co=(e,t)=>{let n=uo(e,t.environment),r=n?t.environment.definitionValues.get(e):void 0;if(r!==void 0)return r;if(e.kind===`Operator`&&e.params.length>0)throw new u(`Operator ${e.name} requires ${e.params.length} arguments`);let i=oo(t,void 0);if(!n)return So(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>So(e,i));return t.environment.definitionValues.set(e,a),a},wo=(e,t)=>{let n=e.state===t.state&&e.next===t.next;if(n&&e.cache!==void 0)return e.cache;let r=X(e.expression,oo(t,e.scope));return n&&(e.cache=r),r},To=/* @__PURE__ */ new WeakMap,Eo=e=>{let t=To.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return To.set(e,n),n},Do=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return Do(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?Eo(r):t.builtins.get(e)},Oo=/* @__PURE__ */ new WeakMap,ko=(e,t)=>{if(W(e))return Ao(e.operator,t);let n=Oo.get(e),r=n===void 0?Do(e.operator,t.environment)??null:n;return n===void 0&&Oo.set(e,r),r??Ao(e.operator,t)},Ao=(e,t)=>{let n=ro(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return Do(e,t.environment)},jo=e=>e.kind===`Closure`?e.params.length:e.arity,Mo=(e,t)=>{if(e.kind===`Lambda`)return{kind:`Closure`,name:`LAMBDA`,params:e.params.map(e=>({name:e,arity:0})),body:e.body,scope:t.scope};let n=e.kind===`OperatorReference`?e.operator:e.kind===`Identifier`?e.name:void 0,r=n===void 0?void 0:Ao(n,t);if(!r)throw new u(`Expected an operator argument at ${Ga(e)}`);return r},No=(e,t,n)=>{if(e.kind===`Builtin`)return e.evaluate(t,n);if(e.kind===`Operator`)return e.call(t);let r=e.params.reduce((e,n,r)=>io(e,n.name,t[r]),e.scope);return X(e.body,oo(n,r))},Po=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:jo(e),call:n=>No(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},Fo=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=Ao(e.name,t);return n!==void 0&&jo(n)>0},Io=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),Lo=(e,t)=>{let n=t.variableIndex.get(e);if(n!==void 0)return{kind:`Variable`,index:n};let r=t.constants.get(e);if(r!==void 0)return{kind:`Value`,value:r};let i=t.substitutions.get(e);if(i!==void 0)return Lo(i,t);let a=t.definitions.get(e);if(a)return{kind:`Definition`,definition:a};let o=t.builtins.get(e);if(o&&o.arity===0&&!o.isStateLevel)return{kind:`Value`,value:o.evaluate([])}},Ro=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Go(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=W(e)?void 0:Lo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=ro(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Io(e,t)}}return t=>Io(e,t)},zo=/* @__PURE__ */ new WeakMap,Bo=(e,t)=>{let n=zo.get(e);if(n)return n;let r=Ro(e,t);return zo.set(e,r),r},Vo=(e,t,n)=>{if(t.length!==e.params.length)throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`);let r=e.scope;for(let i=0;i<t.length;i++){let a=e.params[i],o=a.arity>0?{kind:`Operator`,operator:Mo(t[i],n)}:Bo(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},Ho=(e,t,n)=>e.kind===`Closure`?X(e.body,oo(n,Vo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(ys(t,n),n):e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n),Uo=(e,t,n)=>e.reduce((e,r)=>{let i={name:r.name,binding:{kind:`Value`,value:!1},parent:e};return r.kind===`Function`?(i.binding={kind:`Thunk`,expression:{kind:`FunctionConstruction`,bounds:r.bounds,body:r.body,location:r.location,isRecursive:!0},scope:i,state:t.state,next:t.next,cache:void 0},i):r.params.length>0||n?.has(r)?(i.binding={kind:`Operator`,operator:{kind:`Closure`,name:r.name,params:r.params,body:r.body,scope:i}},i):(i.binding={kind:`Thunk`,expression:r.body,scope:i,state:t.state,next:t.next,cache:void 0},i)},t.scope),Wo=(e,t,n)=>{let r=ro(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return wo(r,n);if(r?.kind===`Operator`){if(jo(r.operator)===0)return No(r.operator,[],n);throw new u(`Operator ${e} is used without arguments`)}let i=Lo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw Rs(e,t);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return Co(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new u(`Unknown identifier ${e} at ${Ga(t)}`)},Go=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,Ko=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),qo=(e,t)=>Ko.has(e)||t.builtins.get(e)?.isStateLevel===!0,Jo={builtins:Ko,definitions:/* @__PURE__ */ new WeakMap},Yo={builtins:new Set([...Ko].filter(e=>e!==`Assert`)),definitions:/* @__PURE__ */ new WeakMap},Xo=(e,t)=>e.kind===`Operator`&&e.params.length===0&&uo(e,t),Zo=(e,t,n)=>{let r=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(r!==void 0&&!W(e)){let e=co(r,t),i=n.builtins.has(r)||t.builtins.get(r)?.isStateLevel===!0;if(!e&&i||e&&!Xo(e,t)&&Qo(e,t,n))return!0}return Un(e).some(e=>Zo(e,t,n))},Qo=(e,t,n=Jo)=>{let r=n.definitions.get(e);if(r!==void 0)return r;n.definitions.set(e,!1);let i=Zo(e.body,t,n)||e.kind===`Function`&&e.bounds.some(e=>Zo(e.set,t,n));return n.definitions.set(e,i),i},$o=(e,t)=>new Set(e.filter(e=>Qo(e,t))),es=e=>[...e.definitions.values()].some(t=>Qo(t,e)),ts=e=>[...e.definitions.values()].some(t=>Qo(t,e,Yo)),ns=/* @__PURE__ */ new WeakMap,rs=(e,t)=>{let n=ns.get(e);if(n!==void 0)return n;let r=ss(e,t,as);return ns.set(e,r),r},is=/* @__PURE__ */ new Set([`Number`,`String`,`Identifier`,`Apply`,`And`,`Or`,`If`,`Case`,`SetEnumeration`,`Tuple`,`FunctionSet`,`Record`,`RecordSet`,`FunctionApplication`,`FieldAccess`,`Product`,`SetFilter`,`SetMap`,`Forall`,`Exists`,`Choose`,`FunctionConstruction`,`Let`]),as=/* @__PURE__ */ new Set,os=(e,t,n)=>n.size===0?rs(e,t):ss(e,t,n),ss=(e,t,n)=>{if(!is.has(e.kind))return!1;if(e.kind===`Identifier`&&W(e))return n.has(e.name);if(W(e))return!1;if(e.kind===`Identifier`){let n=Lo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Xo(n.definition,t)}if(e.kind===`Apply`){if(qo(e.operator,t)||e.operator===`~>`)return!1;let r=Do(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=co(e.operator,t);if(!n||!uo(n,t)||Qo(n,t))return!1}return!(r.kind===`Closure`?r.params:[]).some(e=>e.arity>0)&&e.args.every(e=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&os(e,t,n))}let r={value:!0};return ki(e,(e,i)=>(r.value&&=os(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},cs=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,ls=e=>{let t={value:void 0};return n=>{if(t.value!==void 0)return t.value;let r=e(n);return t.value=r,r}},us=(e,t)=>{if(e.kind===`Identifier`&&W(e))return!t.has(e.name);if(W(e))return!0;let n={value:!1};return ki(e,(e,r)=>(n.value||=us(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},ds=(e,t)=>xi.has(e)&&cs(e)&&Ws(e,t)!==void 0&&!us(e,as),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Vs(e,t),i=cs(e)&&rs(e,t)?ls(r):ds(e,t)?gs(r):r;return t.evaluators.set(e,i),i},fs=/* @__PURE__ */ new WeakMap,ps=(e,t)=>{let n=fs.get(e);if(n)return n;let r=(cs(e)&&rs(e,t)?void 0:Mr(e,t))??Y(e,t);return fs.set(e,r),r},X=(e,t)=>ps(e,t.environment)(t),ms=(e,t)=>{let n=e.name,r=W(e)?void 0:Lo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw Rs(n,e);return i}}if(r?.kind===`Value`){let e=r.value;return()=>e}if(r?.kind===`Definition`&&r.definition.kind===`Operator`){let e=r.definition;if(e.params.length===0&&uo(e,t))return t=>Co(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=ps(e.body,t),n.body(oo(r,void 0)));return hs(e,t)?gs(r):r}return t=>Co(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>Co(e,t);return uo(e,t)||!hs(e,t)?n:gs(n)}return t=>Wo(n,e,t)},hs=(e,t)=>Ws(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ws(e.set,t)!==void 0)),gs=e=>{let t={recent:void 0,recentValue:void 0,previous:void 0,previousValue:void 0};return n=>{let r=n.state;if(r!==void 0&&t.recent===r&&t.recentValue!==void 0)return t.recentValue;if(r!==void 0&&t.previous===r&&t.previousValue!==void 0)return t.previousValue;let i=e(n);return t.previous=t.recent,t.previousValue=t.recentValue,t.recent=r,t.recentValue=i,i}},_s=(e,t)=>{let n=Do(e,t);return n!==void 0&&jo(n)>0},vs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!W(e)&&_s(e.name,t)),ys=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),bs=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(ys(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&vs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&vs(t[0],n)&&vs(t[1],n);if(i&&a){let e=Y(t[0],n),r=Y(t[1],n);return t=>i(e(t),r(t))}return n=>e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n)},xs=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`)};let r=e.params,i=t.map((e,t)=>r[t].arity>0?void 0:Bo(e,n));return n=>{let a=e.scope;for(let e=0;e<t.length;e++){let o=i[e],s=o?o(n):{kind:`Operator`,operator:Mo(t[e],n)};a={name:r[e].name,binding:s,parent:a}}return a}},Ss=/* @__PURE__ */ new WeakMap,Cs=e=>{let t=Ss.get(e);if(t)return t;let n={calls:/* @__PURE__ */ new Map,size:0};return Ss.set(e,n),n},ws=(e,t)=>{let n=[];for(let r=e;n.length<t;r=r?.parent){if(r?.binding.kind!==`Value`)return;n.push(r.binding.value)}return n},Ts=(e,t)=>e.every((e,n)=>Be(e,t[n])),Es=(e,t)=>{let n=co(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&uo(n,t)&&!Qo(n,t)},Ds=(e,t,n,r)=>{let i=xs(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=ps(e.body,n),a.body({environment:n,scope:t,state:r.state,next:r.next}));if(!r)return e=>o(i(e),e);let s=Cs(e);return e=>{let n=i(e),r=ws(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=vn(a,Cn(e));let c=s.calls.get(a),l=c?.find(e=>Ts(e.args,r));if(l)return l.value;let u=o(n,e);return s.size<65536&&(s.size++,c?c.push({args:r,value:u}):s.calls.set(a,[{args:r,value:u}])),u}},Os=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!so(e(t),`the left side of =>`)||so(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new u(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=W(e)?void 0:Do(e.operator,t);if(r?.kind===`Closure`)return Ds(r,n,t,Es(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=$a(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return Si.has(e)||lo(n[0],t)?i:e=>e.scope===void 0?r.evaluate([i(e)]):i(e)}if(r?.kind===`Builtin`&&r.name===`\\subseteq`&&n.length===2){let i=Y(n[0],t),a=Y(n[1],t),o=eo(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?bs(r,n,t):r=>{let i=Ao(e.operator,r);if(i)return Ho(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new u(`${e.operator} is a function; apply it with brackets`):e.operator===`\\cdot`?new u(`The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.`):new u(`Unknown operator ${e.operator} at ${Ga(e)}`)}},ks=(e,t)=>{let n=e.kind===`Forall`,r=n?`the body of \\A`:`the body of \\E`,i=Y(e.body,t),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name,o=Y(a.set,t);return t=>{let s=po(o(t),a.set);for(let a=0;a<s.length;a++){let o=so(i(oo(t,io(t.scope,e,s[a]))),r);if(o!==n)return o}return n}}let o=e.bounds;if(o.every(e=>e.pattern.kind===`Name`)){let e=o.map(e=>e.pattern.kind===`Name`?e.pattern.name:``),a=o.map(e=>Y(e.set,t));return t=>{let s=a.map((e,n)=>po(e(t),o[n].set)),c=(a,o)=>{if(a<0)return so(i(oo(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,io(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>mo(o,e,t=>so(i(oo(e,t)),r)!==n)?!n:n},As=(e,t)=>{if(e.kind===`Tuple`)return R(e.names.map(e=>{let n=ro(t,e);return n?.kind===`Value`&&n.value}));let n=ro(t,e.name);return n?.kind===`Value`?n.value:void 0},js=/* @__PURE__ */ new WeakSet,Ms=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&js.add(e)},Ns=(e,t)=>{let n=js.has(e),r=Y(e.body,t),i=e.bounds,a=i[0],o=i.length===1&&a.pattern.kind===`Name`,s=Y(a.set,t);return t=>{let c=o?s(t):ho(i,t),l=typeof c==`object`&&c.kind===`ProductSet`&&i.length>1?c.factors:[c];if(e.isRecursive||n||!l.every(pe)){let n=bo(c,e=>r(oo(t,go(i,t.scope,e))),yo(i,e.body,c));return i.length===1?nn(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=Dt(c);return nn(Le(n,n.map(n=>r(oo(t,io(t.scope,e,n))))))}let u=[];return mo(i,t,e=>{let n=i.length===1?As(a.pattern,e)??!1:R(i.map(t=>As(t.pattern,e)??!1));return u.push([n,r(oo(t,e))]),!1}),yt(u)}},Ps=(e,t)=>{let n=js.has(e);n&&Ms(e.function);let r=Y(e.function,t),i=e.updates.map(e=>({path:e.path.map(e=>e.kind===`Field`?{kind:`Field`,name:e.name,args:[]}:{kind:`Index`,name:``,args:e.args.map(e=>Y(e,t))}),value:Y(e.value,t)})),a=(e,t)=>e.kind===`Field`?e.name:e.args.length===1?e.args[0](t):R(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(oo(i,io(i.scope,`@`,e))):hn(e,a(t[r],i),e=>o(e,t,r+1,i,s),n&&r===0);return e=>{let t=r(e);for(let n of i)t=o(t,n.path,0,e,n.value);return t}},Fs=(e,t)=>{if(e.kind===`SetFilter`){let n=Y(e.predicate,t),r=Y(e.bound.set,t),i=e.bound.pattern;return t=>{let a=r(t),o=e=>so(n(oo(t,ao(t.scope,i,e))),`a set filter predicate`);if(!pe(a)){if(A(a))return{kind:`FilteredSet`,base:a,test:o};throw new u(`TLC encountered a non-enumerable quantifier bound ${U(a)} at ${Ga(e.bound.set)}`)}return Ie(B(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return mo(e.bounds,t,e=>(r.push(n(oo(t,e))),!1)),mt(r)}}let n=e.set;if(!n)return()=>{throw new u(`TLC cannot evaluate an unbounded CHOOSE at ${Ga(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(mo([i],t,i=>so(r(oo(t,i)),`the body of CHOOSE`)?(n.value=As(e.pattern,i),!0):!1),n.value===void 0)throw new u(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${qa(e)}`);return n.value}},Is=e=>[...e].sort((e,t)=>L(e.name,t.name)),Ls=e=>e.some((t,n)=>n>0&&e[n-1].name===t.name),Rs=(e,t)=>new u(`In evaluation, the identifier ${e} is either undefined or not an operator.\n${qa(t)}`),zs=e=>new u(`A primed expression appears outside an action at ${Ga(e)}`),Bs=(e,t)=>{let n=Is(e),r=Ls(n),i=n.map(e=>Y(e.value,t));if(r)return e=>yt(n.map((t,n)=>[t.name,i[n](e)]));let a=n.map(e=>e.name),o=e.map(e=>e.name);return o.some((e,t)=>e!==a[t])?e=>ye(a,i.map(t=>t(e)),o):e=>Le(a,i.map(t=>t(e)))},Vs=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Go(t)?()=>t:()=>{throw no(e)}}case`Decimal`:return()=>{throw to(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return ms(e,t);case`Apply`:return Os(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new u(`An operator cannot be used as a value at ${Ga(e)}`)};case`And`:{let t=e.operands.map(n);return n=>{for(let r=0;r<t.length;r++){let i=t[r](n);if(typeof i!=`boolean`)throw Ya(e,r,i);if(!i)return!1}return!0}}case`Or`:{let t=e.operands.map(n);return e=>{for(let n=0;n<t.length;n++)if(so(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw zs(e);return t({environment:n.environment,scope:n.scope,state:n.next,next:void 0})}}case`Unchanged`:{let t=n({kind:`Prime`,expression:e.expression,location:e.location}),r=n(e.expression);return e=>Re(t(e),r(e))}case`If`:{let t=n(e.condition),r=n(e.consequent),i=n(e.alternative);return n=>{let a=t(n);if(typeof a!=`boolean`)throw Ja(e,a);return a?r(n):i(n)}}case`Case`:{let t=e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),r=e.other?n(e.other):void 0;return n=>{for(let e of t)if(so(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new u(`Attempted to evaluate a CASE with no conditions true.\n${qa(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(oo(n,Uo(e.definitions,n)))}case`Forall`:case`Exists`:return ks(e,t);case`UnboundedQuantifier`:return()=>{throw new u(`TLC cannot evaluate an unbounded quantifier at ${Ga(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return Fs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>mt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>R(t.map(t=>t(e)))}case`FunctionConstruction`:return Ns(e,t);case`FunctionSet`:{let t=n(e.domain),r=n(e.range);return e=>({kind:`FunctionSet`,domain:t(e),range:r(e)})}case`Record`:return Bs(e.fields,t);case`RecordSet`:{let t=Is(e.fields),r=t.map(e=>e.name),i=t.map(e=>n(e.value));return e=>({kind:`RecordSet`,names:r,sets:i.map(t=>t(e))})}case`Except`:return Ps(e,t);case`ExceptAt`:return t=>{let n=ro(t.scope,`@`);if(n?.kind!==`Value`)throw new u(`@ appears outside an EXCEPT at ${Ga(e)}`);return n.value};case`FunctionApplication`:{Ms(e.function);let t=n(e.function),r=e.args.map(n),i=Za(e);if(r.length===1){let e=r[0];return n=>fn(t(n),e(n),void 0,i)}return e=>fn(t(e),R(r.map(t=>t(e))),void 0,i)}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Xa(e),a=Qa(e);return e=>fn(t(e),r,i,a)}case`Product`:{let t=e.factors.map(n);return e=>({kind:`ProductSet`,factors:t.map(t=>t(e))})}case`BoxAction`:case`AngleAction`:{let t=n(e.action),r=n({kind:`Unchanged`,expression:e.subscript,location:e.location}),i=e.kind===`BoxAction`;return e=>i?so(t(e),`an action`)||r(e):so(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>Ba(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new u(`The subexpression reference at ${Ga(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new u(`TLC cannot evaluate a temporal formula in a state at ${Ga(e)}`)}}},Hs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),Us=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ws=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Hs.has(e.kind))return!1;let a=Us(e),o=(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`;if(a!==void 0&&!o&&!t.constants.has(a)){let e=t.substitutions.get(a)??a,o=t.definitions.get(e);if(!o&&qo(e,t))return!1;let s=t.variableIndex.get(a);if(s!==void 0&&n.add(s),o&&!r.has(o)&&(r.add(o),!i(o.body)||o.kind!==`Operator`&&!o.bounds.every(e=>i(e.set))))return!1}return Un(e).every(i)};return i(e)?[...n]:void 0},Gs=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
23
+ `)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;wr.value??=Cr();let o=wr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Nr={kind:`Local`},Pr=/* @__PURE__ */ new WeakMap,Fr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Ir=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Lr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Rr=e=>e.kind===`Operator`?e.params.length:0,zr=e=>e.name.includes(`!`),Br=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},Vr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Hr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Ur=e=>e.kind===`Name`?[e.name]:e.names,Wr=e=>e.flatMap(e=>Ur(e.pattern)),Gr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Lr(n.location,t.location))?n:void 0},Kr=(e,t)=>{let n=Gr(e,t);return n?{key:t.getKey(n),arity:Rr(n)}:t.imported.get(e)},qr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Jr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),Yr=(e,t,n,r)=>{let i=e.location,a=Pr.get(e);if(a!==void 0)return a;if(Fr(t))return e.kind===`Apply`?e.args.length:0;if(Jr(t))return 0;let o=Br(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Nr),o)return o.arity;let s=Kr(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,Pr.set(e,s.arity),s.arity;let l=t.slice(0,t.indexOf(`!`));if(l!==``&&(Br(n,l)!==void 0||Kr(l,r)!==void 0||r.facts.has(l)))return e.kind===`Apply`?e.args.length:0;let u=`at line ${i.line}, column ${i.column}`;throw new c(qr(t)?`Could not find declaration or definition of symbol '${t}' ${u}`:`Unknown operator: \`${t}' ${u}`)},Xr=(e,t,n)=>(e.forEach(e=>q(e.set,t,n)),e.reduce((e,t)=>Vr(e,Ur(t.pattern)),t)),Zr=(e,t)=>e.kind===`Identifier`&&e.name===t&&W(e)||Un(e).some(e=>Zr(e,t)),Qr=(e,t)=>{e.kind===`FunctionConstruction`&&Zr(e.body,t)&&(e.isRecursive=!0),Un(e).forEach(e=>Qr(e,t))},$r=(e,t,n)=>{let r=Vr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(q(e.body,Xr(e.bounds,r,n),n),Qr(e.body,e.name)):q(e.body,Hr(r,e.params),n)},ei=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Rr(t),parent:e}),t);return e.reduce((e,t)=>(zr(t)||$r(t,e,n),{name:t.name,arity:Rr(t),parent:e}),r)},ti=(e,t,n,r,i)=>{let a=e.kind===`Operator`?e.params[t]:void 0,o=n.location;if(a?.arity===0&&r>0)throw new c(`Operator used in argument number ${t+1} has incorrect number of arguments at line ${o.line}, column ${o.column}`);let s=i?.kind===`Operator`&&i.params.some(e=>e.arity>0);if(a!==void 0&&a.arity>0&&(r!==a.arity||s))throw new c(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},ni=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,ri=(e,t,n)=>{let r=Br(t,e.operator)?void 0:Gr(e.operator,n),i=Yr(e,e.operator,t,n);if(i!==e.args.length)throw new c(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=ni(e),o=a===void 0||Br(t,a)?void 0:Gr(a,n),s=ii(e,t,n);r&&ti(r,i,e,s,o)})},ii=(e,t,n)=>e.kind===`Identifier`?Yr(e,e.name,t,n):e.kind===`OperatorReference`?Yr(e,e.operator,t,n):(q(e,t,n),e.kind===`Lambda`?e.params.length:0),q=(e,t,n)=>{let r=e=>e.forEach(e=>q(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=Yr(e,e.name,t,n);if(r>0)throw new c(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:Yr(e,e.operator,t,n);return;case`Apply`:ri(e,t,n);return;case`Lambda`:q(e.body,Vr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:q(e.expression,t,n);return;case`If`:r([e.condition,e.consequent,e.alternative]);return;case`Case`:r(e.arms.flatMap(e=>[e.condition,e.body])),e.other&&q(e.other,t,n);return;case`Let`:q(e.body,ei(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:q(e.body,Xr(e.bounds,t,n),n);return;case`UnboundedQuantifier`:q(e.body,Vr(t,e.names),n);return;case`Choose`:e.set&&q(e.set,t,n),q(e.body,Vr(t,Ur(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:q(e.predicate,Xr([e.bound],t,n),n);return;case`SetMap`:q(e.expression,Xr(e.bounds,t,n),n);return;case`FunctionSet`:r([e.domain,e.range]);return;case`Record`:case`RecordSet`:if(new Set(e.fields.map(e=>e.name)).size<e.fields.length)throw new c(`Non-unique fields in constructor at line ${e.location.line}, column ${e.location.column}`);r(e.fields.map(e=>e.value));return;case`Except`:r([e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])]);return;case`FunctionApplication`:r([e.function,...e.args]);return;case`FieldAccess`:q(e.record,t,n);return;case`Product`:r(e.factors);return;case`BoxAction`:case`AngleAction`:r([e.action,e.subscript]);return;case`WeakFairness`:case`StrongFairness`:r([e.subscript,e.action]);return;case`Label`:q(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>ii(e,t,n))}},ai=(e,t,n)=>(e.set&&q(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),oi=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?ai(t,e,n):(t.kind===`AssumeProve`?oi(t,e,n):q(t,e,n),e),t);return q(e.conclusion,r,n),r},si=(e,t,n)=>e.kind===`AssumeProve`?oi(e,t,n):(q(e,t,n),t),ci=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>li(t,e,n),t)},li=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=si(r.statement,t,n);return ci(e.proof,i,n),r.isSuffices?i:t}case`Define`:return ei(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return q(r.expression,t,n),ci(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>q(e,t,n)),t;case`Take`:return Vr(Xr(r.bounds,t,n),r.names);case`Pick`:{let i=Vr(Xr(r.bounds,t,n),r.names);return q(r.body,i,n),ci(e.proof,t,n),i}default:return ci(e.proof,t,n),t}},ui=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:si(e.statement,void 0,t);ci(e.proof,n,t)},di=(e,t)=>new c(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),fi=(e,t,n,r)=>t.reduce((e,t)=>{if(Br(e,t)||n(t))throw di(t,r);return{name:t,arity:0,parent:e}},e),pi=(e,t,n,r)=>(e.forEach(e=>hi(e.set,t,n)),e.reduce((e,t)=>fi(e,Ur(t.pattern),n,r),t)),mi=(e,t,n)=>{if(e.kind===`Function`){let r=Vr(t,[e.name]),i=pi(e.bounds,r,n,e.location);hi(e.body,i,n);return}let r=fi(t,e.params.map(e=>e.name),n,e.location);hi(e.body,r,n)},hi=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:hi(e.body,fi(t,e.params,n,r),n);return;case`Let`:{let i=e.definitions.filter(e=>e.isRecursive).map(e=>e.name),a=e.definitions.reduce((e,t)=>{if(Br(e,t.name)||n(t.name))throw di(t.name,t.location);return mi(t,Vr(e,i),n),Vr(e,[t.name])},t);hi(e.body,fi(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:hi(e.body,pi(e.bounds,t,n,r),n);return;case`SetFilter`:hi(e.predicate,pi([e.bound],t,n,r),n);return;case`SetMap`:hi(e.expression,pi(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:hi(e.body,fi(t,e.names,n,r),n);return;case`Choose`:e.set&&hi(e.set,t,n),hi(e.body,fi(t,Ur(e.pattern),n,r),n);return;default:Un(e).forEach(e=>hi(e,t,n))}},gi=e=>[...e.definitions.map(e=>({name:e.name,kind:`Definition`,location:e.location,isRecursive:e.isRecursive})),...e.instances.flatMap(e=>e.name===void 0?[]:[{name:e.name,kind:`Definition`,location:e.location,isRecursive:!1}]),...e.constants.map(e=>({name:e.name,kind:`Constant`,location:e.location,isRecursive:!1})),...e.variables.map(e=>({name:e.name,kind:`Variable`,location:e.location,isRecursive:!1}))],_i=(e,t)=>{let n=/* @__PURE__ */ new Map;gi(e).sort((e,t)=>Lr(e.location,t.location)?-1:1).forEach(e=>n.set(e.name,[...n.get(e.name)??[],e]));let r=(e,n)=>{if(!t.imported.has(e)&&!Ir.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Lr(r,n)};n.forEach(e=>e.forEach((n,i)=>{let a=e.slice(0,i).some(e=>e.kind===`Definition`||n.kind===`Definition`||e.kind!==n.kind),o=t.imported.get(n.name)?.declaration===n.kind;if(a||!o&&r(n.name,n.location))throw di(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Lr(t.location,e))||r(t,e);e.definitions.forEach(e=>mi(e,void 0,i(e.location))),e.assumptions.forEach(e=>hi(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=fi(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>hi(e.expression,n,t))})},vi=(e,t)=>{let n=new Map([...Ir,...t]);return e.constants.forEach(e=>n.set(e.name,{key:e.name,arity:e.arity})),e.variables.forEach(e=>n.set(e.name,{key:e.name,arity:0})),n},yi=(e,t,n,r)=>{let i=Hr(void 0,e.params),a={imported:vi(n,r.imported),definitions:r.definitions,getKey:r.getKey,location:e.location,self:void 0,facts:/* @__PURE__ */ new Set},o=new Map(e.substitutions.map(e=>[e.name,e.expression])),s=`in the instance of ${e.module} at line ${e.location.line}, column ${e.location.column}`;return o.forEach((e,n)=>{if(t.every(e=>e.name!==n))throw new c(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Br(i,n)&&!Kr(n,a))throw new c(`Substitution missing for symbol ${n} ${s}`);let r=o.get(n)??{kind:`Identifier`,name:n,location:e.location};if(t.arity===0||r.kind!==`Identifier`)return q(r,i,a),[n,r];if(Yr(r,r.name,i,a)!==t.arity)throw new c(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},bi=(e,t)=>{let n=vi(e,t.imported),r=new Set([...e.theorems,...e.assumptions].flatMap(e=>e.name??[])),i=(e,i)=>({imported:n,definitions:t.definitions,getKey:t.getKey,location:e,self:i,facts:r});e.definitions.forEach(e=>$r(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>q(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ui(e,i(e.location,void 0)))},xi=/* @__PURE__ */ new WeakSet,Si=/* @__PURE__ */ new WeakSet,Ci=(e,t,n=/* @__PURE__ */ new Set)=>{let r=ki(e,(e,r)=>Ci(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&W(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&W(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},wi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),Ti=/* @__PURE__ */ new WeakMap,Ei=e=>Ti.get(e)??e.name,Di=(e,t)=>(Ei(t)!==e.name&&Ti.set(e,Ei(t)),e),Oi=(e,t)=>Di(e.kind===`Function`?{...e,bounds:wi(e.bounds,e=>t(e,[])),body:t(e.body,Wr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),ki=(e,t)=>{let n=e=>t(e,[]);switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return e;case`Apply`:return{...e,args:e.args.map(n)};case`Lambda`:return{...e,body:t(e.body,e.params)};case`And`:case`Or`:return{...e,operands:e.operands.map(n)};case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return{...e,expression:n(e.expression)};case`If`:return{...e,condition:n(e.condition),consequent:n(e.consequent),alternative:n(e.alternative)};case`Case`:return{...e,arms:e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),other:e.other?n(e.other):void 0};case`Let`:{let n=e.definitions.map(e=>e.name);return{...e,definitions:e.definitions.map(e=>Oi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:wi(e.bounds,n),body:t(e.body,Wr(e.bounds))};case`UnboundedQuantifier`:return{...e,body:t(e.body,e.names)};case`Choose`:return{...e,set:e.set?n(e.set):void 0,body:t(e.body,Ur(e.pattern))};case`SetEnumeration`:case`Tuple`:return{...e,elements:e.elements.map(n)};case`SetFilter`:return{...e,bound:{pattern:e.bound.pattern,set:n(e.bound.set)},predicate:t(e.predicate,Ur(e.bound.pattern))};case`SetMap`:return{...e,bounds:wi(e.bounds,n),expression:t(e.expression,Wr(e.bounds))};case`FunctionSet`:return{...e,domain:n(e.domain),range:n(e.range)};case`Record`:case`RecordSet`:return{...e,fields:e.fields.map(e=>({name:e.name,value:n(e.value)}))};case`Except`:return{...e,function:n(e.function),updates:e.updates.map(e=>({path:e.path.map(e=>e.kind===`Index`?{kind:`Index`,args:e.args.map(n)}:e),value:n(e.value)}))};case`FunctionApplication`:return{...e,function:n(e.function),args:e.args.map(n)};case`FieldAccess`:return{...e,record:n(e.record)};case`Product`:return{...e,factors:e.factors.map(n)};case`BoxAction`:case`AngleAction`:return{...e,action:n(e.action),subscript:n(e.subscript)};case`WeakFairness`:case`StrongFairness`:return{...e,subscript:n(e.subscript),action:n(e.action)};case`Label`:return{...e,body:n(e.body)};case`Subexpression`:return{...e,selectors:e.selectors.map(e=>`args`in e?{...e,args:e.args.map(n)}:e)}}},Ai=`LAMBDA`,ji=(e,t=/* @__PURE__ */ new Set)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0,r=n===void 0||t.has(n)?[]:[n];return ki(e,(e,n)=>(r.push(...ji(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Mi=e=>{let t=/* @__PURE__ */ new Map;return n=>{if(!e.substitutedNames.has(n))return n;let r=t.get(n)??e.instantiation.createName(n);return t.set(n,r),r}},Ni=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),Pi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Fi=(e,t)=>e.map(e=>({pattern:Pi(e.pattern,t),set:e.set})),Ii=(e,t)=>e.kind===`Function`?{...e,bounds:Fi(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Li=(e,t,n)=>{let r=e=>t.get(e)??e;switch(e.kind){case`Identifier`:return{...e,name:r(e.name)};case`Apply`:case`OperatorReference`:return{...e,operator:r(e.operator)};case`Label`:return{...e,params:e.params.map(r)};case`Lambda`:return{...e,params:e.params.map(n)};case`Let`:return{...e,definitions:e.definitions.map(e=>Di({...Ii(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Fi(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:Pi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:Pi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Ri=e=>`${e}@ENABLED`,zi=(e,t)=>e.modes.get(t)??e,Bi=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!W(e)&&t.instantiation.variables.has(e.name),Vi=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>Vi(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Bi(e,t,n))return[e.name];let r=W(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?Vi(r.body,t,/* @__PURE__ */ new Map):void 0},Hi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Bi(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Ri(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:qi(e.expression,zi(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[qi(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:qi(e.subscript,zi(t,`Primed`),n),location:r},qi(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=Vi(e.expression,t,n);if(r!==void 0)return{kind:`And`,operands:r.map(r=>({kind:`Apply`,operator:`=`,args:[{kind:`Prime`,expression:{kind:`Identifier`,name:Ri(r),location:e.location},location:e.location},qi({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ui=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Wi=(e,t)=>e.map(e=>Ui(e.name,t)),Gi=(e,t,n,r)=>{let i=Array.from({length:n},(e,t)=>`argument${t}`);return{kind:`Lambda`,params:i,body:{kind:`Apply`,operator:e,args:[...Wi(t,r),...i.map(e=>Ui(e,r))],location:r},location:r}},Ki=(e,t)=>e.instantiation.isParameterized(t)?Yi(e,t):void 0,qi=(e,t,n)=>{let r=e=>qi(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!W(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:qi(e.expression,zi(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:qi(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},zi(t,`Enabled`),n)};let s=Hi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return xi.add(n),n;let r=Ki(t,e.name);if(r===void 0)return e;if(a.length===0)return{...e,name:r};let o=i.getDefinition(e.name);return o&&Rr(o)>0?Gi(r,a,Rr(o),e.location):{kind:`Apply`,operator:r,args:Wi(a,e.location),location:e.location}}if(e.kind===`Apply`&&o(e.operator)){let n=e.args.map(r),o=i.substitutions.get(e.operator),s=i.substitutions.size>0||a.length>0||Si.has(e),l=e=>(s&&Si.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return l({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return l({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:Ai,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:Ai,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new c(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let u=Ki(t,e.operator);return u===void 0?l({...e,args:n}):{...e,operator:u,args:[...Wi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ki(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Gi(n,a,Rr(r),e.location):{...e,operator:n}}let l=Mi(t),u=ki(e,(e,i)=>i.length===0?r(e):qi(e,t,Ni(n,i,l)));return Li(u,n,l)},Ji=(e,t,n)=>t.length===0?e:e.kind===`Operator`?{...e,params:[...t,...e.params]}:{kind:`Operator`,name:e.name,params:t,body:{kind:`Let`,definitions:[e],body:Ui(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},Yi=(e,t)=>{let n=e.copies.get(t);if(n!==void 0)return n;let r=e.instantiation,i=r.getDefinition(t);return i?r.addDefinition(t,i.name,n=>{e.copies.set(t,n);let a=Mi(e),o=Ii(Oi(i,(t,n)=>qi(t,e,Ni(/* @__PURE__ */ new Map,n,a))),a);return Ji(o,r.params,i.location)}):t},Xi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>ji(e))),r=r=>{let i={instantiation:e,substitutedNames:n,copies:/* @__PURE__ */ new Map,mode:r,modes:t};return t.set(r,i),i},i=r(`Normal`);return r(`Enabled`),r(`Primed`),{getKey:e=>Ki(i,e)??e,getQualifiedKey:t=>{let n=Ki(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(t,r.name,()=>Ji(r,e.params,r.location)):t}}},Zi=e=>e.kind===`Let`&&e.instances!==void 0||Un(e).some(Zi),Qi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=ki(e,e=>Qi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Lr(n.location,e.location)),a=i===-1?e.length:i;return[...e.slice(0,a),...t(n,r),...e.slice(a)]},i.definitions),body:i.body,location:i.location}},$i=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,ea=(e,t)=>e.definitions.some(e=>Zi(e.body))||e.assumptions.some(e=>Zi(e.expression))||e.theorems.some(e=>{let t=$i(e);return t!==void 0&&Zi(t)})?{...e,definitions:e.definitions.map(e=>Oi(e,e=>Qi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Qi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=$i(e);return n===void 0?e:{...e,statement:Qi(n,t,[])}})}:e,ta=/* @__PURE__ */ new WeakMap,na=/* @__PURE__ */ new WeakMap,J=(e,t,n)=>e.isInit?{environment:e.environment,scope:t,state:n,next:void 0}:{environment:e.environment,scope:t,state:e.current,next:n},ra=(e,t,n)=>{let r=e.slice();return r[t]=n,r},ia=(t,r,i=X(t,r))=>{if(typeof i==`boolean`)return i;try{return so(i,`the action at ${Ga(t)}`)}catch(t){throw r.next===void 0?t:n(t,e.error)}},aa=e=>e===`=`||e===`\\in`||e===`\\subseteq`,oa=(e,t,n)=>{let r=n?ta:na,i=r.get(e);if(i)return i;let a=Fa(e,t,n);return r.set(e,a),a},sa=(e,t)=>{if(t.constants.has(e))return;let n=co(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},ca=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return ca(e.expression,t,!1,r);if(e.kind!==`Identifier`||W(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=sa(e.name,t);if(a&&!r.has(a))return r.add(a),ca(a.body,t,n,r)},la=(e,t,n)=>ca(e,t,!n),ua=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ua(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Uo(e.definitions,J(r,t,n));return ua(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=ro(t,e.name);if(a?.kind===`Thunk`)return ua(a.expression,a.scope,n,r,i);if(a)return;let o=r.environment.variableIndex.get(e.name);if(o!==void 0)return i?void 0:o;let s=sa(e.name,r.environment);if(s)return ua(s.body,void 0,n,r,i)},da=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||W(e))||!t&&e.kind===`Prime`,fa=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>fa(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`||n.has(e.name))return;let r=t.variableIndex.get(e.name);if(r!==void 0)return[r];let i=sa(e.name,t);if(i)return n.add(e.name),fa(i.body,t,n)},pa=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ma=(e,t,n)=>pa.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||aa(e.operator)&&(da(e.args[0],n)||la(e.args[0],t,n)!==void 0)||W(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,ha=/* @__PURE__ */ new Set,ga=(e,t)=>{if(W(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return ki(e,(e,i)=>{let a=r.isReadOnly?ga(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},_a=(e,t,n)=>{if(e.kind===`Or`||e.kind===`Exists`)return!0;let r=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0,i=r===void 0||W(e)?void 0:co(r,t);return i&&!n.has(i)&&(n.add(i),_a(i.body,t,n))?!0:Un(e).some(e=>_a(e,t,n))},va=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=la(e.args[0],t,n);if(r!==void 0)return{kind:`Assign`,expression:e,index:r,value:e.args[1]}}if(!n&&e.kind===`Unchanged`){let n=fa(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ma(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:oa(e,t,n)}},ya=(e,t)=>{if(e.bounds.length<2||e.body.kind!==`And`)return;let n=e.bounds.flatMap(e=>e.pattern.kind===`Name`?[e.pattern.name]:[]);if(n.length!==e.bounds.length)return;let r=new Set(n.slice(0,-1)),i=e.body.operands,a=i.findIndex(e=>{if(Ws(e,t)===void 0||_a(e,t,/* @__PURE__ */ new Set))return!0;let n=ga(e,ha);return!n||n.some(e=>r.has(e))}),o=a===-1?i.length:a;if(o!==0)return{names:n,guards:i.slice(0,o),rest:i.slice(o)}},ba=(e,t,n)=>{let r=e.map(e=>va(e,t,n)),i=r.map(()=>void 0),a=(e,n,o,s,c,l)=>{let u=s,d;for(let s=e;s<r.length;s++){if(s>0&&n.isInit&&za(u)){d??=J(n,o,u);for(let e=s;e<r.length;e++)if(!xa(r[e].expression,d))return;l(u,c);return}let e=r[s];if(e.kind===`Guard`){d??=J(n,o,u);let r=i[s]??=ps(e.expression,t);if(!ia(e.expression,d,r(d)))return;continue}if(e.kind===`Assign`){d??=J(n,o,u);let r=(i[s]??=ps(e.value,t))(d),a=u[e.index];if(a===void 0)u=ra(u,e.index,r),d=void 0;else if(!Re(a,r))return;continue}if(e.kind===`Unchanged`){let t=n.current;if(!t)return;for(let n of e.indices){let e=u[n];if(e===void 0)u=ra(u,n,t[n]),d=void 0;else if(!Re(e,t[n]))return}continue}let f=s+1;e.runner(n,o,u,c,!0,(e,t)=>a(f,n,o,e,t,l));return}l(u,c)};return(e,t,n,r,i,o)=>a(0,e,t,n,r,o)},xa=(e,t)=>so(X(e,t),`the initial predicate at ${Ga(e)}`),Sa=e=>(t,n,r,i,a,o)=>{ia(e,J(t,n,r))&&o(r,i)},Ca=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),d=J(r,i,a);if(a[l]!==void 0){ia(e,d)&&c(a,o);return}let f=X(n,d),p=e.operator===`\\subseteq`?{kind:`Subset`,base:f}:f;if(!z(p))throw new u(`In computing ${r.isInit?`initial`:`next`} states, the right side of \\IN is not enumerable.\n${qa(e)}`);let m=Ut(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(ra(a,l,m[e]),o)}},wa=/* @__PURE__ */ new WeakMap,Ta=e=>{let t=wa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(Ta),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return wa.set(e,n),n},Ea=(e,t)=>{let n=e.args[0],r=Sa(e),i=(e,r,i)=>ua(n,r,i,e,!t)??-1,a=Ca(e,i);return(t,n,o,s,c,l)=>{let u=i(t,n,o);if(u===-1||o[u]!==void 0){r(t,n,o,s,c,l);return}if(e.operator!==`=`){a(t,n,o,s,c,l);return}let d=X(e.args[1],J(t,n,o));l(ra(o,u,d),s)}},Da=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=ro(t,e.name);if(i?.kind===`Thunk`)return Da(i.expression,i.scope,n,r);let a=i?void 0:co(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,J(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:io(void 0,a.name,o)}},Oa=(e,t,n)=>{let r=Sa(e);return(i,a,o,s,c,l)=>{let d=Da(e.function,a,o,i);if(!d){r(i,a,o,s,c,l);return}let f=J(i,a,o),p=e.args.map(e=>X(e,f)),m=p.length===1?p[0]:R(p),h=ho(d.bounds,J(i,d.scope,o));if(!$t(m,h))throw new u(`Attempted to apply a function to ${U(m)}, which is not in its domain at ${Ga(e)}`);oa(d.body,t,n)(i,go(d.bounds,d.scope,m),o,s,c,l)}},ka=(e,t)=>{if(fa(e,t))return ba([Ta(e)],t,!1);if(e.kind===`Tuple`)return oa(Ta(e),t,!1);let n=Ta(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?ro(i,e.name):void 0;l?.kind===`Thunk`?oa(Ta(l.expression),t,!1)(r,l.scope,a,o,s,c):ia(n,J(r,i,a))&&c(a,o)}},Aa=(e,t,n)=>{let r=e.operator,i=Sa(e),a=W(e)?void 0:Do(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=xs(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=oa(a.body,t,n),o.body(e,i(J(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=J(a,o,s),f=ko(e,d);f?.kind===`Closure`?oa(f.body,t,n)(a,Vo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ja=(e,t,n)=>{let r=Sa(e),i=W(e)?void 0:sa(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=ro(s,e.name);f?.kind===`Thunk`?oa(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=oa(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},Ma=(e,t,n)=>{if(n.has(e))return;if(n.add(e),e.kind===`And`){let n=e.operands[0];if(n.kind!==`Apply`||n.operator!==`=`||W(n))return;let r=n.args[0],i=n.args[1];if(r.kind!==`FunctionApplication`||i.kind!==`String`)return;let a=r.args.length===1?r.args[0]:void 0;if(a?.kind!==`Identifier`||!W(a))return;let o=ca(r.function,t,!1);return o===void 0?void 0:{variable:o,argument:a.name,labels:/* @__PURE__ */ new Set([i.value])}}if(e.kind===`Or`){let r=e.operands.map(e=>Ma(e,t,n)),i=r[0],a=r.every(e=>e!==void 0&&e.variable===i?.variable&&e.argument===i.argument);if(!i||!a)return;let o=new Set(r.flatMap(e=>[...e?.labels??[]]));return{variable:i.variable,argument:i.argument,labels:o}}if(e.kind!==`Apply`||W(e))return;let r=Do(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=Ma(r.body,t,n),a=r.params.findIndex(e=>e.arity===0&&e.name===i?.argument),o=e.args[a];if(i&&o?.kind===`Identifier`&&W(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Na=(e,t,n)=>{let r=ro(t,n.argument),i=e.current?.[n.variable];if(r?.kind!==`Value`||typeof i!=`object`||i.kind!==`Function`)return;let a=i.domain.indexOf(r.value),o=a===-1?void 0:i.range[a];return typeof o==`string`?o:void 0},Pa=(e,t,n)=>{let r=e.map(e=>oa(e,t,n)),i=n?[]:e.map(e=>Ma(e,t,/* @__PURE__ */ new Set)),a=i.find(e=>e!==void 0),o=i.map(e=>e?.variable===a?.variable&&e?.argument===a?.argument?e?.labels:void 0);return(e,t,n,i,s,c)=>{let l=a&&Na(e,t,a);for(let a=0;a<r.length&&!e.isStopped;a++)(l===void 0||o[a]?.has(l)!==!1)&&r[a](e,t,n,i,s,c)}},Fa=(e,t,n)=>{let r=e=>oa(e,t,n);switch(e.kind){case`And`:return ba(e.operands,t,n);case`Or`:return Pa(e.operands,t,n);case`Exists`:{let i=r(e.body),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name;return(t,n,r,o,s,c)=>{let l=po(X(a.set,J(t,n,r)),a.set);for(let a=0;a<l.length&&!t.isStopped;a++)i(t,io(n,e,l[a]),r,o,s,c)}}let o=n?void 0:ya(e,t);if(o){let r=ba(o.rest,t,n);return(t,n,i,a,s,c)=>{let l=J(t,n,i),u=e.bounds.map(e=>po(X(e.set,l),e.set));if(u.some(e=>e.length===0))return;let d=(e,n)=>{if(e<0)r(t,n,i,a,s,c);else for(let r of u[e]){if(t.isStopped)return;let a=io(n,o.names[e],r);if(e===u.length-1){let e=J(t,a,i);if(!o.guards.every(t=>ia(t,e)))continue}d(e-1,a)}};d(u.length-1,n)}}return(t,n,r,a,o,s)=>{mo(e.bounds,J(t,n,r),e=>(i(t,e,r,a,o,s),t.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ws(e,t)!==void 0&&!_a(e.body,t,/* @__PURE__ */ new Set)?ga(e,ha):void 0;return(t,n,r,o,s,c)=>{let l=[];mo(e.bounds,J(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>ro(n,e)?.kind===`Value`)){u.every(n=>ia(e.body,J(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&za(r)?u.slice(n).every(n=>xa(e.body,J(t,n,r)))&&c(r,a):i(t,u[n],r,a,!0,(e,t)=>d(n+1,e,t))};d(0,r,o)}}case`If`:{let t=r(e.consequent),n=r(e.alternative);return(r,i,a,o,s,c)=>{let l=X(e.condition,J(r,i,a));(so(l,`an IF condition`)?t:n)(r,i,a,o,s,c)}}case`Case`:{let t=e.arms.map(e=>({condition:e.condition,body:r(e.body)})),n=e.other?r(e.other):void 0;return(r,i,a,o,s,c)=>{let l=J(r,i,a),d=t.find(e=>so(X(e.condition,l),`a CASE guard`))?.body??n;if(!d)throw new u(`In computing ${r.isInit?`initial`:`next`} states, TLC encountered a CASE with no conditions true.\n${qa(e)}`);d(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=$o(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Uo(e.definitions,J(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new u(`UNCHANGED appears in an initial predicate at ${Ga(e)}`)}:ka(e.expression,t);case`AngleAction`:{let t=r(e.action),n=Ta(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{ia(n,J(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,Ta(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ja(e,t,n);case`FunctionApplication`:return Oa(e,t,n);case`Apply`:{let i=e.args;if(aa(e.operator)){let r=la(i[0],t,n);if(r!==void 0)return e.operator===`=`?ba([e],t,n):Ca(e,()=>r);if(da(i[0],n))return Ea(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],J(t,n,r));(typeof c==`boolean`?c:so(c,`the left side of => at ${Ga(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return Aa(e,t,n)}default:return Sa(e)}},Ia=/* @__PURE__ */ new WeakMap,La=e=>{let t=Ia.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ia.set(e,n),n},Ra=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),za=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},Ba=(e,t)=>{let n=t.state;if(!n||!za(n))throw new u(`ENABLED needs a complete state at ${Ga(e)}`);let r=t.environment,i={environment:r,isInit:!1,current:n,isStopped:!1};return oa(e,r,!1)(i,t.scope,La(r),void 0,!1,()=>{i.isStopped=!0}),i.isStopped},Va=(t,r,i,a)=>{let o={environment:r,isInit:!0,current:void 0,isStopped:!1};oa(t,r,!0)(o,a,La(r),void 0,!1,t=>{if(!o.isStopped){if(!za(t))throw n(new u(`The initial predicate does not assign ${Ra(t,r).join(`, `)}`),e.error);o.isStopped=i(t)}})},Ha=(e,t,n)=>{let r=[];return Va(e,t,e=>(r.push(e),!1),n),r},Ua=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return oa(e,n,!1)(a,i,La(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!za(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Ra(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Wa=(e,t,n,r)=>{let i=[];return Ua(e,t,n,e=>(i.push(e),!1),r),i},Ga=e=>`line ${e.location.line}, column ${e.location.column}`,Ka=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,qa=e=>{let t=e.location.range;return t?Ka(t):Ga(e)},Ja=(e,t)=>new u(`A non-boolean expression (${fr(t)}) was used as the condition of an IF.\n${qa(e)}`),Ya=(e,t,n)=>{let r=e.location.range;if(r?.start.line===e.location.line&&r.start.column===e.location.column||!r){let r=e.operands[t],i=r.kind===`Number`?String(r.value):qa(r);return new u(`A non-boolean expression (${fr(n)}) was used as a formula in a conjunction.\n${i}`)}let i=e.operands[Math.max(t,1)].location.range?.end??r.end;return new u(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${fr(n)}.\n${qa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Xa=e=>t=>ln(t)?`Attempted to select nonexistent field "${e.field}" from the record\n${U(t)}\n${qa(e)}`:cn(t,e.field),Za=e=>t=>`A non-function (${fr(t)}) was applied as a function.\n${qa(e)}`,Qa=e=>t=>`Attempted to select field "${e.field}" from a non-record value ${U(t)}\n${qa(e)}`,$a=e=>t=>{if(!j(t))throw new u(`Attempted to apply the operator DOMAIN to a non-function\n(${fr(t)})\n${qa(e)}`);return mn(t)},eo=(e,t)=>(n,r)=>{if(!A(n)||F(n))throw new u(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${qa(e)}`);return t.evaluate([n,r])},to=t=>n(new u(`TLC can't handle real numbers: ${t.text} at ${Ga(t)}`),e.configParseError),no=t=>n(new u(`TLC can't handle a number this big: ${t.text??t.value} at ${Ga(t)}`),e.error),ro=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},io=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),ao=(e,t,n)=>{if(t.kind===`Name`)return io(e,t.name,n);let r=Xt(n);if(!r||r.length!==t.names.length)throw new u(`Cannot bind <<${t.names.join(`, `)}>> to ${U(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>io(e,t,r[n]),e)},oo=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),so=(e,t)=>{if(typeof e!=`boolean`)throw new u(`Expected ${t} to be a boolean but found ${U(e)}`);return e},co=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),lo=(e,t)=>{if(e.kind===`Prime`||e.kind===`Unchanged`||e.kind===`Enabled`)return!0;let n=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0;if(n!==void 0){if(t.variableIndex.has(n)||t.builtins.get(n)?.isStateLevel)return!0;let e=co(n,t);if(e&&!t.constants.has(n)&&!uo(e,t))return!0}return Un(e).some(e=>lo(e,t))},uo=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!lo(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>lo(e.set,t)));return t.constantLevels.set(e,r),r},fo=(e,t)=>{let n=`universally (\\A) or existentially (\\E) quantify over ${t}, because this would require checking an\ninfinite number of cases.`;if(typeof e==`object`&&e.kind===`SequenceSet`)return`In TLA+, Seq(S) represents the set of all finite sequences whose elements come from the set S. Even when S\nis a finite set, the number of possible sequences in Seq(S) is unbounded because sequences can have any\nfinite length (e.g., length 0, 1, 2, and so on). As a result, TLC cannot evaluate expressions that\n${n} Note that for a finite set of sequences s, TLC handles s \\subseteq Seq(S).\nSee https://explain.tlapl.us/seq-unenumerable for additional details.`;if(typeof e==`object`&&e.kind===`InfiniteSet`&&e.name!==`STRING`&&e.name!==`Any`)return`The set ${e.name} contains infinitely many elements. As a result, TLC cannot evaluate expressions that\n${n} Note that TLC handles set membership like T \\subseteq ${e.name} for any finite set T.`},po=(e,t)=>{if(!A(e)||F(e)){let n=U(e),r=fo(e,n);throw new u(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${qa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${qa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Tt(e):B(e).elements},mo=(e,t,n)=>{let r=e.map(e=>po(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,ao(a,e[t].pattern,n)));return i(e.length-1,t.scope)},ho=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},go=(e,t,n)=>{if(e.length===1)return ao(t,e[0].pattern,n);let r=Xt(n)??[];return e.reduce((e,t,n)=>ao(e,t.pattern,r[n]),t)},_o=/* @__PURE__ */ new WeakMap,vo=(e,t)=>{let n=_o.get(t);if(n)return n;let r=t.location.range;if(!r)return;let i={parameters:e.flatMap((t,n)=>n>0&&e[n-1].set===t.set?[]:[{index:n,name:t.pattern.kind===`Name`?t.pattern.name:`<<${t.pattern.names.join(`, `)}>>`}]),body:Ka(r)};return _o.set(t,i),i},yo=(e,t,n)=>{let r=vo(e,t);if(!r)return;let i=e.length>1&&typeof n==`object`&&n.kind===`ProductSet`?n.factors:[n];return{parameters:r.parameters.map(e=>({name:e.name,domain:i[e.index]})),body:r.body}},bo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=Cn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(Be(a[t],e))return a[t+1]}let o=t(e,i),s=r.get(n);return s===void 0?r.set(n,[e,o]):s.push(e,o),o}};return i},xo=(e,t)=>{let n=ho(e.bounds,t),r=bo(n,(n,r)=>X(e.body,oo(t,go(e.bounds,io(void 0,e.name,r),n))),yo(e.bounds,e.body,n));return e.bounds.length===1?nn(r):r},So=(e,t)=>e.kind===`Function`?xo(e,t):X(e.body,t),Co=(e,t)=>{let n=uo(e,t.environment),r=n?t.environment.definitionValues.get(e):void 0;if(r!==void 0)return r;if(e.kind===`Operator`&&e.params.length>0)throw new u(`Operator ${e.name} requires ${e.params.length} arguments`);let i=oo(t,void 0);if(!n)return So(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>So(e,i));return t.environment.definitionValues.set(e,a),a},wo=(e,t)=>{let n=e.state===t.state&&e.next===t.next;if(n&&e.cache!==void 0)return e.cache;let r=X(e.expression,oo(t,e.scope));return n&&(e.cache=r),r},To=/* @__PURE__ */ new WeakMap,Eo=e=>{let t=To.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return To.set(e,n),n},Do=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return Do(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?Eo(r):t.builtins.get(e)},Oo=/* @__PURE__ */ new WeakMap,ko=(e,t)=>{if(W(e))return Ao(e.operator,t);let n=Oo.get(e),r=n===void 0?Do(e.operator,t.environment)??null:n;return n===void 0&&Oo.set(e,r),r??Ao(e.operator,t)},Ao=(e,t)=>{let n=ro(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return Do(e,t.environment)},jo=e=>e.kind===`Closure`?e.params.length:e.arity,Mo=(e,t)=>{if(e.kind===`Lambda`)return{kind:`Closure`,name:`LAMBDA`,params:e.params.map(e=>({name:e,arity:0})),body:e.body,scope:t.scope};let n=e.kind===`OperatorReference`?e.operator:e.kind===`Identifier`?e.name:void 0,r=n===void 0?void 0:Ao(n,t);if(!r)throw new u(`Expected an operator argument at ${Ga(e)}`);return r},No=(e,t,n)=>{if(e.kind===`Builtin`)return e.evaluate(t,n);if(e.kind===`Operator`)return e.call(t);let r=e.params.reduce((e,n,r)=>io(e,n.name,t[r]),e.scope);return X(e.body,oo(n,r))},Po=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:jo(e),call:n=>No(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},Fo=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=Ao(e.name,t);return n!==void 0&&jo(n)>0},Io=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),Lo=(e,t)=>{let n=t.variableIndex.get(e);if(n!==void 0)return{kind:`Variable`,index:n};let r=t.constants.get(e);if(r!==void 0)return{kind:`Value`,value:r};let i=t.substitutions.get(e);if(i!==void 0)return Lo(i,t);let a=t.definitions.get(e);if(a)return{kind:`Definition`,definition:a};let o=t.builtins.get(e);if(o&&o.arity===0&&!o.isStateLevel)return{kind:`Value`,value:o.evaluate([])}},Ro=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Go(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=W(e)?void 0:Lo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=ro(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Io(e,t)}}return t=>Io(e,t)},zo=/* @__PURE__ */ new WeakMap,Bo=(e,t)=>{let n=zo.get(e);if(n)return n;let r=Ro(e,t);return zo.set(e,r),r},Vo=(e,t,n)=>{if(t.length!==e.params.length)throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`);let r=e.scope;for(let i=0;i<t.length;i++){let a=e.params[i],o=a.arity>0?{kind:`Operator`,operator:Mo(t[i],n)}:Bo(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},Ho=(e,t,n)=>e.kind===`Closure`?X(e.body,oo(n,Vo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(ys(t,n),n):e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n),Uo=(e,t,n)=>e.reduce((e,r)=>{let i={name:r.name,binding:{kind:`Value`,value:!1},parent:e};return r.kind===`Function`?(i.binding={kind:`Thunk`,expression:{kind:`FunctionConstruction`,bounds:r.bounds,body:r.body,location:r.location,isRecursive:!0},scope:i,state:t.state,next:t.next,cache:void 0},i):r.params.length>0||n?.has(r)?(i.binding={kind:`Operator`,operator:{kind:`Closure`,name:r.name,params:r.params,body:r.body,scope:i}},i):(i.binding={kind:`Thunk`,expression:r.body,scope:i,state:t.state,next:t.next,cache:void 0},i)},t.scope),Wo=(e,t,n)=>{let r=ro(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return wo(r,n);if(r?.kind===`Operator`){if(jo(r.operator)===0)return No(r.operator,[],n);throw new u(`Operator ${e} is used without arguments`)}let i=Lo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw Rs(e,t);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return Co(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new u(`Unknown identifier ${e} at ${Ga(t)}`)},Go=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,Ko=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),qo=(e,t)=>Ko.has(e)||t.builtins.get(e)?.isStateLevel===!0,Jo={builtins:Ko,definitions:/* @__PURE__ */ new WeakMap},Yo={builtins:new Set([...Ko].filter(e=>e!==`Assert`)),definitions:/* @__PURE__ */ new WeakMap},Xo=(e,t)=>e.kind===`Operator`&&e.params.length===0&&uo(e,t),Zo=(e,t,n)=>{let r=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(r!==void 0&&!W(e)){let e=co(r,t),i=n.builtins.has(r)||t.builtins.get(r)?.isStateLevel===!0;if(!e&&i||e&&!Xo(e,t)&&Qo(e,t,n))return!0}return Un(e).some(e=>Zo(e,t,n))},Qo=(e,t,n=Jo)=>{let r=n.definitions.get(e);if(r!==void 0)return r;n.definitions.set(e,!1);let i=Zo(e.body,t,n)||e.kind===`Function`&&e.bounds.some(e=>Zo(e.set,t,n));return n.definitions.set(e,i),i},$o=(e,t)=>new Set(e.filter(e=>Qo(e,t))),es=e=>[...e.definitions.values()].some(t=>Qo(t,e)),ts=e=>[...e.definitions.values()].some(t=>Qo(t,e,Yo)),ns=/* @__PURE__ */ new WeakMap,rs=(e,t)=>{let n=ns.get(e);if(n!==void 0)return n;let r=ss(e,t,as);return ns.set(e,r),r},is=/* @__PURE__ */ new Set([`Number`,`String`,`Identifier`,`Apply`,`And`,`Or`,`If`,`Case`,`SetEnumeration`,`Tuple`,`FunctionSet`,`Record`,`RecordSet`,`FunctionApplication`,`FieldAccess`,`Product`,`SetFilter`,`SetMap`,`Forall`,`Exists`,`Choose`,`FunctionConstruction`,`Let`]),as=/* @__PURE__ */ new Set,os=(e,t,n)=>n.size===0?rs(e,t):ss(e,t,n),ss=(e,t,n)=>{if(!is.has(e.kind))return!1;if(e.kind===`Identifier`&&W(e))return n.has(e.name);if(W(e))return!1;if(e.kind===`Identifier`){let n=Lo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Xo(n.definition,t)}if(e.kind===`Apply`){if(qo(e.operator,t)||e.operator===`~>`)return!1;let r=Do(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=co(e.operator,t);if(!n||!uo(n,t)||Qo(n,t))return!1}return!(r.kind===`Closure`?r.params:[]).some(e=>e.arity>0)&&e.args.every(e=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&os(e,t,n))}let r={value:!0};return ki(e,(e,i)=>(r.value&&=os(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},cs=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,ls=e=>{let t={value:void 0};return n=>{if(t.value!==void 0)return t.value;let r=e(n);return t.value=r,r}},us=(e,t)=>{if(e.kind===`Identifier`&&W(e))return!t.has(e.name);if(W(e))return!0;let n={value:!1};return ki(e,(e,r)=>(n.value||=us(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},ds=(e,t)=>xi.has(e)&&cs(e)&&Ws(e,t)!==void 0&&!us(e,as),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Vs(e,t),i=cs(e)&&rs(e,t)?ls(r):ds(e,t)?gs(r):r;return t.evaluators.set(e,i),i},fs=/* @__PURE__ */ new WeakMap,ps=(e,t)=>{let n=fs.get(e);if(n)return n;let r=(cs(e)&&rs(e,t)?void 0:Mr(e,t))??Y(e,t);return fs.set(e,r),r},X=(e,t)=>ps(e,t.environment)(t),ms=(e,t)=>{let n=e.name,r=W(e)?void 0:Lo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw Rs(n,e);return i}}if(r?.kind===`Value`){let e=r.value;return()=>e}if(r?.kind===`Definition`&&r.definition.kind===`Operator`){let e=r.definition;if(e.params.length===0&&uo(e,t))return t=>Co(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=ps(e.body,t),n.body(oo(r,void 0)));return hs(e,t)?gs(r):r}return t=>Co(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>Co(e,t);return uo(e,t)||!hs(e,t)?n:gs(n)}return t=>Wo(n,e,t)},hs=(e,t)=>Ws(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ws(e.set,t)!==void 0)),gs=e=>{let t={recent:void 0,recentValue:void 0,previous:void 0,previousValue:void 0};return n=>{let r=n.state;if(r!==void 0&&t.recent===r&&t.recentValue!==void 0)return t.recentValue;if(r!==void 0&&t.previous===r&&t.previousValue!==void 0)return t.previousValue;let i=e(n);return t.previous=t.recent,t.previousValue=t.recentValue,t.recent=r,t.recentValue=i,i}},_s=(e,t)=>{let n=Do(e,t);return n!==void 0&&jo(n)>0},vs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!W(e)&&_s(e.name,t)),ys=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),bs=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(ys(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&vs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&vs(t[0],n)&&vs(t[1],n);if(i&&a){let e=Y(t[0],n),r=Y(t[1],n);return t=>i(e(t),r(t))}return n=>e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n)},xs=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`)};let r=e.params,i=t.map((e,t)=>r[t].arity>0?void 0:Bo(e,n));return n=>{let a=e.scope;for(let e=0;e<t.length;e++){let o=i[e],s=o?o(n):{kind:`Operator`,operator:Mo(t[e],n)};a={name:r[e].name,binding:s,parent:a}}return a}},Ss=/* @__PURE__ */ new WeakMap,Cs=e=>{let t=Ss.get(e);if(t)return t;let n={calls:/* @__PURE__ */ new Map,size:0};return Ss.set(e,n),n},ws=(e,t)=>{let n=[];for(let r=e;n.length<t;r=r?.parent){if(r?.binding.kind!==`Value`)return;n.push(r.binding.value)}return n},Ts=(e,t)=>e.every((e,n)=>Be(e,t[n])),Es=(e,t)=>{let n=co(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&uo(n,t)&&!Qo(n,t)},Ds=(e,t,n,r)=>{let i=xs(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=ps(e.body,n),a.body({environment:n,scope:t,state:r.state,next:r.next}));if(!r)return e=>o(i(e),e);let s=Cs(e);return e=>{let n=i(e),r=ws(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=vn(a,Cn(e));let c=s.calls.get(a),l=c?.find(e=>Ts(e.args,r));if(l)return l.value;let u=o(n,e);return s.size<65536&&(s.size++,c?c.push({args:r,value:u}):s.calls.set(a,[{args:r,value:u}])),u}},Os=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!so(e(t),`the left side of =>`)||so(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new u(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=W(e)?void 0:Do(e.operator,t);if(r?.kind===`Closure`)return Ds(r,n,t,Es(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=$a(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return Si.has(e)||lo(n[0],t)?i:e=>e.scope===void 0?r.evaluate([i(e)]):i(e)}if(r?.kind===`Builtin`&&r.name===`\\subseteq`&&n.length===2){let i=Y(n[0],t),a=Y(n[1],t),o=eo(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?bs(r,n,t):r=>{let i=Ao(e.operator,r);if(i)return Ho(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new u(`${e.operator} is a function; apply it with brackets`):new u(`Unknown operator ${e.operator} at ${Ga(e)}`)}},ks=(e,t)=>{let n=e.kind===`Forall`,r=n?`the body of \\A`:`the body of \\E`,i=Y(e.body,t),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name,o=Y(a.set,t);return t=>{let s=po(o(t),a.set);for(let a=0;a<s.length;a++){let o=so(i(oo(t,io(t.scope,e,s[a]))),r);if(o!==n)return o}return n}}let o=e.bounds;if(o.every(e=>e.pattern.kind===`Name`)){let e=o.map(e=>e.pattern.kind===`Name`?e.pattern.name:``),a=o.map(e=>Y(e.set,t));return t=>{let s=a.map((e,n)=>po(e(t),o[n].set)),c=(a,o)=>{if(a<0)return so(i(oo(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,io(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>mo(o,e,t=>so(i(oo(e,t)),r)!==n)?!n:n},As=(e,t)=>{if(e.kind===`Tuple`)return R(e.names.map(e=>{let n=ro(t,e);return n?.kind===`Value`&&n.value}));let n=ro(t,e.name);return n?.kind===`Value`?n.value:void 0},js=/* @__PURE__ */ new WeakSet,Ms=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&js.add(e)},Ns=(e,t)=>{let n=js.has(e),r=Y(e.body,t),i=e.bounds,a=i[0],o=i.length===1&&a.pattern.kind===`Name`,s=Y(a.set,t);return t=>{let c=o?s(t):ho(i,t),l=typeof c==`object`&&c.kind===`ProductSet`&&i.length>1?c.factors:[c];if(e.isRecursive||n||!l.every(pe)){let n=bo(c,e=>r(oo(t,go(i,t.scope,e))),yo(i,e.body,c));return i.length===1?nn(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=Dt(c);return nn(Le(n,n.map(n=>r(oo(t,io(t.scope,e,n))))))}let u=[];return mo(i,t,e=>{let n=i.length===1?As(a.pattern,e)??!1:R(i.map(t=>As(t.pattern,e)??!1));return u.push([n,r(oo(t,e))]),!1}),yt(u)}},Ps=(e,t)=>{let n=js.has(e);n&&Ms(e.function);let r=Y(e.function,t),i=e.updates.map(e=>({path:e.path.map(e=>e.kind===`Field`?{kind:`Field`,name:e.name,args:[]}:{kind:`Index`,name:``,args:e.args.map(e=>Y(e,t))}),value:Y(e.value,t)})),a=(e,t)=>e.kind===`Field`?e.name:e.args.length===1?e.args[0](t):R(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(oo(i,io(i.scope,`@`,e))):hn(e,a(t[r],i),e=>o(e,t,r+1,i,s),n&&r===0);return e=>{let t=r(e);for(let n of i)t=o(t,n.path,0,e,n.value);return t}},Fs=(e,t)=>{if(e.kind===`SetFilter`){let n=Y(e.predicate,t),r=Y(e.bound.set,t),i=e.bound.pattern;return t=>{let a=r(t),o=e=>so(n(oo(t,ao(t.scope,i,e))),`a set filter predicate`);if(!pe(a)){if(A(a))return{kind:`FilteredSet`,base:a,test:o};throw new u(`TLC encountered a non-enumerable quantifier bound ${U(a)} at ${Ga(e.bound.set)}`)}return Ie(B(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return mo(e.bounds,t,e=>(r.push(n(oo(t,e))),!1)),mt(r)}}let n=e.set;if(!n)return()=>{throw new u(`TLC cannot evaluate an unbounded CHOOSE at ${Ga(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(mo([i],t,i=>so(r(oo(t,i)),`the body of CHOOSE`)?(n.value=As(e.pattern,i),!0):!1),n.value===void 0)throw new u(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${qa(e)}`);return n.value}},Is=e=>[...e].sort((e,t)=>L(e.name,t.name)),Ls=e=>e.some((t,n)=>n>0&&e[n-1].name===t.name),Rs=(e,t)=>new u(`In evaluation, the identifier ${e} is either undefined or not an operator.\n${qa(t)}`),zs=e=>new u(`A primed expression appears outside an action at ${Ga(e)}`),Bs=(e,t)=>{let n=Is(e),r=Ls(n),i=n.map(e=>Y(e.value,t));if(r)return e=>yt(n.map((t,n)=>[t.name,i[n](e)]));let a=n.map(e=>e.name),o=e.map(e=>e.name);return o.some((e,t)=>e!==a[t])?e=>ye(a,i.map(t=>t(e)),o):e=>Le(a,i.map(t=>t(e)))},Vs=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Go(t)?()=>t:()=>{throw no(e)}}case`Decimal`:return()=>{throw to(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return ms(e,t);case`Apply`:return Os(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new u(`An operator cannot be used as a value at ${Ga(e)}`)};case`And`:{let t=e.operands.map(n);return n=>{for(let r=0;r<t.length;r++){let i=t[r](n);if(typeof i!=`boolean`)throw Ya(e,r,i);if(!i)return!1}return!0}}case`Or`:{let t=e.operands.map(n);return e=>{for(let n=0;n<t.length;n++)if(so(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw zs(e);return t({environment:n.environment,scope:n.scope,state:n.next,next:void 0})}}case`Unchanged`:{let t=n({kind:`Prime`,expression:e.expression,location:e.location}),r=n(e.expression);return e=>Re(t(e),r(e))}case`If`:{let t=n(e.condition),r=n(e.consequent),i=n(e.alternative);return n=>{let a=t(n);if(typeof a!=`boolean`)throw Ja(e,a);return a?r(n):i(n)}}case`Case`:{let t=e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),r=e.other?n(e.other):void 0;return n=>{for(let e of t)if(so(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new u(`Attempted to evaluate a CASE with no conditions true.\n${qa(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(oo(n,Uo(e.definitions,n)))}case`Forall`:case`Exists`:return ks(e,t);case`UnboundedQuantifier`:return()=>{throw new u(`TLC cannot evaluate an unbounded quantifier at ${Ga(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return Fs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>mt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>R(t.map(t=>t(e)))}case`FunctionConstruction`:return Ns(e,t);case`FunctionSet`:{let t=n(e.domain),r=n(e.range);return e=>({kind:`FunctionSet`,domain:t(e),range:r(e)})}case`Record`:return Bs(e.fields,t);case`RecordSet`:{let t=Is(e.fields),r=t.map(e=>e.name),i=t.map(e=>n(e.value));return e=>({kind:`RecordSet`,names:r,sets:i.map(t=>t(e))})}case`Except`:return Ps(e,t);case`ExceptAt`:return t=>{let n=ro(t.scope,`@`);if(n?.kind!==`Value`)throw new u(`@ appears outside an EXCEPT at ${Ga(e)}`);return n.value};case`FunctionApplication`:{Ms(e.function);let t=n(e.function),r=e.args.map(n),i=Za(e);if(r.length===1){let e=r[0];return n=>fn(t(n),e(n),void 0,i)}return e=>fn(t(e),R(r.map(t=>t(e))),void 0,i)}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Xa(e),a=Qa(e);return e=>fn(t(e),r,i,a)}case`Product`:{let t=e.factors.map(n);return e=>({kind:`ProductSet`,factors:t.map(t=>t(e))})}case`BoxAction`:case`AngleAction`:{let t=n(e.action),r=n({kind:`Unchanged`,expression:e.subscript,location:e.location}),i=e.kind===`BoxAction`;return e=>i?so(t(e),`an action`)||r(e):so(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>Ba(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new u(`The subexpression reference at ${Ga(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new u(`TLC cannot evaluate a temporal formula in a state at ${Ga(e)}`)}}},Hs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),Us=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ws=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Hs.has(e.kind))return!1;let a=Us(e),o=(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`;if(a!==void 0&&!o&&!t.constants.has(a)){let e=t.substitutions.get(a)??a,o=t.definitions.get(e);if(!o&&qo(e,t))return!1;let s=t.variableIndex.get(a);if(s!==void 0&&n.add(s),o&&!r.has(o)&&(r.add(o),!i(o.body)||o.kind!==`Operator`&&!o.bounds.every(e=>i(e.set))))return!1}return Un(e).every(i)};return i(e)?[...n]:void 0},Gs=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
24
24
  (***************************************************************************)
25
25
  (* This module provides dummy definitions of the operators that are *)
26
26
  (* defined by the real Reals module. It is expected that any tool that *)
package/dist/transfer.js CHANGED
@@ -20,7 +20,7 @@ The exception was a java.lang.RuntimeException
20
20
  `).length-1,n.index+=i.length;continue}if(r.startsWith(`\\*`)){let e=r.indexOf(`
21
21
  `);n.index+=e===-1?r.length:e;continue}if(r.startsWith(`(*`)){let e=r.indexOf(`*)`);if(e===-1)throw new l(`Unterminated comment at line ${n.line}`);n.line+=r.slice(0,e).split(`
22
22
  `).length-1,n.index+=e+2;continue}let a=/^\d+(?![A-Za-z_])/.exec(r)?.[0];if(a){t.push({kind:`Number`,text:a,line:n.line}),n.index+=a.length;continue}let o=/^[A-Za-z0-9_!]+/.exec(r)?.[0];if(o){t.push({kind:`Word`,text:o,line:n.line}),n.index+=o.length;continue}let s=/^"((?:[^"\\]|\\.)*)"/.exec(r);if(s){t.push({kind:`String`,text:s[1],line:n.line}),n.index+=s[0].length;continue}let c=E.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let u=ie(e,n.index);if(!u)throw new l(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:u.text,line:n.line}),n.index+=u.length}return t.push({kind:`End`,text:``,line:n.line}),t},ce=e=>{let t=D(e),n={index:0},r={init:void 0,next:void 0,specification:void 0,invariants:[],properties:[],constraints:[],actionConstraints:[],constants:[],substitutions:[],checkDeadlock:!0,symmetry:void 0,view:void 0,alias:void 0,postconditions:[],possible:[],periodic:void 0,rlReward:void 0},i={isSet:!1},a=()=>t[n.index],o=()=>t[n.index++],s=e=>e.kind===`Word`&&T.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new l(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},u=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new l(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},d=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;d();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new l(`The keyword ${t} appears twice`);r[e]=u()},m=()=>{let e=o();if(e.kind===`Number`)return{kind:`Number`,value:Number(e.text)};if(e.kind===`String`)return{kind:`String`,value:e.text};if(e.text===`TRUE`||e.text===`FALSE`)return{kind:`Boolean`,value:e.text===`TRUE`};if(e.text===`{`){let e=[];for(a().text!==`}`&&e.push(m());a().text===`,`;)o(),e.push(m());return c(`}`),{kind:`Set`,elements:e}}if(e.kind===`Word`&&!s(e))return{kind:`ModelValue`,name:e.text};throw new l(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=u();return c(`]`),e},g=()=>{for(;d();){let e=o().text;if(a().text===`(`){o();let t=[];for(;a().text!==`)`;)t.push(m()),a().text!==`)`&&c(`,`);o(),c(`=`);let n=h();r.constants.push({name:e,value:m(),args:t,...n&&{module:n}});continue}if(a().text===`<-`){o();let t=h();r.substitutions.push({name:e,target:u(),...t&&{module:t}});continue}c(`=`);let t=h();r.constants.push({name:e,value:m(),...t&&{module:t}})}};for(;a().kind!==`End`;){let e=o();switch(e.text){case`CONSTANT`:case`CONSTANTS`:g();break;case`INIT`:p(`init`,e.text);break;case`NEXT`:p(`next`,e.text);break;case`SPECIFICATION`:p(`specification`,e.text);break;case`SYMMETRY`:p(`symmetry`,e.text);break;case`VIEW`:p(`view`,e.text);break;case`INVARIANT`:case`INVARIANTS`:r.invariants.push(...f());break;case`PROPERTY`:case`PROPERTIES`:r.properties.push(...f());break;case`CONSTRAINT`:case`CONSTRAINTS`:r.constraints.push(...f());break;case`ACTION_CONSTRAINT`:case`ACTION_CONSTRAINTS`:r.actionConstraints.push(...f());break;case`ALIAS`:p(`alias`,e.text);break;case`POSTCONDITION`:case`POSTCONDITIONS`:r.postconditions.push(...f());break;case`_POSSIBLE`:r.possible.push(...f());break;case`_PERIODIC`:p(`periodic`,e.text);break;case`_RL_REWARD`:p(`rlReward`,e.text);break;case`CHECK_DEADLOCK`:{if(i.isSet)throw new l(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new l(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new l(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new l(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},O={Set:{open:`{ `,close:` }`,pad:` `,separator:`,`,divider:``,dividerPad:``},Sequence:{open:`<< `,close:` >>`,pad:` `,separator:`,`,divider:``,dividerPad:``},Subset:{open:`SUBSET `,close:` `,pad:` `,separator:`,`,divider:``,dividerPad:``},Function:{open:`( `,close:` )`,pad:` `,separator:` @@`,divider:` :>`,dividerPad:` `},Record:{open:`[ `,close:` ]`,pad:` `,separator:`,`,divider:` |->`,dividerPad:` `}},k=e=>{if(e===void 0)return!1;let t=e.charCodeAt(0);return t>=9&&t<=13||t>=28&&t<=32||t===5760||t>=8192&&t<=8202&&t!==8199||t===8232||t===8233||t===8287||t===12288},le=e=>e!==void 0&&/^\p{Nd}$/u.test(e),ue=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),de=e=>{let t=t=>{let n=oe(e,t,k);return e[n]===void 0?void 0:n},n=(n,r,i,a,o,s)=>{let c={kind:n,first:r,last:r,children:[]},l={index:t(i)};for(;l.index!==void 0&&e[l.index]!==a;){let n=o(l.index);if(!n)return;c.children.push(n);let r=t(n.last+1);if(r===void 0)return;let i=e.startsWith(s,r)?r+s.length:r;l.index=t(i)}if(l.index!==void 0)return c.last=l.index,c},r=(n,r)=>{let i=o(n);if(!i)return;let a=t(i.last+1);if(a===void 0||!e.startsWith(r,a))return;let s=t(a+r.length),c=s===void 0?void 0:o(s);if(c)return{kind:`Pair`,first:n,last:c.last,children:[i,c]}},i=t=>{let n=oe(e,t,le);if(e.startsWith(`..`,n)&&le(e[n+2]))return{kind:`Constant`,first:t,last:oe(e,n+2,le)-1,children:[]}},a=t=>{let n=e[t];if(n===`"`){let n=e.indexOf(`"`,t+1);return n===-1?void 0:{kind:`Constant`,first:t,last:n,children:[]}}let r=le(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:oe(e,t+1,le)-1,children:[]};if(ue(n))return{kind:`Constant`,first:t,last:oe(e,t,ue)-1,children:[]}},o=i=>{let s=t(i);if(s!==void 0)switch(e[s]){case`{`:return n(`Set`,s,s+1,`}`,o,`,`);case`<`:{let t=n(`Sequence`,s,s+2,`>`,o,`,`);return!t||t.last+1>=e.length?void 0:{...t,last:t.last+1}}case`[`:return n(`Record`,s,s+1,`]`,e=>r(e,`|->`),`,`);case`(`:return n(`Function`,s,s+1,`)`,e=>r(e,`:>`),`@@`);default:{if(!e.startsWith(`SUBSET`,s))return a(s);let n=t(s+6),r=n===void 0?void 0:o(n);return r?{kind:`Subset`,first:s,last:r.last,children:[r]}:void 0}}};return o(0)},fe=e=>{let t=e.length===0?void 0:de(e);if(!t||t.last<e.length-1)return e;let n=t=>e.slice(t.first,t.last+1),r=e=>e.last-e.first+1,i=(e,t,i,s)=>{if(r(e)<=t-i||e.kind===`Constant`)return n(e);if(e.kind===`Pair`)return;let c=O[e.kind],l=t-Math.max(c.open.length,c.pad.length),u=i+c.close.length,d=s+c.pad,f=e.kind===`Function`||e.kind===`Record`?o(e.children,l,u,d,c):a(e.children,l,u,d,c.separator);return f===void 0?void 0:c.open+f+c.close},a=(e,t,n,r,a)=>{let o=e.map((o,s)=>{let c=s===e.length-1,l=i(o,t,c?n:a.length,r);return l===void 0||c?l:`${l}${a}\n${r}`});return o.includes(void 0)?void 0:o.join(``)},o=(e,t,a,o,s)=>{let c=e.map((c,l)=>{let u=l===e.length-1,d=c.children[0],f=c.children[1],p=u?``:`${s.separator}\n${o}`,m=r(d)+s.divider.length+1+r(f);if(u?m<=t-a:m+s.separator.length<=t)return`${n(d)}${s.divider} ${n(f)}${p}`;let h=r(d)+s.divider.length<=t?n(d):i(d,t,a+s.divider.length,o),g=i(f,t-s.dividerPad.length,u?a:s.separator.length,o+s.dividerPad);return h===void 0||g===void 0?void 0:`${h}${s.divider}\n${o}${s.dividerPad}${g}${p}`});return c.includes(void 0)?void 0:c.join(``)};return i(t,80,0,``)??e},A=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,pe=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`||e.kind===`Interval`),j=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),M=e=>typeof e==`object`&&e.kind===`ModelValue`,me=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:A(e)?`set`:`function`,he=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new d(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},ge={isUsed:!1},_e=(e,t)=>(ge.isUsed=!0,e.writtenKeys=t,e),ve=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Yt(e.domain,t)]]),ye=(e,t,n)=>_e(Le(e,t),n),be=e=>{if(!(typeof e!=`object`||e.kind!==`Function`&&e.kind!==`Set`)&&e.hash===0&&e.fingerprint===0){if(e.kind===`Set`)return e.elements.forEach(be);e.writtenKeys=void 0,e.range.forEach(be)}},xe=e=>e.every(e=>typeof e==`string`),Se=(e,t)=>e.length>0&&t.length>0&&xe(e)!==xe(t),Ce=(e,t)=>{ge.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Se(n,r)?rt(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=L(n[e],r[e]);if(t!==0)return t}let i=e.range,a=t.range;if(i===a)return 0;for(let e=0;e<i.length;e++){let t=L(i[e],a[e]);if(t!==0)return t}return 0},N=(e,t)=>{let n=e.elements,r=t.elements;if(n.length!==r.length)return n.length-r.length;if(n===r)return 0;for(let e=0;e<n.length;e++){let t=L(n[e],r[e]);if(t!==0)return t}return 0},we=e=>M(e)?e.type:void 0,Te=(e,t,n)=>{let r=we(e),i=we(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(M(e)&&M(t)){if(r===void 0||i===void 0||r===i)return;throw new u(`Attempted to ${a} the differently-typed model values ${U(e)} and ${U(t)}`)}throw r===void 0?new u(`Attempted to ${a} the typed model value ${U(t)} and the non-model value\n${U(e)}`):new u(`Attempted to ${a}${n?``:` the`} typed model value ${U(e)} and non-model value\n${U(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0,Pe.clear();for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,P=e=>Ee.ranks[Oe(e)],Ae=(e,t)=>{if(e===t)return 0;let n=Ee.ranks,r=n[e],i=n[t];return r!==void 0&&i!==void 0?r-i:r===void 0?i===void 0&&e<t?-1:1:-1},F=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),je=(e,t)=>(F(e)||F(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Me=(e,t,n)=>{if(!F(e))throw new u(`Attempted to ${n} the set ${U(z(e)?B(e):e,!0)} with the value:\n${U(t,!0)}`);if(!F(t))throw new u(`Attempted to compare overridden value ${U(e,!0)} with non-overridden value:\n${U(t,!0)}`);let r=e.kind===`InfiniteSet`?e.name:void 0,i=t.kind===`InfiniteSet`?t.name:void 0;if(r!==void 0&&r===i)return 0;if(r===`Nat`&&i===`Int`||r===`Int`&&i===`Nat`)return r===`Nat`?-1:1;throw new u(`Attempted to compare ${U(e,!0)} with the value\n${U(t,!0)}`)},I={kind:`InfiniteSet`,name:`Any`},Ne=e=>{throw new u(`Attempted to compare ANY with the value\n${U(e)}`)},L=(e,t)=>{if(e===t)return ge.isUsed&&be(e),e===I?Ne(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return Ce(e,t);if(n===`Set`&&r===`Set`)return N(e,t);if(n===`Interval`&&r===`Interval`){let n=Math.max(0,e.high-e.low+1),r=Math.max(0,t.high-t.low+1);return n!==r||n===0?n-r:e.low-t.low}if(n===`ModelValue`&&r===`ModelValue`)return e.type!==void 0&&t.type!==void 0&&Te(e,t,!1),e.rank-t.rank;if(n===`ModelValue`||r===`ModelValue`)return Te(e,t,!1),n===`ModelValue`?-1:1;if(e===I||t===I)return Ne(e===I?t:e);if(A(e)&&A(t)&&je(e,t))return Me(e,t,`compare`);let i=A(e);if(i!==A(t))throw new u(`Attempted to compare ${me(e)} ${U(e)} with ${me(t)} ${U(t)}`);return i?N(B(e),Ht(t)):Ce(H(e),H(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return Ae(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(Te(e,t,!1),M(e))return-1;if(M(t))return 1;throw new u(`Attempted to compare ${me(e)} ${U(e)} with ${me(t)} ${U(t)}`)},Pe=/* @__PURE__ */ new Map,Fe=e=>{let t=Pe.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0,rank:P(e)};return Pe.set(e,n),n},Ie=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),Le=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Re=(e,t)=>{if(e===t)return ge.isUsed&&be(e),e!==I||Ne(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&Te(e,t,!0),L(e,t)===0);if((e.kind===`Set`||e.kind===`Function`)&&(t.kind===`Set`||t.kind===`Function`)&&e.hash!==0&&t.hash!==0&&e.hash!==t.hash)return!1;if(e.kind===`Function`&&t.kind===`Function`)return Ce(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return N(e,t)===0;if(M(e)||M(t))return Te(e,t,!0),M(e)&&M(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Re(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=V(e.domain),r=V(t.domain);if(n||r)return n===r;let i=V(e.range),a=V(t.range);return i||a?i===a:Re(e.domain,t.domain)&&Re(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(V(e))return V(t);if(V(t))return!1;let n=e=>e.names.map((t,n)=>[t,e.sets[n]]).sort((e,t)=>e[0]<t[0]?-1:1),r=n(t);return e.names.length===t.names.length&&n(e).every((e,t)=>e[0]===r[t][0]&&Re(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return V(e)?V(t):!V(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Re(e,t.factors[n]));let n=A(e)&&A(t)&&!(z(e)&&z(t));if(n&&je(e,t))return Me(e,t,`check equality of`)===0;if(n){if(!F(e)){let n=B(e);if(!F(t))return N(n,B(t))===0}return Mn(e)===Mn(t)}return L(e,t)===0},ze=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>Be(e,t[n])),Be=(e,t)=>e===t?!0:typeof e!=`object`||typeof t!=`object`?!1:e.kind===`Set`&&t.kind===`Set`?(e.hash===0||t.hash===0||e.hash===t.hash)&&ze(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&ze(e.domain,t.domain)&&ze(e.range,t.range):Mn(e)===Mn(t),Ve=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},He=e=>{if(!j(e))return;let t=H(e);return{domain:t.domain,range:t.range,low:Ve(t.domain)}},Ue=e=>{if(!j(e))return;let t=H(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},We=(e,t)=>{if(we(e)!==void 0)throw new u(`Attempted to compare the typed model value ${U(e)} and the non-model value\n${U(t)}`);return 1},Ge=(e,t)=>{if(we(e)!==void 0)throw new u(`Attempted to check equality of the typed model value ${U(e)} and the non-model value\n${U(t)}`);return!1},Ke=(e,t)=>{let n=we(e);if(n===void 0)return M(t)?e.rank-t.rank:-1;if(!M(t))throw new u(`Attempted to compare the typed model value ${U(e)} and non-model value\n${U(t)}`);let r=we(t);if(r!==void 0&&r!==n)throw new u(`Attempted to compare the differently-typed model values ${U(e)} and ${U(t)}`);return e.rank-t.rank},qe=(e,t)=>{let n=we(e);if(n===void 0)return M(t)&&e.name===t.name;if(!M(t))throw new u(`Attempted to check equality of typed model value ${U(e)} and non-model value\n${U(t)}`);let r=we(t);if(r!==void 0&&r!==n)throw new u(`Attempted to check equality of the differently-typed model values ${U(e)} and ${U(t)}`);return e.name===t.name},Je={number:{name:`integer`,other:`non-integer`,equality:`check equality of`},string:{name:`string`,other:`non-string`,equality:`check equality of`},boolean:{name:`boolean`,other:`non-boolean`,equality:`compare equality of`}},Ye=(e,t,n)=>{let r=Je[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${U(e)} with ${r.other}:\n${U(t)}`},Xe=(e,t,n)=>{let r=He(n);if(!r){if(M(n))return We(n,t);throw new u(`Attempted to compare the function ${U(t)} with the value:\n${U(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>Ze(e.range,r.range);if(e.low!==void 0&&e.range.length===0)return 0;if(e.low!==void 0&&r.low!==void 0)return e.low===r.low?i():e.low-r.low;let a=e.low??r.low;if(a===void 0)return Ze(e.domain,r.domain)||i();let o=e.low===void 0?e.domain:r.domain;for(let t=0;t<o.length;t++){let n=o[t];if(typeof n!=`number`)throw new u(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${U(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},Ze=(e,t)=>{for(let n=0;n<e.length;n++){let r=rt(e[n],t[n]);if(r!==0)return r}return 0},Qe=(e,t)=>{let n=H(e);if(e.kind===`Function`&&Xt(n)!==void 0){let r=j(t)?Xt(t):void 0;return r?n.range.length-r.length||Ze(n.range,r):Xe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ue(t);if(!r){if(M(t))return We(t,e);throw new u(`Attempted to compare record:\n${U(e)}\nwith non-record\n${U(t)}`)}return n.domain.length-r.domain.length||Ze(n.domain,r.domain)||Ze(n.range,r.range)}return Xe({...n,low:Ve(n.domain)},e,t)},$e=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),et=(e,t)=>F(e)||F(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),tt=(e,t)=>{let n=$e(t)?t:B(t);return{elements:B(e).elements,otherElements:B(n).elements}},nt=(e,t)=>{if(et(e,t))return L(e,t);if(!$e(e))return nt(B(e),t);if(!A(t)){if(M(t))return We(t,e);throw new u(`Attempted to compare the set ${U(e)} with the value:\n${U(t)}`)}let n=tt(e,t);return n.elements.length-n.otherElements.length||Ze(n.elements,n.otherElements)},rt=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return L(e,t);if(M(t))return We(t,e);throw new u(Ye(e,t,!1))}return e.kind===`ModelValue`?Ke(e,t):j(e)?Qe(e,t):nt(e,t)},it=(e,t)=>e.every((e,n)=>ct(e,t[n])),at=(e,t,n)=>{let r=He(n);if(!r){if(M(n))return Ge(n,t);throw new u(`Attempted to check equality of the function ${U(t)} with the value:\n${U(n)}`)}if(e.range.length!==r.range.length)return!1;if(e.low!==void 0&&r.low!==void 0)return(e.range.length===0||e.low===r.low)&&it(e.range,r.range);let i=e.low??r.low;return i===void 0?it(e.domain,r.domain)&&it(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new u(`Attempted to compare an integer with non-integer:\n${U(e)}.`);return e===i+t})&&it(e.range,r.range)},ot=(e,t)=>{let n=H(e);if(e.kind===`Function`&&Xt(n)!==void 0){let r=j(t)?Xt(t):void 0;return r?n.range.length===r.length&&it(n.range,r):at({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ue(t);if(!r){if(M(t))return Ge(t,e);throw new u(`Attempted to check equality of record:\n${U(e)}\nwith non-record\n${U(t)}`)}return n.domain.length===r.domain.length&&it(n.domain,r.domain)&&it(n.range,r.range)}return at({...n,low:Ve(n.domain)},e,t)},st=(e,t)=>{if(et(e,t))return Re(e,t);if(!$e(e))return st(B(e),t);if(!A(t)){if(M(t))return Ge(t,e);throw new u(`Attempted to check equality of the set ${U(e)} with the value:\n${U(t)}`)}let n=tt(e,t);return n.elements.length===n.otherElements.length&&it(n.elements,n.otherElements)},ct=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(M(t))return Ge(t,e);throw new u(Ye(e,t,!0))}return e.kind===`ModelValue`?qe(e,t):j(e)?ot(e,t):st(e,t)},lt=e=>{let t=[];for(let n of e){let e={low:0,high:t.length,comparison:1};for(;e.low<e.high;){let r=e.low+(e.high-e.low>>1);if(e.comparison=rt(n,t[r]),e.comparison===0)break;e.comparison<0?e.high=r:e.low=r+1}e.comparison!==0&&t.splice(e.low,0,n)}return t},ut=(e,t)=>t.some(t=>ct(t,e)),dt=(e,t)=>{try{return Re(e,t)}catch(n){if(!(n instanceof u))throw n;return ct(e,t)}},ft=e=>{for(let t=1;t<e.length;t++)if(L(e[t],e[t-1])<=0)return!1;return!0},pt=e=>{if(e.length<2||ft(e))return Ie(e);let t=[...e].sort(L),n=[t[0]];for(let e=1;e<t.length;e++)L(n[n.length-1],t[e])!==0&&n.push(t[e]);return Ie(n)},mt=e=>{try{return pt(e)}catch(t){if(!(t instanceof u))throw t;return{kind:`MixedSet`,elements:e}}},ht=Ie([]),gt=(e,t)=>{if(e.elements.length===0||t.elements.length===0)return e;let n=e.elements,r=t.elements,i=[],a=0;for(let e=0;e<n.length;e++){let t=n[e];for(;a<r.length&&L(r[a],t)<0;)a++;a<r.length&&L(r[a],t)===0||i.push(t)}return i.length===n.length?e:Ie(i)},_t=(e,t)=>{let n=e.elements,r=t.elements,i=[],a=0;for(let e=0;e<n.length;e++){let t=n[e];for(;a<r.length&&L(r[a],t)<0;)a++;a<r.length&&L(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Ie(i)},vt=(e,t)=>{if(e.elements.length===0)return t;if(t.elements.length===0)return e;let n=e.elements,r=t.elements,i=[],a=[],o=0,s=0;for(;o<n.length&&s<r.length;){let e=L(n[o],r[s]);e<0?i.push(n[o++]):e>0?(a.push(r[s]),i.push(r[s++])):(i.push(n[o++]),s++)}for(;o<n.length;)i.push(n[o++]);for(;s<r.length;)a.push(r[s]),i.push(r[s++]);if(a.length===0)return e;let c=Ie(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+bn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+Dn(a[e])|0;c.fingerprint=t}return c},yt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>L(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&L(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return Le(n,r)},bt=e=>{let t=yt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:_e(t,e.map(e=>e[0]))},xt=[],St=[],Ct=/* @__PURE__ */ new WeakMap,wt=e=>{let t=xt[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return xt[e]=n,n},Tt=e=>{if(Kt(e),e.low===1&&e.high>=1)return wt(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Et=e=>e.length>0&&(e===xt[e.length]||Ct.has(e)),Dt=e=>{let t=B(e).elements;return Et(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},Ot=e=>{let t=St[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return St[e]=n,n},R=e=>Le(wt(e.length),e),kt=e=>yt(e),At=e=>A(e)&&!F(e),jt=(e,t=!1)=>`UNION(${U(e.base,t)})`,Mt=(e,t=e=>B(e).elements)=>{if(!At(e.base))throw new u(`Attempted to enumerate the nonenumerable set:\n${jt(e)}`);return t(e.base)},Nt=(e,t,n)=>{if(At(e))return e;throw new u(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${U(e)}\nwhen enumerating:\n${jt(n)}`)},z=e=>{if(typeof e!=`object`)return!1;switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:return!0;case`InfiniteSet`:case`SequenceSet`:return!1;case`Subset`:case`FilteredSet`:return z(e.base);case`FunctionSet`:return z(e.domain)&&z(e.range)||Pt(e.domain)===!0||Pt(e)===!0;case`RecordSet`:return e.sets.every(z)||Pt(e)===!0;case`ProductSet`:return e.factors.every(z)||Pt(e)===!0;case`Union`:return z(e.base)&&Ut(e.base).every(z);case`SetOperation`:return e.operator===`\\cup`?z(e.left)&&z(e.right):e.operator===`\\cap`?z(e.left)||z(e.right):z(e.left);default:return!1}},Pt=e=>{if(typeof e==`object`)switch(e.kind){case`Set`:case`MixedSet`:return e.elements.length===0;case`Interval`:return e.low>e.high;case`InfiniteSet`:return e.name===`Any`&&void 0;case`Subset`:case`SequenceSet`:return!1;case`FunctionSet`:{let t=Pt(e.domain);return t===void 0?void 0:!t&&Pt(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=Pt(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return z(e)?B(e).elements.length===0:void 0;default:return}},Ft=e=>{let t=e.reduce((e,t)=>e*t.length,1),n=[],r=e.map(()=>0);for(let i=0;i<t;i++){n.push(e.map((e,t)=>e[r[t]]));for(let t=e.length-1;t>=0&&(r[t]++,!(r[t]<e[t].length));t--)r[t]=0}return n},It=(e,t)=>{for(let n=e.length-1;n>=0;n--)if(e[n]<t-e.length+n){e[n]++;for(let t=n+1;t<e.length;t++)e[t]=e[t-1]+1;return!0}return!1},Lt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Ie(r.map(t=>e[t])));while(It(r,e.length))}return t},Rt=e=>{if(typeof e!=`object`)throw new u(`Expected a set but found ${U(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return lt(e.elements);case`Interval`:{let t=Tt(e);return t!==xt[t.length]&&Ct.set(t,e),t}case`Subset`:{F(e.base)&&Kt(e.base);let t=B(e.base).elements;if(t.length>30)throw new u(Gt(e));return Lt(t)}case`FunctionSet`:{if(V(e))return[];if(F(e.domain))throw new u(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${U(e.domain)}\ncannot be enumerated.`);let t=Dt(e.domain);if(t.length>0&&F(e.range))throw new u(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${U(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:B(e.range).elements;return Ft(t.map(()=>n)).map(e=>Le(t,e))}case`RecordSet`:{if(V(e))return[];let t=e.sets.map((t,n)=>{if(F(t))throw new u(`Attempted to enumerate a set of the form [l1 : v1, ..., ln : vn],\nbut can't enumerate the value of the \`${e.names[n]}' field:\n${U(t)}`);return B(t).elements});return Ft(t).map(t=>kt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(V(e))return[];let t=e.factors.map((e,t)=>{if(F(e))throw new u(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${U(e)}`);return B(e).elements});return Ft(t).map(R)}case`FilteredSet`:if(F(e.base))throw new u(`Attempted to enumerate { x \\in S : p(x) } when S:\n${U(e.base)}\nis not enumerable`);return B(e.base).elements.filter(e.test);case`Union`:{let t=Mt(e).flatMap((t,n)=>B(Nt(t,n,e)).elements);return B(mt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(F(e.left)||F(e.right)))throw new u(`Attempted to enumerate S \\cup T when S:\n${U(e.left)}\nand T:\n${U(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&F(e.left)&&F(e.right))throw new u(`Attempted to enumerate S \\cap T when neither S:\n${U(e.left)}\nnor T:\n${U(e.right)}\nis enumerable`);if(e.operator===`\\`&&F(e.left))throw new u(`Attempted to enumerate S \\ T when S:\n${U(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return vt(B(e.left),B(e.right)).elements;let t=e.operator===`\\`||z(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return B(n).elements.filter(t=>$t(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new u(`Attempted to enumerate the non-enumerable set ${U(e)}`);default:throw new u(`Expected a set but found ${U(e)}`)}},zt=/* @__PURE__ */ new WeakMap,Bt=/* @__PURE__ */ new WeakMap,B=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Ie(Rt(e));let t=typeof e==`object`?zt.get(e):void 0;if(t)return t;let n=(typeof e==`object`?Bt.get(e):void 0)??Vt(e);return typeof e==`object`&&zt.set(e,n),n},Vt=e=>{let t=Rt(e);return typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Ie(t):pt(t)},Ht=e=>{if(typeof e!=`object`||e.kind!==`Union`&&e.kind!==`SetOperation`&&e.kind!==`FilteredSet`||zt.has(e))return B(e);let t=Bt.get(e);if(t)return t;let n=Vt(e);return Bt.set(e,n),n},Ut=e=>{if(typeof e!=`object`||zt.has(e))return B(e).elements;if(e.kind===`Union`)return Mt(e,Ut).flatMap((t,n)=>Ut(Nt(t,n,e)));if(e.kind===`FilteredSet`&&!F(e.base))return Ut(e.base).filter(e.test);if(e.kind!==`SetOperation`)return B(e).elements;if(e.operator===`\\cup`)return F(e.left)||F(e.right)?B(e).elements:[...Ut(e.left),...Ut(e.right)];let t=e.operator===`\\`||z(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return F(n)?B(e).elements:Ut(n).filter(t=>$t(t,r)===(e.operator===`\\cap`))},V=e=>{if(typeof e!=`object`||!A(e))throw new u(`Shouldn't call isEmpty() on value ${U(e)}`);switch(e.kind){case`Set`:case`MixedSet`:return e.elements.length===0;case`Interval`:return e.low>e.high;case`InfiniteSet`:if(e.name===`Any`)throw new u(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(V);case`RecordSet`:return e.sets.some(V);case`FunctionSet`:return!V(e.domain)&&V(e.range);case`Union`:return Mt(e).every((t,n)=>V(Nt(t,n,e)));default:return B(e).elements.length===0}},Wt=(e,t)=>e.reduce((e,n)=>{let r=e*Kt(n);if(r>2147483647)throw new u(t());return r},1),Gt=e=>`Overflow when computing the number of elements in:\n${U(e)}`,Kt=e=>{if(typeof e!=`object`)return B(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new u(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new u(`Attempted to compute the number of elements in the overridden value ${U(e)}.`);case`Subset`:{let t=Kt(e.base);if(t>=31)throw new u(Gt(e));return 2**t}case`FunctionSet`:{if(V(e.domain))return 1;if(V(e.range))return 0;let t=Kt(e.range);if(t===1)return 1;let n=Kt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new u(Gt(e));return r.value}case`RecordSet`:return V(e)?0:Wt(e.sets,()=>Gt(e));case`ProductSet`:return V(e)?0:Wt(e.factors,()=>`Overflow when computing the number of elements in ${U(e)}`);default:return B(e).elements.length}},qt=e=>`Attempted to check if the set ${U(e)}is finite.`,Jt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new u(`Attempted to check if the ${me(e)} ${U(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new u(`Attempted to check if the function:\n${U(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return Jt(e.base)&&V(e.base);case`Subset`:return Jt(e.base);case`FunctionSet`:{let t=Jt(e.domain);if(t&&V(e.domain))return!0;let n=Jt(e.range);return n&&V(e.range)?!0:n&&(t||Kt(e.range)===1)}case`RecordSet`:case`ProductSet`:{let t=e.kind===`RecordSet`?e.sets:e.factors,n={isAllFinite:!0};for(let e of t)if(!Jt(e))n.isAllFinite=!1;else if(V(e))return!0;return n.isAllFinite}case`Union`:if(!At(e.base))throw new u(`Attempted to check if the nonenumerable set:\n${jt(e)}\nis a finite set.`);return B(e.base).elements.every(Jt);case`SetOperation`:if(e.operator===`\\cup`)return Jt(e.left)&&Jt(e.right);if(e.operator===`\\cap`){if(!Jt(e.left)&&!Jt(e.right))throw new u(qt(e));return!0}if(Jt(e.left))return!0;if(!Jt(e.right))throw new u(qt(e));return!1;case`FilteredSet`:if(!Jt(e.base))throw new u(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${U(e.base)}\nis finite.`);return!0}},Yt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=L(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Xt=e=>{if(typeof e!=`object`||!j(e))return;let t=H(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Zt=(e,t)=>`Attempted to check if the value ${U(e)} is an element of ${U(t)}`,Qt=(e,t)=>{let n=`Attempted to check if the value:\n${U(e)}\nis an element of the`;if(!j(t))return`${n} ${me(t)} ${U(t)}`;let r=t.kind===`Function`||z(t.domain)?H(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${U(e)}\nis in the record:\n${U(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${U(t)}`},$t=(e,t)=>{if(!A(t))throw new u(Qt(e,t));switch(t.kind){case`Set`:try{return Yt(t.elements,e)!==-1}catch(n){if(!(n instanceof u))throw n;return ut(e,t.elements)}case`MixedSet`:return ut(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!M(e))throw new u(`Attempted to check if the value:\n${U(e)}\nis in the integer interval ${U(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(M(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new u(`Attempted to check if the value:\n${U(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!A(e))throw new u(Zt(e,t));return B(e).elements.every(e=>$t(e,t.base));case`FunctionSet`:{if(!j(e)){if(M(e))return!1;throw new u(Zt(e,t))}let n=H(e),r=Ve(n.domain);return(r===void 0?dt(t.domain,Ie(n.domain)):dt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>$t(e,t.range))}case`RecordSet`:{let n=j(e)?H(e):void 0;if(!n||!xe(n.domain)){if(M(e))return!1;throw new u(`Attempted to check if non-record\n${U(e)}\nis in the set of records:\n${U(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Yt(n.domain,e);return i!==-1&&$t(n.range[i],t.sets[r])})}case`ProductSet`:{let n=j(e)?Xt(e):void 0;if(n===void 0){if(j(e)&&H(e).domain.every(e=>typeof e==`number`)||M(e))return!1;throw new u(`Attempted to check if non-tuple\n${U(e)}\nis in the set of tuples:\n${U(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>$t(e,t.factors[n]))}case`FilteredSet`:return $t(e,t.base)&&t.test(e);case`Union`:if(!At(t.base))throw new u(`Attempted to check if:\n ${U(e)}\nis an element of the non-enumerable set:\n ${jt(t)}`);return B(t.base).elements.some(t=>$t(e,t));case`SetOperation`:return t.operator===`\\cup`?$t(e,t.left)||$t(e,t.right):$t(e,t.left)&&$t(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Xt(e);if(n===void 0){if(M(e))return!1;throw new u(Zt(e,t))}return n.every(e=>$t(e,t.base))}default:throw new u(Qt(e,t))}},en=(e,t,n)=>e===t?!0:typeof e!=`object`||typeof t!=`object`||typeof n!=`object`||e.kind!==`Function`||t.kind!==`Function`||n.kind!==`FunctionSet`||e.domain!==t.domain?$t(e,n):e.range.every((e,r)=>en(e,t.range[r],n.range)),H=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){z(e.domain)||Kt(e.domain);let t=Dt(e.domain);return Le(t,t.map(t=>e.apply(t)))}throw new u(`Expected a function but found ${U(e)}`)},tn=/* @__PURE__ */ new WeakSet,nn=e=>(tn.add(e),e),rn=e=>{let t=e.domain;return t.length===0?tn.has(e)?`FcnRcdValue`:`TupleValue`:t===xt[t.length]?`TupleValue`:Ct.has(t)?`IntervalFcnRcdValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},an=(e,t)=>`Attempted to apply function:\n${fe(U(e))}\nto argument ${fe(U(t))}, which is not in the domain of the function.`,on=e=>`Attempted to apply function with integer domain to the non-integer argument ${fe(U(e))}`,sn=(e,t)=>{if(tn.has(e)||e.kind===`LazyFunction`)return`In applying the function\n${fe(U(e))},\nthe first argument is:\n${fe(U(t))}\nwhich is not in its domain.\n`;let n=rn(e);return n===`TupleValue`?typeof t==`number`?`Attempted to access index ${t} of tuple\n${fe(U(e))}\nwhich is out of bounds.`:`Attempted to access tuple at a non integral index: ${fe(U(t))}`:n===`RecordValue`?typeof t==`string`?`Attempted to access nonexistent field '${t}' of record\n${fe(U(e))}`:`Attempted to access record by a non-string argument: ${fe(U(t))}`:n===`IntervalFcnRcdValue`&&typeof t!=`number`?on(t):an(e,t)},cn=(e,t)=>rn(e)===`FcnRcdValue`?an(e,t):on(t),ln=e=>rn(e)===`RecordValue`,un=(e,t)=>{let n=e[0];return typeof n==`number`?typeof t!=`number`&&(e===xt[e.length]||Ct.has(e)):typeof n==`string`&&typeof t!=`string`},dn=(e,t)=>{let n=e[0];if(typeof t==`number`&&typeof n==`number`){let r=t-n;if(e[r]===t)return r}if(e.length<=8){for(let n=0;n<e.length;n++)if(e[n]===t)return n}return Yt(e,t)},fn=(e,t,n,r)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!$t(t,e.domain))throw new u(tn.has(e)?sn(e,t):`Attempted to apply a function to ${U(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new u(r?.(e)??`Attempted to apply ${U(e)}, which is not a function`);let i=un(e.domain,t)?-1:dn(e.domain,t);if(i===-1)throw new u(n?.(e)??sn(e,t));return e.range[i]},pn=e=>Et(e)?Ct.get(e)??Ot(e.length):void 0,mn=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=H(e).domain;return pn(t)??Ie(t)},hn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!z(e.domain))){if(!$t(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>dt(t,n)?r:e.apply(n),source:e.source}}let i=H(e),a=dn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=Le(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-xn(e,s)+xn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-On(e,s)+On(e,c)|0}return l},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},vn=(e,t)=>Math.imul(e^t,16777619),yn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return _n(t)},bn=e=>_n(Cn(e)+2654435769|0),xn=(e,t)=>_n(Math.imul(Cn(e),668265261)+Cn(t)|0),Sn=e=>{if(e.kind===`ModelValue`)return _n(vn(yn(e.name),109));if(A(e)){if(!z(e))return yn(Mn(e));let t=B(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+bn(t[e])|0;return n}let t=H(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+xn(t.domain[e],t.range[e])|0;return n},Cn=e=>{if(typeof e==`number`)return _n(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return yn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=Sn(e)),e.hash;let t=gn.get(e);if(t!==void 0)return t;let n=Sn(e);return gn.set(e,n),n},wn=/* @__PURE__ */ new WeakMap,Tn=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},En=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return Tn(t)},Dn=e=>Tn(An(e)+2135587861|0),On=(e,t)=>Tn(Math.imul(An(e),374761393)+An(t)|0),kn=e=>{if(e.kind===`ModelValue`)return Tn(En(e.name)^60);if(A(e)){if(!z(e))return En(Mn(e));let t=B(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+Dn(t[e])|0;return n}let t=H(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+On(t.domain[e],t.range[e])|0;return n},An=e=>{if(typeof e==`number`)return Tn(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return En(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=kn(e)),e.fingerprint;let t=wn.get(e);if(t!==void 0)return t;let n=kn(e);return wn.set(e,n),n},jn=/* @__PURE__ */ new WeakMap,Mn=e=>{if(typeof e==`boolean`)return e?`T`:`F`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return JSON.stringify(e);let t=jn.get(e);if(t!==void 0)return t;let n=Nn(e);return jn.set(e,n),n},Nn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${Mn(e.base)}>`;if(e.kind===`FilteredSet`&&!z(e))throw new u(`Attempted to compare the non-enumerable set ${U(e)} with another value`);if(A(e))return z(e)?`{${B(e).elements.map(Mn).join(`,`)}}`:`<${e.kind} ${U(e)}>`;let t=H(e);return`(${t.domain.map((e,n)=>`${Mn(e)}:${Mn(t.range[n])}`).join(`,`)})`},Pn=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Fn=e=>e.reduce((e,t)=>e===0?0:e*Kt(t),1),In=e=>{if(e.kind===`Subset`)return Kt(e.base)<=6?B(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return B(e);if(e.kind===`FunctionSet`&&z(e.domain)&&Kt(e.domain)===0)return Ie([Le([],[])]);let t=e.kind===`FunctionSet`?Kt(e.range)**Kt(e.domain):Fn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?ht:t<=1999?B(e):void 0},Ln=(e,t)=>{if(t)return In(e);if(z(e))try{return In(e)}catch(e){if(e instanceof u)return;throw e}},Rn=(e,t)=>{let n=e=>U(e,t);switch(e.kind){case`Subset`:return typeof e.base==`object`&&e.base.kind===`Interval`?`SUBSET (${n(e.base)})`:`SUBSET ${n(e.base)}`;case`FunctionSet`:return`[${n(e.domain)} -> ${n(e.range)}]`;case`RecordSet`:return`[${e.names.map((t,r)=>`${t}: ${n(e.sets[r])}`).join(`, `)}]`;case`ProductSet`:return`(${e.factors.map(n).join(` \\X `)})`;case`SetOperation`:return`${n(e.left)} ${e.operator} ${n(e.right)}`;case`FilteredSet`:return`{x \\in ${n(e.base)} : ...}`;case`Union`:return jt(e,t)}},zn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),U=(e,t=!1,n=!1)=>{let r=e=>U(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return Pn(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(B(e));case`Interval`:return e.low<=e.high?`${e.low}..${e.high}`:`{}`;case`InfiniteSet`:return e.name===`Any`?`ANY`:e.name;case`Subset`:case`FunctionSet`:case`RecordSet`:case`ProductSet`:case`SetOperation`:case`FilteredSet`:case`Union`:{let n=Ln(e,t);return n?r(n):Rn(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!z(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${U(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let i=H(e),a=r=>U(r,t,n&&e.kind===`Function`);return i.domain.every((e,t)=>e===t+1)?`<<${i.range.map(a).join(`, `)}>>`:i.domain.every(e=>typeof e==`string`&&zn(e))?`[${(n||i.hash!==0||i.fingerprint!==0?i.domain.map((e,t)=>[e,i.range[t]]):ve(i)).map(e=>`${String(e[0])} |-> ${a(e[1])}`).join(`, `)}]`:`(${i.domain.map((e,t)=>`${r(e)} :> ${a(i.range[t])}`).join(` @@ `)})`}}},Bn=e=>{let t=Object.entries(e);return t.map(([e,n])=>`${t.length===1?``:`/\\ `}${e} = ${fe(U(n))}\n`).join(``)},Vn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Hn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=Cn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||Be(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||Be(i[e],n))return i[e]}return n.kind===`Set`?Vn(n.elements,t):(Vn(n.domain,t),Vn(n.range,t)),i===void 0?e.set(r,n):Array.isArray(i)?i.push(n):e.set(r,[i,n]),n};return e=>{for(let n=0;n<e.length;n++)e[n]=t(e[n]);return e}},Un=e=>{switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return[];case`Apply`:return e.args;case`Lambda`:return[e.body];case`And`:case`Or`:return e.operands;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return[e.expression];case`If`:return[e.condition,e.consequent,e.alternative];case`Case`:return[...e.arms.flatMap(e=>[e.condition,e.body]),...e.other?[e.other]:[]];case`Let`:return[...e.definitions.flatMap(e=>e.kind===`Function`?[...e.bounds.map(e=>e.set),e.body]:[e.body]),e.body];case`Forall`:case`Exists`:return[...e.bounds.map(e=>e.set),e.body];case`UnboundedQuantifier`:return[e.body];case`Choose`:return[...e.set?[e.set]:[],e.body];case`SetEnumeration`:case`Tuple`:return e.elements;case`SetFilter`:return[e.bound.set,e.predicate];case`SetMap`:return[e.expression,...e.bounds.map(e=>e.set)];case`FunctionConstruction`:return[...e.bounds.map(e=>e.set),e.body];case`FunctionSet`:return[e.domain,e.range];case`Record`:case`RecordSet`:return e.fields.map(e=>e.value);case`Except`:return[e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])];case`FunctionApplication`:return[e.function,...e.args];case`FieldAccess`:return[e.record];case`Product`:return e.factors;case`BoxAction`:case`AngleAction`:return[e.action,e.subscript];case`WeakFairness`:case`StrongFairness`:return[e.subscript,e.action];case`Label`:return[e.body];case`Subexpression`:return e.selectors.flatMap(e=>`args`in e?e.args:[])}},W=e=>(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`,Wn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Gn=(e,t)=>Wn.has(e)?new u(t):new d(t),Kn=e=>typeof e==`object`&&e.kind===`Operator`,G=(e,t,n)=>{let r=e[t];if(r===void 0||Kn(r))throw Gn(n,`${n} expects a value as argument ${t+1}`);return r},qn=(e,t,n)=>{let r=G(e,t,n);if(typeof r!=`number`)throw Gn(n,`${n} expects an integer but received ${U(r)}`);return r},Jn=(e,t,n)=>{let r=G(e,t,n);if(typeof r!=`boolean`)throw Gn(n,`${n} expects a boolean but received ${U(r)}`);return r},Yn=(e,t,n)=>{let r=G(e,t,n);if(!A(r))throw Gn(n,`${n} expects a set but received ${U(r)}`);return r},Xn=(e,t,n)=>{let r=Yn(e,t,n);if(!z(r))throw Gn(n,`${n} cannot enumerate the non-enumerable set ${U(r)}`);return r},Zn=(e,t,n)=>B(Xn(e,t,n)),Qn=(e,t,n)=>Zn(e,t,n).elements,$n=(e,t,n)=>{let r=G(e,t,n),i=Xt(r);if(!i)throw Gn(n,`${n} expects a sequence but received ${U(r)}`);return i},er=(e,t,n)=>{let r=G(e,t,n);if(!j(r))throw Gn(n,`${n} expects a function but received ${U(r)}`);return H(r)},tr=(e,t,n)=>{let r=e[t];if(r===void 0||!Kn(r))throw Gn(n,`${n} expects an operator as argument ${t+1}`);return r},nr=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),K=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:t===1?e=>n([e]):void 0,evaluateBinary:t===2?(e,t)=>n([e,t]):void 0}),rr=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),ir=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),ar=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(G(n,0,e),G(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),or=e=>e>2147483647||e<-2147483648,sr=e=>new d(`Overflow when computing ${e}`),cr={ModelValue:`ModelValue`,Set:`SetEnumValue`,Interval:`IntervalValue`,InfiniteSet:`UserValue`,SequenceSet:`UserValue`,Subset:`SubsetValue`,FunctionSet:`SetOfFcnsValue`,RecordSet:`SetOfRcdsValue`,ProductSet:`SetOfTuplesValue`,FilteredSet:`SetPredValue`,LazyFunction:`FcnLambdaValue`,Union:`UnionValue`,MixedSet:`SetEnumValue`},lr={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},ur={BoolValue:`a Boolean value`,IntValue:`an integer`,StringValue:`a string`,RecordValue:`a record`,SetEnumValue:`a set of the form {e1, ... ,eN}`,SetPredValue:`a set of the form {x \\in S : expr}`,TupleValue:`a tuple`,FcnLambdaValue:`a function of the form [x \\in S |-> expr]`,FcnRcdValue:`a function of the form (d1 :> e1 @@ ... @@ dN :> eN)`,SetOfFcnsValue:`a set of the form [S -> T]`,SetOfRcdsValue:`a set of the form [d1 : S1, ... , dN : SN]`,SetOfTuplesValue:`a cartesian product`,SubsetValue:`a set of the form SUBSET S`,SetDiffValue:`a set of the form S \\ T`,SetCapValue:`a set of the form S \\cap T`,SetCupValue:`a set of the form S \\cup T`,ModelValue:`a model value`,UserValue:`a special set constant`,IntervalValue:`a set of the form i..j`,UnionValue:`a set of the form UNION S`},dr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return lr[e.operator];if(e.kind!==`Function`)return cr[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},fr=e=>ur[dr(e)],pr=(e,t)=>new u(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),mr=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof u&&(e.isLazy===!0||!(n instanceof d))?pr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},hr=(e,t)=>{if(typeof e!=`number`)throw pr(t,`Cannot cast tlc2.value.impl.${dr(e)} to tlc2.value.impl.IntValue`);return e},gr=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,_r=(e,t,n)=>{let r=gr(t,`IntValue`,2);return ar(e,(t,i)=>{let a=hr(t,r),o=hr(i,r),s=n(a,o);if(or(s))throw sr(`${a}${e}${o}`);return s})},vr=(e,t,n)=>{if(typeof e!=`number`)throw new d(`The ${t} argument of ${n} should be an integer, but instead it is:\n${U(e)}`);return e},yr=(e,t)=>ar(e,(n,r)=>t(vr(n,`first`,e),vr(r,`second`,e))),br=(e,t,n)=>({...ir(e,t,n),isLazy:!0}),xr=(e,t,n)=>{let r=tr(e,t,n);return()=>r.call([])},Sr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),Cr=()=>({bool:so,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ja(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw Ya(t,n,e);return e},apply:fn,tuple:R,set:mt,record:Le,writtenRecord:ye,equal:Re,enumerate:po,unassigned:(e,t)=>{throw Rs(e,t)},primeOutsideAction:e=>{throw zs(e)},prime:e=>({environment:e.environment,scope:e.scope,state:e.next,next:void 0}),extend:(e,t,n)=>({environment:e.environment,scope:t.reduce((e,t,r)=>io(e,t,n[r]),e.scope),state:e.state,next:e.next})}),wr={value:void 0},Tr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),Er=e=>`local${e.counter++}`,Dr=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Un(e).forEach(e=>Dr(e,t)),t),Or=(e,t,n)=>{let r=Tr(e,Y(t,e.environment)),i=Dr(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=Tr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},kr=(e,t,n)=>{let r=t.kind===`Forall`,i=JSON.stringify(r?`the body of \\A`:`the body of \\E`),a=`quantifier${e.counter++}`,o=[...n.locals.values()],s={context:`context`,state:`state`,next:`next`,isPrimed:!1,locals:new Map(n.locals)},c=t.bounds.map(()=>Er(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${jr(e,t.set,s)}, ${Tr(e,t.set)});`),u=t.bounds.map(t=>{let n=Er(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${jr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=Er(e);return`for (let ${i} = 0; ${i} < ${c[r]}.length; ${i}++) { const ${n} = ${c[r]}[${i}]; ${t} }`},d),p=o.map(e=>`, ${e}`).join(``);return e.helpers.push(`function ${a}(context, state, next${p}) { let primed; ${l.join(` `)} ${f} return ${r}; }`),`${a}(${n.context}, ${n.state}, ${n.isPrimed?`undefined`:n.next}${p})`},Ar=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${jr(e,r[0],n)}, "the left side of =>") ? bool(${jr(e,r[1],n)}, "the right side of =>") : true)`;if(W(t))return Or(e,t,n);let i=Do(t.operator,e.environment);if(i?.kind!==`Builtin`)return Or(e,t,n);if(i.name===`DOMAIN`)return`${Tr(e,$a(t))}(${jr(e,r[0],n)})`;if(i.name===`TLCEval`)return Or(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${Tr(e,eo(t,i))}(${jr(e,r[0],n)}, ${jr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&vs(r[0],a))return`${Tr(e,i.evaluateUnary)}(${jr(e,r[0],n)})`;let o=r.length===2&&vs(r[0],a)&&vs(r[1],a);return i.evaluateBinary&&o?`${Tr(e,i.evaluateBinary)}(${jr(e,r[0],n)}, ${jr(e,r[1],n)})`:Or(e,t,n)},jr=(e,t,n)=>{let r=(t,r=n)=>jr(e,t,r);if(cs(t)&&rs(t,e.environment))return Or(e,t,n);switch(t.kind){case`Number`:return Go(t.value)?String(t.value):Or(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(W(t))return Or(e,t,n);let i=Lo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=Tr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?Tr(e,i.value):Or(e,t,n)}case`Apply`:return Ar(e,t,n);case`And`:{let n=Tr(e,t);return`(${t.operands.map((e,t)=>`conjunct(${r(e)}, ${n}, ${t})`).join(` && `)})`}case`Or`:return`(${t.operands.map(e=>`bool(${r(e)}, "a disjunct")`).join(` || `)})`;case`If`:return`(condition(${r(t.condition)}, ${Tr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{Ms(t.function);let n=r(t.function),i=Tr(e,Za(t));return t.args.length===1?`apply(${n}, ${r(t.args[0])}, undefined, ${i})`:`apply(${n}, tuple([${t.args.map(e=>r(e)).join(`, `)}]), undefined, ${i})`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${Tr(e,Xa(t))}, ${Tr(e,Qa(t))})`;case`Record`:{let i=Is(t.fields);if(Ls(i))return Or(e,t,n);let a=Tr(e,i.map(e=>e.name)),o=`[${i.map(e=>r(e.value)).join(`, `)}]`,s=t.fields.map(e=>e.name);return s.every((e,t)=>e===i[t].name)?`record(${a}, ${o})`:`writtenRecord(${a}, ${o}, ${Tr(e,s)})`}case`Tuple`:return`tuple([${t.elements.map(e=>r(e)).join(`, `)}])`;case`SetEnumeration`:return`set([${t.elements.map(e=>r(e)).join(`, `)}])`;case`Prime`:{let i=Tr(e,t);if(n.isPrimed)return`primeOutsideAction(${i})`;let a={context:`(primed ??= prime(${n.context}))`,state:n.next,next:`undefined`,isPrimed:!0,locals:n.locals};return`(${n.next} === undefined ? primeOutsideAction(${i}) : ${r(t.expression,a)})`}case`Unchanged`:return`equal(${r({kind:`Prime`,expression:t.expression,location:t.location})}, ${r(t.expression)})`;case`Forall`:case`Exists`:return t.bounds.every(e=>e.pattern.kind===`Name`)?kr(e,t,n):Or(e,t,n);default:return Or(e,t,n)}},Mr=(e,t)=>{if(!Sr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=jr(n,e,{context:`context`,state:`state`,next:`next`,isPrimed:!1,locals:/* @__PURE__ */ new Map}),i=n.references.map((e,t)=>`reference${t}`),a=`${n.helpers.join(`
23
- `)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;wr.value??=Cr();let o=wr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Nr={kind:`Local`},Pr=/* @__PURE__ */ new WeakMap,Fr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Ir=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Lr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Rr=e=>e.kind===`Operator`?e.params.length:0,zr=e=>e.name.includes(`!`),Br=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},Vr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Hr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Ur=e=>e.kind===`Name`?[e.name]:e.names,Wr=e=>e.flatMap(e=>Ur(e.pattern)),Gr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Lr(n.location,t.location))?n:void 0},Kr=(e,t)=>{let n=Gr(e,t);return n?{key:t.getKey(n),arity:Rr(n)}:t.imported.get(e)},qr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Jr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),Yr=(e,t,n,r)=>{let i=e.location,a=Pr.get(e);if(a!==void 0)return a;if(Fr(t))return e.kind===`Apply`?e.args.length:0;if(Jr(t))return 0;let o=Br(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Nr),o)return o.arity;let s=Kr(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,Pr.set(e,s.arity),s.arity;let l=t.slice(0,t.indexOf(`!`));if(l!==``&&(Br(n,l)!==void 0||Kr(l,r)!==void 0||r.facts.has(l)))return e.kind===`Apply`?e.args.length:0;let u=`at line ${i.line}, column ${i.column}`;throw new c(qr(t)?`Could not find declaration or definition of symbol '${t}' ${u}`:`Unknown operator: \`${t}' ${u}`)},Xr=(e,t,n)=>(e.forEach(e=>q(e.set,t,n)),e.reduce((e,t)=>Vr(e,Ur(t.pattern)),t)),Zr=(e,t)=>e.kind===`Identifier`&&e.name===t&&W(e)||Un(e).some(e=>Zr(e,t)),Qr=(e,t)=>{e.kind===`FunctionConstruction`&&Zr(e.body,t)&&(e.isRecursive=!0),Un(e).forEach(e=>Qr(e,t))},$r=(e,t,n)=>{let r=Vr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(q(e.body,Xr(e.bounds,r,n),n),Qr(e.body,e.name)):q(e.body,Hr(r,e.params),n)},ei=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Rr(t),parent:e}),t);return e.reduce((e,t)=>(zr(t)||$r(t,e,n),{name:t.name,arity:Rr(t),parent:e}),r)},ti=(e,t,n,r,i)=>{let a=e.kind===`Operator`?e.params[t]:void 0,o=n.location;if(a?.arity===0&&r>0)throw new c(`Operator used in argument number ${t+1} has incorrect number of arguments at line ${o.line}, column ${o.column}`);let s=i?.kind===`Operator`&&i.params.some(e=>e.arity>0);if(a!==void 0&&a.arity>0&&(r!==a.arity||s))throw new c(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},ni=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,ri=(e,t,n)=>{let r=Br(t,e.operator)?void 0:Gr(e.operator,n),i=Yr(e,e.operator,t,n);if(i!==e.args.length)throw new c(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=ni(e),o=a===void 0||Br(t,a)?void 0:Gr(a,n),s=ii(e,t,n);r&&ti(r,i,e,s,o)})},ii=(e,t,n)=>e.kind===`Identifier`?Yr(e,e.name,t,n):e.kind===`OperatorReference`?Yr(e,e.operator,t,n):(q(e,t,n),e.kind===`Lambda`?e.params.length:0),q=(e,t,n)=>{let r=e=>e.forEach(e=>q(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=Yr(e,e.name,t,n);if(r>0)throw new c(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:Yr(e,e.operator,t,n);return;case`Apply`:ri(e,t,n);return;case`Lambda`:q(e.body,Vr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:q(e.expression,t,n);return;case`If`:r([e.condition,e.consequent,e.alternative]);return;case`Case`:r(e.arms.flatMap(e=>[e.condition,e.body])),e.other&&q(e.other,t,n);return;case`Let`:q(e.body,ei(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:q(e.body,Xr(e.bounds,t,n),n);return;case`UnboundedQuantifier`:q(e.body,Vr(t,e.names),n);return;case`Choose`:e.set&&q(e.set,t,n),q(e.body,Vr(t,Ur(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:q(e.predicate,Xr([e.bound],t,n),n);return;case`SetMap`:q(e.expression,Xr(e.bounds,t,n),n);return;case`FunctionSet`:r([e.domain,e.range]);return;case`Record`:case`RecordSet`:if(new Set(e.fields.map(e=>e.name)).size<e.fields.length)throw new c(`Non-unique fields in constructor at line ${e.location.line}, column ${e.location.column}`);r(e.fields.map(e=>e.value));return;case`Except`:r([e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])]);return;case`FunctionApplication`:r([e.function,...e.args]);return;case`FieldAccess`:q(e.record,t,n);return;case`Product`:r(e.factors);return;case`BoxAction`:case`AngleAction`:r([e.action,e.subscript]);return;case`WeakFairness`:case`StrongFairness`:r([e.subscript,e.action]);return;case`Label`:q(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>ii(e,t,n))}},ai=(e,t,n)=>(e.set&&q(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),oi=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?ai(t,e,n):(t.kind===`AssumeProve`?oi(t,e,n):q(t,e,n),e),t);return q(e.conclusion,r,n),r},si=(e,t,n)=>e.kind===`AssumeProve`?oi(e,t,n):(q(e,t,n),t),ci=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>li(t,e,n),t)},li=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=si(r.statement,t,n);return ci(e.proof,i,n),r.isSuffices?i:t}case`Define`:return ei(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return q(r.expression,t,n),ci(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>q(e,t,n)),t;case`Take`:return Vr(Xr(r.bounds,t,n),r.names);case`Pick`:{let i=Vr(Xr(r.bounds,t,n),r.names);return q(r.body,i,n),ci(e.proof,t,n),i}default:return ci(e.proof,t,n),t}},ui=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:si(e.statement,void 0,t);ci(e.proof,n,t)},di=(e,t)=>new c(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),fi=(e,t,n,r)=>t.reduce((e,t)=>{if(Br(e,t)||n(t))throw di(t,r);return{name:t,arity:0,parent:e}},e),pi=(e,t,n,r)=>(e.forEach(e=>hi(e.set,t,n)),e.reduce((e,t)=>fi(e,Ur(t.pattern),n,r),t)),mi=(e,t,n)=>{if(e.kind===`Function`){let r=Vr(t,[e.name]),i=pi(e.bounds,r,n,e.location);hi(e.body,i,n);return}let r=fi(t,e.params.map(e=>e.name),n,e.location);hi(e.body,r,n)},hi=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:hi(e.body,fi(t,e.params,n,r),n);return;case`Let`:{let i=e.definitions.filter(e=>e.isRecursive).map(e=>e.name),a=e.definitions.reduce((e,t)=>{if(Br(e,t.name)||n(t.name))throw di(t.name,t.location);return mi(t,Vr(e,i),n),Vr(e,[t.name])},t);hi(e.body,fi(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:hi(e.body,pi(e.bounds,t,n,r),n);return;case`SetFilter`:hi(e.predicate,pi([e.bound],t,n,r),n);return;case`SetMap`:hi(e.expression,pi(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:hi(e.body,fi(t,e.names,n,r),n);return;case`Choose`:e.set&&hi(e.set,t,n),hi(e.body,fi(t,Ur(e.pattern),n,r),n);return;default:Un(e).forEach(e=>hi(e,t,n))}},gi=e=>[...e.definitions.map(e=>({name:e.name,kind:`Definition`,location:e.location,isRecursive:e.isRecursive})),...e.instances.flatMap(e=>e.name===void 0?[]:[{name:e.name,kind:`Definition`,location:e.location,isRecursive:!1}]),...e.constants.map(e=>({name:e.name,kind:`Constant`,location:e.location,isRecursive:!1})),...e.variables.map(e=>({name:e.name,kind:`Variable`,location:e.location,isRecursive:!1}))],_i=(e,t)=>{let n=/* @__PURE__ */ new Map;gi(e).sort((e,t)=>Lr(e.location,t.location)?-1:1).forEach(e=>n.set(e.name,[...n.get(e.name)??[],e]));let r=(e,n)=>{if(!t.imported.has(e)&&!Ir.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Lr(r,n)};n.forEach(e=>e.forEach((n,i)=>{let a=e.slice(0,i).some(e=>e.kind===`Definition`||n.kind===`Definition`||e.kind!==n.kind),o=t.imported.get(n.name)?.declaration===n.kind;if(a||!o&&r(n.name,n.location))throw di(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Lr(t.location,e))||r(t,e);e.definitions.forEach(e=>mi(e,void 0,i(e.location))),e.assumptions.forEach(e=>hi(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=fi(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>hi(e.expression,n,t))})},vi=(e,t)=>{let n=new Map([...Ir,...t]);return e.constants.forEach(e=>n.set(e.name,{key:e.name,arity:e.arity})),e.variables.forEach(e=>n.set(e.name,{key:e.name,arity:0})),n},yi=(e,t,n,r)=>{let i=Hr(void 0,e.params),a={imported:vi(n,r.imported),definitions:r.definitions,getKey:r.getKey,location:e.location,self:void 0,facts:/* @__PURE__ */ new Set},o=new Map(e.substitutions.map(e=>[e.name,e.expression])),s=`in the instance of ${e.module} at line ${e.location.line}, column ${e.location.column}`;return o.forEach((e,n)=>{if(t.every(e=>e.name!==n))throw new c(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Br(i,n)&&!Kr(n,a))throw new c(`Substitution missing for symbol ${n} ${s}`);let r=o.get(n)??{kind:`Identifier`,name:n,location:e.location};if(t.arity===0||r.kind!==`Identifier`)return q(r,i,a),[n,r];if(Yr(r,r.name,i,a)!==t.arity)throw new c(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},bi=(e,t)=>{let n=vi(e,t.imported),r=new Set([...e.theorems,...e.assumptions].flatMap(e=>e.name??[])),i=(e,i)=>({imported:n,definitions:t.definitions,getKey:t.getKey,location:e,self:i,facts:r});e.definitions.forEach(e=>$r(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>q(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ui(e,i(e.location,void 0)))},xi=/* @__PURE__ */ new WeakSet,Si=/* @__PURE__ */ new WeakSet,Ci=(e,t,n=/* @__PURE__ */ new Set)=>{let r=ki(e,(e,r)=>Ci(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&W(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&W(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},wi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),Ti=/* @__PURE__ */ new WeakMap,Ei=e=>Ti.get(e)??e.name,Di=(e,t)=>(Ei(t)!==e.name&&Ti.set(e,Ei(t)),e),Oi=(e,t)=>Di(e.kind===`Function`?{...e,bounds:wi(e.bounds,e=>t(e,[])),body:t(e.body,Wr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),ki=(e,t)=>{let n=e=>t(e,[]);switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return e;case`Apply`:return{...e,args:e.args.map(n)};case`Lambda`:return{...e,body:t(e.body,e.params)};case`And`:case`Or`:return{...e,operands:e.operands.map(n)};case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return{...e,expression:n(e.expression)};case`If`:return{...e,condition:n(e.condition),consequent:n(e.consequent),alternative:n(e.alternative)};case`Case`:return{...e,arms:e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),other:e.other?n(e.other):void 0};case`Let`:{let n=e.definitions.map(e=>e.name);return{...e,definitions:e.definitions.map(e=>Oi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:wi(e.bounds,n),body:t(e.body,Wr(e.bounds))};case`UnboundedQuantifier`:return{...e,body:t(e.body,e.names)};case`Choose`:return{...e,set:e.set?n(e.set):void 0,body:t(e.body,Ur(e.pattern))};case`SetEnumeration`:case`Tuple`:return{...e,elements:e.elements.map(n)};case`SetFilter`:return{...e,bound:{pattern:e.bound.pattern,set:n(e.bound.set)},predicate:t(e.predicate,Ur(e.bound.pattern))};case`SetMap`:return{...e,bounds:wi(e.bounds,n),expression:t(e.expression,Wr(e.bounds))};case`FunctionSet`:return{...e,domain:n(e.domain),range:n(e.range)};case`Record`:case`RecordSet`:return{...e,fields:e.fields.map(e=>({name:e.name,value:n(e.value)}))};case`Except`:return{...e,function:n(e.function),updates:e.updates.map(e=>({path:e.path.map(e=>e.kind===`Index`?{kind:`Index`,args:e.args.map(n)}:e),value:n(e.value)}))};case`FunctionApplication`:return{...e,function:n(e.function),args:e.args.map(n)};case`FieldAccess`:return{...e,record:n(e.record)};case`Product`:return{...e,factors:e.factors.map(n)};case`BoxAction`:case`AngleAction`:return{...e,action:n(e.action),subscript:n(e.subscript)};case`WeakFairness`:case`StrongFairness`:return{...e,subscript:n(e.subscript),action:n(e.action)};case`Label`:return{...e,body:n(e.body)};case`Subexpression`:return{...e,selectors:e.selectors.map(e=>`args`in e?{...e,args:e.args.map(n)}:e)}}},Ai=`LAMBDA`,ji=(e,t=/* @__PURE__ */ new Set)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0,r=n===void 0||t.has(n)?[]:[n];return ki(e,(e,n)=>(r.push(...ji(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Mi=e=>{let t=/* @__PURE__ */ new Map;return n=>{if(!e.substitutedNames.has(n))return n;let r=t.get(n)??e.instantiation.createName(n);return t.set(n,r),r}},Ni=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),Pi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Fi=(e,t)=>e.map(e=>({pattern:Pi(e.pattern,t),set:e.set})),Ii=(e,t)=>e.kind===`Function`?{...e,bounds:Fi(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Li=(e,t,n)=>{let r=e=>t.get(e)??e;switch(e.kind){case`Identifier`:return{...e,name:r(e.name)};case`Apply`:case`OperatorReference`:return{...e,operator:r(e.operator)};case`Label`:return{...e,params:e.params.map(r)};case`Lambda`:return{...e,params:e.params.map(n)};case`Let`:return{...e,definitions:e.definitions.map(e=>Di({...Ii(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Fi(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:Pi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:Pi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Ri=e=>`${e}@ENABLED`,zi=(e,t)=>e.modes.get(t)??e,Bi=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!W(e)&&t.instantiation.variables.has(e.name),Vi=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>Vi(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Bi(e,t,n))return[e.name];let r=W(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?Vi(r.body,t,/* @__PURE__ */ new Map):void 0},Hi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Bi(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Ri(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:qi(e.expression,zi(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[qi(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:qi(e.subscript,zi(t,`Primed`),n),location:r},qi(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=Vi(e.expression,t,n);if(r!==void 0)return{kind:`And`,operands:r.map(r=>({kind:`Apply`,operator:`=`,args:[{kind:`Prime`,expression:{kind:`Identifier`,name:Ri(r),location:e.location},location:e.location},qi({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ui=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Wi=(e,t)=>e.map(e=>Ui(e.name,t)),Gi=(e,t,n,r)=>{let i=Array.from({length:n},(e,t)=>`argument${t}`);return{kind:`Lambda`,params:i,body:{kind:`Apply`,operator:e,args:[...Wi(t,r),...i.map(e=>Ui(e,r))],location:r},location:r}},Ki=(e,t)=>e.instantiation.isParameterized(t)?Yi(e,t):void 0,qi=(e,t,n)=>{let r=e=>qi(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!W(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:qi(e.expression,zi(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:qi(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},zi(t,`Enabled`),n)};let s=Hi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return xi.add(n),n;let r=Ki(t,e.name);if(r===void 0)return e;if(a.length===0)return{...e,name:r};let o=i.getDefinition(e.name);return o&&Rr(o)>0?Gi(r,a,Rr(o),e.location):{kind:`Apply`,operator:r,args:Wi(a,e.location),location:e.location}}if(e.kind===`Apply`&&o(e.operator)){let n=e.args.map(r),o=i.substitutions.get(e.operator),s=i.substitutions.size>0||a.length>0||Si.has(e),l=e=>(s&&Si.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return l({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return l({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:Ai,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:Ai,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new c(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let u=Ki(t,e.operator);return u===void 0?l({...e,args:n}):{...e,operator:u,args:[...Wi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ki(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Gi(n,a,Rr(r),e.location):{...e,operator:n}}let l=Mi(t),u=ki(e,(e,i)=>i.length===0?r(e):qi(e,t,Ni(n,i,l)));return Li(u,n,l)},Ji=(e,t,n)=>t.length===0?e:e.kind===`Operator`?{...e,params:[...t,...e.params]}:{kind:`Operator`,name:e.name,params:t,body:{kind:`Let`,definitions:[e],body:Ui(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},Yi=(e,t)=>{let n=e.copies.get(t);if(n!==void 0)return n;let r=e.instantiation,i=r.getDefinition(t);return i?r.addDefinition(t,i.name,n=>{e.copies.set(t,n);let a=Mi(e),o=Ii(Oi(i,(t,n)=>qi(t,e,Ni(/* @__PURE__ */ new Map,n,a))),a);return Ji(o,r.params,i.location)}):t},Xi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>ji(e))),r=r=>{let i={instantiation:e,substitutedNames:n,copies:/* @__PURE__ */ new Map,mode:r,modes:t};return t.set(r,i),i},i=r(`Normal`);return r(`Enabled`),r(`Primed`),{getKey:e=>Ki(i,e)??e,getQualifiedKey:t=>{let n=Ki(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(t,r.name,()=>Ji(r,e.params,r.location)):t}}},Zi=e=>e.kind===`Let`&&e.instances!==void 0||Un(e).some(Zi),Qi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=ki(e,e=>Qi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Lr(n.location,e.location)),a=i===-1?e.length:i;return[...e.slice(0,a),...t(n,r),...e.slice(a)]},i.definitions),body:i.body,location:i.location}},$i=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,ea=(e,t)=>e.definitions.some(e=>Zi(e.body))||e.assumptions.some(e=>Zi(e.expression))||e.theorems.some(e=>{let t=$i(e);return t!==void 0&&Zi(t)})?{...e,definitions:e.definitions.map(e=>Oi(e,e=>Qi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Qi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=$i(e);return n===void 0?e:{...e,statement:Qi(n,t,[])}})}:e,ta=/* @__PURE__ */ new WeakMap,na=/* @__PURE__ */ new WeakMap,J=(e,t,n)=>e.isInit?{environment:e.environment,scope:t,state:n,next:void 0}:{environment:e.environment,scope:t,state:e.current,next:n},ra=(e,t,n)=>{let r=e.slice();return r[t]=n,r},ia=(t,r,i=X(t,r))=>{if(typeof i==`boolean`)return i;try{return so(i,`the action at ${Ga(t)}`)}catch(t){throw r.next===void 0?t:n(t,e.error)}},aa=e=>e===`=`||e===`\\in`||e===`\\subseteq`,oa=(e,t,n)=>{let r=n?ta:na,i=r.get(e);if(i)return i;let a=Fa(e,t,n);return r.set(e,a),a},sa=(e,t)=>{if(t.constants.has(e))return;let n=co(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},ca=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return ca(e.expression,t,!1,r);if(e.kind!==`Identifier`||W(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=sa(e.name,t);if(a&&!r.has(a))return r.add(a),ca(a.body,t,n,r)},la=(e,t,n)=>ca(e,t,!n),ua=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ua(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Uo(e.definitions,J(r,t,n));return ua(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=ro(t,e.name);if(a?.kind===`Thunk`)return ua(a.expression,a.scope,n,r,i);if(a)return;let o=r.environment.variableIndex.get(e.name);if(o!==void 0)return i?void 0:o;let s=sa(e.name,r.environment);if(s)return ua(s.body,void 0,n,r,i)},da=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||W(e))||!t&&e.kind===`Prime`,fa=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>fa(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`||n.has(e.name))return;let r=t.variableIndex.get(e.name);if(r!==void 0)return[r];let i=sa(e.name,t);if(i)return n.add(e.name),fa(i.body,t,n)},pa=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ma=(e,t,n)=>pa.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||aa(e.operator)&&(da(e.args[0],n)||la(e.args[0],t,n)!==void 0)||W(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,ha=/* @__PURE__ */ new Set,ga=(e,t)=>{if(W(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return ki(e,(e,i)=>{let a=r.isReadOnly?ga(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},_a=(e,t,n)=>{if(e.kind===`Or`||e.kind===`Exists`)return!0;let r=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0,i=r===void 0||W(e)?void 0:co(r,t);return i&&!n.has(i)&&(n.add(i),_a(i.body,t,n))?!0:Un(e).some(e=>_a(e,t,n))},va=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=la(e.args[0],t,n);if(r!==void 0)return{kind:`Assign`,expression:e,index:r,value:e.args[1]}}if(!n&&e.kind===`Unchanged`){let n=fa(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ma(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:oa(e,t,n)}},ya=(e,t)=>{if(e.bounds.length<2||e.body.kind!==`And`)return;let n=e.bounds.flatMap(e=>e.pattern.kind===`Name`?[e.pattern.name]:[]);if(n.length!==e.bounds.length)return;let r=new Set(n.slice(0,-1)),i=e.body.operands,a=i.findIndex(e=>{if(Ws(e,t)===void 0||_a(e,t,/* @__PURE__ */ new Set))return!0;let n=ga(e,ha);return!n||n.some(e=>r.has(e))}),o=a===-1?i.length:a;if(o!==0)return{names:n,guards:i.slice(0,o),rest:i.slice(o)}},ba=(e,t,n)=>{let r=e.map(e=>va(e,t,n)),i=r.map(()=>void 0),a=(e,n,o,s,c,l)=>{let u=s,d;for(let s=e;s<r.length;s++){if(s>0&&n.isInit&&za(u)){d??=J(n,o,u);for(let e=s;e<r.length;e++)if(!xa(r[e].expression,d))return;l(u,c);return}let e=r[s];if(e.kind===`Guard`){d??=J(n,o,u);let r=i[s]??=ps(e.expression,t);if(!ia(e.expression,d,r(d)))return;continue}if(e.kind===`Assign`){d??=J(n,o,u);let r=(i[s]??=ps(e.value,t))(d),a=u[e.index];if(a===void 0)u=ra(u,e.index,r),d=void 0;else if(!Re(a,r))return;continue}if(e.kind===`Unchanged`){let t=n.current;if(!t)return;for(let n of e.indices){let e=u[n];if(e===void 0)u=ra(u,n,t[n]),d=void 0;else if(!Re(e,t[n]))return}continue}let f=s+1;e.runner(n,o,u,c,!0,(e,t)=>a(f,n,o,e,t,l));return}l(u,c)};return(e,t,n,r,i,o)=>a(0,e,t,n,r,o)},xa=(e,t)=>so(X(e,t),`the initial predicate at ${Ga(e)}`),Sa=e=>(t,n,r,i,a,o)=>{ia(e,J(t,n,r))&&o(r,i)},Ca=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),d=J(r,i,a);if(a[l]!==void 0){ia(e,d)&&c(a,o);return}let f=X(n,d),p=e.operator===`\\subseteq`?{kind:`Subset`,base:f}:f;if(!z(p))throw new u(`In computing ${r.isInit?`initial`:`next`} states, the right side of \\IN is not enumerable.\n${qa(e)}`);let m=Ut(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(ra(a,l,m[e]),o)}},wa=/* @__PURE__ */ new WeakMap,Ta=e=>{let t=wa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(Ta),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return wa.set(e,n),n},Ea=(e,t)=>{let n=e.args[0],r=Sa(e),i=(e,r,i)=>ua(n,r,i,e,!t)??-1,a=Ca(e,i);return(t,n,o,s,c,l)=>{let u=i(t,n,o);if(u===-1||o[u]!==void 0){r(t,n,o,s,c,l);return}if(e.operator!==`=`){a(t,n,o,s,c,l);return}let d=X(e.args[1],J(t,n,o));l(ra(o,u,d),s)}},Da=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=ro(t,e.name);if(i?.kind===`Thunk`)return Da(i.expression,i.scope,n,r);let a=i?void 0:co(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,J(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:io(void 0,a.name,o)}},Oa=(e,t,n)=>{let r=Sa(e);return(i,a,o,s,c,l)=>{let d=Da(e.function,a,o,i);if(!d){r(i,a,o,s,c,l);return}let f=J(i,a,o),p=e.args.map(e=>X(e,f)),m=p.length===1?p[0]:R(p),h=ho(d.bounds,J(i,d.scope,o));if(!$t(m,h))throw new u(`Attempted to apply a function to ${U(m)}, which is not in its domain at ${Ga(e)}`);oa(d.body,t,n)(i,go(d.bounds,d.scope,m),o,s,c,l)}},ka=(e,t)=>{if(fa(e,t))return ba([Ta(e)],t,!1);if(e.kind===`Tuple`)return oa(Ta(e),t,!1);let n=Ta(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?ro(i,e.name):void 0;l?.kind===`Thunk`?oa(Ta(l.expression),t,!1)(r,l.scope,a,o,s,c):ia(n,J(r,i,a))&&c(a,o)}},Aa=(e,t,n)=>{let r=e.operator,i=Sa(e),a=W(e)?void 0:Do(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=xs(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=oa(a.body,t,n),o.body(e,i(J(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=J(a,o,s),f=ko(e,d);f?.kind===`Closure`?oa(f.body,t,n)(a,Vo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ja=(e,t,n)=>{let r=Sa(e),i=W(e)?void 0:sa(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=ro(s,e.name);f?.kind===`Thunk`?oa(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=oa(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},Ma=(e,t,n)=>{if(n.has(e))return;if(n.add(e),e.kind===`And`){let n=e.operands[0];if(n.kind!==`Apply`||n.operator!==`=`||W(n))return;let r=n.args[0],i=n.args[1];if(r.kind!==`FunctionApplication`||i.kind!==`String`)return;let a=r.args.length===1?r.args[0]:void 0;if(a?.kind!==`Identifier`||!W(a))return;let o=ca(r.function,t,!1);return o===void 0?void 0:{variable:o,argument:a.name,labels:/* @__PURE__ */ new Set([i.value])}}if(e.kind===`Or`){let r=e.operands.map(e=>Ma(e,t,n)),i=r[0],a=r.every(e=>e!==void 0&&e.variable===i?.variable&&e.argument===i.argument);if(!i||!a)return;let o=new Set(r.flatMap(e=>[...e?.labels??[]]));return{variable:i.variable,argument:i.argument,labels:o}}if(e.kind!==`Apply`||W(e))return;let r=Do(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=Ma(r.body,t,n),a=r.params.findIndex(e=>e.arity===0&&e.name===i?.argument),o=e.args[a];if(i&&o?.kind===`Identifier`&&W(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Na=(e,t,n)=>{let r=ro(t,n.argument),i=e.current?.[n.variable];if(r?.kind!==`Value`||typeof i!=`object`||i.kind!==`Function`)return;let a=i.domain.indexOf(r.value),o=a===-1?void 0:i.range[a];return typeof o==`string`?o:void 0},Pa=(e,t,n)=>{let r=e.map(e=>oa(e,t,n)),i=n?[]:e.map(e=>Ma(e,t,/* @__PURE__ */ new Set)),a=i.find(e=>e!==void 0),o=i.map(e=>e?.variable===a?.variable&&e?.argument===a?.argument?e?.labels:void 0);return(e,t,n,i,s,c)=>{let l=a&&Na(e,t,a);for(let a=0;a<r.length&&!e.isStopped;a++)(l===void 0||o[a]?.has(l)!==!1)&&r[a](e,t,n,i,s,c)}},Fa=(e,t,n)=>{let r=e=>oa(e,t,n);switch(e.kind){case`And`:return ba(e.operands,t,n);case`Or`:return Pa(e.operands,t,n);case`Exists`:{let i=r(e.body),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name;return(t,n,r,o,s,c)=>{let l=po(X(a.set,J(t,n,r)),a.set);for(let a=0;a<l.length&&!t.isStopped;a++)i(t,io(n,e,l[a]),r,o,s,c)}}let o=n?void 0:ya(e,t);if(o){let r=ba(o.rest,t,n);return(t,n,i,a,s,c)=>{let l=J(t,n,i),u=e.bounds.map(e=>po(X(e.set,l),e.set));if(u.some(e=>e.length===0))return;let d=(e,n)=>{if(e<0)r(t,n,i,a,s,c);else for(let r of u[e]){if(t.isStopped)return;let a=io(n,o.names[e],r);if(e===u.length-1){let e=J(t,a,i);if(!o.guards.every(t=>ia(t,e)))continue}d(e-1,a)}};d(u.length-1,n)}}return(t,n,r,a,o,s)=>{mo(e.bounds,J(t,n,r),e=>(i(t,e,r,a,o,s),t.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ws(e,t)!==void 0&&!_a(e.body,t,/* @__PURE__ */ new Set)?ga(e,ha):void 0;return(t,n,r,o,s,c)=>{let l=[];mo(e.bounds,J(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>ro(n,e)?.kind===`Value`)){u.every(n=>ia(e.body,J(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&za(r)?u.slice(n).every(n=>xa(e.body,J(t,n,r)))&&c(r,a):i(t,u[n],r,a,!0,(e,t)=>d(n+1,e,t))};d(0,r,o)}}case`If`:{let t=r(e.consequent),n=r(e.alternative);return(r,i,a,o,s,c)=>{let l=X(e.condition,J(r,i,a));(so(l,`an IF condition`)?t:n)(r,i,a,o,s,c)}}case`Case`:{let t=e.arms.map(e=>({condition:e.condition,body:r(e.body)})),n=e.other?r(e.other):void 0;return(r,i,a,o,s,c)=>{let l=J(r,i,a),d=t.find(e=>so(X(e.condition,l),`a CASE guard`))?.body??n;if(!d)throw new u(`In computing ${r.isInit?`initial`:`next`} states, TLC encountered a CASE with no conditions true.\n${qa(e)}`);d(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=$o(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Uo(e.definitions,J(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new u(`UNCHANGED appears in an initial predicate at ${Ga(e)}`)}:ka(e.expression,t);case`AngleAction`:{let t=r(e.action),n=Ta(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{ia(n,J(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,Ta(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ja(e,t,n);case`FunctionApplication`:return Oa(e,t,n);case`Apply`:{let i=e.args;if(aa(e.operator)){let r=la(i[0],t,n);if(r!==void 0)return e.operator===`=`?ba([e],t,n):Ca(e,()=>r);if(da(i[0],n))return Ea(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],J(t,n,r));(typeof c==`boolean`?c:so(c,`the left side of => at ${Ga(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return Aa(e,t,n)}default:return Sa(e)}},Ia=/* @__PURE__ */ new WeakMap,La=e=>{let t=Ia.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ia.set(e,n),n},Ra=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),za=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},Ba=(e,t)=>{let n=t.state;if(!n||!za(n))throw new u(`ENABLED needs a complete state at ${Ga(e)}`);let r=t.environment,i={environment:r,isInit:!1,current:n,isStopped:!1};return oa(e,r,!1)(i,t.scope,La(r),void 0,!1,()=>{i.isStopped=!0}),i.isStopped},Va=(t,r,i,a)=>{let o={environment:r,isInit:!0,current:void 0,isStopped:!1};oa(t,r,!0)(o,a,La(r),void 0,!1,t=>{if(!o.isStopped){if(!za(t))throw n(new u(`The initial predicate does not assign ${Ra(t,r).join(`, `)}`),e.error);o.isStopped=i(t)}})},Ha=(e,t,n)=>{let r=[];return Va(e,t,e=>(r.push(e),!1),n),r},Ua=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return oa(e,n,!1)(a,i,La(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!za(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Ra(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Wa=(e,t,n,r)=>{let i=[];return Ua(e,t,n,e=>(i.push(e),!1),r),i},Ga=e=>`line ${e.location.line}, column ${e.location.column}`,Ka=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,qa=e=>{let t=e.location.range;return t?Ka(t):Ga(e)},Ja=(e,t)=>new u(`A non-boolean expression (${fr(t)}) was used as the condition of an IF.\n${qa(e)}`),Ya=(e,t,n)=>{let r=e.location.range;if(r?.start.line===e.location.line&&r.start.column===e.location.column||!r){let r=e.operands[t],i=r.kind===`Number`?String(r.value):qa(r);return new u(`A non-boolean expression (${fr(n)}) was used as a formula in a conjunction.\n${i}`)}let i=e.operands[Math.max(t,1)].location.range?.end??r.end;return new u(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${fr(n)}.\n${qa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Xa=e=>t=>ln(t)?`Attempted to select nonexistent field "${e.field}" from the record\n${U(t)}\n${qa(e)}`:cn(t,e.field),Za=e=>t=>`A non-function (${fr(t)}) was applied as a function.\n${qa(e)}`,Qa=e=>t=>`Attempted to select field "${e.field}" from a non-record value ${U(t)}\n${qa(e)}`,$a=e=>t=>{if(!j(t))throw new u(`Attempted to apply the operator DOMAIN to a non-function\n(${fr(t)})\n${qa(e)}`);return mn(t)},eo=(e,t)=>(n,r)=>{if(!A(n)||F(n))throw new u(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${qa(e)}`);return t.evaluate([n,r])},to=t=>n(new u(`TLC can't handle real numbers: ${t.text} at ${Ga(t)}`),e.configParseError),no=t=>n(new u(`TLC can't handle a number this big: ${t.text??t.value} at ${Ga(t)}`),e.error),ro=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},io=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),ao=(e,t,n)=>{if(t.kind===`Name`)return io(e,t.name,n);let r=Xt(n);if(!r||r.length!==t.names.length)throw new u(`Cannot bind <<${t.names.join(`, `)}>> to ${U(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>io(e,t,r[n]),e)},oo=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),so=(e,t)=>{if(typeof e!=`boolean`)throw new u(`Expected ${t} to be a boolean but found ${U(e)}`);return e},co=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),lo=(e,t)=>{if(e.kind===`Prime`||e.kind===`Unchanged`||e.kind===`Enabled`)return!0;let n=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0;if(n!==void 0){if(t.variableIndex.has(n)||t.builtins.get(n)?.isStateLevel)return!0;let e=co(n,t);if(e&&!t.constants.has(n)&&!uo(e,t))return!0}return Un(e).some(e=>lo(e,t))},uo=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!lo(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>lo(e.set,t)));return t.constantLevels.set(e,r),r},fo=(e,t)=>{let n=`universally (\\A) or existentially (\\E) quantify over ${t}, because this would require checking an\ninfinite number of cases.`;if(typeof e==`object`&&e.kind===`SequenceSet`)return`In TLA+, Seq(S) represents the set of all finite sequences whose elements come from the set S. Even when S\nis a finite set, the number of possible sequences in Seq(S) is unbounded because sequences can have any\nfinite length (e.g., length 0, 1, 2, and so on). As a result, TLC cannot evaluate expressions that\n${n} Note that for a finite set of sequences s, TLC handles s \\subseteq Seq(S).\nSee https://explain.tlapl.us/seq-unenumerable for additional details.`;if(typeof e==`object`&&e.kind===`InfiniteSet`&&e.name!==`STRING`&&e.name!==`Any`)return`The set ${e.name} contains infinitely many elements. As a result, TLC cannot evaluate expressions that\n${n} Note that TLC handles set membership like T \\subseteq ${e.name} for any finite set T.`},po=(e,t)=>{if(!A(e)||F(e)){let n=U(e),r=fo(e,n);throw new u(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${qa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${qa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Tt(e):B(e).elements},mo=(e,t,n)=>{let r=e.map(e=>po(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,ao(a,e[t].pattern,n)));return i(e.length-1,t.scope)},ho=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},go=(e,t,n)=>{if(e.length===1)return ao(t,e[0].pattern,n);let r=Xt(n)??[];return e.reduce((e,t,n)=>ao(e,t.pattern,r[n]),t)},_o=/* @__PURE__ */ new WeakMap,vo=(e,t)=>{let n=_o.get(t);if(n)return n;let r=t.location.range;if(!r)return;let i={parameters:e.flatMap((t,n)=>n>0&&e[n-1].set===t.set?[]:[{index:n,name:t.pattern.kind===`Name`?t.pattern.name:`<<${t.pattern.names.join(`, `)}>>`}]),body:Ka(r)};return _o.set(t,i),i},yo=(e,t,n)=>{let r=vo(e,t);if(!r)return;let i=e.length>1&&typeof n==`object`&&n.kind===`ProductSet`?n.factors:[n];return{parameters:r.parameters.map(e=>({name:e.name,domain:i[e.index]})),body:r.body}},bo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=Cn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(Be(a[t],e))return a[t+1]}let o=t(e,i),s=r.get(n);return s===void 0?r.set(n,[e,o]):s.push(e,o),o}};return i},xo=(e,t)=>{let n=ho(e.bounds,t),r=bo(n,(n,r)=>X(e.body,oo(t,go(e.bounds,io(void 0,e.name,r),n))),yo(e.bounds,e.body,n));return e.bounds.length===1?nn(r):r},So=(e,t)=>e.kind===`Function`?xo(e,t):X(e.body,t),Co=(e,t)=>{let n=uo(e,t.environment),r=n?t.environment.definitionValues.get(e):void 0;if(r!==void 0)return r;if(e.kind===`Operator`&&e.params.length>0)throw new u(`Operator ${e.name} requires ${e.params.length} arguments`);let i=oo(t,void 0);if(!n)return So(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>So(e,i));return t.environment.definitionValues.set(e,a),a},wo=(e,t)=>{let n=e.state===t.state&&e.next===t.next;if(n&&e.cache!==void 0)return e.cache;let r=X(e.expression,oo(t,e.scope));return n&&(e.cache=r),r},To=/* @__PURE__ */ new WeakMap,Eo=e=>{let t=To.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return To.set(e,n),n},Do=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return Do(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?Eo(r):t.builtins.get(e)},Oo=/* @__PURE__ */ new WeakMap,ko=(e,t)=>{if(W(e))return Ao(e.operator,t);let n=Oo.get(e),r=n===void 0?Do(e.operator,t.environment)??null:n;return n===void 0&&Oo.set(e,r),r??Ao(e.operator,t)},Ao=(e,t)=>{let n=ro(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return Do(e,t.environment)},jo=e=>e.kind===`Closure`?e.params.length:e.arity,Mo=(e,t)=>{if(e.kind===`Lambda`)return{kind:`Closure`,name:`LAMBDA`,params:e.params.map(e=>({name:e,arity:0})),body:e.body,scope:t.scope};let n=e.kind===`OperatorReference`?e.operator:e.kind===`Identifier`?e.name:void 0,r=n===void 0?void 0:Ao(n,t);if(!r)throw new u(`Expected an operator argument at ${Ga(e)}`);return r},No=(e,t,n)=>{if(e.kind===`Builtin`)return e.evaluate(t,n);if(e.kind===`Operator`)return e.call(t);let r=e.params.reduce((e,n,r)=>io(e,n.name,t[r]),e.scope);return X(e.body,oo(n,r))},Po=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:jo(e),call:n=>No(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},Fo=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=Ao(e.name,t);return n!==void 0&&jo(n)>0},Io=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),Lo=(e,t)=>{let n=t.variableIndex.get(e);if(n!==void 0)return{kind:`Variable`,index:n};let r=t.constants.get(e);if(r!==void 0)return{kind:`Value`,value:r};let i=t.substitutions.get(e);if(i!==void 0)return Lo(i,t);let a=t.definitions.get(e);if(a)return{kind:`Definition`,definition:a};let o=t.builtins.get(e);if(o&&o.arity===0&&!o.isStateLevel)return{kind:`Value`,value:o.evaluate([])}},Ro=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Go(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=W(e)?void 0:Lo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=ro(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Io(e,t)}}return t=>Io(e,t)},zo=/* @__PURE__ */ new WeakMap,Bo=(e,t)=>{let n=zo.get(e);if(n)return n;let r=Ro(e,t);return zo.set(e,r),r},Vo=(e,t,n)=>{if(t.length!==e.params.length)throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`);let r=e.scope;for(let i=0;i<t.length;i++){let a=e.params[i],o=a.arity>0?{kind:`Operator`,operator:Mo(t[i],n)}:Bo(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},Ho=(e,t,n)=>e.kind===`Closure`?X(e.body,oo(n,Vo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(ys(t,n),n):e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n),Uo=(e,t,n)=>e.reduce((e,r)=>{let i={name:r.name,binding:{kind:`Value`,value:!1},parent:e};return r.kind===`Function`?(i.binding={kind:`Thunk`,expression:{kind:`FunctionConstruction`,bounds:r.bounds,body:r.body,location:r.location,isRecursive:!0},scope:i,state:t.state,next:t.next,cache:void 0},i):r.params.length>0||n?.has(r)?(i.binding={kind:`Operator`,operator:{kind:`Closure`,name:r.name,params:r.params,body:r.body,scope:i}},i):(i.binding={kind:`Thunk`,expression:r.body,scope:i,state:t.state,next:t.next,cache:void 0},i)},t.scope),Wo=(e,t,n)=>{let r=ro(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return wo(r,n);if(r?.kind===`Operator`){if(jo(r.operator)===0)return No(r.operator,[],n);throw new u(`Operator ${e} is used without arguments`)}let i=Lo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw Rs(e,t);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return Co(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new u(`Unknown identifier ${e} at ${Ga(t)}`)},Go=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,Ko=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),qo=(e,t)=>Ko.has(e)||t.builtins.get(e)?.isStateLevel===!0,Jo={builtins:Ko,definitions:/* @__PURE__ */ new WeakMap},Yo={builtins:new Set([...Ko].filter(e=>e!==`Assert`)),definitions:/* @__PURE__ */ new WeakMap},Xo=(e,t)=>e.kind===`Operator`&&e.params.length===0&&uo(e,t),Zo=(e,t,n)=>{let r=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(r!==void 0&&!W(e)){let e=co(r,t),i=n.builtins.has(r)||t.builtins.get(r)?.isStateLevel===!0;if(!e&&i||e&&!Xo(e,t)&&Qo(e,t,n))return!0}return Un(e).some(e=>Zo(e,t,n))},Qo=(e,t,n=Jo)=>{let r=n.definitions.get(e);if(r!==void 0)return r;n.definitions.set(e,!1);let i=Zo(e.body,t,n)||e.kind===`Function`&&e.bounds.some(e=>Zo(e.set,t,n));return n.definitions.set(e,i),i},$o=(e,t)=>new Set(e.filter(e=>Qo(e,t))),es=e=>[...e.definitions.values()].some(t=>Qo(t,e)),ts=e=>[...e.definitions.values()].some(t=>Qo(t,e,Yo)),ns=/* @__PURE__ */ new WeakMap,rs=(e,t)=>{let n=ns.get(e);if(n!==void 0)return n;let r=ss(e,t,as);return ns.set(e,r),r},is=/* @__PURE__ */ new Set([`Number`,`String`,`Identifier`,`Apply`,`And`,`Or`,`If`,`Case`,`SetEnumeration`,`Tuple`,`FunctionSet`,`Record`,`RecordSet`,`FunctionApplication`,`FieldAccess`,`Product`,`SetFilter`,`SetMap`,`Forall`,`Exists`,`Choose`,`FunctionConstruction`,`Let`]),as=/* @__PURE__ */ new Set,os=(e,t,n)=>n.size===0?rs(e,t):ss(e,t,n),ss=(e,t,n)=>{if(!is.has(e.kind))return!1;if(e.kind===`Identifier`&&W(e))return n.has(e.name);if(W(e))return!1;if(e.kind===`Identifier`){let n=Lo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Xo(n.definition,t)}if(e.kind===`Apply`){if(qo(e.operator,t)||e.operator===`~>`)return!1;let r=Do(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=co(e.operator,t);if(!n||!uo(n,t)||Qo(n,t))return!1}return!(r.kind===`Closure`?r.params:[]).some(e=>e.arity>0)&&e.args.every(e=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&os(e,t,n))}let r={value:!0};return ki(e,(e,i)=>(r.value&&=os(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},cs=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,ls=e=>{let t={value:void 0};return n=>{if(t.value!==void 0)return t.value;let r=e(n);return t.value=r,r}},us=(e,t)=>{if(e.kind===`Identifier`&&W(e))return!t.has(e.name);if(W(e))return!0;let n={value:!1};return ki(e,(e,r)=>(n.value||=us(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},ds=(e,t)=>xi.has(e)&&cs(e)&&Ws(e,t)!==void 0&&!us(e,as),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Vs(e,t),i=cs(e)&&rs(e,t)?ls(r):ds(e,t)?gs(r):r;return t.evaluators.set(e,i),i},fs=/* @__PURE__ */ new WeakMap,ps=(e,t)=>{let n=fs.get(e);if(n)return n;let r=(cs(e)&&rs(e,t)?void 0:Mr(e,t))??Y(e,t);return fs.set(e,r),r},X=(e,t)=>ps(e,t.environment)(t),ms=(e,t)=>{let n=e.name,r=W(e)?void 0:Lo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw Rs(n,e);return i}}if(r?.kind===`Value`){let e=r.value;return()=>e}if(r?.kind===`Definition`&&r.definition.kind===`Operator`){let e=r.definition;if(e.params.length===0&&uo(e,t))return t=>Co(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=ps(e.body,t),n.body(oo(r,void 0)));return hs(e,t)?gs(r):r}return t=>Co(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>Co(e,t);return uo(e,t)||!hs(e,t)?n:gs(n)}return t=>Wo(n,e,t)},hs=(e,t)=>Ws(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ws(e.set,t)!==void 0)),gs=e=>{let t={recent:void 0,recentValue:void 0,previous:void 0,previousValue:void 0};return n=>{let r=n.state;if(r!==void 0&&t.recent===r&&t.recentValue!==void 0)return t.recentValue;if(r!==void 0&&t.previous===r&&t.previousValue!==void 0)return t.previousValue;let i=e(n);return t.previous=t.recent,t.previousValue=t.recentValue,t.recent=r,t.recentValue=i,i}},_s=(e,t)=>{let n=Do(e,t);return n!==void 0&&jo(n)>0},vs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!W(e)&&_s(e.name,t)),ys=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),bs=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(ys(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&vs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&vs(t[0],n)&&vs(t[1],n);if(i&&a){let e=Y(t[0],n),r=Y(t[1],n);return t=>i(e(t),r(t))}return n=>e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n)},xs=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`)};let r=e.params,i=t.map((e,t)=>r[t].arity>0?void 0:Bo(e,n));return n=>{let a=e.scope;for(let e=0;e<t.length;e++){let o=i[e],s=o?o(n):{kind:`Operator`,operator:Mo(t[e],n)};a={name:r[e].name,binding:s,parent:a}}return a}},Ss=/* @__PURE__ */ new WeakMap,Cs=e=>{let t=Ss.get(e);if(t)return t;let n={calls:/* @__PURE__ */ new Map,size:0};return Ss.set(e,n),n},ws=(e,t)=>{let n=[];for(let r=e;n.length<t;r=r?.parent){if(r?.binding.kind!==`Value`)return;n.push(r.binding.value)}return n},Ts=(e,t)=>e.every((e,n)=>Be(e,t[n])),Es=(e,t)=>{let n=co(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&uo(n,t)&&!Qo(n,t)},Ds=(e,t,n,r)=>{let i=xs(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=ps(e.body,n),a.body({environment:n,scope:t,state:r.state,next:r.next}));if(!r)return e=>o(i(e),e);let s=Cs(e);return e=>{let n=i(e),r=ws(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=vn(a,Cn(e));let c=s.calls.get(a),l=c?.find(e=>Ts(e.args,r));if(l)return l.value;let u=o(n,e);return s.size<65536&&(s.size++,c?c.push({args:r,value:u}):s.calls.set(a,[{args:r,value:u}])),u}},Os=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!so(e(t),`the left side of =>`)||so(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new u(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=W(e)?void 0:Do(e.operator,t);if(r?.kind===`Closure`)return Ds(r,n,t,Es(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=$a(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return Si.has(e)||lo(n[0],t)?i:e=>e.scope===void 0?r.evaluate([i(e)]):i(e)}if(r?.kind===`Builtin`&&r.name===`\\subseteq`&&n.length===2){let i=Y(n[0],t),a=Y(n[1],t),o=eo(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?bs(r,n,t):r=>{let i=Ao(e.operator,r);if(i)return Ho(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new u(`${e.operator} is a function; apply it with brackets`):e.operator===`\\cdot`?new u(`The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.`):new u(`Unknown operator ${e.operator} at ${Ga(e)}`)}},ks=(e,t)=>{let n=e.kind===`Forall`,r=n?`the body of \\A`:`the body of \\E`,i=Y(e.body,t),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name,o=Y(a.set,t);return t=>{let s=po(o(t),a.set);for(let a=0;a<s.length;a++){let o=so(i(oo(t,io(t.scope,e,s[a]))),r);if(o!==n)return o}return n}}let o=e.bounds;if(o.every(e=>e.pattern.kind===`Name`)){let e=o.map(e=>e.pattern.kind===`Name`?e.pattern.name:``),a=o.map(e=>Y(e.set,t));return t=>{let s=a.map((e,n)=>po(e(t),o[n].set)),c=(a,o)=>{if(a<0)return so(i(oo(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,io(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>mo(o,e,t=>so(i(oo(e,t)),r)!==n)?!n:n},As=(e,t)=>{if(e.kind===`Tuple`)return R(e.names.map(e=>{let n=ro(t,e);return n?.kind===`Value`&&n.value}));let n=ro(t,e.name);return n?.kind===`Value`?n.value:void 0},js=/* @__PURE__ */ new WeakSet,Ms=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&js.add(e)},Ns=(e,t)=>{let n=js.has(e),r=Y(e.body,t),i=e.bounds,a=i[0],o=i.length===1&&a.pattern.kind===`Name`,s=Y(a.set,t);return t=>{let c=o?s(t):ho(i,t),l=typeof c==`object`&&c.kind===`ProductSet`&&i.length>1?c.factors:[c];if(e.isRecursive||n||!l.every(pe)){let n=bo(c,e=>r(oo(t,go(i,t.scope,e))),yo(i,e.body,c));return i.length===1?nn(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=Dt(c);return nn(Le(n,n.map(n=>r(oo(t,io(t.scope,e,n))))))}let u=[];return mo(i,t,e=>{let n=i.length===1?As(a.pattern,e)??!1:R(i.map(t=>As(t.pattern,e)??!1));return u.push([n,r(oo(t,e))]),!1}),yt(u)}},Ps=(e,t)=>{let n=js.has(e);n&&Ms(e.function);let r=Y(e.function,t),i=e.updates.map(e=>({path:e.path.map(e=>e.kind===`Field`?{kind:`Field`,name:e.name,args:[]}:{kind:`Index`,name:``,args:e.args.map(e=>Y(e,t))}),value:Y(e.value,t)})),a=(e,t)=>e.kind===`Field`?e.name:e.args.length===1?e.args[0](t):R(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(oo(i,io(i.scope,`@`,e))):hn(e,a(t[r],i),e=>o(e,t,r+1,i,s),n&&r===0);return e=>{let t=r(e);for(let n of i)t=o(t,n.path,0,e,n.value);return t}},Fs=(e,t)=>{if(e.kind===`SetFilter`){let n=Y(e.predicate,t),r=Y(e.bound.set,t),i=e.bound.pattern;return t=>{let a=r(t),o=e=>so(n(oo(t,ao(t.scope,i,e))),`a set filter predicate`);if(!pe(a)){if(A(a))return{kind:`FilteredSet`,base:a,test:o};throw new u(`TLC encountered a non-enumerable quantifier bound ${U(a)} at ${Ga(e.bound.set)}`)}return Ie(B(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return mo(e.bounds,t,e=>(r.push(n(oo(t,e))),!1)),mt(r)}}let n=e.set;if(!n)return()=>{throw new u(`TLC cannot evaluate an unbounded CHOOSE at ${Ga(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(mo([i],t,i=>so(r(oo(t,i)),`the body of CHOOSE`)?(n.value=As(e.pattern,i),!0):!1),n.value===void 0)throw new u(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${qa(e)}`);return n.value}},Is=e=>[...e].sort((e,t)=>L(e.name,t.name)),Ls=e=>e.some((t,n)=>n>0&&e[n-1].name===t.name),Rs=(e,t)=>new u(`In evaluation, the identifier ${e} is either undefined or not an operator.\n${qa(t)}`),zs=e=>new u(`A primed expression appears outside an action at ${Ga(e)}`),Bs=(e,t)=>{let n=Is(e),r=Ls(n),i=n.map(e=>Y(e.value,t));if(r)return e=>yt(n.map((t,n)=>[t.name,i[n](e)]));let a=n.map(e=>e.name),o=e.map(e=>e.name);return o.some((e,t)=>e!==a[t])?e=>ye(a,i.map(t=>t(e)),o):e=>Le(a,i.map(t=>t(e)))},Vs=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Go(t)?()=>t:()=>{throw no(e)}}case`Decimal`:return()=>{throw to(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return ms(e,t);case`Apply`:return Os(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new u(`An operator cannot be used as a value at ${Ga(e)}`)};case`And`:{let t=e.operands.map(n);return n=>{for(let r=0;r<t.length;r++){let i=t[r](n);if(typeof i!=`boolean`)throw Ya(e,r,i);if(!i)return!1}return!0}}case`Or`:{let t=e.operands.map(n);return e=>{for(let n=0;n<t.length;n++)if(so(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw zs(e);return t({environment:n.environment,scope:n.scope,state:n.next,next:void 0})}}case`Unchanged`:{let t=n({kind:`Prime`,expression:e.expression,location:e.location}),r=n(e.expression);return e=>Re(t(e),r(e))}case`If`:{let t=n(e.condition),r=n(e.consequent),i=n(e.alternative);return n=>{let a=t(n);if(typeof a!=`boolean`)throw Ja(e,a);return a?r(n):i(n)}}case`Case`:{let t=e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),r=e.other?n(e.other):void 0;return n=>{for(let e of t)if(so(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new u(`Attempted to evaluate a CASE with no conditions true.\n${qa(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(oo(n,Uo(e.definitions,n)))}case`Forall`:case`Exists`:return ks(e,t);case`UnboundedQuantifier`:return()=>{throw new u(`TLC cannot evaluate an unbounded quantifier at ${Ga(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return Fs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>mt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>R(t.map(t=>t(e)))}case`FunctionConstruction`:return Ns(e,t);case`FunctionSet`:{let t=n(e.domain),r=n(e.range);return e=>({kind:`FunctionSet`,domain:t(e),range:r(e)})}case`Record`:return Bs(e.fields,t);case`RecordSet`:{let t=Is(e.fields),r=t.map(e=>e.name),i=t.map(e=>n(e.value));return e=>({kind:`RecordSet`,names:r,sets:i.map(t=>t(e))})}case`Except`:return Ps(e,t);case`ExceptAt`:return t=>{let n=ro(t.scope,`@`);if(n?.kind!==`Value`)throw new u(`@ appears outside an EXCEPT at ${Ga(e)}`);return n.value};case`FunctionApplication`:{Ms(e.function);let t=n(e.function),r=e.args.map(n),i=Za(e);if(r.length===1){let e=r[0];return n=>fn(t(n),e(n),void 0,i)}return e=>fn(t(e),R(r.map(t=>t(e))),void 0,i)}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Xa(e),a=Qa(e);return e=>fn(t(e),r,i,a)}case`Product`:{let t=e.factors.map(n);return e=>({kind:`ProductSet`,factors:t.map(t=>t(e))})}case`BoxAction`:case`AngleAction`:{let t=n(e.action),r=n({kind:`Unchanged`,expression:e.subscript,location:e.location}),i=e.kind===`BoxAction`;return e=>i?so(t(e),`an action`)||r(e):so(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>Ba(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new u(`The subexpression reference at ${Ga(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new u(`TLC cannot evaluate a temporal formula in a state at ${Ga(e)}`)}}},Hs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),Us=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ws=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Hs.has(e.kind))return!1;let a=Us(e),o=(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`;if(a!==void 0&&!o&&!t.constants.has(a)){let e=t.substitutions.get(a)??a,o=t.definitions.get(e);if(!o&&qo(e,t))return!1;let s=t.variableIndex.get(a);if(s!==void 0&&n.add(s),o&&!r.has(o)&&(r.add(o),!i(o.body)||o.kind!==`Operator`&&!o.bounds.every(e=>i(e.set))))return!1}return Un(e).every(i)};return i(e)?[...n]:void 0},Gs=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
23
+ `)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;wr.value??=Cr();let o=wr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Nr={kind:`Local`},Pr=/* @__PURE__ */ new WeakMap,Fr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Ir=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Lr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Rr=e=>e.kind===`Operator`?e.params.length:0,zr=e=>e.name.includes(`!`),Br=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},Vr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Hr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Ur=e=>e.kind===`Name`?[e.name]:e.names,Wr=e=>e.flatMap(e=>Ur(e.pattern)),Gr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Lr(n.location,t.location))?n:void 0},Kr=(e,t)=>{let n=Gr(e,t);return n?{key:t.getKey(n),arity:Rr(n)}:t.imported.get(e)},qr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Jr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),Yr=(e,t,n,r)=>{let i=e.location,a=Pr.get(e);if(a!==void 0)return a;if(Fr(t))return e.kind===`Apply`?e.args.length:0;if(Jr(t))return 0;let o=Br(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Nr),o)return o.arity;let s=Kr(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,Pr.set(e,s.arity),s.arity;let l=t.slice(0,t.indexOf(`!`));if(l!==``&&(Br(n,l)!==void 0||Kr(l,r)!==void 0||r.facts.has(l)))return e.kind===`Apply`?e.args.length:0;let u=`at line ${i.line}, column ${i.column}`;throw new c(qr(t)?`Could not find declaration or definition of symbol '${t}' ${u}`:`Unknown operator: \`${t}' ${u}`)},Xr=(e,t,n)=>(e.forEach(e=>q(e.set,t,n)),e.reduce((e,t)=>Vr(e,Ur(t.pattern)),t)),Zr=(e,t)=>e.kind===`Identifier`&&e.name===t&&W(e)||Un(e).some(e=>Zr(e,t)),Qr=(e,t)=>{e.kind===`FunctionConstruction`&&Zr(e.body,t)&&(e.isRecursive=!0),Un(e).forEach(e=>Qr(e,t))},$r=(e,t,n)=>{let r=Vr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(q(e.body,Xr(e.bounds,r,n),n),Qr(e.body,e.name)):q(e.body,Hr(r,e.params),n)},ei=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Rr(t),parent:e}),t);return e.reduce((e,t)=>(zr(t)||$r(t,e,n),{name:t.name,arity:Rr(t),parent:e}),r)},ti=(e,t,n,r,i)=>{let a=e.kind===`Operator`?e.params[t]:void 0,o=n.location;if(a?.arity===0&&r>0)throw new c(`Operator used in argument number ${t+1} has incorrect number of arguments at line ${o.line}, column ${o.column}`);let s=i?.kind===`Operator`&&i.params.some(e=>e.arity>0);if(a!==void 0&&a.arity>0&&(r!==a.arity||s))throw new c(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},ni=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,ri=(e,t,n)=>{let r=Br(t,e.operator)?void 0:Gr(e.operator,n),i=Yr(e,e.operator,t,n);if(i!==e.args.length)throw new c(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=ni(e),o=a===void 0||Br(t,a)?void 0:Gr(a,n),s=ii(e,t,n);r&&ti(r,i,e,s,o)})},ii=(e,t,n)=>e.kind===`Identifier`?Yr(e,e.name,t,n):e.kind===`OperatorReference`?Yr(e,e.operator,t,n):(q(e,t,n),e.kind===`Lambda`?e.params.length:0),q=(e,t,n)=>{let r=e=>e.forEach(e=>q(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=Yr(e,e.name,t,n);if(r>0)throw new c(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:Yr(e,e.operator,t,n);return;case`Apply`:ri(e,t,n);return;case`Lambda`:q(e.body,Vr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:q(e.expression,t,n);return;case`If`:r([e.condition,e.consequent,e.alternative]);return;case`Case`:r(e.arms.flatMap(e=>[e.condition,e.body])),e.other&&q(e.other,t,n);return;case`Let`:q(e.body,ei(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:q(e.body,Xr(e.bounds,t,n),n);return;case`UnboundedQuantifier`:q(e.body,Vr(t,e.names),n);return;case`Choose`:e.set&&q(e.set,t,n),q(e.body,Vr(t,Ur(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:q(e.predicate,Xr([e.bound],t,n),n);return;case`SetMap`:q(e.expression,Xr(e.bounds,t,n),n);return;case`FunctionSet`:r([e.domain,e.range]);return;case`Record`:case`RecordSet`:if(new Set(e.fields.map(e=>e.name)).size<e.fields.length)throw new c(`Non-unique fields in constructor at line ${e.location.line}, column ${e.location.column}`);r(e.fields.map(e=>e.value));return;case`Except`:r([e.function,...e.updates.flatMap(e=>[...e.path.flatMap(e=>e.kind===`Index`?e.args:[]),e.value])]);return;case`FunctionApplication`:r([e.function,...e.args]);return;case`FieldAccess`:q(e.record,t,n);return;case`Product`:r(e.factors);return;case`BoxAction`:case`AngleAction`:r([e.action,e.subscript]);return;case`WeakFairness`:case`StrongFairness`:r([e.subscript,e.action]);return;case`Label`:q(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>ii(e,t,n))}},ai=(e,t,n)=>(e.set&&q(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),oi=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?ai(t,e,n):(t.kind===`AssumeProve`?oi(t,e,n):q(t,e,n),e),t);return q(e.conclusion,r,n),r},si=(e,t,n)=>e.kind===`AssumeProve`?oi(e,t,n):(q(e,t,n),t),ci=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>li(t,e,n),t)},li=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=si(r.statement,t,n);return ci(e.proof,i,n),r.isSuffices?i:t}case`Define`:return ei(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return q(r.expression,t,n),ci(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>q(e,t,n)),t;case`Take`:return Vr(Xr(r.bounds,t,n),r.names);case`Pick`:{let i=Vr(Xr(r.bounds,t,n),r.names);return q(r.body,i,n),ci(e.proof,t,n),i}default:return ci(e.proof,t,n),t}},ui=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:si(e.statement,void 0,t);ci(e.proof,n,t)},di=(e,t)=>new c(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),fi=(e,t,n,r)=>t.reduce((e,t)=>{if(Br(e,t)||n(t))throw di(t,r);return{name:t,arity:0,parent:e}},e),pi=(e,t,n,r)=>(e.forEach(e=>hi(e.set,t,n)),e.reduce((e,t)=>fi(e,Ur(t.pattern),n,r),t)),mi=(e,t,n)=>{if(e.kind===`Function`){let r=Vr(t,[e.name]),i=pi(e.bounds,r,n,e.location);hi(e.body,i,n);return}let r=fi(t,e.params.map(e=>e.name),n,e.location);hi(e.body,r,n)},hi=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:hi(e.body,fi(t,e.params,n,r),n);return;case`Let`:{let i=e.definitions.filter(e=>e.isRecursive).map(e=>e.name),a=e.definitions.reduce((e,t)=>{if(Br(e,t.name)||n(t.name))throw di(t.name,t.location);return mi(t,Vr(e,i),n),Vr(e,[t.name])},t);hi(e.body,fi(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:hi(e.body,pi(e.bounds,t,n,r),n);return;case`SetFilter`:hi(e.predicate,pi([e.bound],t,n,r),n);return;case`SetMap`:hi(e.expression,pi(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:hi(e.body,fi(t,e.names,n,r),n);return;case`Choose`:e.set&&hi(e.set,t,n),hi(e.body,fi(t,Ur(e.pattern),n,r),n);return;default:Un(e).forEach(e=>hi(e,t,n))}},gi=e=>[...e.definitions.map(e=>({name:e.name,kind:`Definition`,location:e.location,isRecursive:e.isRecursive})),...e.instances.flatMap(e=>e.name===void 0?[]:[{name:e.name,kind:`Definition`,location:e.location,isRecursive:!1}]),...e.constants.map(e=>({name:e.name,kind:`Constant`,location:e.location,isRecursive:!1})),...e.variables.map(e=>({name:e.name,kind:`Variable`,location:e.location,isRecursive:!1}))],_i=(e,t)=>{let n=/* @__PURE__ */ new Map;gi(e).sort((e,t)=>Lr(e.location,t.location)?-1:1).forEach(e=>n.set(e.name,[...n.get(e.name)??[],e]));let r=(e,n)=>{if(!t.imported.has(e)&&!Ir.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Lr(r,n)};n.forEach(e=>e.forEach((n,i)=>{let a=e.slice(0,i).some(e=>e.kind===`Definition`||n.kind===`Definition`||e.kind!==n.kind),o=t.imported.get(n.name)?.declaration===n.kind;if(a||!o&&r(n.name,n.location))throw di(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Lr(t.location,e))||r(t,e);e.definitions.forEach(e=>mi(e,void 0,i(e.location))),e.assumptions.forEach(e=>hi(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=fi(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>hi(e.expression,n,t))})},vi=(e,t)=>{let n=new Map([...Ir,...t]);return e.constants.forEach(e=>n.set(e.name,{key:e.name,arity:e.arity})),e.variables.forEach(e=>n.set(e.name,{key:e.name,arity:0})),n},yi=(e,t,n,r)=>{let i=Hr(void 0,e.params),a={imported:vi(n,r.imported),definitions:r.definitions,getKey:r.getKey,location:e.location,self:void 0,facts:/* @__PURE__ */ new Set},o=new Map(e.substitutions.map(e=>[e.name,e.expression])),s=`in the instance of ${e.module} at line ${e.location.line}, column ${e.location.column}`;return o.forEach((e,n)=>{if(t.every(e=>e.name!==n))throw new c(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Br(i,n)&&!Kr(n,a))throw new c(`Substitution missing for symbol ${n} ${s}`);let r=o.get(n)??{kind:`Identifier`,name:n,location:e.location};if(t.arity===0||r.kind!==`Identifier`)return q(r,i,a),[n,r];if(Yr(r,r.name,i,a)!==t.arity)throw new c(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},bi=(e,t)=>{let n=vi(e,t.imported),r=new Set([...e.theorems,...e.assumptions].flatMap(e=>e.name??[])),i=(e,i)=>({imported:n,definitions:t.definitions,getKey:t.getKey,location:e,self:i,facts:r});e.definitions.forEach(e=>$r(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>q(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ui(e,i(e.location,void 0)))},xi=/* @__PURE__ */ new WeakSet,Si=/* @__PURE__ */ new WeakSet,Ci=(e,t,n=/* @__PURE__ */ new Set)=>{let r=ki(e,(e,r)=>Ci(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&W(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&W(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},wi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),Ti=/* @__PURE__ */ new WeakMap,Ei=e=>Ti.get(e)??e.name,Di=(e,t)=>(Ei(t)!==e.name&&Ti.set(e,Ei(t)),e),Oi=(e,t)=>Di(e.kind===`Function`?{...e,bounds:wi(e.bounds,e=>t(e,[])),body:t(e.body,Wr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),ki=(e,t)=>{let n=e=>t(e,[]);switch(e.kind){case`Number`:case`Decimal`:case`String`:case`Identifier`:case`OperatorReference`:case`ExceptAt`:return e;case`Apply`:return{...e,args:e.args.map(n)};case`Lambda`:return{...e,body:t(e.body,e.params)};case`And`:case`Or`:return{...e,operands:e.operands.map(n)};case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:return{...e,expression:n(e.expression)};case`If`:return{...e,condition:n(e.condition),consequent:n(e.consequent),alternative:n(e.alternative)};case`Case`:return{...e,arms:e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),other:e.other?n(e.other):void 0};case`Let`:{let n=e.definitions.map(e=>e.name);return{...e,definitions:e.definitions.map(e=>Oi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:wi(e.bounds,n),body:t(e.body,Wr(e.bounds))};case`UnboundedQuantifier`:return{...e,body:t(e.body,e.names)};case`Choose`:return{...e,set:e.set?n(e.set):void 0,body:t(e.body,Ur(e.pattern))};case`SetEnumeration`:case`Tuple`:return{...e,elements:e.elements.map(n)};case`SetFilter`:return{...e,bound:{pattern:e.bound.pattern,set:n(e.bound.set)},predicate:t(e.predicate,Ur(e.bound.pattern))};case`SetMap`:return{...e,bounds:wi(e.bounds,n),expression:t(e.expression,Wr(e.bounds))};case`FunctionSet`:return{...e,domain:n(e.domain),range:n(e.range)};case`Record`:case`RecordSet`:return{...e,fields:e.fields.map(e=>({name:e.name,value:n(e.value)}))};case`Except`:return{...e,function:n(e.function),updates:e.updates.map(e=>({path:e.path.map(e=>e.kind===`Index`?{kind:`Index`,args:e.args.map(n)}:e),value:n(e.value)}))};case`FunctionApplication`:return{...e,function:n(e.function),args:e.args.map(n)};case`FieldAccess`:return{...e,record:n(e.record)};case`Product`:return{...e,factors:e.factors.map(n)};case`BoxAction`:case`AngleAction`:return{...e,action:n(e.action),subscript:n(e.subscript)};case`WeakFairness`:case`StrongFairness`:return{...e,subscript:n(e.subscript),action:n(e.action)};case`Label`:return{...e,body:n(e.body)};case`Subexpression`:return{...e,selectors:e.selectors.map(e=>`args`in e?{...e,args:e.args.map(n)}:e)}}},Ai=`LAMBDA`,ji=(e,t=/* @__PURE__ */ new Set)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0,r=n===void 0||t.has(n)?[]:[n];return ki(e,(e,n)=>(r.push(...ji(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Mi=e=>{let t=/* @__PURE__ */ new Map;return n=>{if(!e.substitutedNames.has(n))return n;let r=t.get(n)??e.instantiation.createName(n);return t.set(n,r),r}},Ni=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),Pi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Fi=(e,t)=>e.map(e=>({pattern:Pi(e.pattern,t),set:e.set})),Ii=(e,t)=>e.kind===`Function`?{...e,bounds:Fi(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Li=(e,t,n)=>{let r=e=>t.get(e)??e;switch(e.kind){case`Identifier`:return{...e,name:r(e.name)};case`Apply`:case`OperatorReference`:return{...e,operator:r(e.operator)};case`Label`:return{...e,params:e.params.map(r)};case`Lambda`:return{...e,params:e.params.map(n)};case`Let`:return{...e,definitions:e.definitions.map(e=>Di({...Ii(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Fi(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:Pi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:Pi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Ri=e=>`${e}@ENABLED`,zi=(e,t)=>e.modes.get(t)??e,Bi=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!W(e)&&t.instantiation.variables.has(e.name),Vi=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>Vi(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Bi(e,t,n))return[e.name];let r=W(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?Vi(r.body,t,/* @__PURE__ */ new Map):void 0},Hi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Bi(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Ri(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:qi(e.expression,zi(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[qi(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:qi(e.subscript,zi(t,`Primed`),n),location:r},qi(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=Vi(e.expression,t,n);if(r!==void 0)return{kind:`And`,operands:r.map(r=>({kind:`Apply`,operator:`=`,args:[{kind:`Prime`,expression:{kind:`Identifier`,name:Ri(r),location:e.location},location:e.location},qi({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ui=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Wi=(e,t)=>e.map(e=>Ui(e.name,t)),Gi=(e,t,n,r)=>{let i=Array.from({length:n},(e,t)=>`argument${t}`);return{kind:`Lambda`,params:i,body:{kind:`Apply`,operator:e,args:[...Wi(t,r),...i.map(e=>Ui(e,r))],location:r},location:r}},Ki=(e,t)=>e.instantiation.isParameterized(t)?Yi(e,t):void 0,qi=(e,t,n)=>{let r=e=>qi(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!W(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:qi(e.expression,zi(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:qi(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},zi(t,`Enabled`),n)};let s=Hi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return xi.add(n),n;let r=Ki(t,e.name);if(r===void 0)return e;if(a.length===0)return{...e,name:r};let o=i.getDefinition(e.name);return o&&Rr(o)>0?Gi(r,a,Rr(o),e.location):{kind:`Apply`,operator:r,args:Wi(a,e.location),location:e.location}}if(e.kind===`Apply`&&o(e.operator)){let n=e.args.map(r),o=i.substitutions.get(e.operator),s=i.substitutions.size>0||a.length>0||Si.has(e),l=e=>(s&&Si.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return l({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return l({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:Ai,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:Ai,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new c(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let u=Ki(t,e.operator);return u===void 0?l({...e,args:n}):{...e,operator:u,args:[...Wi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ki(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Gi(n,a,Rr(r),e.location):{...e,operator:n}}let l=Mi(t),u=ki(e,(e,i)=>i.length===0?r(e):qi(e,t,Ni(n,i,l)));return Li(u,n,l)},Ji=(e,t,n)=>t.length===0?e:e.kind===`Operator`?{...e,params:[...t,...e.params]}:{kind:`Operator`,name:e.name,params:t,body:{kind:`Let`,definitions:[e],body:Ui(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},Yi=(e,t)=>{let n=e.copies.get(t);if(n!==void 0)return n;let r=e.instantiation,i=r.getDefinition(t);return i?r.addDefinition(t,i.name,n=>{e.copies.set(t,n);let a=Mi(e),o=Ii(Oi(i,(t,n)=>qi(t,e,Ni(/* @__PURE__ */ new Map,n,a))),a);return Ji(o,r.params,i.location)}):t},Xi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>ji(e))),r=r=>{let i={instantiation:e,substitutedNames:n,copies:/* @__PURE__ */ new Map,mode:r,modes:t};return t.set(r,i),i},i=r(`Normal`);return r(`Enabled`),r(`Primed`),{getKey:e=>Ki(i,e)??e,getQualifiedKey:t=>{let n=Ki(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(t,r.name,()=>Ji(r,e.params,r.location)):t}}},Zi=e=>e.kind===`Let`&&e.instances!==void 0||Un(e).some(Zi),Qi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=ki(e,e=>Qi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Lr(n.location,e.location)),a=i===-1?e.length:i;return[...e.slice(0,a),...t(n,r),...e.slice(a)]},i.definitions),body:i.body,location:i.location}},$i=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,ea=(e,t)=>e.definitions.some(e=>Zi(e.body))||e.assumptions.some(e=>Zi(e.expression))||e.theorems.some(e=>{let t=$i(e);return t!==void 0&&Zi(t)})?{...e,definitions:e.definitions.map(e=>Oi(e,e=>Qi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Qi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=$i(e);return n===void 0?e:{...e,statement:Qi(n,t,[])}})}:e,ta=/* @__PURE__ */ new WeakMap,na=/* @__PURE__ */ new WeakMap,J=(e,t,n)=>e.isInit?{environment:e.environment,scope:t,state:n,next:void 0}:{environment:e.environment,scope:t,state:e.current,next:n},ra=(e,t,n)=>{let r=e.slice();return r[t]=n,r},ia=(t,r,i=X(t,r))=>{if(typeof i==`boolean`)return i;try{return so(i,`the action at ${Ga(t)}`)}catch(t){throw r.next===void 0?t:n(t,e.error)}},aa=e=>e===`=`||e===`\\in`||e===`\\subseteq`,oa=(e,t,n)=>{let r=n?ta:na,i=r.get(e);if(i)return i;let a=Fa(e,t,n);return r.set(e,a),a},sa=(e,t)=>{if(t.constants.has(e))return;let n=co(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},ca=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return ca(e.expression,t,!1,r);if(e.kind!==`Identifier`||W(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=sa(e.name,t);if(a&&!r.has(a))return r.add(a),ca(a.body,t,n,r)},la=(e,t,n)=>ca(e,t,!n),ua=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ua(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Uo(e.definitions,J(r,t,n));return ua(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=ro(t,e.name);if(a?.kind===`Thunk`)return ua(a.expression,a.scope,n,r,i);if(a)return;let o=r.environment.variableIndex.get(e.name);if(o!==void 0)return i?void 0:o;let s=sa(e.name,r.environment);if(s)return ua(s.body,void 0,n,r,i)},da=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||W(e))||!t&&e.kind===`Prime`,fa=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>fa(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`||n.has(e.name))return;let r=t.variableIndex.get(e.name);if(r!==void 0)return[r];let i=sa(e.name,t);if(i)return n.add(e.name),fa(i.body,t,n)},pa=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ma=(e,t,n)=>pa.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||aa(e.operator)&&(da(e.args[0],n)||la(e.args[0],t,n)!==void 0)||W(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,ha=/* @__PURE__ */ new Set,ga=(e,t)=>{if(W(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return ki(e,(e,i)=>{let a=r.isReadOnly?ga(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},_a=(e,t,n)=>{if(e.kind===`Or`||e.kind===`Exists`)return!0;let r=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0,i=r===void 0||W(e)?void 0:co(r,t);return i&&!n.has(i)&&(n.add(i),_a(i.body,t,n))?!0:Un(e).some(e=>_a(e,t,n))},va=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=la(e.args[0],t,n);if(r!==void 0)return{kind:`Assign`,expression:e,index:r,value:e.args[1]}}if(!n&&e.kind===`Unchanged`){let n=fa(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ma(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:oa(e,t,n)}},ya=(e,t)=>{if(e.bounds.length<2||e.body.kind!==`And`)return;let n=e.bounds.flatMap(e=>e.pattern.kind===`Name`?[e.pattern.name]:[]);if(n.length!==e.bounds.length)return;let r=new Set(n.slice(0,-1)),i=e.body.operands,a=i.findIndex(e=>{if(Ws(e,t)===void 0||_a(e,t,/* @__PURE__ */ new Set))return!0;let n=ga(e,ha);return!n||n.some(e=>r.has(e))}),o=a===-1?i.length:a;if(o!==0)return{names:n,guards:i.slice(0,o),rest:i.slice(o)}},ba=(e,t,n)=>{let r=e.map(e=>va(e,t,n)),i=r.map(()=>void 0),a=(e,n,o,s,c,l)=>{let u=s,d;for(let s=e;s<r.length;s++){if(s>0&&n.isInit&&za(u)){d??=J(n,o,u);for(let e=s;e<r.length;e++)if(!xa(r[e].expression,d))return;l(u,c);return}let e=r[s];if(e.kind===`Guard`){d??=J(n,o,u);let r=i[s]??=ps(e.expression,t);if(!ia(e.expression,d,r(d)))return;continue}if(e.kind===`Assign`){d??=J(n,o,u);let r=(i[s]??=ps(e.value,t))(d),a=u[e.index];if(a===void 0)u=ra(u,e.index,r),d=void 0;else if(!Re(a,r))return;continue}if(e.kind===`Unchanged`){let t=n.current;if(!t)return;for(let n of e.indices){let e=u[n];if(e===void 0)u=ra(u,n,t[n]),d=void 0;else if(!Re(e,t[n]))return}continue}let f=s+1;e.runner(n,o,u,c,!0,(e,t)=>a(f,n,o,e,t,l));return}l(u,c)};return(e,t,n,r,i,o)=>a(0,e,t,n,r,o)},xa=(e,t)=>so(X(e,t),`the initial predicate at ${Ga(e)}`),Sa=e=>(t,n,r,i,a,o)=>{ia(e,J(t,n,r))&&o(r,i)},Ca=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),d=J(r,i,a);if(a[l]!==void 0){ia(e,d)&&c(a,o);return}let f=X(n,d),p=e.operator===`\\subseteq`?{kind:`Subset`,base:f}:f;if(!z(p))throw new u(`In computing ${r.isInit?`initial`:`next`} states, the right side of \\IN is not enumerable.\n${qa(e)}`);let m=Ut(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(ra(a,l,m[e]),o)}},wa=/* @__PURE__ */ new WeakMap,Ta=e=>{let t=wa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(Ta),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return wa.set(e,n),n},Ea=(e,t)=>{let n=e.args[0],r=Sa(e),i=(e,r,i)=>ua(n,r,i,e,!t)??-1,a=Ca(e,i);return(t,n,o,s,c,l)=>{let u=i(t,n,o);if(u===-1||o[u]!==void 0){r(t,n,o,s,c,l);return}if(e.operator!==`=`){a(t,n,o,s,c,l);return}let d=X(e.args[1],J(t,n,o));l(ra(o,u,d),s)}},Da=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=ro(t,e.name);if(i?.kind===`Thunk`)return Da(i.expression,i.scope,n,r);let a=i?void 0:co(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,J(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:io(void 0,a.name,o)}},Oa=(e,t,n)=>{let r=Sa(e);return(i,a,o,s,c,l)=>{let d=Da(e.function,a,o,i);if(!d){r(i,a,o,s,c,l);return}let f=J(i,a,o),p=e.args.map(e=>X(e,f)),m=p.length===1?p[0]:R(p),h=ho(d.bounds,J(i,d.scope,o));if(!$t(m,h))throw new u(`Attempted to apply a function to ${U(m)}, which is not in its domain at ${Ga(e)}`);oa(d.body,t,n)(i,go(d.bounds,d.scope,m),o,s,c,l)}},ka=(e,t)=>{if(fa(e,t))return ba([Ta(e)],t,!1);if(e.kind===`Tuple`)return oa(Ta(e),t,!1);let n=Ta(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?ro(i,e.name):void 0;l?.kind===`Thunk`?oa(Ta(l.expression),t,!1)(r,l.scope,a,o,s,c):ia(n,J(r,i,a))&&c(a,o)}},Aa=(e,t,n)=>{let r=e.operator,i=Sa(e),a=W(e)?void 0:Do(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=xs(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=oa(a.body,t,n),o.body(e,i(J(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=J(a,o,s),f=ko(e,d);f?.kind===`Closure`?oa(f.body,t,n)(a,Vo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ja=(e,t,n)=>{let r=Sa(e),i=W(e)?void 0:sa(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=ro(s,e.name);f?.kind===`Thunk`?oa(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=oa(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},Ma=(e,t,n)=>{if(n.has(e))return;if(n.add(e),e.kind===`And`){let n=e.operands[0];if(n.kind!==`Apply`||n.operator!==`=`||W(n))return;let r=n.args[0],i=n.args[1];if(r.kind!==`FunctionApplication`||i.kind!==`String`)return;let a=r.args.length===1?r.args[0]:void 0;if(a?.kind!==`Identifier`||!W(a))return;let o=ca(r.function,t,!1);return o===void 0?void 0:{variable:o,argument:a.name,labels:/* @__PURE__ */ new Set([i.value])}}if(e.kind===`Or`){let r=e.operands.map(e=>Ma(e,t,n)),i=r[0],a=r.every(e=>e!==void 0&&e.variable===i?.variable&&e.argument===i.argument);if(!i||!a)return;let o=new Set(r.flatMap(e=>[...e?.labels??[]]));return{variable:i.variable,argument:i.argument,labels:o}}if(e.kind!==`Apply`||W(e))return;let r=Do(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=Ma(r.body,t,n),a=r.params.findIndex(e=>e.arity===0&&e.name===i?.argument),o=e.args[a];if(i&&o?.kind===`Identifier`&&W(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Na=(e,t,n)=>{let r=ro(t,n.argument),i=e.current?.[n.variable];if(r?.kind!==`Value`||typeof i!=`object`||i.kind!==`Function`)return;let a=i.domain.indexOf(r.value),o=a===-1?void 0:i.range[a];return typeof o==`string`?o:void 0},Pa=(e,t,n)=>{let r=e.map(e=>oa(e,t,n)),i=n?[]:e.map(e=>Ma(e,t,/* @__PURE__ */ new Set)),a=i.find(e=>e!==void 0),o=i.map(e=>e?.variable===a?.variable&&e?.argument===a?.argument?e?.labels:void 0);return(e,t,n,i,s,c)=>{let l=a&&Na(e,t,a);for(let a=0;a<r.length&&!e.isStopped;a++)(l===void 0||o[a]?.has(l)!==!1)&&r[a](e,t,n,i,s,c)}},Fa=(e,t,n)=>{let r=e=>oa(e,t,n);switch(e.kind){case`And`:return ba(e.operands,t,n);case`Or`:return Pa(e.operands,t,n);case`Exists`:{let i=r(e.body),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name;return(t,n,r,o,s,c)=>{let l=po(X(a.set,J(t,n,r)),a.set);for(let a=0;a<l.length&&!t.isStopped;a++)i(t,io(n,e,l[a]),r,o,s,c)}}let o=n?void 0:ya(e,t);if(o){let r=ba(o.rest,t,n);return(t,n,i,a,s,c)=>{let l=J(t,n,i),u=e.bounds.map(e=>po(X(e.set,l),e.set));if(u.some(e=>e.length===0))return;let d=(e,n)=>{if(e<0)r(t,n,i,a,s,c);else for(let r of u[e]){if(t.isStopped)return;let a=io(n,o.names[e],r);if(e===u.length-1){let e=J(t,a,i);if(!o.guards.every(t=>ia(t,e)))continue}d(e-1,a)}};d(u.length-1,n)}}return(t,n,r,a,o,s)=>{mo(e.bounds,J(t,n,r),e=>(i(t,e,r,a,o,s),t.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ws(e,t)!==void 0&&!_a(e.body,t,/* @__PURE__ */ new Set)?ga(e,ha):void 0;return(t,n,r,o,s,c)=>{let l=[];mo(e.bounds,J(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>ro(n,e)?.kind===`Value`)){u.every(n=>ia(e.body,J(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&za(r)?u.slice(n).every(n=>xa(e.body,J(t,n,r)))&&c(r,a):i(t,u[n],r,a,!0,(e,t)=>d(n+1,e,t))};d(0,r,o)}}case`If`:{let t=r(e.consequent),n=r(e.alternative);return(r,i,a,o,s,c)=>{let l=X(e.condition,J(r,i,a));(so(l,`an IF condition`)?t:n)(r,i,a,o,s,c)}}case`Case`:{let t=e.arms.map(e=>({condition:e.condition,body:r(e.body)})),n=e.other?r(e.other):void 0;return(r,i,a,o,s,c)=>{let l=J(r,i,a),d=t.find(e=>so(X(e.condition,l),`a CASE guard`))?.body??n;if(!d)throw new u(`In computing ${r.isInit?`initial`:`next`} states, TLC encountered a CASE with no conditions true.\n${qa(e)}`);d(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=$o(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Uo(e.definitions,J(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new u(`UNCHANGED appears in an initial predicate at ${Ga(e)}`)}:ka(e.expression,t);case`AngleAction`:{let t=r(e.action),n=Ta(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{ia(n,J(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,Ta(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ja(e,t,n);case`FunctionApplication`:return Oa(e,t,n);case`Apply`:{let i=e.args;if(aa(e.operator)){let r=la(i[0],t,n);if(r!==void 0)return e.operator===`=`?ba([e],t,n):Ca(e,()=>r);if(da(i[0],n))return Ea(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],J(t,n,r));(typeof c==`boolean`?c:so(c,`the left side of => at ${Ga(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return Aa(e,t,n)}default:return Sa(e)}},Ia=/* @__PURE__ */ new WeakMap,La=e=>{let t=Ia.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ia.set(e,n),n},Ra=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),za=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},Ba=(e,t)=>{let n=t.state;if(!n||!za(n))throw new u(`ENABLED needs a complete state at ${Ga(e)}`);let r=t.environment,i={environment:r,isInit:!1,current:n,isStopped:!1};return oa(e,r,!1)(i,t.scope,La(r),void 0,!1,()=>{i.isStopped=!0}),i.isStopped},Va=(t,r,i,a)=>{let o={environment:r,isInit:!0,current:void 0,isStopped:!1};oa(t,r,!0)(o,a,La(r),void 0,!1,t=>{if(!o.isStopped){if(!za(t))throw n(new u(`The initial predicate does not assign ${Ra(t,r).join(`, `)}`),e.error);o.isStopped=i(t)}})},Ha=(e,t,n)=>{let r=[];return Va(e,t,e=>(r.push(e),!1),n),r},Ua=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return oa(e,n,!1)(a,i,La(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!za(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Ra(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Wa=(e,t,n,r)=>{let i=[];return Ua(e,t,n,e=>(i.push(e),!1),r),i},Ga=e=>`line ${e.location.line}, column ${e.location.column}`,Ka=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,qa=e=>{let t=e.location.range;return t?Ka(t):Ga(e)},Ja=(e,t)=>new u(`A non-boolean expression (${fr(t)}) was used as the condition of an IF.\n${qa(e)}`),Ya=(e,t,n)=>{let r=e.location.range;if(r?.start.line===e.location.line&&r.start.column===e.location.column||!r){let r=e.operands[t],i=r.kind===`Number`?String(r.value):qa(r);return new u(`A non-boolean expression (${fr(n)}) was used as a formula in a conjunction.\n${i}`)}let i=e.operands[Math.max(t,1)].location.range?.end??r.end;return new u(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${fr(n)}.\n${qa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Xa=e=>t=>ln(t)?`Attempted to select nonexistent field "${e.field}" from the record\n${U(t)}\n${qa(e)}`:cn(t,e.field),Za=e=>t=>`A non-function (${fr(t)}) was applied as a function.\n${qa(e)}`,Qa=e=>t=>`Attempted to select field "${e.field}" from a non-record value ${U(t)}\n${qa(e)}`,$a=e=>t=>{if(!j(t))throw new u(`Attempted to apply the operator DOMAIN to a non-function\n(${fr(t)})\n${qa(e)}`);return mn(t)},eo=(e,t)=>(n,r)=>{if(!A(n)||F(n))throw new u(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${qa(e)}`);return t.evaluate([n,r])},to=t=>n(new u(`TLC can't handle real numbers: ${t.text} at ${Ga(t)}`),e.configParseError),no=t=>n(new u(`TLC can't handle a number this big: ${t.text??t.value} at ${Ga(t)}`),e.error),ro=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},io=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),ao=(e,t,n)=>{if(t.kind===`Name`)return io(e,t.name,n);let r=Xt(n);if(!r||r.length!==t.names.length)throw new u(`Cannot bind <<${t.names.join(`, `)}>> to ${U(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>io(e,t,r[n]),e)},oo=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),so=(e,t)=>{if(typeof e!=`boolean`)throw new u(`Expected ${t} to be a boolean but found ${U(e)}`);return e},co=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),lo=(e,t)=>{if(e.kind===`Prime`||e.kind===`Unchanged`||e.kind===`Enabled`)return!0;let n=e.kind===`Identifier`?e.name:e.kind===`Apply`?e.operator:void 0;if(n!==void 0){if(t.variableIndex.has(n)||t.builtins.get(n)?.isStateLevel)return!0;let e=co(n,t);if(e&&!t.constants.has(n)&&!uo(e,t))return!0}return Un(e).some(e=>lo(e,t))},uo=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!lo(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>lo(e.set,t)));return t.constantLevels.set(e,r),r},fo=(e,t)=>{let n=`universally (\\A) or existentially (\\E) quantify over ${t}, because this would require checking an\ninfinite number of cases.`;if(typeof e==`object`&&e.kind===`SequenceSet`)return`In TLA+, Seq(S) represents the set of all finite sequences whose elements come from the set S. Even when S\nis a finite set, the number of possible sequences in Seq(S) is unbounded because sequences can have any\nfinite length (e.g., length 0, 1, 2, and so on). As a result, TLC cannot evaluate expressions that\n${n} Note that for a finite set of sequences s, TLC handles s \\subseteq Seq(S).\nSee https://explain.tlapl.us/seq-unenumerable for additional details.`;if(typeof e==`object`&&e.kind===`InfiniteSet`&&e.name!==`STRING`&&e.name!==`Any`)return`The set ${e.name} contains infinitely many elements. As a result, TLC cannot evaluate expressions that\n${n} Note that TLC handles set membership like T \\subseteq ${e.name} for any finite set T.`},po=(e,t)=>{if(!A(e)||F(e)){let n=U(e),r=fo(e,n);throw new u(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${qa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${qa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Tt(e):B(e).elements},mo=(e,t,n)=>{let r=e.map(e=>po(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,ao(a,e[t].pattern,n)));return i(e.length-1,t.scope)},ho=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},go=(e,t,n)=>{if(e.length===1)return ao(t,e[0].pattern,n);let r=Xt(n)??[];return e.reduce((e,t,n)=>ao(e,t.pattern,r[n]),t)},_o=/* @__PURE__ */ new WeakMap,vo=(e,t)=>{let n=_o.get(t);if(n)return n;let r=t.location.range;if(!r)return;let i={parameters:e.flatMap((t,n)=>n>0&&e[n-1].set===t.set?[]:[{index:n,name:t.pattern.kind===`Name`?t.pattern.name:`<<${t.pattern.names.join(`, `)}>>`}]),body:Ka(r)};return _o.set(t,i),i},yo=(e,t,n)=>{let r=vo(e,t);if(!r)return;let i=e.length>1&&typeof n==`object`&&n.kind===`ProductSet`?n.factors:[n];return{parameters:r.parameters.map(e=>({name:e.name,domain:i[e.index]})),body:r.body}},bo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=Cn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(Be(a[t],e))return a[t+1]}let o=t(e,i),s=r.get(n);return s===void 0?r.set(n,[e,o]):s.push(e,o),o}};return i},xo=(e,t)=>{let n=ho(e.bounds,t),r=bo(n,(n,r)=>X(e.body,oo(t,go(e.bounds,io(void 0,e.name,r),n))),yo(e.bounds,e.body,n));return e.bounds.length===1?nn(r):r},So=(e,t)=>e.kind===`Function`?xo(e,t):X(e.body,t),Co=(e,t)=>{let n=uo(e,t.environment),r=n?t.environment.definitionValues.get(e):void 0;if(r!==void 0)return r;if(e.kind===`Operator`&&e.params.length>0)throw new u(`Operator ${e.name} requires ${e.params.length} arguments`);let i=oo(t,void 0);if(!n)return So(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>So(e,i));return t.environment.definitionValues.set(e,a),a},wo=(e,t)=>{let n=e.state===t.state&&e.next===t.next;if(n&&e.cache!==void 0)return e.cache;let r=X(e.expression,oo(t,e.scope));return n&&(e.cache=r),r},To=/* @__PURE__ */ new WeakMap,Eo=e=>{let t=To.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return To.set(e,n),n},Do=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return Do(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?Eo(r):t.builtins.get(e)},Oo=/* @__PURE__ */ new WeakMap,ko=(e,t)=>{if(W(e))return Ao(e.operator,t);let n=Oo.get(e),r=n===void 0?Do(e.operator,t.environment)??null:n;return n===void 0&&Oo.set(e,r),r??Ao(e.operator,t)},Ao=(e,t)=>{let n=ro(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return Do(e,t.environment)},jo=e=>e.kind===`Closure`?e.params.length:e.arity,Mo=(e,t)=>{if(e.kind===`Lambda`)return{kind:`Closure`,name:`LAMBDA`,params:e.params.map(e=>({name:e,arity:0})),body:e.body,scope:t.scope};let n=e.kind===`OperatorReference`?e.operator:e.kind===`Identifier`?e.name:void 0,r=n===void 0?void 0:Ao(n,t);if(!r)throw new u(`Expected an operator argument at ${Ga(e)}`);return r},No=(e,t,n)=>{if(e.kind===`Builtin`)return e.evaluate(t,n);if(e.kind===`Operator`)return e.call(t);let r=e.params.reduce((e,n,r)=>io(e,n.name,t[r]),e.scope);return X(e.body,oo(n,r))},Po=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:jo(e),call:n=>No(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},Fo=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=Ao(e.name,t);return n!==void 0&&jo(n)>0},Io=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),Lo=(e,t)=>{let n=t.variableIndex.get(e);if(n!==void 0)return{kind:`Variable`,index:n};let r=t.constants.get(e);if(r!==void 0)return{kind:`Value`,value:r};let i=t.substitutions.get(e);if(i!==void 0)return Lo(i,t);let a=t.definitions.get(e);if(a)return{kind:`Definition`,definition:a};let o=t.builtins.get(e);if(o&&o.arity===0&&!o.isStateLevel)return{kind:`Value`,value:o.evaluate([])}},Ro=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Go(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=W(e)?void 0:Lo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=ro(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Io(e,t)}}return t=>Io(e,t)},zo=/* @__PURE__ */ new WeakMap,Bo=(e,t)=>{let n=zo.get(e);if(n)return n;let r=Ro(e,t);return zo.set(e,r),r},Vo=(e,t,n)=>{if(t.length!==e.params.length)throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`);let r=e.scope;for(let i=0;i<t.length;i++){let a=e.params[i],o=a.arity>0?{kind:`Operator`,operator:Mo(t[i],n)}:Bo(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},Ho=(e,t,n)=>e.kind===`Closure`?X(e.body,oo(n,Vo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(ys(t,n),n):e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n),Uo=(e,t,n)=>e.reduce((e,r)=>{let i={name:r.name,binding:{kind:`Value`,value:!1},parent:e};return r.kind===`Function`?(i.binding={kind:`Thunk`,expression:{kind:`FunctionConstruction`,bounds:r.bounds,body:r.body,location:r.location,isRecursive:!0},scope:i,state:t.state,next:t.next,cache:void 0},i):r.params.length>0||n?.has(r)?(i.binding={kind:`Operator`,operator:{kind:`Closure`,name:r.name,params:r.params,body:r.body,scope:i}},i):(i.binding={kind:`Thunk`,expression:r.body,scope:i,state:t.state,next:t.next,cache:void 0},i)},t.scope),Wo=(e,t,n)=>{let r=ro(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return wo(r,n);if(r?.kind===`Operator`){if(jo(r.operator)===0)return No(r.operator,[],n);throw new u(`Operator ${e} is used without arguments`)}let i=Lo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw Rs(e,t);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return Co(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new u(`Unknown identifier ${e} at ${Ga(t)}`)},Go=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,Ko=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),qo=(e,t)=>Ko.has(e)||t.builtins.get(e)?.isStateLevel===!0,Jo={builtins:Ko,definitions:/* @__PURE__ */ new WeakMap},Yo={builtins:new Set([...Ko].filter(e=>e!==`Assert`)),definitions:/* @__PURE__ */ new WeakMap},Xo=(e,t)=>e.kind===`Operator`&&e.params.length===0&&uo(e,t),Zo=(e,t,n)=>{let r=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(r!==void 0&&!W(e)){let e=co(r,t),i=n.builtins.has(r)||t.builtins.get(r)?.isStateLevel===!0;if(!e&&i||e&&!Xo(e,t)&&Qo(e,t,n))return!0}return Un(e).some(e=>Zo(e,t,n))},Qo=(e,t,n=Jo)=>{let r=n.definitions.get(e);if(r!==void 0)return r;n.definitions.set(e,!1);let i=Zo(e.body,t,n)||e.kind===`Function`&&e.bounds.some(e=>Zo(e.set,t,n));return n.definitions.set(e,i),i},$o=(e,t)=>new Set(e.filter(e=>Qo(e,t))),es=e=>[...e.definitions.values()].some(t=>Qo(t,e)),ts=e=>[...e.definitions.values()].some(t=>Qo(t,e,Yo)),ns=/* @__PURE__ */ new WeakMap,rs=(e,t)=>{let n=ns.get(e);if(n!==void 0)return n;let r=ss(e,t,as);return ns.set(e,r),r},is=/* @__PURE__ */ new Set([`Number`,`String`,`Identifier`,`Apply`,`And`,`Or`,`If`,`Case`,`SetEnumeration`,`Tuple`,`FunctionSet`,`Record`,`RecordSet`,`FunctionApplication`,`FieldAccess`,`Product`,`SetFilter`,`SetMap`,`Forall`,`Exists`,`Choose`,`FunctionConstruction`,`Let`]),as=/* @__PURE__ */ new Set,os=(e,t,n)=>n.size===0?rs(e,t):ss(e,t,n),ss=(e,t,n)=>{if(!is.has(e.kind))return!1;if(e.kind===`Identifier`&&W(e))return n.has(e.name);if(W(e))return!1;if(e.kind===`Identifier`){let n=Lo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Xo(n.definition,t)}if(e.kind===`Apply`){if(qo(e.operator,t)||e.operator===`~>`)return!1;let r=Do(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=co(e.operator,t);if(!n||!uo(n,t)||Qo(n,t))return!1}return!(r.kind===`Closure`?r.params:[]).some(e=>e.arity>0)&&e.args.every(e=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&os(e,t,n))}let r={value:!0};return ki(e,(e,i)=>(r.value&&=os(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},cs=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,ls=e=>{let t={value:void 0};return n=>{if(t.value!==void 0)return t.value;let r=e(n);return t.value=r,r}},us=(e,t)=>{if(e.kind===`Identifier`&&W(e))return!t.has(e.name);if(W(e))return!0;let n={value:!1};return ki(e,(e,r)=>(n.value||=us(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},ds=(e,t)=>xi.has(e)&&cs(e)&&Ws(e,t)!==void 0&&!us(e,as),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Vs(e,t),i=cs(e)&&rs(e,t)?ls(r):ds(e,t)?gs(r):r;return t.evaluators.set(e,i),i},fs=/* @__PURE__ */ new WeakMap,ps=(e,t)=>{let n=fs.get(e);if(n)return n;let r=(cs(e)&&rs(e,t)?void 0:Mr(e,t))??Y(e,t);return fs.set(e,r),r},X=(e,t)=>ps(e,t.environment)(t),ms=(e,t)=>{let n=e.name,r=W(e)?void 0:Lo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw Rs(n,e);return i}}if(r?.kind===`Value`){let e=r.value;return()=>e}if(r?.kind===`Definition`&&r.definition.kind===`Operator`){let e=r.definition;if(e.params.length===0&&uo(e,t))return t=>Co(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=ps(e.body,t),n.body(oo(r,void 0)));return hs(e,t)?gs(r):r}return t=>Co(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>Co(e,t);return uo(e,t)||!hs(e,t)?n:gs(n)}return t=>Wo(n,e,t)},hs=(e,t)=>Ws(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ws(e.set,t)!==void 0)),gs=e=>{let t={recent:void 0,recentValue:void 0,previous:void 0,previousValue:void 0};return n=>{let r=n.state;if(r!==void 0&&t.recent===r&&t.recentValue!==void 0)return t.recentValue;if(r!==void 0&&t.previous===r&&t.previousValue!==void 0)return t.previousValue;let i=e(n);return t.previous=t.recent,t.previousValue=t.recentValue,t.recent=r,t.recentValue=i,i}},_s=(e,t)=>{let n=Do(e,t);return n!==void 0&&jo(n)>0},vs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!W(e)&&_s(e.name,t)),ys=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),bs=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(ys(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&vs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&vs(t[0],n)&&vs(t[1],n);if(i&&a){let e=Y(t[0],n),r=Y(t[1],n);return t=>i(e(t),r(t))}return n=>e.evaluate(t.map(e=>Fo(e,n)?Po(Mo(e,n),n):X(e,n)),n)},xs=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new u(`Operator ${e.name} expects ${e.params.length} arguments but received ${t.length}`)};let r=e.params,i=t.map((e,t)=>r[t].arity>0?void 0:Bo(e,n));return n=>{let a=e.scope;for(let e=0;e<t.length;e++){let o=i[e],s=o?o(n):{kind:`Operator`,operator:Mo(t[e],n)};a={name:r[e].name,binding:s,parent:a}}return a}},Ss=/* @__PURE__ */ new WeakMap,Cs=e=>{let t=Ss.get(e);if(t)return t;let n={calls:/* @__PURE__ */ new Map,size:0};return Ss.set(e,n),n},ws=(e,t)=>{let n=[];for(let r=e;n.length<t;r=r?.parent){if(r?.binding.kind!==`Value`)return;n.push(r.binding.value)}return n},Ts=(e,t)=>e.every((e,n)=>Be(e,t[n])),Es=(e,t)=>{let n=co(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&uo(n,t)&&!Qo(n,t)},Ds=(e,t,n,r)=>{let i=xs(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=ps(e.body,n),a.body({environment:n,scope:t,state:r.state,next:r.next}));if(!r)return e=>o(i(e),e);let s=Cs(e);return e=>{let n=i(e),r=ws(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=vn(a,Cn(e));let c=s.calls.get(a),l=c?.find(e=>Ts(e.args,r));if(l)return l.value;let u=o(n,e);return s.size<65536&&(s.size++,c?c.push({args:r,value:u}):s.calls.set(a,[{args:r,value:u}])),u}},Os=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!so(e(t),`the left side of =>`)||so(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new u(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=W(e)?void 0:Do(e.operator,t);if(r?.kind===`Closure`)return Ds(r,n,t,Es(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=$a(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return Si.has(e)||lo(n[0],t)?i:e=>e.scope===void 0?r.evaluate([i(e)]):i(e)}if(r?.kind===`Builtin`&&r.name===`\\subseteq`&&n.length===2){let i=Y(n[0],t),a=Y(n[1],t),o=eo(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?bs(r,n,t):r=>{let i=Ao(e.operator,r);if(i)return Ho(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new u(`${e.operator} is a function; apply it with brackets`):new u(`Unknown operator ${e.operator} at ${Ga(e)}`)}},ks=(e,t)=>{let n=e.kind===`Forall`,r=n?`the body of \\A`:`the body of \\E`,i=Y(e.body,t),a=e.bounds[0];if(e.bounds.length===1&&a.pattern.kind===`Name`){let e=a.pattern.name,o=Y(a.set,t);return t=>{let s=po(o(t),a.set);for(let a=0;a<s.length;a++){let o=so(i(oo(t,io(t.scope,e,s[a]))),r);if(o!==n)return o}return n}}let o=e.bounds;if(o.every(e=>e.pattern.kind===`Name`)){let e=o.map(e=>e.pattern.kind===`Name`?e.pattern.name:``),a=o.map(e=>Y(e.set,t));return t=>{let s=a.map((e,n)=>po(e(t),o[n].set)),c=(a,o)=>{if(a<0)return so(i(oo(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,io(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>mo(o,e,t=>so(i(oo(e,t)),r)!==n)?!n:n},As=(e,t)=>{if(e.kind===`Tuple`)return R(e.names.map(e=>{let n=ro(t,e);return n?.kind===`Value`&&n.value}));let n=ro(t,e.name);return n?.kind===`Value`?n.value:void 0},js=/* @__PURE__ */ new WeakSet,Ms=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&js.add(e)},Ns=(e,t)=>{let n=js.has(e),r=Y(e.body,t),i=e.bounds,a=i[0],o=i.length===1&&a.pattern.kind===`Name`,s=Y(a.set,t);return t=>{let c=o?s(t):ho(i,t),l=typeof c==`object`&&c.kind===`ProductSet`&&i.length>1?c.factors:[c];if(e.isRecursive||n||!l.every(pe)){let n=bo(c,e=>r(oo(t,go(i,t.scope,e))),yo(i,e.body,c));return i.length===1?nn(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=Dt(c);return nn(Le(n,n.map(n=>r(oo(t,io(t.scope,e,n))))))}let u=[];return mo(i,t,e=>{let n=i.length===1?As(a.pattern,e)??!1:R(i.map(t=>As(t.pattern,e)??!1));return u.push([n,r(oo(t,e))]),!1}),yt(u)}},Ps=(e,t)=>{let n=js.has(e);n&&Ms(e.function);let r=Y(e.function,t),i=e.updates.map(e=>({path:e.path.map(e=>e.kind===`Field`?{kind:`Field`,name:e.name,args:[]}:{kind:`Index`,name:``,args:e.args.map(e=>Y(e,t))}),value:Y(e.value,t)})),a=(e,t)=>e.kind===`Field`?e.name:e.args.length===1?e.args[0](t):R(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(oo(i,io(i.scope,`@`,e))):hn(e,a(t[r],i),e=>o(e,t,r+1,i,s),n&&r===0);return e=>{let t=r(e);for(let n of i)t=o(t,n.path,0,e,n.value);return t}},Fs=(e,t)=>{if(e.kind===`SetFilter`){let n=Y(e.predicate,t),r=Y(e.bound.set,t),i=e.bound.pattern;return t=>{let a=r(t),o=e=>so(n(oo(t,ao(t.scope,i,e))),`a set filter predicate`);if(!pe(a)){if(A(a))return{kind:`FilteredSet`,base:a,test:o};throw new u(`TLC encountered a non-enumerable quantifier bound ${U(a)} at ${Ga(e.bound.set)}`)}return Ie(B(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return mo(e.bounds,t,e=>(r.push(n(oo(t,e))),!1)),mt(r)}}let n=e.set;if(!n)return()=>{throw new u(`TLC cannot evaluate an unbounded CHOOSE at ${Ga(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(mo([i],t,i=>so(r(oo(t,i)),`the body of CHOOSE`)?(n.value=As(e.pattern,i),!0):!1),n.value===void 0)throw new u(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${qa(e)}`);return n.value}},Is=e=>[...e].sort((e,t)=>L(e.name,t.name)),Ls=e=>e.some((t,n)=>n>0&&e[n-1].name===t.name),Rs=(e,t)=>new u(`In evaluation, the identifier ${e} is either undefined or not an operator.\n${qa(t)}`),zs=e=>new u(`A primed expression appears outside an action at ${Ga(e)}`),Bs=(e,t)=>{let n=Is(e),r=Ls(n),i=n.map(e=>Y(e.value,t));if(r)return e=>yt(n.map((t,n)=>[t.name,i[n](e)]));let a=n.map(e=>e.name),o=e.map(e=>e.name);return o.some((e,t)=>e!==a[t])?e=>ye(a,i.map(t=>t(e)),o):e=>Le(a,i.map(t=>t(e)))},Vs=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Go(t)?()=>t:()=>{throw no(e)}}case`Decimal`:return()=>{throw to(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return ms(e,t);case`Apply`:return Os(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new u(`An operator cannot be used as a value at ${Ga(e)}`)};case`And`:{let t=e.operands.map(n);return n=>{for(let r=0;r<t.length;r++){let i=t[r](n);if(typeof i!=`boolean`)throw Ya(e,r,i);if(!i)return!1}return!0}}case`Or`:{let t=e.operands.map(n);return e=>{for(let n=0;n<t.length;n++)if(so(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw zs(e);return t({environment:n.environment,scope:n.scope,state:n.next,next:void 0})}}case`Unchanged`:{let t=n({kind:`Prime`,expression:e.expression,location:e.location}),r=n(e.expression);return e=>Re(t(e),r(e))}case`If`:{let t=n(e.condition),r=n(e.consequent),i=n(e.alternative);return n=>{let a=t(n);if(typeof a!=`boolean`)throw Ja(e,a);return a?r(n):i(n)}}case`Case`:{let t=e.arms.map(e=>({condition:n(e.condition),body:n(e.body)})),r=e.other?n(e.other):void 0;return n=>{for(let e of t)if(so(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new u(`Attempted to evaluate a CASE with no conditions true.\n${qa(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(oo(n,Uo(e.definitions,n)))}case`Forall`:case`Exists`:return ks(e,t);case`UnboundedQuantifier`:return()=>{throw new u(`TLC cannot evaluate an unbounded quantifier at ${Ga(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return Fs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>mt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>R(t.map(t=>t(e)))}case`FunctionConstruction`:return Ns(e,t);case`FunctionSet`:{let t=n(e.domain),r=n(e.range);return e=>({kind:`FunctionSet`,domain:t(e),range:r(e)})}case`Record`:return Bs(e.fields,t);case`RecordSet`:{let t=Is(e.fields),r=t.map(e=>e.name),i=t.map(e=>n(e.value));return e=>({kind:`RecordSet`,names:r,sets:i.map(t=>t(e))})}case`Except`:return Ps(e,t);case`ExceptAt`:return t=>{let n=ro(t.scope,`@`);if(n?.kind!==`Value`)throw new u(`@ appears outside an EXCEPT at ${Ga(e)}`);return n.value};case`FunctionApplication`:{Ms(e.function);let t=n(e.function),r=e.args.map(n),i=Za(e);if(r.length===1){let e=r[0];return n=>fn(t(n),e(n),void 0,i)}return e=>fn(t(e),R(r.map(t=>t(e))),void 0,i)}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Xa(e),a=Qa(e);return e=>fn(t(e),r,i,a)}case`Product`:{let t=e.factors.map(n);return e=>({kind:`ProductSet`,factors:t.map(t=>t(e))})}case`BoxAction`:case`AngleAction`:{let t=n(e.action),r=n({kind:`Unchanged`,expression:e.subscript,location:e.location}),i=e.kind===`BoxAction`;return e=>i?so(t(e),`an action`)||r(e):so(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>Ba(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new u(`The subexpression reference at ${Ga(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new u(`TLC cannot evaluate a temporal formula in a state at ${Ga(e)}`)}}},Hs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),Us=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ws=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Hs.has(e.kind))return!1;let a=Us(e),o=(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`;if(a!==void 0&&!o&&!t.constants.has(a)){let e=t.substitutions.get(a)??a,o=t.definitions.get(e);if(!o&&qo(e,t))return!1;let s=t.variableIndex.get(a);if(s!==void 0&&n.add(s),o&&!r.has(o)&&(r.add(o),!i(o.body)||o.kind!==`Operator`&&!o.bounds.every(e=>i(e.set))))return!1}return Un(e).every(i)};return i(e)?[...n]:void 0},Gs=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
24
24
  (***************************************************************************)
25
25
  (* This module provides dummy definitions of the operators that are *)
26
26
  (* defined by the real Reals module. It is expected that any tool that *)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.3-dev.e9d8347",
3
+ "version": "0.0.3-dev.f362080",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/evaluate.ts CHANGED
@@ -1337,11 +1337,6 @@ const compileApply = (
1337
1337
  if (definition?.kind === "Function") {
1338
1338
  throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
1339
1339
  }
1340
- if (expression.operator === "\\cdot") {
1341
- throw new TlaEvaluationError(
1342
- "The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.",
1343
- );
1344
- }
1345
1340
  throw new TlaEvaluationError(
1346
1341
  `Unknown operator ${expression.operator} at ${describeLocation(expression)}`,
1347
1342
  );