text
stringlengths
0
601k
let elements : ' a list -> ' a gen = fun xs -> map_gen ( List . nth xs ) ( choose_int0 ( List . length xs ) )
let vector : ' a gen -> int -> ' a list gen = fun ( Gen gelt ) l -> Gen ( fun n -> let rec gen acc = function 0 -> acc | l -> gen ( gelt n :: acc ) ( l - 1 ) in gen [ ] l )
let oneof : ' a gen list -> ' a gen = fun gens -> elements gens >>= fun x -> x
module type ARBITRARY = sig type t val arbitrary : t gen end
module Arbitrary_unit = struct type t = unit let arbitrary = ret_gen ( ) end
module Arbitrary_bool = struct type t = bool let arbitrary = elements [ true ; false ] end
module Arbitrary_char = struct type t = char let arbitrary = choose_int ( 32 , 255 ) >>= fun c -> ret_gen ( Char . chr c ) end
module Arbitrary_int = struct type t = int let arbitrary = sized ( fun n -> choose_int ( - n , n ) ) end
module Arbitrary_float = struct type t = float let arbitrary = Arbitrary_int . arbitrary >>= fun a -> Arbitrary_int . arbitrary >>= fun b -> sized choose_int0 >>= fun c -> ret_gen ( float a . + ( float b . / ( float c . + 1 . ) ) ) end
module Aribitrary_pair ( Fst : ARBITRARY ) ( Snd : ARBITRARY ) = struct type t = Fst . t * Snd . t let arbitrary = Fst . arbitrary >>= fun v1 -> Snd . arbitrary >>= fun v2 -> ret_gen ( v1 , v2 ) end
module Aribitrary_triple ( Fst : ARBITRARY ) ( Snd : ARBITRARY ) ( Trd : ARBITRARY ) = struct type t = Fst . t * Snd . t * Trd . t let arbitrary = Fst . arbitrary >>= fun v1 -> Snd . arbitrary >>= fun v2 -> Trd . arbitrary >>= fun v3 -> ret_gen ( v1 , v2 , v3 ) end
module Arbitrary_list ( Elt : ARBITRARY ) = struct type t = Elt . t list let arbitrary = sized choose_int0 >>= vector Elt . arbitrary end
type result = { ok : bool option ; stamp : string list ; arguments : pretty_str list ; }
type property = Prop of result gen
module type TESTABLE = sig type t val property : t -> property end
let nothing : result = { ok = None ; stamp [ ] ; = arguments [ ] } =
let result : result -> property = fun res -> Prop ( ret_gen res )
module Testable_unit = struct type t = unit let property ( ) = result nothing end
module Testable_bool = struct type t = bool let property b = result { nothing with ok = Some b } end
module Testable_result = struct type t = result let property r = result r end
module Testable_property = struct type t = property let property p = p end
module Evaluate ( T : TESTABLE ) = struct let evaluate : T . t -> result gen = fun a -> let Prop gen = T . property a in gen end
module ForAll ( S : PSHOW ) ( T : TESTABLE ) = struct module E = Evaluate ( T ) let forAll : S . t gen -> ( S . t -> T . t ) -> property = fun gen body -> let argument a res = { res with arguments = S . show a :: res . arguments } in Prop ( gen >>= fun a -> E . evaluate ( body a ) >>= fun res -> ret_gen ( argument a re...
module Testable_fun ( A : ARBITRARY ) ( S : PSHOW with type t = A . t ) ( T : TESTABLE ) = struct module F = ForAll ( S ) ( T ) type t = A . t -> T . t let property : t -> property = fun f -> F . forAll A . arbitrary f end
module Implies ( T : TESTABLE ) = struct let ( ) ==> : bool -> T . t -> property = fun b a -> if b then T . property a else Testable_unit . property ( ) end
module Label ( T : TESTABLE ) = struct module E = Evaluate ( T ) let label : string -> T . t -> property = fun s a -> let add r = { r with stamp = s :: r . stamp } in let a ' = E . evaluate a in Prop ( map_gen add a ' ) end
module Classify ( T : TESTABLE ) = struct module L = Label ( T ) let classify : bool -> string -> T . t -> property = function true -> L . label | false -> fun _ -> T . property let trivial : bool -> T . t -> property = fun b -> classify b " trivial " end
module Collect ( S : SHOW ) ( T : TESTABLE ) = struct module L = Label ( T ) let collect : S . t -> T . t -> property = fun v -> L . label ( S . show v ) end
type config = { maxTest : int ; maxFail : int ; size : int -> int ; every : Format . formatter -> int * pretty_str list -> unit ; }
let quick = { maxTest = 100 ; maxFail = 1000 ; size = ( fun n -> 3 + n / 2 ) ; every = ( fun _ ( _ , _ ) -> ( ) ) }
let verbose = { quick with every = begin fun f ( n , args ) -> let pargs fmt l = List . iter ( fun a -> Format . fprintf fmt " @ % a " a ( ) ) l in Format . fprintf f " [ @% d [ :@< hov 2 >% a ] ] . " @@@ n pargs args end }
let done_ : string -> int -> string list list -> unit = fun mesg ntest stamps -> let percentage n m = Format . sprintf " % 2d " %% ( ( 100 * n ) / m ) in let entry ( n , xs ) = Format . sprintf " % s % s " ( percentage n ntest ) ( String . concat " , " xs ) in let pairLength = function ( xs :: _ ) as xss -> ( List . le...
let rec tests : config -> result gen -> int -> int -> string list list -> unit = fun config gen ntest nfail stamps -> if ntest = config . maxTest then done_ " OK , passed " ntest stamps else if nfail = config . maxFail then done_ " Arguments exhausted after " nfail stamps else begin let result = generate ( config . siz...
module Check ( T : TESTABLE ) = struct module E = Evaluate ( T ) let check : config -> T . t -> unit = fun config a -> tests config ( E . evaluate a ) 0 0 [ ] let test = check quick let quickCheck = test let verboseCheck = check verbose end
module type Conv = Conv
module type Conv_filtered = Conv_filtered
module type S = Quickcheck . S
module type S1 = Quickcheck . S1
module type S2 = Quickcheck . S2
module type S_int = Quickcheck . S_int
module Of_quickcheckable ( Quickcheckable : S ) ( Conv : Conv with type quickcheckable := Quickcheckable . t ) : S with type t := Conv . t = struct let quickcheck_generator = Quickcheck . Generator . map Quickcheckable . quickcheck_generator ~ f : Conv . of_quickcheckable ; ; let quickcheck_observer = Quickcheck . Obse...
module Of_quickcheckable_filtered ( Quickcheckable : S ) ( Conv : Conv_filtered with type quickcheckable := Quickcheckable . t ) : S with type t := Conv . t = struct let quickcheck_generator = Quickcheck . Generator . filter_map Quickcheckable . quickcheck_generator ~ f : Conv . of_quickcheckable ; ; let quickcheck_obs...
module type Conv = sig type quickcheckable type t val of_quickcheckable : quickcheckable -> t val to_quickcheckable : t -> quickcheckable end
module type Conv_filtered = sig type quickcheckable type t val of_quickcheckable : quickcheckable -> t option val to_quickcheckable : t -> quickcheckable end
module type Quickcheckable = sig module type Conv = Conv module type Conv_filtered = Conv_filtered module type S = Quickcheck . S module type S1 = Quickcheck . S1 module type S2 = Quickcheck . S2 module type S_int = Quickcheck . S_int module Of_quickcheckable ( Quickcheckable : S ) ( Conv : Conv with type quickcheckabl...
module type Generator = sig type ' + a t = ' a Generator . t val create : ( size : int -> random : Splittable_random . State . t -> ' a ) -> ' a t val generate : ' a t -> size : int -> random : Splittable_random . State . t -> ' a include Monad . S with type ' a t := ' a t include Applicative . S with type ' a t := ' a...
module type Deriving_hash = sig type t [ @@ deriving hash ] end
module type Observer = sig type ' - a t = ' a Observer . t val create : ( ' a -> size : int -> hash : Hash . state -> Hash . state ) -> ' a t val observe : ' a t -> ' a -> size : int -> hash : Hash . state -> Hash . state val of_hash : ( module Deriving_hash with type t = ' a ) -> ' a t val bool : bool t val char : cha...
module type Shrinker = sig type ' a t = ' a Shrinker . t val shrink : ' a t -> ' a -> ' a Sequence . t val create : ( ' a -> ' a Sequence . t ) -> ' a t val empty : unit -> ' a t val bool : bool t val char : char t val map : ' a t -> f ( ' : a -> ' b ) -> f_inverse ( ' : b -> ' a ) -> ' b t val filter : ' a t -> f ( ' ...
module type S = sig type t val quickcheck_generator : t Generator . t val quickcheck_observer : t Observer . t val quickcheck_shrinker : t Shrinker . t end
module type S1 = sig type ' a t val quickcheck_generator : ' a Generator . t -> ' a t Generator . t val quickcheck_observer : ' a Observer . t -> ' a t Observer . t val quickcheck_shrinker : ' a Shrinker . t -> ' a t Shrinker . t end
module type S2 = sig type ( ' a , ' b ) t val quickcheck_generator : ' a Generator . t -> ' b Generator . t -> ( ' a , ' b ) t Generator . t val quickcheck_observer : ' a Observer . t -> ' b Observer . t -> ( ' a , ' b ) t Observer . t val quickcheck_shrinker : ' a Shrinker . t -> ' b Shrinker . t -> ( ' a , ' b ) t Sh...
module type S_range = sig include S val gen_incl : t -> t -> t Generator . t val gen_uniform_incl : t -> t -> t Generator . t end
module type S_int = sig include S_range val gen_log_uniform_incl : t -> t -> t Generator . t val gen_log_incl : t -> t -> t Generator . t end
type seed = [ ` Deterministic of string | ` Nondeterministic ]
type shrink_attempts = [ ` Exhaustive | ` Limit of int ]
module type Quickcheck_config = sig val default_seed : seed val default_sizes : int Sequence . t val default_trial_count : int val default_can_generate_trial_count : int val default_shrink_attempts : shrink_attempts end
module type Quickcheck_configured = sig include Quickcheck_config val random_value : ? seed : seed -> ? size : int -> ' a Generator . t -> ' a val iter : ? seed : seed -> ? sizes : int Sequence . t -> ? trials : int -> ' a Generator . t -> f ( ' : a -> unit ) -> unit val test : ? seed : seed -> ? sizes : int Sequence ....
module type Syntax = sig module Generator : Generator module Open_on_rhs : Generator with type ' a t := ' a Generator . t and module Let_syntax := Generator . Let_syntax include Monad . Syntax with type ' a t := ' a Generator . t and module Let_syntax . Let_syntax . Open_on_rhs = Open_on_rhs end
module type Quickcheck = sig type nonrec seed = seed type nonrec shrink_attempts = shrink_attempts module Generator : Generator module Observer : Observer module Shrinker : Shrinker module type S = S module type S1 = S1 module type S2 = S2 module type S_int = S_int module type S_range = S_range include Syntax with modu...
let of_array array = Quickcheck . Generator . of_list @@ Array . to_list array
let rec map_gens ls ~ f = match ls with | [ ] -> return [ ] | h :: t -> let % bind h ' = f h in let % map t ' = map_gens t ~ f in h ' :: t '
let replicate_gen g n = map_gens ( List . init n ~ f : Fn . id ) id ~ f ( : Fn . const g ) g
let init_gen ~ f n = let rec go : ' a list -> int -> ' a list Quickcheck . Generator . t = fun xs n ' -> if n ' < n then f n ' >>= fun x -> go ( x :: xs ) xs ( n ' + 1 ) 1 else return @@ List . rev xs in go [ ] 0
let init_gen_array ~ f n = map ~ f : Array . of_list @@ init_gen ~ f n
let gen_pair g = let % map a = g and b = g in ( a , b ) b
let shuffle_arr_inplace arr = let rec go n = if n < Array . length arr then ( let % bind swap_idx = Int . gen_uniform_incl n ( Array . length arr - 1 ) 1 in Array . swap arr n swap_idx ; go ( n + 1 ) 1 ) else return arr in go 0
let shuffle_arr arr = shuffle_arr_inplace @@ Array . copy arr
let shuffle list = Array . of_list list |> shuffle_arr_inplace |> map ~ f : Array . to_list
let gen_symm_dirichlet : int -> float list Quickcheck . Generator . t = fun n -> let open Quickcheck . Generator . Let_syntax in let % map gammas = map_gens ( List . init n ~ f ( : Fn . const ( ) ) ) ~ f ( : fun _ -> let open Quickcheck . Generator . Let_syntax in let % map uniform = Float . gen_uniform_excl 0 . 1 . in...
module type Int_s = sig type t val zero : t val ( + ) : t -> t -> t val ( - ) : t -> t -> t val ( > ) : t -> t -> bool val of_int : int -> t val to_int : t -> int end
let gen_division_generic ( type t ) t ( module M : Int_s with type t = t ) t ( n : t ) t ( k : int ) int : M . t list Quickcheck . Generator . t = if k = 0 then Quickcheck . Generator . return [ ] else let open Quickcheck . Generator . Let_syntax in let % bind dirichlet = gen_symm_dirichlet k in let n_float = Float . o...
let gen_division = gen_division_generic ( module Int ) Int
let gen_division_currency = gen_division_generic ( module struct include Currency . Amount let ( + ) a b = Option . value_exn ( a + b ) b let ( - ) a b = Option . value_exn ( a - b ) b end )
let imperative_fixed_point root ~ f = let % map f ' = fixed_point f in f ' root
let gen_imperative_rose_tree ( ? p = 0 . 75 ) 75 ( root_gen : ' a t ) t ( node_gen : ( ' a -> ' a ) ' a t ) t = let % bind root = root_gen in imperative_fixed_point root ~ f ( : fun self -> match % bind size with | 0 -> return ( fun parent -> Rose_tree . T ( parent , [ ] ) ) | n -> let % bind fork_count = geometric ~ p...
let gen_imperative_ktree ( ? p = 0 . 75 ) 75 ( root_gen : ' a t ) t ( node_gen : ( ' a -> ' a ) ' a t ) t = let % bind root = root_gen in imperative_fixed_point root ~ f ( : fun self -> match % bind size with | 0 -> return ( fun _ -> [ ] ) | 1 -> let % map this = node_gen in fun parent -> [ this parent ] | n -> let % b...
let gen_imperative_list ( root_gen : ' a t ) t ( node_gen : ( ' a -> ' a ) ' a t ) t = let % bind root = root_gen in imperative_fixed_point root ~ f ( : fun self -> match % bind size with | 0 -> return ( fun _ -> [ ] ) | n -> let % bind this = node_gen in let % map f = with_size ~ size ( : n - 1 ) 1 self in fun parent ...
let open Quickcheck . Let_syntax in let quickcheck_generator = [ % map_open let triple = tuple3 Bool . quickcheck_generator Char . quickcheck_generator Float . quickcheck_generator and choice = variant2 String . quickcheck_generator Int . quickcheck_generator in [ % sexp ( triple : bool * char * float ) , ( choice : [ ...
module Test ( S : sig val default_seed : Quickcheck . seed end ) : sig end = struct let int_middle_bits = match Word_size . word_size with | W64 -> Int . of_string " 0x0000_ffff_ffff_0000 " | W32 -> Int . of_string " 0x00ff_ff00 " ; ; module Q = Quickcheck . Configure ( struct include Quickcheck include S end ) open Q ...
module Shrinker = struct open Shrinker module Test_data = struct let singleton equal min = create ( fun v -> if equal min v then Sequence . empty else Sequence . singleton min ) ; ; let % test_module " singleton " = ( module struct let t = singleton Poly . ( = ) 42 let % test_unit " singleton produces values " = let sh...
let ( ) |> x f = f x THEN object method log : string -> unit end
let console = ( Ocamljs . var " console " : console ) ENDIF THEN
module S = struct let ticks = ref 0 let maps = ref 0 let filters = ref 0 let maxs = ref 0 end ENDIF THEN
module Random = struct let int = Random . int let a = [ | 1 . ; 2 . ; 3 . ; 4 . ; 1 . ; 2 . ; 3 . ; 4 . ; 1 . ; 2 . ; 3 . ; 4 . ; 1 . ; 2 . ; 3 . ; 4 . ; 1 . ; 2 . ; 3 . ; 4 . ; 1 . ; 2 . ; 3 . ; 4 . ; ] | let ai = ref 0 let b = [ | 100 . ; 200 . ; 300 . ; 400 . ; 210 . ; 110 . ; 410 . ; 310 . ; 320 . ; 220 . ; 120 . ;...
module L = struct type ' a lst = Nil | Cons of ' a * ' a t and ' a t = ' a lst F . behavior let nil ( ) = F . return Nil let cons h t = F . return ( Cons ( h , t ) ) let rec of_list = function | [ ] -> nil ( ) | h :: t -> cons h ( of_list t ) let rec to_list l = l >>= function | Nil -> F . return [ ] | Cons ( h , t ) -...
module G = struct type point = float * float type line = point * point let compare ( ax , ay ) ( bx , by ) = match compare ax bx with | 0 -> compare ay by | c -> c let cross ( ux , uy ) ( vx , vy ) = ux . * vy . - vx . * uy let line_side ( ( ax , ay ) , ( bx , by ) ) ( cx , cy ) = let u = bx . - ax , by . - ay in let v...
module QH = struct let rec split p1 p2 l hull = l >>= function | L . Nil -> L . cons p1 hull | _ -> let line_dist = G . line_dist ( p1 , p2 ) in L . max ( fun a b -> compare ( line_dist a ) ( line_dist b ) ) l >>= fun m -> let left = L . filter ( G . above_line ( p1 , m ) ) l in let right = L . filter ( G . above_line ...
let get id = D . document # getElementById id
let onload ( ) = let min = 0 . in let max = 500 . in let ticks = Fd . ticks 20 . in let random_color ( ) = Fda . color ( Random . int 255 ) ( Random . int 255 ) ( Random . int 255 ) in let bouncing ( ) = let coord ( ) = let v = Random . float 5 . in let init = Random . float max in let collect ( p , v ) ( ) = let p = p...
type point = float * float
type line = point * point
type points = point list
let point_sub : point -> point -> point = fun p q -> ( fst p . - fst q , snd p . - snd q )
let points_distance : point -> point -> float = fun p q -> sqrt ( ( . ) + ( ( ( fst p ) . - ( fst q ) ) . * ( ( fst p ) . - ( fst q ) ) ) ( ( ( snd p ) . - ( snd q ) ) . * ( ( snd p ) . - ( snd q ) ) ) )
let magnitude : point -> float = fun p -> sqrt ( ( fst p ) . * ( fst p ) ) . + ( ( snd p ) . * snd p )
let cross_product : point -> point -> float = fun p q -> ( ( fst p ) . * ( snd q ) ) . - ( ( snd p ) . * ( fst q ) )
let line_point_distance : line -> point -> float = fun line point -> let diff1 = point_sub ( snd line ) ( fst line ) in let diff2 = point_sub ( fst line ) point in let diff3 = point_sub ( snd line ) ( fst line ) in let numer = abs_float ( cross_product diff1 diff2 ) in let denom = magnitude diff3 in numer . / denom
let line_side_test : line -> point -> bool = fun line p -> if ( fst line ) = p || ( snd line ) = p then false else let diff1 = point_sub ( snd line ) ( fst line ) in let diff2 = point_sub ( fst line ) p in let cross = cross_product diff1 diff2 in if cross <= 0 . 0 then false else true
let furthest_point_from_line : line -> points -> ( point * float ) = fun line points -> match points with | [ ] -> failwith " no points " | p :: points -> List . fold_left ( fun ( q , max_dis ) p -> let d = line_point_distance line p in if d > max_dis then p , d else q , max_dis ) ( p , line_point_distance line p ) poi...