Byte optimization

master
Freywar Ulvnaudgari 2 years ago
parent e7010aaeb0
commit c421f4123d
  1. 1
      profile.json
  2. 6
      src/byte.show.ts
  3. 47
      src/byte.ts
  4. 2
      src/char.ts
  5. 4
      src/index.ts
  6. 3
      src/num.show.ts
  7. 4
      src/num.ts

File diff suppressed because one or more lines are too long

@ -1,5 +1,7 @@
import { n } from './core'; import { _l, g, n } from './core';
import { snd } from './pair';
import { show as nshow } from './num.show';
n('byte.show'); n('byte.show');
export { show } from './num.show'; export const show = g('show', _l(n => nshow._(snd._(n))));

@ -1,25 +1,50 @@
import { Term, _, _l, g, id, n } from './core'; import { Term, _, _l, cnst, g, n } from './core';
import { iif } from './bool'; import { iif } from './bool';
import { Succ, Zero, fromNumber as _n, eq } from './num'; import { toNumber as $n, Zero, fromNumber as _n, cmp as ncmp, eq as neq, even as neven, ge as nge, gt as ngt, le as nle, lt as nlt, odd as nodd, pred, succ } from './num';
import { Pair, snd, uncurry } from './pair';
n('byte'); n('byte');
export const x00 = g('x00', Zero); export const x00 = g('x00', Pair._(_n(255))._(Zero));
export const xFF = g('xFF', _n(255)); export const xFF = g('xFF', Pair._(Zero)._(_n(255)));
export const Inc = g('Inc', _l(p => iif._(eq._(xFF)._(p))._(x00)._(Succ._(p)))); export const Inc = g('Inc', uncurry._(_l(i => _l(s => i._(x00)._(_l(pi => Pair._(pi)._(succ._(s))))))));
export { ifz, cmp, lt, le, eq, ge, gt, even, odd, div, mod, toNumber } from './num'; export const ifz = g('ifz', _l(n => _l(t => _l(f => snd._(n)._(t)._(cnst._(f))))));
export const inc = g('inc', Inc); export const inc = g('inc', Inc);
export const dec = g('dec', _l(n => n._(xFF)._(id))); export const dec = g('dec', _l(n => uncurry._(_l(i => _l(s => s._(xFF)._(_l(ps => Pair._(succ._(i))._(ps))))))._(n)));
export const sum = g('sum', _l(l => _l(r => r._(l)._(sum._(inc._(l)))))); export const cmp = g('cmp', _l(l => _l(r => ncmp._(snd._(l))._(snd._(r)))));
export const sub = g('sub', _l(l => _l(r => r._(l)._(sub._(dec._(l)))))); export const lt = g('lt', _l(l => _l(r => nlt._(snd._(l))._(snd._(r)))));
export const mul = g('mul', _l(l => _l(r => l._(x00)._(_l(pl => sum._(r)._(mul._(pl)._(r))))))); export const le = g('le', _l(l => _l(r => nle._(snd._(l))._(snd._(r)))));
export const fromNumber: (n: number) => Term = n => _n(n % 256); export const eq = g('eq', _l(l => _l(r => neq._(snd._(l))._(snd._(r)))));
export const ge = g('ge', _l(l => _l(r => nge._(snd._(l))._(snd._(r)))));
export const gt = g('gt', _l(l => _l(r => ngt._(snd._(l))._(snd._(r)))));
export const even = g('even', _l(l => _l(r => neven._(snd._(l))._(snd._(r)))));
export const odd = g('odd', _l(l => _l(r => nodd._(snd._(l))._(snd._(r)))));
export const sum = g('sum', _l(l => _l(r => snd._(r)._(l)._(_l(_ => sum._(inc._(l))._(dec._(r)))))));
export const sub = g('sub', _l(l => _l(r => snd._(r)._(l)._(_l(_ => sub._(dec._(l))._(dec._(r)))))));
export const mul = g('mul', _l(l => _l(r => snd._(l)._(x00)._(_l(_ => sum._(r)._(mul._(dec._(l))._(r)))))));
export const div = g('div', _l(l => _l(r => iif._(lt._(l)._(r))._(x00)._(inc._(div._(sub._(l)._(r))._(r))))));
export const mod = g('mod', _l(l => _l(r => iif._(lt._(l)._(r))._(l)._(mod._(sub._(l)._(r))._(r)))));
export const byte = g('byte', _l(n => n._(x00)._(_l(pn => inc._(byte._(pn))))));
export const fromNumber: (n: number) => Term = n => !n ? x00 : Inc._(_(() => fromNumber(n - 1)));
export const toNumber: (n: Term) => number = n => $n(snd._(n));

@ -5,7 +5,7 @@ n('char');
export const Null = g('Null', x00); export const Null = g('Null', x00);
export { inc, dec, ifz as ifn, cmp, lt, le, eq, ge, gt } from './byte'; export { inc, dec, ifz as ifn, cmp, lt, le, eq, ge, gt, byte as char } from './byte';
export const fromChar: (c: string) => Term = c => _n(c.charCodeAt(0)); export const fromChar: (c: string) => Term = c => _n(c.charCodeAt(0));

@ -1,11 +1,11 @@
import { dump, log, profile } from './debug'; import { dump, log, profile } from './debug';
import { _ } from './core'; import { _ } from './core';
import { fromNumber, sum, toNumber } from './byte'; import { fromNumber, mul, toNumber } from './byte';
log(); log();
profile(); profile();
const program = sum._(fromNumber(10))._(fromNumber(10)); const program = mul._(fromNumber(1))._(fromNumber(10));
try { try {
console.log(program.stringify(), ' -> ', toNumber(program)); console.log(program.stringify(), ' -> ', toNumber(program));

@ -1,8 +1,9 @@
import { _, _l, g, n } from './core'; import { _, _l, g, n } from './core';
import { iif } from './bool'; import { iif } from './bool';
import { fromNumber as _n, div, lt, mod, sum } from './num'; import { fromNumber as _n, div, lt, mod, sum } from './num';
import { char } from './char';
import { Empty, append, cons } from './string'; import { Empty, append, cons } from './string';
n('num.show'); n('num.show');
export const show = g('show', _l(n => iif._(lt._(n)._(_n(10)))._(cons._(sum._(n)._(_n(48)))._(Empty))._(append._(show._(div._(n)._(_n(10))))._(show._(mod._(n)._(_n(10))))))); export const show = g('show', _l(n => iif._(lt._(n)._(_n(10)))._(cons._(char._(sum._(n)._(_n(48))))._(Empty))._(append._(show._(div._(n)._(_n(10))))._(show._(mod._(n)._(_n(10)))))));

@ -4,9 +4,9 @@ import { False, True, iif } from './bool';
n('num'); n('num');
export const Zero = g('Zero', _l(z => _l(_n => z))); export const Zero = g('Zero', _l(z => _l(_ => z)));
export const Succ = g('Succ', _l(p => _l(_z => _l(n => n._(p))))); export const Succ = g('Succ', _l(p => _l(_ => _l(n => n._(p)))));
export const ifz = g('ifz', _l(n => _l(t => _l(f => n._(t)._(cnst._(f)))))); export const ifz = g('ifz', _l(n => _l(t => _l(f => n._(t)._(cnst._(f))))));

Loading…
Cancel
Save