@dallaylaen/ski-interpreter 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/bin/ski.js +7 -10
- package/lib/ski-interpreter.cjs.js +74 -60
- package/lib/ski-interpreter.cjs.js.map +3 -3
- package/lib/ski-interpreter.min.js +5 -5
- package/lib/ski-interpreter.min.js.map +4 -4
- package/lib/ski-interpreter.mjs +74 -60
- package/lib/ski-interpreter.mjs.map +3 -3
- package/lib/ski-quest.min.js +5 -5
- package/lib/ski-quest.min.js.map +4 -4
- package/lib/types/expr.d.ts +33 -6
- package/lib/types/extras.d.ts +4 -10
- package/lib/types/index.d.ts +2 -5
- package/lib/types/parser.d.ts +11 -13
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";(()=>{var
|
|
1
|
+
"use strict";(()=>{var R=class{constructor(...e){let t="$|(\\s+)|"+e.map(r=>"(?:"+r+")").sort((r,n)=>n.length-r.length).join("|");this.rex=new RegExp(t,"gys")}split(e){this.rex.lastIndex=0;let t=[...e.matchAll(this.rex)],n=t.pop()?.index??0;if(n!==e.length)throw new Error("Unknown tokens at pos "+n+"/"+e.length+" starting with "+e.substring(n));return t.filter(i=>i[1]===void 0).map(i=>i[0])}},ue=new R("[-=+]","[A-Z]","\\b[a-z_][a-z_0-9]*\\b");function P(s,e){if(!e)return s;let t=new Set([...s]),r="=",n={"=":i=>{t=new Set([i]),r="+"},"+":i=>{t.add(i)},"-":i=>{t.delete(i)}};for(let i of ue.split(e))n[i]?r=i:n[r](i);return t}var L=class{constructor(e,t){this.value=e,this.decoration=t}};function v(s){return s instanceof L?[s.value??void 0,s.decoration]:[s??void 0,void 0]}function _(s){let e=t=>new L(t,e);return e.label=s,e.toString=()=>"TraverseControl::"+s,e}var O={max:1e3,maxArgs:32,maxSize:1e6},ce={"leftmost-outermost":"LO","leftmost-innermost":"LI",LO:"LO",LI:"LI"},c={descend:_("descend"),prune:_("prune"),redo:_("redo"),stop:_("stop")},w={},y=class s{annotate(e={}){if(e.fancy!==void 0&&this instanceof b&&(this.fancyName=e.fancy),e.note!==void 0&&(this.note=e.note),e.arity!==void 0&&(this.arity=e.arity),e.canonize){let t=this.infer(e);t.normal&&(this.arity=this.arity??t.arity,this.note=this.note??t.expr.format({html:!0,lambda:[""," ↦ ",""]}),delete t.steps,this.props=t)}return this}apply(...e){let t=this;for(let r of e)t=new k(t,r);return t}expand(){return this.traverse(e=>{if(e instanceof f)return e.impl.expand()})??this}traverse(e,t){typeof e=="function"&&(t=e,e={});let r=ce[e.order??"LO"];if(r===void 0)throw new Error("Unknown traversal order: "+e.order);let[n]=v(this._traverse_redo({order:r},t));return n??null}_traverse_redo(e,t){let r,n=this,i;do{i=n;let o=e.order==="LI"?n._traverse_descend(e,t)??t(n):t(n)??n._traverse_descend(e,t);[n,r]=v(o)}while(n&&r===c.redo);return!n&&i!==this&&(n=i),r?r(n):n}_traverse_descend(e,t){return null}any(e){return e(this)}fold(e,t){let[r]=v(this._fold(e,t));return r??e}_fold(e,t){return t(e,this)}foldBottomUp(e){let[t,...r]=this.unroll();return e(t,r.map(n=>n.foldBottomUp(e)))}infer(e={}){let t={},r=new Set;return this.traverse(n=>{n instanceof b&&!r.has(n)&&(t[n.name]=!0),n instanceof E&&r.add(n.arg)}),this._infer({max:e.max??O.max,maxArgs:e.maxArgs??O.maxArgs,maxSize:e.maxSize??O.maxSize,skipNames:t},0)}_infer(e,t){let r=[],n=0,i=this;e:for(let o=0;o<e.maxArgs;o++){let a=i.run({max:e.max-n,maxSize:e.maxSize});if(n+=a.steps,!a.final)break;if(fe(a.expr)){if(i=a.expr,!i.any(p=>!(p instanceof h||p instanceof k)))return ee(r,i,{steps:n});let u=i.unroll(),d=!1,x=!1,m=[];for(let p=1;p<u.length;p++){let g=u[p]._infer({maxArgs:e.maxArgs-t,max:e.max-n,maxSize:e.maxSize,skipNames:e.skipNames},t+o);if(n+=g.steps??0,!g.expr)break e;g.discard&&(d=!0),g.duplicate&&(x=!0),m.push(g.expr)}return ee(r,u[0].apply(...m),{discard:d,duplicate:x,steps:n})}for(;e.skipNames[te(t+o).name];)t++;let l=te(t+o);r.push(l),i=a.expr.apply(l)}return{normal:!1,proper:!1,steps:n}}unroll(){return[this]}*toLambda(e={}){let t=this.traverse(i=>{if(i instanceof h)return i;if(i instanceof k||i instanceof E||i instanceof f)return null;let o=i.infer({max:e.max,maxArgs:e.maxArgs});if(!o.normal)throw new Error("Failed to infer an equivalent lambda term for "+i);return o.expr})??this,r=new Set,n=0;for(;t;){let i=t.traverse({order:"LI"},o=>{if(r.has(o))return null;if(o instanceof k&&o.fun instanceof E){let a=o.infer({max:e.max,maxArgs:e.maxArgs});return n+=a.steps??0,a.normal?c.stop(a.expr):(r.add(o),null)}});yield{expr:t,steps:n},t=i}}*toSKI(e={}){let t=this.traverse(n=>n instanceof h||n instanceof k||n instanceof E||n instanceof f?null:n.infer(e).expr)??this,r=0;for(;t;){let n=t.traverse({order:"LI"},i=>{if(!(i instanceof E)||i.impl instanceof E)return null;if(i.impl===i.arg)return c.stop(w.I);if(!i.impl.any(o=>o===i.arg))return c.stop(w.K.apply(i.impl));if(!(i.impl instanceof k))throw new Error("toSKI: assert failed: lambda body is of unexpected type "+i.impl.constructor.name);return i.impl.arg===i.arg&&!i.impl.fun.any(o=>o===i.arg)?c.stop(i.impl.fun):c.stop(w.S.apply(new E(i.arg,i.impl.fun),new E(i.arg,i.impl.arg)))});yield{expr:t,steps:r,final:!n},r++,t=n}}subst(e,t){return this===e?t:null}invoke(e){return null}step(){return{expr:this,steps:0,changed:!1}}run(e={},...t){e instanceof s&&(t.unshift(e),e={});let r=t?this.apply(...t):this,n=e.steps??0,i=Math.max(e.max??O.max,1)+n,o=!1;for(;n<i;){let a=r.step();if(!a.changed){o=!0;break}if((a.expr.size??1)>(e.maxSize??O.maxSize))break;n+=a.steps,r=a.expr}if(e.throw&&!o)throw new Error("Failed to compute expression in "+i+" steps");return{final:o,steps:n,expr:r}}*walk(e={}){let t=e.max??1/0,r=0,n=this,i=!1;for(;r<t&&(n.size??1)<(e.maxSize??O.maxSize);){let o=n.step();if(o.changed||(i=!0),yield{expr:n,steps:r,final:i},i)break;r+=o.steps,n=o.expr}}equals(e){return!this.diff(e)}diff(e,t=!1){return this===e?null:e instanceof f?e.impl.diff(this,!t):t?"["+e+" != "+this+"]":"["+this+" != "+e+"]"}expect(e,t=""){if(t=t?t+": ":"",!(e instanceof s))throw new Error(t+"Expected a combinator but found "+(e?.constructor?.name??typeof e));let r=this.diff(e);if(!r)return;let n=new Error(t+r);throw n.expected=this.diag(),n.actual=e.diag(),n}toString(){return this.format()}_braced(e){return!1}_unspaced(e){return this._braced(!0)}format(e={}){let t=e.html?{brackets:["(",")"],space:" ",var:["<var>","</var>"],lambda:["","->",""],around:["",""],redex:["",""]}:{brackets:["(",")"],space:" ",var:["",""],lambda:["","->",""],around:["",""],redex:["",""]};return this.formatImpl({terse:e.terse??!0,brackets:e.brackets??t.brackets,space:e.space??t.space,var:e.var??t.var,lambda:e.lambda??t.lambda,around:e.around??t.around,redex:e.redex??t.redex,inventory:e.inventory,html:e.html??!1},0)}diag(e=""){return e+this.constructor.name+": "+this}declare(e={}){let{declaration:t=["","=","; "],...r}=e,n=I({list:[this],env:r.inventory});return n.list.map(i=>i instanceof f?t[0]+i.name+t[1]+i.impl.format({...r,inventory:n.env}):i instanceof h?t[0]+i.name+t[1]:i.format({...r,inventory:n.env})).join(t[2])}toJSON(){return this.declare()}},k=class s extends y{constructor(e,t){super(),this.arg=t,this.fun=e,this.size=(e.size??1)+(t.size??1)}_traverse_descend(e,t){let[r,n]=v(this.fun._traverse_redo(e,t));if(n===c.stop)return c.stop(r?r.apply(this.arg):null);let[i,o]=v(this.arg._traverse_redo(e,t)),a=r||i?(r??this.fun).apply(i??this.arg):null;return o===c.stop?c.stop(a):a}any(e){return e(this)||this.fun.any(e)||this.arg.any(e)}_fold(e,t){let[r=e,n="descend"]=v(t(e,this));if(n===c.prune)return r;if(n===c.stop)return c.stop(r);let[i=r,o="descend"]=v(this.fun._fold(r,t));if(o===c.stop)return c.stop(i);let[a=i,l="descend"]=v(this.arg._fold(i,t));return l===c.stop?c.stop(a):a}subst(e,t){let r=this.fun.subst(e,t),n=this.arg.subst(e,t);return r||n?(r??this.fun).apply(n??this.arg):null}step(){if(!this.final){let e=this.fun.invoke(this.arg);if(e instanceof y)return{expr:e,steps:1,changed:!0};typeof e=="function"&&(this.invoke=e);let t=this.fun.step();if(t.changed)return{expr:t.expr.apply(this.arg),steps:t.steps,changed:!0};let r=this.arg.step();if(r.changed)return{expr:this.fun.apply(r.expr),steps:r.steps,changed:!0};this.final=!0}return{expr:this,steps:0,changed:!1}}invoke(e){let t=this.fun.invoke(this.arg);return t instanceof y?t.apply(e):typeof t=="function"?(this.invoke=t,t(e)):(this.invoke=r=>null,null)}unroll(){return[...this.fun.unroll(),this.arg]}diff(e,t=!1){if(!(e instanceof s))return super.diff(e,t);let r=this.fun.diff(e.fun,t);if(r)return r+"(...)";let n=this.arg.diff(e.arg,t);return n?this.fun+"("+n+")":null}_braced(e){return!e}formatImpl(e,t){let r=this.fun.formatImpl(e,t+1),n=this.arg.formatImpl(e,0),i=t?["",""]:e.around;return e.terse&&!this.arg._braced(!1)?i[0]+r+(this.fun._unspaced(this.arg)?"":e.space)+n+i[1]:i[0]+r+e.brackets[0]+n+e.brackets[1]+i[1]}diag(e=""){return e+`App:
|
|
2
2
|
`+this.unroll().map(t=>t.diag(e+" ")).join(`
|
|
3
|
-
`)}_unspaced(e){return this.arg._braced(!1)?!0:this.arg._unspaced(e)}},
|
|
4
|
-
`+this.impl.diag(e+" ")}_braced(e){return!0}},A=class
|
|
5
|
-
`+this.impl.diag(e)}};function S(i,e,t={}){w[i]=new C(i,e,t)}S("I",i=>i);S("K",i=>e=>i);S("S",i=>e=>t=>i.apply(t,e.apply(t)));S("B",i=>e=>t=>i.apply(e.apply(t)));S("C",i=>e=>t=>i.apply(t).apply(e));S("W",i=>e=>i.apply(e).apply(e));S("+",i=>i instanceof A?new A(i.n+1):e=>t=>e.apply(i.apply(e,t)),{note:"Increase a Church numeral argument by 1, otherwise n => f => x => f(n f x)"});function fe(i){for(;i instanceof k;)i=i.fun;return i instanceof f}function ee(i,e,t){let r=new Array(i.length).fill(0),n=!0;e.traverse(a=>{if(a instanceof f){let l=i.findIndex(c=>c.name===a.name);if(l>=0){r[l]++;return}}a instanceof k||(n=!1)});let s=new Set,o=new Set;for(let a=0;a<i.length;a++)r[a]===0?s.add(a):r[a]>1&&o.add(a);for(let a=i.length;a-- >0;)e=new v(i[a],e);return{normal:!0,steps:t.steps,expr:e,arity:i.length,...s.size?{skip:s}:{},...o.size?{dup:o}:{},duplicate:!!o.size||t.duplicate||!1,discard:!!s.size||t.discard||!1,proper:n}}function te(i){return new f("abcdefgh"[i]??"x"+i)}var re={Expr:b,App:k,Named:y,FreeVar:f,Native:C,Lambda:v,Church:A,Alias:h};function I(i,e){if(i instanceof b&&(i=[i]),e)i||(i=Object.keys(e).sort().map(s=>e[s]));else{if(!i)return{list:[],env:{}};e={};for(let s of i)if(s instanceof y){if(e[s.name])throw new Error("duplicate name "+s);e[s.name]=s}}let t=[],r=new Set,n=s=>{r.has(s)||(s.fold(!1,(o,a)=>{if(a!==s&&a instanceof y&&e[a.name]===a)return n(a),u.prune(!1)}),t.push(s),r.add(s))};for(let s of i)n(s);return{list:t,env:e}}var F=class extends b{apply(...e){return e.length>0?e.shift().apply(...e):this}postParse(){throw new Error("Attempt to use empty expression () as a term")}formatImpl(e,t){return"()"}},W=class i extends F{constructor(e,t={}){if(super(),this.impl=new F,e instanceof f)this.terms=[e];else if(e instanceof i){if(!(e.impl instanceof f))throw new Error("Expected FreeVar->...->FreeVar->Expr");this.terms=[...e.terms,e.impl]}else throw new Error("Expected FreeVar or PartialLambda")}apply(e,...t){if(e===null||t.length!==0)throw new Error("bad syntax in partial lambda expr");return this.impl=this.impl.apply(e),this}postParse(){let e=this.impl;for(let t=this.terms.length;t-- >0;)e=new v(this.terms[t],e);return e}};function ne(i){return i.postParse?i.postParse():i}var he=new _("[()]","[A-Z]","[a-z_][a-z_0-9]*","\\b[0-9]+\\b","->","\\+"),z=class{constructor(e={}){if(this.annotate=!!e.annotate,this.addContext=!!e.addContext,this.known={...w},this.hasNumbers=!0,this.hasLambdas=!0,this.allow=new Set(Object.keys(this.known)),Array.isArray(e.terms))this.bulkAdd(e.terms);else if(e.terms)for(let t in e.terms)(typeof e.terms[t]!="string"||!e.terms[t].match(/^Native:/))&&this.add(t,e.terms[t]);this.hasNumbers=e.numbers??!0,this.hasLambdas=e.lambdas??!0,e.allow&&this.restrict(e.allow)}add(e,t,r){let n=this._named(e,t),s=typeof r=="string"?{note:r,canonize:!1}:r??{};n._setup({canonize:this.annotate,...s});let o=this.known[n.name];return o instanceof h&&o.makeInline(),this.known[n.name]=n,this.allow.add(n.name),this}_named(e,t){if(e instanceof y)return Z(e.name,e);if(typeof e!="string")throw new Error("add(): term must be an Alias or a string");if(t===void 0)throw new Error("add(): impl must be provided when term is a string");if(typeof t=="string")return Z(e,this.parse(t));if(t instanceof b)return Z(e,t);if(typeof t=="function")return new C(e,t);throw new Error("add(): impl must be an Expr, a string, or a function with a signature Expr => ... => Expr")}maybeAdd(e,t){return this.known[e]?this.allow.add(e):this.add(e,t),this}bulkAdd(e){for(let t of e){let r=t.match(/^([A-Z]|[a-z][a-z_0-9]*)\s*=\s*(.*)$/s);if(!r)throw new Error("bulkAdd: invalid declaration: "+t);r[2]===""?this.remove(r[1]):this.add(r[1],this.parse(r[2]))}return this}restrict(e){return this.allow=P(this.allow,e),this}showRestrict(e="+"){let t=[],r=!0;for(let n of[...P(this.allow,e)].sort()){let s=!!n.match(/^[A-Z]$/);t.length&&!(r&&s)&&t.push(" "),t.push(n),r=s}return t.join("")}remove(e){let t=this.known[e];return t instanceof h&&t.makeInline(),delete this.known[e],this.allow.delete(e),this}getTerms(){let e={};for(let t of Object.keys(this.known))this.allow.has(t)&&(e[t]=this.known[t]);return e}declare(){let e={};for(let[a,l]of Object.entries(this.getTerms()))l instanceof h&&(e[a]=l);let t={},r=1;for(let a in w){if(!(e[a]instanceof h))continue;for(;"tmp"+r in e;)r++;let l=new h("tmp"+r,e[a]);t[l.name]=e[a],e[l.name]=l,delete e[a]}let n=I(void 0,e).list,s=new Map;if(Object.keys(t).length){let a=l=>l.traverse(c=>{if(!(c instanceof h))return null;let m=s.get(c);return m||new h(c.name,a(c.impl))})??l;for(let l=0;l<n.length;l++)n[l]=a(n[l]),s.set(t[n[l].name],n[l]),e[n[l].name]=n[l]}let o=n.map(a=>t[a.name]?a.name+"="+t[a.name].name+"="+a.impl.format({inventory:e}):a.name+"="+a.impl.format({inventory:e}));for(let[a,l]of s)o.push(a.name+"="+l,l+"=");return o}parse(e,t={}){if(typeof e!="string")throw new Error("parse: source must be a string, got "+typeof e);let r=e.replace(/\/\/[^\n]*$/gm," ").replace(/\/\*.*?\*\//gs," ").trim().split(/\s*;[\s;]*/).filter(o=>o.match(/\S/)),n={...t.env},s=new F;for(let o of r){s instanceof h&&s.makeInline();let a=o.match(/^([A-Z]|[a-z][a-z_0-9]*)\s*=(.*)$/s);if(a&&a[2]===""?s=new f(a[1],t.scope??f.global):s=this.parseLine(o,n,t),a){if(n[a[1]]!==void 0)throw new Error("Attempt to redefine a known term: "+a[1]);n[a[1]]=s}}return this.addContext&&(s instanceof y&&(s=new h(s.name,s,{inline:!0})),s.context={env:{...this.getTerms(),...n},scope:t.scope,src:e,parser:this}),s}parseLine(e,t={},r={}){let n=e.match(/^\s*([A-Z]|[a-z][a-z_0-9]*)\s*=\s*(.*)$/s);if(n)return new h(n[1],this.parseLine(n[2],t,r));let s={numbers:r.numbers??this.hasNumbers,lambdas:r.lambdas??this.hasLambdas,allow:P(this.allow,r.allow)};s.numbers?s.allow.add("+"):s.allow.delete("+");let o=he.split(e),a=new F,l=[a],c=r.scope||f.global;for(let m of o)if(m==="(")l.push(a);else if(m===")"){if(l.length<2)throw new Error("unbalanced input: extra closing parenthesis"+e);let x=ne(l.pop()),d=l.pop();l.push(d.apply(x))}else if(m==="->"){if(!s.lambdas)throw new Error("Lambdas not supported, allow them explicitly");l.push(new W(l.pop()))}else if(m.match(/^[0-9]+$/)){if(!s.numbers)throw new Error("Church numbers not supported, allow them explicitly");let x=l.pop();l.push(x.apply(new A(Number.parseInt(m))))}else{let x=l.pop();if(!t[m]&&this.known[m]&&!s.allow.has(m))throw new Error("Term '"+m+"' is not in the restricted set "+[...s.allow].sort().join(" "));let d=t[m]??this.known[m]??(t[m]=new f(m,c));l.push(x.apply(d))}if(l.length!==1)throw new Error("unbalanced input: missing "+(l.length-1)+" closing parenthesis:"+e);return ne(l.pop())}toJSON(){return{version:"1.1.1",allow:this.showRestrict("+"),numbers:this.hasNumbers,lambdas:this.hasLambdas,annotate:this.annotate,terms:this.declare()}}};function Z(i,e){for(;e instanceof h&&e.name===i;)e=e.impl;return e instanceof y&&e.name===i?e:new h(i,e,{canonize:!0})}var T=class{constructor(e){let{input:t,cases:r,allow:n,numbers:s,lambdas:o,engine:a,engineFull:l,...c}=e,m=e.env??[];this.engineFull=l??new z,this.engine=a??this.engineFull,this.restrict={allow:n,numbers:s??!1,lambdas:o??!1},this.env={};let x={};for(let d of m??[]){let p=this.engineFull.parse(d,{env:x,scope:this});if(p instanceof h)this.env[p.name]=new h(p.name,p.impl,{canonize:!1});else if(p instanceof f)this.env[p.name]=p;else throw new Error("Unsupported given variable type: "+d)}this.input=[];for(let d of Array.isArray(t)?t:[t])this.addInput(d);if(!this.input.length)throw new Error("Quest needs at least one input placeholder");this.envFull={...this.env,...x};for(let d of this.input){if(d.name in this.envFull)throw new Error("input placeholder name is duplicated or clashes with env: "+d.name);this.envFull[d.name]=d.placeholder}this.cases=[],this.name=e.name,this.intro=ie(e.intro),this.id=e.id,this.meta=c;for(let d of r??[])this.add(...d)}allowed(){let e=this.restrict.allow??"",t=Object.keys(this.env).sort();return e?this.engine.showRestrict(e+"+"+t.join(" ")):t.map(r=>"+"+r).join(" ")}addInput(e){if(typeof e!="object"&&(e={name:e}),typeof e.name!="string")throw new Error("quest 'input' field must be a string or a {name: string, ...} object");let t=e;t.placeholder=new f(e.name),this.input.push(t)}add(e,...t){let r;typeof e=="string"?(t.unshift(e),r={}):r={...e},r.engine=r.engine??this.engineFull,r.env=r.env??this.envFull;let n=this.input.map(s=>s.placeholder);return this.cases.push(r.caps?new B(n,r,t):new q(n,r,t)),this}prepare(...e){if(e.length!==this.input.length)throw new Error("Solutions provided "+e.length+" terms where "+this.input.length+" are expected");let t=0,r=[],n={...this.env};for(let s=0;s<e.length;s++){let o=this.input[s],a=this.engine.parse(e[s],{env:n,allow:o.allow??this.restrict.allow,numbers:o.numbers??this.restrict.numbers,lambdas:o.lambdas??this.restrict.lambdas}),l={...this.engine.getTerms(),...n};t+=a.fold(0,(m,x)=>{if(x instanceof y&&l[x.name]===x)return u.prune(m+1)});let c=a instanceof f?a:new h(o.fancy??o.name,a,{canonize:!1});n[o.name]=c,r.push(c)}return{prepared:r,weight:t}}check(...e){try{let{prepared:t,weight:r}=this.prepare(...e),n=this.cases.map(a=>a.check(...t)),s=n.reduce((a,l)=>a&&l.pass,!0),o=n.reduce((a,l)=>a+l.steps,0);return{expr:t[0],input:t,pass:s,steps:o,details:n,weight:r}}catch(t){return{pass:!1,details:[],exception:t,steps:0,input:e}}}verify(e){let t=this.verifyMeta(e);if(e.solutions){let r=this.verifySolutions(e.solutions);r&&(t.solutions=r)}return e.seen&&(this.id||(t.seen="No id in quest "+(this.name??"(unnamed)")),e.seen.has(this.id)&&(t.seen="Duplicate quest id "+this.id),e.seen.add(this.id)),Object.keys(t).length?t:null}verifySolutions(e){if(typeof e=="object"&&!Array.isArray(e.accepted)&&!Array.isArray(e.rejected)&&(!this.id||!e[this.id]))return null;let t=this.id!==void 0?e[this.id]:void 0,{accepted:r=[],rejected:n=[]}=t??e,s={shouldPass:[],shouldFail:[]};for(let o of r){let a=this.check(...o);a.pass||s.shouldPass.push({input:o,result:a})}for(let o of n){let a=this.check(...o);a.pass&&s.shouldFail.push({input:o,result:a})}return s.shouldFail.length+s.shouldPass.length?s:null}verifyMeta(e={}){let t={};for(let r of["name","intro"]){let n=ae(this[r]);n&&(t[r]=n)}if(e.date){let r=new Date(this.meta?.created_at);isNaN(r.getTime())?t.date="invalid date format: "+this.meta?.created_at:(r.getTime()<new Date("2024-07-15").getTime()||r.getTime()>new Date().getTime())&&(t.date="date out of range: "+this.meta?.created_at)}return t}show(){return[...this.cases]}},V=class{constructor(e,t){this.max=t.max??1e3,this.note=t.note,this.env={...t.env??{}},this.input=e,this.engine=t.engine}parse(e){return new M(this.engine.parse(e,{env:this.env,scope:this}),this.input)}},q=class extends V{constructor(e,t,r){if(r.length!==2)throw new Error("Case accepts exactly 2 strings");super(e,t),this.canonize=t.canonize,[this.e1,this.e2]=r.map(n=>this.parse(n))}check(...e){let t=this.e1.apply(e),r=t.run({max:this.max}),s=this.e2.apply(e).run({max:this.max}),o=null;return!r.final||!s.final?o="failed to reach normal form in "+this.max+" steps":o=this.canonize?se(r.expr,this.canonize).diff(se(s.expr,this.canonize)):r.expr.diff(s.expr),{pass:!o,reason:o??void 0,steps:r.steps,start:t,found:r.expr,expected:s.expr,note:this.note,args:e,case:this}}},me={normal:!0,proper:!0,discard:!0,duplicate:!0,linear:!0,affine:!0,arity:!0},B=class extends V{constructor(e,t,r){if(super(e,t),r.length>1)throw new Error("PropertyCase accepts exactly 1 string");if(!t.caps||typeof t.caps!="object"||!Object.keys(t.caps).length)throw new Error("PropertyCase requires a caps object with at least one capability");let n=Object.keys(t.caps).filter(s=>!me[s]);if(n.length)throw new Error("PropertyCase: don't know how to test these capabilities: "+n.join(", "));this.expr=this.parse(r[0]),this.caps={...t.caps},this.caps.linear&&(delete this.caps.linear,this.caps.duplicate=!1,this.caps.discard=!1,this.caps.normal=!0),this.caps.affine&&(delete this.caps.affine,this.isAffine=!0)}check(...e){let t=this.expr.apply(e),r=t.run({max:this.max}),n=r.expr.infer({max:this.max}),s=[];for(let o in this.caps)n[o]!==this.caps[o]&&s.push("expected property "+o+" to be "+this.caps[o]+", found "+n[o]);if(this.isAffine){let o=t.fold(void 0,(a,l)=>{if(l instanceof k||l instanceof f)return;let c=l.infer({max:this.max});if(!c.expr||c.duplicate)return u.stop({expr:l,props:c});if(l instanceof h&&e.indexOf(l)<0)return u.prune(a)});o&&s.push("found unexpected subterm "+o.expr+(o.props.expr?" that duplicates arguments":" without a normal form"))}return{pass:!s.length,reason:s.length?s.join(`
|
|
6
|
-
`):void 0,steps:r.steps,start:t,found:r.expr,case:this,note:this.note,args:e}}},M=class{constructor(e,t){this.expr=e,this.env=t}apply(e){if(e.length!==this.env.length)throw new Error("Subst: expected "+this.env.length+" terms, got "+e.length);let t=this.expr;for(let r=0;r<this.env.length;r++)t=t.subst(this.env[r],e[r])??t;return t}},G=class{constructor(e){this.name=e.name,this.intro=ie(e.intro),this.id=e.id,e.content&&(this.content=e.content.map(t=>t instanceof T?t:new T(t)))}verify(e){let t={},r=
|
|
3
|
+
`)}_unspaced(e){return this.arg._braced(!1)?!0:this.arg._unspaced(e)}},b=class s extends y{constructor(e){if(super(),typeof e!="string"||e.length===0)throw new Error("Attempt to create a named term with improper name");this.name=e}_unspaced(e){return!!(e instanceof s&&(this.name.match(/^[A-Z+]$/)&&e.name.match(/^[a-z+]/i)||this.name.match(/^[a-z+]/i)&&e.name.match(/^[A-Z+]$/)))}formatImpl(e,t){let r=e.html?this.fancyName??this.name:this.name;return this.arity!==void 0&&this.arity>0&&this.arity<=t?e.redex[0]+r+e.redex[1]:r}},pe=0,h=class s extends b{constructor(e,t){super(e),this.id=++pe,this.scope=t===void 0?this:t}diff(e,t=!1){if(!(e instanceof s))return super.diff(e,t);if(this.name===e.name&&this.scope===e.scope)return null;let r=this.name+"["+this.id+"]",n=e.name+"["+e.id+"]";return t?"["+n+" != "+r+"]":"["+r+" != "+n+"]"}subst(e,t){return e instanceof s&&e.name===this.name&&e.scope===this.scope?t:null}formatImpl(e,t){let r=e.html?this.fancyName??this.name:this.name;return e.var[0]+r+e.var[1]}diag(e=""){return`${e}FreeVar: ${this.name}[${this.id}]`}static{this.global=["global"]}},C=class extends b{constructor(e,t,r={}){super(e),this.invoke=t,this.annotate({canonize:!0,...r})}},E=class s extends y{constructor(e,t){if(super(),!(e instanceof h))throw new Error("Lambda argument must be a FreeVar");let r=new h(e.name,this);this.arg=r,this.impl=t.subst(e,r)??t,this.arity=1,this.size=(t.size??1)+1}invoke(e){return this.impl.subst(this.arg,e)??this.impl}_traverse_descend(e,t){let[r,n]=v(this.impl._traverse_redo(e,t)),i=r?new s(this.arg,r):null;return n===c.stop?c.stop(i):i}any(e){return e(this)||this.impl.any(e)}_fold(e,t){let[r=e,n="descend"]=v(t(e,this));if(n===c.prune)return r;if(n===c.stop)return c.stop(r);let[i,o]=v(this.impl._fold(r,t));return o===c.stop?c.stop(i):i??r}subst(e,t){if(e===this.arg)return null;let r=this.impl.subst(e,t);return r?new s(this.arg,r):null}diff(e,t=!1){if(!(e instanceof s))return super.diff(e,t);let r=new h("t"),n=this.invoke(r).diff(e.invoke(r),t);return n?"(t->"+n+")":null}formatImpl(e,t){return(t>0?e.brackets[0]:"")+e.lambda[0]+this.arg.formatImpl(e,0)+e.lambda[1]+this.impl.formatImpl(e,0)+e.lambda[2]+(t>0?e.brackets[1]:"")}diag(e=""){return`${e}Lambda (${this.arg.name}[${this.arg.id}]):
|
|
4
|
+
`+this.impl.diag(e+" ")}_braced(e){return!0}},A=class s extends y{constructor(e){if(e=Number.parseInt(String(e)),!(e>=0))throw new Error("Church number must be a non-negative integer");super(),this.invoke=t=>r=>{let n=r;for(let i=e;i-- >0;)n=t.apply(n);return n},this.n=e,this.arity=2}diff(e,t=!1){return e instanceof s?this.n===e.n?null:t?"["+e.n+" != "+this.n+"]":"["+this.n+" != "+e.n+"]":super.diff(e,t)}_unspaced(e){return!1}formatImpl(e,t){return t>=2?e.redex[0]+this.n+e.redex[1]:this.n+""}};function U(s){return s<=1?e=>t=>e.apply(t):e=>t=>U(s-1)(e.apply(t))}var f=class extends b{constructor(e,t,r={}){if(super(e),!(t instanceof y))throw new Error("Attempt to create an alias for a non-expression: "+t);this.impl=t,this.annotate(r),this.invoke=U(r.inline?0:this.arity??0)(t),this.size=t.size,r.inline&&(this.inline=!0)}makeInline(){return this.invoke=U(0)(this.impl),this.inline=!0,this}_traverse_descend(e,t){return this.impl._traverse_redo(e,t)}any(e){return e(this)||this.impl.any(e)}_fold(e,t){let[r=e,n]=v(t(e,this));if(n===c.prune)return r;if(n===c.stop)return c.stop(r);let[i,o]=v(this.impl._fold(r,t));return o===c.stop?c.stop(i):i??r}subst(e,t){return this===e?t:this.impl.subst(e,t)}step(){return(this.arity??0)>0?{expr:this,steps:0,changed:!1}:{expr:this.impl,steps:0,changed:!0}}diff(e,t=!1){return this===e?null:e.diff(this.impl,!t)}_braced(e){return this.inline?this.impl._braced(e):!1}formatImpl(e,t){return(e.inventory?e.inventory[this.name]!==this:this.inline)?this.impl.formatImpl(e,t):super.formatImpl(e,t)}diag(e=""){return`${e}Alias (${this.name}): \\
|
|
5
|
+
`+this.impl.diag(e)}};function S(s,e,t={}){w[s]=new C(s,e,t)}S("I",s=>s);S("K",s=>e=>s);S("S",s=>e=>t=>s.apply(t,e.apply(t)));S("B",s=>e=>t=>s.apply(e.apply(t)));S("C",s=>e=>t=>s.apply(t).apply(e));S("W",s=>e=>s.apply(e).apply(e));S("+",s=>s instanceof A?new A(s.n+1):e=>t=>e.apply(s.apply(e,t)),{note:"Increase a Church numeral argument by 1, otherwise n => f => x => f(n f x)"});function fe(s){for(;s instanceof k;)s=s.fun;return s instanceof h}function ee(s,e,t){let r=new Array(s.length).fill(0),n=!0;e.traverse(a=>{if(a instanceof h){let l=s.findIndex(u=>u.name===a.name);if(l>=0){r[l]++;return}}a instanceof k||(n=!1)});let i=new Set,o=new Set;for(let a=0;a<s.length;a++)r[a]===0?i.add(a):r[a]>1&&o.add(a);for(let a=s.length;a-- >0;)e=new E(s[a],e);return{normal:!0,steps:t.steps,expr:e,arity:s.length,...i.size?{skip:i}:{},...o.size?{dup:o}:{},duplicate:!!o.size||t.duplicate||!1,discard:!!i.size||t.discard||!1,proper:n}}function te(s){return new h("abcdefgh"[s]??"x"+s)}function I(s){if(typeof s!="object"||s===null||Array.isArray(s)||s instanceof y)throw new Error("positional arguments to toposort are deprecated, use { list: ..., env: ... } instead");let e=s.allow?{...s.allow}:null,t={...s.env??{}},r=s.list instanceof y?[s.list]:s.list??[];if(e)for(let a of r)a instanceof b&&(e[a.name]=a);for(let a of r)a instanceof b&&t[a.name]===a&&delete t[a.name];let n=[],i=new Set(Object.values(t)),o=a=>{i.has(a)||(a.fold(void 0,(l,u)=>{if(u instanceof b&&!(e&&e[u.name]!==u)&&!(!e&&u instanceof f&&u.inline)&&u!==a)return o(u),c.prune()}),n.push(a),i.add(a),a instanceof b&&(t[a.name]||=a))};for(let a of r)o(a);return{list:n,env:t}}var re={Expr:y,App:k,Named:b,FreeVar:h,Native:C,Lambda:E,Church:A,Alias:f};var F=class extends y{apply(...e){return e.length>0?e.shift().apply(...e):this}postParse(){throw new Error("Attempt to use empty expression () as a term")}formatImpl(e,t){return"()"}},W=class s extends F{constructor(e,t={}){if(super(),this.impl=new F,e instanceof h)this.terms=[e];else if(e instanceof s){if(!(e.impl instanceof h))throw new Error("Expected FreeVar->...->FreeVar->Expr");this.terms=[...e.terms,e.impl]}else throw new Error("Expected FreeVar or PartialLambda")}apply(e,...t){if(e===null||t.length!==0)throw new Error("bad syntax in partial lambda expr");return this.impl=this.impl.apply(e),this}postParse(){let e=this.impl;for(let t=this.terms.length;t-- >0;)e=new E(this.terms[t],e);return e}};function ne(s){return s.postParse?s.postParse():s}var he=new R("[()]","[A-Z]","[a-z_][a-z_0-9]*","\\b[0-9]+\\b","->","\\+"),z=class{constructor(e={}){if(this.annotate=!!e.annotate,this.addContext=!!e.addContext,this.known={...w},this.hasNumbers=!0,this.hasLambdas=!0,this.allow=new Set(Object.keys(this.known)),Array.isArray(e.terms))this.bulkAdd(e.terms);else if(e.terms)for(let t in e.terms)(typeof e.terms[t]!="string"||!e.terms[t].match(/^Native:/))&&this.add(t,e.terms[t]);this.hasNumbers=e.numbers??!0,this.hasLambdas=e.lambdas??!0,e.allow&&this.restrict(e.allow)}add(e,t,r){let n=this._named(e,t),i=typeof r=="string"?{note:r,canonize:!1}:r??{};n.annotate({canonize:this.annotate,...i});let o=this.known[n.name];return o instanceof f&&o.makeInline(),this.known[n.name]=n,this.allow.add(n.name),this}_named(e,t){if(e instanceof b)return Z(e.name,e);if(typeof e!="string")throw new Error("add(): term must be an Alias or a string");if(t===void 0)throw new Error("add(): impl must be provided when term is a string");if(typeof t=="string")return Z(e,this.parse(t));if(t instanceof y)return Z(e,t);if(typeof t=="function")return new C(e,t);throw new Error("add(): impl must be an Expr, a string, or a function with a signature Expr => ... => Expr")}maybeAdd(e,t){return this.known[e]?this.allow.add(e):this.add(e,t),this}bulkAdd(e){for(let t of e){let r=t.match(/^([A-Z]|[a-z][a-z_0-9]*)\s*=\s*(.*)$/s);if(!r)throw new Error("bulkAdd: invalid declaration: "+t);r[2]===""?this.remove(r[1]):this.add(r[1],this.parse(r[2]))}return this}restrict(e){return this.allow=P(this.allow,e),this}showRestrict(e="+"){let t=[],r=!0;for(let n of[...P(this.allow,e)].sort()){let i=!!n.match(/^[A-Z]$/);t.length&&!(r&&i)&&t.push(" "),t.push(n),r=i}return t.join("")}remove(e){let t=this.known[e];return t instanceof f&&t.makeInline(),delete this.known[e],this.allow.delete(e),this}getTerms(){let e={};for(let t of Object.keys(this.known))this.allow.has(t)&&(e[t]=this.known[t]);return e}declare(){let e={};for(let[a,l]of Object.entries(this.getTerms()))l instanceof f&&(e[a]=l);let t={},r=1;for(let a in w){if(!(e[a]instanceof f))continue;for(;"tmp"+r in e;)r++;let l=new f("tmp"+r,e[a]);t[l.name]=e[a],e[l.name]=l,delete e[a]}let n=I({list:Object.values(e),allow:{}}).list,i=new Map;if(Object.keys(t).length){let a=l=>l.traverse(u=>{if(!(u instanceof f))return null;let d=i.get(u);return d||new f(u.name,a(u.impl))})??l;for(let l=0;l<n.length;l++)n[l]=a(n[l]),i.set(t[n[l].name],n[l]),e[n[l].name]=n[l]}let o=n.map(a=>t[a.name]?a.name+"="+t[a.name].name+"="+a.impl.format({inventory:e}):a.name+"="+a.impl.format({inventory:e}));for(let[a,l]of i)o.push(a.name+"="+l,l+"=");return o}parse(e,t={}){if(typeof e!="string")throw new Error("parse: source must be a string, got "+typeof e);let r=e.replace(/\/\/[^\n]*$/gm," ").replace(/\/\*.*?\*\//gs," ").trim().split(/\s*;[\s;]*/).filter(o=>o.match(/\S/)),n={...t.env},i=new F;for(let o of r){let[a,l,u]=o.match(/^([A-Z]|[a-z][a-z_0-9]*)\s*=(.*)$/s)||[];l!==void 0&&(n[l]instanceof f&&n[l]!==t.env?.[l]&&n[l].makeInline(),delete n[l]),u===""?i=new h(l,t.scope??h.global):i=this.parseLine(o,n,t),l&&(n[l]=i)}return this.addContext&&(i instanceof b&&(i=new f(i.name,i,{inline:!0})),i.context={env:{...this.getTerms(),...n},scope:t.scope,src:e,parser:this}),i}parseLine(e,t={},r={}){let n=e.match(/^\s*([A-Z]|[a-z][a-z_0-9]*)\s*=\s*(.*)$/s);if(n)return new f(n[1],this.parseLine(n[2],t,r),{canonize:r.canonize});let i={numbers:r.numbers??this.hasNumbers,lambdas:r.lambdas??this.hasLambdas,allow:P(this.allow,r.allow)};i.numbers?i.allow.add("+"):i.allow.delete("+");let o=he.split(e),a=new F,l=[a],u=r.scope||h.global;for(let d of o)if(d==="(")l.push(a);else if(d===")"){if(l.length<2)throw new Error("unbalanced input: extra closing parenthesis"+e);let x=ne(l.pop()),m=l.pop();l.push(m.apply(x))}else if(d==="->"){if(!i.lambdas)throw new Error("Lambdas not supported, allow them explicitly");l.push(new W(l.pop()))}else if(d.match(/^[0-9]+$/)){if(!i.numbers)throw new Error("Church numbers not supported, allow them explicitly");let x=l.pop();l.push(x.apply(new A(Number.parseInt(d))))}else{let x=l.pop();if(!t[d]&&this.known[d]&&!i.allow.has(d))throw new Error("Term '"+d+"' is not in the restricted set "+[...i.allow].sort().join(" "));let m=t[d]??this.known[d]??(t[d]=new h(d,u));l.push(x.apply(m))}if(l.length!==1)throw new Error("unbalanced input: missing "+(l.length-1)+" closing parenthesis:"+e);return ne(l.pop())}toJSON(){return{version:"1.1.1",allow:this.showRestrict("+"),numbers:this.hasNumbers,lambdas:this.hasLambdas,annotate:this.annotate,terms:this.declare()}}};function Z(s,e){for(;e instanceof f&&e.name===s;)e=e.impl;return e instanceof b&&e.name===s?e:new f(s,e,{canonize:!0})}var T=class{constructor(e){let{input:t,cases:r,allow:n,numbers:i,lambdas:o,engine:a,engineFull:l,...u}=e,d=e.env??[];this.engineFull=l??new z,this.engine=a??this.engineFull,this.restrict={allow:n,numbers:i??!1,lambdas:o??!1},this.env={};let x={};for(let m of d??[]){let p=this.engineFull.parse(m,{env:x,scope:this});if(p instanceof f)this.env[p.name]=new f(p.name,p.impl,{canonize:!1});else if(p instanceof h)this.env[p.name]=p;else throw new Error("Unsupported given variable type: "+m)}this.input=[];for(let m of Array.isArray(t)?t:[t])this.addInput(m);if(!this.input.length)throw new Error("Quest needs at least one input placeholder");this.envFull={...this.env,...x};for(let m of this.input){if(m.name in this.envFull)throw new Error("input placeholder name is duplicated or clashes with env: "+m.name);this.envFull[m.name]=m.placeholder}this.cases=[],this.name=e.name,this.intro=ie(e.intro),this.id=e.id,this.meta=u;for(let m of r??[])this.add(...m)}allowed(){let e=this.restrict.allow??"",t=Object.keys(this.env).sort();return e?this.engine.showRestrict(e+"+"+t.join(" ")):t.map(r=>"+"+r).join(" ")}addInput(e){if(typeof e!="object"&&(e={name:e}),typeof e.name!="string")throw new Error("quest 'input' field must be a string or a {name: string, ...} object");let t=e;t.placeholder=new h(e.name),this.input.push(t)}add(e,...t){let r;typeof e=="string"?(t.unshift(e),r={}):r={...e},r.engine=r.engine??this.engineFull,r.env=r.env??this.envFull;let n=this.input.map(i=>i.placeholder);return this.cases.push(r.caps?new B(n,r,t):new q(n,r,t)),this}prepare(...e){if(e.length!==this.input.length)throw new Error("Solutions provided "+e.length+" terms where "+this.input.length+" are expected");let t=0,r=[],n={...this.env};for(let i=0;i<e.length;i++){let o=this.input[i],a=this.engine.parse(e[i],{env:n,allow:o.allow??this.restrict.allow,numbers:o.numbers??this.restrict.numbers,lambdas:o.lambdas??this.restrict.lambdas}),l={...this.engine.getTerms(),...n};t+=a.fold(0,(d,x)=>{if(x instanceof b&&l[x.name]===x)return c.prune(d+1)});let u=a instanceof h?a:new f(o.fancy??o.name,a,{canonize:!1});n[o.name]=u,r.push(u)}return{prepared:r,weight:t}}check(...e){try{let{prepared:t,weight:r}=this.prepare(...e),n=this.cases.map(a=>a.check(...t)),i=n.reduce((a,l)=>a&&l.pass,!0),o=n.reduce((a,l)=>a+l.steps,0);return{expr:t[0],input:t,pass:i,steps:o,details:n,weight:r}}catch(t){return{pass:!1,details:[],exception:t,steps:0,input:e}}}verify(e){let t=this.verifyMeta(e);if(e.solutions){let r=this.verifySolutions(e.solutions);r&&(t.solutions=r)}return e.seen&&(this.id||(t.seen="No id in quest "+(this.name??"(unnamed)")),e.seen.has(this.id)&&(t.seen="Duplicate quest id "+this.id),e.seen.add(this.id)),Object.keys(t).length?t:null}verifySolutions(e){if(typeof e=="object"&&!Array.isArray(e.accepted)&&!Array.isArray(e.rejected)&&(!this.id||!e[this.id]))return null;let t=this.id!==void 0?e[this.id]:void 0,{accepted:r=[],rejected:n=[]}=t??e,i={shouldPass:[],shouldFail:[]};for(let o of r){let a=this.check(...o);a.pass||i.shouldPass.push({input:o,result:a})}for(let o of n){let a=this.check(...o);a.pass&&i.shouldFail.push({input:o,result:a})}return i.shouldFail.length+i.shouldPass.length?i:null}verifyMeta(e={}){let t={};for(let r of["name","intro"]){let n=ae(this[r]);n&&(t[r]=n)}if(e.date){let r=new Date(this.meta?.created_at);isNaN(r.getTime())?t.date="invalid date format: "+this.meta?.created_at:(r.getTime()<new Date("2024-07-15").getTime()||r.getTime()>new Date().getTime())&&(t.date="date out of range: "+this.meta?.created_at)}return t}show(){return[...this.cases]}},V=class{constructor(e,t){this.max=t.max??1e3,this.note=t.note,this.env={...t.env??{}},this.input=e,this.engine=t.engine}parse(e){return new M(this.engine.parse(e,{env:this.env,scope:this}),this.input)}},q=class extends V{constructor(e,t,r){if(r.length!==2)throw new Error("Case accepts exactly 2 strings");super(e,t),this.canonize=t.canonize,[this.e1,this.e2]=r.map(n=>this.parse(n))}check(...e){let t=this.e1.apply(e),r=t.run({max:this.max}),i=this.e2.apply(e).run({max:this.max}),o=null;return!r.final||!i.final?o="failed to reach normal form in "+this.max+" steps":o=this.canonize?se(r.expr,this.canonize).diff(se(i.expr,this.canonize)):r.expr.diff(i.expr),{pass:!o,reason:o??void 0,steps:r.steps,start:t,found:r.expr,expected:i.expr,note:this.note,args:e,case:this}}},de={normal:!0,proper:!0,discard:!0,duplicate:!0,linear:!0,affine:!0,arity:!0},B=class extends V{constructor(e,t,r){if(super(e,t),r.length>1)throw new Error("PropertyCase accepts exactly 1 string");if(!t.caps||typeof t.caps!="object"||!Object.keys(t.caps).length)throw new Error("PropertyCase requires a caps object with at least one capability");let n=Object.keys(t.caps).filter(i=>!de[i]);if(n.length)throw new Error("PropertyCase: don't know how to test these capabilities: "+n.join(", "));this.expr=this.parse(r[0]),this.caps={...t.caps},this.caps.linear&&(delete this.caps.linear,this.caps.duplicate=!1,this.caps.discard=!1,this.caps.normal=!0),this.caps.affine&&(delete this.caps.affine,this.isAffine=!0)}check(...e){let t=this.expr.apply(e),r=t.run({max:this.max}),n=r.expr.infer({max:this.max}),i=[];for(let o in this.caps)n[o]!==this.caps[o]&&i.push("expected property "+o+" to be "+this.caps[o]+", found "+n[o]);if(this.isAffine){let o=t.fold(void 0,(a,l)=>{if(l instanceof k||l instanceof h)return;let u=l.infer({max:this.max});if(!u.expr||u.duplicate)return c.stop({expr:l,props:u});if(l instanceof f&&e.indexOf(l)<0)return c.prune(a)});o&&i.push("found unexpected subterm "+o.expr+(o.props.expr?" that duplicates arguments":" without a normal form"))}return{pass:!i.length,reason:i.length?i.join(`
|
|
6
|
+
`):void 0,steps:r.steps,start:t,found:r.expr,case:this,note:this.note,args:e}}},M=class{constructor(e,t){this.expr=e,this.env=t}apply(e){if(e.length!==this.env.length)throw new Error("Subst: expected "+this.env.length+" terms, got "+e.length);let t=this.expr;for(let r=0;r<this.env.length;r++)t=t.subst(this.env[r],e[r])??t;return t}},G=class{constructor(e){this.name=e.name,this.intro=ie(e.intro),this.id=e.id,e.content&&(this.content=e.content.map(t=>t instanceof T?t:new T(t)))}verify(e){let t={},r=me(this.id,e.seen);r&&(t.id=r);for(let n of["name","intro"]){let i=ae(this[n]);i&&(t[n]=i)}return t.content=this.content.map(n=>n.verify(e)),t}};function ie(s){return s===void 0||typeof s=="string"?s:Array.isArray(s)?s.join(" "):""+s}function me(s,e){if(s===void 0)return"missing";if(typeof s!="string"&&typeof s!="number")return"is a "+typeof s;if(e){if(e.has(s))return"duplicate id "+s;e.add(s)}}function ae(s){if(s===void 0)return"missing";if(typeof s!="string")return"not a string but "+typeof s;let e=[],t=/<\/?([a-z]+)(?:\s[^>]*)?>/gi,r;for(;(r=t.exec(s))!==null;){let[n,i]=r;if(n.startsWith("</")){if(e.length===0||e.pop()!==i)return`Unmatched closing tag: </${i}>`}else e.push(i)}return e.length>0?`Unclosed tags: ${e.join(", ")}`:null}T.Group=G;T.Case=V;function se(s,e={}){return s.traverse({},t=>t.infer(e).expr)??s}function ge(s,e,t){let{depth:r=16,infer:n=!0,progressInterval:i=1e3}=e,o=n&&!e.noskip,a=[[]],l=0,u=0,d={},x=p=>{l++;let g=n?p.infer({max:e.max,maxArgs:e.maxArgs}):null;if(o&&g&&g.expr){let j=String(g.expr);if(d[j])return{res:-1,props:g};d[j]=!0}return u++,{res:t(p,g),props:g}};for(let p of s){let{res:g=0}=x(p);if(g>0)return{expr:p,total:l,probed:u,gen:1};if(g<0)continue;a[0].push(p)}let m=0;for(let p=1;p<r;p++){e.progress&&(e.progress({gen:p,total:l,probed:u,step:!0}),m=l);for(let g=0;g<p;g++)for(let X of a[p-g-1]||[])for(let j of a[g]||[]){if(l>=(e.tries??1/0))return{total:l,probed:u,gen:p,...e.retain?{cache:a}:{}};e.progress&&l-m>=i&&(e.progress({gen:p,total:l,probed:u,step:!1}),m=l);let K=X.apply(j),{res:Y,props:N}=x(K);if((Y??0)>0)return{expr:K,total:l,probed:u,gen:p,...e.retain?{cache:a}:{}};if((Y??0)<0)continue;let D=n&&N?(N.expr?0:3)+(N.dup?1:0)+(N.proper?0:1):0;a[p+D]||(a[p+D]=[]),a[p+D].push(K)}}return{total:l,probed:u,gen:r,...e.retain?{cache:a}:{}}}function J(s,e={}){if(s instanceof y)return s.format(e);if(s instanceof T)return"Quest("+s.name+")";if(s instanceof T.Case)return"Quest.Case";if(Array.isArray(s))return s.map(r=>J(r,e));if(typeof s!="object"||s===null||s.constructor!==Object)return s;let t={};for(let r in s)t[r]=J(s[r],e);return t}function xe(s,e){return s.declare({inventory:e})}var $=s=>Array.isArray(s)&&s.length===2&&typeof s[0]=="string"&&typeof s[1]=="string"?void 0:"must be a pair of strings",be=s=>Array.isArray(s)&&s.length===3&&typeof s[0]=="string"&&typeof s[1]=="string"&&typeof s[2]=="string"?void 0:"must be a triplet of strings",oe={html:s=>typeof s=="boolean"?void 0:"must be a boolean",terse:s=>typeof s=="boolean"?void 0:"must be a boolean",space:s=>typeof s=="string"?void 0:"must be a string",brackets:$,var:$,around:$,redex:$,lambda:be,inventory:s=>{if(typeof s!="object"||s===null||s.constructor!==Object)return"must be an object, not "+(s?.constructor?.name??typeof s);let e=s;for(let t of Object.keys(e))if(!(e[t]instanceof y))return"key "+t+"is not an Expr"}};function ye(s){if(s==null)return{value:{}};if(typeof s!="object"||Array.isArray(s)||s.constructor!==Object)return{error:{object:"Format options must be an object, not "+(s?.constructor?.name??typeof s)}};let e=s,t={};for(let r in e)if(oe[r]){let n=oe[r](e[r]);n&&(t[r]=n)}else t[r]="unknown option";return Object.keys(t).length>0?{error:t}:{value:e}}var H={search:ge,deepFormat:J,declare:xe,toposort:I,checkFormatOptions:ye};H.toposort=I;var Q=class extends z{static{this.native=w}static{this.control=c}static{this.classes=re}static{this.B=w.B}static{this.C=w.C}static{this.I=w.I}static{this.K=w.K}static{this.S=w.S}static{this.W=w.W}static vars(e={}){let t={};return new Proxy(t,{get(r,n){return n in r||(r[n]=new h(n,e)),r[n]}})}static church(e){return new A(e)}static{this.extras=H}static{this.Quest=T}},le=globalThis;le.process?.env.SKI_REPL&&(le.SKI=Q,console.log("SKI_REPL activated, try `new SKI();`"));typeof window<"u"&&(window.SKI=Q);})();
|
|
7
7
|
//# sourceMappingURL=ski-interpreter.min.js.map
|