parent
1c70da5f3b
commit
531c52212d
@ -1,30 +1,30 @@ |
|||||||
import { Lazy, _ } from './core'; |
import { Lazy, V, _ } from './core'; |
||||||
|
|
||||||
export type Bool = <T>(t: Lazy<T>) => (f: Lazy<T>) => Lazy<T>; |
export type Bool = Lazy<<T extends V>(t: T) => (f: T) => T>; |
||||||
|
|
||||||
export const True: Lazy<Bool> |
export const True: Bool |
||||||
= _(t => _ => t) as Lazy<Bool>; |
= _(t => _ => t) as Bool; |
||||||
|
|
||||||
export const False: Lazy<Bool> |
export const False: Bool |
||||||
= _(_ => f => f); |
= _(_ => f => f); |
||||||
|
|
||||||
export const iif: <T>(b: Lazy<Bool>) => (t: Lazy<T>) => (f: Lazy<T>) => Lazy<T> |
export const iif: <T extends V>(b: Bool) => (t: T) => (f: T) => T |
||||||
= b => t => f => b.then(b => b(t)(f)); |
= b => t => f => b.then(b => b(t)(f)); |
||||||
|
|
||||||
export const not: (b: Lazy<Bool>) => Lazy<Bool> |
export const not: (b: Bool) => Bool |
||||||
= b => b.then(b => b(False)(True)); |
= b => b.then(b => b(False)(True)); |
||||||
|
|
||||||
export const and: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> |
export const and: (l: Bool) => (r: Bool) => Bool |
||||||
= l => r => l.then(l => l(r)(False)); |
= l => r => l.then(l => l(r)(False)); |
||||||
|
|
||||||
export const or: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> |
export const or: (l: Bool) => (r: Bool) => Bool |
||||||
= l => r => l.then(l => l(True)(r)); |
= l => r => l.then(l => l(True)(r)); |
||||||
|
|
||||||
export const xor: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> |
export const xor: (l: Bool) => (r: Bool) => Bool |
||||||
= l => r => l.then(l => l(not(r))(r)); |
= l => r => l.then(l => l(not(r))(r)); |
||||||
|
|
||||||
export const fromBoolean: (b: boolean) => Lazy<Bool> |
export const fromBoolean: (b: boolean) => Bool |
||||||
= b => b ? True : False; |
= b => b ? True : False; |
||||||
|
|
||||||
export const toBoolean: (b: Lazy<Bool>) => Lazy<boolean> |
export const toBoolean: (b: Bool) => boolean |
||||||
= b => b.then(b => b(_(true))(_(false))); |
= b => b.then(b => b(_(true))(_(false))).value; |
||||||
|
|||||||
@ -1,50 +1,50 @@ |
|||||||
import { Lazy, _, cnst, id, undef } from './core'; |
import { Lazy, V, _, cnst, id, undef } from './core'; |
||||||
import { fromNumber, Num, toNumber } from './num'; |
import { fromNumber, Num, toNumber } from './num'; |
||||||
import { Bool, iif } from './bool'; |
import { Bool, iif } from './bool'; |
||||||
|
|
||||||
export type List<T> = <R>(z: Lazy<R>) => (f: (x: Lazy<T>) => (xs: Lazy<List<T>>) => Lazy<R>) => Lazy<R>; |
export type List<T extends V> = Lazy<<R extends V>(z: R) => (f: (x: T) => (xs: List<T>) => R) => R>; |
||||||
|
|
||||||
export const Nil: Lazy<List<any>> |
export const Nil: List<any> |
||||||
= _<List<any>>(z => _ => z); |
= _(z => _ => z) as List<any>; |
||||||
|
|
||||||
export const Cons: <T>(x: Lazy<T>) => (xs: Lazy<List<T>>) => Lazy<List<T>> |
export const Cons: <T extends V>(x: T) => (xs: List<T>) => List<T> |
||||||
= <T>(x: Lazy<T>) => (xs: Lazy<List<T>>) => _<List<T>>(_ => f => f(x)(xs)); |
= <T extends V>(x: T) => (xs: List<T>) => _(_ => f => f(x)(xs)) as List<T>; |
||||||
|
|
||||||
export const cons: <T>(x: Lazy<T>) => (xs: Lazy<List<T>>) => Lazy<List<T>> |
export const cons: <T extends V>(x: T) => (xs: List<T>) => List<T> |
||||||
= Cons; |
= Cons; |
||||||
|
|
||||||
export const head: <T>(xs: Lazy<List<T>>) => Lazy<T> |
export const head: <T extends V>(xs: List<T>) => T |
||||||
= xs => xs.then(xs => xs(undef)(cnst)); |
= xs => xs.then(xs => xs(undef)(cnst)); |
||||||
|
|
||||||
export const tail: <T>(xs: Lazy<List<T>>) => Lazy<List<T>> |
export const tail: <T extends V>(xs: List<T>) => List<T> |
||||||
= xs => xs.then(xs => xs(undef)(_ => id)); |
= xs => xs.then(xs => xs(undef)(_ => id)); |
||||||
|
|
||||||
export const foldl: <T, R>(f: (a: Lazy<R>) => (x: Lazy<T>) => Lazy<R>) => (z: Lazy<R>) => (xs: Lazy<List<T>>) => Lazy<R> |
export const foldl: <T extends V, R extends V>(f: (a: R) => (x: T) => R) => (z: R) => (xs: List<T>) => R |
||||||
= f => z => xs => xs.then(xs => xs(z)(x => xs => foldl(f)(f(z)(x))(xs))); |
= f => z => xs => xs.then(xs => xs(z)(x => xs => foldl(f)(f(z)(x))(xs))); |
||||||
|
|
||||||
export const foldlz: <T>(f: (a: Lazy<T>) => (x: Lazy<T>) => Lazy<T>) => (xs: Lazy<List<T>>) => Lazy<T> |
export const foldlz: <T extends V>(f: (a: T) => (x: T) => T) => (xs: List<T>) => T |
||||||
= f => xs => xs.then(xs => xs(undef)(foldl(f))); |
= f => xs => xs.then(xs => xs(undef)(foldl(f))); |
||||||
|
|
||||||
export const foldr: <T, R>(f: (x: Lazy<T>) => (a: Lazy<R>) => Lazy<R>) => (z: Lazy<R>) => (xs: Lazy<List<T>>) => Lazy<R> |
export const foldr: <T extends V, R extends V>(f: (x: T) => (a: R) => R) => (z: R) => (xs: List<T>) => R |
||||||
= f => z => xs => xs.then(xs => xs(z)(x => xs => f(x)(foldr(f)(z)(xs)))); |
= f => z => xs => xs.then(xs => xs(z)(x => xs => f(x)(foldr(f)(z)(xs)))); |
||||||
|
|
||||||
export const foldrz: <T>(f: (x: Lazy<T>) => (a: Lazy<T>) => Lazy<T>) => (xs: Lazy<List<T>>) => Lazy<T> |
export const foldrz: <T extends V>(f: (x: T) => (a: T) => T) => (xs: List<T>) => T |
||||||
= _ => _ => undef; |
= _ => _ => undef; |
||||||
|
|
||||||
export const map: <T, R>(f: (x: Lazy<T>) => Lazy<R>) => (xs: Lazy<List<T>>) => Lazy<List<R>> |
export const map: <T extends V, R extends V>(f: (x: T) => R) => (xs: List<T>) => List<R> |
||||||
= f => xs => xs.then(xs => xs(Nil)(x => xs => Cons(f(x))(map(f)(xs)))); |
= f => xs => xs.then(xs => xs(Nil)(x => xs => Cons(f(x))(map(f)(xs)))); |
||||||
|
|
||||||
export const filter: <T>(f: (x: Lazy<T>) => Lazy<Bool>) => (xs: Lazy<List<T>>) => Lazy<List<T>> |
export const filter: <T extends V>(f: (x: T) => Bool) => (xs: List<T>) => List<T> |
||||||
= <T>(f: (x: Lazy<T>) => Lazy<Bool>) => (xs: Lazy<List<T>>) => xs.then(xs => xs(Nil)(x => xs => iif<List<T>>(f(x))(Cons(x)(filter(f)(xs)))(filter(f)(xs)))); |
= <T extends V>(f: (x: T) => Bool) => (xs: List<T>) => xs.then(xs => xs(Nil)(x => xs => iif<List<T>>(f(x))(Cons(x)(filter(f)(xs)))(filter(f)(xs)))); |
||||||
|
|
||||||
export const fromArray: <T>(xs: T[]) => Lazy<List<T>> |
export const fromArray: <T extends V>(xs: T[]) => List<T> |
||||||
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(_(xs[0]))(fromArray(xs.slice(1))).value); |
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(xs[0])(fromArray(xs.slice(1))).value); |
||||||
|
|
||||||
export const fromNumberArray: (xs: number[]) => Lazy<List<Num>> |
export const fromNumberArray: (xs: number[]) => List<Num> |
||||||
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(fromNumber(xs[0]))(fromNumberArray(xs.slice(1))).value); |
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(fromNumber(xs[0]))(fromNumberArray(xs.slice(1))).value); |
||||||
|
|
||||||
export const toArray: <T>(xs: Lazy<List<T>>) => Lazy<T[]> |
export const toArray: <R, T extends Lazy<R>>(xs: List<T>) => R[] |
||||||
= <T>(xs: Lazy<List<T>>) => xs.then(xs => xs(_([] as T[]))(x => xs => _([x.value, ...toArray(xs).value]))); |
= <R, T extends Lazy<R>>(xs: List<T>) => xs.then(xs => xs(_([] as R[]))(x => xs => _([x.value, ...toArray<R, T>(xs)]))).value; |
||||||
|
|
||||||
export const toNumberArray: (xs: Lazy<List<Num>>) => Lazy<number[]> |
export const toNumberArray: (xs: List<Num>) => number[] |
||||||
= xs => xs.then(xs => xs(_([] as number[]))(x => xs => _([toNumber(x).value, ...toNumberArray(xs).value]))); |
= xs => xs.then(xs => xs(_([] as number[]))(x => xs => _([toNumber(x), ...toNumberArray(xs)]))).value; |
||||||
|
|||||||
@ -1,40 +1,40 @@ |
|||||||
import { Bool, not, True } from './bool'; |
import { Bool, not, True } from './bool'; |
||||||
import { Lazy, _, id } from './core'; |
import { Lazy, V, _, id } from './core'; |
||||||
|
|
||||||
export type Num = <T>(z: Lazy<T>) => (n: (p: Lazy<Num>) => Lazy<T>) => Lazy<T>; |
export type Num = Lazy<<T extends V>(z: T) => (n: (p: Num) => T) => T>; |
||||||
|
|
||||||
export const Zero: Lazy<Num> |
export const Zero: Num |
||||||
= _<Num>(z => _ => z); |
= _(z => _ => z) as Num; |
||||||
|
|
||||||
export const Succ: (p: Lazy<Num>) => Lazy<Num> |
export const Succ: (p: Num) => Num |
||||||
= p => _(_ => n => n(p)); |
= p => _(_ => n => n(p)); |
||||||
|
|
||||||
export const ifz: <T>(n: Lazy<Num>) => (t: Lazy<T>) => (f: Lazy<T>) => Lazy<T> |
export const ifz: <T extends V>(n: Num) => (t: T) => (f: T) => T |
||||||
= n => t => f => n.then(n => n(t)(_ => f)); |
= n => t => f => n.then(n => n(t)(_ => f)); |
||||||
|
|
||||||
export const succ: (n: Lazy<Num>) => Lazy<Num> |
export const succ: (n: Num) => Num |
||||||
= Succ; |
= Succ; |
||||||
|
|
||||||
export const pred: (n: Lazy<Num>) => Lazy<Num> |
export const pred: (n: Num) => Num |
||||||
= n => n.then(n => n(Zero)(id)); |
= n => n.then(n => n(Zero)(id)); |
||||||
|
|
||||||
export const even: (n: Lazy<Num>) => Lazy<Bool> |
export const even: (n: Num) => Bool |
||||||
= n => n.then(n => n(True)(odd)); |
= n => n.then(n => n(True)(odd)); |
||||||
|
|
||||||
export const odd: (n: Lazy<Num>) => Lazy<Bool> |
export const odd: (n: Num) => Bool |
||||||
= n => not(even(n)); |
= n => not(even(n)); |
||||||
|
|
||||||
export const sum: (l: Lazy<Num>) => (r: Lazy<Num>) => Lazy<Num> |
export const sum: (l: Num) => (r: Num) => Num |
||||||
= l => r => r.then(r => r(l)(sum(Succ(l)))); |
= l => r => r.then(r => r(l)(sum(Succ(l)))); |
||||||
|
|
||||||
export const sub: (l: Lazy<Num>) => (r: Lazy<Num>) => Lazy<Num> |
export const sub: (l: Num) => (r: Num) => Num |
||||||
= l => r => r.then(r => r(l)(pr => l.then(l => l(Zero)(pl => sub(pl)(pr))))); |
= l => r => r.then(r => r(l)(pr => l.then(l => l(Zero)(pl => sub(pl)(pr))))); |
||||||
|
|
||||||
export const mul: (l: Lazy<Num>) => (r: Lazy<Num>) => Lazy<Num> |
export const mul: (l: Num) => (r: Num) => Num |
||||||
= l => r => l.then(l => l(Zero)(pl => sum(r)(mul(pl)(r)))); |
= l => r => l.then(l => l(Zero)(pl => sum(r)(mul(pl)(r)))); |
||||||
|
|
||||||
export const fromNumber: (n: number) => Lazy<Num> |
export const fromNumber: (n: number) => Num |
||||||
= n => !n ? Zero : new Lazy(() => Succ(fromNumber(n - 1)).value); |
= n => !n ? Zero : new Lazy(() => Succ(fromNumber(n - 1)).value); |
||||||
|
|
||||||
export const toNumber: (n: Lazy<Num>) => Lazy<number> |
export const toNumber: (n: Num) => number |
||||||
= n => n.then(n => n(_(0))(p => toNumber(p).then(p => _(1 + p)))); |
= n => n.then(n => n(_(0))(p => _(1 + toNumber(p)))).value; |
||||||
|
|||||||
Loading…
Reference in new issue