@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
1 lines • 84.1 kB
JavaScript
function e(e){if(!e.position||!e.code)return"";const t=e.position.column-1,n=e.code.length-t-1;return`${" ".repeat(Math.max(t,0))}^${" ".repeat(Math.max(n,0))}`}class t extends Error{params;constructor(e){super(`recur, params: ${e}`),Object.setPrototypeOf(this,t.prototype),this.name="RecurSignal",this.params=e}}class n extends Error{sourceCodeInfo;shortMessage;constructor(t,r){const o=t instanceof Error?t.message:`${t}`;super(function(t,n){if(!n)return t;const r=`${n.position.line}:${n.position.column}`;return`${t}${n.filePath?`\n${n.filePath}:${r}`:`\nLocation ${r}`}\n${n.code}\n${e(n)}`}(o,r)),this.shortMessage=o,this.sourceCodeInfo=r,Object.setPrototypeOf(this,n.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&e(this.sourceCodeInfo)}}class r extends n{userMessage;constructor(e,t){super(e,t),this.userMessage=e,Object.setPrototypeOf(this,r.prototype),this.name="UserDefinedError"}}class o extends n{constructor(e,t){super(e,t),Object.setPrototypeOf(this,o.prototype),this.name="AssertionError"}}class i extends n{symbol;constructor(e,t){super(`Undefined symbol '${e}'.`,t),this.symbol=e,Object.setPrototypeOf(this,i.prototype),this.name="UndefinedSymbolError"}}function a(e){return e instanceof n}const s={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},u=new Set(Object.values(s));function c(e){return Object.keys(s).find(t=>s[t]===e)}const l=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const f="^^fn^^",d="^^re^^";function h(e){return null!==e&&"object"==typeof e&&(f in e&&"functionType"in e&&function(e){return"string"==typeof e&&l.has(e)}(e.functionType))}function p(e){return!(!Array.isArray(e)||e.length<2)&&function(e){return"number"==typeof e&&u.has(e)}(e[0])}function y(e){return h(e)?`<function ${e.name||"λ"}>`:p(e)?`${c(e[0])}-node`:null===e?"null":"object"==typeof e&&e instanceof RegExp?`${e}`:"object"==typeof e&&e instanceof Error?e.toString():JSON.stringify(e)}function g(e,t){return e?.sourceCodeInfo??t}function m(e,t,r){return new n(`Expected ${e}, got ${y(t)}.`,g(t,r))}function v(e,t){return b(e,t),e}function b(e,t){if(!function(e){return void 0!==e}(e))throw new n("Unexpected undefined",g(e,t))}function w(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)}function S(e){return null!==e&&"object"==typeof e&&!!e[f]}function x(e,t){return E(e,t),e}function E(e,t){if(!S(e))throw m("LitsFunction",e,t)}function k(e){return S(e)&&"UserDefined"===e.functionType}function N(e,t){return P(e,t),e}function P(e,t){if(!k(e))throw m("NativeJsFunction",e,t)}function $(e){return S(e)&&"NativeJsFunction"===e.functionType}function A(e,t){return I(e,t),e}function I(e,t){if(!$(e))throw m("NativeJsFunction",e,t)}function j(e){return w(e)&&"Builtin"===e.functionType}function C(e){return void 0!==e}function O(e,t){return M(e,t),e}function M(e,t){if(!C(e))throw m("not undefined",e,t)}function U(e){return Array.isArray(e)||"string"==typeof e}function F(e,t){if(!U(e))throw m("string or array",e,t)}function T(e){return!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof RegExp||S(e)||D(e))}function B(e,t){if(!T(e))throw m("object",e,t)}function R(e){return U(e)||T(e)}function _(e,t){if(!R(e))throw m("string, array or object",e,t)}function D(e){return null!==e&&"object"==typeof e&&!!e[d]}function z(e,t){if(!function(e){return D(e)||"string"==typeof e}(e))throw m("string or RegularExpression",e,t)}function L(e,t){return V(e,t),e}function V(e,t){if(!function(e){return"number"==typeof e||!!R(e)||!!S(e)}(e))throw m("FunctionLike",e,t)}function W(e,t={}){return"string"==typeof e&&((!t.nonEmpty||0!==e.length)&&(!t.char||1===e.length))}function J(e,t,n={}){if(!W(e,n))throw m(""+(n.nonEmpty?"non empty string":n.char?"character":"string"),e,t)}function q(e,t,n={}){return J(e,t,n),e}function Y(e){return"string"==typeof e||"number"==typeof e}function G(e,t){return X(e,t),e}function X(e,t){if(!Y(e))throw m("string or number",e,t)}const H={assert:{evaluate:(e,t)=>{const n=e[0],r=2===e.length?e[1]:`${n}`;if(J(r,t),!n)throw new o(r,t);return O(n,t)},arity:{min:1,max:2}}};function K(e,t={}){return"number"==typeof e&&(!Number.isNaN(e)&&(!(t.integer&&!Number.isInteger(e))&&(!(t.finite&&!Number.isFinite(e))&&((!t.zero||0===e)&&((!t.nonZero||0!==e)&&(!(t.positive&&e<=0)&&(!(t.negative&&e>=0)&&(!(t.nonPositive&&e>0)&&(!(t.nonNegative&&e<0)&&(!("number"==typeof t.gt&&e<=t.gt)&&(!("number"==typeof t.gte&&e<t.gte)&&(!("number"==typeof t.lt&&e>=t.lt)&&!("number"==typeof t.lte&&e>t.lte)))))))))))))}function Z(e,t,r={}){if(!K(e,r))throw new n(`Expected ${function(e){if(e.zero)return"zero";const t=function(e){return e.positive?"positive":e.negative?"negative":e.nonNegative?"non negative":e.nonPositive?"non positive":e.nonZero?"non zero":""}(e),n=e.integer?"integer":"number",r=e.finite?"finite":"",o=function(e){return"number"!=typeof e.gt&&"number"!=typeof e.gte||"number"!=typeof e.lt&&"number"!=typeof e.lte?"number"==typeof e.gt||"number"==typeof e.gte?"number"==typeof e.gt?`n > ${e.gt}`:`n >= ${e.gte}`:"number"==typeof e.lt||"number"==typeof e.lte?"number"==typeof e.lt?`n < ${e.lt}`:`n <= ${e.lte}`:"":`${"number"==typeof e.gt?`${e.gt} < n `:`${e.gte} <= n `}${"number"==typeof e.lt?`< ${e.lt}`:`<= ${e.lte}`}`}(e);return[t,r,n,o].filter(e=>!!e).join(" ")}(r)}, got ${y(e)}.`,g(e,t))}function Q(e,t,r){const{min:o,max:i}=e;if("number"==typeof o&&t<o)throw new n(`Wrong number of arguments, expected at least ${o}, got ${y(t)}.`,r);if("number"==typeof i&&t>i)throw new n(`Wrong number of arguments, expected at most ${i}, got ${y(t)}.`,r)}function ee(e){return{min:e,max:e}}const te={"<<":{evaluate:([e,t],n)=>(Z(e,n,{integer:!0}),Z(t,n,{integer:!0,nonNegative:!0}),e<<t),arity:ee(2)},">>":{evaluate:([e,t],n)=>(Z(e,n,{integer:!0}),Z(t,n,{integer:!0,nonNegative:!0}),e>>t),arity:ee(2)},">>>":{evaluate:([e,t],n)=>(Z(e,n,{integer:!0}),Z(t,n,{integer:!0,nonNegative:!0}),e>>>t),arity:ee(2)},"&":{evaluate:([e,...t],n)=>(Z(e,n,{integer:!0}),t.reduce((e,t)=>(Z(t,n,{integer:!0}),e&t),e)),arity:{min:2}},"|":{evaluate:([e,...t],n)=>(Z(e,n,{integer:!0}),t.reduce((e,t)=>(Z(t,n,{integer:!0}),e|t),e)),arity:{min:2}},xor:{evaluate:([e,...t],n)=>(Z(e,n,{integer:!0}),t.reduce((e,t)=>(Z(t,n,{integer:!0}),e^t),e)),arity:{min:2}}};function ne(e,t){return!!R(e)&&("string"==typeof e||Array.isArray(e)?!!K(t,{integer:!0})&&(t>=0&&t<e.length):!!Object.getOwnPropertyDescriptor(e,t))}function re(e,t,r){if(X(e,r),X(t,r),"string"==typeof e&&"string"==typeof t)return e<t?-1:e>t?1:0;if("number"==typeof e&&"number"==typeof t)return Math.sign(e-t);throw new n(`Cannot compare values of different types: ${typeof e} and ${typeof t}`,r)}function oe(e,t,n){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return function(e,t,n=ue){if(e===t)return!0;const r=Math.abs(e-t);if(0===e||0===t||r<n)return r<n;const o=Math.abs(e),i=Math.abs(t);return r/(o+i)<n}(e,t);if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r+=1)if(!oe(O(e[r],n),O(t[r],n),n))return!1;return!0}if(D(e)&&D(t))return e.s===t.s&&e.f===t.f;if(w(e)&&w(t)){const r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let o=0;o<r.length;o+=1){const i=q(r[o],n);if(!oe(e[i],t[i],n))return!1}return!0}return!1}function ie(e){return e??null}function ae(...e){const t=new Set;for(const n of e)n.forEach(e=>t.add(e));return t}function se(e,t){t.forEach(t=>e.add(t))}const ue=1e-10;function ce(e,t=0){const n=e.split("\n");for(;n[0]?.match(/^\s*$/);)n.shift();for(;n[n.length-1]?.match(/^\s*$/);)n.pop();const r=n.reduce((e,t)=>{if(t.match(/^\s*$/))return e;const n=t.match(/^\s*/)[0].length;return Math.min(e,n)},1/0);return n.map(e=>" ".repeat(t)+e.slice(r)).join("\n").trimEnd()}function le(e,t){if(!Array.isArray(e))throw m("array",e,t)}function fe(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e)}(e))throw m("array of strings",e,t)}function de(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e&&1===e.length)}(e))throw m("array of strings",e,t)}function he(e,t){return e instanceof Promise?e.then(t):t(e)}function pe(e,t){const n=[];for(let r=0;r<e.length;r++){const o=t(e[r],r);if(o instanceof Promise)return ye(o,n,e,t,r);n.push(o)}return n}async function ye(e,t,n,r,o){t.push(await e);for(let e=o+1;e<n.length;e++)t.push(await r(n[e],e));return t}function ge(e,t,n){let r=n;for(let n=0;n<e.length;n++){const o=t(r,e[n],n);if(o instanceof Promise)return me(o,e,t,n);r=o}return r}async function me(e,t,n,r){let o=await e;for(let e=r+1;e<t.length;e++)o=await n(o,t[e],e);return o}function ve(e,t){for(let n=0;n<e.length;n++){const r=t(e[n],n);if(r instanceof Promise)return be(r,e,t,n)}}async function be(e,t,n,r){await e;for(let e=r+1;e<t.length;e++)await n(t[e],e)}function we(e,t){try{const n=e();return n instanceof Promise?n.catch(t):n}catch(e){return t(e)}}function Se(e,t){for(let n=0;n<e.length;n++){const r=t(e[n],n);if(r instanceof Promise)return xe(r,e,t,n);if(r)return n}return-1}async function xe(e,t,n,r){if(await e)return r;for(let e=r+1;e<t.length;e++)if(await n(t[e],e))return e;return-1}const Ee={filter:{evaluate:([e,t],n,r,{executeFunction:o})=>{if(_(e,n),V(t,n),Array.isArray(e))return ge(e,(e,i)=>he(o(t,[i],r,n),t=>(t&&e.push(i),e)),[]);if(W(e)){return he(ge(e.split(""),(e,i)=>he(o(t,[i],r,n),t=>(t&&e.push(i),e)),[]),e=>e.join(""))}return ge(Object.entries(e),(e,[i,a])=>he(o(t,[a],r,n),t=>(t&&(e[i]=a),e)),{})},arity:ee(2)},map:{evaluate:(e,t,r,{executeFunction:o})=>{const i=L(e.at(-1),t);if(T(e[0]))return function({colls:e,contextStack:t,executeFunction:r,fn:o,sourceCodeInfo:i}){B(e[0],i);const a=Object.keys(e[0]),s={};return e.forEach(e=>{B(e,i);const t=Object.keys(e);if(t.length!==a.length)throw new n(`All objects must have the same keys. Expected: ${a.join(", ")}. Found: ${t.join(", ")}`,i);if(!t.every(e=>a.includes(e)))throw new n(`All objects must have the same keys. Expected: ${a.join(", ")}. Found: ${t.join(", ")}`,i);Object.entries(e).forEach(([e,t])=>{s[e]||(s[e]=[]),s[e].push(t)})}),ge(a,(e,n)=>he(r(o,s[n],t,i),t=>(e[n]=t,e)),{})}({colls:e.slice(0,-1),fn:i,sourceCodeInfo:t,contextStack:r,executeFunction:o});const a=e.slice(0,-1);F(a[0],t);const s="string"==typeof a[0];let u=a[0].length;a.slice(1).forEach(e=>{s?J(e,t):le(e,t),u=Math.min(u,e.length)});const c=[];for(let e=0;e<u;e++)c.push(a.map(t=>t[e]));const l=pe(c,e=>o(i,e,r,t));return s?he(l,e=>(e.forEach(e=>J(e,t)),e.join(""))):l},arity:{min:2}},reduce:{evaluate:([e,t,n],r,o,{executeFunction:i})=>(_(e,r),V(t,r),M(n,r),"string"==typeof e?(J(n,r),0===e.length?n:ge(e.split(""),(e,n)=>i(t,[e,n],o,r),n)):Array.isArray(e)?0===e.length?n:ge(e,(e,n)=>i(t,[e,n],o,r),n):0===Object.keys(e).length?n:ge(Object.entries(e),(e,[,n])=>i(t,[e,n],o,r),n)),arity:ee(3)},get:{evaluate:(e,t)=>{const[n,r]=e,o=ie(e[2]);if(X(r,t),null===n)return o;_(n,t);const i=function(e,t){if(T(e)){if("string"==typeof t&&ne(e,t))return ie(e[t])}else if(K(t,{nonNegative:!0,integer:!0})&&t>=0&&t<e.length)return ie(e[t])}(n,r);return void 0===i?o:i},arity:{min:2,max:3}},count:{evaluate:([e],t)=>null===e?0:"string"==typeof e?e.length:(_(e,t),Array.isArray(e)?e.length:Object.keys(e).length),arity:ee(1)},"contains?":{evaluate:([e,t],n)=>null!==e&&(_(e,n),W(e)?(J(t,n),e.includes(t)):U(e)?(M(t,n),!!e.find(e=>oe(O(e),t,n))):(J(t,n),t in e)),arity:ee(2)},assoc:{evaluate:([e,t,n],r)=>(_(e,r),X(t,r),M(n,r),function(e,t,n,r){if(_(e,r),X(t,r),Array.isArray(e)||"string"==typeof e){if(Z(t,r,{integer:!0}),Z(t,r,{gte:0}),Z(t,r,{lte:e.length}),"string"==typeof e)return J(n,r,{char:!0}),`${e.slice(0,t)}${n}${e.slice(t+1)}`;const o=[...e];return o[t]=n,o}J(t,r);const o={...e};return o[t]=n,o}(e,t,n,r)),arity:ee(3)},"++":{evaluate:(e,t)=>(K(e[0])||_(e[0],t),Array.isArray(e[0])?e.reduce((e,n)=>(le(n,t),e.concat(n)),[]):Y(e[0])?e.reduce((e,n)=>(X(n,t),`${e}${n}`),""):e.reduce((e,n)=>(B(n,t),Object.assign(e,n)),{})),arity:{min:1}}},ke={range:{evaluate:(e,t)=>{const[n,r,o]=e;let i,a,s;Z(n,t,{finite:!0}),1===e.length?(i=0,a=n,s=a>=0?1:-1):2===e.length?(Z(r,t,{finite:!0}),i=n,a=r,s=a>=i?1:-1):(Z(r,t,{finite:!0}),Z(o,t,{finite:!0}),i=n,a=r,s=o,Z(s,t,a>i?{positive:!0}:a<i?{negative:!0}:{nonZero:!0}));const u=[];for(let e=i;s<0?e>a:e<a;e+=s)u.push(e);return u},arity:{min:1,max:3}},repeat:{evaluate:([e,t],n)=>{Z(t,n,{integer:!0,nonNegative:!0});const r=[];for(let n=0;n<t;n+=1)r.push(e);return r},arity:ee(2)},flatten:{evaluate:([e,t],n)=>{le(e,n);const r=void 0===t||t===Number.POSITIVE_INFINITY?Number.POSITIVE_INFINITY:function(e,t,n={}){return Z(e,t,n),e}(t,n,{integer:!0,nonNegative:!0});return e.flat(r)},arity:{min:1,max:2}},mapcat:{evaluate:([e,t],n,r,{executeFunction:o})=>(le(e,n),V(t,n),he(pe(e,e=>o(t,[e],r,n)),e=>e.flat(1))),arity:ee(2)},"moving-fn":{evaluate:([e,t,n],r,o,{executeFunction:i})=>{le(e,r),Z(t,r,{integer:!0,lte:e.length}),V(n,r);const a=[];for(let n=0;n<=e.length-t;n++)a.push(e.slice(n,n+t));return pe(a,e=>i(n,[e],o,r))},arity:ee(3)},"running-fn":{evaluate:([e,t],n,r,{executeFunction:o})=>{le(e,n),V(t,n);const i=[];for(let t=0;t<e.length;t+=1)i.push(e.slice(0,t+1));return pe(i,e=>o(t,[e],r,n))},arity:ee(2)}},Ne={nth:{evaluate:(e,t)=>{const[n,r]=e,o=ie(e[2]);if(Z(r,t,{integer:!0}),null===n)return o;if(F(n,t),r>=0&&r<n.length){return ie(n[r])}return o},arity:{min:2,max:3}},first:{evaluate:([e],t)=>{if(null===e)return null;F(e,t);return ie(e[0])},arity:ee(1)},last:{evaluate:([e],t)=>{if(null===e)return null;F(e,t);return ie(e.at(-1))},arity:ee(1)},pop:{evaluate:([e],t)=>(F(e,t),"string"==typeof e?e.substring(0,e.length-1):e.slice(0,e.length-1)),arity:ee(1)},"index-of":{evaluate:([e,t],n)=>{if(M(t,n),null===e)return null;if(F(e,n),"string"==typeof e){J(t,n);const r=e.indexOf(t);return-1!==r?r:null}{const r=e.findIndex(e=>oe(O(e,n),t),n);return-1!==r?r:null}},arity:ee(2)},push:{evaluate:([e,...t],n)=>(F(e,n),"string"==typeof e?(de(t,n),[e,...t].join("")):[...e,...t]),arity:{min:2}},rest:{evaluate:([e],t)=>(F(e,t),Array.isArray(e)?e.length<=1?[]:e.slice(1):e.substring(1)),arity:ee(1)},next:{evaluate:([e],t)=>(F(e,t),Array.isArray(e)?e.length<=1?null:e.slice(1):e.length<=1?null:e.substring(1)),arity:ee(1)},reverse:{evaluate:([e],t)=>null===e?null:(F(e,t),Array.isArray(e)?[...e].reverse():e.split("").reverse().join("")),arity:ee(1)},second:{evaluate:([e],t)=>null===e?null:(F(e,t),ie(e[1])),arity:ee(1)},slice:{evaluate:(e,t)=>{const[n,r,o]=e;return F(n,t),Z(r,t,{integer:!0}),2===e.length?(Array.isArray(n),n.slice(r)):(Z(o,t,{integer:!0}),Array.isArray(n),n.slice(r,o))},arity:{min:2,max:3}},some:{evaluate:([e,t],n,r,{executeFunction:o})=>{if(V(t,n),null===e)return null;if(F(e,n),0===e.length)return null;return ge("string"==typeof e?e.split(""):e,(e,i)=>null!==e?e:he(o(t,[i],r,n),e=>e?ie(i):null),null)},arity:ee(2)},sort:{evaluate:(e,t,n,{executeFunction:r})=>{const[o]=e,i=1===e.length,a=i?null:e[1];if(F(o,t),"string"==typeof o){const e=o.split("");return i?e.sort((e,n)=>re(e,n,t)):(V(a,t),e.sort((e,o)=>{const i=r(a,[e,o],n,t);if(i instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return Z(i,t,{finite:!0}),i})),e.join("")}const s=[...o];return i?s.sort((e,n)=>(X(e,t),X(n,t),re(e,n,t))):s.sort((e,o)=>{V(a,t);const i=r(a,[e,o],n,t);if(i instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return Z(i,t,{finite:!0}),i}),s},arity:{min:1,max:2}},take:{evaluate:([e,t],n)=>{Z(t,n),F(e,n);const r=Math.max(Math.ceil(t),0);return e.slice(0,r)},arity:ee(2)},"take-last":{evaluate:([e,t],n)=>{F(e,n),Z(t,n);const r=Math.max(Math.ceil(t),0),o=e.length-r;return e.slice(o)},arity:ee(2)},drop:{evaluate:([e,t],n)=>{Z(t,n);const r=Math.max(Math.ceil(t),0);return F(e,n),e.slice(r)},arity:ee(2)},"drop-last":{evaluate:([e,t],n)=>{F(e,n),Z(t,n);const r=Math.max(Math.ceil(t),0),o=e.length-r;return e.slice(0,o)},arity:ee(2)},"take-while":{evaluate:([e,t],n,r,{executeFunction:o})=>{F(e,n),V(t,n);const i="string"==typeof e?e.split(""):Array.from(e);return he(Se(i,e=>he(o(t,[e],r,n),e=>!e)),t=>{const n=-1===t?i:i.slice(0,t);return"string"==typeof e?n.join(""):n})},arity:ee(2)},"drop-while":{evaluate:([e,t],n,r,{executeFunction:o})=>{F(e,n),V(t,n);const i=Array.isArray(e)?e:e.split("");return he(Se(i,e=>he(o(t,[e],r,n),e=>!e)),t=>-1===t?"string"==typeof e?"":[]:"string"==typeof e?i.slice(t).join(""):e.slice(t))},arity:ee(2)}},Pe=new WeakSet,$e=new WeakSet,Ae=new WeakSet,Ie=new WeakSet,je=new WeakSet,Ce=new WeakSet,Oe=new WeakSet;function Me(e){return Array.isArray(e)?(Pe.has(e)||(Ue(e),Re(e)||Be(e)),e):e}function Ue(e){return!!Array.isArray(e)&&(!!$e.has(e)||!Ae.has(e)&&(e.every(e=>K(e))?(Pe.add(e),$e.add(e),!0):(Ae.add(e),!1)))}function Fe(e,t){if(!Ue(e))throw new n(`Expected a vector, but got ${e}`,t)}function Te(e,t){if(Fe(e,t),0===e.length)throw new n(`Expected a non empty vector, but got ${e}`,t)}function Be(e){if(!Array.isArray(e))return!1;if(Ce.has(e))return!0;if(Oe.has(e))return!1;if(0===e.length)return Oe.add(e),!1;if(!Array.isArray(e[0]))return Oe.add(e),!1;const t=e[0].length;for(const n of e.slice(1)){if(!Array.isArray(n))return Oe.add(e),!1;if(n.length!==t)return Oe.add(e),!1}return Pe.add(e),Ce.add(e),!0}function Re(e){if(!Array.isArray(e))return!1;if(Ie.has(e))return!0;if(je.has(e))return!1;if(0===e.length)return je.add(e),!1;if(!Array.isArray(e[0])||0===e[0].length)return je.add(e),!1;const t=e[0].length;for(const n of e)if(!Array.isArray(n)||n.length!==t||n.some(e=>!K(e)))return je.add(e),!1;return Pe.add(e),Ce.add(e),Ie.add(e),!0}function _e(e,t){let r=!1,o=!1;for(const i of e)if(Ue(i))r=!0;else if(Re(i))o=!0;else if(!K(i))throw new n("Invalid parameter type: "+typeof i,t);if(o){if(r)throw new n("Cannot mix vector and matrix types",t);let o=null,i=null;for(const r of e)if(Re(r))if(null===o)o=r.length,i=r[0].length;else if(r.length!==o||r[0].length!==i)throw new n("Matrix dimensions do not match",t);return["matrix",e.map(e=>Re(e)?e:Array.from({length:o},()=>Array.from({length:i},()=>e)))]}if(r){let r=null;for(const o of e)if(Ue(o))if(null===r)r=o.length;else if(o.length!==r)throw new n("Vector lengths do not match",t);return["vector",e.map(e=>Ue(e)?e:Array.from({length:r},()=>e))]}return["number",e]}function De(e){return(t,n)=>{const[r,o]=_e(t,n);return"number"===r?e(o[0]):"vector"===r?o[0].map(t=>e(t)):o[0].map(t=>t.map(t=>e(t)))}}function ze(e){return(t,n)=>{const[r,o]=_e(t,n);return"number"===r?e(o[0],o[1]):"vector"===r?o[0].map((t,n)=>e(t,o[1][n])):o[0].map((t,n)=>t.map((t,r)=>e(t,o[1][n][r])))}}function Le(e,t){return(n,r)=>{if(0===n.length)return e;const[o,i]=_e(n,r);if("number"===o)return i.reduce((e,n)=>t(e,n),e);if("vector"===o){const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>t(e,n[r])),e)}{const[e,...n]=i;return n.reduce((e,n)=>e.map((e,r)=>e.map((e,o)=>t(e,n[r][o]))),e)}}}const Ve={inc:{evaluate:De(e=>e+1),arity:ee(1)},dec:{evaluate:De(e=>e-1),arity:ee(1)},"+":{evaluate:Le(0,(e,t)=>e+t),arity:{}},"*":{evaluate:Le(1,(e,t)=>e*t),arity:{}},"/":{evaluate:(e,t)=>{if(0===e.length)return 1;const[n,r]=_e(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?1/e:t.reduce((e,t)=>e/t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e/t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e/t[n][r])),e)}},arity:{}},"-":{evaluate:(e,t)=>{if(0===e.length)return 0;const[n,r]=_e(e,t);if("number"===n){const[e,...t]=r;return 0===t.length?-e:t.reduce((e,t)=>e-t,e)}if("vector"===n){const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e-t[n]),e)}{const e=r[0];return r.slice(1).reduce((e,t)=>e.map((e,n)=>e.map((e,r)=>e-t[n][r])),e)}},arity:{}},quot:{evaluate:ze((e,t)=>Math.trunc(e/t)),arity:ee(2)},mod:{evaluate:ze((e,t)=>e-t*Math.floor(e/t)),arity:ee(2)},"%":{evaluate:ze((e,t)=>e%t),arity:ee(2)},sqrt:{evaluate:De(e=>Math.sqrt(e)),arity:ee(1)},cbrt:{evaluate:De(e=>Math.cbrt(e)),arity:ee(1)},"^":{evaluate:ze((e,t)=>e**t),arity:ee(2)},round:{evaluate:([e,t],n)=>{const[r,o]=_e([e],n);if("number"===r){if(void 0===t||0===t)return Math.round(o[0]);{Z(t,n,{integer:!0,positive:!0});const e=10**t;return Math.round(o[0]*e)/e}}if("vector"===r){const e=o[0];if(void 0===t||0===t)return e.map(e=>Math.round(e));{Z(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>Math.round(e*r)/r)}}{const e=o[0];if(void 0===t||0===t)return e.map(e=>e.map(e=>Math.round(e)));{Z(t,n,{integer:!0,positive:!0});const r=10**t;return e.map(e=>e.map(e=>Math.round(e*r)/r))}}},arity:{min:1,max:2}},trunc:{evaluate:De(e=>Math.trunc(e)),arity:ee(1)},floor:{evaluate:De(e=>Math.floor(e)),arity:ee(1)},ceil:{evaluate:De(e=>Math.ceil(e)),arity:ee(1)},min:{evaluate:(e,t)=>{if(1===e.length&&Ue(e[0])){const n=e[0];return Te(n,t),n.reduce((e,t)=>Math.min(e,t),1/0)}const[n,...r]=e;return Z(n,t),r.reduce((e,n)=>(Z(n,t),Math.min(e,n)),n)},arity:{min:1}},max:{evaluate:(e,t)=>{if(1===e.length&&Ue(e[0])){const n=e[0];return Te(n,t),n.reduce((e,t)=>Math.max(e,t),-1/0)}const[n,...r]=e;return Z(n,t),r.reduce((e,n)=>(Z(n,t),Math.max(e,n)),n)},arity:{min:1}},abs:{evaluate:De(e=>Math.abs(e)),arity:ee(1)},sign:{evaluate:De(e=>Math.sign(e)),arity:ee(1)}};function We([e,...t],n){const r=O(e,n);for(const e of t)if(!oe(r,O(e,n),n))return!1;return!0}const Je={"==":{evaluate:(e,t)=>We(e,t),arity:{min:1}},"!=":{evaluate:(e,t)=>!We(e,t),arity:{min:1}},"identical?":{evaluate:e=>function([e,...t]){for(const n of t)if(n!==e)return!1;return!0}(e),arity:{min:1}},">":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(re(r,G(e),n)<=0)return!1;r=G(e)}return!0},arity:{min:1}},"<":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(re(r,G(e),n)>=0)return!1;r=G(e)}return!0},arity:{min:1}},">=":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(re(r,G(e),n)<0)return!1;r=G(e)}return!0},arity:{min:1}},"<=":{evaluate:([e,...t],n)=>{let r=G(e);for(const e of t){if(re(r,G(e),n)>0)return!1;r=G(e)}return!0},arity:{min:1}},not:{evaluate:([e])=>!e,arity:ee(1)},"epoch->iso-date":{evaluate:([e],t)=>(Z(e,t),new Date(e).toISOString()),arity:ee(1)},"iso-date->epoch":{evaluate:([e],t)=>{J(e,t);const n=new Date(e).valueOf();return Z(n,t,{finite:!0}),n},arity:ee(1)},"write!":{evaluate:(e,t)=>(console.log(...e),e.length>0?O(e[e.length-1],t):null),pure:!1,arity:{}},boolean:{evaluate:([e])=>!!e,arity:ee(1)},compare:{evaluate:([e,t],n)=>(X(e,n),X(t,n),re(e,t,n)),arity:ee(2)},"json-parse":{evaluate:([e],t)=>(J(e,t),JSON.parse(e)),arity:ee(1)},"json-stringify":{evaluate:([e,t],n)=>(M(e,n),void 0===t?JSON.stringify(e):(Z(t,n),JSON.stringify(e,null,t))),arity:{min:1,max:2}}},qe={keys:{evaluate:([e],t)=>(B(e,t),Object.keys(e)),arity:ee(1)},vals:{evaluate:([e],t)=>(B(e,t),Object.values(e)),arity:ee(1)},entries:{evaluate:([e],t)=>(B(e,t),Object.entries(e)),arity:ee(1)},find:{evaluate:([e,t],n)=>(B(e,n),J(t,n),ne(e,t)?[t,e[t]]:null),arity:ee(2)},dissoc:{evaluate:([e,t],n)=>{B(e,n),J(t,n);const r={...e};return delete r[t],r},arity:ee(2)},merge:{evaluate:(e,t)=>{if(0===e.length)return null;const[n,...r]=e;return B(n,t),r.reduce((e,n)=>(B(n,t),{...e,...n}),{...n})},arity:{min:0}},"merge-with":{evaluate:(e,t,n,{executeFunction:r})=>{const o=e[0],i=e.at(-1),a=e.slice(1,-1);return B(o,t),V(i,t),ge(a,(e,o)=>{B(o,t);return he(ge(Object.entries(o),(e,o)=>{const a=q(o[0],t),s=ie(o[1]);return ne(e,a)?he(r(i,[e[a],s],n,t),t=>(e[a]=t,e)):(e[a]=s,e)},e),e=>e)},{...o})},arity:{min:2}},zipmap:{evaluate:([e,t],n)=>{fe(e,n),le(t,n);const r=Math.min(e.length,t.length),o={};for(let i=0;i<r;i+=1){o[q(e[i],n)]=ie(t[i])}return o},arity:ee(2)},"select-keys":{evaluate:([e,t],n)=>(fe(t,n),B(e,n),t.reduce((t,n)=>(ne(e,n)&&(t[n]=ie(e[n])),t),{})),arity:ee(2)}},Ye={"function?":{evaluate:([e])=>S(e),arity:ee(1)},"string?":{evaluate:([e])=>"string"==typeof e,arity:ee(1)},"number?":{evaluate:([e])=>"number"==typeof e,arity:ee(1)},"integer?":{evaluate:([e])=>"number"==typeof e&&K(e,{integer:!0}),arity:ee(1)},"boolean?":{evaluate:([e])=>"boolean"==typeof e,arity:ee(1)},"null?":{evaluate:([e])=>null==e,arity:ee(1)},"zero?":{evaluate:([e],t)=>(Z(e,t,{finite:!0}),Math.abs(e)<ue),arity:ee(1)},"pos?":{evaluate:([e],t)=>(Z(e,t,{finite:!0}),e>0),arity:ee(1)},"neg?":{evaluate:([e],t)=>(Z(e,t,{finite:!0}),e<0),arity:ee(1)},"even?":{evaluate:([e],t)=>(Z(e,t,{finite:!0}),e%2==0),arity:ee(1)},"odd?":{evaluate:([e],t)=>(Z(e,t,{finite:!0}),K(e,{integer:!0})&&e%2!=0),arity:ee(1)},"array?":{evaluate:([e])=>Array.isArray(e),arity:ee(1)},"collection?":{evaluate:([e])=>R(e),arity:ee(1)},"sequence?":{evaluate:([e])=>U(e),arity:ee(1)},"object?":{evaluate:([e])=>T(e),arity:ee(1)},"regexp?":{evaluate:([e])=>D(e),arity:ee(1)},"finite?":{evaluate:([e],t)=>(Z(e,t),Number.isFinite(e)),arity:ee(1)},"positive-infinity?":{evaluate:([e],t)=>(Z(e,t),e===Number.POSITIVE_INFINITY),arity:ee(1)},"negative-infinity?":{evaluate:([e],t)=>(Z(e,t),e===Number.NEGATIVE_INFINITY),arity:ee(1)},"true?":{evaluate:([e])=>!0===e,arity:ee(1)},"false?":{evaluate:([e])=>!1===e,arity:ee(1)},"empty?":{evaluate:([e],t)=>null===e||(_(e,t),"string"==typeof e||Array.isArray(e)?0===e.length:0===Object.keys(e).length),arity:ee(1)},"not-empty?":{evaluate:([e],t)=>null!==e&&(_(e,t),"string"==typeof e||Array.isArray(e)?e.length>0:Object.keys(e).length>0),arity:ee(1)},"vector?":{evaluate:([e])=>Ue(e),arity:ee(1)},"matrix?":{evaluate:([e])=>Re(e),arity:ee(1)},"grid?":{evaluate:([e])=>Be(e),arity:ee(1)}},Ge={regexp:{evaluate:([e,t],r)=>{J(e,r);const o=e||"(?:)",i="string"==typeof t?t:"";try{new RegExp(o,i)}catch(e){throw new n(`Invalid regular expression: ${o} ${i}`,r)}return{[d]:!0,sourceCodeInfo:r,s:o,f:i}},arity:{min:1,max:2}},"re-match":{evaluate:([e,t],n)=>{if(function(e,t){if(!D(e))throw m("RegularExpression",e,t)}(t,n),!W(e))return null;const r=new RegExp(t.s,t.f).exec(e);return r?[...r]:null},arity:ee(2)},replace:{evaluate:([e,t,n],r)=>{J(e,r),z(t,r),J(n,r);const o=D(t)?new RegExp(t.s,`${t.f}`):t;return e.replace(o,n)},arity:ee(3)},"replace-all":{evaluate:([e,t,n],r)=>{J(e,r),z(t,r),J(n,r);const o=D(t)?new RegExp(t.s,`${t.f.includes("g")?t.f:`${t.f}g`}`):t;return e.replaceAll(o,n)},arity:ee(3)}},Xe=/^\s*$/,He={str:{evaluate:e=>e.reduce((e,t)=>e+(null==t?"":T(t)||Array.isArray(t)?JSON.stringify(t):`${t}`),""),arity:{}},number:{evaluate:([e],t)=>{J(e,t);const r=Number(e);if(Number.isNaN(r))throw new n(`Could not convert '${e}' to a number.`,t);return r},arity:ee(1)},"lower-case":{evaluate:([e],t)=>(J(e,t),e.toLowerCase()),arity:ee(1)},"upper-case":{evaluate:([e],t)=>(J(e,t),e.toUpperCase()),arity:ee(1)},trim:{evaluate:([e],t)=>(J(e,t),e.trim()),arity:ee(1)},join:{evaluate:([e,t],n)=>(le(e,n),e.forEach(e=>X(e,n)),J(t,n),e.join(t)),arity:ee(2)},split:{evaluate:([e,t,n],r)=>{J(e,r),z(t,r),void 0!==n&&Z(n,r,{integer:!0,nonNegative:!0});const o="string"==typeof t?t:new RegExp(t.s,t.f);return e.split(o,n)},arity:{min:2,max:3}},"blank?":{evaluate:([e],t)=>null===e||(J(e,t),Xe.test(e)),arity:ee(1)}},Ke={"|>":{evaluate:([e,t],n,r,{executeFunction:o})=>(V(t,n),o(t,[e],r,n)),arity:ee(2)},apply:{evaluate:([e,...t],n,r,{executeFunction:o})=>{V(e,n);const i=t[t.length-1];le(i,n);return o(e,[...t.slice(0,-1),...i],r,n)},arity:{min:2}},identity:{evaluate:([e])=>ie(e),arity:ee(1)},comp:{evaluate:(e,t)=>{return e.forEach(e=>V(e,t)),{[f]:!0,sourceCodeInfo:t,functionType:"Comp",params:e,arity:e.length>0?(n=e.at(-1),"number"==typeof n||R(n)?ee(1):n.arity):{min:1,max:1}};var n},arity:{}},constantly:{evaluate:([e],t)=>({[f]:!0,sourceCodeInfo:t,functionType:"Constantly",value:ie(e),arity:{}}),arity:ee(1)}};function Ze(e){return ce(`\n ${e.title}\n\n ${e.description.replace(/`(.+?)`/g,"$1").replace(/\$(\w+)/g,"$1").replace(/\*\*\*(.+)\*\*\*/g,"$1").replace(/\*\*(.+)\*\*/g,"$1")}\n\n Signature:\n ${function({title:e,variants:t,args:n,returns:r,_isOperator:o}){const i=t.map(t=>`${` ${e}(${t.argumentNames.map(e=>{let t="";return n[e].rest&&(t+="..."),t+=e,t}).join(", ")})`} -> ${Qe(r)}`),a=o?["","Operator:",` a ${e} b -> ${Qe(r)}`]:[];return[...i,...a]}(e).join("\n ")}\n\n Arguments:\n ${function(e){return Object.entries(e.args).map(([e,t])=>`${e}: ${Qe(t)}`)}(e).join("\n ")}\n\n Examples:\n${e.examples.map(e=>ce(e,4)).join("\n\n")}`)}function Qe(e){const t=e.type,n=(Array.isArray(t)?t:[t]).join(" | ");return e.array||e.rest?`Array<${n}>`:n}const et={sum:{evaluate:([e],t)=>(Fe(e,t),e.reduce((e,t)=>e+t,0)),arity:ee(1)},prod:{evaluate:([e],t)=>(Fe(e,t),e.reduce((e,t)=>e*t,1)),arity:ee(1)},mean:{evaluate:([e],t)=>(Te(e,t),e.reduce((e,t)=>e+t,0)/e.length),arity:ee(1)},median:{evaluate:([e],t)=>(Te(e,t),function(e){const t=[...e].sort((e,t)=>e-t),n=Math.floor(t.length/2);return t.length%2==0?(t[n-1]+t[n])/2:t[n]}(e)),arity:ee(1)}},tt={...H,...te,...Ee,...ke,...Ne,...Ve,...function(e){return{doc:{evaluate:([t],n)=>{if(b(e),V(t,n),!S(t))return"";if("Builtin"===t.functionType){const n=e[t.name];return n?Ze(n):""}return"UserDefined"===t.functionType||"NativeJsFunction"===t.functionType?t.docString:""},arity:ee(1)},arity:{evaluate:([e],t)=>(V(e,t),S(e)?e.arity:ee(1)),arity:ee(1)}}}({}),...Je,...qe,...Ye,...Ge,...He,...Ke,...et};Object.entries(tt).forEach(([e,t])=>{t.name=e});const nt={...tt},rt={},ot=[];Object.entries(nt).forEach(([e,t],n)=>{rt[e]=n,ot.push(t)});const it={arity:{},evaluate:(e,t,{evaluateNode:n})=>ge(e[1][1],(e,r)=>e?n(r,t):e,!0),evaluateAsNormalExpression:(e,t)=>{let n=!0;for(const r of e)if(n=O(r,t),!n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},at={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1];return function e(o){if(o>=r.length)return null;const[i,a]=r[o];return he(n(i,t),r=>r?n(a,t):e(o+1))}(0)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].flat(),t,r,o)},st=11,ut=12,ct=13,lt=14,ft=15,dt=16;function ht(e,t){if(e[0]===ct)Object.values(e[1][0]).forEach(e=>{e[1][1]&&t(e[1][1]),ht(e,t)});else if(e[0]===lt)for(let n=0;n<e[1][0].length;n+=1){const r=e[1][0][n]??null;null!==r&&(r[1][1]&&t(r[1][1]),ht(r,t))}}function pt(e,t,n){const r=e[2],o={};return he(yt(e,t,n,r,o),()=>o)}function yt(e,t,r,o,i){if(e[0]===ct){!function(e,t){if(!w(e))throw new n(`Expected UnknownRecord, got ${y(e)}.`,g(e,t))}(t,o);const a=new Set;let s;return he(ve(Object.entries(e[1][0]),([e,n])=>{if(n[0]===ut)return void(s=n);a.add(e);const u=t[e];return he(void 0!==u?u:n[1][1]?r(n[1][1]):null,e=>{const t=e??null;return M(t,o),yt(n,t,r,o,i)})}),()=>{if(s){const e=Object.entries(t).filter(([e])=>!a.has(e)).reduce((e,[t,n])=>(e[t]=O(n),e),{});i[s[1][0]]=e}})}if(e[0]===lt){let n=null;le(t,o);const a=[];for(let t=0;t<e[1][0].length;t+=1){const r=e[1][0][t]??null;if(null!==r){if(r[0]===ut){n=t;break}a.push({element:r,index:t})}}return he(ve(a,({element:e,index:n})=>{const a=t[n];return he(void 0!==a?a:e[1][1]?r(e[1][1]):null,t=>{const n=t??null;return M(n,o),yt(e,n,r,o,i)})}),()=>{if(null!==n){const r=t.slice(n),o=e[1][0][n];i[o[1][0]]=r}})}e[0]===ut?i[e[1][0]]=O(t):i[e[1][0][1]]=O(t)}function gt(e){const t={};return mt(e,t),t}function mt(e,t){if(null!==e)if(e[0]===lt)for(const n of e[1][0])mt(n,t);else if(e[0]===ct)for(const n of Object.values(e[1][0]))mt(n,t);else if(e[0]===ut){if(t[e[1][0]])throw new n(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0]]=!0}else if(e[0]===st){if(t[e[1][0][1]])throw new n(`Duplicate binding name: ${e[1][0]}`,e[2]);t[e[1][0][1]]=!0}}function vt(e,t,n){const r={};return he(bt(e,t,n,r),e=>e?r:null)}function bt(e,t,n,r){if(e[0]===dt)return!0;if(e[0]===ft){const r=e[1][0];return he(n(r),e=>oe(t,e))}if(e[0]===st){const o=e[1][0],i=e[1][1];if(null==t){if(i)return he(n(i),e=>(r[o[1]]=O(e),!0));r[o[1]]=t??null}else r[o[1]]=O(t);return!0}if(e[0]===ct){if(!w(t))return!1;const o=new Set;let i;const a=Object.entries(e[1][0]);let s=!0;for(const[e,u]of a)s=he(s,a=>{if(!a)return!1;if(u[0]===ut)return i=u,!0;o.add(e);const s=t[e];if(void 0===s&&u[0]===ft)return he(n(u[1][0]),e=>oe(void 0,e));return he(void 0!==s?s:u[1]&&u[1][1]?n(u[1][1]):null,e=>bt(u,e??null,n,r))});return he(s,e=>{if(!e)return!1;if(i){const e=Object.entries(t).filter(([e])=>!o.has(e)).reduce((e,[t,n])=>(e[t]=O(n),e),{});r[i[1][0]]=e}return!0})}{const o=e;if(!Array.isArray(t))return!1;const i=o[1][0];let a=null;for(let e=0;e<i.length;e+=1){const t=i[e];if(null!==t&&t[0]===ut){a=e;break}}if(null===a&&t.length!==i.length)return!1;if(null!==a&&t.length<a)return!1;let s=!0;for(let e=0;e<i.length;e+=1){const o=i[e];if(null===o)continue;if(o[0]===ut){r[o[1][0]]=t.slice(e);break}const a=o;s=he(s,o=>!!o&&bt(a,O(t[e]),n,r))}return s}}const wt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o]=e[1];return he(n(r,t),e=>function r(i){if(i>=o.length)return null;const[a,s,u]=o[i];return he(vt(a,e,e=>n(e,t)),e=>{if(null===e)return r(i+1);const o={};for(const[t,n]of Object.entries(e))o[t]={value:n};const a=t.create(o);return u?he(n(u,a),e=>e?n(s,a):r(i+1)):n(s,a)})}(0))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=new Set;n([e[1][1]],t,r,o).forEach(e=>i.add(e));for(const[a,s,u]of e[1][2]){const e={};Object.assign(e,gt(a));const c=t.create(e);u&&n([u],c,r,o).forEach(e=>i.add(e)),n([s],c,r,o).forEach(e=>i.add(e))}return i}};function St(e){const t=e[0];return s.UserDefinedSymbol===t||s.NormalBuiltinSymbol===t||s.SpecialBuiltinSymbol===t}function xt(e){return s.UserDefinedSymbol===e[0]}function Et(e,t){return function(e,t){if(!xt(e))throw m("UserDefinedSymbolNode",e,t)}(e,t),e}function kt(e){return s.NormalBuiltinSymbol===e[0]}function Nt(e){return s.SpecialBuiltinSymbol===e[0]}function Pt(e){return!!function(e){return e[0]===s.NormalExpression}(e)&&St(e[1][0])}function $t(e){return e[0]===s.Spread}const At={arity:ee(1),evaluate:(e,t)=>{const n=e[1][1];if(function(e,t){if(!St(e))throw m("SymbolNode",e,t)}(n),!xt(n))return!0;return null!==t.lookUp(n)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)},It={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const r=t.create({});return ge(e[1][1],(e,t)=>n(t,r),null)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t.create({}),r,o)},jt={arity:{},evaluate:(e,t,{builtin:n,getUndefinedSymbols:r,evaluateNode:o})=>{const i=e[1][1],a=e[1][2],s=function(e,t,n,r,o){const i={},a=e[0].reduce((e,t)=>(Object.keys(gt(t)).forEach(t=>{e[t]={value:null}}),e),{}),s=r(e[1],t.new(a),n,o);s.forEach(e=>{const n=t.getValue(e);C(n)&&(i[e]={value:n})});const u=[e[0],e[1],i];return u}(i,t,n,r,o),u=s[0].filter(e=>e[0]!==ut&&void 0===e[1][1]).length,c={min:u>0?u:void 0,max:s[0].some(e=>e[0]===ut)?void 0:s[0].length};return{[f]:!0,sourceCodeInfo:e[2],functionType:"UserDefined",name:void 0,evaluatedfunction:s,arity:c,docString:a}},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>function(e,t,n,r,o){const i=new Set,a={self:{value:null}};e[0].forEach(e=>{Object.assign(a,gt(e)),ht(e,e=>{se(i,n([e],t,r,o))})});const s=t.create(a),u=n(e[1],s,r,o);return se(i,u),i}(e[1][1],t,n,r,o)};const Ct={arity:{min:2,max:3},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return he(n(r,t),e=>e?n(o,t):i?n(i,t):null)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)},Ot={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[r,o,i]=e[1][1];return he(n(r,t),e=>e?i?n(i,t):null:n(o,t))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1].filter(e=>!!e),t,r,o)},Mt={arity:ee(0),evaluate:(e,t,{evaluateNode:n})=>{const r=e[1][1],o=r[1][0],i=r[1][1];return he(n(i,t),e=>he(pt(o,e,e=>n(e,t)),n=>(t.addValues(n,o[2]),e)))},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],a=i[1][0],s=i[1][1],u=n([s],t,r,o);return ht(a,e=>{se(u,n([e],t,r,o))}),t.addValues(gt(a),a[2]),u}},Ut={arity:{},evaluate:(e,r,{evaluateNode:o})=>{const i=e[1][1],a=ge(i,(e,t)=>he(o(t[1][1],r.create(e)),n=>he(pt(t[1][0],n,e=>o(e,r)),t=>(Object.entries(t).forEach(([t,n])=>{e[t]={value:n}}),e))),{});return he(a,a=>{const s=r.create(a),u=e[1][2];function c(t){if(t.length!==i.length)throw new n(`recur expected ${i.length} parameters, got ${y(t.length)}`,e[2]);return he(ve(i,(e,n)=>he(pt(e[1][0],O(t[n]),e=>o(e,r)),e=>{for(const[t,n]of Object.entries(e))a[t].value=n})),()=>l())}function l(){return we(()=>o(u,s),e=>{if(e instanceof t)return c(e.params);throw e})}for(;;)try{const e=o(u,s);return e instanceof Promise?e.catch(e=>{if(e instanceof t)return c(e.params);throw e}):e}catch(s){if(s instanceof t){const t=s.params;if(t.length!==i.length)throw new n(`recur expected ${i.length} parameters, got ${y(t.length)}`,e[2]);for(let e=0;e<i.length;e+=1){const n=pt(i[e][1][0],O(t[e]),e=>o(e,r));if(n instanceof Promise)return n.then(n=>{for(const[e,t]of Object.entries(n))a[e].value=t;return he(ve(i.slice(e+1),(n,i)=>he(pt(n[1][0],O(t[e+1+i]),e=>o(e,r)),e=>{for(const[t,n]of Object.entries(e))a[t].value=n})),()=>l())});for(const[e,t]of Object.entries(n))a[e].value=t}continue}throw s}})},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const i=e[1][1],a=i.reduce((e,t)=>{const n=gt(t[1][0]);return Object.keys(n).forEach(t=>{e[t]={value:!0}}),e},{});return ae(n(i.map(e=>e[1][1]),t,r,o),n([e[1][2]],t.create(a),r,o))}};function Ft(e,t,n,r){const o=t[2],[,i,a]=t[1],s=[],u=i.map(()=>0);return function t(){const c={},l=n.create(c);return he(function e(t){if(t>=i.length)return"continue";const[n,a,s,f]=i[t],[d,h]=n[1];return he(r(h,l),n=>{const i=function(e,t){return _(e,t),e}(n,o),h=U(i)?i:Object.entries(i);if(0===h.length)return"abort";const p=v(u[t],o);if(p>=h.length)return 0===t?"abort":(u[t]=0,u[t-1]=v(u[t-1],o)+1,"skip");const y=O(h[p],o);return he(pt(d,y,e=>r(e,l)),n=>(Object.entries(n).forEach(([e,t])=>{c[e]={value:t}}),he(a.length>0?function(e,t,n,r){let o;for(const i of e)o=he(o,()=>{const[e,o]=i[1];return he(r(o,n),o=>he(pt(e,o,e=>r(e,n)),e=>{Object.entries(e).forEach(([e,n])=>{t[e]={value:n}})}))});return o}(a,c,l,r):void 0,()=>s?he(r(s,l),n=>n?f?he(r(f,l),n=>n?e(t+1):(u[t]=Number.POSITIVE_INFINITY,"skip")):e(t+1):(u[t]=v(u[t],o)+1,"skip")):f?he(r(f,l),n=>n?e(t+1):(u[t]=Number.POSITIVE_INFINITY,"skip")):e(t+1))))})}(0),n=>"abort"===n?e?s:null:"skip"===n?t():he(r(a,l),n=>(e&&s.push(n),u.length>0&&(u[u.length-1]+=1),t())))}()}function Tt(e,t,n,r,o){const i=new Set,a={},[,s,u]=e[1];return s.forEach(e=>{const[s,u,c,l]=e,[f,d]=s[1];n([d],t.create(a),r,o).forEach(e=>i.add(e)),Object.assign(a,gt(f)),u&&u.forEach(e=>{const[s,u]=e[1];n([u],t.create(a),r,o).forEach(e=>i.add(e)),Object.assign(a,gt(s))}),c&&n([c],t.create(a),r,o).forEach(e=>i.add(e)),l&&n([l],t.create(a),r,o).forEach(e=>i.add(e))}),n([u],t.create(a),r,o).forEach(e=>i.add(e)),i}const Bt={arity:ee(1),evaluate:(e,t,n)=>Ft(!0,e,t,n.evaluateNode),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>Tt(e,t,n,r,o)},Rt={arity:ee(1),evaluate:(e,t,n)=>he(Ft(!1,e,t,n.evaluateNode),()=>null),getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>Tt(e,t,n,r,o)},_t={arity:{},evaluate:(e,t,{evaluateNode:n})=>ge(e[1][1],(e,r)=>e||n(r,t),!1),evaluateAsNormalExpression:(e,t)=>{let n=!1;for(const r of e)if(n=O(r,t),n)break;return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Dt={arity:{min:1},evaluate:(e,t,{evaluateNode:n})=>{const r=Symbol("qq-sentinel");return he(ge(e[1][1],(e,o)=>e!==r?e:xt(o)&&null===t.lookUp(o)?r:he(n(o,t),e=>null!==e?e:r),r),e=>e===r?null:e)},evaluateAsNormalExpression:(e,t)=>{for(const n of e){const e=O(n,t);if(null!==e)return e}return null},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},zt={arity:{},evaluate:(e,n,{evaluateNode:r})=>he(pe(e[1][1],e=>r(e,n)),e=>{throw new t(e)}),evaluateAsNormalExpression:e=>{throw new t(e)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Lt={arity:ee(1),evaluate:(e,t,{evaluateNode:n})=>he(n(e[1][1],t),t=>{const n=q(t,e[2],{nonEmpty:!0});throw new r(n,e[2])}),evaluateAsNormalExpression:(e,t)=>{const n=q(e[0],t,{nonEmpty:!0});throw new r(n,void 0)},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n([e[1][1]],t,r,o)},Vt={arity:{},evaluate:(e,t,{evaluateNode:n})=>{const[,r,o,i]=e[1];return we(()=>n(r,t),e=>{const r=o?{[o[1]]:{value:e}}:{};return n(i,t.create(r))})},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>{const[,i,a,s]=e[1],u=n([i],t,r,o),c=a?{[a[1]]:{value:!0}}:{};return ae(u,n([s],t.create(c),r,o))}},Wt={arity:{},evaluate:(e,t,{evaluateNode:r})=>{const o=[];return he(ve(e[1][1],e=>$t(e)?he(r(e[1],t),t=>{if(!Array.isArray(t))throw new n("Spread value is not an array",e[2]);o.push(...t)}):he(r(e,t),e=>{o.push(e)})),()=>o)},evaluateAsNormalExpression:(e,t)=>{const n=[];for(const r of e)n.push(O(r,t));return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},Jt={arity:{},evaluate:(e,t,{evaluateNode:r})=>{const o={},i=e[1][1];return function e(a){if(a>=i.length)return o;const s=i[a];if($t(s))return he(r(s[1],t),t=>{if(!w(t))throw new n("Spread value is not an object",s[2]);return Object.assign(o,t),e(a+1)});{const u=i[a+1];if(void 0===u)throw new n("Missing value for key",s[2]);return he(r(s,t),n=>he(r(u,t),t=>(J(n,s[2]),o[n]=t,e(a+2))))}}(0)},evaluateAsNormalExpression:(e,t)=>{const n={};for(let r=0;r<e.length;r+=2){const o=e[r],i=e[r+1];J(o,t),n[o]=i??null}return n},getUndefinedSymbols:(e,t,{getUndefinedSymbols:n,builtin:r,evaluateNode:o})=>n(e[1][1],t,r,o)},qt={arity:ee(1),evaluate:(e,t)=>{const r=e[1][1],o=e[2],i=t.getValueModule(r);if(i.found)return i.value;const a=t.getModule(r);if(!a)throw new n(`Unknown module: '${r}'`,o);const s={};for(const[e,t]of Object.entries(a.functions))s[e]={[f]:!0,sourceCodeInfo:o,functionType:"Module",moduleName:r,functionName:e,arity:t.arity};return s},getUndefinedSymbols:()=>new Set},Yt={"??":0,"&&":1,"||":2,array:3,cond:4,"defined?":5,block:6,doseq:7,"0_lambda":8,for:9,if:10,let:11,loop:12,object:13,recur:14,match:15,throw:16,try:17,unless:18,import:19},Gt=[Dt,it,_t,Wt,at,At,It,Rt,jt,Bt,Ct,Mt,Ut,Jt,zt,wt,Lt,Vt,Ot,qt],Xt={normalExpressions:nt,specialExpressions:Gt,allNormalExpressions:ot},Ht=Object.keys(nt),Kt=Object.keys(Yt),Zt=(e,t,n,r)=>{const o=Array.isArray(e)?e:[[s.SpecialExpression,[Yt.block,e.body]]],i=new Set;for(const e of o)Qt(e,t,n,r)?.forEach(e=>i.add(e));return i};function Qt(e,t,r,o){const i=e[0];switch(i){case s.UserDefinedSymbol:{const n=e;return null===t.lookUp(n)?new Set([n[1]]):null}case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.String:case s.Number:case s.ReservedSymbol:case s.Binding:return null;case s.NormalExpression:{const n=e,i=new Set;if(Pt(n)){const[,[e]]=n;if(xt(e)){null===t.lookUp(e)&&i.add(e[1])}}else{const[,[e]]=n;Qt(e,t,r,o)?.forEach(e=>i.add(e))}for(const e of n[1][1])Qt(e,t,r,o)?.forEach(e=>i.add(e));return i}case s.SpecialExpression:{const n=e,i=n[1][0];return(0,r.specialExpressions[i].getUndefinedSymbols)(n,t,{getUndefinedSymbols:Zt,builtin:r,evaluateNode:o})}case s.Spread:return Qt(e[1],t,r,o);default:throw new n(`Unhandled node type: ${i}`,e[2])}}const en=(1+Math.sqrt(5))/2,tn={E:Math.E,"-E":-Math.E,"ε":Math.E,"-ε":-Math.E,PI:Math.PI,"-PI":-Math.PI,"π":Math.PI,"-π":-Math.PI,PHI:en,"-PHI":-en,"φ":en,"-φ":-en,POSITIVE_INFINITY:Number.POSITIVE_INFINITY,"∞":Number.POSITIVE_INFINITY,NEGATIVE_INFINITY:Number.NEGATIVE_INFINITY,"-∞":Number.NEGATIVE_INFINITY,MAX_SAFE_INTEGER:Number.MAX_SAFE_INTEGER,MIN_SAFE_INTEGER:Number.MIN_SAFE_INTEGER,MAX_VALUE:Number.MAX_VALUE,MIN_VALUE:Number.MIN_VALUE,NaN:Number.NaN},nn={true:!0,false:!1,null:null,do:null,else:null,case:null,each:null,in:null,when:null,while:null,catch:null,function:null,as:null,then:null,end:null,_:null,...tn};const rn={NativeJsFunction:(e,t,r,o)=>{if(o.pure&&!e.nativeFn.pure)throw new n(`Cannot call impure native function '${e.name}' in pure mode`,r);try{const o=e.nativeFn.fn(...t);return o instanceof Promise?o.then(e=>ie(e),e=>{const t="string"==typeof e?e:w(e)&&"string"==typeof e.message?e.message:"<no message>";throw new n(`Native function threw: "${t}"`,r)}):ie(o)}catch(e){const t="string"==typeof e?e:w(e)&&"string"==typeof e.message?e.message:"<no message>";throw new n(`Native function threw: "${t}"`,r)}},UserDefined:(e,r,o,i,{evaluateNode:a})=>{function s(r){if(!function(e,t){const{min:n}=e;return!("number"==typeof n&&t<n)}(e.arity,r.length))throw new n(`Expected ${e.arity} arguments, got ${r.length}.`,o);const u=e.evaluatedfunction,c=u[0],l=c.filter(e=>e[0]!==ut).length,f=i.create(e.evaluatedfunction[2]),d={self:{value:e}},h=[];let p,y;for(let e=0;e<r.length;e+=1)if(e<l){const t=e;p=he(p,()=>{const e=ie(r[t]);return he(pt(c[t],e,e=>a(e,f.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})})})}else h.push(ie(r[e]));for(let e=r.length;e<l;e++){const t=e;y=he(y,()=>{const e=c[t];return he(a(e[1][1],i.create(d)),t=>he(pt(e,t,e=>a(e,i.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})}))})}return he(p,()=>he(y,()=>{const e=c.find(e=>e[0]===ut),n=void 0!==e?he(pt(e,h,e=>a(e,i.create(d))),e=>{Object.entries(e).forEach(([e,t])=>{d[e]={value:t}})}):void 0;return he(n,()=>{const e=f.create(d),n=ge(u[1],(t,n)=>a(n,e),null);return n instanceof Promise?n.catch(e=>{if(e instanceof t)return s(e.params);throw e}):n})}))}for(;;)try{return s(r)}catch(e){if(e instanceof t){r=e.params;continue}throw e}},Partial:(e,t,r,o,{executeFunction:i})=>{const a=[...e.params];if(t.length!==e.placeholders.length)throw new n(`(partial) expects ${e.placeholders.length} arguments, got ${t.length}.`,r);const s=[...t];for(const t of e.placeholders)a.splice(t,0,s.shift());return i(e.function,a,o,r)},Comp:(e,t,r,o,{executeFunction:i})=>{const{params:a}=e;if(0===a.length){if(1!==t.length)throw new n(`(comp) expects one argument, got ${y(t.length)}.`,r);return O(t[0],r)}let s=t;for(let e=a.length-1;e>=0;e--){const t=a[e];s=he(s,e=>he(i(L(t,r),e,o,r),e=>[e]))}return he(s,e=>O(e[0],r))},Constantly:e=>e.value,Juxt:(e,t,n,r,{executeFunction:o})=>pe(e.params,e=>o(L(e,n),t,r,n)),Complement:(e,t,n,r,{executeFunction:o})=>he(o(e.function,t,r,n),e=>!e),EveryPred:(e,t,n,r,{executeFunction:o})=>{const i=[];for(const a of e.params)for(const e of t)i.push(()=>o(L(a,n),[e],r,n));return ge(i,(e,t)=>!!e&&he(t(),e=>!!e),!0)},SomePred:(e,t,n,r,{executeFunction:o})=>{const i=[];for(const a of e.params)for(const e of t)i.push(()=>o(L(a,n),[e],r,n));return ge(i,(e,t)=>!!e||he(t(),e=>!!e),!1)},Fnull:(e,t,n,r,{executeFunction:o})=>{const i=t.map((t,n)=>null===t?ie(e.params[n]):t);return o(L(e.function,n),i,r,n)},Builtin:(e,t,r,o,{executeFunction:i})=>{const a=v(ot[e.normalBuiltinSymbolType],r);if(o.pure&&!1===a.pure)throw new n(`Cannot call impure function '${e.name}' in pure mode`,r);return a.evaluate(t,r,o,{executeFunction:i})},SpecialBuiltin:(e,t,r,o,{executeFunction:i})=>{const a=v(Gt[e.specialBuiltinSymbolType],r);if(a.evaluateAsNormalExpression)return a.evaluateAsNormalExpression(t,r,o,{executeFunction:i});throw new n(`Special builtin function ${e.specialBuiltinSymbolType} is not supported as normal expression.`,r)},Module:(e,t,r,o,{executeFunction:i})=>{const a=o.getModule(e.moduleName);if(!a)throw new n(`Module '${e.moduleName}' not found.`,r);const s=a.functions[e.functionName];if(!s)throw new n(`Function '${e.functionName}' not found in module '${e.moduleName}'.`,r);if(o.pure&&!1===s.pure)throw new n(`Cannot call impure function '${e.functionName}' in pure mode`,r);return Q(s.arity,t.length,r),s.evaluate(t,r,o,{executeFunction:i})}};function on(e,t){return ge(e.body,(e,n)=>an(n,t),null)}function an(e,t){switch(e[0]){case s.Number:case s.String:return function(e){return e[1]}(e);case s.NormalBuiltinSymbol:case s.SpecialBuiltinSymbol:case s.UserDefinedSymbol:return t.evaluateSymbol(e);case s.ReservedSymbol:return function(e){const t=e[1];if(!["true","false","null"].includes(t))throw new n(`Reserved symbol ${t} cannot be evaluated`,e[2]);return v(nn[t],e[2])}(e);case s.NormalExpression:{const r=function(e,t){const r=e[2];return he(function(e,t){const r=[],o=[],i=ve(e,(e,i)=>$t(e)?he(an(e[1],t),t=>{if(!Array.isArray(t))throw new n(`Spread operator requires an array, got ${y(e)}`,e[2]);r.push(...t)}):e[0]!==s.ReservedSymbol||"_"!==e[1]?he(an(e,t),e=>{r.push(e)}):void o.push(i));return he(i,()=>({params:r,placeholders:o}))}(e[1][1],t),({params:o,placeholders:a})=>{if(Pt(e)){const s=e[1][0];if(a.length>0){return he(an(s,t),e=>({[f]:!0,function:L(e,r),functionType:"Partial",params:o,placeholders:a,sourceCodeInfo:r,arity:ee(a.length)}))}if(kt(s)){const r=s[1],i=Xt.allNormalExpressions[r];if(t.pure&&!1===i.pure)throw new n(`Cannot call impure function '${i.name}' in pure mode`,e[2]);return i.evaluate(o,e[2],t,{executeFunction:sn})}{const n=t.getValue(s[1]);if(void 0!==n)return sn(L(n,r),o,t,r);throw new i(s[1],e[2])}}return he(an(e[1][0],t),e=>{const n=L(e,r);if(a.length>0){return{[f]:!0,function:n,functionType:"Partial",params:o,placeholders:a,sourceCodeInfo:r,arity:ee(a.length)}}return sn(n,o,t,r)})})}(e,t);return he(r,t=>{if("number"==typeof t&&Number.isNaN(t))throw new n("Number is NaN",e[2]);return Me(t)})}case s.SpecialExpression:return he(function(e,t){const n=e[1][0],r=v(Xt.specialExpressions[n],e[2]);return(0,r.evaluate)(e,t,{evaluateNode:an,builtin:Xt,getUndefinedSymbols:Zt})}(e,t),e=>Me(e));default:throw new n(`${c(e[0])}-node cannot be evaluated`,e[2])}}function sn(e,t,r,o){if(S(e))return rn[e.functionType](e,t,o,r,{evaluateNode:an,executeFunction:sn});if(Array.isArray(e))return function(e,t,r){if(1!==t.length)throw new n("Array as function requires one non negative integer parameter.",r);const o=t[0];return Z(o,r,{integer:!0,nonNegative:!0}),ie(e[o])}(e,t,o);if(T(e))return function(e,t,r){if(1!==t.length)throw new n("Object as function requires one string parameter.",r);const o=t[0];return J(o,r),ie(e[o])}(e,t,o);if("string"==typeof e)return function(e,