Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Created November 14, 2015 00:38
Show Gist options
  • Save samwgoldman/4770329618cdbaa1b2bd to your computer and use it in GitHub Desktop.
Save samwgoldman/4770329618cdbaa1b2bd to your computer and use it in GitHub Desktop.
First pass at Flow interface file for jsverify
declare module "jsverify" {
// Either
declare interface Either<A,B> {
either<T>(l: (a: A) => T, r: (b: B) => T): T;
isEqual(other: Either<A,B>): boolean;
bimap<C,D>(f: (a: A) => C, g: (b: B) => D): Either<C,D>;
first<C>(f: (a: A) => C): Either<C,B>;
second<D>(g: (b: B) => D): Either<A,D>;
}
declare var either: {
left<A>(value: A): Either<A,mixed>;
right<B>(value: B): Either<mixed,B>;
}
// Random
declare var random: {
(min: number, max: number): number;
integer(min: number, max: number): number;
number(min: number, max: number): number;
}
// Generator
declare interface Generator<T> {
(size: number): T;
map<U>(f: (x: T) => U): Generator<U>;
flatmap<U>(f: (x: T) => Generator<U>): Generator<U>;
}
declare var generator: {
bless<T>(f: (n: number) => T): Generator<T>;
constant<T>(x: T): Generator<T>;
combine<T,U>(a: Generator<T>, f: (x: T) => U): Generator<U>;
oneof<T>(gens: Generator<T>[]): Generator<T>;
recursive<T>(genZ: Generator<T>, genS: (gen: Generator<T>) => Generator<T>): Generator<T>;
pair<A,B>(genA: Generator<A>, genB: Generator<B>): Generator<[A,B]>;
either<A,B>(genA: Generator<A>, genB: Generator<B>): Generator<Either<A,B>>;
unit: Generator<[]>;
tuple<A,B,C,D,E>(gens: [Generator<A>, Generator<B>, Generator<C>, Generator<D>, Generator<E>]): Generator<[A,B,C,D,E]>;
sum<A,B,C,D,E>(gens: [Generator<A>, Generator<B>, Generator<C>, Generator<D>, Generator<E>]): Generator<A|B|C|D|E>;
array<T>(gen: Generator<T>): Generator<T[]>;
nearray<T>(gen: Generator<T>): Generator<T[]>;
dict<T>(gen: Generator<T>): Generator<{[k: string]: T}>;
small<T>(gen: Generator<T>): Generator<T>;
}
// Show
declare interface Show<T> {
(x: T): string;
}
declare var show: {
def(x: any): string;
pair<A,B>(a: Show<A>, b: Show<B>): Show<[A,B]>;
either<A,B>(a: Show<A>, b: Show<B>): Show<Either<A,B>>;
tuple<A,B,C,D,E>(ss: [Show<A>, Show<B>, Show<C>, Show<D>, Show<E>]): Show<[A,B,C,D,E]>;
sum<A,B,C,D,E>(ss: [Show<A>, Show<B>, Show<C>, Show<D>, Show<E>]): Show<A|B|C|D|E>;
array<T>(s: Show<T>): Show<T[]>;
}
// Shrink
declare interface Shrink<T> {
(x: T): T[];
smap<U>(f: (x: T) => U, g: (x: U) => T): Shrink<U>;
}
declare var shrink: {
bless<T>(f: (x: T) => T[]): Shrink<T>;
noop: Shrink<mixed>;
pair<A,B>(a: Shrink<A>, b: Shrink<B>): Shrink<[A,B]>;
either<A,B>(a: Shrink<A>, b: Shrink<B>): Shrink<Either<A,B>>;
tuple<A,B,C,D,E>(ss: [Shrink<A>, Shrink<B>, Shrink<C>, Shrink<D>, Shrink<E>]): Shrink<[A,B,C,D,E]>;
sum<A,B,C,D,E>(ss: [Shrink<A>, Shrink<B>, Shrink<C>, Shrink<D>, Shrink<E>]): Shrink<A|B|C|D|E>;
array<T>(s: Shrink<T>): Shrink<T[]>;
nearray<T>(s: Shrink<T>): Shrink<T[]>;
}
// Arbitrary
declare interface Arbitrary<T> {
generator: Generator<T>;
shrink: Shrink<T>;
show: Show<T>;
smap<U>(f: (x: T) => U, g: (x: U) => T, newShow?: Show<U>): Arbitrary<U>;
}
declare var arbitrary: {
bless<T>(arb: { generator: Generator<T>, shrink: Shrink<T>, show: Show<T> }): Arbitrary<T>;
}
// Primitives
declare var integer: Arbitrary<number> & {
(maxSize: number, _: void): Arbitrary<number>,
(minSize: number, maxSize: number): Arbitrary<number>
};
declare var nat: Arbitrary<number> & {
(maxSize: number): Arbitrary<number>,
};
declare var number: Arbitrary<number> & {
(maxSize: number, _: void): Arbitrary<number>,
(minSize: number, maxSize: number): Arbitrary<number>
};
declare var uint8: Arbitrary<number>;
declare var uint16: Arbitrary<number>;
declare var uint32: Arbitrary<number>;
declare var int8: Arbitrary<number>;
declare var int16: Arbitrary<number>;
declare var int32: Arbitrary<number>;
declare var bool: Arbitrary<boolean>;
declare var datetime: Arbitrary<Date>;
declare function elements<T>(args: T[]): Arbitrary<T>;
declare var falsey: Arbitrary<mixed>;
declare function constant<T>(x: T): Arbitrary<T>;
// Combinators
declare function nonshrink<T>(arb: Arbitrary<T>): Arbitrary<T>;
declare var unit: Arbitrary<[]>;
declare function either<A,B>(a: Arbitrary<A>, b: Arbitrary<B>): Arbitrary<Either<A,B>>;
declare function pair<A,B>(a: Arbitrary<A>, b: Arbitrary<B>): Arbitrary<[A,B]>;
declare function tuple<A,B,C,D,E>(arbs: [Arbitrary<A>, Arbitrary<B>, Arbitrary<C>, Arbitrary<D>, Arbitrary<E>]): Arbitrary<[A,B,C,D,E]>;
declare function sum<A,B,C,D,E>(arbs: [Arbitrary<A>, Arbitrary<B>, Arbitrary<C>, Arbitrary<D>, Arbitrary<E>]): Arbitrary<A|B|C|D|E>;
declare function dict<T>(arb: Arbitrary<T>): Arbitrary<{[k:string]: T}>;
declare function array<T>(arb: Arbitrary<T>): Arbitrary<T[]>;
declare function nearray<T>(arb: Arbitrary<T>): Arbitrary<T[]>;
declare var json: Arbitrary<any>; // TODO JSON type
declare function oneof<T>(arbs: Arbitrary<T>[]): Arbitrary<T>;
declare function small<T>(arb: Arbitrary<T>): Arbitrary<T>;
// TODO: Records. Flow isn't good at typing this kind of function.
// Strings
declare var char: Arbitrary<string>;
declare var asciichar: Arbitrary<string>;
declare var string: Arbitrary<string>;
declare var nestring: Arbitrary<string>;
declare var asciistring: Arbitrary<string>;
declare var asciinestring: Arbitrary<string>;
// Functions
declare function fn<T>(arb: Arbitrary<T>): Arbitrary<() => T>;
declare function fun<T>(arb: Arbitrary<T>): Arbitrary<() => T>;
// Conditional
declare function suchthat<T>(
arb: Arbitrary<T>,
pred: (x: T) => boolean
): Arbitrary<T>;
// Properties
declare type Property<T> = {
(size: number): CheckResult<T>;
}
declare type CheckOptions = {
size?: number,
tests?: number,
quiet?: boolean,
rngState?: string
}
declare type CheckResult<T> = true | {
counterexample: T,
counterexamplestr: string,
tests: number,
shrinks: number,
exc: ?Error,
rngState: string
}
// TODO forall has a vararg signature, but representing such a
// type in Flow is difficult. Pass a single tuple instead.
declare function forall<T>(
arb: Arbitrary<T>,
f: (x: T) => boolean
): Property<T>;
declare function check<T>(prop: Property<T>, opts?: CheckOptions): CheckResult<T>;
declare function assert(prop: Property, opts?: CheckOptions): void;
declare type Sampler<T>
= (sampleSize: number) => T[]
& (_: void) => T;
declare function sampler<T>(arb: Arbitrary<T>, genSize?: number): Sampler<T>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment