text stringlengths 0 601k |
|---|
let rm_reify_tydecl sub tydecl = match filter_reify tydecl . ptype_attributes with | [ ] -> default_mapper . type_declaration sub tydecl | [ ( _ , PStr [ ] ) ] -> default_mapper . type_declaration rm_reify_mapper tydecl | ( { txt = _ ; loc } , _ ) :: _ -> let msg = " [ " @@^ reify_attrname " ] ^ doesn ' t take any payl... |
let new_items new_class new_repr ext_exn typ_ext reify_all inside_module module_name = let class_info cis = let cis ' = if reify_all then List . filter ( fun ci -> not ( has_dont_reify ci . pci_attributes ) ) cis else List . filter ( fun ci -> has_reify ci . pci_attributes ) cis in new_class ( List . map ( fun ci -> { ... |
let str_proj = function | Pstr_type ( r , td ) -> Type ( r , td ) | Pstr_attribute ( a , p ) -> Attr ( a , p ) | Pstr_exception e -> Exn e | Pstr_typext e -> TypExt e | Pstr_class cds -> ClassExpr cds | Pstr_class_type ctds -> ClassType ctds | _ -> Other |
let sig_proj = function | Psig_type ( r , td ) -> Type ( r , td ) | Psig_attribute ( a , p ) -> Attr ( a , p ) | Psig_class cds -> ClassType cds | Psig_class_type ctds -> ClassType ctds | _ -> Other |
let tydecl_of_classinfo ci = { ptype_name = ci . pci_name ; ptype_params = ci . pci_params ; ptype_loc = ci . pci_loc ; ptype_attributes = ci . pci_attributes ; ptype_cstrs = [ ] ; ptype_kind = Ptype_abstract ; ptype_private = Public ; ptype_manifest = None } |
let rec main super reify_all module_lid = let inside_module = match module_lid with | Lident _ -> false | _ -> true in let module_path = Longident . flatten module_lid in let module_name = String . concat " . " module_path in let str_desc types = List . map str_witness types @ List . concat ( List . map ( new_desc modu... |
let module_of_file s = String . capitalize_ascii ( List . hd ( String . split_on_char ' . ' s ) ) |
let top = let top_module ( ) = Lident ( module_of_file ! Location . input_name ) and str_reify = check_reify_all str_proj -< List . map ( fun s -> s . pstr_desc ) and sig_reify = check_reify_all sig_proj -< List . map ( fun s -> s . psig_desc ) in { default_mapper with structure = ( fun sub ss -> let m = main default_m... |
let ( ) = register " reify " ( fun argv -> top ) |
module type MONAD = sig type + _ t val return : ' a -> ' a t val bind : ' a t -> ( ' a -> ' b t ) -> ' b t end |
module RR ( M : MONAD ) : sig val reify : ( unit -> ' a ) -> ' a M . t val reflect : ' a M . t -> ' a struct type _ Effect . t += E : ' a M . t -> ' a Effect . t let reify f = match_with f ( ) { retc = ( fun x -> M . return x ) ; exnc = raise ; effc = fun ( type a ) ( e : a Effect . t ) -> match e with | E m -> Some ( ... |
module State = struct type ' a t = int -> int * ' a let return v s = ( s , v ) let bind m k s = let s , a = m s in k a s let get s = ( s , s ) let put s _ = ( s , ( ) ) let run s ~ init = s init end |
let put v = StateR . reflect ( State . put v ) |
let get ( ) = StateR . reflect State . get |
let run_state f ~ init = let final , v = State . run ( StateR . reify f ) ~ init in Printf . printf " Final state : % d \ n " final ; v |
module Exception = struct type ' a t = Ok of ' a | Exn of exn let return v = Ok v let bind m k = match m with Ok v -> k v | Exn e -> Exn e let raise exn = Exn exn let run m ~ catch = match m with Ok v -> v | Exn e -> catch e end |
let raise e = ExceptionR . reflect ( Exception . raise e ) |
let run_exception m ~ catch = Exception . run ( ExceptionR . reify m ) ~ catch |
let state_example ( ) = let initial = get ( ) in Printf . printf " Initial state : % d \ n " initial ; put 10 ; assert ( get ( ) = 10 ) ; put ( get ( ) + 1 ) ; assert ( get ( ) = 11 ) ; put 12 ; ( ` Initial initial , ` Final ( get ( ) ) ) |
let exception_example ( ) = Printf . printf " Raising an exception \ n " ; raise ( Failure " An error " ) ! |> ignore ; Printf . printf " This shouldn ' t be displayed \ n " |
let combined_example ( ) = Printf . printf " Initial state : % d \ n " ( get ( ) ) ; put 100 ; raise ( Failure " An error " ) ! |> ignore ; put 200 |
let print_exception e = Printf . printf " Exception : % s \ n " ( Printexc . to_string e ) |
let ( ) = run_state ~ init : 10 state_example |> ignore ; print_endline " " ; ======================================== run_exception ~ catch : print_exception exception_example ; print_endline " " ; ======================================== begin run_exception ~ catch : print_exception @@ fun ( ) -> run_state ~ init : 1... |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " RejectTransitGatewayPeeringAttachment " ] ) ] ( Util . drop_empty ( Uri . query_of_en... |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " RejectTransitGatewayPeeringAttachmentResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp RejectTransitGatewayPeeringAttachmentResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find w... |
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " RejectTransitGatewayVpcAttachment " ] ) ] ( Util . drop_empty ( Uri . query_of_encode... |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " RejectTransitGatewayVpcAttachmentResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp RejectTransitGatewayVpcAttachmentResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find well form... |
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " RejectVpcEndpointConnections " ] ) ] ( Util . drop_empty ( Uri . query_of_encoded ( Q... |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " RejectVpcEndpointConnectionsResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp RejectVpcEndpointConnectionsResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find well formed RejectV... |
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " RejectVpcPeeringConnection " ] ) ] ( Util . drop_empty ( Uri . query_of_encoded ( Que... |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " RejectVpcPeeringConnectionResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp RejectVpcPeeringConnectionResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find well formed RejectVpcPe... |
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None |
let make_data s = { | { " operation " : { " branch " : " BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 " , " contents " : [ { " kind " : " register_global_constant " , " value " : { " prim " : " unit " , " annots " : [ } | ^ s ^ { | ] } , " source " : " tz1LZhjR5Tyn63zZ1JSgT4WQZYyPXKTQiveu " , " fee " : " 10000 "... |
let reject_malformed_micheline = Protocol . register_test ~ __FILE__ ~ title " : Reject malformed micheline " ~ tags [ ] : @@ fun protocol -> let * ( node , _client ) = Client . init_with_protocol ` Client ~ protocol ( ) in let send_operation data = let rpc_path = sf " http :// localhost :% d / chains / main / blocks /... |
let register ~ protocols = reject_malformed_micheline ~ protocols |
let batches = 10 ** 8 |
let create_generator vs = let tr2d ~ stride ~ padding ~ input_dim n = Layer . conv_transpose2d_ vs ~ ksize : 4 ~ stride ~ padding ~ use_bias : false ~ input_dim n ~ w_init ( : Normal { mean = 0 . ; stdev = 0 . 02 } ) in let batch_norm2d dim = Layer . batch_norm2d vs dim ~ w_init : Ones in let convt1 = tr2d ~ stride : 1... |
let create_discriminator vs = let conv2d ~ stride ~ padding ~ input_dim n = Layer . conv2d_ vs ~ ksize : 4 ~ stride ~ padding ~ use_bias : false ~ input_dim n ~ w_init ( : Normal { mean = 0 . ; stdev = 0 . 02 } ) in let batch_norm2d dim = Layer . batch_norm2d vs dim ~ w_init : Ones in let leaky_relu xs = Tensor . ( max... |
let rand ( ) = Tensor . ( ( f 2 . * rand [ batch_size ; latent_dim ; 1 ; 1 ] ) - f 1 . ) |
let write_samples samples ~ filename = List . init 4 ~ f ( : fun i -> List . init 4 ~ f ( : fun j -> Tensor . narrow samples ~ dim : 0 ~ start ( ( : 4 * i ) + j ) ~ length : 1 ) |> Tensor . cat ~ dim : 2 ) |> Tensor . cat ~ dim : 3 |> Torch_vision . Image . write_image ~ filename |
let ( ) = let module Sys = Caml . Sys in let device = Device . cuda_if_available ( ) in let images = Serialize . load ~ filename : Sys . argv . ( 1 ) in let train_size = Tensor . shape images |> List . hd_exn in let generator_vs = Var_store . create ~ name " : gen " ~ device ( ) in let generator = create_generator gene... |
type command = string * string array |
let fork ( ) = Lwt_io . flush_all ( ) >>= fun ( ) -> match Lwt_unix . fork ( ) with | 0 -> Lwt . return 0 | pid -> Lwt . return pid |
let daemon f = let grandchild ( ) = Lwt_unix . chdir " " / >>= fun ( ) -> Lwt_unix . openfile " / dev / null " [ Unix . O_RDWR ] 0 >>= fun dev_null -> let close_and_dup fd = Lwt_unix . close fd >>= fun ( ) -> Lwt_unix . dup2 dev_null fd ; Lwt . return_unit in let descrs = Lwt_unix . [ stdin ; stdout ; stderr ] in Lwt_l... |
let read_pid fd = let buf = Buffer . create 32 in Io . read_once fd buf 0 32 >>= fun k -> let pid = String . trim ( Buffer . to_string ( Buffer . sub buf 0 k ) ) in Lwt . catch ( fun ( ) -> Lwt . return ( Some ( int_of_string pid ) ) ) ( fun _ -> Lwt . return_none ) |
let read_lock_file path = Lwt . catch ( fun ( ) -> Lwt_unix . openfile path [ Unix . O_RDONLY ] 0 >>= fun fd -> Lwt . finalize ( fun ( ) -> read_pid fd ) ( fun ( ) -> Lwt_unix . close fd ) ) ( function | Unix . Unix_error ( Unix . ENOENT , _ , _ ) -> Lwt . return_none | Unix . Unix_error ( e , _ , _ ) -> let err = Unix... |
let write_pid path = Lwt . catch ( fun ( ) -> let buf = Buffer . of_string ( string_of_int ( Unix . getpid ( ) ) ^ " \ n " ) in let flags = [ Unix . O_CREAT ; Unix . O_WRONLY ] in Lwt_unix . openfile path flags 0o644 >>= fun fd -> Io . write fd buf ) ( function | Unix . Unix_error ( e , _ , _ ) -> let err = Unix . erro... |
let create_lock_file path = read_lock_file path >>= fun pid -> match pid with | None -> write_pid path | Some pid -> Lwt . catch ( fun ( ) -> Unix . kill pid 0 ; Lwt_log . error_f " there is already a running instance of % s : % d " Sys . argv . ( 0 ) pid ) ( function | Unix . Unix_error ( Unix . ESRCH , _ , _ ) -> Lwt... |
let remove_lock_file path = read_lock_file path >>= function | None -> Lwt_log . info_f " couldn ' t find lock file % s " path | Some pid -> let mypid = Unix . getpid ( ) in if pid = mypid then Lwt_unix . unlink path else Lwt_log . info_f " pid mismatch in lock file : found % d , mine is % d ; not removing " pid mypid |
let check_user test msg = if not ( test ( Unix . getuid ( ) ) ) then Lwt_log . error_f " % s % s " Sys . argv . ( 0 ) msg >>= fun ( ) -> exit 1 else Lwt . return_unit |
let check_root ( ) = check_user ( ( ) = 0 ) " must be run by root " |
let check_nonroot ( ) = check_user ( ( ) <> 0 ) " cannot be run by root " |
let try_exec run ( ( path , _ ) as cmd ) = let can_exec st = let kind = st . Lwt_unix . st_kind in let perm = st . Lwt_unix . st_perm in kind = Unix . S_REG && perm land 0o100 <> 0 in Lwt . catch ( fun ( ) -> Lwt_unix . lstat path >>= fun st -> Lwt . return ( can_exec st ) ) ( function | Unix . Unix_error ( e , _ , _ )... |
let posix_signals = [ | " SIGABRT " ; " SIGALRM " ; " SIGFPE " ; " SIGHUP " ; " SIGILL " ; " SIGINT " ; " SIGKILL " ; " SIGPIPE " ; " SIGQUIT " ; " SIGSEGV " ; " SIGTERM " ; " SIGUSR1 " ; " SIGUSR2 " ; " SIGCHLD " ; " SIGCONT " ; " SIGSTOP " ; " SIGTSTP " ; " SIGTTIN " ; " SIGTTOU " ; " SIGVTALRM " ; " SIGPROF " ] | |
let signame s = let i = - s - 1 in if i >= 0 && i < Array . length posix_signals then posix_signals . ( i ) else string_of_int s |
module ConnMap = Map . Make ( struct type t = int let compare = compare end ) |
let slave_connection_map = ref ConnMap . empty |
let slave_connection_map_mtx = Lwt_mutex . create ( ) |
let add_slave_connection pid conn = Lwt_mutex . with_lock slave_connection_map_mtx ( fun ( ) -> slave_connection_map := ConnMap . add pid conn ! slave_connection_map ; Lwt . return_unit ) |
let remove_slave_connection pid = Lwt_mutex . with_lock slave_connection_map_mtx ( fun ( ) -> slave_connection_map := ConnMap . remove pid ! slave_connection_map ; Lwt . return_unit ) |
let slave_connections ( ) = ConnMap . bindings ! slave_connection_map |
let getenv k = try let v = Sys . getenv k in Some ( sprintf " % s =% s " k v ) with Not_found -> None |
let restrict_env = function | ` Inherit -> [ ] || | ` Keep allowed -> let setenv env k = Option . may_default env ( fun v -> v :: env ) ( getenv k ) in Array . of_list ( List . fold_left setenv [ ] allowed ) |
let log_status pid = let log fmt = ksprintf ( fun s -> Lwt_log . notice_f " process % s " s ) fmt in function | Lwt_unix . WEXITED 0 -> log " % d exited normally " pid | Lwt_unix . WEXITED s -> log " % d exited with status % s " pid ( signame s ) | Lwt_unix . WSIGNALED s -> log " % d signaled to death by % s " pid ( si... |
let handle_process master_fd reexec proc = let log fmt = ksprintf ( fun s -> Lwt_log . notice_f " process % s " s ) fmt in let pid = proc # pid in add_slave_connection pid master_fd >>= fun ( ) -> log " % d created " pid >>= fun ( ) -> remove_slave_connection pid >>= fun ( ) -> reexec ( ) |
let fork_exec cmd env ipc_handler reexec = let master_fd , slave_fd = Lwt_unix . ( socketpair PF_UNIX SOCK_STREAM 0 ) in Lwt_unix . set_close_on_exec master_fd ; let master_conn = Ipc . create_connection master_fd in let _ipc_t = ipc_handler master_conn in Lwt_process . with_process_none ~ stdin ( ` : FD_move ( Lwt_uni... |
let rec exec_process cmd ipc_handler slave_env check_death_rate = let run_proc cmd = let reexec ( ) = match check_death_rate ( ) with | ` Ok -> exec_process cmd ipc_handler slave_env check_death_rate | ` Disabled -> Lwt . return_unit | ` Exceeded -> Lwt_log . error " slave process dying too fast " >>= fun ( ) -> exit 1... |
let death_rate_checking_enabled = ref true |
let disable_slave_restart ( ) = death_rate_checking_enabled := false |
let check_death_rate time tries ( ) = if ! death_rate_checking_enabled then begin let now = Unix . time ( ) in if now . - ! time > 1 . then begin tries := num_exec_tries ; time := now end ; match ! tries with | 0 -> ` Exceeded | _ -> decr tries ; ` Ok end else ` Disabled |
let init_exec_slave max_tries = let tries = ref max_tries in let time = ref 0 . in fun cmd ipc_handler slave_env -> exec_process cmd ipc_handler slave_env ( check_death_rate time tries ) |
let signal_slaves signum = Lwt_list . iter_p ( fun ( pid , _ ) -> Lwt_log . info_f " signaling process % d " pid >>= fun ( ) -> Unix . kill pid signum ; Lwt . return_unit ) ( slave_connections ( ) ) |
let async_exit signame signum = disable_slave_restart ( ) ; Lwt . async ( fun ( ) -> Lwt_log . info_f " got % s , signaling child processes " signame >>= fun ( ) -> signal_slaves signum >>= fun ( ) -> Lwt_log . info " exiting " >>= fun ( ) -> exit ( 128 + signum ) ) ; exit ( 128 + signum ) |
let handle_sigint _ = async_exit " SIGINT " 2 |
let handle_sigterm _ = async_exit " SIGTERM " 15 |
let curry f ( x , y ) = f x y |
let rec loop i n f = if i > n then Lwt . return_unit else f ( ) >>= fun ( ) -> loop ( i + 1 ) n f |
let default_slave_env = ` Keep [ " TZ " ; " OCAMLRUNPARAM " ] |
let set_logger logger = Lwt_log . default := logger |
let master_slaves ( ? background = true ) ? logger ( ? privileged = true ) ( ? slave_env = default_slave_env ) ? control ? main ~ lock_file ~ slaves ( ) = Option . either ignore set_logger logger ; let create_slaves ( cmd , ipc_handler , n ) = loop 1 n ( fun ( ) -> let exec_slave = init_exec_slave num_exec_tries in exe... |
let master_slave ~ slave = let cmd , ipc_handler = slave in master_slaves ~ slaves [ : cmd , ipc_handler , 1 ] |
let lose_privileges user = check_root ( ) >>= fun ( ) -> Lwt . catch ( fun ( ) -> Privileges . drop user ) ( function | Privileges . Error err -> Lwt_log . error_f " Error dropping privileges : % s " err >>= fun ( ) -> exit 1 | e -> Lwt . fail e ) |
let me ? logger ? user ~ main ( ) = let main_t = Option . either check_nonroot lose_privileges user >>= fun ( ) -> Option . either ignore set_logger logger ; let ipc_fd = Lwt_unix . dup Lwt_unix . stdin in Lwt_unix . close Lwt_unix . stdin >>= fun ( ) -> let pid = Unix . getpid ( ) in Lwt_log . info_f " starting up ( P... |
let to_http service region req = let uri = Uri . add_query_params ( Uri . of_string ( Aws . Util . of_option_exn ( Endpoints . url_of service region ) ) ) ( List . append [ ( " Version " , [ " 2016 - 11 - 15 " ] ) ; ( " Action " , [ " ReleaseHosts " ] ) ] ( Util . drop_empty ( Uri . query_of_encoded ( Query . render ( ... |
let of_http body = try let xml = Ezxmlm . from_string body in let resp = Xml . member " ReleaseHostsResponse " ( snd xml ) in try Util . or_error ( Util . option_bind resp ReleaseHostsResult . parse ) ( let open Error in BadResponse { body ; message = " Could not find well formed ReleaseHostsResult . " } ) with | Xml .... |
let parse_error code err = let errors = [ ] @ Errors_internal . common in match Errors_internal . of_string err with | Some var -> if ( List . mem var errors ) && ( ( match Errors_internal . to_http_code var with | Some var -> var = code | None -> true ) ) then Some var else None | None -> None |
let name_of_release = Github_t . ( function | { release_name = Some name ; _ } -> name | { release_name = None ; _ } -> " NULL " ) |
let latest_release m = let open Github_t in let name = name_of_release m in eprintf " latest release % Ld : % s ( % s ) \ n " %! m . release_id name m . release_created_at ; eprintf " --\ n " ; %! ( ) |
let print_releases m = Github . ( Monad . ( Stream . iter ( fun m -> let open Github_t in let name = name_of_release m in eprintf " release % Ld : % s ( % s ) \ n " %! m . release_id name m . release_created_at ; return ( ) ) m >>= fun ( ) -> eprintf " --\ n " ; %! return ( ) ) ) |
let print_release_assets m = let open Github_t in List . iter ( fun x -> eprintf " asset % Ld % s \ n " %! x . release_asset_id x . release_asset_url ) m ; eprintf " --\ n " ; %! ( ) |
let t = Github . ( Monad . ( run ( return ( Release . for_repo ~ user " : mirage " ~ repo " : ocaml - github " ( ) ) >>= print_releases >>= fun ( ) -> ( Release . get_latest ~ user " : mirage " ~ repo " : ocaml - github " ( ) >|= Response . value >>= fun x -> latest_release x ; return x ) >>= fun a -> ( Release . list_... |
type t = { bytes : Lwt_bytes . t ; mutable len : int } |
let create n = { bytes = Lwt_bytes . create n ; len = 0 } |
let make n c = let buf = create n in Lwt_bytes . fill buf . bytes 0 n c ; buf . len <- n ; buf |
let get buf i = Lwt_bytes . get buf . bytes i |
let set buf i c = Lwt_bytes . set buf . bytes i c ; let len = i + 1 in if len > buf . len then buf . len <- len |
let size buf = Lwt_bytes . length buf . bytes |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.