UNPKG

@mojir/lits

Version:

Lits is a pure functional programming language implemented in TypeScript

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