Callable lambdas

This commit is contained in:
Freywar Ulvnaudgari
2024-08-09 16:10:12 +03:00
parent 385a3b30f2
commit 2038699a9e
20 changed files with 804 additions and 802 deletions
+41 -40
View File
@@ -1,4 +1,4 @@
import { L, _, fromNative as __ } from './core';
import { L, _, fromNative as __, cnst, id } from './core';
import { Bool, iif } from './bool';
import { Num, Zero, ge, ifz, pred, succ } from './num';
import { x00 } from './byte';
@@ -15,71 +15,72 @@ type IO = Pair<String, String>;
type State = Pair<Program, Pair<Tape, IO>>;
const pcommand: L<(p: Program) => Char>
= uncurry._(_(is => get._(head._(is))));
const padvance: L<(p: Program) => Program>
= first._(update._(succ)._(Zero));
const pjump: L<(d: Num) => L<(p: Program) => Program>>
= _(d => _(p =>
iif._<Program>(eq._(_c('['))._(pcommand._(p)))._(pjump._(succ._(d))._(padvance._(p)))
._(iif._<Program>(eq._(_c(']'))._(pcommand._(p)))._(ifz._<Program>(pred._(d))._(p)._(pjump._(pred._(d))._(padvance._(p))))
._(pjump._(d)._(padvance._(p))))));
const done: L<(s: State) => Bool>
= _(s => uncurry._(_(is => _(cs => ge._(head._(is))._(len._(cs)))))._(fst._(s)));
= uncurry(_(p => cnst(uncurry(_(is => _(cs => ge(head(is))(len(cs)))))(p))));
const command: L<(s: State) => Char>
= _(s => pcommand._(fst._(s)));
const command: L<(c: String) => L<(s: State) => Bool>>
= _(c => uncurry(_(p => cnst(eq(c)(uncurry(_(is => get(head(is))))(p))))));
const advance: L<(s: State) => State>
= first._(padvance);
= first(first(update(succ)(Zero)));
const jump: L<(s: State) => State>
= first._(pjump._(Zero));
const save: L<(s: State) => State>
= first._(first._(_(is => cons._(head._(is))._(is))));
= first(first(_(is => cons(head(is))(is))));
const restore: L<(s: State) => State>
= first._(first._(tail));
= first(first(tail));
const reset: L<(s: State) => State>
= first._(first._(_(is => cons._(head._(is))._(tail._(tail._(is))))));
= first(first(_(is => cons(head(is))(tail(tail(is))))));
const inc: L<(t: State) => State>
= second._(first._(_(t => second._(update._(cinc)._(fst._(t)))._(t))));
= second(first(_(t => second(update(cinc)(fst(t)))(t))));
const dec: L<(t: State) => State>
= second._(first._(_(t => second._(update._(cdec)._(fst._(t)))._(t))));
= second(first(_(t => second(update(cdec)(fst(t)))(t))));
const read: L<(s: State) => Char>
= _(s => uncurry._(get)._(fst._(snd._(s))));
= _(s => uncurry(get)(fst(snd(s))));
const input: L<(s: State) => State>
= second._(uncurry._(_(t => _(io => Pair._(second._(set._(head._(fst._(io)))._(fst._(t)))._(t))._(first._(tail)._(io))))));
= second(uncurry(_(t => _(io => Pair(second(set(head(fst(io)))(fst(t)))(t))(first(tail)(io))))));
const output: L<(s: State) => State>
= second._(uncurry._(_(t => _(io => Pair._(t)._(second._(_(os => snoc._(os)._(uncurry._(get)._(t))))._(io))))));
= second(uncurry(_(t => _(io => Pair(t)(second(_(os => snoc(os)(uncurry(get)(t))))(io))))));
const next: L<(t: State) => State>
= second._(first._(first._(succ)));
= second(first(first(succ)));
const prev: L<(t: State) => State>
= second._(first._(first._(pred)));
= second(first(first(pred)));
const jump: L<(d: Num) => L<(s: State) => State>>
= _(d => _(s =>
iif(command(_c('['))(s))(jump(succ(d)))
(iif(command(_c(']'))(s))(ifz(pred(d))(cnst(s))(jump(pred(d))))
(jump(d)))(advance(s))));
const whlnz: L<(t: State) => State>
= _(s => ifn(read(s))(jump(Zero))(save)(s));
const endwhl: L<(t: State) => State>
= _(s => ifn(read(s))(reset(s))(save(restore(s))));
const when: L<(c: String) => L<(f: L<(s: State) => State>) => L<(s:State)=>State>>>
= _(c => _(f => _(s => iif(command(c)(s))(f(s))(s))));
const exec: L<(s: State) => State>
= _(s => iif._<State>(done._(s))._(s)._(exec._(advance
._(iif._<State>(eq._(_c('>'))._(command._(s)))._(next._(s))
._(iif._<State>(eq._(_c('<'))._(command._(s)))._(prev._(s))
._(iif._<State>(eq._(_c('+'))._(command._(s)))._(inc._(s))
._(iif._<State>(eq._(_c('-'))._(command._(s)))._(dec._(s))
._(iif._<State>(eq._(_c('.'))._(command._(s)))._(output._(s))
._(iif._<State>(eq._(_c(','))._(command._(s)))._(input._(s))
._(iif._<State>(eq._(_c('['))._(command._(s)))._(ifn._<State>(read._(s))._(jump._(s))._(save._(s)))
._(iif._<State>(eq._(_c(']'))._(command._(s)))._(ifn._<State>(read._(s))._(reset._(s))._(save._(restore._(s))))
._(s))))))))))));
= _(s => iif<State>(done(s))(s)(exec(advance
(iif(command(_c('>'))(s))(next)
(iif(command(_c('<'))(s))(prev)
(iif(command(_c('+'))(s))(inc)
(iif(command(_c('-'))(s))(dec)
(iif(command(_c('.'))(s))(output)
(iif(command(_c(','))(s))(input)
(iif(command(_c('['))(s))(whlnz)
(iif(command(_c(']'))(s))(endwhl)
(id))))))))(s)))));
export const run: L<(p: String) => L<(i: String) => String>>
= _(p => _(i => snd._(snd._(snd._(exec._(Pair._(Pair._(cons._(Zero)._(Nil))._(p))._(Pair._(Pair._(Zero)._(repeat._(x00)))._(Pair._(i)._(Empty)))))))));
= _(p => _(i => snd(snd(snd(exec(Pair(Pair(cons(Zero)(Nil))(p))(Pair(Pair(Zero)(repeat(x00)))(Pair(i)(Empty)))))))));
+14 -14
View File
@@ -1,4 +1,4 @@
import { toNative as $$, L, P, _, fromNative as __ } from './core';
import { toNative as $$, L, P, _, fromNative as __, id } from './core';
import { EQ, GT, LT, Ord } from './ord';
import { String, fromString as _s } from './string';
@@ -11,43 +11,43 @@ export const False: Bool
= _(_t => _(f => f));
export const iif: L<<T extends P>(b: Bool) => L<(t: T) => L<(f: T) => T>>>
= _(b => _(t => _(f => b._(t)._(f))));
= id;
export const not: L<(b: Bool) => Bool>
= _(b => b._(False)._(True));
= _(b => b(False)(True));
export const cmp: L<(l: Bool) => L<(r: Bool) => Ord>>
= _(l => _(r => l._(r._(EQ)._(GT))._(r._(LT)._(EQ))));
= _(l => _(r => l(r(EQ)(GT))(r(LT)(EQ))));
export const lt: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(False)._(r)));
= _(l => l(False));
export const le: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(r)._(True)));
= _(l => not(l)(True));
export const eq: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(r)._(not._(r))));
= _(l => _(r => l(r)(not(r))));
export const ge: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(True)._(not._(r))));
= _(l => _(r => not(lt(l)(r))));
export const gt: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(not._(r))._(False)));
= _(l => _(r => not(le(l)(r))));
export const and: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(r)._(False)));
= _(l => not(l)(False));
export const or: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(True)._(r)));
= _(l => l(True));
export const xor: L<(l: Bool) => L<(r: Bool) => Bool>>
= _(l => _(r => l._(not._(r))._(r)));
= _(l => _(r => l(not(r))(r)));
export const show: L<(b: Bool) => String>
= _(b => iif._(b)._(_s('True'))._(_s('False')));
= _(b => iif(b)(_s('True'))(_s('False')));
export const fromBoolean: (b: boolean) => Bool
= b => b ? True : False;
export const toBoolean: (b: Bool) => boolean
= b => $$(b._(__(true))._(__(false)));
= b => $$(b(__(true))(__(false)));
+5 -5
View File
@@ -11,7 +11,7 @@ export const xFF: Num
= _n(255);
export const Inc: L<(p: Byte) => Byte>
= _(p => iif._(eq._(xFF)._(p))._(x00)._(Succ._(p)));
= _(p => iif(eq(xFF)(p))(x00)(Succ(p)));
export { ifz, cmp, lt, le, eq, ge, gt, even, odd, div, mod, show, toNumber } from './num';
@@ -19,16 +19,16 @@ export const inc: L<(n: Byte) => Byte>
= Inc;
export const dec: L<(n: Byte) => Byte>
= _(n => n._(xFF)._(id));
= _(n => n(xFF)(id));
export const sum: L<(l: Byte) => L<(r: Byte) => Byte>>
= _(l => _(r => r._(l)._(_(pr => sum._(inc._(l))._(pr)))));
= _(l => _(r => r(l)(sum(inc(l)))));
export const sub: L<(l: Byte) => L<(r: Byte) => Byte>>
= _(l => _(r => r._(l)._(_(pr => sub._(dec._(l))._(pr)))));
= _(l => _(r => r(l)(sub(dec(l)))));
export const mul: L<(l: Byte) => L<(r: Byte) => Byte>>
= _(l => _(r => l._(x00)._(_(pl => sum._(r)._(mul._(pl)._(r))))));
= _(l => _(r => l(x00)(_(pl => sum(r)(mul(pl)(r))))));
export const fromNumber: (n: number) => Byte
= n => _n(n % 256);
+2 -2
View File
@@ -1,6 +1,6 @@
import { L, _ } from './core';
import { toNumber as $n, Byte, fromNumber as _n, x00 } from './byte';
import { Empty, String, cons } from './string';
import { Empty, String, cons, fromString as _s, wrap } from './string';
export type Char = Byte;
@@ -10,7 +10,7 @@ export const Null: Char
export { inc, dec, ifz as ifn, cmp, lt, le, eq, ge, gt } from './byte';
export const show: L<(c: Char) => String>
= _(c => cons._(fromChar('\''))._(cons._(c)._(cons._(fromChar('\''))._(Empty))));
= _(c => wrap(_s('\''))(cons(c)(Empty)));
export const fromChar: (c: string) => Char
= c => _n(c.charCodeAt(0));
+14 -19
View File
@@ -1,33 +1,28 @@
export class L<T extends (v: unknown) => L<any>> {
protected _value?: T;
export type L<T extends (v: P) => L<any>> = T & { run: () => T; }
constructor(private _eval: () => T) { }
export function ___<T extends (v: P) => L<any>>(run: () => T): L<T>{
const r: T = ((p: Parameters<T>[0]) => {
let v: any;
return ___(() => run()(
___(() => v ??= p.run())
).run());
}) as T;
public evaluated: boolean = false;
(r as L<T>).run = run;
public get value(): T {
if (this.evaluated) {
return this._value!;
}
this.evaluated = true;
return this._value = this._eval();
}
public get _(): T {
return ((p: Parameters<T>[0]) => new L(() => this.value(p).value)) as T;
}
return r as L<T>;
}
export type P = L<(v: unknown) => L<any>>;
export const _ = <T extends ((v: unknown) => L<any>)>(v: T): L<T> => new L(() => v);
export const _ = <T extends ((v: unknown) => L<any>)>(v: T): L<T> => ___(() => v);
export const fromNative: <T>(v: T) => L<any>
= v => new L(() => v as any);
= v => ___(() => v as any);
export const toNative: <T>(v: L<any>) => T
= <T>(v: L<any>) => v.value as T;
= <T>(v: L<any>) => v.run() as T;
export const undef: any = new L(() => {
export const undef: any = ___(() => {
throw new Error('undefined');
});
+1 -1
View File
@@ -1,3 +1,3 @@
import { toNative as $$, _, fromNative as __, id } from './core';
console.log($$(id._(__('first'))));
console.log($$(id(__('first'))));
+37 -47
View File
@@ -1,4 +1,4 @@
import { toNative as $$, L, P, _, fromNative as __, undef } from './core';
import { toNative as $$, L, P, _, fromNative as __, ___, cnst, id, undef } from './core';
import { EQ, GT, LT, Ord, eq as eqc } from './ord';
import { Bool, False, True, and, iif, or } from './bool';
import { Num, Zero, succ } from './num';
@@ -10,142 +10,132 @@ export const Nil: List<any>
= _(z => _(_f => z)) as List<any>;
export const Cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
= _(<T extends P>(x: T) => _((xs: List<T>) => _(_z => _(f => f._(x)._(xs))) as List<T>));
= _(<T extends P>(x: T) => _((xs: List<T>) => _(_z => _(f => f(x)(xs))) as List<T>));
export const cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
= Cons;
export const snoc: L<<T extends P>(xs: List<T>) => L<(x: T) => List<T>>>
= _(<T extends P>(xs: List<T>) =>
_((x: T) => xs._(cons._(x)._(Nil))._(_(y => _(ys => cons._(y)._(snoc._(ys)._(x)))))));
_((x: T) => xs(cons(x)(Nil))(_(y => _(ys => cons(y)(snoc(ys)(x)))))));
export const nul: L<<T extends P>(xs: List<T>) => Bool>
= _(xs => xs._(True)._(_(_x => _(_xs => False))));
= _(xs => xs(True)(cnst(cnst(False))));
export const len: L<<T extends P>(xs: List<T>) => Num>
= _(xs => xs._(Zero)._(_(_x => _(xs => succ._(len._(xs))))));
= _(xs => xs(Zero)(_(_x => _(xs => succ(len(xs))))));
export const append: L<<T extends P>(l: List<T>) => L<(r: List<T>) => List<T>>>
= _(l => _(r => l._(r)._(_(x => _(xs => cons._(x)._(append._(xs)._(r)))))));
= _(l => _(r => l(r)(_(x => _(xs => cons(x)(append(xs)(r)))))));
export const concat: L<<T extends P>(ls: List<List<T>>) => List<T>>
= _(ls => ls._(Nil)._(_(x => _(xs => append._(x)._(concat._(xs))))));
= _(ls => ls(Nil)(_(x => _(xs => append(x)(concat(xs))))));
export const repeat: L<<T extends P>(x: T) => List<T>>
= _(x => cons._(x)._(repeat._(x)));
= _(x => cons(x)(repeat(x)));
export const iterate: L<<T extends P>(f: L<(x: T) => T>) => L<(x: T) => List<T>>>
= _(<T extends P>(f: L<(x: T) => T>) =>
_((x: T) =>
cons._(x)._(iterate._(f)._(f._(x)))));
= _(<T extends P>(f: L<(x: T) => T>) => _((x: T) => cons(x)(iterate(f)(f(x)))));
export const head: L<<T extends P>(xs: List<T>) => T>
= _(xs => xs._(undef)._(_(x => _(_xs => x))));
= _(xs => xs(undef)(cnst));
export const tail: L<<T extends P>(xs: List<T>) => List<T>>
= _(xs => xs._(undef)._(_(_x => _(xs => xs))));
= _(xs => xs(undef)(cnst(id)));
export const foldl: L<<T extends P, R extends P>(f: L<(a: R) => L<(x: T) => R>>) => L<(z: R) => L<(xs: List<T>) => R>>>
= _(<T extends P, R extends P>(f: L<(a: R) => L<(x: T) => R>>) =>
_((z: R) =>
_((xs: List<T>) => xs._(z)._(_(x =>
_(xs => foldl._(f)._(f._(z)._(x))._(xs)))))));
_((xs: List<T>) => xs(z)(_(x => foldl(f)(f(z)(x)))))));
export const foldlz: L<<T extends P>(f: L<(a: T) => L<(x: T) => T>>) => L<(xs: List<T>) => T>>
= _(<T extends P>(f: L<(a: T) => L<(x: T) => T>>) =>
_((xs: List<T>) =>
xs._(undef)._(_(x =>
_(xs => foldl._(f)._(x)._(xs))))));
= _(<T extends P>(f: L<(a: T) => L<(x: T) => T>>) => _((xs: List<T>) => xs(undef)(foldl(f))));
export const foldr: L<<T extends P, R extends P>(f: L<(x: T) => L<(a: R) => R>>) => L<(z: R) => L<(xs: List<T>) => R>>>
= _(<T extends P, R extends P>(f: L<(x: T) => L<(a: R) => R>>) =>
_((z: R) =>
_((xs: List<T>) => xs._(z)._(_(x => _(xs => f._(x)._(foldr._(f)._(z)._(xs))))))));
_((xs: List<T>) => xs(z)(_(x => _(xs => f(x)(foldr(f)(z)(xs))))))));
export const foldrz: L<<T extends P>(f: L<(x: T) => L<(a: T) => T>>) => L<(xs: List<T>) => T>>
= _(_f => _(_z => undef));
= undef;
export const cmp: L<<T extends P>(ecmp: L<(l: T) => L <(r: T) => Ord>>) => L<(l: List<T>) => L<(r: List<T>) => Ord>>>
= _(<T extends P>(ecmp: L<(l: T) => L<(r: T) => Ord>>) =>
_((l: List<T>) =>
_((r: List<T>) => l
._(r._(EQ)._(_(_x => _(_xs => LT))))
._(_(lx => _(lxs => r._(GT)._(_(rx => _(rxs => iif
._(eqc._(EQ)._(ecmp._(lx)._(rx)))
._(cmp._(ecmp)._(lxs)._(rxs))
._(ecmp._(lx)._(rx)))))))))));
(r(EQ)(cnst(cnst(LT))))
(_(lx => _(lxs => r(GT)(_(rx => _(rxs => iif
(eqc(EQ)(ecmp(lx)(rx)))
(cmp(ecmp)(lxs)(rxs))
(ecmp(lx)(rx)))))))))));
export const lt: L<<T extends P>(ecmp: L<(l: T) => L <(r: T) => Ord>>) => L<(l: List<T>) => L<(r: List<T>) => Bool>>>
= _(<T extends P>(ecmp: L<(l: T) => L<(r: T) => Ord>>) =>
_((l: List<T>) =>
_((r: List<T>) =>
r._(False)._(_(rx => _(rxs => l._(True)._(_(lx => _(lxs => ecmp._(lx)._(rx)._(True)._(lt._(ecmp)._(lxs)._(rxs))._(False))))))))));
r(False)(_(rx => _(rxs => l(True)(_(lx => _(lxs => ecmp(lx)(rx)(True)(lt(ecmp)(lxs)(rxs))(False))))))))));
export const le: L<<T extends P>(ecmp: L<(l: T) => L <(r: T) => Ord>>) => L<(l: List<T>) => L<(r: List<T>) => Bool>>>
= _(<T extends P>(ecmp: L<(l: T) => L<(r: T) => Ord>>) =>
_((l: List<T>) =>
_((r: List<T>) =>
l._(True)._(_(lx => _(lxs => r._(False)._(_(rx => _(rxs => ecmp._(lx)._(rx)._(True)._(le._(ecmp)._(lxs)._(rxs))._(False))))))))));
l(True)(_(lx => _(lxs => r(False)(_(rx => _(rxs => ecmp(lx)(rx)(True)(le(ecmp)(lxs)(rxs))(False))))))))));
export const eq: L<<T extends P>(eeq: L<(l: T) => L <(r: T) => Bool>>) => L<(l: List<T>) => L<(r: List<T>) => Bool>>>
= _(<T extends P>(eeq: L<(l: T) => L<(r: T) => Bool>>) =>
_((l: List<T>) =>
_((r: List<T>) => l
._(r._(True)._(_(_x => _(_xs => False))))
._(_(lx => _(lxs => r._(False)._(_(rx => _(rxs => and._(eeq._(lx)._(rx))._(eq._(eeq)._(lxs)._(rxs)))))))))));
(r(True)(cnst(cnst(False))))
(_(lx => _(lxs => r(False)(_(rx => _(rxs => and(eeq(lx)(rx))(eq(eeq)(lxs)(rxs)))))))))));
export const ge: L<<T extends P>(ecmp: L<(l: T) => L <(r: T) => Ord>>) => L<(l: List<T>) => L<(r: List<T>) => Bool>>>
= _(<T extends P>(ecmp: L<(l: T) => L<(r: T) => Ord>>) =>
_((l: List<T>) =>
_((r: List<T>) =>
r._(True)._(_(rx => _(rxs => l._(False)._(_(lx => _(lxs => ecmp._(lx)._(rx)._(False)._(ge._(ecmp)._(lxs)._(rxs))._(True))))))))));
r(True)(_(rx => _(rxs => l(False)(_(lx => _(lxs => ecmp(lx)(rx)(False)(ge(ecmp)(lxs)(rxs))(True))))))))));
export const gt: L<<T extends P>(ecmp: L<(l: T) => L <(r: T) => Ord>>) => L<(l: List<T>) => L<(r: List<T>) => Bool>>>
= _(<T extends P>(ecmp: L<(l: T) => L<(r: T) => Ord>>) =>
_((l: List<T>) =>
_((r: List<T>) =>
l._(False)._(_(lx => _(lxs => r._(True)._(_(rx => _(rxs => ecmp._(lx)._(rx)._(False)._(gt._(ecmp)._(lxs)._(rxs))._(True))))))))));
l(False)(_(lx => _(lxs => r(True)(_(rx => _(rxs => ecmp(lx)(rx)(False)(gt(ecmp)(lxs)(rxs))(True))))))))));
export const map: L<<T extends P, R extends P>(f: L<(x: T) => R>) => L<(xs: List<T>) => List<R>>>
= _(<T extends P, R extends P>(f: L<(x: T) => R>) =>
_((xs: List<T>) => xs._(Nil)._(_(x => _(xs => Cons._(f._(x))._(map._(f)._(xs)))))));
_((xs: List<T>) => xs(Nil)(_(x => _(xs => cons(f(x))(map(f)(xs)))))));
export const filter: L<<T extends P>(f: L<(x: T) => Bool>) => L<(xs: List<T>) => List<T>>>
= _(<T extends P>(f: L<(x: T) => Bool>) =>
_((xs: List<T>) => xs._(Nil)._(_(x => _(xs => iif._(f._(x))._(Cons._(x)._(filter._(f)._(xs)))._(filter._(f)._(xs)))))));
_((xs: List<T>) => xs(Nil)(_(x => _(xs => iif(f(x))(cons(x))(id)(filter(f)(xs)))))));
export const any: L<<T extends P>(f: L<(x: T) => Bool>) => L<(xs: List<T>) => Bool>>
= _(<T extends P>(f: L<(x: T) => Bool>) =>
_((xs: List<T>) => xs._(False)._(_(x => _(xs => or._(f._(x))._(any._(f)._(xs)))))));
_((xs: List<T>) => xs(False)(_(x => _(xs => or(f(x))(any(f)(xs)))))));
export const all: L<<T extends P>(f: L<(x: T) => Bool>) => L<(xs: List<T>) => Bool>>
= _(<T extends P>(f: L<(x: T) => Bool>) =>
_((xs: List<T>) => xs._(True)._(_(x => _(xs => and._(f._(x))._(all._(f)._(xs)))))));
_((xs: List<T>) => xs(True)(_(x => _(xs => and(f(x))(all(f)(xs)))))));
export const get: L<<T extends P>(i: Num) => L<(xs: List<T>) => T>>
= _(<T extends P>(i: Num) =>
_((xs: List<T>) =>
xs._(undef)._(_((x: T) => _((xs: List<T>) => i._(x)._(_(pi => get._<T>(pi)._(xs))))))));
= _(i => _(xs => xs(undef)(_(x => i(cnst(x))(get)))));
export const update: L<<T extends P>(f: L< (p: T) => T>) => L<(i: Num) => L<(xs: List<T>) => List<T>>>>
= _(<T extends P>(f: L< (p: T) => T>) =>
_((i: Num) =>
_((xs: List<T>) =>
xs._(undef)._(_(x => _(xs => i._(cons._(f._(x))._(xs))._(_(pi => cons._(x)._(update._(f)._(pi)._(xs))))))))));
xs(undef)(_(x => _(xs => i(cons(f(x))(xs))(_(pi => cons(x)(update(f)(pi)(xs))))))))));
export const set: L<<T extends P>(v: T) => L<(i: Num) => L<(xs: List<T>) => List<T>>>>
= _(<T extends P>(v: T) => update._<T>(_(_ => v)));
= _(v => update(cnst(v)));
export const intersperse: L<<T extends P>(v: T) => L<(xs: List<T>) => List<T>>>
= _(v => _(xs =>
xs._(xs)._(_(x => _(rxs => rxs._(xs)._(_(_rx => _(_rrxs => cons._(x)._(cons._(v)._(intersperse._(v)._(rxs)))))))))));
= _(v => _(xs => xs(xs)(_(x => _(rxs => rxs(xs)(cnst(cnst(cons(x)(cons(v)(intersperse(v)(rxs)))))))))));
export const show: L<<T extends P>(eshow: L<(e: T) => String>) => L<(xs: List<T>) => String>>
= _(eshow => _(xs =>
concat._(snoc._(cons._(_s('['))._(intersperse._(_s(', '))._(map._(eshow)._(xs))))._(_s(']')))));
= _(eshow => _(xs => concat(snoc(cons(_s('['))(intersperse(_s(', '))(map(eshow)(xs))))(_s(']')))));
export const fromArray: <T extends P>(xs: T[]) => List<T>
= xs => xs.length === 0 ? Nil : Cons._(xs[0])._(new L(() => fromArray(xs.slice(1)).value));
= xs => xs.length === 0 ? Nil : cons(xs[0])(___(() => fromArray(xs.slice(1)).run()));
export const toArray: <T extends P>(xs: List<T>) => P[]
= xs => $$(xs._(__([]))._(_(x => _(xs => __([x, ...toArray(xs)])))));
= xs => $$(xs(__([]))(_(x => _(xs => __([x, ...toArray(xs)])))));
+22 -22
View File
@@ -1,4 +1,4 @@
import { toNative as $$, L, P, _, fromNative as __, id } from './core';
import { toNative as $$, L, P, _, fromNative as __, ___, cnst, id } from './core';
import { EQ, GT, LT, Ord } from './ord';
import { Bool, False, True, iif } from './bool';
import { Empty, String, append, cons } from './string';
@@ -9,63 +9,63 @@ export const Zero: Num
= _(z => _(_n => z)) as Num;
export const Succ: L<(p: Num) => Num>
= _(p => _(_z => _(n => n._(p))));
= _(p => _(_z => _(n => n(p))));
export const ifz: L<<T extends P>(n: Num) => L<(t: T) => L<(f: T) => T>>>
= _(n => _(t => _(f => n._(t)._(_(_ => f)))));
= _(n => _(t => _(f => n(t)(cnst(f)))));
export const succ: L<(n: Num) => Num>
= Succ;
export const pred: L<(n: Num) => Num>
= _(n => n._(Zero)._(id));
= _(n => n(Zero)(id));
export const cmp: L<(l: Num) => L<(r: Num) => Ord>>
= _(l => _(r => l._(r._(EQ)._(_(_ => LT)))._(_(pl => r._(GT)._(_(pr => cmp._(pl)._(pr)))))));
= _(l => _(r => l(r(EQ)(cnst(LT)))(_(pl => r(GT)(cmp(pl))))));
export const lt: L<(l: Num) => L<(r: Num) => Bool>>
= _(l => _(r => r._(False)._(_(pr => l._(True)._(_(pl => lt._(pl)._(pr)))))));
= _(l => _(r => r(False)(l(cnst(True))(lt))));
export const le: L<(l: Num) => L<(r: Num) => Bool>>
= _(l => _(r => l._(True)._(_(pl => r._(False)._(_(pr => le._(pl)._(pr)))))));
= _(l => _(r => l(True)(r(cnst(False))(ge))));
export const eq: L<(l: Num) => L<(r: Num) => Bool>>
= _(l => _(r => l._(r._(True)._(_(_ => False)))._(_(pl => r._(False)._(_(pr => eq._(pl)._(pr)))))));
= _(l => _(r => l(r(True)(cnst(False)))(r(cnst(False))(eq))));
export const ge: L<(l: Num) => L<(r: Num) => Bool>>
= _(l => _(r => r._(True)._(_(pr => l._(False)._(_(pl => ge._(pl)._(pr)))))));
= _(l => _(r => r(True)(l(cnst(False))(ge))));
export const gt: L<(l: Num) => L<(r: Num) => Bool>>
= _(l => _(r => l._(False)._(_(pl => r._(True)._(_(pr => gt._(pl)._(pr)))))));
= _(l => _(r => l(False)(r(cnst(True))(lt))));
export const even: L<(n: Num) => Bool>
= _(n => n._(True)._(odd));
= _(n => n(True)(odd));
export const odd: L<(n: Num) => Bool>
= _(n => n._(False)._(even));
= _(n => n(False)(even));
export const sum: L<(l: Num) => L<(r: Num) => Num>>
= _(l => _(r => r._(l)._(_(t => sum._(Succ._(l))._(t)))));
= _(l => _(r => r(l)(sum(Succ(l)))));
export const sub: L<(l: Num) => L<(r: Num) => Num>>
= _(l => _(r => r._(l)._(_(pr => l._(Zero)._(_(pl => sub._(pl)._(pr)))))));
= _(l => _(r => r(l)(l(cnst(Zero))(sub))));
export const mul: L<(l: Num) => L<(r: Num) => Num>>
= _(l => _(r => l._(Zero)._(_(pl => sum._(r)._(mul._(pl)._(r))))));
= _(l => _(r => l(Zero)(_(pl => sum(r)(mul(pl)(r))))));
export const div: L<(l: Num) => L<(r: Num) => Num>>
= _(l => _(r => iif._(lt._(l)._(r))._(Zero)._(succ._(div._(sub._(l)._(r))._(r)))));
= _(l => _(r => iif(lt(l)(r))(Zero)(succ(div(sub(l)(r))(r)))));
export const mod: L<(l: Num) => L<(r: Num) => Num>>
= _(l => _(r => iif._(lt._(l)._(r))._(l)._(mod._(sub._(l)._(r))._(r))));
= _(l => _(r => iif(lt(l)(r))(l)(mod(sub(l)(r))(r))));
export const show: L<(n: Num) => String>
= _(n => iif._(lt._(n)._(fromNumber(10)))
._(cons._(sum._(n)._(fromNumber(48)))._(Empty))
._(append._(show._(div._(n)._(fromNumber(10))))._(show._(mod._(n)._(fromNumber(10))))));
= _(n => iif(lt(n)(fromNumber(10)))
(cons(sum(n)(fromNumber(48)))(Empty))
(append(show(div(n)(fromNumber(10))))(show(mod(n)(fromNumber(10))))));
export const fromNumber: (n: number) => Num
= n => !n ? Zero : Succ._(new L(() => fromNumber(n - 1).value));
= n => !n ? Zero : Succ(___(() => fromNumber(n - 1).run()));
export const toNumber: (n: Num) => number
= n => $$(n._(__(0))._(_(p => __(1 + toNumber(p)))));
= n => $$(n(__(0))(_(p => __(1 + toNumber(p)))));
+5 -5
View File
@@ -21,15 +21,15 @@ export const GT: Ord
export const eq: L<(l: Ord) => L<(r: Ord) => Bool>>
= _(l => _(r => l
._(r._(True)._(False)._(False))
._(r._(False)._(True)._(False))
._(r._(False)._(False)._(True))));
(r(True)(False)(False))
(r(False)(True)(False))
(r(False)(False)(True))));
export const show: L<(o: Ord) => String>
= _(o => o._(_s('LT'))._(_s('EQ'))._(_s('GT')));
= _(o => o(_s('LT'))(_s('EQ'))(_s('GT')));
export const fromNOrd: (b: NOrd) => Ord
= o => o === NOrd.LT ? LT : o === NOrd.EQ ? EQ : GT;
export const toNOrd: (b: Ord) => NOrd
= o => $$(o._(__(NOrd.LT))._(__(NOrd.EQ))._(__(NOrd.GT)));
= o => $$(o(__(NOrd.LT))(__(NOrd.EQ))(__(NOrd.GT)));
+32 -33
View File
@@ -9,75 +9,74 @@ export type Pair<F extends P, S extends P> = L<<R extends P>(f: L<(f: F) => L<(s
export const Pair: L<<F extends P, S extends P>(f: F) => L<(s: S) => Pair<F, S>>>
= _(<F extends P, S extends P>(f: F) =>
_((s: S) =>
_(<R extends P>(p: L<(f: F) => L<(s: S) => R>>) => p._(f)._(s))));
_(<R extends P>(p: L<(f: F) => L<(s: S) => R>>) => p(f)(s))));
export const fst: L<<F extends P, S extends P>(p: Pair<F, S>) => F>
= _(p => p._(_(f => _(_s => f))));
= _(p => p(_(f => _(_s => f))));
export const snd: L<<F extends P, S extends P>(p: Pair<F, S>) => S>
= _(p => p._(_(_f => _(s => s))));
= _(p => p(_(_f => _(s => s))));
export const first: L<<F extends P, FR extends P, S extends P>(t: L<(f: F) => FR>) => L<(p: Pair<F, S>) => Pair<FR, S>>>
= _(<F extends P, FR extends P>(t: L<(f: F) => FR>) =>
_(<S extends P>(p: Pair<F, S>) => p._(_(f => _(s => Pair._(t._(f))._(s))))));
_(<S extends P>(p: Pair<F, S>) => p(_(f => _(s => Pair(t(f))(s))))));
export const second: L<<F extends P, S extends P, SR extends P>(t: L<(s: S) => SR>) => L<(p: Pair<F, S>) => Pair<F, SR>>>
= _(<S extends P, SR extends P>(t: L<(s: S) => SR>) =>
_(<F extends P>(p: Pair<F, S>) => p._(_(f => _(s => Pair._(f)._(t._(s)))))));
_(<F extends P>(p: Pair<F, S>) => p(_(f => _(s => Pair(f)(t(s)))))));
export const both: L<<F extends P, FR extends P, S extends P, SR extends P>(tf: L<(f: F) => FR>) => L<(ts: L<(s: S) => SR>) => L<(p: Pair<F, S>) => Pair<FR, SR>>>>
= _(<F extends P, FR extends P>(tf: L<(f: F) => FR>) =>
_(<S extends P, SR extends P>(ts: L<(s: S) => SR>) =>
_((p: Pair<F, S>) => p._(_(f => _(s => Pair._(tf._(f))._(ts._(s))))))));
_((p: Pair<F, S>) => p(_(f => _(s => Pair(tf(f))(ts(s))))))));
export const uncurry: L<<F extends P, S extends P, R extends P>(t: L<(f: F) => L<(s: S) => R>>) => L<(p: Pair<F, S>) => R>>
= _(<F extends P, S extends P, R extends P>(t: L<(f: F) => L<(s: S) => R>>) =>
_((p: Pair<F, S>) => p._(t)));
_((p: Pair<F, S>) => p(t)));
export const cmp: L<<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) => L<(scmp: L<(l: S) => L<(r: S) => Ord>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Ord>>>>
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
fcmp._(fst._(l))._(fst._(r))._(LT)._(scmp._(snd._(l))._(snd._(r)))._(GT)))));
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
fcmp(lf)(rf)(LT)(scmp(ls)(rs))(GT)))))))));
export const lt: L<<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) => L<(scmp: L<(l: S) => L<(r: S) => Bool>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Bool>>>>
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
fcmp._(fst._(l))._(fst._(r))._(True)._(scmp._(snd._(l))._(snd._(r))._(True)._(False)._(False))._(False)))));
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
fcmp(lf)(rf)(True)(scmp(ls)(rs)(True)(False)(False))(False)))))))));
export const le: L<<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) => L<(scmp: L<(l: S) => L<(r: S) => Bool>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Bool>>>>
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
fcmp._(fst._(l))._(fst._(r))._(True)._(scmp._(snd._(l))._(snd._(r))._(True)._(True)._(False))._(False)))));
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
fcmp(lf)(rf)(True)(scmp(ls)(rs)(True)(True)(False))(False)))))))));
export const eq: L<<F extends P, S extends P>(feq: L<(l: F) => L<(r: F) => Bool>>) => L<(seq: L<(l: S) => L<(r: S) => Bool>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Bool>>>>
= _(<F extends P, S extends P>(feq: L<(l: F) => L<(r: F) => Bool>>) =>
_((seq: L<(l: S) => L<(r: S) => Bool>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
and._(feq._(fst._(l))._(fst._(r)))._(seq._(snd._(l))._(snd._(r)))))));
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
and(feq(lf)(rf))(seq(ls)(rs))))))))));
export const ge: L<<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) => L<(scmp: L<(l: S) => L<(r: S) => Bool>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Bool>>>>
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
fcmp._(fst._(l))._(fst._(r))._(False)._(scmp._(snd._(l))._(snd._(r))._(False)._(True)._(True))._(True)))));
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
fcmp(lf)(rf)(False)(scmp(ls)(rs)(False)(True)(True))(True)))))))));
export const gt: L<<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) => L<(scmp: L<(l: S) => L<(r: S) => Bool>>) => L<(l: Pair<F, S>) => L<(r: Pair<F, S>) => Bool>>>>
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
_((l: Pair<F, S>) =>
_((r: Pair<F, S>) =>
fcmp._(fst._(l))._(fst._(r))._(False)._(scmp._(snd._(l))._(snd._(r))._(False)._(False)._(True))._(True)))));
= _(<F extends P, S extends P>(fcmp: L<(l: F) => L<(r: F) => Ord>>) =>
_((scmp: L<(l: S) => L<(r: S) => Ord>>) =>
uncurry(_((lf: F) => _((ls: S) =>
uncurry(_((rf: F) => _((rs: S) =>
fcmp(lf)(rf)(False)(scmp(ls)(rs)(False)(False)(True))(True)))))))));
export const show: L<<F extends P, S extends P>(fshow: L<(f: F) => String>) => L<(sshow: L<(s: S) => String>) => L<(p: Pair<F, S>) => String>>>
= _(<F extends P>(fshow: L<(f: F) => String>) =>
_(<S extends P>(sshow: L<(s: S) => String>) =>
_((p: Pair<F, S>) =>
uncurry._(_(f => _(s => concat._(cons._(_s('('))._(cons._(fshow._(f))._(cons._(_s(', '))._(cons._(sshow._(s))._(cons._(_s(')'))._(Nil)))))))))._(p))));
uncurry(_((f: F) => _((s: S) => concat(cons(_s('('))(cons(fshow(f))(cons(_s(', '))(cons(sshow(s))(cons(_s(')'))(Nil)))))))))));
+15 -12
View File
@@ -1,37 +1,40 @@
import { toNative as $$, L, _, fromNative as __ } from './core';
import { toNative as $$, L, _, fromNative as __, ___ } from './core';
import { Ord } from './ord';
import { Bool } from './bool';
import { fromNumber as _nn } from './num';
import { toChar as $c, Char, fromChar as _c, cmp as cmpc, eq as eqc } from './char';
import { Cons, List, Nil, cmp as cmpl, concat, cons, eq as eql, ge as gel, gt as gtl, le as lel, lt as ltl } from './list';
import { Cons, List, Nil, append, cmp as cmpl, concat, cons, eq as eql, ge as gel, gt as gtl, le as lel, lt as ltl } from './list';
export type String = List<Char>;
export { Nil as Empty, nul as empty, len, Cons, cons, snoc, append, repeat, iterate, get, update, set } from './list';
export const cmp: L<(l: String) => L<(r: String) => Ord>>
= cmpl._(cmpc);
= cmpl(cmpc);
export const lt: L<(l: String) => L<(r: String) => Bool>>
= ltl._(cmpc);
= ltl(cmpc);
export const le: L<(l: String) => L<(r: String) => Bool>>
= lel._(cmpc);
= lel(cmpc);
export const eq: L<(l: String) => L<(r: String) => Bool>>
= eql._(eqc);
= eql(eqc);
export const ge: L<(l: String) => L<(r: String) => Bool>>
= gel._(cmpc);
= gel(cmpc);
export const gt: L<(l: String) => L<(r: String) => Bool>>
= gtl._(cmpc);
= gtl(cmpc);
export const show: L<(s: String) => String>
= _(s => concat._(cons._(fromString('"'))._(cons._(s)._(cons._(fromString('"'))._(Nil)))));
export const wrap: L<(w: String) => L<(c: String) => String>>
= _(w => _(c => append(w)(append(c)(w))));
export const fromString: (xs: string) => String
= xs => xs.length === 0 ? Nil : Cons._(_c(xs[0]))._(new L(() => fromString(xs.slice(1)).value));
= xs => xs.length === 0 ? Nil : Cons(_c(xs[0]))(___(() => fromString(xs.slice(1)).run()));
export const toString: (xs: String) => string
= xs => $$(xs._(__(''))._(_(x => _(xs => __($c(x) + toString(xs))))));
= xs => $$(xs(__(''))(_(x => _(xs => __($c(x) + toString(xs))))));
export const show: L<(s: String) => String>
= wrap(fromString('"'));