tla-ts 0.0.3-dev.79f7ed7 → 0.0.3-dev.e946acb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/transfer.cjs +1 -1
- package/dist/transfer.js +1 -1
- package/package.json +1 -1
- package/src/values.ts +7 -4
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/transfer.cjs
CHANGED
|
@@ -15,7 +15,7 @@ const e={success:0,assumptionViolation:10,deadlock:11,safetyViolation:12,livenes
|
|
|
15
15
|
`,t:` `,r:`\r`,f:`\f`}[t]??t),c(2)}else a.push(e[i.index]),c(1)}c(1),n.push({kind:`String`,text:a.join(``),line:t,column:r,endColumn:o()-1});continue}let r=te.exec(e.slice(i.index,i.index+64));if(r){s(`Number`,r[0],r[0].length);continue}if(ie(t)){let t=e.slice(i.index,C(e,i.index,ie));if(/^\d+$/.test(t)){let n=/^\d+(\.\d+)?/.exec(e.slice(i.index))?.[0]??t;s(n.includes(`.`)?`Decimal`:`Number`,n,n.length);continue}if(t.startsWith(`WF_`)||t.startsWith(`SF_`)){s(`Keyword`,t.slice(0,3),3);continue}s(v.has(t)?`Keyword`:`Identifier`,t,t.length);continue}let a=w(e,i.index);if(a>0){s(`Step`,e.slice(i.index,i.index+a),a);continue}let u=ae(e,i.index);if(u)s(`Operator`,u.text,u.length);else throw l(`Unexpected character ${JSON.stringify(t)}`)}let d=o();return n.push({kind:`End`,text:``,line:i.line,column:d,endColumn:d}),n},E=/* @__PURE__ */ new Set([`CONSTANT`,`CONSTANTS`,`INIT`,`NEXT`,`SPECIFICATION`,`INVARIANT`,`INVARIANTS`,`PROPERTY`,`PROPERTIES`,`CONSTRAINT`,`CONSTRAINTS`,`ACTION_CONSTRAINT`,`ACTION_CONSTRAINTS`,`SYMMETRY`,`VIEW`,`CHECK_DEADLOCK`,`ALIAS`,`POSTCONDITION`,`POSTCONDITIONS`,`_POSSIBLE`,`_PERIODIC`,`_RL_REWARD`]),D=[`<-`,`<<`,`>>`,`=`,`{`,`}`,`,`,`[`,`]`,`(`,`)`],ce=e=>{let t=[],n={index:0,line:1};for(;n.index<e.length;){let r=e.slice(n.index),i=/^\s+/.exec(r)?.[0];if(i){n.line+=i.split(`
|
|
16
16
|
`).length-1,n.index+=i.length;continue}if(r.startsWith(`\\*`)){let e=r.indexOf(`
|
|
17
17
|
`);n.index+=e===-1?r.length:e;continue}if(r.startsWith(`(*`)){let e=r.indexOf(`*)`);if(e===-1)throw new d(`Unterminated comment at line ${n.line}`);n.line+=r.slice(0,e).split(`
|
|
18
|
-
`).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=D.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let l=ae(e,n.index);if(!l)throw new d(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:l.text,line:n.line}),n.index+=l.length}return t.push({kind:`End`,text:``,line:n.line}),t},O=e=>{let t=ce(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`&&E.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new d(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},l=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new d(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},u=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;u();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new d(`The keyword ${t} appears twice`);r[e]=l()},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 d(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=l();return c(`]`),e},g=()=>{for(;u();){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:l(),...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 d(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new d(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new d(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new d(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},le={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:` `}},ue=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},de=e=>e!==void 0&&/^\p{Nd}$/u.test(e),fe=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),pe=e=>{let t=t=>{let n=C(e,t,ue);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=C(e,t,de);if(e.startsWith(`..`,n)&&de(e[n+2]))return{kind:`Constant`,first:t,last:C(e,n+2,de)-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=de(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:C(e,t+1,de)-1,children:[]};if(fe(n))return{kind:`Constant`,first:t,last:C(e,t,fe)-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)},me=e=>{let t=e.length===0?void 0:pe(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=le[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},k=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,A=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),j=e=>typeof e==`object`&&e.kind===`ModelValue`,he=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:k(e)?`set`:`function`,ge=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new p(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},_e={isUsed:!1},ve=(e,t)=>(_e.isUsed=!0,e.writtenKeys=t,e),ye=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Jt(e.domain,t)]]),be=(e,t,n)=>ve(ze(e,t),n),xe=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(xe);e.writtenKeys=void 0,e.range.forEach(xe)}},Se=e=>e.every(e=>typeof e==`string`),Ce=(e,t)=>e.length>0&&t.length>0&&Se(e)!==Se(t),we=(e,t)=>{_e.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Ce(n,r)?at(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=F(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=F(i[e],a[e]);if(t!==0)return t}return 0},M=(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=F(n[e],r[e]);if(t!==0)return t}return 0},Te=e=>j(e)?e.type:void 0,N=(e,t,n)=>{let r=Te(e),i=Te(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(j(e)&&j(t)){if(r===void 0||i===void 0||r===i)return;throw new f(`Attempted to ${a} the differently-typed model values ${H(e)} and ${H(t)}`)}throw r===void 0?new f(`Attempted to ${a} the typed model value ${H(t)} and the non-model value\n${H(e)}`):new f(`Attempted to ${a}${n?``:` the`} typed model value ${H(e)} and non-model value\n${H(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0;for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,Ae=e=>Ee.ranks[Oe(e)],je=(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},P=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),Me=(e,t)=>(P(e)||P(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Ne=(e,t,n)=>{if(!P(e))throw new f(`Attempted to ${n} the set ${H(L(e)?R(e):e,!0)} with the value:\n${H(t,!0)}`);if(!P(t))throw new f(`Attempted to compare overridden value ${H(e,!0)} with non-overridden value:\n${H(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 f(`Attempted to compare ${H(e,!0)} with the value\n${H(t,!0)}`)},Pe={kind:`InfiniteSet`,name:`Any`},Fe=e=>{throw new f(`Attempted to compare ANY with the value\n${H(e)}`)},F=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e===Pe?Fe(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return we(e,t);if(n===`Set`&&r===`Set`)return M(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&&N(e,t,!1),je(e.name,t.name);if(n===`ModelValue`||r===`ModelValue`)return N(e,t,!1),n===`ModelValue`?-1:1;if(e===Pe||t===Pe)return Fe(e===Pe?t:e);if(k(e)&&k(t)&&Me(e,t))return Ne(e,t,`compare`);let i=k(e);if(i!==k(t))throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`);return i?M(R(e),R(t)):we(V(e),V(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return je(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(N(e,t,!1),j(e))return-1;if(j(t))return 1;throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`)},Ie=/* @__PURE__ */ new Map,Le=e=>{Oe(e);let t=Ie.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0};return Ie.set(e,n),n},Re=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),ze=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Be=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e!==Pe||Fe(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&N(e,t,!0),F(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 we(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return M(e,t)===0;if(j(e)||j(t))return N(e,t,!0),j(e)&&j(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Be(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=z(e.domain),r=z(t.domain);if(n||r)return n===r;let i=z(e.range),a=z(t.range);return i||a?i===a:Be(e.domain,t.domain)&&Be(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(z(e))return z(t);if(z(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]&&Be(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return z(e)?z(t):!z(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Be(e,t.factors[n]));let n=k(e)&&k(t)&&!(L(e)&&L(t));if(n&&Me(e,t))return Ne(e,t,`check equality of`)===0;if(n){if(!P(e)){let n=R(e);if(!P(t))return M(n,R(t))===0}return wn(e)===wn(t)}return F(e,t)===0},Ve=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>He(e,t[n])),He=(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)&&Ve(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&Ve(e.domain,t.domain)&&Ve(e.range,t.range):wn(e)===wn(t),Ue=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},We=e=>{if(!A(e))return;let t=V(e);return{domain:t.domain,range:t.range,low:Ue(t.domain)}},Ge=e=>{if(!A(e))return;let t=V(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},Ke=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to compare the typed model value ${H(e)} and the non-model value\n${H(t)}`);return 1},qe=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to check equality of the typed model value ${H(e)} and the non-model value\n${H(t)}`);return!1},Je=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)?je(e.name,t.name):-1;if(!j(t))throw new f(`Attempted to compare the typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to compare the differently-typed model values ${H(e)} and ${H(t)}`);return je(e.name,t.name)},Ye=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)&&e.name===t.name;if(!j(t))throw new f(`Attempted to check equality of typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to check equality of the differently-typed model values ${H(e)} and ${H(t)}`);return e.name===t.name},Xe={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`}},Ze=(e,t,n)=>{let r=Xe[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${H(e)} with ${r.other}:\n${H(t)}`},Qe=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return Ke(n,t);throw new f(`Attempted to compare the function ${H(t)} with the value:\n${H(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>$e(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 $e(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 f(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${H(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},$e=(e,t)=>{for(let n=0;n<e.length;n++){let r=at(e[n],t[n]);if(r!==0)return r}return 0},et=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length-r.length||$e(n.range,r):Qe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return Ke(t,e);throw new f(`Attempted to compare record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length-r.domain.length||$e(n.domain,r.domain)||$e(n.range,r.range)}return Qe({...n,low:Ue(n.domain)},e,t)},tt=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),nt=(e,t)=>P(e)||P(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),rt=(e,t)=>{let n=tt(t)?t:R(t);return{elements:R(e).elements,otherElements:R(n).elements}},it=(e,t)=>{if(nt(e,t))return F(e,t);if(!tt(e))return it(R(e),t);if(!k(t)){if(j(t))return Ke(t,e);throw new f(`Attempted to compare the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length-n.otherElements.length||$e(n.elements,n.otherElements)},at=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return F(e,t);if(j(t))return Ke(t,e);throw new f(Ze(e,t,!1))}return e.kind===`ModelValue`?Je(e,t):A(e)?et(e,t):it(e,t)},ot=(e,t)=>e.every((e,n)=>ut(e,t[n])),st=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return qe(n,t);throw new f(`Attempted to check equality of the function ${H(t)} with the value:\n${H(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)&&ot(e.range,r.range);let i=e.low??r.low;return i===void 0?ot(e.domain,r.domain)&&ot(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new f(`Attempted to compare an integer with non-integer:\n${H(e)}.`);return e===i+t})&&ot(e.range,r.range)},ct=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length===r.length&&ot(n.range,r):st({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length===r.domain.length&&ot(n.domain,r.domain)&&ot(n.range,r.range)}return st({...n,low:Ue(n.domain)},e,t)},lt=(e,t)=>{if(nt(e,t))return Be(e,t);if(!tt(e))return lt(R(e),t);if(!k(t)){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length===n.otherElements.length&&ot(n.elements,n.otherElements)},ut=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(j(t))return qe(t,e);throw new f(Ze(e,t,!0))}return e.kind===`ModelValue`?Ye(e,t):A(e)?ct(e,t):lt(e,t)},dt=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=at(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},ft=(e,t)=>t.some(t=>ut(t,e)),pt=(e,t)=>{try{return Be(e,t)}catch(n){if(!(n instanceof f))throw n;return ut(e,t)}},mt=e=>{for(let t=1;t<e.length;t++)if(F(e[t-1],e[t])>=0)return!1;return!0},ht=e=>{if(e.length<2||mt(e))return Re(e);let t=[...e].sort(F),n=[t[0]];for(let e=1;e<t.length;e++)F(n[n.length-1],t[e])!==0&&n.push(t[e]);return Re(n)},gt=e=>{try{return ht(e)}catch(t){if(!(t instanceof f))throw t;return{kind:`MixedSet`,elements:e}}},_t=Re([]),vt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0||i.push(t)}return i.length===n.length?e:Re(i)},yt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Re(i)},bt=(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=F(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=Re(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+fn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+yn(a[e])|0;c.fingerprint=t}return c},xt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>F(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&F(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return ze(n,r)},St=e=>{let t=xt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:ve(t,e.map(e=>e[0]))},Ct=[],wt=[],Tt=/* @__PURE__ */ new WeakMap,Et=e=>{let t=Ct[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return Ct[e]=n,n},Dt=e=>{if(Gt(e),e.low===1&&e.high>=1)return Et(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Ot=e=>e.length>0&&(e===Ct[e.length]||Tt.has(e)),kt=e=>{let t=R(e).elements;return Ot(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},At=e=>{let t=wt[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return wt[e]=n,n},I=e=>ze(Et(e.length),e),jt=e=>xt(e),Mt=e=>k(e)&&!P(e),Nt=(e,t=!1)=>`UNION(${H(e.base,t)})`,Pt=e=>{if(!Mt(e.base))throw new f(`Attempted to enumerate the nonenumerable set:\n${Nt(e)}`);return R(e.base).elements},Ft=(e,t,n)=>{if(Mt(e))return e;throw new f(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${H(e)}\nwhen enumerating:\n${Nt(n)}`)},L=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 L(e.base);case`FunctionSet`:return L(e.domain)&&L(e.range)||It(e.domain)===!0||It(e)===!0;case`RecordSet`:return e.sets.every(L)||It(e)===!0;case`ProductSet`:return e.factors.every(L)||It(e)===!0;case`Union`:return L(e.base)&&R(e.base).elements.every(L);case`SetOperation`:return e.operator===`\\cup`?L(e.left)&&L(e.right):e.operator===`\\cap`?L(e.left)||L(e.right):L(e.left);default:return!1}},It=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=It(e.domain);return t===void 0?void 0:!t&&It(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=It(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return L(e)?R(e).elements.length===0:void 0;default:return}},Lt=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},Rt=(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},zt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Re(r.map(t=>e[t])));while(Rt(r,e.length))}return t},Bt=e=>{if(typeof e!=`object`)throw new f(`Expected a set but found ${H(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return dt(e.elements);case`Interval`:{let t=Dt(e);return t!==Ct[t.length]&&Tt.set(t,e),t}case`Subset`:{P(e.base)&&Gt(e.base);let t=R(e.base).elements;if(t.length>30)throw new f(`SUBSET of a set with ${t.length} elements is too large to enumerate`);return zt(t)}case`FunctionSet`:{if(z(e))return[];if(P(e.domain))throw new f(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${H(e.domain)}\ncannot be enumerated.`);let t=kt(e.domain);if(t.length>0&&P(e.range))throw new f(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${H(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:R(e.range).elements;return Lt(t.map(()=>n)).map(e=>ze(t,e))}case`RecordSet`:{if(z(e))return[];let t=e.sets.map((t,n)=>{if(P(t))throw new f(`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${H(t)}`);return R(t).elements});return Lt(t).map(t=>jt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(z(e))return[];let t=e.factors.map((e,t)=>{if(P(e))throw new f(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${H(e)}`);return R(e).elements});return Lt(t).map(I)}case`FilteredSet`:if(P(e.base))throw new f(`Attempted to enumerate { x \\in S : p(x) } when S:\n${H(e.base)}\nis not enumerable`);return R(e.base).elements.filter(e.test);case`Union`:{let t=Pt(e).flatMap((t,n)=>R(Ft(t,n,e)).elements);return R(gt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(P(e.left)||P(e.right)))throw new f(`Attempted to enumerate S \\cup T when S:\n${H(e.left)}\nand T:\n${H(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&P(e.left)&&P(e.right))throw new f(`Attempted to enumerate S \\cap T when neither S:\n${H(e.left)}\nnor T:\n${H(e.right)}\nis enumerable`);if(e.operator===`\\`&&P(e.left))throw new f(`Attempted to enumerate S \\ T when S:\n${H(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return bt(R(e.left),R(e.right)).elements;let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return R(n).elements.filter(t=>B(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to enumerate the non-enumerable set ${H(e)}`);default:throw new f(`Expected a set but found ${H(e)}`)}},Vt=/* @__PURE__ */ new WeakMap,R=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Re(Bt(e));let t=typeof e==`object`?Vt.get(e):void 0;if(t)return t;let n=Bt(e),r=typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Re(n):ht(n);return typeof e==`object`&&Vt.set(e,r),r},Ht=e=>{if(typeof e!=`object`||Vt.has(e))return R(e).elements;if(e.kind===`Union`)return Pt(e).flatMap((t,n)=>Ht(Ft(t,n,e)));if(e.kind===`FilteredSet`&&!P(e.base))return Ht(e.base).filter(e.test);if(e.kind!==`SetOperation`)return R(e).elements;if(e.operator===`\\cup`)return P(e.left)||P(e.right)?R(e).elements:[...Ht(e.left),...Ht(e.right)];let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return P(n)?R(e).elements:Ht(n).filter(t=>B(t,r)===(e.operator===`\\cap`))},z=e=>{if(typeof e!=`object`||!k(e))throw new f(`Shouldn't call isEmpty() on value ${H(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 f(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(z);case`RecordSet`:return e.sets.some(z);case`FunctionSet`:return!z(e.domain)&&z(e.range);case`Union`:return Pt(e).every((t,n)=>z(Ft(t,n,e)));default:return R(e).elements.length===0}},Ut=(e,t)=>e.reduce((e,n)=>{let r=e*Gt(n);if(r>2147483647)throw new f(t());return r},1),Wt=e=>`Overflow when computing the number of elements in:\n${H(e)}`,Gt=e=>{if(typeof e!=`object`)return R(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new f(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to compute the number of elements in the overridden value ${H(e)}.`);case`Subset`:{let t=Gt(e.base);if(t>=31)throw new f(Wt(e));return 2**t}case`FunctionSet`:{if(z(e.domain))return 1;if(z(e.range))return 0;let t=Gt(e.range);if(t===1)return 1;let n=Gt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new f(Wt(e));return r.value}case`RecordSet`:return z(e)?0:Ut(e.sets,()=>Wt(e));case`ProductSet`:return z(e)?0:Ut(e.factors,()=>`Overflow when computing the number of elements in ${H(e)}`);default:return R(e).elements.length}},Kt=e=>`Attempted to check if the set ${H(e)}is finite.`,qt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new f(`Attempted to check if the ${he(e)} ${H(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new f(`Attempted to check if the function:\n${H(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return qt(e.base)&&z(e.base);case`Subset`:return qt(e.base);case`FunctionSet`:{let t=qt(e.domain);if(t&&z(e.domain))return!0;let n=qt(e.range);return n&&z(e.range)?!0:n&&(t||Gt(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(!qt(e))n.isAllFinite=!1;else if(z(e))return!0;return n.isAllFinite}case`Union`:if(!Mt(e.base))throw new f(`Attempted to check if the nonenumerable set:\n${Nt(e)}\nis a finite set.`);return R(e.base).elements.every(qt);case`SetOperation`:if(e.operator===`\\cup`)return qt(e.left)&&qt(e.right);if(e.operator===`\\cap`){if(!qt(e.left)&&!qt(e.right))throw new f(Kt(e));return!0}if(qt(e.left))return!0;if(!qt(e.right))throw new f(Kt(e));return!1;case`FilteredSet`:if(!qt(e.base))throw new f(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${H(e.base)}\nis finite.`);return!0}},Jt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=F(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Yt=e=>{if(typeof e!=`object`||!A(e))return;let t=V(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Xt=(e,t)=>`Attempted to check if the value ${H(e)} is an element of ${H(t)}`,Zt=(e,t)=>{let n=`Attempted to check if the value:\n${H(e)}\nis an element of the`;if(!A(t))return`${n} ${he(t)} ${H(t)}`;let r=t.kind===`Function`||L(t.domain)?V(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${H(e)}\nis in the record:\n${H(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${H(t)}`},B=(e,t)=>{if(!k(t))throw new f(Zt(e,t));switch(t.kind){case`Set`:try{return Jt(t.elements,e)!==-1}catch(n){if(!(n instanceof f))throw n;return ft(e,t.elements)}case`MixedSet`:return ft(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!j(e))throw new f(`Attempted to check if the value:\n${H(e)}\nis in the integer interval ${H(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(j(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new f(`Attempted to check if the value:\n${H(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!k(e))throw new f(Xt(e,t));return R(e).elements.every(e=>B(e,t.base));case`FunctionSet`:{if(!A(e)){if(j(e))return!1;throw new f(Xt(e,t))}let n=V(e),r=Ue(n.domain);return(r===void 0?pt(t.domain,Re(n.domain)):pt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>B(e,t.range))}case`RecordSet`:{let n=A(e)?V(e):void 0;if(!n||!Se(n.domain)){if(j(e))return!1;throw new f(`Attempted to check if non-record\n${H(e)}\nis in the set of records:\n${H(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Jt(n.domain,e);return i!==-1&&B(n.range[i],t.sets[r])})}case`ProductSet`:{let n=A(e)?Yt(e):void 0;if(n===void 0){if(A(e)&&V(e).domain.every(e=>typeof e==`number`)||j(e))return!1;throw new f(`Attempted to check if non-tuple\n${H(e)}\nis in the set of tuples:\n${H(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>B(e,t.factors[n]))}case`FilteredSet`:return B(e,t.base)&&t.test(e);case`Union`:if(!Mt(t.base))throw new f(`Attempted to check if:\n ${H(e)}\nis an element of the non-enumerable set:\n ${Nt(t)}`);return R(t.base).elements.some(t=>B(e,t));case`SetOperation`:return t.operator===`\\cup`?B(e,t.left)||B(e,t.right):B(e,t.left)&&B(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Yt(e);if(n===void 0){if(j(e))return!1;throw new f(Xt(e,t))}return n.every(e=>B(e,t.base))}default:throw new f(Zt(e,t))}},Qt=(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?B(e,n):e.range.every((e,r)=>Qt(e,t.range[r],n.range)),V=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){L(e.domain)||Gt(e.domain);let t=kt(e.domain);return ze(t,t.map(t=>e.apply(t)))}throw new f(`Expected a function but found ${H(e)}`)},$t=/* @__PURE__ */ new WeakSet,en=e=>($t.add(e),e),tn=(e,t)=>typeof e==`object`&&$t.has(e)?`In applying the function\n${me(H(e))},\nthe first argument is:\n${me(H(t))}\nwhich is not in its domain.\n`:`Attempted to apply function ${H(e)} to ${H(t)}, which is not in its domain`,nn=(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 Jt(e,t)},rn=(e,t,n)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!B(t,e.domain))throw new f($t.has(e)?tn(e,t):`Attempted to apply a function to ${H(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new f(`Attempted to apply ${H(e)}, which is not a function`);let r=nn(e.domain,t);if(r===-1)throw new f(n?.(e)??tn(e,t));return e.range[r]},an=e=>Ot(e)?Tt.get(e)??At(e.length):void 0,on=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=V(e).domain;return an(t)??Re(t)},sn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!L(e.domain))){if(!B(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>pt(t,n)?r:e.apply(n),source:e.source}}let i=V(e),a=nn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=ze(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-pn(e,s)+pn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-bn(e,s)+bn(e,c)|0}return l},cn=/* @__PURE__ */ new WeakMap,ln=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},un=(e,t)=>Math.imul(e^t,16777619),dn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return ln(t)},fn=e=>ln(hn(e)+2654435769|0),pn=(e,t)=>ln(Math.imul(hn(e),668265261)+hn(t)|0),mn=e=>{if(e.kind===`ModelValue`)return ln(un(dn(e.name),109));if(k(e)){if(!L(e))return dn(wn(e));let t=R(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+fn(t[e])|0;return n}let t=V(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+pn(t.domain[e],t.range[e])|0;return n},hn=e=>{if(typeof e==`number`)return ln(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return dn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=mn(e)),e.hash;let t=cn.get(e);if(t!==void 0)return t;let n=mn(e);return cn.set(e,n),n},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},vn=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return _n(t)},yn=e=>_n(Sn(e)+2135587861|0),bn=(e,t)=>_n(Math.imul(Sn(e),374761393)+Sn(t)|0),xn=e=>{if(e.kind===`ModelValue`)return _n(vn(e.name)^60);if(k(e)){if(!L(e))return vn(wn(e));let t=R(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+yn(t[e])|0;return n}let t=V(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+bn(t.domain[e],t.range[e])|0;return n},Sn=e=>{if(typeof e==`number`)return _n(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return vn(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=xn(e)),e.fingerprint;let t=gn.get(e);if(t!==void 0)return t;let n=xn(e);return gn.set(e,n),n},Cn=/* @__PURE__ */ new WeakMap,wn=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=Cn.get(e);if(t!==void 0)return t;let n=Tn(e);return Cn.set(e,n),n},Tn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${wn(e.base)}>`;if(e.kind===`FilteredSet`&&!L(e))throw new f(`Attempted to compare the non-enumerable set ${H(e)} with another value`);if(k(e))return L(e)?`{${R(e).elements.map(wn).join(`,`)}}`:`<${e.kind} ${H(e)}>`;let t=V(e);return`(${t.domain.map((e,n)=>`${wn(e)}:${wn(t.range[n])}`).join(`,`)})`},En=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Dn=e=>e.reduce((e,t)=>e===0?0:e*Gt(t),1),On=e=>{if(e.kind===`Subset`)return Gt(e.base)<=6?R(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return R(e);if(e.kind===`FunctionSet`&&L(e.domain)&&Gt(e.domain)===0)return Re([ze([],[])]);let t=e.kind===`FunctionSet`?Gt(e.range)**Gt(e.domain):Dn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?_t:t<=1999?R(e):void 0},kn=(e,t)=>{if(t)return On(e);if(L(e))try{return On(e)}catch(e){if(e instanceof f)return;throw e}},An=(e,t)=>{let n=e=>H(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 Nt(e,t)}},jn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),H=(e,t=!1,n=!1)=>{let r=e=>H(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return En(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(R(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=kn(e,t);return n?r(n):An(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!L(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${H(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let t=V(e);return t.domain.every((e,t)=>e===t+1)?`<<${t.range.map(r).join(`, `)}>>`:t.domain.every(e=>typeof e==`string`&&jn(e))?`[${(n||t.hash!==0||t.fingerprint!==0?t.domain.map((e,n)=>[e,t.range[n]]):ye(t)).map(e=>`${String(e[0])} |-> ${r(e[1])}`).join(`, `)}]`:`(${t.domain.map((e,n)=>`${r(e)} :> ${r(t.range[n])}`).join(` @@ `)})`}}},Mn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Nn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=hn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||He(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||He(i[e],n))return i[e]}return n.kind===`Set`?Mn(n.elements,t):(Mn(n.domain,t),Mn(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}},Pn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Fn=(e,t)=>Pn.has(e)?new f(t):new p(t),In=e=>typeof e==`object`&&e.kind===`Operator`,U=(e,t,n)=>{let r=e[t];if(r===void 0||In(r))throw Fn(n,`${n} expects a value as argument ${t+1}`);return r},Ln=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`number`)throw Fn(n,`${n} expects an integer but received ${H(r)}`);return r},Rn=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`boolean`)throw Fn(n,`${n} expects a boolean but received ${H(r)}`);return r},zn=(e,t,n)=>{let r=U(e,t,n);if(!k(r))throw Fn(n,`${n} expects a set but received ${H(r)}`);return r},Bn=(e,t,n)=>{let r=zn(e,t,n);if(!L(r))throw Fn(n,`${n} cannot enumerate the non-enumerable set ${H(r)}`);return r},Vn=(e,t,n)=>R(Bn(e,t,n)),Hn=(e,t,n)=>Vn(e,t,n).elements,Un=(e,t,n)=>{let r=U(e,t,n),i=Yt(r);if(!i)throw Fn(n,`${n} expects a sequence but received ${H(r)}`);return i},Wn=(e,t,n)=>{let r=U(e,t,n);if(!A(r))throw Fn(n,`${n} expects a function but received ${H(r)}`);return V(r)},Gn=(e,t,n)=>{let r=e[t];if(r===void 0||!In(r))throw Fn(n,`${n} expects an operator as argument ${t+1}`);return r},Kn=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),W=(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}),qn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),Jn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),Yn=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(U(n,0,e),U(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),Xn=e=>e>2147483647||e<-2147483648,Zn=e=>new p(`Overflow when computing ${e}`),Qn={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`},$n={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},er={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`},tr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return $n[e.operator];if(e.kind!==`Function`)return Qn[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},nr=e=>er[tr(e)],rr=(e,t)=>new f(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),ir=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof f&&(e.isLazy===!0||!(n instanceof p))?rr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},ar=(e,t)=>{if(typeof e!=`number`)throw rr(t,`Cannot cast tlc2.value.impl.${tr(e)} to tlc2.value.impl.IntValue`);return e},or=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,sr=(e,t,n)=>{let r=or(t,`IntValue`,2);return Yn(e,(t,i)=>{let a=ar(t,r),o=ar(i,r),s=n(a,o);if(Xn(s))throw Zn(`${a}${e}${o}`);return s})},cr=(e,t,n)=>{if(typeof e!=`number`)throw new p(`The ${t} argument of ${n} should be an integer, but instead it is:\n${H(e)}`);return e},lr=(e,t)=>Yn(e,(n,r)=>t(cr(n,`first`,e),cr(r,`second`,e))),ur=(e,t,n)=>({...Jn(e,t,n),isLazy:!0}),dr=(e,t,n)=>{let r=Gn(e,t,n);return()=>r.call([])},fr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),pr=()=>({bool:qa,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ia(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw La(t,n,e);return e},apply:rn,tuple:I,set:gt,record:ze,writtenRecord:be,equal:Be,enumerate:$a,unassigned:(e,t)=>{throw new f(`The variable ${e} is not assigned at ${q(t)}`)},primeOutsideAction:e=>{throw new f(`A primed expression appears outside an action at ${q(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)=>Wa(e,t,n[r]),e.scope),state:e.state,next:e.next})}),mr={value:void 0},hr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),gr=e=>`local${e.counter++}`,_r=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Ja(e).forEach(e=>_r(e,t)),t),vr=(e,t,n)=>{let r=hr(e,Y(t,e.environment)),i=_r(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=hr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},yr=(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(()=>gr(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${xr(e,t.set,s)}, ${hr(e,t.set)});`),u=t.bounds.map(t=>{let n=gr(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${xr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=gr(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})`},br=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${xr(e,r[0],n)}, "the left side of =>") ? bool(${xr(e,r[1],n)}, "the right side of =>") : true)`;if(J(t))return vr(e,t,n);let i=mo(t.operator,e.environment);if(i?.kind!==`Builtin`)return vr(e,t,n);if(i.name===`DOMAIN`)return`${hr(e,za(t))}(${xr(e,r[0],n)})`;if(i.name===`TLCEval`)return vr(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${hr(e,Ba(t,i))}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&rs(r[0],a))return`${hr(e,i.evaluateUnary)}(${xr(e,r[0],n)})`;let o=r.length===2&&rs(r[0],a)&&rs(r[1],a);return i.evaluateBinary&&o?`${hr(e,i.evaluateBinary)}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`:vr(e,t,n)},xr=(e,t,n)=>{let r=(t,r=n)=>xr(e,t,r);if(qo(t)&&Ho(t,e.environment))return vr(e,t,n);switch(t.kind){case`Number`:return Mo(t.value)?String(t.value):vr(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(J(t))return vr(e,t,n);let i=wo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=hr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?hr(e,i.value):vr(e,t,n)}case`Apply`:return br(e,t,n);case`And`:{let n=hr(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)}, ${hr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{_s(t.function);let e=r(t.function);return t.args.length===1?`apply(${e}, ${r(t.args[0])})`:`apply(${e}, tuple([${t.args.map(e=>r(e)).join(`, `)}]))`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${hr(e,Ra(t))})`;case`Record`:{let i=[...t.fields].sort((e,t)=>F(e.name,t.name));if(i.some((e,t)=>t>0&&i[t-1].name===e.name))return vr(e,t,n);let a=hr(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}, ${hr(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=hr(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`)?yr(e,t,n):vr(e,t,n);default:return vr(e,t,n)}},Sr=(e,t)=>{if(!fr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=xr(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(`
|
|
18
|
+
`).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=D.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let l=ae(e,n.index);if(!l)throw new d(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:l.text,line:n.line}),n.index+=l.length}return t.push({kind:`End`,text:``,line:n.line}),t},O=e=>{let t=ce(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`&&E.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new d(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},l=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new d(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},u=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;u();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new d(`The keyword ${t} appears twice`);r[e]=l()},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 d(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=l();return c(`]`),e},g=()=>{for(;u();){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:l(),...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 d(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new d(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new d(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new d(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},le={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:` `}},ue=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},de=e=>e!==void 0&&/^\p{Nd}$/u.test(e),fe=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),pe=e=>{let t=t=>{let n=C(e,t,ue);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=C(e,t,de);if(e.startsWith(`..`,n)&&de(e[n+2]))return{kind:`Constant`,first:t,last:C(e,n+2,de)-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=de(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:C(e,t+1,de)-1,children:[]};if(fe(n))return{kind:`Constant`,first:t,last:C(e,t,fe)-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)},me=e=>{let t=e.length===0?void 0:pe(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=le[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},k=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,A=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),j=e=>typeof e==`object`&&e.kind===`ModelValue`,he=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:k(e)?`set`:`function`,ge=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new p(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},_e={isUsed:!1},ve=(e,t)=>(_e.isUsed=!0,e.writtenKeys=t,e),ye=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Jt(e.domain,t)]]),be=(e,t,n)=>ve(ze(e,t),n),xe=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(xe);e.writtenKeys=void 0,e.range.forEach(xe)}},Se=e=>e.every(e=>typeof e==`string`),Ce=(e,t)=>e.length>0&&t.length>0&&Se(e)!==Se(t),we=(e,t)=>{_e.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Ce(n,r)?at(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=F(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=F(i[e],a[e]);if(t!==0)return t}return 0},M=(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=F(n[e],r[e]);if(t!==0)return t}return 0},Te=e=>j(e)?e.type:void 0,N=(e,t,n)=>{let r=Te(e),i=Te(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(j(e)&&j(t)){if(r===void 0||i===void 0||r===i)return;throw new f(`Attempted to ${a} the differently-typed model values ${H(e)} and ${H(t)}`)}throw r===void 0?new f(`Attempted to ${a} the typed model value ${H(t)} and the non-model value\n${H(e)}`):new f(`Attempted to ${a}${n?``:` the`} typed model value ${H(e)} and non-model value\n${H(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0,Ie.clear();for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,Ae=e=>Ee.ranks[Oe(e)],je=(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},P=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),Me=(e,t)=>(P(e)||P(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Ne=(e,t,n)=>{if(!P(e))throw new f(`Attempted to ${n} the set ${H(L(e)?R(e):e,!0)} with the value:\n${H(t,!0)}`);if(!P(t))throw new f(`Attempted to compare overridden value ${H(e,!0)} with non-overridden value:\n${H(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 f(`Attempted to compare ${H(e,!0)} with the value\n${H(t,!0)}`)},Pe={kind:`InfiniteSet`,name:`Any`},Fe=e=>{throw new f(`Attempted to compare ANY with the value\n${H(e)}`)},F=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e===Pe?Fe(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return we(e,t);if(n===`Set`&&r===`Set`)return M(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&&N(e,t,!1),e.rank-t.rank;if(n===`ModelValue`||r===`ModelValue`)return N(e,t,!1),n===`ModelValue`?-1:1;if(e===Pe||t===Pe)return Fe(e===Pe?t:e);if(k(e)&&k(t)&&Me(e,t))return Ne(e,t,`compare`);let i=k(e);if(i!==k(t))throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`);return i?M(R(e),R(t)):we(V(e),V(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return je(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(N(e,t,!1),j(e))return-1;if(j(t))return 1;throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`)},Ie=/* @__PURE__ */ new Map,Le=e=>{let t=Ie.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0,rank:Ae(e)};return Ie.set(e,n),n},Re=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),ze=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Be=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e!==Pe||Fe(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&N(e,t,!0),F(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 we(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return M(e,t)===0;if(j(e)||j(t))return N(e,t,!0),j(e)&&j(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Be(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=z(e.domain),r=z(t.domain);if(n||r)return n===r;let i=z(e.range),a=z(t.range);return i||a?i===a:Be(e.domain,t.domain)&&Be(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(z(e))return z(t);if(z(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]&&Be(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return z(e)?z(t):!z(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Be(e,t.factors[n]));let n=k(e)&&k(t)&&!(L(e)&&L(t));if(n&&Me(e,t))return Ne(e,t,`check equality of`)===0;if(n){if(!P(e)){let n=R(e);if(!P(t))return M(n,R(t))===0}return wn(e)===wn(t)}return F(e,t)===0},Ve=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>He(e,t[n])),He=(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)&&Ve(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&Ve(e.domain,t.domain)&&Ve(e.range,t.range):wn(e)===wn(t),Ue=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},We=e=>{if(!A(e))return;let t=V(e);return{domain:t.domain,range:t.range,low:Ue(t.domain)}},Ge=e=>{if(!A(e))return;let t=V(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},Ke=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to compare the typed model value ${H(e)} and the non-model value\n${H(t)}`);return 1},qe=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to check equality of the typed model value ${H(e)} and the non-model value\n${H(t)}`);return!1},Je=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)?e.rank-t.rank:-1;if(!j(t))throw new f(`Attempted to compare the typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to compare the differently-typed model values ${H(e)} and ${H(t)}`);return e.rank-t.rank},Ye=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)&&e.name===t.name;if(!j(t))throw new f(`Attempted to check equality of typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to check equality of the differently-typed model values ${H(e)} and ${H(t)}`);return e.name===t.name},Xe={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`}},Ze=(e,t,n)=>{let r=Xe[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${H(e)} with ${r.other}:\n${H(t)}`},Qe=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return Ke(n,t);throw new f(`Attempted to compare the function ${H(t)} with the value:\n${H(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>$e(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 $e(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 f(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${H(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},$e=(e,t)=>{for(let n=0;n<e.length;n++){let r=at(e[n],t[n]);if(r!==0)return r}return 0},et=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length-r.length||$e(n.range,r):Qe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return Ke(t,e);throw new f(`Attempted to compare record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length-r.domain.length||$e(n.domain,r.domain)||$e(n.range,r.range)}return Qe({...n,low:Ue(n.domain)},e,t)},tt=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),nt=(e,t)=>P(e)||P(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),rt=(e,t)=>{let n=tt(t)?t:R(t);return{elements:R(e).elements,otherElements:R(n).elements}},it=(e,t)=>{if(nt(e,t))return F(e,t);if(!tt(e))return it(R(e),t);if(!k(t)){if(j(t))return Ke(t,e);throw new f(`Attempted to compare the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length-n.otherElements.length||$e(n.elements,n.otherElements)},at=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return F(e,t);if(j(t))return Ke(t,e);throw new f(Ze(e,t,!1))}return e.kind===`ModelValue`?Je(e,t):A(e)?et(e,t):it(e,t)},ot=(e,t)=>e.every((e,n)=>ut(e,t[n])),st=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return qe(n,t);throw new f(`Attempted to check equality of the function ${H(t)} with the value:\n${H(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)&&ot(e.range,r.range);let i=e.low??r.low;return i===void 0?ot(e.domain,r.domain)&&ot(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new f(`Attempted to compare an integer with non-integer:\n${H(e)}.`);return e===i+t})&&ot(e.range,r.range)},ct=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length===r.length&&ot(n.range,r):st({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length===r.domain.length&&ot(n.domain,r.domain)&&ot(n.range,r.range)}return st({...n,low:Ue(n.domain)},e,t)},lt=(e,t)=>{if(nt(e,t))return Be(e,t);if(!tt(e))return lt(R(e),t);if(!k(t)){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length===n.otherElements.length&&ot(n.elements,n.otherElements)},ut=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(j(t))return qe(t,e);throw new f(Ze(e,t,!0))}return e.kind===`ModelValue`?Ye(e,t):A(e)?ct(e,t):lt(e,t)},dt=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=at(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},ft=(e,t)=>t.some(t=>ut(t,e)),pt=(e,t)=>{try{return Be(e,t)}catch(n){if(!(n instanceof f))throw n;return ut(e,t)}},mt=e=>{for(let t=1;t<e.length;t++)if(F(e[t-1],e[t])>=0)return!1;return!0},ht=e=>{if(e.length<2||mt(e))return Re(e);let t=[...e].sort(F),n=[t[0]];for(let e=1;e<t.length;e++)F(n[n.length-1],t[e])!==0&&n.push(t[e]);return Re(n)},gt=e=>{try{return ht(e)}catch(t){if(!(t instanceof f))throw t;return{kind:`MixedSet`,elements:e}}},_t=Re([]),vt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0||i.push(t)}return i.length===n.length?e:Re(i)},yt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Re(i)},bt=(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=F(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=Re(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+fn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+yn(a[e])|0;c.fingerprint=t}return c},xt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>F(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&F(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return ze(n,r)},St=e=>{let t=xt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:ve(t,e.map(e=>e[0]))},Ct=[],wt=[],Tt=/* @__PURE__ */ new WeakMap,Et=e=>{let t=Ct[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return Ct[e]=n,n},Dt=e=>{if(Gt(e),e.low===1&&e.high>=1)return Et(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Ot=e=>e.length>0&&(e===Ct[e.length]||Tt.has(e)),kt=e=>{let t=R(e).elements;return Ot(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},At=e=>{let t=wt[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return wt[e]=n,n},I=e=>ze(Et(e.length),e),jt=e=>xt(e),Mt=e=>k(e)&&!P(e),Nt=(e,t=!1)=>`UNION(${H(e.base,t)})`,Pt=e=>{if(!Mt(e.base))throw new f(`Attempted to enumerate the nonenumerable set:\n${Nt(e)}`);return R(e.base).elements},Ft=(e,t,n)=>{if(Mt(e))return e;throw new f(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${H(e)}\nwhen enumerating:\n${Nt(n)}`)},L=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 L(e.base);case`FunctionSet`:return L(e.domain)&&L(e.range)||It(e.domain)===!0||It(e)===!0;case`RecordSet`:return e.sets.every(L)||It(e)===!0;case`ProductSet`:return e.factors.every(L)||It(e)===!0;case`Union`:return L(e.base)&&R(e.base).elements.every(L);case`SetOperation`:return e.operator===`\\cup`?L(e.left)&&L(e.right):e.operator===`\\cap`?L(e.left)||L(e.right):L(e.left);default:return!1}},It=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=It(e.domain);return t===void 0?void 0:!t&&It(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=It(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return L(e)?R(e).elements.length===0:void 0;default:return}},Lt=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},Rt=(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},zt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Re(r.map(t=>e[t])));while(Rt(r,e.length))}return t},Bt=e=>{if(typeof e!=`object`)throw new f(`Expected a set but found ${H(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return dt(e.elements);case`Interval`:{let t=Dt(e);return t!==Ct[t.length]&&Tt.set(t,e),t}case`Subset`:{P(e.base)&&Gt(e.base);let t=R(e.base).elements;if(t.length>30)throw new f(`SUBSET of a set with ${t.length} elements is too large to enumerate`);return zt(t)}case`FunctionSet`:{if(z(e))return[];if(P(e.domain))throw new f(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${H(e.domain)}\ncannot be enumerated.`);let t=kt(e.domain);if(t.length>0&&P(e.range))throw new f(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${H(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:R(e.range).elements;return Lt(t.map(()=>n)).map(e=>ze(t,e))}case`RecordSet`:{if(z(e))return[];let t=e.sets.map((t,n)=>{if(P(t))throw new f(`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${H(t)}`);return R(t).elements});return Lt(t).map(t=>jt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(z(e))return[];let t=e.factors.map((e,t)=>{if(P(e))throw new f(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${H(e)}`);return R(e).elements});return Lt(t).map(I)}case`FilteredSet`:if(P(e.base))throw new f(`Attempted to enumerate { x \\in S : p(x) } when S:\n${H(e.base)}\nis not enumerable`);return R(e.base).elements.filter(e.test);case`Union`:{let t=Pt(e).flatMap((t,n)=>R(Ft(t,n,e)).elements);return R(gt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(P(e.left)||P(e.right)))throw new f(`Attempted to enumerate S \\cup T when S:\n${H(e.left)}\nand T:\n${H(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&P(e.left)&&P(e.right))throw new f(`Attempted to enumerate S \\cap T when neither S:\n${H(e.left)}\nnor T:\n${H(e.right)}\nis enumerable`);if(e.operator===`\\`&&P(e.left))throw new f(`Attempted to enumerate S \\ T when S:\n${H(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return bt(R(e.left),R(e.right)).elements;let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return R(n).elements.filter(t=>B(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to enumerate the non-enumerable set ${H(e)}`);default:throw new f(`Expected a set but found ${H(e)}`)}},Vt=/* @__PURE__ */ new WeakMap,R=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Re(Bt(e));let t=typeof e==`object`?Vt.get(e):void 0;if(t)return t;let n=Bt(e),r=typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Re(n):ht(n);return typeof e==`object`&&Vt.set(e,r),r},Ht=e=>{if(typeof e!=`object`||Vt.has(e))return R(e).elements;if(e.kind===`Union`)return Pt(e).flatMap((t,n)=>Ht(Ft(t,n,e)));if(e.kind===`FilteredSet`&&!P(e.base))return Ht(e.base).filter(e.test);if(e.kind!==`SetOperation`)return R(e).elements;if(e.operator===`\\cup`)return P(e.left)||P(e.right)?R(e).elements:[...Ht(e.left),...Ht(e.right)];let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return P(n)?R(e).elements:Ht(n).filter(t=>B(t,r)===(e.operator===`\\cap`))},z=e=>{if(typeof e!=`object`||!k(e))throw new f(`Shouldn't call isEmpty() on value ${H(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 f(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(z);case`RecordSet`:return e.sets.some(z);case`FunctionSet`:return!z(e.domain)&&z(e.range);case`Union`:return Pt(e).every((t,n)=>z(Ft(t,n,e)));default:return R(e).elements.length===0}},Ut=(e,t)=>e.reduce((e,n)=>{let r=e*Gt(n);if(r>2147483647)throw new f(t());return r},1),Wt=e=>`Overflow when computing the number of elements in:\n${H(e)}`,Gt=e=>{if(typeof e!=`object`)return R(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new f(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to compute the number of elements in the overridden value ${H(e)}.`);case`Subset`:{let t=Gt(e.base);if(t>=31)throw new f(Wt(e));return 2**t}case`FunctionSet`:{if(z(e.domain))return 1;if(z(e.range))return 0;let t=Gt(e.range);if(t===1)return 1;let n=Gt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new f(Wt(e));return r.value}case`RecordSet`:return z(e)?0:Ut(e.sets,()=>Wt(e));case`ProductSet`:return z(e)?0:Ut(e.factors,()=>`Overflow when computing the number of elements in ${H(e)}`);default:return R(e).elements.length}},Kt=e=>`Attempted to check if the set ${H(e)}is finite.`,qt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new f(`Attempted to check if the ${he(e)} ${H(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new f(`Attempted to check if the function:\n${H(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return qt(e.base)&&z(e.base);case`Subset`:return qt(e.base);case`FunctionSet`:{let t=qt(e.domain);if(t&&z(e.domain))return!0;let n=qt(e.range);return n&&z(e.range)?!0:n&&(t||Gt(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(!qt(e))n.isAllFinite=!1;else if(z(e))return!0;return n.isAllFinite}case`Union`:if(!Mt(e.base))throw new f(`Attempted to check if the nonenumerable set:\n${Nt(e)}\nis a finite set.`);return R(e.base).elements.every(qt);case`SetOperation`:if(e.operator===`\\cup`)return qt(e.left)&&qt(e.right);if(e.operator===`\\cap`){if(!qt(e.left)&&!qt(e.right))throw new f(Kt(e));return!0}if(qt(e.left))return!0;if(!qt(e.right))throw new f(Kt(e));return!1;case`FilteredSet`:if(!qt(e.base))throw new f(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${H(e.base)}\nis finite.`);return!0}},Jt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=F(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Yt=e=>{if(typeof e!=`object`||!A(e))return;let t=V(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Xt=(e,t)=>`Attempted to check if the value ${H(e)} is an element of ${H(t)}`,Zt=(e,t)=>{let n=`Attempted to check if the value:\n${H(e)}\nis an element of the`;if(!A(t))return`${n} ${he(t)} ${H(t)}`;let r=t.kind===`Function`||L(t.domain)?V(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${H(e)}\nis in the record:\n${H(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${H(t)}`},B=(e,t)=>{if(!k(t))throw new f(Zt(e,t));switch(t.kind){case`Set`:try{return Jt(t.elements,e)!==-1}catch(n){if(!(n instanceof f))throw n;return ft(e,t.elements)}case`MixedSet`:return ft(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!j(e))throw new f(`Attempted to check if the value:\n${H(e)}\nis in the integer interval ${H(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(j(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new f(`Attempted to check if the value:\n${H(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!k(e))throw new f(Xt(e,t));return R(e).elements.every(e=>B(e,t.base));case`FunctionSet`:{if(!A(e)){if(j(e))return!1;throw new f(Xt(e,t))}let n=V(e),r=Ue(n.domain);return(r===void 0?pt(t.domain,Re(n.domain)):pt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>B(e,t.range))}case`RecordSet`:{let n=A(e)?V(e):void 0;if(!n||!Se(n.domain)){if(j(e))return!1;throw new f(`Attempted to check if non-record\n${H(e)}\nis in the set of records:\n${H(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Jt(n.domain,e);return i!==-1&&B(n.range[i],t.sets[r])})}case`ProductSet`:{let n=A(e)?Yt(e):void 0;if(n===void 0){if(A(e)&&V(e).domain.every(e=>typeof e==`number`)||j(e))return!1;throw new f(`Attempted to check if non-tuple\n${H(e)}\nis in the set of tuples:\n${H(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>B(e,t.factors[n]))}case`FilteredSet`:return B(e,t.base)&&t.test(e);case`Union`:if(!Mt(t.base))throw new f(`Attempted to check if:\n ${H(e)}\nis an element of the non-enumerable set:\n ${Nt(t)}`);return R(t.base).elements.some(t=>B(e,t));case`SetOperation`:return t.operator===`\\cup`?B(e,t.left)||B(e,t.right):B(e,t.left)&&B(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Yt(e);if(n===void 0){if(j(e))return!1;throw new f(Xt(e,t))}return n.every(e=>B(e,t.base))}default:throw new f(Zt(e,t))}},Qt=(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?B(e,n):e.range.every((e,r)=>Qt(e,t.range[r],n.range)),V=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){L(e.domain)||Gt(e.domain);let t=kt(e.domain);return ze(t,t.map(t=>e.apply(t)))}throw new f(`Expected a function but found ${H(e)}`)},$t=/* @__PURE__ */ new WeakSet,en=e=>($t.add(e),e),tn=(e,t)=>typeof e==`object`&&$t.has(e)?`In applying the function\n${me(H(e))},\nthe first argument is:\n${me(H(t))}\nwhich is not in its domain.\n`:`Attempted to apply function ${H(e)} to ${H(t)}, which is not in its domain`,nn=(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 Jt(e,t)},rn=(e,t,n)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!B(t,e.domain))throw new f($t.has(e)?tn(e,t):`Attempted to apply a function to ${H(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new f(`Attempted to apply ${H(e)}, which is not a function`);let r=nn(e.domain,t);if(r===-1)throw new f(n?.(e)??tn(e,t));return e.range[r]},an=e=>Ot(e)?Tt.get(e)??At(e.length):void 0,on=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=V(e).domain;return an(t)??Re(t)},sn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!L(e.domain))){if(!B(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>pt(t,n)?r:e.apply(n),source:e.source}}let i=V(e),a=nn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=ze(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-pn(e,s)+pn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-bn(e,s)+bn(e,c)|0}return l},cn=/* @__PURE__ */ new WeakMap,ln=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},un=(e,t)=>Math.imul(e^t,16777619),dn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return ln(t)},fn=e=>ln(hn(e)+2654435769|0),pn=(e,t)=>ln(Math.imul(hn(e),668265261)+hn(t)|0),mn=e=>{if(e.kind===`ModelValue`)return ln(un(dn(e.name),109));if(k(e)){if(!L(e))return dn(wn(e));let t=R(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+fn(t[e])|0;return n}let t=V(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+pn(t.domain[e],t.range[e])|0;return n},hn=e=>{if(typeof e==`number`)return ln(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return dn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=mn(e)),e.hash;let t=cn.get(e);if(t!==void 0)return t;let n=mn(e);return cn.set(e,n),n},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},vn=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return _n(t)},yn=e=>_n(Sn(e)+2135587861|0),bn=(e,t)=>_n(Math.imul(Sn(e),374761393)+Sn(t)|0),xn=e=>{if(e.kind===`ModelValue`)return _n(vn(e.name)^60);if(k(e)){if(!L(e))return vn(wn(e));let t=R(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+yn(t[e])|0;return n}let t=V(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+bn(t.domain[e],t.range[e])|0;return n},Sn=e=>{if(typeof e==`number`)return _n(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return vn(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=xn(e)),e.fingerprint;let t=gn.get(e);if(t!==void 0)return t;let n=xn(e);return gn.set(e,n),n},Cn=/* @__PURE__ */ new WeakMap,wn=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=Cn.get(e);if(t!==void 0)return t;let n=Tn(e);return Cn.set(e,n),n},Tn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${wn(e.base)}>`;if(e.kind===`FilteredSet`&&!L(e))throw new f(`Attempted to compare the non-enumerable set ${H(e)} with another value`);if(k(e))return L(e)?`{${R(e).elements.map(wn).join(`,`)}}`:`<${e.kind} ${H(e)}>`;let t=V(e);return`(${t.domain.map((e,n)=>`${wn(e)}:${wn(t.range[n])}`).join(`,`)})`},En=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Dn=e=>e.reduce((e,t)=>e===0?0:e*Gt(t),1),On=e=>{if(e.kind===`Subset`)return Gt(e.base)<=6?R(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return R(e);if(e.kind===`FunctionSet`&&L(e.domain)&&Gt(e.domain)===0)return Re([ze([],[])]);let t=e.kind===`FunctionSet`?Gt(e.range)**Gt(e.domain):Dn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?_t:t<=1999?R(e):void 0},kn=(e,t)=>{if(t)return On(e);if(L(e))try{return On(e)}catch(e){if(e instanceof f)return;throw e}},An=(e,t)=>{let n=e=>H(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 Nt(e,t)}},jn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),H=(e,t=!1,n=!1)=>{let r=e=>H(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return En(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(R(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=kn(e,t);return n?r(n):An(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!L(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${H(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let t=V(e);return t.domain.every((e,t)=>e===t+1)?`<<${t.range.map(r).join(`, `)}>>`:t.domain.every(e=>typeof e==`string`&&jn(e))?`[${(n||t.hash!==0||t.fingerprint!==0?t.domain.map((e,n)=>[e,t.range[n]]):ye(t)).map(e=>`${String(e[0])} |-> ${r(e[1])}`).join(`, `)}]`:`(${t.domain.map((e,n)=>`${r(e)} :> ${r(t.range[n])}`).join(` @@ `)})`}}},Mn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Nn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=hn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||He(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||He(i[e],n))return i[e]}return n.kind===`Set`?Mn(n.elements,t):(Mn(n.domain,t),Mn(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}},Pn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Fn=(e,t)=>Pn.has(e)?new f(t):new p(t),In=e=>typeof e==`object`&&e.kind===`Operator`,U=(e,t,n)=>{let r=e[t];if(r===void 0||In(r))throw Fn(n,`${n} expects a value as argument ${t+1}`);return r},Ln=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`number`)throw Fn(n,`${n} expects an integer but received ${H(r)}`);return r},Rn=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`boolean`)throw Fn(n,`${n} expects a boolean but received ${H(r)}`);return r},zn=(e,t,n)=>{let r=U(e,t,n);if(!k(r))throw Fn(n,`${n} expects a set but received ${H(r)}`);return r},Bn=(e,t,n)=>{let r=zn(e,t,n);if(!L(r))throw Fn(n,`${n} cannot enumerate the non-enumerable set ${H(r)}`);return r},Vn=(e,t,n)=>R(Bn(e,t,n)),Hn=(e,t,n)=>Vn(e,t,n).elements,Un=(e,t,n)=>{let r=U(e,t,n),i=Yt(r);if(!i)throw Fn(n,`${n} expects a sequence but received ${H(r)}`);return i},Wn=(e,t,n)=>{let r=U(e,t,n);if(!A(r))throw Fn(n,`${n} expects a function but received ${H(r)}`);return V(r)},Gn=(e,t,n)=>{let r=e[t];if(r===void 0||!In(r))throw Fn(n,`${n} expects an operator as argument ${t+1}`);return r},Kn=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),W=(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}),qn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),Jn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),Yn=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(U(n,0,e),U(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),Xn=e=>e>2147483647||e<-2147483648,Zn=e=>new p(`Overflow when computing ${e}`),Qn={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`},$n={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},er={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`},tr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return $n[e.operator];if(e.kind!==`Function`)return Qn[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},nr=e=>er[tr(e)],rr=(e,t)=>new f(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),ir=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof f&&(e.isLazy===!0||!(n instanceof p))?rr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},ar=(e,t)=>{if(typeof e!=`number`)throw rr(t,`Cannot cast tlc2.value.impl.${tr(e)} to tlc2.value.impl.IntValue`);return e},or=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,sr=(e,t,n)=>{let r=or(t,`IntValue`,2);return Yn(e,(t,i)=>{let a=ar(t,r),o=ar(i,r),s=n(a,o);if(Xn(s))throw Zn(`${a}${e}${o}`);return s})},cr=(e,t,n)=>{if(typeof e!=`number`)throw new p(`The ${t} argument of ${n} should be an integer, but instead it is:\n${H(e)}`);return e},lr=(e,t)=>Yn(e,(n,r)=>t(cr(n,`first`,e),cr(r,`second`,e))),ur=(e,t,n)=>({...Jn(e,t,n),isLazy:!0}),dr=(e,t,n)=>{let r=Gn(e,t,n);return()=>r.call([])},fr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),pr=()=>({bool:qa,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ia(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw La(t,n,e);return e},apply:rn,tuple:I,set:gt,record:ze,writtenRecord:be,equal:Be,enumerate:$a,unassigned:(e,t)=>{throw new f(`The variable ${e} is not assigned at ${q(t)}`)},primeOutsideAction:e=>{throw new f(`A primed expression appears outside an action at ${q(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)=>Wa(e,t,n[r]),e.scope),state:e.state,next:e.next})}),mr={value:void 0},hr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),gr=e=>`local${e.counter++}`,_r=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Ja(e).forEach(e=>_r(e,t)),t),vr=(e,t,n)=>{let r=hr(e,Y(t,e.environment)),i=_r(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=hr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},yr=(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(()=>gr(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${xr(e,t.set,s)}, ${hr(e,t.set)});`),u=t.bounds.map(t=>{let n=gr(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${xr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=gr(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})`},br=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${xr(e,r[0],n)}, "the left side of =>") ? bool(${xr(e,r[1],n)}, "the right side of =>") : true)`;if(J(t))return vr(e,t,n);let i=mo(t.operator,e.environment);if(i?.kind!==`Builtin`)return vr(e,t,n);if(i.name===`DOMAIN`)return`${hr(e,za(t))}(${xr(e,r[0],n)})`;if(i.name===`TLCEval`)return vr(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${hr(e,Ba(t,i))}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&rs(r[0],a))return`${hr(e,i.evaluateUnary)}(${xr(e,r[0],n)})`;let o=r.length===2&&rs(r[0],a)&&rs(r[1],a);return i.evaluateBinary&&o?`${hr(e,i.evaluateBinary)}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`:vr(e,t,n)},xr=(e,t,n)=>{let r=(t,r=n)=>xr(e,t,r);if(qo(t)&&Ho(t,e.environment))return vr(e,t,n);switch(t.kind){case`Number`:return Mo(t.value)?String(t.value):vr(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(J(t))return vr(e,t,n);let i=wo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=hr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?hr(e,i.value):vr(e,t,n)}case`Apply`:return br(e,t,n);case`And`:{let n=hr(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)}, ${hr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{_s(t.function);let e=r(t.function);return t.args.length===1?`apply(${e}, ${r(t.args[0])})`:`apply(${e}, tuple([${t.args.map(e=>r(e)).join(`, `)}]))`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${hr(e,Ra(t))})`;case`Record`:{let i=[...t.fields].sort((e,t)=>F(e.name,t.name));if(i.some((e,t)=>t>0&&i[t-1].name===e.name))return vr(e,t,n);let a=hr(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}, ${hr(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=hr(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`)?yr(e,t,n):vr(e,t,n);default:return vr(e,t,n)}},Sr=(e,t)=>{if(!fr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=xr(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(`
|
|
19
19
|
`)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;mr.value??=pr();let o=mr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Cr={kind:`Local`},wr=/* @__PURE__ */ new WeakMap,Tr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Er=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Dr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Or=e=>e.kind===`Operator`?e.params.length:0,kr=e=>e.name.includes(`!`),Ar=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},jr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Mr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Nr=e=>e.kind===`Name`?[e.name]:e.names,Pr=e=>e.flatMap(e=>Nr(e.pattern)),Fr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Dr(n.location,t.location))?n:void 0},Ir=(e,t)=>{let n=Fr(e,t);return n?{key:t.getKey(n),arity:Or(n)}:t.imported.get(e)},Lr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Rr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),zr=(e,t,n,r)=>{let i=e.location,a=wr.get(e);if(a!==void 0)return a;if(Tr(t))return e.kind===`Apply`?e.args.length:0;if(Rr(t))return 0;let o=Ar(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Cr),o)return o.arity;let s=Ir(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,wr.set(e,s.arity),s.arity;let c=t.slice(0,t.indexOf(`!`));if(c!==``&&(Ar(n,c)!==void 0||Ir(c,r)!==void 0||r.facts.has(c)))return e.kind===`Apply`?e.args.length:0;let l=`at line ${i.line}, column ${i.column}`;throw new u(Lr(t)?`Could not find declaration or definition of symbol '${t}' ${l}`:`Unknown operator: \`${t}' ${l}`)},Br=(e,t,n)=>(e.forEach(e=>G(e.set,t,n)),e.reduce((e,t)=>jr(e,Nr(t.pattern)),t)),Vr=(e,t)=>e.kind===`Identifier`&&e.name===t&&J(e)||Ja(e).some(e=>Vr(e,t)),Hr=(e,t)=>{e.kind===`FunctionConstruction`&&Vr(e.body,t)&&(e.isRecursive=!0),Ja(e).forEach(e=>Hr(e,t))},Ur=(e,t,n)=>{let r=jr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(G(e.body,Br(e.bounds,r,n),n),Hr(e.body,e.name)):G(e.body,Mr(r,e.params),n)},Wr=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Or(t),parent:e}),t);return e.reduce((e,t)=>(kr(t)||Ur(t,e,n),{name:t.name,arity:Or(t),parent:e}),r)},Gr=(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 u(`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 u(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},Kr=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,qr=(e,t,n)=>{let r=Ar(t,e.operator)?void 0:Fr(e.operator,n),i=zr(e,e.operator,t,n);if(i!==e.args.length)throw new u(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=Kr(e),o=a===void 0||Ar(t,a)?void 0:Fr(a,n),s=Jr(e,t,n);r&&Gr(r,i,e,s,o)})},Jr=(e,t,n)=>e.kind===`Identifier`?zr(e,e.name,t,n):e.kind===`OperatorReference`?zr(e,e.operator,t,n):(G(e,t,n),e.kind===`Lambda`?e.params.length:0),G=(e,t,n)=>{let r=e=>e.forEach(e=>G(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=zr(e,e.name,t,n);if(r>0)throw new u(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:zr(e,e.operator,t,n);return;case`Apply`:qr(e,t,n);return;case`Lambda`:G(e.body,jr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:G(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&&G(e.other,t,n);return;case`Let`:G(e.body,Wr(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:G(e.body,Br(e.bounds,t,n),n);return;case`UnboundedQuantifier`:G(e.body,jr(t,e.names),n);return;case`Choose`:e.set&&G(e.set,t,n),G(e.body,jr(t,Nr(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:G(e.predicate,Br([e.bound],t,n),n);return;case`SetMap`:G(e.expression,Br(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 u(`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`:G(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`:G(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>Jr(e,t,n))}},Yr=(e,t,n)=>(e.set&&G(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),Xr=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?Yr(t,e,n):(t.kind===`AssumeProve`?Xr(t,e,n):G(t,e,n),e),t);return G(e.conclusion,r,n),r},Zr=(e,t,n)=>e.kind===`AssumeProve`?Xr(e,t,n):(G(e,t,n),t),Qr=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>$r(t,e,n),t)},$r=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=Zr(r.statement,t,n);return Qr(e.proof,i,n),r.isSuffices?i:t}case`Define`:return Wr(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return G(r.expression,t,n),Qr(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>G(e,t,n)),t;case`Take`:return jr(Br(r.bounds,t,n),r.names);case`Pick`:{let i=jr(Br(r.bounds,t,n),r.names);return G(r.body,i,n),Qr(e.proof,t,n),i}default:return Qr(e.proof,t,n),t}},ei=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:Zr(e.statement,void 0,t);Qr(e.proof,n,t)},ti=(e,t)=>new u(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),ni=(e,t,n,r)=>t.reduce((e,t)=>{if(Ar(e,t)||n(t))throw ti(t,r);return{name:t,arity:0,parent:e}},e),ri=(e,t,n,r)=>(e.forEach(e=>ai(e.set,t,n)),e.reduce((e,t)=>ni(e,Nr(t.pattern),n,r),t)),ii=(e,t,n)=>{if(e.kind===`Function`){let r=jr(t,[e.name]),i=ri(e.bounds,r,n,e.location);ai(e.body,i,n);return}let r=ni(t,e.params.map(e=>e.name),n,e.location);ai(e.body,r,n)},ai=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:ai(e.body,ni(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(Ar(e,t.name)||n(t.name))throw ti(t.name,t.location);return ii(t,jr(e,i),n),jr(e,[t.name])},t);ai(e.body,ni(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:ai(e.body,ri(e.bounds,t,n,r),n);return;case`SetFilter`:ai(e.predicate,ri([e.bound],t,n,r),n);return;case`SetMap`:ai(e.expression,ri(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:ai(e.body,ni(t,e.names,n,r),n);return;case`Choose`:e.set&&ai(e.set,t,n),ai(e.body,ni(t,Nr(e.pattern),n,r),n);return;default:Ja(e).forEach(e=>ai(e,t,n))}},oi=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}))],si=(e,t)=>{let n=/* @__PURE__ */ new Map;oi(e).sort((e,t)=>Dr(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)&&!Er.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Dr(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 ti(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Dr(t.location,e))||r(t,e);e.definitions.forEach(e=>ii(e,void 0,i(e.location))),e.assumptions.forEach(e=>ai(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=ni(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>ai(e.expression,n,t))})},ci=(e,t)=>{let n=new Map([...Er,...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},li=(e,t,n,r)=>{let i=Mr(void 0,e.params),a={imported:ci(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 u(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Ar(i,n)&&!Ir(n,a))throw new u(`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 G(r,i,a),[n,r];if(zr(r,r.name,i,a)!==t.arity)throw new u(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},ui=(e,t)=>{let n=ci(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=>Ur(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>G(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ei(e,i(e.location,void 0)))},di=/* @__PURE__ */ new WeakSet,fi=/* @__PURE__ */ new WeakSet,pi=(e,t,n=/* @__PURE__ */ new Set)=>{let r=yi(e,(e,r)=>pi(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&J(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&J(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},mi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),hi=/* @__PURE__ */ new WeakMap,gi=e=>hi.get(e)??e.name,_i=(e,t)=>(gi(t)!==e.name&&hi.set(e,gi(t)),e),vi=(e,t)=>_i(e.kind===`Function`?{...e,bounds:mi(e.bounds,e=>t(e,[])),body:t(e.body,Pr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),yi=(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=>vi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:mi(e.bounds,n),body:t(e.body,Pr(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,Nr(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,Nr(e.bound.pattern))};case`SetMap`:return{...e,bounds:mi(e.bounds,n),expression:t(e.expression,Pr(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)}}},bi=`LAMBDA`,xi=(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 yi(e,(e,n)=>(r.push(...xi(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Si=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}},Ci=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),wi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Ti=(e,t)=>e.map(e=>({pattern:wi(e.pattern,t),set:e.set})),Ei=(e,t)=>e.kind===`Function`?{...e,bounds:Ti(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Di=(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=>_i({...Ei(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Ti(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:wi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:wi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Oi=e=>`${e}@ENABLED`,ki=(e,t)=>e.modes.get(t)??e,Ai=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!J(e)&&t.instantiation.variables.has(e.name),ji=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>ji(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Ai(e,t,n))return[e.name];let r=J(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?ji(r.body,t,/* @__PURE__ */ new Map):void 0},Mi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Ai(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Oi(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:Li(e.expression,ki(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[Li(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:Li(e.subscript,ki(t,`Primed`),n),location:r},Li(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=ji(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:Oi(r),location:e.location},location:e.location},Li({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ni=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Pi=(e,t)=>e.map(e=>Ni(e.name,t)),Fi=(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:[...Pi(t,r),...i.map(e=>Ni(e,r))],location:r},location:r}},Ii=(e,t)=>e.instantiation.isParameterized(t)?zi(e,t):void 0,Li=(e,t,n)=>{let r=e=>Li(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!J(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:Li(e.expression,ki(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:Li(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},ki(t,`Enabled`),n)};let s=Mi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return di.add(n),n;let r=Ii(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&&Or(o)>0?Fi(r,a,Or(o),e.location):{kind:`Apply`,operator:r,args:Pi(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||fi.has(e),c=e=>(s&&fi.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return c({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return c({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:bi,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:bi,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new u(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let l=Ii(t,e.operator);return l===void 0?c({...e,args:n}):{...e,operator:l,args:[...Pi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ii(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Fi(n,a,Or(r),e.location):{...e,operator:n}}let c=Si(t),l=yi(e,(e,i)=>i.length===0?r(e):Li(e,t,Ci(n,i,c)));return Di(l,n,c)},Ri=(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:Ni(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},zi=(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(i.name,n=>{e.copies.set(t,n);let a=Si(e),o=Ei(vi(i,(t,n)=>Li(t,e,Ci(/* @__PURE__ */ new Map,n,a))),a);return Ri(o,r.params,i.location)}):t},Bi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>xi(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=>Ii(i,e)??e,getQualifiedKey:t=>{let n=Ii(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(r.name,()=>Ri(r,e.params,r.location)):t}}},Vi=e=>e.kind===`Let`&&e.instances!==void 0||Ja(e).some(Vi),Hi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=yi(e,e=>Hi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Dr(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}},Ui=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,Wi=(e,t)=>e.definitions.some(e=>Vi(e.body))||e.assumptions.some(e=>Vi(e.expression))||e.theorems.some(e=>{let t=Ui(e);return t!==void 0&&Vi(t)})?{...e,definitions:e.definitions.map(e=>vi(e,e=>Hi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Hi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=Ui(e);return n===void 0?e:{...e,statement:Hi(n,t,[])}})}:e,Gi=/* @__PURE__ */ new WeakMap,Ki=/* @__PURE__ */ new WeakMap,K=(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},qi=(e,t,n)=>{let r=e.slice();return r[t]=n,r},Ji=(t,n)=>{let r=X(t,n);if(typeof r==`boolean`)return r;try{return qa(r,`the action at ${q(t)}`)}catch(t){throw n.next===void 0?t:a(t,e.error)}},Yi=e=>e===`=`||e===`\\in`||e===`\\subseteq`,Xi=(e,t,n)=>{let r=n?Gi:Ki,i=r.get(e);if(i)return i;let a=wa(e,t,n);return r.set(e,a),a},Zi=(e,t)=>{if(t.constants.has(e))return;let n=Ya(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},Qi=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return Qi(e.expression,t,!1,r);if(e.kind!==`Identifier`||J(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=Zi(e.name,t);if(a&&!r.has(a))return r.add(a),Qi(a.body,t,n,r)},$i=(e,t,n)=>Qi(e,t,!n),ea=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ea(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Ao(e.definitions,K(r,t,n));return ea(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=Ua(t,e.name);if(a?.kind===`Thunk`)return ea(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=Zi(e.name,r.environment);if(s)return ea(s.body,void 0,n,r,i)},ta=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||J(e))||!t&&e.kind===`Prime`,na=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>na(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=Zi(e.name,t);if(i)return n.add(e.name),na(i.body,t,n)},ra=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ia=(e,t,n)=>ra.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||Yi(e.operator)&&(ta(e.args[0],n)||$i(e.args[0],t,n)!==void 0)||J(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,aa=/* @__PURE__ */ new Set,oa=(e,t)=>{if(J(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return yi(e,(e,i)=>{let a=r.isReadOnly?oa(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},sa=(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||J(e)?void 0:Ya(r,t);return i&&!n.has(i)&&(n.add(i),sa(i.body,t,n))?!0:Ja(e).some(e=>sa(e,t,n))},ca=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=$i(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=na(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ia(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:Xi(e,t,n)}},la=(e,t,n)=>{let r=e.map(e=>ca(e,t,n)),i=(e,t,n,a,o,s)=>{let c=a,l;for(let a=e;a<r.length;a++){if(a>0&&t.isInit&&Oa(c)){l??=K(t,n,c);for(let e=a;e<r.length;e++)if(!ua(r[e].expression,l))return;s(c,o);return}let e=r[a];if(e.kind===`Guard`){if(l??=K(t,n,c),!Ji(e.expression,l))return;continue}if(e.kind===`Assign`){l??=K(t,n,c);let r=X(e.value,l),i=c[e.index];if(i===void 0)c=qi(c,e.index,r),l=void 0;else if(!Be(i,r))return;continue}if(e.kind===`Unchanged`){let n=t.current;if(!n)return;for(let t of e.indices){let e=c[t];if(e===void 0)c=qi(c,t,n[t]),l=void 0;else if(!Be(e,n[t]))return}continue}let u=a+1;e.runner(t,n,c,o,!0,(e,r)=>i(u,t,n,e,r,s));return}s(c,o)};return(e,t,n,r,a,o)=>i(0,e,t,n,r,o)},ua=(e,t)=>qa(X(e,t),`the initial predicate at ${q(e)}`),da=e=>(t,n,r,i,a,o)=>{Ji(e,K(t,n,r))&&o(r,i)},fa=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),u=K(r,i,a);if(a[l]!==void 0){Ji(e,u)&&c(a,o);return}let d=X(n,u),p=e.operator===`\\subseteq`?{kind:`Subset`,base:d}:d;if(!L(p))throw new f(`In computing next states, the right side of \\in is not enumerable: ${H(p)} at ${q(e)}`);let m=Ht(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(qi(a,l,m[e]),o)}},pa=/* @__PURE__ */ new WeakMap,ma=e=>{let t=pa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(ma),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return pa.set(e,n),n},ha=(e,t)=>{let n=e.args[0],r=da(e),i=(e,r,i)=>ea(n,r,i,e,!t)??-1,a=fa(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],K(t,n,o));l(qi(o,u,d),s)}},ga=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=Ua(t,e.name);if(i?.kind===`Thunk`)return ga(i.expression,i.scope,n,r);let a=i?void 0:Ya(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,K(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:Wa(void 0,a.name,o)}},_a=(e,t,n)=>{let r=da(e);return(i,a,o,s,c,l)=>{let u=ga(e.function,a,o,i);if(!u){r(i,a,o,s,c,l);return}let d=K(i,a,o),p=e.args.map(e=>X(e,d)),m=p.length===1?p[0]:I(p),h=to(u.bounds,K(i,u.scope,o));if(!B(m,h))throw new f(`Attempted to apply a function to ${H(m)}, which is not in its domain at ${q(e)}`);Xi(u.body,t,n)(i,no(u.bounds,u.scope,m),o,s,c,l)}},va=(e,t)=>{if(na(e,t))return la([ma(e)],t,!1);if(e.kind===`Tuple`)return Xi(ma(e),t,!1);let n=ma(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?Ua(i,e.name):void 0;l?.kind===`Thunk`?Xi(ma(l.expression),t,!1)(r,l.scope,a,o,s,c):Ji(n,K(r,i,a))&&c(a,o)}},ya=(e,t,n)=>{let r=e.operator,i=da(e),a=J(e)?void 0:mo(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=os(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=Xi(a.body,t,n),o.body(e,i(K(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=K(a,o,s),f=go(e,d);f?.kind===`Closure`?Xi(f.body,t,n)(a,Oo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ba=(e,t,n)=>{let r=da(e),i=J(e)?void 0:Zi(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=Ua(s,e.name);f?.kind===`Thunk`?Xi(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=Xi(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},xa=(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!==`=`||J(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`||!J(a))return;let o=Qi(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=>xa(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`||J(e))return;let r=mo(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=xa(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`&&J(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Sa=(e,t,n)=>{let r=Ua(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},Ca=(e,t,n)=>{let r=e.map(e=>Xi(e,t,n)),i=n?[]:e.map(e=>xa(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&&Sa(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)}},wa=(e,t,n)=>{let r=e=>Xi(e,t,n);switch(e.kind){case`And`:return la(e.operands,t,n);case`Or`:return Ca(e.operands,t,n);case`Exists`:{let t=r(e.body),n=e.bounds[0];if(e.bounds.length===1&&n.pattern.kind===`Name`){let e=n.pattern.name;return(r,i,a,o,s,c)=>{let l=$a(X(n.set,K(r,i,a)),n.set);for(let n=0;n<l.length&&!r.isStopped;n++)t(r,Wa(i,e,l[n]),a,o,s,c)}}return(n,r,i,a,o,s)=>{eo(e.bounds,K(n,r,i),e=>(t(n,e,i,a,o,s),n.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ts(e,t)!==void 0&&!sa(e.body,t,/* @__PURE__ */ new Set)?oa(e,aa):void 0;return(t,n,r,o,s,c)=>{let l=[];eo(e.bounds,K(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>Ua(n,e)?.kind===`Value`)){u.every(n=>Ji(e.body,K(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&Oa(r)?u.slice(n).every(n=>ua(e.body,K(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,K(r,i,a));(qa(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=K(r,i,a),u=t.find(e=>qa(X(e.condition,l),`a CASE guard`))?.body??n;if(!u)throw new f(`No CASE guard is true at ${q(e)}`);u(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=zo(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Ao(e.definitions,K(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new f(`UNCHANGED appears in an initial predicate at ${q(e)}`)}:va(e.expression,t);case`AngleAction`:{let t=r(e.action),n=ma(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{Ji(n,K(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,ma(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ba(e,t,n);case`FunctionApplication`:return _a(e,t,n);case`Apply`:{let i=e.args;if(Yi(e.operator)){let r=$i(i[0],t,n);if(r!==void 0)return e.operator===`=`?la([e],t,n):fa(e,()=>r);if(ta(i[0],n))return ha(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],K(t,n,r));(typeof c==`boolean`?c:qa(c,`the left side of => at ${q(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return ya(e,t,n)}default:return da(e)}},Ta=/* @__PURE__ */ new WeakMap,Ea=e=>{let t=Ta.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ta.set(e,n),n},Da=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),Oa=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},ka=(e,t)=>{let n=t.state;if(!n||!Oa(n))throw new f(`ENABLED needs a complete state at ${q(e)}`);let r=t.environment,i={value:!1};return Xi(e,r,!1)({environment:r,isInit:!1,current:n,isStopped:!1},t.scope,Ea(r),void 0,!1,()=>{i.value=!0}),i.value},Aa=(t,n,r,i)=>{let o={environment:n,isInit:!0,current:void 0,isStopped:!1};Xi(t,n,!0)(o,i,Ea(n),void 0,!1,t=>{if(!o.isStopped){if(!Oa(t))throw a(new f(`The initial predicate does not assign ${Da(t,n).join(`, `)}`),e.error);o.isStopped=r(t)}})},ja=(e,t,n)=>{let r=[];return Aa(e,t,e=>(r.push(e),!1),n),r},Ma=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return Xi(e,n,!1)(a,i,Ea(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!Oa(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Da(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Na=(e,t,n,r)=>{let i=[];return Ma(e,t,n,e=>(i.push(e),!1),r),i},q=e=>`line ${e.location.line}, column ${e.location.column}`,Pa=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,Fa=e=>{let t=e.location.range;return t?Pa(t):q(e)},Ia=(e,t)=>new f(`A non-boolean expression (${nr(t)}) was used as the condition of an IF.\n${Fa(e)}`),La=(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):Fa(r);return new f(`A non-boolean expression (${nr(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 f(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${nr(n)}.\n${Fa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Ra=e=>t=>`Attempted to select nonexistent field "${e.field}" from the record\n${H(t)}\n${Fa(e)}`,za=e=>t=>{if(!A(t))throw new f(`Attempted to apply the operator DOMAIN to a non-function\n(${nr(t)})\n${Fa(e)}`);return on(t)},Ba=(e,t)=>(n,r)=>{if(!k(n)||P(n))throw new f(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${Fa(e)}`);return t.evaluate([n,r])},Va=t=>a(new f(`TLC can't handle real numbers: ${t.text} at ${q(t)}`),e.configParseError),Ha=t=>a(new f(`TLC can't handle a number this big: ${t.text??t.value} at ${q(t)}`),e.error),Ua=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},Wa=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),Ga=(e,t,n)=>{if(t.kind===`Name`)return Wa(e,t.name,n);let r=Yt(n);if(!r||r.length!==t.names.length)throw new f(`Cannot bind <<${t.names.join(`, `)}>> to ${H(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>Wa(e,t,r[n]),e)},Ka=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),qa=(e,t)=>{if(typeof e!=`boolean`)throw new f(`Expected ${t} to be a boolean but found ${H(e)}`);return e},Ja=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:[])}},J=e=>(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`,Ya=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),Xa=(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=Ya(n,t);if(e&&!t.constants.has(n)&&!Za(e,t))return!0}return Ja(e).some(e=>Xa(e,t))},Za=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!Xa(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>Xa(e.set,t)));return t.constantLevels.set(e,r),r},Qa=(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.`},$a=(e,t)=>{if(!k(e)||P(e)){let n=H(e),r=Qa(e,n);throw new f(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${Fa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${Fa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Dt(e):R(e).elements},eo=(e,t,n)=>{let r=e.map(e=>$a(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,Ga(a,e[t].pattern,n)));return i(e.length-1,t.scope)},to=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},no=(e,t,n)=>{if(e.length===1)return Ga(t,e[0].pattern,n);let r=Yt(n)??[];return e.reduce((e,t,n)=>Ga(e,t.pattern,r[n]),t)},ro=/* @__PURE__ */ new WeakMap,io=(e,t)=>{let n=ro.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:Pa(r)};return ro.set(t,i),i},ao=(e,t,n)=>{let r=io(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}},oo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=hn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(He(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},so=(e,t)=>{let n=to(e.bounds,t),r=oo(n,(n,r)=>X(e.body,Ka(t,no(e.bounds,Wa(void 0,e.name,r),n))),ao(e.bounds,e.body,n));return e.bounds.length===1?en(r):r},co=(e,t)=>e.kind===`Function`?so(e,t):X(e.body,t),lo=(e,t)=>{let n=Za(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 f(`Operator ${e.name} requires ${e.params.length} arguments`);let i=Ka(t,void 0);if(!n)return co(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>co(e,i));return t.environment.definitionValues.set(e,a),a},uo=(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,Ka(t,e.scope));return n&&(e.cache=r),r},fo=/* @__PURE__ */ new WeakMap,po=e=>{let t=fo.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return fo.set(e,n),n},mo=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return mo(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?po(r):t.builtins.get(e)},ho=/* @__PURE__ */ new WeakMap,go=(e,t)=>{if(J(e))return _o(e.operator,t);let n=ho.get(e),r=n===void 0?mo(e.operator,t.environment)??null:n;return n===void 0&&ho.set(e,r),r??_o(e.operator,t)},_o=(e,t)=>{let n=Ua(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return mo(e,t.environment)},vo=e=>e.kind===`Closure`?e.params.length:e.arity,yo=(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:_o(n,t);if(!r)throw new f(`Expected an operator argument at ${q(e)}`);return r},bo=(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)=>Wa(e,n.name,t[r]),e.scope);return X(e.body,Ka(n,r))},xo=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:vo(e),call:n=>bo(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},So=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=_o(e.name,t);return n!==void 0&&vo(n)>0},Co=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),wo=(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 wo(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([])}},To=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Mo(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=J(e)?void 0:wo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=Ua(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Co(e,t)}}return t=>Co(e,t)},Eo=/* @__PURE__ */ new WeakMap,Do=(e,t)=>{let n=Eo.get(e);if(n)return n;let r=To(e,t);return Eo.set(e,r),r},Oo=(e,t,n)=>{if(t.length!==e.params.length)throw new f(`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:yo(t[i],n)}:Do(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},ko=(e,t,n)=>e.kind===`Closure`?X(e.body,Ka(n,Oo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(is(t,n),n):e.evaluate(t.map(e=>So(e,n)?xo(yo(e,n),n):X(e,n)),n),Ao=(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),jo=(e,t,n)=>{let r=Ua(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return uo(r,n);if(r?.kind===`Operator`){if(vo(r.operator)===0)return bo(r.operator,[],n);throw new f(`Operator ${e} is used without arguments`)}let i=wo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw new f(`The variable ${e} is not assigned at ${q(t)}`);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return lo(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new f(`Unknown identifier ${e} at ${q(t)}`)},Mo=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,No=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),Po=(e,t)=>No.has(e)||t.builtins.get(e)?.isStateLevel===!0,Fo=/* @__PURE__ */ new WeakMap,Io=(e,t)=>e.kind===`Operator`&&e.params.length===0&&Za(e,t),Lo=(e,t)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(n!==void 0&&!J(e)){let e=Ya(n,t);if(!e&&Po(n,t)||e&&!Io(e,t)&&Ro(e,t))return!0}return Ja(e).some(e=>Lo(e,t))},Ro=(e,t)=>{let n=Fo.get(e);if(n!==void 0)return n;Fo.set(e,!1);let r=Lo(e.body,t)||e.kind===`Function`&&e.bounds.some(e=>Lo(e.set,t));return Fo.set(e,r),r},zo=(e,t)=>new Set(e.filter(e=>Ro(e,t))),Bo=e=>[...e.definitions.values()].some(t=>Ro(t,e)),Vo=/* @__PURE__ */ new WeakMap,Ho=(e,t)=>{let n=Vo.get(e);if(n!==void 0)return n;let r=Ko(e,t,Wo);return Vo.set(e,r),r},Uo=/* @__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`]),Wo=/* @__PURE__ */ new Set,Go=(e,t,n)=>n.size===0?Ho(e,t):Ko(e,t,n),Ko=(e,t,n)=>{if(!Uo.has(e.kind))return!1;if(e.kind===`Identifier`&&J(e))return n.has(e.name);if(J(e))return!1;if(e.kind===`Identifier`){let n=wo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Io(n.definition,t)}if(e.kind===`Apply`){if(Po(e.operator,t)||e.operator===`~>`)return!1;let r=mo(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=Ya(e.operator,t);if(!n||!Za(n,t)||Ro(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`&&Go(e,t,n))}let r={value:!0};return yi(e,(e,i)=>(r.value&&=Go(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},qo=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,Jo=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}},Yo=(e,t)=>{if(e.kind===`Identifier`&&J(e))return!t.has(e.name);if(J(e))return!0;let n={value:!1};return yi(e,(e,r)=>(n.value||=Yo(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},Xo=(e,t)=>di.has(e)&&qo(e)&&Ts(e,t)!==void 0&&!Yo(e,Wo),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Ss(e,t),i=qo(e)&&Ho(e,t)?Jo(r):Xo(e,t)?ts(r):r;return t.evaluators.set(e,i),i},Zo=/* @__PURE__ */ new WeakMap,Qo=(e,t)=>{let n=Zo.get(e);if(n)return n;let r=(qo(e)&&Ho(e,t)?void 0:Sr(e,t))??Y(e,t);return Zo.set(e,r),r},X=(e,t)=>Qo(e,t.environment)(t),$o=(e,t)=>{let n=e.name,r=J(e)?void 0:wo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw new f(`The variable ${n} is not assigned at ${q(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&&Za(e,t))return t=>lo(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=Qo(e.body,t),n.body(Ka(r,void 0)));return es(e,t)?ts(r):r}return t=>lo(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>lo(e,t);return Za(e,t)||!es(e,t)?n:ts(n)}return t=>jo(n,e,t)},es=(e,t)=>Ts(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ts(e.set,t)!==void 0)),ts=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}},ns=(e,t)=>{let n=mo(e,t);return n!==void 0&&vo(n)>0},rs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!J(e)&&ns(e.name,t)),is=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),as=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(is(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&rs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&rs(t[0],n)&&rs(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=>So(e,n)?xo(yo(e,n),n):X(e,n)),n)},os=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new f(`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:Do(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:yo(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},ls=(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},us=(e,t)=>e.every((e,n)=>He(e,t[n])),ds=(e,t)=>{let n=Ya(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&Za(n,t)&&!Ro(n,t)},fs=(e,t,n,r)=>{let i=os(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=Qo(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=ls(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=un(a,hn(e));let c=s.calls.get(a),l=c?.find(e=>us(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}},ps=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!qa(e(t),`the left side of =>`)||qa(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new f(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=J(e)?void 0:mo(e.operator,t);if(r?.kind===`Closure`)return fs(r,n,t,ds(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=za(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return fi.has(e)||Xa(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=Ba(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?as(r,n,t):r=>{let i=_o(e.operator,r);if(i)return ko(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new f(`${e.operator} is a function; apply it with brackets`):new f(`Unknown operator ${e.operator} at ${q(e)}`)}},ms=(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=$a(o(t),a.set);for(let a=0;a<s.length;a++){let o=qa(i(Ka(t,Wa(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)=>$a(e(t),o[n].set)),c=(a,o)=>{if(a<0)return qa(i(Ka(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,Wa(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>eo(o,e,t=>qa(i(Ka(e,t)),r)!==n)?!n:n},hs=(e,t)=>{if(e.kind===`Tuple`)return I(e.names.map(e=>{let n=Ua(t,e);return n?.kind===`Value`&&n.value}));let n=Ua(t,e.name);return n?.kind===`Value`?n.value:void 0},gs=/* @__PURE__ */ new WeakSet,_s=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&gs.add(e)},vs=(e,t)=>{let n=gs.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):to(i,t);if(e.isRecursive||n||!L(c)){let n=oo(c,e=>r(Ka(t,no(i,t.scope,e))),ao(i,e.body,c));return i.length===1?en(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=kt(c);return en(ze(n,n.map(n=>r(Ka(t,Wa(t.scope,e,n))))))}let l=[];return eo(i,t,e=>{let n=i.length===1?hs(a.pattern,e)??!1:I(i.map(t=>hs(t.pattern,e)??!1));return l.push([n,r(Ka(t,e))]),!1}),xt(l)}},ys=(e,t)=>{let n=gs.has(e);n&&_s(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):I(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(Ka(i,Wa(i.scope,`@`,e))):sn(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}},bs=(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=>qa(n(Ka(t,Ga(t.scope,i,e))),`a set filter predicate`);if(typeof a!=`object`||a.kind!==`Set`&&a.kind!==`MixedSet`&&a.kind!==`Interval`){if(k(a))return{kind:`FilteredSet`,base:a,test:o};throw new f(`TLC encountered a non-enumerable quantifier bound ${H(a)} at ${q(e.bound.set)}`)}return Re(R(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return eo(e.bounds,t,e=>(r.push(n(Ka(t,e))),!1)),gt(r)}}let n=e.set;if(!n)return()=>{throw new f(`TLC cannot evaluate an unbounded CHOOSE at ${q(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(eo([i],t,i=>qa(r(Ka(t,i)),`the body of CHOOSE`)?(n.value=hs(e.pattern,i),!0):!1),n.value===void 0)throw new f(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${Fa(e)}`);return n.value}},xs=(e,t)=>{let n=[...e].sort((e,t)=>F(e.name,t.name)),r=n.some((e,t)=>t>0&&n[t-1].name===e.name),i=n.map(e=>Y(e.value,t));if(r)return e=>xt(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=>be(a,i.map(t=>t(e)),o):e=>ze(a,i.map(t=>t(e)))},Ss=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Mo(t)?()=>t:()=>{throw Ha(e)}}case`Decimal`:return()=>{throw Va(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return $o(e,t);case`Apply`:return ps(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new f(`An operator cannot be used as a value at ${q(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 La(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(qa(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw new f(`A primed expression appears outside an action at ${q(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=>Be(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 Ia(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(qa(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new f(`No CASE guard is true at ${q(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(Ka(n,Ao(e.definitions,n)))}case`Forall`:case`Exists`:return ms(e,t);case`UnboundedQuantifier`:return()=>{throw new f(`TLC cannot evaluate an unbounded quantifier at ${q(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return bs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>gt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>I(t.map(t=>t(e)))}case`FunctionConstruction`:return vs(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 xs(e.fields,t);case`RecordSet`:{let t=[...e.fields].sort((e,t)=>F(e.name,t.name)),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 ys(e,t);case`ExceptAt`:return t=>{let n=Ua(t.scope,`@`);if(n?.kind!==`Value`)throw new f(`@ appears outside an EXCEPT at ${q(e)}`);return n.value};case`FunctionApplication`:{_s(e.function);let t=n(e.function),r=e.args.map(n);if(r.length===1){let e=r[0];return n=>rn(t(n),e(n))}return e=>rn(t(e),I(r.map(t=>t(e))))}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Ra(e);return e=>rn(t(e),r,i)}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?qa(t(e),`an action`)||r(e):qa(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>ka(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new f(`The subexpression reference at ${q(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new f(`TLC cannot evaluate a temporal formula in a state at ${q(e)}`)}}},Cs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),ws=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ts=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Cs.has(e.kind))return!1;let a=ws(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&&Po(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 Ja(e).every(i)};return i(e)?[...n]:void 0},Es=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
|
|
20
20
|
(***************************************************************************)
|
|
21
21
|
(* This module provides dummy definitions of the operators that are *)
|
package/dist/transfer.js
CHANGED
|
@@ -15,7 +15,7 @@ const e={success:0,assumptionViolation:10,deadlock:11,safetyViolation:12,livenes
|
|
|
15
15
|
`,t:` `,r:`\r`,f:`\f`}[t]??t),c(2)}else a.push(e[i.index]),c(1)}c(1),n.push({kind:`String`,text:a.join(``),line:t,column:r,endColumn:o()-1});continue}let r=te.exec(e.slice(i.index,i.index+64));if(r){s(`Number`,r[0],r[0].length);continue}if(ie(t)){let t=e.slice(i.index,C(e,i.index,ie));if(/^\d+$/.test(t)){let n=/^\d+(\.\d+)?/.exec(e.slice(i.index))?.[0]??t;s(n.includes(`.`)?`Decimal`:`Number`,n,n.length);continue}if(t.startsWith(`WF_`)||t.startsWith(`SF_`)){s(`Keyword`,t.slice(0,3),3);continue}s(v.has(t)?`Keyword`:`Identifier`,t,t.length);continue}let a=w(e,i.index);if(a>0){s(`Step`,e.slice(i.index,i.index+a),a);continue}let u=ae(e,i.index);if(u)s(`Operator`,u.text,u.length);else throw l(`Unexpected character ${JSON.stringify(t)}`)}let d=o();return n.push({kind:`End`,text:``,line:i.line,column:d,endColumn:d}),n},E=/* @__PURE__ */ new Set([`CONSTANT`,`CONSTANTS`,`INIT`,`NEXT`,`SPECIFICATION`,`INVARIANT`,`INVARIANTS`,`PROPERTY`,`PROPERTIES`,`CONSTRAINT`,`CONSTRAINTS`,`ACTION_CONSTRAINT`,`ACTION_CONSTRAINTS`,`SYMMETRY`,`VIEW`,`CHECK_DEADLOCK`,`ALIAS`,`POSTCONDITION`,`POSTCONDITIONS`,`_POSSIBLE`,`_PERIODIC`,`_RL_REWARD`]),D=[`<-`,`<<`,`>>`,`=`,`{`,`}`,`,`,`[`,`]`,`(`,`)`],ce=e=>{let t=[],n={index:0,line:1};for(;n.index<e.length;){let r=e.slice(n.index),i=/^\s+/.exec(r)?.[0];if(i){n.line+=i.split(`
|
|
16
16
|
`).length-1,n.index+=i.length;continue}if(r.startsWith(`\\*`)){let e=r.indexOf(`
|
|
17
17
|
`);n.index+=e===-1?r.length:e;continue}if(r.startsWith(`(*`)){let e=r.indexOf(`*)`);if(e===-1)throw new d(`Unterminated comment at line ${n.line}`);n.line+=r.slice(0,e).split(`
|
|
18
|
-
`).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=D.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let l=ae(e,n.index);if(!l)throw new d(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:l.text,line:n.line}),n.index+=l.length}return t.push({kind:`End`,text:``,line:n.line}),t},O=e=>{let t=ce(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`&&E.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new d(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},l=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new d(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},u=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;u();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new d(`The keyword ${t} appears twice`);r[e]=l()},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 d(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=l();return c(`]`),e},g=()=>{for(;u();){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:l(),...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 d(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new d(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new d(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new d(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},le={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:` `}},ue=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},de=e=>e!==void 0&&/^\p{Nd}$/u.test(e),fe=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),pe=e=>{let t=t=>{let n=C(e,t,ue);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=C(e,t,de);if(e.startsWith(`..`,n)&&de(e[n+2]))return{kind:`Constant`,first:t,last:C(e,n+2,de)-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=de(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:C(e,t+1,de)-1,children:[]};if(fe(n))return{kind:`Constant`,first:t,last:C(e,t,fe)-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)},me=e=>{let t=e.length===0?void 0:pe(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=le[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},k=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,A=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),j=e=>typeof e==`object`&&e.kind===`ModelValue`,he=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:k(e)?`set`:`function`,ge=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new p(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},_e={isUsed:!1},ve=(e,t)=>(_e.isUsed=!0,e.writtenKeys=t,e),ye=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Jt(e.domain,t)]]),be=(e,t,n)=>ve(ze(e,t),n),xe=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(xe);e.writtenKeys=void 0,e.range.forEach(xe)}},Se=e=>e.every(e=>typeof e==`string`),Ce=(e,t)=>e.length>0&&t.length>0&&Se(e)!==Se(t),we=(e,t)=>{_e.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Ce(n,r)?at(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=F(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=F(i[e],a[e]);if(t!==0)return t}return 0},M=(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=F(n[e],r[e]);if(t!==0)return t}return 0},Te=e=>j(e)?e.type:void 0,N=(e,t,n)=>{let r=Te(e),i=Te(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(j(e)&&j(t)){if(r===void 0||i===void 0||r===i)return;throw new f(`Attempted to ${a} the differently-typed model values ${H(e)} and ${H(t)}`)}throw r===void 0?new f(`Attempted to ${a} the typed model value ${H(t)} and the non-model value\n${H(e)}`):new f(`Attempted to ${a}${n?``:` the`} typed model value ${H(e)} and non-model value\n${H(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0;for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,Ae=e=>Ee.ranks[Oe(e)],je=(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},P=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),Me=(e,t)=>(P(e)||P(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Ne=(e,t,n)=>{if(!P(e))throw new f(`Attempted to ${n} the set ${H(L(e)?R(e):e,!0)} with the value:\n${H(t,!0)}`);if(!P(t))throw new f(`Attempted to compare overridden value ${H(e,!0)} with non-overridden value:\n${H(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 f(`Attempted to compare ${H(e,!0)} with the value\n${H(t,!0)}`)},Pe={kind:`InfiniteSet`,name:`Any`},Fe=e=>{throw new f(`Attempted to compare ANY with the value\n${H(e)}`)},F=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e===Pe?Fe(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return we(e,t);if(n===`Set`&&r===`Set`)return M(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&&N(e,t,!1),je(e.name,t.name);if(n===`ModelValue`||r===`ModelValue`)return N(e,t,!1),n===`ModelValue`?-1:1;if(e===Pe||t===Pe)return Fe(e===Pe?t:e);if(k(e)&&k(t)&&Me(e,t))return Ne(e,t,`compare`);let i=k(e);if(i!==k(t))throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`);return i?M(R(e),R(t)):we(V(e),V(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return je(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(N(e,t,!1),j(e))return-1;if(j(t))return 1;throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`)},Ie=/* @__PURE__ */ new Map,Le=e=>{Oe(e);let t=Ie.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0};return Ie.set(e,n),n},Re=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),ze=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Be=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e!==Pe||Fe(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&N(e,t,!0),F(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 we(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return M(e,t)===0;if(j(e)||j(t))return N(e,t,!0),j(e)&&j(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Be(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=z(e.domain),r=z(t.domain);if(n||r)return n===r;let i=z(e.range),a=z(t.range);return i||a?i===a:Be(e.domain,t.domain)&&Be(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(z(e))return z(t);if(z(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]&&Be(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return z(e)?z(t):!z(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Be(e,t.factors[n]));let n=k(e)&&k(t)&&!(L(e)&&L(t));if(n&&Me(e,t))return Ne(e,t,`check equality of`)===0;if(n){if(!P(e)){let n=R(e);if(!P(t))return M(n,R(t))===0}return wn(e)===wn(t)}return F(e,t)===0},Ve=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>He(e,t[n])),He=(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)&&Ve(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&Ve(e.domain,t.domain)&&Ve(e.range,t.range):wn(e)===wn(t),Ue=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},We=e=>{if(!A(e))return;let t=V(e);return{domain:t.domain,range:t.range,low:Ue(t.domain)}},Ge=e=>{if(!A(e))return;let t=V(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},Ke=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to compare the typed model value ${H(e)} and the non-model value\n${H(t)}`);return 1},qe=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to check equality of the typed model value ${H(e)} and the non-model value\n${H(t)}`);return!1},Je=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)?je(e.name,t.name):-1;if(!j(t))throw new f(`Attempted to compare the typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to compare the differently-typed model values ${H(e)} and ${H(t)}`);return je(e.name,t.name)},Ye=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)&&e.name===t.name;if(!j(t))throw new f(`Attempted to check equality of typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to check equality of the differently-typed model values ${H(e)} and ${H(t)}`);return e.name===t.name},Xe={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`}},Ze=(e,t,n)=>{let r=Xe[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${H(e)} with ${r.other}:\n${H(t)}`},Qe=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return Ke(n,t);throw new f(`Attempted to compare the function ${H(t)} with the value:\n${H(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>$e(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 $e(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 f(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${H(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},$e=(e,t)=>{for(let n=0;n<e.length;n++){let r=at(e[n],t[n]);if(r!==0)return r}return 0},et=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length-r.length||$e(n.range,r):Qe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return Ke(t,e);throw new f(`Attempted to compare record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length-r.domain.length||$e(n.domain,r.domain)||$e(n.range,r.range)}return Qe({...n,low:Ue(n.domain)},e,t)},tt=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),nt=(e,t)=>P(e)||P(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),rt=(e,t)=>{let n=tt(t)?t:R(t);return{elements:R(e).elements,otherElements:R(n).elements}},it=(e,t)=>{if(nt(e,t))return F(e,t);if(!tt(e))return it(R(e),t);if(!k(t)){if(j(t))return Ke(t,e);throw new f(`Attempted to compare the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length-n.otherElements.length||$e(n.elements,n.otherElements)},at=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return F(e,t);if(j(t))return Ke(t,e);throw new f(Ze(e,t,!1))}return e.kind===`ModelValue`?Je(e,t):A(e)?et(e,t):it(e,t)},ot=(e,t)=>e.every((e,n)=>ut(e,t[n])),st=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return qe(n,t);throw new f(`Attempted to check equality of the function ${H(t)} with the value:\n${H(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)&&ot(e.range,r.range);let i=e.low??r.low;return i===void 0?ot(e.domain,r.domain)&&ot(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new f(`Attempted to compare an integer with non-integer:\n${H(e)}.`);return e===i+t})&&ot(e.range,r.range)},ct=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length===r.length&&ot(n.range,r):st({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length===r.domain.length&&ot(n.domain,r.domain)&&ot(n.range,r.range)}return st({...n,low:Ue(n.domain)},e,t)},lt=(e,t)=>{if(nt(e,t))return Be(e,t);if(!tt(e))return lt(R(e),t);if(!k(t)){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length===n.otherElements.length&&ot(n.elements,n.otherElements)},ut=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(j(t))return qe(t,e);throw new f(Ze(e,t,!0))}return e.kind===`ModelValue`?Ye(e,t):A(e)?ct(e,t):lt(e,t)},dt=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=at(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},ft=(e,t)=>t.some(t=>ut(t,e)),pt=(e,t)=>{try{return Be(e,t)}catch(n){if(!(n instanceof f))throw n;return ut(e,t)}},mt=e=>{for(let t=1;t<e.length;t++)if(F(e[t-1],e[t])>=0)return!1;return!0},ht=e=>{if(e.length<2||mt(e))return Re(e);let t=[...e].sort(F),n=[t[0]];for(let e=1;e<t.length;e++)F(n[n.length-1],t[e])!==0&&n.push(t[e]);return Re(n)},gt=e=>{try{return ht(e)}catch(t){if(!(t instanceof f))throw t;return{kind:`MixedSet`,elements:e}}},_t=Re([]),vt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0||i.push(t)}return i.length===n.length?e:Re(i)},yt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Re(i)},bt=(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=F(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=Re(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+fn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+yn(a[e])|0;c.fingerprint=t}return c},xt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>F(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&F(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return ze(n,r)},St=e=>{let t=xt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:ve(t,e.map(e=>e[0]))},Ct=[],wt=[],Tt=/* @__PURE__ */ new WeakMap,Et=e=>{let t=Ct[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return Ct[e]=n,n},Dt=e=>{if(Gt(e),e.low===1&&e.high>=1)return Et(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Ot=e=>e.length>0&&(e===Ct[e.length]||Tt.has(e)),kt=e=>{let t=R(e).elements;return Ot(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},At=e=>{let t=wt[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return wt[e]=n,n},I=e=>ze(Et(e.length),e),jt=e=>xt(e),Mt=e=>k(e)&&!P(e),Nt=(e,t=!1)=>`UNION(${H(e.base,t)})`,Pt=e=>{if(!Mt(e.base))throw new f(`Attempted to enumerate the nonenumerable set:\n${Nt(e)}`);return R(e.base).elements},Ft=(e,t,n)=>{if(Mt(e))return e;throw new f(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${H(e)}\nwhen enumerating:\n${Nt(n)}`)},L=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 L(e.base);case`FunctionSet`:return L(e.domain)&&L(e.range)||It(e.domain)===!0||It(e)===!0;case`RecordSet`:return e.sets.every(L)||It(e)===!0;case`ProductSet`:return e.factors.every(L)||It(e)===!0;case`Union`:return L(e.base)&&R(e.base).elements.every(L);case`SetOperation`:return e.operator===`\\cup`?L(e.left)&&L(e.right):e.operator===`\\cap`?L(e.left)||L(e.right):L(e.left);default:return!1}},It=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=It(e.domain);return t===void 0?void 0:!t&&It(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=It(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return L(e)?R(e).elements.length===0:void 0;default:return}},Lt=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},Rt=(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},zt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Re(r.map(t=>e[t])));while(Rt(r,e.length))}return t},Bt=e=>{if(typeof e!=`object`)throw new f(`Expected a set but found ${H(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return dt(e.elements);case`Interval`:{let t=Dt(e);return t!==Ct[t.length]&&Tt.set(t,e),t}case`Subset`:{P(e.base)&&Gt(e.base);let t=R(e.base).elements;if(t.length>30)throw new f(`SUBSET of a set with ${t.length} elements is too large to enumerate`);return zt(t)}case`FunctionSet`:{if(z(e))return[];if(P(e.domain))throw new f(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${H(e.domain)}\ncannot be enumerated.`);let t=kt(e.domain);if(t.length>0&&P(e.range))throw new f(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${H(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:R(e.range).elements;return Lt(t.map(()=>n)).map(e=>ze(t,e))}case`RecordSet`:{if(z(e))return[];let t=e.sets.map((t,n)=>{if(P(t))throw new f(`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${H(t)}`);return R(t).elements});return Lt(t).map(t=>jt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(z(e))return[];let t=e.factors.map((e,t)=>{if(P(e))throw new f(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${H(e)}`);return R(e).elements});return Lt(t).map(I)}case`FilteredSet`:if(P(e.base))throw new f(`Attempted to enumerate { x \\in S : p(x) } when S:\n${H(e.base)}\nis not enumerable`);return R(e.base).elements.filter(e.test);case`Union`:{let t=Pt(e).flatMap((t,n)=>R(Ft(t,n,e)).elements);return R(gt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(P(e.left)||P(e.right)))throw new f(`Attempted to enumerate S \\cup T when S:\n${H(e.left)}\nand T:\n${H(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&P(e.left)&&P(e.right))throw new f(`Attempted to enumerate S \\cap T when neither S:\n${H(e.left)}\nnor T:\n${H(e.right)}\nis enumerable`);if(e.operator===`\\`&&P(e.left))throw new f(`Attempted to enumerate S \\ T when S:\n${H(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return bt(R(e.left),R(e.right)).elements;let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return R(n).elements.filter(t=>B(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to enumerate the non-enumerable set ${H(e)}`);default:throw new f(`Expected a set but found ${H(e)}`)}},Vt=/* @__PURE__ */ new WeakMap,R=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Re(Bt(e));let t=typeof e==`object`?Vt.get(e):void 0;if(t)return t;let n=Bt(e),r=typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Re(n):ht(n);return typeof e==`object`&&Vt.set(e,r),r},Ht=e=>{if(typeof e!=`object`||Vt.has(e))return R(e).elements;if(e.kind===`Union`)return Pt(e).flatMap((t,n)=>Ht(Ft(t,n,e)));if(e.kind===`FilteredSet`&&!P(e.base))return Ht(e.base).filter(e.test);if(e.kind!==`SetOperation`)return R(e).elements;if(e.operator===`\\cup`)return P(e.left)||P(e.right)?R(e).elements:[...Ht(e.left),...Ht(e.right)];let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return P(n)?R(e).elements:Ht(n).filter(t=>B(t,r)===(e.operator===`\\cap`))},z=e=>{if(typeof e!=`object`||!k(e))throw new f(`Shouldn't call isEmpty() on value ${H(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 f(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(z);case`RecordSet`:return e.sets.some(z);case`FunctionSet`:return!z(e.domain)&&z(e.range);case`Union`:return Pt(e).every((t,n)=>z(Ft(t,n,e)));default:return R(e).elements.length===0}},Ut=(e,t)=>e.reduce((e,n)=>{let r=e*Gt(n);if(r>2147483647)throw new f(t());return r},1),Wt=e=>`Overflow when computing the number of elements in:\n${H(e)}`,Gt=e=>{if(typeof e!=`object`)return R(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new f(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to compute the number of elements in the overridden value ${H(e)}.`);case`Subset`:{let t=Gt(e.base);if(t>=31)throw new f(Wt(e));return 2**t}case`FunctionSet`:{if(z(e.domain))return 1;if(z(e.range))return 0;let t=Gt(e.range);if(t===1)return 1;let n=Gt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new f(Wt(e));return r.value}case`RecordSet`:return z(e)?0:Ut(e.sets,()=>Wt(e));case`ProductSet`:return z(e)?0:Ut(e.factors,()=>`Overflow when computing the number of elements in ${H(e)}`);default:return R(e).elements.length}},Kt=e=>`Attempted to check if the set ${H(e)}is finite.`,qt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new f(`Attempted to check if the ${he(e)} ${H(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new f(`Attempted to check if the function:\n${H(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return qt(e.base)&&z(e.base);case`Subset`:return qt(e.base);case`FunctionSet`:{let t=qt(e.domain);if(t&&z(e.domain))return!0;let n=qt(e.range);return n&&z(e.range)?!0:n&&(t||Gt(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(!qt(e))n.isAllFinite=!1;else if(z(e))return!0;return n.isAllFinite}case`Union`:if(!Mt(e.base))throw new f(`Attempted to check if the nonenumerable set:\n${Nt(e)}\nis a finite set.`);return R(e.base).elements.every(qt);case`SetOperation`:if(e.operator===`\\cup`)return qt(e.left)&&qt(e.right);if(e.operator===`\\cap`){if(!qt(e.left)&&!qt(e.right))throw new f(Kt(e));return!0}if(qt(e.left))return!0;if(!qt(e.right))throw new f(Kt(e));return!1;case`FilteredSet`:if(!qt(e.base))throw new f(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${H(e.base)}\nis finite.`);return!0}},Jt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=F(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Yt=e=>{if(typeof e!=`object`||!A(e))return;let t=V(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Xt=(e,t)=>`Attempted to check if the value ${H(e)} is an element of ${H(t)}`,Zt=(e,t)=>{let n=`Attempted to check if the value:\n${H(e)}\nis an element of the`;if(!A(t))return`${n} ${he(t)} ${H(t)}`;let r=t.kind===`Function`||L(t.domain)?V(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${H(e)}\nis in the record:\n${H(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${H(t)}`},B=(e,t)=>{if(!k(t))throw new f(Zt(e,t));switch(t.kind){case`Set`:try{return Jt(t.elements,e)!==-1}catch(n){if(!(n instanceof f))throw n;return ft(e,t.elements)}case`MixedSet`:return ft(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!j(e))throw new f(`Attempted to check if the value:\n${H(e)}\nis in the integer interval ${H(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(j(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new f(`Attempted to check if the value:\n${H(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!k(e))throw new f(Xt(e,t));return R(e).elements.every(e=>B(e,t.base));case`FunctionSet`:{if(!A(e)){if(j(e))return!1;throw new f(Xt(e,t))}let n=V(e),r=Ue(n.domain);return(r===void 0?pt(t.domain,Re(n.domain)):pt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>B(e,t.range))}case`RecordSet`:{let n=A(e)?V(e):void 0;if(!n||!Se(n.domain)){if(j(e))return!1;throw new f(`Attempted to check if non-record\n${H(e)}\nis in the set of records:\n${H(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Jt(n.domain,e);return i!==-1&&B(n.range[i],t.sets[r])})}case`ProductSet`:{let n=A(e)?Yt(e):void 0;if(n===void 0){if(A(e)&&V(e).domain.every(e=>typeof e==`number`)||j(e))return!1;throw new f(`Attempted to check if non-tuple\n${H(e)}\nis in the set of tuples:\n${H(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>B(e,t.factors[n]))}case`FilteredSet`:return B(e,t.base)&&t.test(e);case`Union`:if(!Mt(t.base))throw new f(`Attempted to check if:\n ${H(e)}\nis an element of the non-enumerable set:\n ${Nt(t)}`);return R(t.base).elements.some(t=>B(e,t));case`SetOperation`:return t.operator===`\\cup`?B(e,t.left)||B(e,t.right):B(e,t.left)&&B(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Yt(e);if(n===void 0){if(j(e))return!1;throw new f(Xt(e,t))}return n.every(e=>B(e,t.base))}default:throw new f(Zt(e,t))}},Qt=(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?B(e,n):e.range.every((e,r)=>Qt(e,t.range[r],n.range)),V=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){L(e.domain)||Gt(e.domain);let t=kt(e.domain);return ze(t,t.map(t=>e.apply(t)))}throw new f(`Expected a function but found ${H(e)}`)},$t=/* @__PURE__ */ new WeakSet,en=e=>($t.add(e),e),tn=(e,t)=>typeof e==`object`&&$t.has(e)?`In applying the function\n${me(H(e))},\nthe first argument is:\n${me(H(t))}\nwhich is not in its domain.\n`:`Attempted to apply function ${H(e)} to ${H(t)}, which is not in its domain`,nn=(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 Jt(e,t)},rn=(e,t,n)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!B(t,e.domain))throw new f($t.has(e)?tn(e,t):`Attempted to apply a function to ${H(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new f(`Attempted to apply ${H(e)}, which is not a function`);let r=nn(e.domain,t);if(r===-1)throw new f(n?.(e)??tn(e,t));return e.range[r]},an=e=>Ot(e)?Tt.get(e)??At(e.length):void 0,on=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=V(e).domain;return an(t)??Re(t)},sn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!L(e.domain))){if(!B(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>pt(t,n)?r:e.apply(n),source:e.source}}let i=V(e),a=nn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=ze(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-pn(e,s)+pn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-bn(e,s)+bn(e,c)|0}return l},cn=/* @__PURE__ */ new WeakMap,ln=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},un=(e,t)=>Math.imul(e^t,16777619),dn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return ln(t)},fn=e=>ln(hn(e)+2654435769|0),pn=(e,t)=>ln(Math.imul(hn(e),668265261)+hn(t)|0),mn=e=>{if(e.kind===`ModelValue`)return ln(un(dn(e.name),109));if(k(e)){if(!L(e))return dn(wn(e));let t=R(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+fn(t[e])|0;return n}let t=V(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+pn(t.domain[e],t.range[e])|0;return n},hn=e=>{if(typeof e==`number`)return ln(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return dn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=mn(e)),e.hash;let t=cn.get(e);if(t!==void 0)return t;let n=mn(e);return cn.set(e,n),n},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},vn=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return _n(t)},yn=e=>_n(Sn(e)+2135587861|0),bn=(e,t)=>_n(Math.imul(Sn(e),374761393)+Sn(t)|0),xn=e=>{if(e.kind===`ModelValue`)return _n(vn(e.name)^60);if(k(e)){if(!L(e))return vn(wn(e));let t=R(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+yn(t[e])|0;return n}let t=V(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+bn(t.domain[e],t.range[e])|0;return n},Sn=e=>{if(typeof e==`number`)return _n(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return vn(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=xn(e)),e.fingerprint;let t=gn.get(e);if(t!==void 0)return t;let n=xn(e);return gn.set(e,n),n},Cn=/* @__PURE__ */ new WeakMap,wn=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=Cn.get(e);if(t!==void 0)return t;let n=Tn(e);return Cn.set(e,n),n},Tn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${wn(e.base)}>`;if(e.kind===`FilteredSet`&&!L(e))throw new f(`Attempted to compare the non-enumerable set ${H(e)} with another value`);if(k(e))return L(e)?`{${R(e).elements.map(wn).join(`,`)}}`:`<${e.kind} ${H(e)}>`;let t=V(e);return`(${t.domain.map((e,n)=>`${wn(e)}:${wn(t.range[n])}`).join(`,`)})`},En=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Dn=e=>e.reduce((e,t)=>e===0?0:e*Gt(t),1),On=e=>{if(e.kind===`Subset`)return Gt(e.base)<=6?R(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return R(e);if(e.kind===`FunctionSet`&&L(e.domain)&&Gt(e.domain)===0)return Re([ze([],[])]);let t=e.kind===`FunctionSet`?Gt(e.range)**Gt(e.domain):Dn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?_t:t<=1999?R(e):void 0},kn=(e,t)=>{if(t)return On(e);if(L(e))try{return On(e)}catch(e){if(e instanceof f)return;throw e}},An=(e,t)=>{let n=e=>H(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 Nt(e,t)}},jn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),H=(e,t=!1,n=!1)=>{let r=e=>H(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return En(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(R(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=kn(e,t);return n?r(n):An(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!L(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${H(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let t=V(e);return t.domain.every((e,t)=>e===t+1)?`<<${t.range.map(r).join(`, `)}>>`:t.domain.every(e=>typeof e==`string`&&jn(e))?`[${(n||t.hash!==0||t.fingerprint!==0?t.domain.map((e,n)=>[e,t.range[n]]):ye(t)).map(e=>`${String(e[0])} |-> ${r(e[1])}`).join(`, `)}]`:`(${t.domain.map((e,n)=>`${r(e)} :> ${r(t.range[n])}`).join(` @@ `)})`}}},Mn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Nn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=hn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||He(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||He(i[e],n))return i[e]}return n.kind===`Set`?Mn(n.elements,t):(Mn(n.domain,t),Mn(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}},Pn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Fn=(e,t)=>Pn.has(e)?new f(t):new p(t),In=e=>typeof e==`object`&&e.kind===`Operator`,U=(e,t,n)=>{let r=e[t];if(r===void 0||In(r))throw Fn(n,`${n} expects a value as argument ${t+1}`);return r},Ln=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`number`)throw Fn(n,`${n} expects an integer but received ${H(r)}`);return r},Rn=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`boolean`)throw Fn(n,`${n} expects a boolean but received ${H(r)}`);return r},zn=(e,t,n)=>{let r=U(e,t,n);if(!k(r))throw Fn(n,`${n} expects a set but received ${H(r)}`);return r},Bn=(e,t,n)=>{let r=zn(e,t,n);if(!L(r))throw Fn(n,`${n} cannot enumerate the non-enumerable set ${H(r)}`);return r},Vn=(e,t,n)=>R(Bn(e,t,n)),Hn=(e,t,n)=>Vn(e,t,n).elements,Un=(e,t,n)=>{let r=U(e,t,n),i=Yt(r);if(!i)throw Fn(n,`${n} expects a sequence but received ${H(r)}`);return i},Wn=(e,t,n)=>{let r=U(e,t,n);if(!A(r))throw Fn(n,`${n} expects a function but received ${H(r)}`);return V(r)},Gn=(e,t,n)=>{let r=e[t];if(r===void 0||!In(r))throw Fn(n,`${n} expects an operator as argument ${t+1}`);return r},Kn=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),W=(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}),qn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),Jn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),Yn=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(U(n,0,e),U(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),Xn=e=>e>2147483647||e<-2147483648,Zn=e=>new p(`Overflow when computing ${e}`),Qn={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`},$n={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},er={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`},tr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return $n[e.operator];if(e.kind!==`Function`)return Qn[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},nr=e=>er[tr(e)],rr=(e,t)=>new f(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),ir=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof f&&(e.isLazy===!0||!(n instanceof p))?rr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},ar=(e,t)=>{if(typeof e!=`number`)throw rr(t,`Cannot cast tlc2.value.impl.${tr(e)} to tlc2.value.impl.IntValue`);return e},or=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,sr=(e,t,n)=>{let r=or(t,`IntValue`,2);return Yn(e,(t,i)=>{let a=ar(t,r),o=ar(i,r),s=n(a,o);if(Xn(s))throw Zn(`${a}${e}${o}`);return s})},cr=(e,t,n)=>{if(typeof e!=`number`)throw new p(`The ${t} argument of ${n} should be an integer, but instead it is:\n${H(e)}`);return e},lr=(e,t)=>Yn(e,(n,r)=>t(cr(n,`first`,e),cr(r,`second`,e))),ur=(e,t,n)=>({...Jn(e,t,n),isLazy:!0}),dr=(e,t,n)=>{let r=Gn(e,t,n);return()=>r.call([])},fr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),pr=()=>({bool:qa,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ia(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw La(t,n,e);return e},apply:rn,tuple:I,set:gt,record:ze,writtenRecord:be,equal:Be,enumerate:$a,unassigned:(e,t)=>{throw new f(`The variable ${e} is not assigned at ${q(t)}`)},primeOutsideAction:e=>{throw new f(`A primed expression appears outside an action at ${q(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)=>Wa(e,t,n[r]),e.scope),state:e.state,next:e.next})}),mr={value:void 0},hr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),gr=e=>`local${e.counter++}`,_r=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Ja(e).forEach(e=>_r(e,t)),t),vr=(e,t,n)=>{let r=hr(e,Y(t,e.environment)),i=_r(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=hr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},yr=(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(()=>gr(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${xr(e,t.set,s)}, ${hr(e,t.set)});`),u=t.bounds.map(t=>{let n=gr(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${xr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=gr(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})`},br=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${xr(e,r[0],n)}, "the left side of =>") ? bool(${xr(e,r[1],n)}, "the right side of =>") : true)`;if(J(t))return vr(e,t,n);let i=mo(t.operator,e.environment);if(i?.kind!==`Builtin`)return vr(e,t,n);if(i.name===`DOMAIN`)return`${hr(e,za(t))}(${xr(e,r[0],n)})`;if(i.name===`TLCEval`)return vr(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${hr(e,Ba(t,i))}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&rs(r[0],a))return`${hr(e,i.evaluateUnary)}(${xr(e,r[0],n)})`;let o=r.length===2&&rs(r[0],a)&&rs(r[1],a);return i.evaluateBinary&&o?`${hr(e,i.evaluateBinary)}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`:vr(e,t,n)},xr=(e,t,n)=>{let r=(t,r=n)=>xr(e,t,r);if(qo(t)&&Ho(t,e.environment))return vr(e,t,n);switch(t.kind){case`Number`:return Mo(t.value)?String(t.value):vr(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(J(t))return vr(e,t,n);let i=wo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=hr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?hr(e,i.value):vr(e,t,n)}case`Apply`:return br(e,t,n);case`And`:{let n=hr(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)}, ${hr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{_s(t.function);let e=r(t.function);return t.args.length===1?`apply(${e}, ${r(t.args[0])})`:`apply(${e}, tuple([${t.args.map(e=>r(e)).join(`, `)}]))`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${hr(e,Ra(t))})`;case`Record`:{let i=[...t.fields].sort((e,t)=>F(e.name,t.name));if(i.some((e,t)=>t>0&&i[t-1].name===e.name))return vr(e,t,n);let a=hr(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}, ${hr(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=hr(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`)?yr(e,t,n):vr(e,t,n);default:return vr(e,t,n)}},Sr=(e,t)=>{if(!fr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=xr(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(`
|
|
18
|
+
`).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=D.find(e=>r.startsWith(e));if(c){t.push({kind:`Symbol`,text:c,line:n.line}),n.index+=c.length;continue}let l=ae(e,n.index);if(!l)throw new d(`Unexpected character ${JSON.stringify(r[0])} at line ${n.line}`);t.push({kind:`Word`,text:l.text,line:n.line}),n.index+=l.length}return t.push({kind:`End`,text:``,line:n.line}),t},O=e=>{let t=ce(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`&&E.has(e.text),c=e=>{let t=o();if(t.text!==e)throw new d(`Expected ${JSON.stringify(e)} but found ${JSON.stringify(t.text)} at line ${t.line}`)},l=()=>{let e=o();if(e.kind!==`Word`||s(e))throw new d(`Expected a name but found ${JSON.stringify(e.text)} at line ${e.line}`);return e.text},u=()=>a().kind===`Word`&&!s(a()),f=()=>{let e=[];for(;u();)e.push(o().text);return e},p=(e,t)=>{if(r[e]!==void 0)throw new d(`The keyword ${t} appears twice`);r[e]=l()},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 d(`Expected a value but found ${JSON.stringify(e.text)} at line ${e.line}`)},h=()=>{if(a().text!==`[`)return;o();let e=l();return c(`]`),e},g=()=>{for(;u();){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:l(),...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 d(`The keyword CHECK_DEADLOCK appears twice`);i.isSet=!0;let e=o().text;if(e!==`TRUE`&&e!==`FALSE`)throw new d(`CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(e)}`);r.checkDeadlock=e===`TRUE`;break}default:throw new d(`Unexpected ${JSON.stringify(e.text)} at line ${e.line}`)}}if(r.specification!==void 0&&(r.init!==void 0||r.next!==void 0))throw new d(`SPECIFICATION cannot be combined with INIT or NEXT`);return r},le={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:` `}},ue=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},de=e=>e!==void 0&&/^\p{Nd}$/u.test(e),fe=e=>e!==void 0&&/^[\p{L}\p{Nd}]$/u.test(e),pe=e=>{let t=t=>{let n=C(e,t,ue);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=C(e,t,de);if(e.startsWith(`..`,n)&&de(e[n+2]))return{kind:`Constant`,first:t,last:C(e,n+2,de)-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=de(n)?i(t):void 0;if(r)return r;if(n===`-`)return t+1>=e.length?void 0:{kind:`Constant`,first:t,last:C(e,t+1,de)-1,children:[]};if(fe(n))return{kind:`Constant`,first:t,last:C(e,t,fe)-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)},me=e=>{let t=e.length===0?void 0:pe(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=le[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},k=e=>typeof e==`object`&&e.kind!==`ModelValue`&&e.kind!==`Function`&&e.kind!==`LazyFunction`,A=e=>typeof e==`object`&&(e.kind===`Function`||e.kind===`LazyFunction`),j=e=>typeof e==`object`&&e.kind===`ModelValue`,he=e=>typeof e==`boolean`?`boolean`:typeof e==`number`?`integer`:typeof e==`string`?`string`:e.kind===`ModelValue`?`model value`:k(e)?`set`:`function`,ge=e=>{if(!Number.isInteger(e)||e>2147483647||e<-2147483648)throw new p(`Integer overflow: ${e} is outside TLC's 32-bit range`);return e},_e={isUsed:!1},ve=(e,t)=>(_e.isUsed=!0,e.writtenKeys=t,e),ye=e=>(e.writtenKeys??e.domain).map(t=>[t,e.range[Jt(e.domain,t)]]),be=(e,t,n)=>ve(ze(e,t),n),xe=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(xe);e.writtenKeys=void 0,e.range.forEach(xe)}},Se=e=>e.every(e=>typeof e==`string`),Ce=(e,t)=>e.length>0&&t.length>0&&Se(e)!==Se(t),we=(e,t)=>{_e.isUsed&&(e.writtenKeys=void 0,t.writtenKeys=void 0);let n=e.domain,r=t.domain;if(n.length!==r.length)return Ce(n,r)?at(e,t):n.length-r.length;if(n!==r)for(let e=0;e<n.length;e++){let t=F(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=F(i[e],a[e]);if(t!==0)return t}return 0},M=(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=F(n[e],r[e]);if(t!==0)return t}return 0},Te=e=>j(e)?e.type:void 0,N=(e,t,n)=>{let r=Te(e),i=Te(t);if(r===void 0&&i===void 0)return;let a=n?`check equality of`:`compare`;if(j(e)&&j(t)){if(r===void 0||i===void 0||r===i)return;throw new f(`Attempted to ${a} the differently-typed model values ${H(e)} and ${H(t)}`)}throw r===void 0?new f(`Attempted to ${a} the typed model value ${H(t)} and the non-model value\n${H(e)}`):new f(`Attempted to ${a}${n?``:` the`} typed model value ${H(e)} and non-model value\n${H(t)}`)},Ee={ranks:Object.create(null),size:0},De=e=>{Ee.ranks=Object.create(null),Ee.size=0,Ie.clear();for(let t of e)Oe(t)},Oe=e=>(Ee.ranks[e]===void 0&&(Ee.ranks[e]=Ee.size++),e),ke=()=>Ee.size,Ae=e=>Ee.ranks[Oe(e)],je=(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},P=e=>typeof e==`object`&&(e.kind===`InfiniteSet`||e.kind===`SequenceSet`),Me=(e,t)=>(P(e)||P(t))&&(typeof e!=`object`||e.kind!==`SequenceSet`||typeof t!=`object`||t.kind!==`SequenceSet`),Ne=(e,t,n)=>{if(!P(e))throw new f(`Attempted to ${n} the set ${H(L(e)?R(e):e,!0)} with the value:\n${H(t,!0)}`);if(!P(t))throw new f(`Attempted to compare overridden value ${H(e,!0)} with non-overridden value:\n${H(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 f(`Attempted to compare ${H(e,!0)} with the value\n${H(t,!0)}`)},Pe={kind:`InfiniteSet`,name:`Any`},Fe=e=>{throw new f(`Attempted to compare ANY with the value\n${H(e)}`)},F=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e===Pe?Fe(e):0;if(typeof e==`object`&&typeof t==`object`){let n=e.kind,r=t.kind;if(n===`Function`&&r===`Function`)return we(e,t);if(n===`Set`&&r===`Set`)return M(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&&N(e,t,!1),e.rank-t.rank;if(n===`ModelValue`||r===`ModelValue`)return N(e,t,!1),n===`ModelValue`?-1:1;if(e===Pe||t===Pe)return Fe(e===Pe?t:e);if(k(e)&&k(t)&&Me(e,t))return Ne(e,t,`compare`);let i=k(e);if(i!==k(t))throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`);return i?M(R(e),R(t)):we(V(e),V(t))}if(typeof e==`number`&&typeof t==`number`)return e-t;if(typeof e==`string`&&typeof t==`string`)return je(e,t);if(typeof e==`boolean`&&typeof t==`boolean`)return e<t?-1:1;if(N(e,t,!1),j(e))return-1;if(j(t))return 1;throw new f(`Attempted to compare ${he(e)} ${H(e)} with ${he(t)} ${H(t)}`)},Ie=/* @__PURE__ */ new Map,Le=e=>{let t=Ie.get(e);if(t)return t;let n={kind:`ModelValue`,name:e,type:e.length>2&&e[1]===`_`?e[0]:void 0,rank:Ae(e)};return Ie.set(e,n),n},Re=e=>({kind:`Set`,elements:e,hash:0,fingerprint:0}),ze=(e,t)=>({kind:`Function`,domain:e,range:t,hash:0,fingerprint:0,writtenKeys:void 0}),Be=(e,t)=>{if(e===t)return _e.isUsed&&xe(e),e!==Pe||Fe(e);if(typeof e!=`object`||typeof t!=`object`)return typeof e!=typeof t&&((typeof e==`object`||typeof t==`object`)&&N(e,t,!0),F(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 we(e,t)===0;if(e.kind===`Set`&&t.kind===`Set`)return M(e,t)===0;if(j(e)||j(t))return N(e,t,!0),j(e)&&j(t)&&e.name===t.name;if(e.kind===`Subset`&&t.kind===`Subset`)return Be(e.base,t.base);if(e.kind===`FunctionSet`&&t.kind===`FunctionSet`){let n=z(e.domain),r=z(t.domain);if(n||r)return n===r;let i=z(e.range),a=z(t.range);return i||a?i===a:Be(e.domain,t.domain)&&Be(e.range,t.range)}if(e.kind===`RecordSet`&&t.kind===`RecordSet`){if(z(e))return z(t);if(z(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]&&Be(e[1],r[t][1]))}if(e.kind===`ProductSet`&&t.kind===`ProductSet`)return z(e)?z(t):!z(t)&&e.factors.length===t.factors.length&&e.factors.every((e,n)=>Be(e,t.factors[n]));let n=k(e)&&k(t)&&!(L(e)&&L(t));if(n&&Me(e,t))return Ne(e,t,`check equality of`)===0;if(n){if(!P(e)){let n=R(e);if(!P(t))return M(n,R(t))===0}return wn(e)===wn(t)}return F(e,t)===0},Ve=(e,t)=>e===t||e.length===t.length&&e.every((e,n)=>He(e,t[n])),He=(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)&&Ve(e.elements,t.elements):e.kind===`Function`&&t.kind===`Function`?(e.hash===0||t.hash===0||e.hash===t.hash)&&Ve(e.domain,t.domain)&&Ve(e.range,t.range):wn(e)===wn(t),Ue=e=>{let t=e.length===0?1:e[0];return typeof t==`number`&&e.every((e,n)=>e===t+n)?t:void 0},We=e=>{if(!A(e))return;let t=V(e);return{domain:t.domain,range:t.range,low:Ue(t.domain)}},Ge=e=>{if(!A(e))return;let t=V(e);return t.domain.every(e=>typeof e==`string`)?t:void 0},Ke=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to compare the typed model value ${H(e)} and the non-model value\n${H(t)}`);return 1},qe=(e,t)=>{if(Te(e)!==void 0)throw new f(`Attempted to check equality of the typed model value ${H(e)} and the non-model value\n${H(t)}`);return!1},Je=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)?e.rank-t.rank:-1;if(!j(t))throw new f(`Attempted to compare the typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to compare the differently-typed model values ${H(e)} and ${H(t)}`);return e.rank-t.rank},Ye=(e,t)=>{let n=Te(e);if(n===void 0)return j(t)&&e.name===t.name;if(!j(t))throw new f(`Attempted to check equality of typed model value ${H(e)} and non-model value\n${H(t)}`);let r=Te(t);if(r!==void 0&&r!==n)throw new f(`Attempted to check equality of the differently-typed model values ${H(e)} and ${H(t)}`);return e.name===t.name},Xe={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`}},Ze=(e,t,n)=>{let r=Xe[typeof e==`number`?`number`:typeof e==`string`?`string`:`boolean`];return`Attempted to ${n?r.equality:`compare`} ${r.name} ${H(e)} with ${r.other}:\n${H(t)}`},Qe=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return Ke(n,t);throw new f(`Attempted to compare the function ${H(t)} with the value:\n${H(n)}`)}if(e.range.length!==r.range.length)return e.range.length-r.range.length;let i=()=>$e(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 $e(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 f(`Attempted to compare integer with non-integer${e.low===void 0?``:`:`}\n${H(n)}.`);let r=e.low===void 0?n-(a+t):a+t-n;if(r!==0)return r}return i()},$e=(e,t)=>{for(let n=0;n<e.length;n++){let r=at(e[n],t[n]);if(r!==0)return r}return 0},et=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length-r.length||$e(n.range,r):Qe({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return Ke(t,e);throw new f(`Attempted to compare record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length-r.domain.length||$e(n.domain,r.domain)||$e(n.range,r.range)}return Qe({...n,low:Ue(n.domain)},e,t)},tt=e=>typeof e==`object`&&(e.kind===`Set`||e.kind===`MixedSet`),nt=(e,t)=>P(e)||P(t)||typeof t==`object`&&e.kind===t.kind&&(e.kind===`Interval`||e.kind===`Subset`||e.kind===`FunctionSet`||e.kind===`RecordSet`||e.kind===`ProductSet`),rt=(e,t)=>{let n=tt(t)?t:R(t);return{elements:R(e).elements,otherElements:R(n).elements}},it=(e,t)=>{if(nt(e,t))return F(e,t);if(!tt(e))return it(R(e),t);if(!k(t)){if(j(t))return Ke(t,e);throw new f(`Attempted to compare the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length-n.otherElements.length||$e(n.elements,n.otherElements)},at=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return F(e,t);if(j(t))return Ke(t,e);throw new f(Ze(e,t,!1))}return e.kind===`ModelValue`?Je(e,t):A(e)?et(e,t):it(e,t)},ot=(e,t)=>e.every((e,n)=>ut(e,t[n])),st=(e,t,n)=>{let r=We(n);if(!r){if(j(n))return qe(n,t);throw new f(`Attempted to check equality of the function ${H(t)} with the value:\n${H(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)&&ot(e.range,r.range);let i=e.low??r.low;return i===void 0?ot(e.domain,r.domain)&&ot(e.range,r.range):(e.low===void 0?e.domain:r.domain).every((e,t)=>{if(typeof e!=`number`)throw new f(`Attempted to compare an integer with non-integer:\n${H(e)}.`);return e===i+t})&&ot(e.range,r.range)},ct=(e,t)=>{let n=V(e);if(e.kind===`Function`&&Yt(n)!==void 0){let r=A(t)?Yt(t):void 0;return r?n.range.length===r.length&&ot(n.range,r):st({...n,low:1},e,t)}if(e.kind===`Function`&&n.domain.every(e=>typeof e==`string`)){let r=Ge(t);if(!r){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of record:\n${H(e)}\nwith non-record\n${H(t)}`)}return n.domain.length===r.domain.length&&ot(n.domain,r.domain)&&ot(n.range,r.range)}return st({...n,low:Ue(n.domain)},e,t)},lt=(e,t)=>{if(nt(e,t))return Be(e,t);if(!tt(e))return lt(R(e),t);if(!k(t)){if(j(t))return qe(t,e);throw new f(`Attempted to check equality of the set ${H(e)} with the value:\n${H(t)}`)}let n=rt(e,t);return n.elements.length===n.otherElements.length&&ot(n.elements,n.otherElements)},ut=(e,t)=>{if(typeof e!=`object`){if(typeof t==typeof e)return e===t;if(j(t))return qe(t,e);throw new f(Ze(e,t,!0))}return e.kind===`ModelValue`?Ye(e,t):A(e)?ct(e,t):lt(e,t)},dt=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=at(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},ft=(e,t)=>t.some(t=>ut(t,e)),pt=(e,t)=>{try{return Be(e,t)}catch(n){if(!(n instanceof f))throw n;return ut(e,t)}},mt=e=>{for(let t=1;t<e.length;t++)if(F(e[t-1],e[t])>=0)return!1;return!0},ht=e=>{if(e.length<2||mt(e))return Re(e);let t=[...e].sort(F),n=[t[0]];for(let e=1;e<t.length;e++)F(n[n.length-1],t[e])!==0&&n.push(t[e]);return Re(n)},gt=e=>{try{return ht(e)}catch(t){if(!(t instanceof f))throw t;return{kind:`MixedSet`,elements:e}}},_t=Re([]),vt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0||i.push(t)}return i.length===n.length?e:Re(i)},yt=(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&&F(r[a],t)<0;)a++;a<r.length&&F(r[a],t)===0&&i.push(t)}return i.length===n.length?e:Re(i)},bt=(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=F(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=Re(i);if(e.hash!==0){let t=e.hash;for(let e=0;e<a.length;e++)t=t+fn(a[e])|0;c.hash=t}if(e.fingerprint!==0){let t=e.fingerprint;for(let e=0;e<a.length;e++)t=t+yn(a[e])|0;c.fingerprint=t}return c},xt=e=>{let t=e.length<2?e:[...e].sort((e,t)=>F(e[0],t[0])),n=[],r=[];for(let e of t)n.length>0&&F(n[n.length-1],e[0])===0||(n.push(e[0]),r.push(e[1]));return ze(n,r)},St=e=>{let t=xt(e);return t.domain.length!==e.length||e.some(e=>typeof e[0]!=`string`)||e.every((e,n)=>e[0]===t.domain[n])?t:ve(t,e.map(e=>e[0]))},Ct=[],wt=[],Tt=/* @__PURE__ */ new WeakMap,Et=e=>{let t=Ct[e];if(t)return t;let n=Array.from({length:e},(e,t)=>t+1);return Ct[e]=n,n},Dt=e=>{if(Gt(e),e.low===1&&e.high>=1)return Et(e.high);let t=[];for(let n=e.low;n<=e.high;n++)t.push(n);return t},Ot=e=>e.length>0&&(e===Ct[e.length]||Tt.has(e)),kt=e=>{let t=R(e).elements;return Ot(t)&&(typeof e!=`object`||e.kind!==`Interval`)?[...t]:t},At=e=>{let t=wt[e];if(t)return t;let n={kind:`Interval`,low:1,high:e};return wt[e]=n,n},I=e=>ze(Et(e.length),e),jt=e=>xt(e),Mt=e=>k(e)&&!P(e),Nt=(e,t=!1)=>`UNION(${H(e.base,t)})`,Pt=e=>{if(!Mt(e.base))throw new f(`Attempted to enumerate the nonenumerable set:\n${Nt(e)}`);return R(e.base).elements},Ft=(e,t,n)=>{if(Mt(e))return e;throw new f(t===0?`Attempted to enumerate UNION(s), but some element of s is nonenumerable.`:`Attempted to enumerate the nonenumerable set:\n${H(e)}\nwhen enumerating:\n${Nt(n)}`)},L=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 L(e.base);case`FunctionSet`:return L(e.domain)&&L(e.range)||It(e.domain)===!0||It(e)===!0;case`RecordSet`:return e.sets.every(L)||It(e)===!0;case`ProductSet`:return e.factors.every(L)||It(e)===!0;case`Union`:return L(e.base)&&R(e.base).elements.every(L);case`SetOperation`:return e.operator===`\\cup`?L(e.left)&&L(e.right):e.operator===`\\cap`?L(e.left)||L(e.right):L(e.left);default:return!1}},It=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=It(e.domain);return t===void 0?void 0:!t&&It(e.range)}case`RecordSet`:case`ProductSet`:for(let t of e.kind===`RecordSet`?e.sets:e.factors){let e=It(t);if(e!==!1)return e}return!1;case`SetOperation`:case`FilteredSet`:case`Union`:return L(e)?R(e).elements.length===0:void 0;default:return}},Lt=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},Rt=(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},zt=e=>{let t=[];for(let n=0;n<=e.length;n++){let r=Array.from({length:n},(e,t)=>t);do t.push(Re(r.map(t=>e[t])));while(Rt(r,e.length))}return t},Bt=e=>{if(typeof e!=`object`)throw new f(`Expected a set but found ${H(e)}`);switch(e.kind){case`Set`:return e.elements;case`MixedSet`:return dt(e.elements);case`Interval`:{let t=Dt(e);return t!==Ct[t.length]&&Tt.set(t,e),t}case`Subset`:{P(e.base)&&Gt(e.base);let t=R(e.base).elements;if(t.length>30)throw new f(`SUBSET of a set with ${t.length} elements is too large to enumerate`);return zt(t)}case`FunctionSet`:{if(z(e))return[];if(P(e.domain))throw new f(`Attempted to enumerate a set of the form [D -> R],but the domain D:\n${H(e.domain)}\ncannot be enumerated.`);let t=kt(e.domain);if(t.length>0&&P(e.range))throw new f(`Attempted to enumerate a set of the form [D -> R],but the range R:\n${H(e.range)}\ncannot be enumerated.`);let n=t.length===0?[]:R(e.range).elements;return Lt(t.map(()=>n)).map(e=>ze(t,e))}case`RecordSet`:{if(z(e))return[];let t=e.sets.map((t,n)=>{if(P(t))throw new f(`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${H(t)}`);return R(t).elements});return Lt(t).map(t=>jt(e.names.map((e,n)=>[e,t[n]])))}case`ProductSet`:{if(z(e))return[];let t=e.factors.map((e,t)=>{if(P(e))throw new f(`Attempted to enumerate a set of the form s1 \\X s2 ... \\X sn,\nbut can't enumerate s${t}:\n${H(e)}`);return R(e).elements});return Lt(t).map(I)}case`FilteredSet`:if(P(e.base))throw new f(`Attempted to enumerate { x \\in S : p(x) } when S:\n${H(e.base)}\nis not enumerable`);return R(e.base).elements.filter(e.test);case`Union`:{let t=Pt(e).flatMap((t,n)=>R(Ft(t,n,e)).elements);return R(gt(t)).elements}case`SetOperation`:{if(e.operator===`\\cup`&&(P(e.left)||P(e.right)))throw new f(`Attempted to enumerate S \\cup T when S:\n${H(e.left)}\nand T:\n${H(e.right)}\nare not both enumerable`);if(e.operator===`\\cap`&&P(e.left)&&P(e.right))throw new f(`Attempted to enumerate S \\cap T when neither S:\n${H(e.left)}\nnor T:\n${H(e.right)}\nis enumerable`);if(e.operator===`\\`&&P(e.left))throw new f(`Attempted to enumerate S \\ T when S:\n${H(e.left)}\nis not enumerable.`);if(e.operator===`\\cup`)return bt(R(e.left),R(e.right)).elements;let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return R(n).elements.filter(t=>B(t,r)===(e.operator===`\\cap`))}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to enumerate the non-enumerable set ${H(e)}`);default:throw new f(`Expected a set but found ${H(e)}`)}},Vt=/* @__PURE__ */ new WeakMap,R=e=>{if(typeof e==`object`&&e.kind===`Set`)return e;if(typeof e==`object`&&e.kind===`Interval`)return Re(Bt(e));let t=typeof e==`object`?Vt.get(e):void 0;if(t)return t;let n=Bt(e),r=typeof e==`object`&&(e.kind===`MixedSet`||e.kind===`Union`||e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`ProductSet`||e.kind===`FunctionSet`||e.kind===`Subset`)?Re(n):ht(n);return typeof e==`object`&&Vt.set(e,r),r},Ht=e=>{if(typeof e!=`object`||Vt.has(e))return R(e).elements;if(e.kind===`Union`)return Pt(e).flatMap((t,n)=>Ht(Ft(t,n,e)));if(e.kind===`FilteredSet`&&!P(e.base))return Ht(e.base).filter(e.test);if(e.kind!==`SetOperation`)return R(e).elements;if(e.operator===`\\cup`)return P(e.left)||P(e.right)?R(e).elements:[...Ht(e.left),...Ht(e.right)];let t=e.operator===`\\`||L(e.left),n=t?e.left:e.right,r=t?e.right:e.left;return P(n)?R(e).elements:Ht(n).filter(t=>B(t,r)===(e.operator===`\\cap`))},z=e=>{if(typeof e!=`object`||!k(e))throw new f(`Shouldn't call isEmpty() on value ${H(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 f(`Shouldn't call isEmpty() on value ANY`);return!1;case`Subset`:case`SequenceSet`:return!1;case`ProductSet`:return e.factors.some(z);case`RecordSet`:return e.sets.some(z);case`FunctionSet`:return!z(e.domain)&&z(e.range);case`Union`:return Pt(e).every((t,n)=>z(Ft(t,n,e)));default:return R(e).elements.length===0}},Ut=(e,t)=>e.reduce((e,n)=>{let r=e*Gt(n);if(r>2147483647)throw new f(t());return r},1),Wt=e=>`Overflow when computing the number of elements in:\n${H(e)}`,Gt=e=>{if(typeof e!=`object`)return R(e).elements.length;switch(e.kind){case`Interval`:{let t=Math.max(0,e.high-e.low+1);if(t>2147483647)throw new f(`Size of interval value exceeds the maximum representable size (32bits): ${e.low}..${e.high}.`);return t}case`InfiniteSet`:case`SequenceSet`:throw new f(`Attempted to compute the number of elements in the overridden value ${H(e)}.`);case`Subset`:{let t=Gt(e.base);if(t>=31)throw new f(Wt(e));return 2**t}case`FunctionSet`:{if(z(e.domain))return 1;if(z(e.range))return 0;let t=Gt(e.range);if(t===1)return 1;let n=Gt(e.domain),r={value:1};for(let i=0;i<n;i++)if(r.value*=t,r.value>2147483647)throw new f(Wt(e));return r.value}case`RecordSet`:return z(e)?0:Ut(e.sets,()=>Wt(e));case`ProductSet`:return z(e)?0:Ut(e.factors,()=>`Overflow when computing the number of elements in ${H(e)}`);default:return R(e).elements.length}},Kt=e=>`Attempted to check if the set ${H(e)}is finite.`,qt=e=>{if(typeof e!=`object`||e.kind===`ModelValue`)throw new f(`Attempted to check if the ${he(e)} ${H(e)} is a finite set.`);switch(e.kind){case`Set`:case`MixedSet`:case`Interval`:case`Function`:return!0;case`LazyFunction`:throw new f(`Attempted to check if the function:\n${H(e)}\nis a finite set.`);case`InfiniteSet`:return!1;case`SequenceSet`:return qt(e.base)&&z(e.base);case`Subset`:return qt(e.base);case`FunctionSet`:{let t=qt(e.domain);if(t&&z(e.domain))return!0;let n=qt(e.range);return n&&z(e.range)?!0:n&&(t||Gt(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(!qt(e))n.isAllFinite=!1;else if(z(e))return!0;return n.isAllFinite}case`Union`:if(!Mt(e.base))throw new f(`Attempted to check if the nonenumerable set:\n${Nt(e)}\nis a finite set.`);return R(e.base).elements.every(qt);case`SetOperation`:if(e.operator===`\\cup`)return qt(e.left)&&qt(e.right);if(e.operator===`\\cap`){if(!qt(e.left)&&!qt(e.right))throw new f(Kt(e));return!0}if(qt(e.left))return!0;if(!qt(e.right))throw new f(Kt(e));return!1;case`FilteredSet`:if(!qt(e.base))throw new f(`Attempted to check if expression of form {x \\in S : p(x)} is a finite set, but cannot check if S:\n${H(e.base)}\nis finite.`);return!0}},Jt=(e,t)=>{let n={low:0,high:e.length-1};for(;n.low<=n.high;){let r=n.low+n.high>>1,i=F(e[r],t);if(i===0)return r;i<0?n.low=r+1:n.high=r-1}return-1},Yt=e=>{if(typeof e!=`object`||!A(e))return;let t=V(e);return t.domain.every((e,t)=>e===t+1)?t.range:void 0},Xt=(e,t)=>`Attempted to check if the value ${H(e)} is an element of ${H(t)}`,Zt=(e,t)=>{let n=`Attempted to check if the value:\n${H(e)}\nis an element of the`;if(!A(t))return`${n} ${he(t)} ${H(t)}`;let r=t.kind===`Function`||L(t.domain)?V(t).domain:[];return r.length>0&&r.every(e=>typeof e==`string`)?`Attempted to check if element:\n${H(e)}\nis in the record:\n${H(t)}`:r.every((e,t)=>e===t+1)?`Attempted to check set membership in a tuple value.`:`${n} function ${H(t)}`},B=(e,t)=>{if(!k(t))throw new f(Zt(e,t));switch(t.kind){case`Set`:try{return Jt(t.elements,e)!==-1}catch(n){if(!(n instanceof f))throw n;return ft(e,t.elements)}case`MixedSet`:return ft(e,t.elements);case`Interval`:if(typeof e==`number`)return e>=t.low&&e<=t.high;if(t.low<=t.high&&!j(e))throw new f(`Attempted to check if the value:\n${H(e)}\nis in the integer interval ${H(t)}`);return!1;case`InfiniteSet`:if(t.name===`Any`)return!0;if(j(e))return!1;if(!(t.name===`STRING`?typeof e==`string`:typeof e==`number`))throw new f(`Attempted to check if the value:\n${H(e)}\nis an element of ${t.name}.`);return t.name!==`Nat`||typeof e==`number`&&e>=0;case`Subset`:if(!k(e))throw new f(Xt(e,t));return R(e).elements.every(e=>B(e,t.base));case`FunctionSet`:{if(!A(e)){if(j(e))return!1;throw new f(Xt(e,t))}let n=V(e),r=Ue(n.domain);return(r===void 0?pt(t.domain,Re(n.domain)):pt({kind:`Interval`,low:r,high:r+n.domain.length-1},t.domain))&&n.range.every(e=>B(e,t.range))}case`RecordSet`:{let n=A(e)?V(e):void 0;if(!n||!Se(n.domain)){if(j(e))return!1;throw new f(`Attempted to check if non-record\n${H(e)}\nis in the set of records:\n${H(t)}`)}return n.domain.length===t.names.length&&t.names.every((e,r)=>{let i=Jt(n.domain,e);return i!==-1&&B(n.range[i],t.sets[r])})}case`ProductSet`:{let n=A(e)?Yt(e):void 0;if(n===void 0){if(A(e)&&V(e).domain.every(e=>typeof e==`number`)||j(e))return!1;throw new f(`Attempted to check if non-tuple\n${H(e)}\nis in the set of tuples:\n${H(t)}`)}return n.length===t.factors.length&&n.every((e,n)=>B(e,t.factors[n]))}case`FilteredSet`:return B(e,t.base)&&t.test(e);case`Union`:if(!Mt(t.base))throw new f(`Attempted to check if:\n ${H(e)}\nis an element of the non-enumerable set:\n ${Nt(t)}`);return R(t.base).elements.some(t=>B(e,t));case`SetOperation`:return t.operator===`\\cup`?B(e,t.left)||B(e,t.right):B(e,t.left)&&B(e,t.right)===(t.operator===`\\cap`);case`SequenceSet`:{let n=Yt(e);if(n===void 0){if(j(e))return!1;throw new f(Xt(e,t))}return n.every(e=>B(e,t.base))}default:throw new f(Zt(e,t))}},Qt=(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?B(e,n):e.range.every((e,r)=>Qt(e,t.range[r],n.range)),V=e=>{if(typeof e==`object`&&e.kind===`Function`)return e;if(typeof e==`object`&&e.kind===`LazyFunction`){L(e.domain)||Gt(e.domain);let t=kt(e.domain);return ze(t,t.map(t=>e.apply(t)))}throw new f(`Expected a function but found ${H(e)}`)},$t=/* @__PURE__ */ new WeakSet,en=e=>($t.add(e),e),tn=(e,t)=>typeof e==`object`&&$t.has(e)?`In applying the function\n${me(H(e))},\nthe first argument is:\n${me(H(t))}\nwhich is not in its domain.\n`:`Attempted to apply function ${H(e)} to ${H(t)}, which is not in its domain`,nn=(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 Jt(e,t)},rn=(e,t,n)=>{if(typeof e==`object`&&e.kind===`LazyFunction`){if(!B(t,e.domain))throw new f($t.has(e)?tn(e,t):`Attempted to apply a function to ${H(t)}, which is not in its domain`);return e.apply(t)}if(typeof e!=`object`||e.kind!==`Function`)throw new f(`Attempted to apply ${H(e)}, which is not a function`);let r=nn(e.domain,t);if(r===-1)throw new f(n?.(e)??tn(e,t));return e.range[r]},an=e=>Ot(e)?Tt.get(e)??At(e.length):void 0,on=e=>{if(typeof e==`object`&&e.kind===`LazyFunction`)return e.domain;let t=V(e).domain;return an(t)??Re(t)},sn=(e,t,n,r=!1)=>{if(typeof e==`object`&&e.kind===`LazyFunction`&&(r||!L(e.domain))){if(!B(t,e.domain))return e;let r=n(e.apply(t));return{kind:`LazyFunction`,domain:e.domain,apply:n=>pt(t,n)?r:e.apply(n),source:e.source}}let i=V(e),a=nn(i.domain,t);if(a===-1)return i;let o=[...i.range],s=i.range[a],c=n(s);o[a]=c;let l=ze(i.domain,o);if(l.writtenKeys=i.writtenKeys,i.hash!==0){let e=i.domain[a];l.hash=s===c?i.hash:i.hash-pn(e,s)+pn(e,c)|0}if(i.fingerprint!==0){let e=i.domain[a];l.fingerprint=s===c?i.fingerprint:i.fingerprint-bn(e,s)+bn(e,c)|0}return l},cn=/* @__PURE__ */ new WeakMap,ln=e=>{let t=Math.imul(e^e>>>16,2246822507),n=Math.imul(t^t>>>13,3266489909);return n^n>>>16},un=(e,t)=>Math.imul(e^t,16777619),dn=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),16777619);return ln(t)},fn=e=>ln(hn(e)+2654435769|0),pn=(e,t)=>ln(Math.imul(hn(e),668265261)+hn(t)|0),mn=e=>{if(e.kind===`ModelValue`)return ln(un(dn(e.name),109));if(k(e)){if(!L(e))return dn(wn(e));let t=R(e).elements,n=1940243425;for(let e=0;e<t.length;e++)n=n+fn(t[e])|0;return n}let t=V(e),n=1725019383;for(let e=0;e<t.domain.length;e++)n=n+pn(t.domain[e],t.range[e])|0;return n},hn=e=>{if(typeof e==`number`)return ln(e^1540483477);if(typeof e==`boolean`)return e?729683222:682545830;if(typeof e==`string`)return dn(e);if(e.kind===`Set`||e.kind===`Function`)return e.hash===0&&(e.hash=mn(e)),e.hash;let t=cn.get(e);if(t!==void 0)return t;let n=mn(e);return cn.set(e,n),n},gn=/* @__PURE__ */ new WeakMap,_n=e=>{let t=Math.imul(e^e>>>15,739982445),n=Math.imul(t^t>>>12,695872825);return n^n>>>15},vn=e=>{let t=2538058380;for(let n=0;n<e.length;n++)t=Math.imul(t^e.charCodeAt(n),1540483477);return _n(t)},yn=e=>_n(Sn(e)+2135587861|0),bn=(e,t)=>_n(Math.imul(Sn(e),374761393)+Sn(t)|0),xn=e=>{if(e.kind===`ModelValue`)return _n(vn(e.name)^60);if(k(e)){if(!L(e))return vn(wn(e));let t=R(e).elements,n=461845907;for(let e=0;e<t.length;e++)n=n+yn(t[e])|0;return n}let t=V(e),n=1291169091;for(let e=0;e<t.domain.length;e++)n=n+bn(t.domain[e],t.range[e])|0;return n},Sn=e=>{if(typeof e==`number`)return _n(e^1759714724);if(typeof e==`boolean`)return e?1000000007:521288629;if(typeof e==`string`)return vn(e);if(e.kind===`Set`||e.kind===`Function`)return e.fingerprint===0&&(e.fingerprint=xn(e)),e.fingerprint;let t=gn.get(e);if(t!==void 0)return t;let n=xn(e);return gn.set(e,n),n},Cn=/* @__PURE__ */ new WeakMap,wn=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=Cn.get(e);if(t!==void 0)return t;let n=Tn(e);return Cn.set(e,n),n},Tn=e=>{if(e.kind===`ModelValue`)return`@${e.name}`;if(e.kind===`InfiniteSet`)return`<${e.name}>`;if(e.kind===`SequenceSet`)return`<Seq ${wn(e.base)}>`;if(e.kind===`FilteredSet`&&!L(e))throw new f(`Attempted to compare the non-enumerable set ${H(e)} with another value`);if(k(e))return L(e)?`{${R(e).elements.map(wn).join(`,`)}}`:`<${e.kind} ${H(e)}>`;let t=V(e);return`(${t.domain.map((e,n)=>`${wn(e)}:${wn(t.range[n])}`).join(`,`)})`},En=e=>`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\t/g,`\\t`)}"`,Dn=e=>e.reduce((e,t)=>e===0?0:e*Gt(t),1),On=e=>{if(e.kind===`Subset`)return Gt(e.base)<=6?R(e):void 0;if(e.kind===`SetOperation`||e.kind===`FilteredSet`||e.kind===`Union`)return R(e);if(e.kind===`FunctionSet`&&L(e.domain)&&Gt(e.domain)===0)return Re([ze([],[])]);let t=e.kind===`FunctionSet`?Gt(e.range)**Gt(e.domain):Dn(e.kind===`RecordSet`?e.sets:e.factors);return t===0?_t:t<=1999?R(e):void 0},kn=(e,t)=>{if(t)return On(e);if(L(e))try{return On(e)}catch(e){if(e instanceof f)return;throw e}},An=(e,t)=>{let n=e=>H(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 Nt(e,t)}},jn=e=>/^[\p{L}\p{Nd}_]*$/u.test(e)&&/\p{L}/u.test(e)&&(e.length<4||!/^[WS]F_/.test(e)),H=(e,t=!1,n=!1)=>{let r=e=>H(e,t,n);if(typeof e==`boolean`)return e?`TRUE`:`FALSE`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return En(e);switch(e.kind){case`ModelValue`:return e.name;case`Set`:return`{${e.elements.map(r).join(`, `)}}`;case`MixedSet`:return r(R(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=kn(e,t);return n?r(n):An(e,t)}case`SequenceSet`:return`Seq(${r(e.base)})`;case`Function`:case`LazyFunction`:{if(e.kind===`LazyFunction`&&!L(e.domain)){let t=e.source;return t?`[${t.parameters.map(e=>`${e.name} \\in ${H(e.domain)}`).join(`, `)} |-> <expression ${t.body}>]`:`<function>`}let t=V(e);return t.domain.every((e,t)=>e===t+1)?`<<${t.range.map(r).join(`, `)}>>`:t.domain.every(e=>typeof e==`string`&&jn(e))?`[${(n||t.hash!==0||t.fingerprint!==0?t.domain.map((e,n)=>[e,t.range[n]]):ye(t)).map(e=>`${String(e[0])} |-> ${r(e[1])}`).join(`, `)}]`:`(${t.domain.map((e,n)=>`${r(e)} :> ${r(t.range[n])}`).join(` @@ `)})`}}},Mn=(e,t)=>{for(let n=0;n<e.length;n++)e[n]=t(e[n])},Nn=()=>{let e=/* @__PURE__ */ new Map,t=n=>{if(typeof n!=`object`||n.kind!==`Set`&&n.kind!==`Function`)return n;let r=hn(n),i=e.get(r);if(i!==void 0&&!Array.isArray(i)&&(i===n||He(i,n)))return i;if(Array.isArray(i)){for(let e=0;e<i.length;e++)if(i[e]===n||He(i[e],n))return i[e]}return n.kind===`Set`?Mn(n.elements,t):(Mn(n.domain,t),Mn(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}},Pn=/* @__PURE__ */ new Set(/* @__PURE__ */ `=,#,~,<=>,/\\,\\/,=>,\\in,\\notin,\\subseteq,\\cup,\\cap,\\,SUBSET,UNION,DOMAIN,+,-,*,^,\\div,%,..,Seq,IsFiniteSet,RandomElement`.split(`,`)),Fn=(e,t)=>Pn.has(e)?new f(t):new p(t),In=e=>typeof e==`object`&&e.kind===`Operator`,U=(e,t,n)=>{let r=e[t];if(r===void 0||In(r))throw Fn(n,`${n} expects a value as argument ${t+1}`);return r},Ln=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`number`)throw Fn(n,`${n} expects an integer but received ${H(r)}`);return r},Rn=(e,t,n)=>{let r=U(e,t,n);if(typeof r!=`boolean`)throw Fn(n,`${n} expects a boolean but received ${H(r)}`);return r},zn=(e,t,n)=>{let r=U(e,t,n);if(!k(r))throw Fn(n,`${n} expects a set but received ${H(r)}`);return r},Bn=(e,t,n)=>{let r=zn(e,t,n);if(!L(r))throw Fn(n,`${n} cannot enumerate the non-enumerable set ${H(r)}`);return r},Vn=(e,t,n)=>R(Bn(e,t,n)),Hn=(e,t,n)=>Vn(e,t,n).elements,Un=(e,t,n)=>{let r=U(e,t,n),i=Yt(r);if(!i)throw Fn(n,`${n} expects a sequence but received ${H(r)}`);return i},Wn=(e,t,n)=>{let r=U(e,t,n);if(!A(r))throw Fn(n,`${n} expects a function but received ${H(r)}`);return V(r)},Gn=(e,t,n)=>{let r=e[t];if(r===void 0||!In(r))throw Fn(n,`${n} expects an operator as argument ${t+1}`);return r},Kn=(e,t)=>({kind:`Builtin`,name:e,arity:0,evaluate:()=>t,evaluateUnary:void 0,evaluateBinary:void 0}),W=(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}),qn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0}),Jn=(e,t,n)=>({kind:`Builtin`,name:e,arity:t,evaluate:n,evaluateUnary:void 0,evaluateBinary:void 0,isStateLevel:!0}),Yn=(e,t)=>({kind:`Builtin`,name:e,arity:2,evaluate:n=>t(U(n,0,e),U(n,1,e)),evaluateUnary:void 0,evaluateBinary:t}),Xn=e=>e>2147483647||e<-2147483648,Zn=e=>new p(`Overflow when computing ${e}`),Qn={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`},$n={"\\cup":`SetCupValue`,"\\cap":`SetCapValue`,"\\":`SetDiffValue`},er={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`},tr=e=>{if(typeof e==`number`)return`IntValue`;if(typeof e==`string`)return`StringValue`;if(typeof e==`boolean`)return`BoolValue`;if(e.kind===`SetOperation`)return $n[e.operator];if(e.kind!==`Function`)return Qn[e.kind];let t=e.domain;return t.every((e,t)=>e===t+1)?`TupleValue`:t.every(e=>typeof e==`string`)?`RecordValue`:`FcnRcdValue`},nr=e=>er[tr(e)],rr=(e,t)=>new f(`Attempted to apply the operator overridden by the Java method\n${e},\nbut it produced the following error:\n${t}`),ir=(e,t)=>{let n=n=>(...r)=>{try{return n(...r)}catch(n){throw n instanceof f&&(e.isLazy===!0||!(n instanceof p))?rr(t,n.message):n}};return{...e,evaluate:n(e.evaluate),evaluateUnary:e.evaluateUnary&&n(e.evaluateUnary),evaluateBinary:e.evaluateBinary&&n(e.evaluateBinary)}},ar=(e,t)=>{if(typeof e!=`number`)throw rr(t,`Cannot cast tlc2.value.impl.${tr(e)} to tlc2.value.impl.IntValue`);return e},or=(e,t,n)=>`public static tlc2.value.impl.${t} tlc2.module.Integers.${e}(${Array.from({length:n},()=>`tlc2.value.impl.IntValue`).join(`,`)})`,sr=(e,t,n)=>{let r=or(t,`IntValue`,2);return Yn(e,(t,i)=>{let a=ar(t,r),o=ar(i,r),s=n(a,o);if(Xn(s))throw Zn(`${a}${e}${o}`);return s})},cr=(e,t,n)=>{if(typeof e!=`number`)throw new p(`The ${t} argument of ${n} should be an integer, but instead it is:\n${H(e)}`);return e},lr=(e,t)=>Yn(e,(n,r)=>t(cr(n,`first`,e),cr(r,`second`,e))),ur=(e,t,n)=>({...Jn(e,t,n),isLazy:!0}),dr=(e,t,n)=>{let r=Gn(e,t,n);return()=>r.call([])},fr=/* @__PURE__ */ new Set([`Apply`,`And`,`Or`,`If`,`FunctionApplication`,`FieldAccess`,`Record`,`Tuple`,`SetEnumeration`,`Prime`,`Unchanged`,`Forall`,`Exists`]),pr=()=>({bool:qa,condition:(e,t)=>{if(typeof e!=`boolean`)throw Ia(t,e);return e},conjunct:(e,t,n)=>{if(typeof e!=`boolean`)throw La(t,n,e);return e},apply:rn,tuple:I,set:gt,record:ze,writtenRecord:be,equal:Be,enumerate:$a,unassigned:(e,t)=>{throw new f(`The variable ${e} is not assigned at ${q(t)}`)},primeOutsideAction:e=>{throw new f(`A primed expression appears outside an action at ${q(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)=>Wa(e,t,n[r]),e.scope),state:e.state,next:e.next})}),mr={value:void 0},hr=(e,t)=>(e.references.push(t),`reference${e.references.length-1}`),gr=e=>`local${e.counter++}`,_r=(e,t=/* @__PURE__ */ new Set)=>(e.kind===`Identifier`&&t.add(e.name),e.kind===`Apply`&&t.add(e.operator),Ja(e).forEach(e=>_r(e,t)),t),vr=(e,t,n)=>{let r=hr(e,Y(t,e.environment)),i=_r(t),a=[...n.locals].filter(([e])=>i.has(e));if(a.length===0)return`${r}(${n.context})`;let o=hr(e,a.map(([e])=>e));return`${r}(extend(${n.context}, ${o}, [${a.map(([,e])=>e).join(`, `)}]))`},yr=(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(()=>gr(e)),l=t.bounds.map((t,n)=>`const ${c[n]} = enumerate(${xr(e,t.set,s)}, ${hr(e,t.set)});`),u=t.bounds.map(t=>{let n=gr(e);return t.pattern.kind===`Name`&&s.locals.set(t.pattern.name,n),n}),d=`if (bool(${xr(e,t.body,s)}, ${i}) !== ${r}) return ${!r};`,f=u.reduce((t,n,r)=>{let i=gr(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})`},br=(e,t,n)=>{let r=t.args;if(t.operator===`=>`)return`(bool(${xr(e,r[0],n)}, "the left side of =>") ? bool(${xr(e,r[1],n)}, "the right side of =>") : true)`;if(J(t))return vr(e,t,n);let i=mo(t.operator,e.environment);if(i?.kind!==`Builtin`)return vr(e,t,n);if(i.name===`DOMAIN`)return`${hr(e,za(t))}(${xr(e,r[0],n)})`;if(i.name===`TLCEval`)return vr(e,t,n);if(i.name===`\\subseteq`&&r.length===2)return`${hr(e,Ba(t,i))}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`;let a=e.environment;if(i.evaluateUnary&&r.length===1&&rs(r[0],a))return`${hr(e,i.evaluateUnary)}(${xr(e,r[0],n)})`;let o=r.length===2&&rs(r[0],a)&&rs(r[1],a);return i.evaluateBinary&&o?`${hr(e,i.evaluateBinary)}(${xr(e,r[0],n)}, ${xr(e,r[1],n)})`:vr(e,t,n)},xr=(e,t,n)=>{let r=(t,r=n)=>xr(e,t,r);if(qo(t)&&Ho(t,e.environment))return vr(e,t,n);switch(t.kind){case`Number`:return Mo(t.value)?String(t.value):vr(e,t,n);case`String`:return JSON.stringify(t.value);case`Identifier`:{let r=n.locals.get(t.name);if(r)return r;if(J(t))return vr(e,t,n);let i=wo(t.name,e.environment);if(i?.kind===`Variable`){let r=JSON.stringify(t.name),a=hr(e,t);return`(${n.state}?.[${i.index}] ?? unassigned(${r}, ${a}))`}return i?.kind===`Value`?hr(e,i.value):vr(e,t,n)}case`Apply`:return br(e,t,n);case`And`:{let n=hr(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)}, ${hr(e,t)}) ? ${r(t.consequent)} : ${r(t.alternative)})`;case`FunctionApplication`:{_s(t.function);let e=r(t.function);return t.args.length===1?`apply(${e}, ${r(t.args[0])})`:`apply(${e}, tuple([${t.args.map(e=>r(e)).join(`, `)}]))`}case`FieldAccess`:return`apply(${r(t.record)}, ${JSON.stringify(t.field)}, ${hr(e,Ra(t))})`;case`Record`:{let i=[...t.fields].sort((e,t)=>F(e.name,t.name));if(i.some((e,t)=>t>0&&i[t-1].name===e.name))return vr(e,t,n);let a=hr(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}, ${hr(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=hr(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`)?yr(e,t,n):vr(e,t,n);default:return vr(e,t,n)}},Sr=(e,t)=>{if(!fr.has(e.kind))return;let n={environment:t,references:[],helpers:[],counter:0},r=xr(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(`
|
|
19
19
|
`)}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${r}; };`;mr.value??=pr();let o=mr.value,s=Object.keys(o);return Function(...s,...i,a)(...Object.values(o),...n.references)},Cr={kind:`Local`},wr=/* @__PURE__ */ new WeakMap,Tr=e=>{let t=e.lastIndexOf(`@`);return t>0&&t<e.length-1},Er=new Map([`~>`,`-+->`,`\\cdot`].map(e=>[e,{key:e,arity:2}])),Dr=(e,t)=>e.line<t.line||e.line===t.line&&e.column<t.column,Or=e=>e.kind===`Operator`?e.params.length:0,kr=e=>e.name.includes(`!`),Ar=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n},jr=(e,t,n=0)=>t.reduce((e,t)=>({name:t,arity:n,parent:e}),e),Mr=(e,t)=>t.reduce((e,t)=>({name:t.name,arity:t.arity,parent:e}),e),Nr=e=>e.kind===`Name`?[e.name]:e.names,Pr=e=>e.flatMap(e=>Nr(e.pattern)),Fr=(e,t)=>{let n=t.definitions.get(e);return n!==void 0&&(n===t.self?n.kind===`Function`||n.isRecursive:n.isRecursive||Dr(n.location,t.location))?n:void 0},Ir=(e,t)=>{let n=Fr(e,t);return n?{key:t.getKey(n),arity:Or(n)}:t.imported.get(e)},Lr=e=>e[0].toLowerCase()===e[0].toUpperCase()&&e[0]!==`_`&&(e[0]<`0`||e[0]>`9`),Rr=e=>e.startsWith(`<`)&&(e[1]>=`0`&&e[1]<=`9`||e[1]===`*`),zr=(e,t,n,r)=>{let i=e.location,a=wr.get(e);if(a!==void 0)return a;if(Tr(t))return e.kind===`Apply`?e.args.length:0;if(Rr(t))return 0;let o=Ar(n,t);if(o&&e.kind!==`OperatorReference`&&(e.resolution=Cr),o)return o.arity;let s=Ir(t,r);if(s)return e.kind===`Identifier`?e.name=s.key:e.operator=s.key,wr.set(e,s.arity),s.arity;let c=t.slice(0,t.indexOf(`!`));if(c!==``&&(Ar(n,c)!==void 0||Ir(c,r)!==void 0||r.facts.has(c)))return e.kind===`Apply`?e.args.length:0;let l=`at line ${i.line}, column ${i.column}`;throw new u(Lr(t)?`Could not find declaration or definition of symbol '${t}' ${l}`:`Unknown operator: \`${t}' ${l}`)},Br=(e,t,n)=>(e.forEach(e=>G(e.set,t,n)),e.reduce((e,t)=>jr(e,Nr(t.pattern)),t)),Vr=(e,t)=>e.kind===`Identifier`&&e.name===t&&J(e)||Ja(e).some(e=>Vr(e,t)),Hr=(e,t)=>{e.kind===`FunctionConstruction`&&Vr(e.body,t)&&(e.isRecursive=!0),Ja(e).forEach(e=>Hr(e,t))},Ur=(e,t,n)=>{let r=jr(t,e.kind===`Function`?[e.name]:[]);e.kind===`Function`?(G(e.body,Br(e.bounds,r,n),n),Hr(e.body,e.name)):G(e.body,Mr(r,e.params),n)},Wr=(e,t,n)=>{let r=e.filter(e=>e.isRecursive).reduce((e,t)=>({name:t.name,arity:Or(t),parent:e}),t);return e.reduce((e,t)=>(kr(t)||Ur(t,e,n),{name:t.name,arity:Or(t),parent:e}),r)},Gr=(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 u(`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 u(`Argument number ${t+1} to operator '${e.name}' should be a ${a.arity}-parameter operator at line ${o.line}, column ${o.column}`)},Kr=e=>e.kind===`Identifier`?e.name:e.kind===`OperatorReference`?e.operator:void 0,qr=(e,t,n)=>{let r=Ar(t,e.operator)?void 0:Fr(e.operator,n),i=zr(e,e.operator,t,n);if(i!==e.args.length)throw new u(`The operator ${e.operator} requires ${i} arguments at line ${e.location.line}, column ${e.location.column}`);e.args.forEach((e,i)=>{let a=Kr(e),o=a===void 0||Ar(t,a)?void 0:Fr(a,n),s=Jr(e,t,n);r&&Gr(r,i,e,s,o)})},Jr=(e,t,n)=>e.kind===`Identifier`?zr(e,e.name,t,n):e.kind===`OperatorReference`?zr(e,e.operator,t,n):(G(e,t,n),e.kind===`Lambda`?e.params.length:0),G=(e,t,n)=>{let r=e=>e.forEach(e=>G(e,t,n));switch(e.kind){case`Number`:case`Decimal`:case`String`:case`ExceptAt`:return;case`Identifier`:{let r=zr(e,e.name,t,n);if(r>0)throw new u(`The operator ${e.name} requires ${r} arguments at line ${e.location.line}, column ${e.location.column}`);return}case`OperatorReference`:zr(e,e.operator,t,n);return;case`Apply`:qr(e,t,n);return;case`Lambda`:G(e.body,jr(t,e.params),n);return;case`And`:case`Or`:r(e.operands);return;case`Prime`:case`Unchanged`:case`Enabled`:case`Always`:case`Eventually`:G(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&&G(e.other,t,n);return;case`Let`:G(e.body,Wr(e.definitions,t,n),n);return;case`Forall`:case`Exists`:case`FunctionConstruction`:G(e.body,Br(e.bounds,t,n),n);return;case`UnboundedQuantifier`:G(e.body,jr(t,e.names),n);return;case`Choose`:e.set&&G(e.set,t,n),G(e.body,jr(t,Nr(e.pattern)),n);return;case`SetEnumeration`:case`Tuple`:r(e.elements);return;case`SetFilter`:G(e.predicate,Br([e.bound],t,n),n);return;case`SetMap`:G(e.expression,Br(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 u(`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`:G(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`:G(e.body,t,n);return;case`Subexpression`:e.selectors.flatMap(e=>`args`in e?e.args:[]).forEach(e=>Jr(e,t,n))}},Yr=(e,t,n)=>(e.set&&G(e.set,t,n),{name:e.name,arity:e.arity,parent:t}),Xr=(e,t,n)=>{let r=e.assumptions.reduce((e,t)=>t.kind===`NewSymbol`?Yr(t,e,n):(t.kind===`AssumeProve`?Xr(t,e,n):G(t,e,n),e),t);return G(e.conclusion,r,n),r},Zr=(e,t,n)=>e.kind===`AssumeProve`?Xr(e,t,n):(G(e,t,n),t),Qr=(e,t,n)=>{e?.kind===`Steps`&&e.steps.reduce((e,t)=>$r(t,e,n),t)},$r=(e,t,n)=>{let r=e.body;switch(r.kind){case`Assert`:{let i=Zr(r.statement,t,n);return Qr(e.proof,i,n),r.isSuffices?i:t}case`Define`:return Wr(r.definitions.flatMap(e=>e.kind===`Instance`?[]:[e]),t,n);case`Have`:case`Case`:return G(r.expression,t,n),Qr(e.proof,t,n),t;case`Witness`:return r.expressions.forEach(e=>G(e,t,n)),t;case`Take`:return jr(Br(r.bounds,t,n),r.names);case`Pick`:{let i=jr(Br(r.bounds,t,n),r.names);return G(r.body,i,n),Qr(e.proof,t,n),i}default:return Qr(e.proof,t,n),t}},ei=(e,t)=>{let n=e.name!==void 0&&e.statement.kind!==`AssumeProve`?void 0:Zr(e.statement,void 0,t);Qr(e.proof,n,t)},ti=(e,t)=>new u(`Multiply-defined symbol '${e}' at line ${t.line}, column ${t.column}`),ni=(e,t,n,r)=>t.reduce((e,t)=>{if(Ar(e,t)||n(t))throw ti(t,r);return{name:t,arity:0,parent:e}},e),ri=(e,t,n,r)=>(e.forEach(e=>ai(e.set,t,n)),e.reduce((e,t)=>ni(e,Nr(t.pattern),n,r),t)),ii=(e,t,n)=>{if(e.kind===`Function`){let r=jr(t,[e.name]),i=ri(e.bounds,r,n,e.location);ai(e.body,i,n);return}let r=ni(t,e.params.map(e=>e.name),n,e.location);ai(e.body,r,n)},ai=(e,t,n)=>{let r=e.location;switch(e.kind){case`Lambda`:ai(e.body,ni(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(Ar(e,t.name)||n(t.name))throw ti(t.name,t.location);return ii(t,jr(e,i),n),jr(e,[t.name])},t);ai(e.body,ni(a,(e.instances??[]).flatMap(e=>e.name===void 0?[]:[e.name]),n,r),n);return}case`Forall`:case`Exists`:case`FunctionConstruction`:ai(e.body,ri(e.bounds,t,n,r),n);return;case`SetFilter`:ai(e.predicate,ri([e.bound],t,n,r),n);return;case`SetMap`:ai(e.expression,ri(e.bounds,t,n,r),n);return;case`UnboundedQuantifier`:ai(e.body,ni(t,e.names,n,r),n);return;case`Choose`:e.set&&ai(e.set,t,n),ai(e.body,ni(t,Nr(e.pattern),n,r),n);return;default:Ja(e).forEach(e=>ai(e,t,n))}},oi=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}))],si=(e,t)=>{let n=/* @__PURE__ */ new Map;oi(e).sort((e,t)=>Dr(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)&&!Er.has(e))return!1;let r=t.importedAt.get(e);return r===void 0||Dr(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 ti(n.name,n.location)}));let i=e=>t=>(n.get(t)??[]).some(t=>t.isRecursive||Dr(t.location,e))||r(t,e);e.definitions.forEach(e=>ii(e,void 0,i(e.location))),e.assumptions.forEach(e=>ai(e.expression,void 0,i(e.location))),e.instances.forEach(e=>{let t=i(e.location),n=ni(void 0,e.params.map(e=>e.name),t,e.location);e.substitutions.forEach(e=>ai(e.expression,n,t))})},ci=(e,t)=>{let n=new Map([...Er,...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},li=(e,t,n,r)=>{let i=Mr(void 0,e.params),a={imported:ci(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 u(`Identifier '${n}' is not a legal target of a substitution ${s}`)}),new Map(t.map(t=>{let n=t.name;if(!o.has(n)&&!Ar(i,n)&&!Ir(n,a))throw new u(`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 G(r,i,a),[n,r];if(zr(r,r.name,i,a)!==t.arity)throw new u(`An operator must be substituted for symbol '${n}', and it must have arity ${t.arity} ${s}`);return[n,r]}))},ui=(e,t)=>{let n=ci(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=>Ur(e,void 0,i(e.location,e))),e.assumptions.forEach(e=>G(e.expression,void 0,i(e.location,void 0))),e.theorems.forEach(e=>ei(e,i(e.location,void 0)))},di=/* @__PURE__ */ new WeakSet,fi=/* @__PURE__ */ new WeakSet,pi=(e,t,n=/* @__PURE__ */ new Set)=>{let r=yi(e,(e,r)=>pi(e,t,r.length===0?n:/* @__PURE__ */ new Set([...n,...r])));return r.kind===`Identifier`&&J(r)&&!n.has(r.name)?{...r,name:t.get(r.name)??r.name}:r.kind===`Apply`&&J(r)&&!n.has(r.operator)?{...r,operator:t.get(r.operator)??r.operator}:r},mi=(e,t)=>e.map(e=>({pattern:e.pattern,set:t(e.set)})),hi=/* @__PURE__ */ new WeakMap,gi=e=>hi.get(e)??e.name,_i=(e,t)=>(gi(t)!==e.name&&hi.set(e,gi(t)),e),vi=(e,t)=>_i(e.kind===`Function`?{...e,bounds:mi(e.bounds,e=>t(e,[])),body:t(e.body,Pr(e.bounds))}:{...e,body:t(e.body,e.params.map(e=>e.name))},e),yi=(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=>vi(e,(e,r)=>t(e,[...n,...r]))),body:t(e.body,n)}}case`Forall`:case`Exists`:case`FunctionConstruction`:return{...e,bounds:mi(e.bounds,n),body:t(e.body,Pr(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,Nr(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,Nr(e.bound.pattern))};case`SetMap`:return{...e,bounds:mi(e.bounds,n),expression:t(e.expression,Pr(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)}}},bi=`LAMBDA`,xi=(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 yi(e,(e,n)=>(r.push(...xi(e,n.length===0?t:/* @__PURE__ */ new Set([...t,...n]))),e)),r},Si=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}},Ci=(e,t,n)=>new Map([...e,...t.map(e=>[e,n(e)])]),wi=(e,t)=>e.kind===`Name`?{kind:`Name`,name:t(e.name)}:{kind:`Tuple`,names:e.names.map(t)},Ti=(e,t)=>e.map(e=>({pattern:wi(e.pattern,t),set:e.set})),Ei=(e,t)=>e.kind===`Function`?{...e,bounds:Ti(e.bounds,t)}:{...e,params:e.params.map(e=>({name:t(e.name),arity:e.arity}))},Di=(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=>_i({...Ei(e,n),name:n(e.name)},e))};case`Forall`:case`Exists`:case`FunctionConstruction`:case`SetMap`:return{...e,bounds:Ti(e.bounds,n)};case`UnboundedQuantifier`:return{...e,names:e.names.map(n)};case`Choose`:return{...e,pattern:wi(e.pattern,n)};case`SetFilter`:return{...e,bound:{pattern:wi(e.bound.pattern,n),set:e.bound.set}};default:return e}},Oi=e=>`${e}@ENABLED`,ki=(e,t)=>e.modes.get(t)??e,Ai=(e,t,n)=>e.kind===`Identifier`&&!n.has(e.name)&&!J(e)&&t.instantiation.variables.has(e.name),ji=(e,t,n)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>ji(e,t,n));return r.every(e=>e!==void 0)?r.flat():void 0}if(e.kind!==`Identifier`)return;if(Ai(e,t,n))return[e.name];let r=J(e)?void 0:t.instantiation.getDefinition(e.name);return r?.kind===`Operator`&&r.params.length===0?ji(r.body,t,/* @__PURE__ */ new Map):void 0},Mi=(e,t,n)=>{if(t.mode===`Normal`)return;if(t.mode===`Primed`)return Ai(e,t,n)&&e.kind===`Identifier`?{kind:`Identifier`,name:Oi(e.name),location:e.location}:void 0;if(e.kind===`Prime`)return{...e,expression:Li(e.expression,ki(t,`Primed`),n)};if(e.kind===`AngleAction`){let r=e.location;return{kind:`And`,operands:[Li(e.action,t,n),{kind:`Apply`,operator:`#`,args:[{kind:`Prime`,expression:Li(e.subscript,ki(t,`Primed`),n),location:r},Li(e.subscript,t,n)],location:r}],location:r}}if(e.kind!==`Unchanged`)return;let r=ji(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:Oi(r),location:e.location},location:e.location},Li({kind:`Identifier`,name:r,location:e.location},t,n)],location:e.location})),location:e.location}},Ni=(e,t)=>({kind:`Identifier`,name:e,location:t,resolution:{kind:`Local`}}),Pi=(e,t)=>e.map(e=>Ni(e.name,t)),Fi=(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:[...Pi(t,r),...i.map(e=>Ni(e,r))],location:r},location:r}},Ii=(e,t)=>e.instantiation.isParameterized(t)?zi(e,t):void 0,Li=(e,t,n)=>{let r=e=>Li(e,t,n),i=t.instantiation,a=i.params,o=t=>!n.has(t)&&!J(e);if(e.kind===`Enabled`&&t.mode===`Normal`)return{...e,expression:Li(e.expression,ki(t,`Enabled`),n)};if((e.kind===`WeakFairness`||e.kind===`StrongFairness`)&&t.mode===`Normal`)return{...e,subscript:r(e.subscript),action:r(e.action),enabled:Li(e.enabled??{kind:`AngleAction`,action:e.action,subscript:e.subscript,location:e.location},ki(t,`Enabled`),n)};let s=Mi(e,t,n);if(s)return s;if(e.kind===`Identifier`&&o(e.name)){let n=i.substitutions.get(e.name);if(n)return di.add(n),n;let r=Ii(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&&Or(o)>0?Fi(r,a,Or(o),e.location):{kind:`Apply`,operator:r,args:Pi(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||fi.has(e),c=e=>(s&&fi.add(e),e);if(o?.kind===`Identifier`||o?.kind===`OperatorReference`){let t=o.kind===`Identifier`?o.name:o.operator;return c({...e,operator:t,args:n,resolution:o.kind===`Identifier`?o.resolution:void 0})}if(o?.kind===`Apply`)return c({...e,operator:o.operator,args:[...o.args,...n],resolution:o.resolution});if(o?.kind===`Lambda`)return{kind:`Let`,definitions:[{kind:`Operator`,name:bi,params:o.params.map(e=>({name:e,arity:0})),body:o.body,isLocal:!0,isRecursive:!1,location:o.location}],body:{...e,operator:bi,args:n,resolution:{kind:`Local`}},location:e.location};if(o)throw new u(`The operator ${e.operator} can only be substituted by an operator at line ${e.location.line}, column ${e.location.column}`);let l=Ii(t,e.operator);return l===void 0?c({...e,args:n}):{...e,operator:l,args:[...Pi(a,e.location),...n]}}if(e.kind===`OperatorReference`&&o(e.operator)){let n=Ii(t,e.operator);if(n===void 0)return e;let r=i.getDefinition(e.operator);return a.length>0&&r?Fi(n,a,Or(r),e.location):{...e,operator:n}}let c=Si(t),l=yi(e,(e,i)=>i.length===0?r(e):Li(e,t,Ci(n,i,c)));return Di(l,n,c)},Ri=(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:Ni(e.name,n),location:n},isLocal:e.isLocal,isRecursive:!1,location:e.location},zi=(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(i.name,n=>{e.copies.set(t,n);let a=Si(e),o=Ei(vi(i,(t,n)=>Li(t,e,Ci(/* @__PURE__ */ new Map,n,a))),a);return Ri(o,r.params,i.location)}):t},Bi=e=>{let t=/* @__PURE__ */ new Map,n=new Set([...e.substitutions.values()].flatMap(e=>xi(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=>Ii(i,e)??e,getQualifiedKey:t=>{let n=Ii(i,t);if(n!==void 0)return n;let r=e.getDefinition(t);return r?e.addDefinition(r.name,()=>Ri(r,e.params,r.location)):t}}},Vi=e=>e.kind===`Let`&&e.instances!==void 0||Ja(e).some(Vi),Hi=(e,t,n)=>{let r=e.kind===`Let`?[...n,...e.definitions]:n,i=yi(e,e=>Hi(e,t,r));return i.kind!==`Let`||!i.instances?i:{kind:`Let`,definitions:i.instances.reduce((e,n)=>{let i=e.findIndex(e=>Dr(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}},Ui=e=>e.name===void 0&&e.statement.kind!==`AssumeProve`?e.statement:void 0,Wi=(e,t)=>e.definitions.some(e=>Vi(e.body))||e.assumptions.some(e=>Vi(e.expression))||e.theorems.some(e=>{let t=Ui(e);return t!==void 0&&Vi(t)})?{...e,definitions:e.definitions.map(e=>vi(e,e=>Hi(e,t,[]))),assumptions:e.assumptions.map(e=>({...e,expression:Hi(e.expression,t,[])})),theorems:e.theorems.map(e=>{let n=Ui(e);return n===void 0?e:{...e,statement:Hi(n,t,[])}})}:e,Gi=/* @__PURE__ */ new WeakMap,Ki=/* @__PURE__ */ new WeakMap,K=(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},qi=(e,t,n)=>{let r=e.slice();return r[t]=n,r},Ji=(t,n)=>{let r=X(t,n);if(typeof r==`boolean`)return r;try{return qa(r,`the action at ${q(t)}`)}catch(t){throw n.next===void 0?t:a(t,e.error)}},Yi=e=>e===`=`||e===`\\in`||e===`\\subseteq`,Xi=(e,t,n)=>{let r=n?Gi:Ki,i=r.get(e);if(i)return i;let a=wa(e,t,n);return r.set(e,a),a},Zi=(e,t)=>{if(t.constants.has(e))return;let n=Ya(e,t);return n?.kind===`Operator`&&n.params.length===0?n:void 0},Qi=(e,t,n,r=/* @__PURE__ */ new Set)=>{if(e.kind===`Prime`&&n)return Qi(e.expression,t,!1,r);if(e.kind!==`Identifier`||J(e))return;let i=t.variableIndex.get(e.name);if(i!==void 0)return n?void 0:i;let a=Zi(e.name,t);if(a&&!r.has(a))return r.add(a),Qi(a.body,t,n,r)},$i=(e,t,n)=>Qi(e,t,!n),ea=(e,t,n,r,i)=>{if(e.kind===`Prime`&&i)return ea(e.expression,t,n,r,!1);if(e.kind===`Let`){let a=Ao(e.definitions,K(r,t,n));return ea(e.body,a,n,r,i)}if(e.kind!==`Identifier`)return;let a=Ua(t,e.name);if(a?.kind===`Thunk`)return ea(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=Zi(e.name,r.environment);if(s)return ea(s.body,void 0,n,r,i)},ta=(e,t)=>e.kind===`Let`||e.kind===`Identifier`&&(t||J(e))||!t&&e.kind===`Prime`,na=(e,t,n=/* @__PURE__ */ new Set)=>{if(e.kind===`Tuple`){let r=e.elements.map(e=>na(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=Zi(e.name,t);if(i)return n.add(e.name),na(i.body,t,n)},ra=/* @__PURE__ */ new Set([`Label`,`And`,`Or`,`Exists`,`Forall`,`If`,`Case`,`Let`,`Unchanged`,`BoxAction`,`AngleAction`,`FunctionApplication`]),ia=(e,t,n)=>ra.has(e.kind)?!1:e.kind===`Identifier`?t.variableIndex.has(e.name)||t.constants.has(e.name):e.kind===`Apply`?e.operator===`=>`||Yi(e.operator)&&(ta(e.args[0],n)||$i(e.args[0],t,n)!==void 0)||J(e)?!1:t.definitions.get(t.substitutions.get(e.operator)??e.operator)===void 0&&t.builtins.has(e.operator):!0,aa=/* @__PURE__ */ new Set,oa=(e,t)=>{if(J(e))return e.kind===`Identifier`?t.has(e.name)?[]:[e.name]:void 0;let n=[],r={isReadOnly:!0};return yi(e,(e,i)=>{let a=r.isReadOnly?oa(e,i.length===0?t:/* @__PURE__ */ new Set([...t,...i])):[];return a?n.push(...a):r.isReadOnly=!1,e}),r.isReadOnly?n:void 0},sa=(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||J(e)?void 0:Ya(r,t);return i&&!n.has(i)&&(n.add(i),sa(i.body,t,n))?!0:Ja(e).some(e=>sa(e,t,n))},ca=(e,t,n)=>{if(e.kind===`Apply`&&e.operator===`=`){let r=$i(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=na(e.expression,t);if(n)return{kind:`Unchanged`,expression:e,indices:n}}return ia(e,t,n)?{kind:`Guard`,expression:e}:{kind:`Branch`,expression:e,runner:Xi(e,t,n)}},la=(e,t,n)=>{let r=e.map(e=>ca(e,t,n)),i=(e,t,n,a,o,s)=>{let c=a,l;for(let a=e;a<r.length;a++){if(a>0&&t.isInit&&Oa(c)){l??=K(t,n,c);for(let e=a;e<r.length;e++)if(!ua(r[e].expression,l))return;s(c,o);return}let e=r[a];if(e.kind===`Guard`){if(l??=K(t,n,c),!Ji(e.expression,l))return;continue}if(e.kind===`Assign`){l??=K(t,n,c);let r=X(e.value,l),i=c[e.index];if(i===void 0)c=qi(c,e.index,r),l=void 0;else if(!Be(i,r))return;continue}if(e.kind===`Unchanged`){let n=t.current;if(!n)return;for(let t of e.indices){let e=c[t];if(e===void 0)c=qi(c,t,n[t]),l=void 0;else if(!Be(e,n[t]))return}continue}let u=a+1;e.runner(t,n,c,o,!0,(e,r)=>i(u,t,n,e,r,s));return}s(c,o)};return(e,t,n,r,a,o)=>i(0,e,t,n,r,o)},ua=(e,t)=>qa(X(e,t),`the initial predicate at ${q(e)}`),da=e=>(t,n,r,i,a,o)=>{Ji(e,K(t,n,r))&&o(r,i)},fa=(e,t)=>{let n=e.args[1];return(r,i,a,o,s,c)=>{let l=t(r,i,a),u=K(r,i,a);if(a[l]!==void 0){Ji(e,u)&&c(a,o);return}let d=X(n,u),p=e.operator===`\\subseteq`?{kind:`Subset`,base:d}:d;if(!L(p))throw new f(`In computing next states, the right side of \\in is not enumerable: ${H(p)} at ${q(e)}`);let m=Ht(p);for(let e=0;e<m.length&&!r.isStopped;e++)c(qi(a,l,m[e]),o)}},pa=/* @__PURE__ */ new WeakMap,ma=e=>{let t=pa.get(e);if(t)return t;let n=e.kind===`Tuple`?{kind:`And`,operands:e.elements.map(ma),location:e.location}:{kind:`Unchanged`,expression:e,location:e.location};return pa.set(e,n),n},ha=(e,t)=>{let n=e.args[0],r=da(e),i=(e,r,i)=>ea(n,r,i,e,!t)??-1,a=fa(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],K(t,n,o));l(qi(o,u,d),s)}},ga=(e,t,n,r)=>{if(e.kind===`FunctionConstruction`)return{bounds:e.bounds,body:e.body,scope:t};if(e.kind!==`Identifier`)return;let i=Ua(t,e.name);if(i?.kind===`Thunk`)return ga(i.expression,i.scope,n,r);let a=i?void 0:Ya(e.name,r.environment);if(a?.kind!==`Function`)return;let o=X(e,K(r,void 0,n));return{bounds:a.bounds,body:a.body,scope:Wa(void 0,a.name,o)}},_a=(e,t,n)=>{let r=da(e);return(i,a,o,s,c,l)=>{let u=ga(e.function,a,o,i);if(!u){r(i,a,o,s,c,l);return}let d=K(i,a,o),p=e.args.map(e=>X(e,d)),m=p.length===1?p[0]:I(p),h=to(u.bounds,K(i,u.scope,o));if(!B(m,h))throw new f(`Attempted to apply a function to ${H(m)}, which is not in its domain at ${q(e)}`);Xi(u.body,t,n)(i,no(u.bounds,u.scope,m),o,s,c,l)}},va=(e,t)=>{if(na(e,t))return la([ma(e)],t,!1);if(e.kind===`Tuple`)return Xi(ma(e),t,!1);let n=ma(e);return(r,i,a,o,s,c)=>{let l=e.kind===`Identifier`?Ua(i,e.name):void 0;l?.kind===`Thunk`?Xi(ma(l.expression),t,!1)(r,l.scope,a,o,s,c):Ji(n,K(r,i,a))&&c(a,o)}},ya=(e,t,n)=>{let r=e.operator,i=da(e),a=J(e)?void 0:mo(r,t);if(a?.kind===`Builtin`)return i;if(a?.kind===`Closure`){let i=os(a,e.args,t),o={body:void 0};return(e,s,c,l,u,d)=>{o.body??=Xi(a.body,t,n),o.body(e,i(K(e,s,c)),c,u?l:r,u,d)}}return(a,o,s,c,l,u)=>{let d=K(a,o,s),f=go(e,d);f?.kind===`Closure`?Xi(f.body,t,n)(a,Oo(f,e.args,d),s,l?c:r,l,u):i(a,o,s,c,l,u)}},ba=(e,t,n)=>{let r=da(e),i=J(e)?void 0:Zi(e.name,t),a={body:void 0};return(o,s,c,l,u,d)=>{let f=Ua(s,e.name);f?.kind===`Thunk`?Xi(f.expression,t,n)(o,f.scope,c,l,u,d):f||!i?r(o,s,c,l,u,d):(a.body??=Xi(i.body,t,n),a.body(o,void 0,c,u?l:i.name,u,d))}},xa=(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!==`=`||J(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`||!J(a))return;let o=Qi(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=>xa(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`||J(e))return;let r=mo(e.operator,t);if(r?.kind!==`Closure`||r.scope!==void 0)return;let i=xa(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`&&J(o))return{variable:i.variable,argument:o.name,labels:i.labels}},Sa=(e,t,n)=>{let r=Ua(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},Ca=(e,t,n)=>{let r=e.map(e=>Xi(e,t,n)),i=n?[]:e.map(e=>xa(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&&Sa(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)}},wa=(e,t,n)=>{let r=e=>Xi(e,t,n);switch(e.kind){case`And`:return la(e.operands,t,n);case`Or`:return Ca(e.operands,t,n);case`Exists`:{let t=r(e.body),n=e.bounds[0];if(e.bounds.length===1&&n.pattern.kind===`Name`){let e=n.pattern.name;return(r,i,a,o,s,c)=>{let l=$a(X(n.set,K(r,i,a)),n.set);for(let n=0;n<l.length&&!r.isStopped;n++)t(r,Wa(i,e,l[n]),a,o,s,c)}}return(n,r,i,a,o,s)=>{eo(e.bounds,K(n,r,i),e=>(t(n,e,i,a,o,s),n.isStopped))}}case`Forall`:{let i=r(e.body),a=!n&&Ts(e,t)!==void 0&&!sa(e.body,t,/* @__PURE__ */ new Set)?oa(e,aa):void 0;return(t,n,r,o,s,c)=>{let l=[];eo(e.bounds,K(t,n,r),e=>(l.push(e),!1));let u=[...l.slice(0,1),...l.slice(1).reverse()];if(a?.every(e=>Ua(n,e)?.kind===`Value`)){u.every(n=>Ji(e.body,K(t,n,r)))&&c(r,o);return}let d=(n,r,a)=>{n===u.length?c(r,a):n>0&&t.isInit&&Oa(r)?u.slice(n).every(n=>ua(e.body,K(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,K(r,i,a));(qa(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=K(r,i,a),u=t.find(e=>qa(X(e.condition,l),`a CASE guard`))?.body??n;if(!u)throw new f(`No CASE guard is true at ${q(e)}`);u(r,i,a,o,s,c)}}case`Let`:{let n=r(e.body),i=zo(e.definitions,t);return(t,r,a,o,s,c)=>n(t,Ao(e.definitions,K(t,r,a),i),a,o,s,c)}case`Unchanged`:return n?()=>{throw new f(`UNCHANGED appears in an initial predicate at ${q(e)}`)}:va(e.expression,t);case`AngleAction`:{let t=r(e.action),n=ma(e.subscript);return(e,r,i,a,o,s)=>t(e,r,i,a,o,(t,i)=>{Ji(n,K(e,r,t))||s(t,i)})}case`BoxAction`:return r({kind:`Or`,operands:[e.action,ma(e.subscript)],location:e.location});case`Label`:return r(e.body);case`Identifier`:return ba(e,t,n);case`FunctionApplication`:return _a(e,t,n);case`Apply`:{let i=e.args;if(Yi(e.operator)){let r=$i(i[0],t,n);if(r!==void 0)return e.operator===`=`?la([e],t,n):fa(e,()=>r);if(ta(i[0],n))return ha(e,n)}if(e.operator===`=>`){let e=r(i[1]);return(t,n,r,a,o,s)=>{let c=X(i[0],K(t,n,r));(typeof c==`boolean`?c:qa(c,`the left side of => at ${q(i[0])}`))?e(t,n,r,a,o,s):s(r,a)}}return ya(e,t,n)}default:return da(e)}},Ta=/* @__PURE__ */ new WeakMap,Ea=e=>{let t=Ta.get(e);if(t)return t;let n=e.variables.map(()=>void 0);return Ta.set(e,n),n},Da=(e,t)=>t.variables.filter((t,n)=>e[n]===void 0),Oa=e=>{for(let t=0;t<e.length;t++)if(e[t]===void 0)return!1;return!0},ka=(e,t)=>{let n=t.state;if(!n||!Oa(n))throw new f(`ENABLED needs a complete state at ${q(e)}`);let r=t.environment,i={value:!1};return Xi(e,r,!1)({environment:r,isInit:!1,current:n,isStopped:!1},t.scope,Ea(r),void 0,!1,()=>{i.value=!0}),i.value},Aa=(t,n,r,i)=>{let o={environment:n,isInit:!0,current:void 0,isStopped:!1};Xi(t,n,!0)(o,i,Ea(n),void 0,!1,t=>{if(!o.isStopped){if(!Oa(t))throw a(new f(`The initial predicate does not assign ${Da(t,n).join(`, `)}`),e.error);o.isStopped=r(t)}})},ja=(e,t,n)=>{let r=[];return Aa(e,t,e=>(r.push(e),!1),n),r},Ma=(e,t,n,r,i)=>{let a={environment:n,isInit:!1,current:t,isStopped:!1},o={successors:0};return Xi(e,n,!1)(a,i,Ea(n),void 0,!1,(e,t)=>{if(!a.isStopped){if(!Oa(e))throw new f(`Successor state is not completely specified by action ${t??`Next`}. The following variables are not assigned: ${Da(e,n).join(`, `)}`);o.successors++,a.isStopped=r({state:e,action:t})}}),o.successors},Na=(e,t,n,r)=>{let i=[];return Ma(e,t,n,e=>(i.push(e),!1),r),i},q=e=>`line ${e.location.line}, column ${e.location.column}`,Pa=e=>`line ${e.start.line}, col ${e.start.column} to line ${e.end.line}, col ${e.end.column} of module ${e.module}`,Fa=e=>{let t=e.location.range;return t?Pa(t):q(e)},Ia=(e,t)=>new f(`A non-boolean expression (${nr(t)}) was used as the condition of an IF.\n${Fa(e)}`),La=(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):Fa(r);return new f(`A non-boolean expression (${nr(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 f(`Attempted to evaluate an expression of form P /\\ Q when ${t===0?`P`:`Q`} was\n${nr(n)}.\n${Fa({...e,location:{...e.location,range:{...r,end:i}}})}`)},Ra=e=>t=>`Attempted to select nonexistent field "${e.field}" from the record\n${H(t)}\n${Fa(e)}`,za=e=>t=>{if(!A(t))throw new f(`Attempted to apply the operator DOMAIN to a non-function\n(${nr(t)})\n${Fa(e)}`);return on(t)},Ba=(e,t)=>(n,r)=>{if(!k(n)||P(n))throw new f(`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${Fa(e)}`);return t.evaluate([n,r])},Va=t=>a(new f(`TLC can't handle real numbers: ${t.text} at ${q(t)}`),e.configParseError),Ha=t=>a(new f(`TLC can't handle a number this big: ${t.text??t.value} at ${q(t)}`),e.error),Ua=(e,t)=>{for(let n=e;n;n=n.parent)if(n.name===t)return n.binding},Wa=(e,t,n)=>({name:t,binding:{kind:`Value`,value:n},parent:e}),Ga=(e,t,n)=>{if(t.kind===`Name`)return Wa(e,t.name,n);let r=Yt(n);if(!r||r.length!==t.names.length)throw new f(`Cannot bind <<${t.names.join(`, `)}>> to ${H(n)}, which is not a tuple of that length`);return t.names.reduce((e,t,n)=>Wa(e,t,r[n]),e)},Ka=(e,t)=>({environment:e.environment,scope:t,state:e.state,next:e.next}),qa=(e,t)=>{if(typeof e!=`boolean`)throw new f(`Expected ${t} to be a boolean but found ${H(e)}`);return e},Ja=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:[])}},J=e=>(e.kind===`Identifier`||e.kind===`Apply`)&&e.resolution?.kind===`Local`,Ya=(e,t)=>t.definitions.get(t.substitutions.get(e)??e),Xa=(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=Ya(n,t);if(e&&!t.constants.has(n)&&!Za(e,t))return!0}return Ja(e).some(e=>Xa(e,t))},Za=(e,t)=>{let n=t.constantLevels.get(e);if(n!==void 0)return n;t.constantLevels.set(e,!0);let r=!Xa(e.body,t)&&(e.kind===`Operator`||!e.bounds.some(e=>Xa(e.set,t)));return t.constantLevels.set(e,r),r},Qa=(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.`},$a=(e,t)=>{if(!k(e)||P(e)){let n=H(e),r=Qa(e,n);throw new f(r===void 0?`TLC encountered a non-enumerable quantifier bound\n${n}.\n${Fa(t)}`:`TLC encountered the non-enumerable quantifier bound\n${n}\n${Fa(t)}\n${r}`)}return typeof e==`object`&&e.kind===`Interval`?Dt(e):R(e).elements},eo=(e,t,n)=>{let r=e.map(e=>$a(X(e.set,t),e.set)),i=(t,a)=>t<0?n(a):r[t].some(n=>i(t-1,Ga(a,e[t].pattern,n)));return i(e.length-1,t.scope)},to=(e,t)=>{let n=e.map(e=>X(e.set,t));return n.length===1?n[0]:{kind:`ProductSet`,factors:n}},no=(e,t,n)=>{if(e.length===1)return Ga(t,e[0].pattern,n);let r=Yt(n)??[];return e.reduce((e,t,n)=>Ga(e,t.pattern,r[n]),t)},ro=/* @__PURE__ */ new WeakMap,io=(e,t)=>{let n=ro.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:Pa(r)};return ro.set(t,i),i},ao=(e,t,n)=>{let r=io(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}},oo=(e,t,n)=>{let r=/* @__PURE__ */ new Map,i={kind:`LazyFunction`,domain:e,source:n,apply:e=>{let n=hn(e),a=r.get(n);if(a!==void 0){for(let t=0;t<a.length;t+=2)if(He(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},so=(e,t)=>{let n=to(e.bounds,t),r=oo(n,(n,r)=>X(e.body,Ka(t,no(e.bounds,Wa(void 0,e.name,r),n))),ao(e.bounds,e.body,n));return e.bounds.length===1?en(r):r},co=(e,t)=>e.kind===`Function`?so(e,t):X(e.body,t),lo=(e,t)=>{let n=Za(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 f(`Operator ${e.name} requires ${e.params.length} arguments`);let i=Ka(t,void 0);if(!n)return co(e,i);let a=t.environment.runtime.evaluateAtStartup(()=>co(e,i));return t.environment.definitionValues.set(e,a),a},uo=(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,Ka(t,e.scope));return n&&(e.cache=r),r},fo=/* @__PURE__ */ new WeakMap,po=e=>{let t=fo.get(e);if(t)return t;let n={kind:`Closure`,name:e.name,params:e.params,body:e.body,scope:void 0};return fo.set(e,n),n},mo=(e,t)=>{let n=t.substitutions.get(e);if(n!==void 0)return mo(n,t);let r=t.definitions.get(e);return r?.kind===`Operator`?po(r):t.builtins.get(e)},ho=/* @__PURE__ */ new WeakMap,go=(e,t)=>{if(J(e))return _o(e.operator,t);let n=ho.get(e),r=n===void 0?mo(e.operator,t.environment)??null:n;return n===void 0&&ho.set(e,r),r??_o(e.operator,t)},_o=(e,t)=>{let n=Ua(t.scope,e);if(n?.kind===`Operator`)return n.operator;if(!n)return mo(e,t.environment)},vo=e=>e.kind===`Closure`?e.params.length:e.arity,yo=(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:_o(n,t);if(!r)throw new f(`Expected an operator argument at ${q(e)}`);return r},bo=(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)=>Wa(e,n.name,t[r]),e.scope);return X(e.body,Ka(n,r))},xo=(e,t)=>e.kind===`Operator`?e:{kind:`Operator`,name:e.name,arity:vo(e),call:n=>bo(e,n,t),literalBody:e.kind===`Closure`&&(e.body.kind===`Number`||e.body.kind===`String`)?e.body.value:void 0},So=(e,t)=>{if(e.kind===`Lambda`||e.kind===`OperatorReference`)return!0;if(e.kind!==`Identifier`)return!1;let n=_o(e.name,t);return n!==void 0&&vo(n)>0},Co=(e,t)=>({kind:`Thunk`,expression:e,scope:t.scope,state:t.state,next:t.next,cache:void 0}),wo=(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 wo(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([])}},To=(e,t)=>{if(e.kind===`String`||e.kind===`Number`&&Mo(e.value)){let t={kind:`Value`,value:e.value};return()=>t}if(e.kind===`Identifier`){let n=J(e)?void 0:wo(e.name,t);if(n?.kind===`Value`){let e={kind:`Value`,value:n.value};return()=>e}if(n===void 0)return t=>{let n=Ua(t.scope,e.name);return n?.kind===`Value`||n?.kind===`Thunk`?n:Co(e,t)}}return t=>Co(e,t)},Eo=/* @__PURE__ */ new WeakMap,Do=(e,t)=>{let n=Eo.get(e);if(n)return n;let r=To(e,t);return Eo.set(e,r),r},Oo=(e,t,n)=>{if(t.length!==e.params.length)throw new f(`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:yo(t[i],n)}:Do(t[i],n.environment)(n);r={name:a.name,binding:o,parent:r}}return r},ko=(e,t,n)=>e.kind===`Closure`?X(e.body,Ka(n,Oo(e,t,n))):e.kind===`Operator`?e.call(t.map(e=>X(e,n))):e.isLazy?e.evaluate(is(t,n),n):e.evaluate(t.map(e=>So(e,n)?xo(yo(e,n),n):X(e,n)),n),Ao=(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),jo=(e,t,n)=>{let r=Ua(n.scope,e);if(r?.kind===`Value`)return r.value;if(r?.kind===`Thunk`)return uo(r,n);if(r?.kind===`Operator`){if(vo(r.operator)===0)return bo(r.operator,[],n);throw new f(`Operator ${e} is used without arguments`)}let i=wo(e,n.environment);if(i?.kind===`Variable`){let r=n.state?.[i.index];if(r===void 0)throw new f(`The variable ${e} is not assigned at ${q(t)}`);return r}if(i?.kind===`Value`)return i.value;if(i?.kind===`Definition`)return lo(i.definition,n);let a=n.environment.builtins.get(e);if(a?.arity===0)return a.evaluate([],n);throw new f(`Unknown identifier ${e} at ${q(t)}`)},Mo=e=>Number.isInteger(e)&&e<=2147483647&&e>=-2147483648,No=/* @__PURE__ */ new Set([`Print`,`PrintT`,`Assert`,`JavaTime`,`RandomElement`,`TLCGet`,`TLCSet`]),Po=(e,t)=>No.has(e)||t.builtins.get(e)?.isStateLevel===!0,Fo=/* @__PURE__ */ new WeakMap,Io=(e,t)=>e.kind===`Operator`&&e.params.length===0&&Za(e,t),Lo=(e,t)=>{let n=e.kind===`Identifier`?e.name:e.kind===`Apply`||e.kind===`OperatorReference`?e.operator:void 0;if(n!==void 0&&!J(e)){let e=Ya(n,t);if(!e&&Po(n,t)||e&&!Io(e,t)&&Ro(e,t))return!0}return Ja(e).some(e=>Lo(e,t))},Ro=(e,t)=>{let n=Fo.get(e);if(n!==void 0)return n;Fo.set(e,!1);let r=Lo(e.body,t)||e.kind===`Function`&&e.bounds.some(e=>Lo(e.set,t));return Fo.set(e,r),r},zo=(e,t)=>new Set(e.filter(e=>Ro(e,t))),Bo=e=>[...e.definitions.values()].some(t=>Ro(t,e)),Vo=/* @__PURE__ */ new WeakMap,Ho=(e,t)=>{let n=Vo.get(e);if(n!==void 0)return n;let r=Ko(e,t,Wo);return Vo.set(e,r),r},Uo=/* @__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`]),Wo=/* @__PURE__ */ new Set,Go=(e,t,n)=>n.size===0?Ho(e,t):Ko(e,t,n),Ko=(e,t,n)=>{if(!Uo.has(e.kind))return!1;if(e.kind===`Identifier`&&J(e))return n.has(e.name);if(J(e))return!1;if(e.kind===`Identifier`){let n=wo(e.name,t);return n?.kind===`Value`||n?.kind===`Definition`&&Io(n.definition,t)}if(e.kind===`Apply`){if(Po(e.operator,t)||e.operator===`~>`)return!1;let r=mo(e.operator,t);if(r?.kind!==`Builtin`&&r?.kind!==`Closure`)return!1;if(r.kind===`Closure`){let n=Ya(e.operator,t);if(!n||!Za(n,t)||Ro(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`&&Go(e,t,n))}let r={value:!0};return yi(e,(e,i)=>(r.value&&=Go(e,t,i.length===0?n:/* @__PURE__ */ new Set([...n,...i])),e)),r.value},qo=e=>e.kind!==`Number`&&e.kind!==`String`&&e.kind!==`Identifier`,Jo=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}},Yo=(e,t)=>{if(e.kind===`Identifier`&&J(e))return!t.has(e.name);if(J(e))return!0;let n={value:!1};return yi(e,(e,r)=>(n.value||=Yo(e,r.length===0?t:/* @__PURE__ */ new Set([...t,...r])),e)),n.value},Xo=(e,t)=>di.has(e)&&qo(e)&&Ts(e,t)!==void 0&&!Yo(e,Wo),Y=(e,t)=>{let n=t.evaluators.get(e);if(n)return n;let r=Ss(e,t),i=qo(e)&&Ho(e,t)?Jo(r):Xo(e,t)?ts(r):r;return t.evaluators.set(e,i),i},Zo=/* @__PURE__ */ new WeakMap,Qo=(e,t)=>{let n=Zo.get(e);if(n)return n;let r=(qo(e)&&Ho(e,t)?void 0:Sr(e,t))??Y(e,t);return Zo.set(e,r),r},X=(e,t)=>Qo(e,t.environment)(t),$o=(e,t)=>{let n=e.name,r=J(e)?void 0:wo(n,t);if(r?.kind===`Variable`){let t=r.index;return r=>{let i=r.state?.[t];if(i===void 0)throw new f(`The variable ${n} is not assigned at ${q(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&&Za(e,t))return t=>lo(e,t);if(e.params.length===0){let n={body:void 0},r=r=>(n.body??=Qo(e.body,t),n.body(Ka(r,void 0)));return es(e,t)?ts(r):r}return t=>lo(e,t)}if(r?.kind===`Definition`){let e=r.definition,n=t=>lo(e,t);return Za(e,t)||!es(e,t)?n:ts(n)}return t=>jo(n,e,t)},es=(e,t)=>Ts(e.body,t)!==void 0&&(e.kind===`Operator`||e.bounds.every(e=>Ts(e.set,t)!==void 0)),ts=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}},ns=(e,t)=>{let n=mo(e,t);return n!==void 0&&vo(n)>0},rs=(e,t)=>e.kind!==`Lambda`&&e.kind!==`OperatorReference`&&!(e.kind===`Identifier`&&!J(e)&&ns(e.name,t)),is=(e,t)=>e.map(e=>({kind:`Operator`,name:`argument`,arity:0,call:()=>X(e,t),expression:e})),as=(e,t,n)=>{if(e.isLazy)return n=>e.evaluate(is(t,n),n);let r=e.evaluateUnary;if(r&&t.length===1&&rs(t[0],n)){let e=Y(t[0],n);return t=>r(e(t))}let i=e.evaluateBinary,a=t.length===2&&rs(t[0],n)&&rs(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=>So(e,n)?xo(yo(e,n),n):X(e,n)),n)},os=(e,t,n)=>{if(t.length!==e.params.length)return()=>{throw new f(`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:Do(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:yo(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},ls=(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},us=(e,t)=>e.every((e,n)=>He(e,t[n])),ds=(e,t)=>{let n=Ya(e.operator,t);return n?.kind===`Operator`&&n.params.length>0&&n.params.every(e=>e.arity===0)&&Za(n,t)&&!Ro(n,t)},fs=(e,t,n,r)=>{let i=os(e,t,n),a={body:void 0},o=(t,r)=>(a.body??=Qo(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=ls(n,t.length);if(!r)return o(n,e);let a=r.length;for(let e of r)a=un(a,hn(e));let c=s.calls.get(a),l=c?.find(e=>us(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}},ps=(e,t)=>{let n=e.args;if(e.operator===`=>`){let e=Y(n[0],t),r=Y(n[1],t);return t=>!qa(e(t),`the left side of =>`)||qa(r(t),`the right side of =>`)}if(e.operator===`~>`)return()=>{throw new f(`TLC cannot evaluate a temporal formula such as ~> in a state`)};let r=J(e)?void 0:mo(e.operator,t);if(r?.kind===`Closure`)return fs(r,n,t,ds(e,t));if(r?.kind===`Builtin`&&r.name===`DOMAIN`){let r=Y(n[0],t),i=za(e);return e=>i(r(e))}if(r?.kind===`Builtin`&&r.name===`TLCEval`&&n.length===1){let i=Y(n[0],t);return fi.has(e)||Xa(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=Ba(e,r);return e=>o(i(e),a(e))}return r?.kind===`Builtin`?as(r,n,t):r=>{let i=_o(e.operator,r);if(i)return ko(i,n,r);throw t.definitions.get(e.operator)?.kind===`Function`?new f(`${e.operator} is a function; apply it with brackets`):new f(`Unknown operator ${e.operator} at ${q(e)}`)}},ms=(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=$a(o(t),a.set);for(let a=0;a<s.length;a++){let o=qa(i(Ka(t,Wa(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)=>$a(e(t),o[n].set)),c=(a,o)=>{if(a<0)return qa(i(Ka(t,o)),r)!==n;let l=s[a];for(let t=0;t<l.length;t++)if(c(a-1,Wa(o,e[a],l[t])))return!0;return!1};return c(o.length-1,t.scope)?!n:n}}return e=>eo(o,e,t=>qa(i(Ka(e,t)),r)!==n)?!n:n},hs=(e,t)=>{if(e.kind===`Tuple`)return I(e.names.map(e=>{let n=Ua(t,e);return n?.kind===`Value`&&n.value}));let n=Ua(t,e.name);return n?.kind===`Value`?n.value:void 0},gs=/* @__PURE__ */ new WeakSet,_s=e=>{(e.kind===`FunctionConstruction`||e.kind===`Except`)&&gs.add(e)},vs=(e,t)=>{let n=gs.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):to(i,t);if(e.isRecursive||n||!L(c)){let n=oo(c,e=>r(Ka(t,no(i,t.scope,e))),ao(i,e.body,c));return i.length===1?en(n):n}if(o&&a.pattern.kind===`Name`){let e=a.pattern.name,n=kt(c);return en(ze(n,n.map(n=>r(Ka(t,Wa(t.scope,e,n))))))}let l=[];return eo(i,t,e=>{let n=i.length===1?hs(a.pattern,e)??!1:I(i.map(t=>hs(t.pattern,e)??!1));return l.push([n,r(Ka(t,e))]),!1}),xt(l)}},ys=(e,t)=>{let n=gs.has(e);n&&_s(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):I(e.args.map(e=>e(t))),o=(e,t,r,i,s)=>r===t.length?s(Ka(i,Wa(i.scope,`@`,e))):sn(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}},bs=(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=>qa(n(Ka(t,Ga(t.scope,i,e))),`a set filter predicate`);if(typeof a!=`object`||a.kind!==`Set`&&a.kind!==`MixedSet`&&a.kind!==`Interval`){if(k(a))return{kind:`FilteredSet`,base:a,test:o};throw new f(`TLC encountered a non-enumerable quantifier bound ${H(a)} at ${q(e.bound.set)}`)}return Re(R(a).elements.filter(o))}}if(e.kind===`SetMap`){let n=Y(e.expression,t);return t=>{let r=[];return eo(e.bounds,t,e=>(r.push(n(Ka(t,e))),!1)),gt(r)}}let n=e.set;if(!n)return()=>{throw new f(`TLC cannot evaluate an unbounded CHOOSE at ${q(e)}`)};let r=Y(e.body,t),i={pattern:e.pattern,set:n};return t=>{let n={value:void 0};if(eo([i],t,i=>qa(r(Ka(t,i)),`the body of CHOOSE`)?(n.value=hs(e.pattern,i),!0):!1),n.value===void 0)throw new f(`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${Fa(e)}`);return n.value}},xs=(e,t)=>{let n=[...e].sort((e,t)=>F(e.name,t.name)),r=n.some((e,t)=>t>0&&n[t-1].name===e.name),i=n.map(e=>Y(e.value,t));if(r)return e=>xt(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=>be(a,i.map(t=>t(e)),o):e=>ze(a,i.map(t=>t(e)))},Ss=(e,t)=>{let n=e=>Y(e,t);switch(e.kind){case`Number`:{let t=e.value;return Mo(t)?()=>t:()=>{throw Ha(e)}}case`Decimal`:return()=>{throw Va(e)};case`String`:{let t=e.value;return()=>t}case`Identifier`:return $o(e,t);case`Apply`:return ps(e,t);case`OperatorReference`:case`Lambda`:return()=>{throw new f(`An operator cannot be used as a value at ${q(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 La(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(qa(t[n](e),`a disjunct`))return!0;return!1}}case`Prime`:{let t=n(e.expression);return n=>{if(!n.next)throw new f(`A primed expression appears outside an action at ${q(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=>Be(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 Ia(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(qa(e.condition(n),`a CASE guard`))return e.body(n);if(r)return r(n);throw new f(`No CASE guard is true at ${q(e)}`)}}case`Let`:{let t=n(e.body);return n=>t(Ka(n,Ao(e.definitions,n)))}case`Forall`:case`Exists`:return ms(e,t);case`UnboundedQuantifier`:return()=>{throw new f(`TLC cannot evaluate an unbounded quantifier at ${q(e)}`)};case`Choose`:case`SetFilter`:case`SetMap`:return bs(e,t);case`SetEnumeration`:{let t=e.elements.map(n);return e=>gt(t.map(t=>t(e)))}case`Tuple`:{let t=e.elements.map(n);return e=>I(t.map(t=>t(e)))}case`FunctionConstruction`:return vs(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 xs(e.fields,t);case`RecordSet`:{let t=[...e.fields].sort((e,t)=>F(e.name,t.name)),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 ys(e,t);case`ExceptAt`:return t=>{let n=Ua(t.scope,`@`);if(n?.kind!==`Value`)throw new f(`@ appears outside an EXCEPT at ${q(e)}`);return n.value};case`FunctionApplication`:{_s(e.function);let t=n(e.function),r=e.args.map(n);if(r.length===1){let e=r[0];return n=>rn(t(n),e(n))}return e=>rn(t(e),I(r.map(t=>t(e))))}case`FieldAccess`:{let t=n(e.record),r=e.field,i=Ra(e);return e=>rn(t(e),r,i)}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?qa(t(e),`an action`)||r(e):qa(t(e),`an action`)&&!r(e)}case`Enabled`:{let t=e.expression;return e=>ka(t,e)}case`Label`:return n(e.body);case`Subexpression`:return()=>{throw new f(`The subexpression reference at ${q(e)} does not name a subexpression`)};case`Always`:case`Eventually`:case`WeakFairness`:case`StrongFairness`:return()=>{throw new f(`TLC cannot evaluate a temporal formula in a state at ${q(e)}`)}}},Cs=/* @__PURE__ */ new Set([`Prime`,`Unchanged`,`Enabled`,`Always`,`Eventually`,`BoxAction`,`AngleAction`,`WeakFairness`,`StrongFairness`]),ws=e=>{if(e.kind===`Identifier`)return e.name;if(e.kind===`Apply`||e.kind===`OperatorReference`)return e.operator},Ts=(e,t)=>{let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Set,i=e=>{if(Cs.has(e.kind))return!1;let a=ws(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&&Po(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 Ja(e).every(i)};return i(e)?[...n]:void 0},Es=/* @__PURE__ */ new Map([[`Reals`,`-------------------------------- MODULE Reals -------------------------------
|
|
20
20
|
(***************************************************************************)
|
|
21
21
|
(* This module provides dummy definitions of the operators that are *)
|
package/package.json
CHANGED
package/src/values.ts
CHANGED
|
@@ -5,6 +5,7 @@ export interface ModelValue {
|
|
|
5
5
|
kind: "ModelValue";
|
|
6
6
|
name: string;
|
|
7
7
|
type: string | undefined;
|
|
8
|
+
rank: number;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export interface SetValue {
|
|
@@ -264,6 +265,7 @@ const stringOrder = { ranks: Object.create(null) as Record<string, number>, size
|
|
|
264
265
|
export const setStringOrder = (texts: Iterable<string>) => {
|
|
265
266
|
stringOrder.ranks = Object.create(null);
|
|
266
267
|
stringOrder.size = 0;
|
|
268
|
+
modelValues.clear();
|
|
267
269
|
for (const text of texts) rankString(text);
|
|
268
270
|
};
|
|
269
271
|
|
|
@@ -353,7 +355,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
353
355
|
if (left.type !== undefined && right.type !== undefined) {
|
|
354
356
|
checkModelValueTypes(left, right, false);
|
|
355
357
|
}
|
|
356
|
-
return
|
|
358
|
+
return left.rank - right.rank;
|
|
357
359
|
}
|
|
358
360
|
if (leftKind === "ModelValue" || rightKind === "ModelValue") {
|
|
359
361
|
checkModelValueTypes(left, right, false);
|
|
@@ -385,14 +387,15 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
385
387
|
|
|
386
388
|
const modelValues = new Map<string, ModelValue>();
|
|
387
389
|
|
|
390
|
+
// A check ranks every model value's name when it creates the value, and creates its own values.
|
|
388
391
|
export const createModelValue = (name: string): ModelValue => {
|
|
389
|
-
rankString(name);
|
|
390
392
|
const known = modelValues.get(name);
|
|
391
393
|
if (known) return known;
|
|
392
394
|
const value: ModelValue = {
|
|
393
395
|
kind: "ModelValue",
|
|
394
396
|
name,
|
|
395
397
|
type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
|
|
398
|
+
rank: getStringRank(name),
|
|
396
399
|
};
|
|
397
400
|
modelValues.set(name, value);
|
|
398
401
|
return value;
|
|
@@ -562,7 +565,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
|
|
|
562
565
|
|
|
563
566
|
const compareModelValue = (left: ModelValue, right: Value) => {
|
|
564
567
|
const type = getModelValueType(left);
|
|
565
|
-
if (type === undefined) return isModelValue(right) ?
|
|
568
|
+
if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
|
|
566
569
|
if (!isModelValue(right)) {
|
|
567
570
|
throw new TlaEvaluationError(
|
|
568
571
|
`Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
|
|
@@ -574,7 +577,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
|
|
|
574
577
|
`Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
|
|
575
578
|
);
|
|
576
579
|
}
|
|
577
|
-
return
|
|
580
|
+
return left.rank - right.rank;
|
|
578
581
|
};
|
|
579
582
|
|
|
580
583
|
const equalsModelValue = (left: ModelValue, right: Value) => {
|