text stringlengths 12 786k |
|---|
let ceil_div a b = 1 + ( a - 1 ) / b |
let _BRANCHING = 1 lsl _BITS |
let _SKIP_SIZE = _BRANCHING - ceil_div _EXTRA_STEPS 2 |
let check_depth = let max_depth = if Sys . int_size = 31 then 6 else 12 in fun d -> if d > max_depth then failwith " clarity - vector - too - large " |
type ' a t = | Leaf of ' a array | R_node of int array * ' a t array | B_node of ' a t array |
let depth x = let rec loop : type a . int -> a t -> int = fun a -> function | Leaf _ -> a | R_node ( _ , n ) | B_node n -> assert ( Arr . len n > 0 ) ; loop ( a + 1 ) ( Arr . get n 0 ) in loop 0 x |
let rec length = function | Leaf x -> Arr . len x | R_node ( i , v ) -> assert ( Arr . len i = Arr . len v ) ; Arr . get i ( Arr . len i - 1 ) | B_node v as node -> assert ( Arr . len v > 0 ) ; let d = depth node in check_depth d ; let item_sz = 1 lsl ( d * _BITS )... |
let update_lengths = function | Leaf _ | B_node _ -> ( ) | R_node ( is , vs ) -> assert ( Arr . len is = Arr . len vs ) ; let sum = ref 0 in for i = 0 to Arr . len vs - 1 do sum := ! sum + length ( Arr . get vs i ) ; Arr . set is i ! sum done |
let mk_rnode arr = let res = R_node ( Arr . make ( Arr . len arr ) 0 , arr ) in update_lengths res ; res |
let rr_search : int array -> int -> int -> int * int = fun sizes depth idx -> assert ( Arr . len sizes > 0 ) ; assert ( idx <= Arr . get sizes ( Arr . len sizes - 1 ) ) ; check_depth depth ; let start = idx lsr ( _BITS * depth ) in assert ( start < Arr . len sizes ) ; ... |
let radix_search : int -> int -> int * int = fun depth idx -> check_depth depth ; let shift = _BITS * depth in let slot = idx lsr shift in slot , idx - slot lsl shift |
let max_nodes_allowed subnodes = _EXTRA_STEPS + ( subnodes - 1 ) / _BRANCHING + 1 |
let empty = Leaf [ ] || |
let get_leaf = function | Leaf x -> x | _ -> assert false |
let get_rnode = function | R_node ( i , v ) -> i , v | B_node v -> let sizes = Arr . make ( Arr . len v ) 0 in update_lengths ( R_node ( sizes , v ) ) ; sizes , v | _ -> assert false |
let get_bnode = function | B_node x -> x | _ -> assert false |
let node_len : type a . a t -> int = function | Leaf x -> Arr . len x | R_node ( _ , x ) | B_node x -> Arr . len x |
module Concatenation = struct let assign_subnode_lengths : type a . a t AP . t -> int AP . t -> unit = fun src dst -> let dst_idx = ref 0 in let src_idx = ref 0 in let size = ref 0 in let not_done = let need = AP . len src - AP . len dst in fun ( ) -> let need = if ! size = 0 then need... |
let cons x v = append ( Leaf [ | x ] ) | v |
let snoc v x = append v ( Leaf [ | x ] ) | |
let check_bounds n index = let size = length n in if size - 1 < index || index < 0 then raise ( Out_of_bounds { index ; size } ) |
let get : type a . a t -> int -> a = fun n i -> check_bounds n i ; let rec loop n i = function | 0 -> Arr . get ( get_leaf n ) i | d -> begin match n with | Leaf _ -> assert false | R_node ( is , vs ) -> let slot , new_i = rr_search is d i in loop ( Arr . get vs slot ) new_i ( ... |
let update : type a . a t -> int -> a -> a t = fun n i x -> check_bounds n i ; let rec loop n i = function | 0 -> let res = Arr . copy ( get_leaf n ) in Arr . set res i x ; Leaf res | d -> begin match n with | Leaf _ -> assert false | R_node ( is , vs ) -> let slot , new_i = rr_... |
let split_at : type a . a t -> int -> a t * a t = fun n i -> let size = length n in if i < 0 then raise ( Out_of_bounds { index = i ; size } ) ; let rec loop n i = function | 0 -> begin match i with | 0 -> None , Some n | i when node_len n = i -> Some n , None | i -> let a = get... |
let take : type a . a t -> int -> a t = fun n i -> let sz = length n in fst ( split_at n ( if i > sz then sz else i ) ) |
let drop : type a . a t -> int -> a t = fun n i -> let sz = length n in snd ( split_at n ( if i > sz then sz else i ) ) |
let rec iter f = function | Leaf x -> Arr . iter f x | R_node ( _ , x ) | B_node x -> Arr . iter ( iter f ) x |
module Builder = struct type ' a vector = ' a t type ' a chunk = { mutable cnt : int ; vec : ' a vector } type ' a t = ' a chunk list ref let copy : ' a t -> ' a t = fun x -> match ! x with | [ ] -> ref [ ] | { cnt ; vec } :: t -> ref ( { cnt = cnt ; vec = vec... |
let init : type a . int -> ( int -> a ) -> a t = fun l f -> let b = Builder . empty ( ) in for i = 1 to l do Builder . put b ( f i ) done ; Builder . result b type nonrec ' a t = ' a t let pure x = Leaf [ | x ] | let bind f x = let b = Builder . empty ( ) in iter ( f... |
let rec foldr ' f a = function | Leaf x -> Arr . foldr ' f a x | R_node ( _ , x ) | B_node x -> Arr . foldr ' ( fun x a -> foldr ' f a x ) a x |
let to_list x = foldr ' Clarity_list . _Cons [ ] x |
let of_list x = let b = Builder . empty ( ) in Clarity_list . iter ( Builder . put b ) x ; Builder . result b type nonrec ' a t = ' a t let align_as both left right a b = let la = length a in let lb = length b in let build = Builder . empty ( ) in for i = 0 to min la lb - 1 do Bui... |
module A3 ( A : Applicative . Basic3 ) = Traversable . Make3 ( struct type nonrec ' a t = ' a t type ( ' u , ' v , ' a ) f = ( ' u , ' v , ' a ) A . t module Ap = Applicative . Make3 ( A ) let traverse f x = let cf x l = let open ! Ap in ap ( map ( fun h t ... |
module A2 ( A : Applicative . Basic2 ) = A3 ( struct type ( _ , ' p , ' a ) t = ( ' p , ' a ) A . t include ( A : Applicative . Basic2 with type ( ' p , ' a ) t := ( ' p , ' a ) A . t ) end ) |
module A ( A : Applicative . Basic ) = A2 ( struct type ( _ , ' a ) t = ' a A . t include ( A : Applicative . Basic with type ' a t := ' a A . t ) end ) |
module M3 ( M : Monad . Basic3 ) = struct include A3 ( M ) let foldr_m f a l = let g k x z = M . bind k ( f x z ) in foldl g M . pure l a let foldl_m f a l = let g x k z = M . bind ( fun x -> k ( ) x ) ( f z x ) in foldr g ( const M . pure ) l a end |
module M2 ( M : Monad . Basic2 ) = M3 ( struct type ( _ , ' p , ' a ) t = ( ' p , ' a ) M . t include ( M : Monad . Basic2 with type ( ' p , ' a ) t := ( ' p , ' a ) M . t ) end ) |
module M ( M : Monad . Basic ) = M2 ( struct type ( _ , ' a ) t = ' a M . t include ( M : Monad . Basic with type ' a t := ' a M . t ) end ) |
module type Vector_base = sig type t type elt type index val fold_index : ( index -> elt -> ' a ) -> ( ' a -> index -> elt -> ' a ) -> t -> ' a val fold_index_2 : ( index -> elt -> elt -> ' a ) -> ( ' a -> index -> elt -> elt -> ' a ) -> t -> t -> ' a val map : ( ... |
module Vect2 = struct type t = float * float type elt = float type index = Fst | Snd let fold_index init f ( fst , snd ) = f ( init Fst fst ) Snd snd let fold_index_2 init f ( fst1 , snd1 ) ( fst2 , snd2 ) = f ( init Fst fst1 fst2 ) Snd snd1 snd2 let map f ( fst , snd ) = ( f ... |
module Vect3 = struct type t = float * float * float type elt = float type index = Fst | Snd | Trd let fold_index init f ( fst , snd , trd ) = f ( f ( init Fst fst ) Snd snd ) Trd trd let fold_index_2 init f ( fst1 , snd1 , trd1 ) ( fst2 , snd2 , trd2 ) = f ( f ( init Fst f... |
module Vect2_record = struct type t = { x : float ; y : float } type elt = float type index = X | Y let fold_index init f { x ; y } = f ( init X x ) Y y let fold_index_2 init f v1 v2 = f ( init X v1 . x v2 . x ) Y v1 . y v2 . y let map f { x ; y } = { x = f X x ; y = f ... |
module Vect3_record = struct type t = { x : float ; y : float ; z : float } type elt = float type index = X | Y | Z let fold_index init f { x ; y ; z } = f ( f ( init X x ) Y y ) Z z let fold_index_2 init f v1 v2 = f ( f ( init X v1 . x v2 . x ) Y v1 . y v2 . y ) Z v1 ... |
module Vect_array = struct type t = float array type elt = float type index = int let fold_index init f a = if Array . length a = 0 then invalid_arg " fold_index " ; let r = ref ( init 0 ( Array . unsafe_get a 0 ) ) in for i = 1 to Array . length a - 1 do r := f ! r i ( Array . u... |
module Vector_operations ( V : Vector_base with type elt = float ) = struct type elt = V . elt type t = V . t let norm v = let sum_sq = V . fold_index ( fun _ elt -> elt . * elt ) ( fun acc _ elt -> acc . + elt . * elt ) v in sqrt sum_sq let scale s v = V . map ( fun _ x -> ... |
type step = { obs : Tensor . t ; reward : Tensor . t ; is_done : Tensor . t } |
type t = { envs : Pytypes . pyobject ; np : Pytypes . pyobject } |
let create str ~ num_processes = if not ( Py . is_initialized ( ) ) then ( Py . add_python_path " examples / reinforcement - learning " ; Py . initialize ( ) ) ; let wrappers = Py . import " atari_wrappers " in let envs = Py . Module . get_function wrappers " make " [ ... |
let to_tensor t np_array = let np_array = Py . Module . get_function t . np " ascontiguousarray " [ | np_array ] | in Py . Object . call_method np_array " astype " [ | Py . Module . get t . np " float32 " ] | |> Numpy . to_bigarray Float32 C_layout |> Tensor . of_bigarray... |
let reset t = let reset_fn = Py . Object . get_attr_string t . envs " reset " in Py . Callable . to_function ( Option . value_exn reset_fn ) [ ] || |> to_tensor t |
let step t ~ actions = let v = Py . Object . call_method t . envs " step " [ | Py . List . of_list_map Py . Int . of_int actions ] | in let obs , reward , is_done , _ = Py . Tuple . to_tuple4 v in { obs = to_tensor t obs ; reward = to_tensor t reward ; is_done = to_tensor... |
let action_space t = let action_space = Option . value_exn ( Py . Object . get_attr_string t . envs " action_space " ) in Option . value_exn ( Py . Object . get_attr_string action_space " n " ) |> Py . Int . to_int |
module type ResizeType = sig type t val null : t end |
module type S = sig type elt type t val length : t -> int val compact : t -> unit val singleton : elt -> t val empty : unit -> t val make : int -> t val init : int -> ( int -> elt ) -> t val is_empty : t -> bool val of_sub_array : elt array -> int -> int -> t val unsafe_internal_array : t -> ... |
let err_argv = " argv array must have at least one element " |
let err_not_opt = " Option argument without name " |
let err_not_pos = " Positional argument with a name " |
let err_help s = " Term error , help requested for unknown command " ^ s |
let err_empty_list = " Empty list " |
let err_incomplete_enum = " Incomplete enumeration for the type " |
let err_doc_string s = str " Variable substitution failed on documentation fragment ` % s ' " s |
let rev_compare n n ' = compare n ' n |
let str_of_pp pp v = pp Format . str_formatter v ; Format . flush_str_formatter ( ) |
let quote s = str " ` % s ' " s |
let alts_str ( ? quoted = true ) alts = let quote = if quoted then quote else ( fun s -> s ) in match alts with | [ ] -> invalid_arg err_empty_list | [ a ] -> ( quote a ) | [ a ; b ] -> str " either % s or % s " ( quote a ) ( quote b ) | alts -> let rev_alts = List ... |
let pr_white_str spaces ppf s = let left = ref 0 and right = ref 0 and len = String . length s in let flush ( ) = Format . pp_print_string ppf ( String . sub s ! left ( ! right - ! left ) ) ; incr right ; left := ! right ; in while ( ! right <> len ) do if s . [ ! right ]... |
let pr_text = pr_white_str true |
let pr_lines = pr_white_str false |
let pr_to_temp_file pr v = try let exec = Filename . basename Sys . argv . ( 0 ) in let file , oc = Filename . open_temp_file exec " out " in let ppf = Format . formatter_of_out_channel oc in pr ppf v ; Format . pp_print_flush ppf ( ) ; close_out oc ; at_exit ( fun ( ) -> try... |
let levenshtein_distance s t = let minimum a b c = min a ( min b c ) in let m = String . length s in let n = String . length t in let d = Array . make_matrix ( m + 1 ) ( n + 1 ) 0 in for i = 0 to m do d . ( i ) . ( 0 ) <- i done ; for j = 0 to n do d . ( 0 ) . ( ... |
let suggest s candidates = let add ( min , acc ) name = let d = levenshtein_distance s name in if d = min then min , ( name :: acc ) else if d < min then d , [ name ] else min , acc in let dist , suggs = List . fold_left add ( max_int , [ ] ) candidates in if dist < 3 then sug... |
module Trie : sig type ' a t val empty : ' a t val is_empty : ' a t -> bool val add : ' a t -> string -> ' a -> ' a t val find : ' a t -> string -> [ ` Ok of ' a | ` Ambiguous | ` Not_found ] val ambiguities : ' a t -> string -> string list val of_list : ( string * ' a ... |
type env_info = { env_var : string ; env_doc : string ; env_docs : string ; } |
type absence = | Error | Val of string Lazy . t |
type opt_kind = | Flag | Opt | Opt_vopt of string |
type pos_kind = | All | Nth of bool * int | Left of bool * int | Right of bool * int |
type arg_info = { id : int ; absent : absence ; env_info : env_info option ; doc : string ; docv : string ; docs : string ; p_kind : pos_kind ; o_kind : opt_kind ; o_names : string list ; o_all : bool ; } |
let arg_id = let c = ref 0 in fun ( ) -> let id = ! c in incr c ; if id > ! c then assert false else id |
let is_opt a = a . o_names <> [ ] |
let is_pos a = a . o_names = [ ] |
module Amap = Map . Make ( struct type t = arg_info let compare a a ' = compare a . id a ' . id end ) |
type arg = | O of ( int * string * ( string option ) ) list | P of string list |
type cmdline = arg Amap . t |
type man_block = [ | ` S of string | ` P of string | ` Pre of string | ` I of string * string | ` Noblank ] |
type term_info = { name : string ; version : string option ; tdoc : string ; tdocs : string ; sdocs : string ; man : man_block list ; } |
type eval_info = { term : term_info * arg_info list ; main : term_info * arg_info list ; choices : ( term_info * arg_info list ) list ; env : string -> string option } |
let eval_kind ei = if ei . choices = [ ] then ` Simple else if ( fst ei . term ) == ( fst ei . main ) then ` M_main else ` M_choice |
module Manpage = struct type title = string * int * string * string * string type block = man_block type t = title * block list let p_indent = 7 let l_indent = 4 let escape subst esc buf s = let subst s = let len = String . length s in if not ( len > 1 && s . [ 1 ] = ' , ' ) then ... |
module Help = struct let invocation ( ? sep = ' ' ) ei = match eval_kind ei with | ` Simple | ` M_main -> ( fst ei . main ) . name | ` M_choice -> str " % s % c % s " ( fst ei . main ) . name sep ( fst ei . term ) . name let title ei = let prog = String . capita... |
module Err = struct let invalid kind s exp = str " invalid % s % s , % s " kind ( quote s ) exp let invalid_val = invalid " value " let no kind s = str " no % s % s " ( quote s ) kind let not_dir s = str " % s is not a directory " ( quote s ) let is_dir s = str " % s is a di... |
module Cmdline : sig exception Error of string val choose_term : term_info -> ( term_info * ' a ) list -> string list -> term_info * string list val create : ? peek_opts : bool -> arg_info list -> string list -> cmdline val opt_arg : cmdline -> arg_info -> ( int * string * ( string option ) ... |
module Arg = struct type ' a parser = string -> [ ` Ok of ' a | ` Error of string ] type ' a printer = Format . formatter -> ' a -> unit type ' a converter = ' a parser * ' a printer type env = env_info type ' a arg_converter = ( eval_info -> cmdline -> ' a ) type ' a t =... |
module Term = struct type info = term_info type ' + a t = arg_info list * ( eval_info -> cmdline -> ' a ) type ' a result = [ | ` Ok of ' a | ` Error of [ ` Parse | ` Term | ` Exn ] | ` Version | ` Help ] exception Term of [ ` Help of [ ` Pager | ` Plain | ` ... |
module List = struct include List let map f l = List . rev_map f l |> List . rev let rev_split l = let rec inner xs ys = function | ( x , y ) :: xys -> inner ( x :: xs ) ( y :: ys ) xys | [ ] -> ( xs , ys ) in inner [ ] [ ] l let split l = rev_split ( List . rev l ) ... |
type wrap = [ | ` Wrap_atoms | ` Always_wrap | ` Never_wrap | ` Force_breaks | ` Force_breaks_rec | ` No_breaks ] |
type label_break = [ | ` Auto | ` Always | ` Always_rec | ` Never ] |
type style = { tag_open : string ; tag_close : string } |
type atom_param = { atom_style : style_name option ; } |
let atom = { atom_style = None } |
type list_param = { space_after_opening : bool ; space_after_separator : bool ; space_before_separator : bool ; separators_stick_left : bool ; space_before_closing : bool ; stick_to_label : bool ; align_closing : bool ; wrap_body : wrap ; indent_body : int ; list_style : style_name option... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.