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