text
stringlengths
0
601k
let ts_equal x y = Ident . equal x . ts_ident y . ts_ident
let rec ty_equal x y = match ( x . ty_node , y . ty_node ) with | Tyvar tvx , Tyvar tvy -> tv_equal tvx tvy | Tyapp ( tsx , tylx ) , Tyapp ( tsy , tyly ) -> ts_equal tsx tsy && List . for_all2 ty_equal tylx tyly | _ -> false
module Ts = struct type t = tysymbol let equal = ts_equal let hash x = x . ts_ident . id_tag let compare x y = Ident . compare x . ts_ident y . ts_ident end
module Mts = Map . Make ( Ts )
module Hts = Hashtbl . Make ( Ts )
let ts id args = { ts_ident = id ; ts_args = args ; ts_alias = None }
let mk_ts id args alias = { ts_ident = id ; ts_args = args ; ts_alias = alias }
let ts_ident ts = ts . ts_ident
let ts_args ts = ts . ts_args
let ts_alias ts = ts . ts_alias
let ts_arity ts = List . length ts . ts_args
let fresh_ty_var ( ? loc = Location . none ) s = { ty_node = Tyvar { tv_name = Ident . create ~ loc s } }
let ty_of_var tv = { ty_node = Tyvar tv }
let ty_app ts tyl = if ts_arity ts = List . length tyl then { ty_node = Tyapp ( ts , tyl ) } else W . error ~ loc : ts . ts_ident . id_loc ( W . Bad_type_arity ( ts . ts_ident . id_str , ts_arity ts , List . length tyl ) )
let rec ty_full_inst m ty = match ty . ty_node with | Tyvar tv -> Mtv . find tv m | Tyapp ( ts , tyl ) -> ty_app ts ( List . map ( ty_full_inst m ) tyl )
let ts_match_args ts tl = try List . fold_right2 Mtv . add ts . ts_args tl Mtv . empty with Invalid_argument _ -> W . error ~ loc : ts . ts_ident . id_loc ( W . Bad_type_arity ( ts . ts_ident . id_str , ts_arity ts , List . length tl ) )
let ty_app ts tyl = match ts . ts_alias with | None -> ty_app ts tyl | Some ty -> ty_full_inst ( ts_match_args ts tyl ) ty
let rec ts_subst_ts old_ts new_ts ( { ts_ident ; ts_args ; ts_alias } as ts ) = if ts_equal old_ts ts then new_ts else let ts_alias = Option . map ( ty_subst_ts old_ts new_ts ) ts_alias in mk_ts ts_ident ts_args ts_alias match ty . ty_node with | Tyvar _ -> ty | Tyapp ( ts , tyl ) -> let ts = if ts_equal old_ts ts then new_ts else ts in ty_app ts ( List . map ( ty_subst_ts old_ts new_ts ) tyl )
let rec ty_subst_ty old_ts new_ts new_ty ty = match ty . ty_node with | Tyvar _ -> ty | Tyapp ( ts , tyl ) -> if ts_equal old_ts ts then ty_full_inst ( ts_match_args new_ts tyl ) new_ty else let subst ty = ty_subst_ty old_ts new_ts new_ty ty in let tyl = List . map subst tyl in ty_app ts tyl let subst ty = ty_subst_ty old_ts new_ts new_ty ty in let ts_alias = Option . map subst ts . ts_alias in mk_ts ts . ts_ident ts . ts_args ts_alias
let rec ty_match mtv ty1 ty2 = let set = function | None -> Some ty2 | Some ty1 as r when ty_equal ty1 ty2 -> r | _ -> raise Exit in match ( ty1 . ty_node , ty2 . ty_node ) with | Tyvar tv1 , _ -> Mtv . update tv1 set mtv | Tyapp ( ts1 , tyl1 ) , Tyapp ( ts2 , tyl2 ) when ts_equal ts1 ts2 -> List . fold_left2 ty_match mtv tyl1 tyl2 | _ -> raise Exit
let ty_match mtv ty1 ty2 = let rec ty_inst mtv ty = match ty . ty_node with | Tyvar n -> ( try Mtv . find n mtv with Not_found -> ty ) | Tyapp ( ts , tyl ) -> { ty_node = Tyapp ( ts , List . map ( ty_inst mtv ) tyl ) } in try ty_match mtv ty1 ty2 with Exit -> raise ( TypeMismatch ( ty_inst mtv ty1 , ty2 ) )
let ty_equal_check ty1 ty2 = if not ( ty_equal ty1 ty2 ) then raise ( TypeMismatch ( ty1 , ty2 ) )
let ts_unit = ts ( Ident . create ~ loc : Location . none " unit " ) [ ]
let ts_integer = ts ( Ident . create ~ loc : Location . none " integer " ) [ ]
let ts_int = ts ( Ident . create ~ loc : Location . none " int " ) [ ]
let ts_char = ts ( Ident . create ~ loc : Location . none " char " ) [ ]
let ts_bytes = ts ( Ident . create ~ loc : Location . none " bytes " ) [ ]
let ts_string = ts ( Ident . create ~ loc : Location . none " string " ) [ ]
let ts_float = ts ( Ident . create ~ loc : Location . none " float " ) [ ]
let ts_bool = ts ( Ident . create ~ loc : Location . none " bool " ) [ ]
let ts_exn = ts ( Ident . create ~ loc : Location . none " exn " ) [ ]
let ts_array = ts ( Ident . create ~ loc : Location . none " array " ) [ fresh_tv ~ loc : Location . none " a " ]
let ts_list = ts ( Ident . create ~ loc : Location . none " list " ) [ fresh_tv ~ loc : Location . none " a " ]
let ts_option = ts ( Ident . create ~ loc : Location . none " option " ) [ fresh_tv ~ loc : Location . none " a " ]
let ts_int32 = ts ( Ident . create ~ loc : Location . none " int32 " ) [ ]
let ts_int64 = ts ( Ident . create ~ loc : Location . none " int64 " ) [ ]
let ts_nativeint = ts ( Ident . create ~ loc : Location . none " nativeint " ) [ ]
let ts_format6 = ts ( Ident . create ~ loc : Location . none " format6 " ) [ fresh_tv ~ loc : Location . none " a " ; fresh_tv ~ loc : Location . none " b " ; fresh_tv ~ loc : Location . none " c " ; fresh_tv ~ loc : Location . none " d " ; fresh_tv ~ loc : Location . none " e " ; fresh_tv ~ loc : Location . none " f " ; ]
let ts_lazy = ts ( Ident . create ~ loc : Location . none " lazy " ) [ fresh_tv ~ loc : Location . none " a " ]
let ts_tuple = let ts_tuples = Hashtbl . create 0 in Hashtbl . add ts_tuples 0 ts_unit ; fun n -> try Hashtbl . find ts_tuples n with Not_found -> let ts_id = Ident . create ~ loc : Location . none ( " tuple " ^ string_of_int n ) in let ts_args = List . init n ( fun x -> fresh_tv ~ loc : Location . none ( " a " ^ string_of_int x ) ) in let ts = ts ts_id ts_args in Hashtbl . add ts_tuples n ts ; ts
let ts_arrow = let ta = fresh_tv ~ loc : Location . none " a " in let tb = fresh_tv ~ loc : Location . none " b " in let id = Ident . create ~ loc : Location . none " " -> in ts id [ ta ; tb ]
let is_ts_tuple ts = let ts_tuple = ts_tuple ( ts_arity ts ) in Ident . equal ts_tuple . ts_ident ts . ts_ident
let is_ts_arrow ts = Ident . equal ts_arrow . ts_ident ts . ts_ident
let ty_unit = ty_app ts_unit [ ]
let ty_integer = ty_app ts_integer [ ]
let ty_int = ty_app ts_int [ ]
let ty_bool = ty_app ts_bool [ ]
let ty_float = ty_app ts_float [ ]
let ty_char = ty_app ts_char [ ]
let ty_string = ty_app ts_string [ ]
let ty_option ty = ty_app ts_option [ ty ]
let ty_list ty = ty_app ts_list [ ty ]
let ty_tuple = function | [ ] -> ty_unit | [ ty ] -> ty | tyl -> ty_app ( ts_tuple ( List . length tyl ) ) tyl
type exn_type = | Exn_tuple of ty list | Exn_record of ( Ident . t * ty ) list
type xsymbol = { xs_ident : Ident . t ; xs_type : exn_type } [ @@ deriving show ]
let xsymbol id ty = { xs_ident = id ; xs_type = ty }
let xs_equal x y = Ident . equal x . xs_ident y . xs_ident
module Xs = struct type t = xsymbol let equal = xs_equal let compare x y = Ident . compare x . xs_ident y . xs_ident end
module Mxs = Map . Make ( Xs )
let xs_subst_ts old_ts new_ts { xs_ident ; xs_type } = let subst = function | Exn_tuple tyl -> Exn_tuple ( List . map ( ty_subst_ts old_ts new_ts ) tyl ) | Exn_record l -> Exn_record ( List . map ( fun ( id , ty ) -> ( id , ty_subst_ts old_ts new_ts ty ) ) l ) in xsymbol xs_ident ( subst xs_type )
let xs_subst_ty old_ts new_ts new_ty xs = let subst = function | Exn_tuple tyl -> let subst ty = ty_subst_ty old_ts new_ts new_ty ty in Exn_tuple ( List . map subst tyl ) | Exn_record l -> let subst ( id , ty ) = ( id , ty_subst_ty old_ts new_ts new_ty ty ) in Exn_record ( List . map subst l ) in { xs with xs_type = subst xs . xs_type }
let print_tv fmt tv = pp fmt ( if tv . tv_name . id_str = " _ " then " % a " else " ' % a " ) Ident . pp tv . tv_name
let print_ts_name fmt ts = pp fmt " [ @% a ] " @ Ident . pp ( ts_ident ts )
let rec print_ty fmt { ty_node } = print_ty_node fmt ty_node | Tyvar v -> pp fmt " % a " print_tv v | Tyapp ( ts , [ ] ) -> print_ts_name fmt ts | Tyapp ( ts , tys ) when is_ts_arrow ts -> print_arrow_ty fmt tys | Tyapp ( ts , tyl ) when is_ts_tuple ts -> pp fmt " % a " ( list ~ sep : star print_ty ) tyl | Tyapp ( ts , [ ty ] ) -> pp fmt " % a % a " print_ty ty print_ts_name ts | Tyapp ( ts , tyl ) -> pp fmt " ( % a ) % a " ( list ~ sep : comma print_ty ) tyl print_ts_name ts
let print_ts fmt ts = pp fmt " [ @% a % a % a ] " @ ( list ~ sep : comma ~ first : lparens ~ last : rparens print_tv ) ts . ts_args Ident . pp ( ts_ident ts ) ( fun fmt alias -> match alias with None -> ( ) | Some ty -> pp fmt " [ =% a ] " print_ty ty ) ts . ts_alias
let print_exn_type f = function | Exn_tuple tyl -> list ~ sep : star print_ty f tyl | Exn_record args -> let print_arg f ( id , ty ) = pp f " % a :% a " Ident . pp id print_ty ty in list ~ sep : semi ~ first : rbrace ~ last : lbrace print_arg f args
let print_xs f x = pp f " % a " Ident . pp x . xs_ident
module User_input = struct type t = | Ctrl_c | Escape | Backspace | Return | Char of char [ @@ deriving sexp_of ] end
module Configure_terminal = struct type t = { attr_in : Unix . Terminal_io . t ; attr_out : Unix . Terminal_io . t ; } let setattr_out fd ~ attr_out = Unix . Terminal_io . tcsetattr attr_out ~ mode : Unix . Terminal_io . TCSAFLUSH fd let setattr_in fd ~ attr_in = Unix . Terminal_io . tcsetattr attr_in ~ mode : Unix . Terminal_io . TCSADRAIN fd let get_current_settings ~ input ~ output ( ) = let % bind attr_in = Unix . Terminal_io . tcgetattr input in let % bind attr_out = Unix . Terminal_io . tcgetattr output in return { attr_in = attr_in ; attr_out = attr_out ; } let set ~ input ~ output t = let % bind ( ) = setattr_in input ~ attr_in : t . attr_in in let % bind ( ) = setattr_out output ~ attr_out : t . attr_out in return ( ) let map_termio ( attrs : Core . Unix . Terminal_io . t ) = { attrs with Core . Unix . Terminal_io . c_ignbrk = false ; c_brkint = false ; c_parmrk = false ; c_istrip = false ; c_inlcr = false ; c_igncr = false ; c_icrnl = false ; c_ixon = false ; c_opost = false ; c_echo = false ; c_echonl = false ; c_icanon = false ; c_isig = false ; c_csize = 8 ; c_parenb = false ; c_vmin = 1 ; c_vtime = 0 ; } ; ; let to_drawing t = { attr_in = map_termio t . attr_in ; attr_out = map_termio t . attr_out } ; ; end
let esc rest = " \ x1b [ " ^ rest ; ;
module Direction = struct type t = | Up | Down | Left | Right [ @@ deriving enumerate ] let escape = function | Up -> esc " A " | Down -> esc " B " | Right -> esc " C " | Left -> esc " D " end
module Action = struct type t = | Clear_screen | Move_cursor_to_home | Next_line | Move of Direction . t | Switch_to_alternate_buffer | Switch_from_alternate_buffer | Erase_to_end_of_line let _compilation_fix_for_unused_constructor = Move Left let to_string = function | Clear_screen -> esc " 2J " | Erase_to_end_of_line -> esc " K " | Move_cursor_to_home -> esc " H " | Next_line -> " \ r \ n " | Move dir -> Direction . escape dir | Switch_to_alternate_buffer -> esc " ? 1049h " | Switch_from_alternate_buffer -> esc " ? 1049l " end
let do_action writer action = Writer . write writer ( Action . to_string action ) ; Writer . flushed writer ; ;
type t = { dimensions : Screen_dimensions . t ; writer : Writer . t }
let screen_dimensions { dimensions ; _ } = dimensions
let stop_rendering t = do_action t Switch_from_alternate_buffer ; ;
let with_rendering f = let % bind tty_reader = Reader . open_file ~ buf_len : 1 " / dev / tty " in let % bind tty_writer = Writer . open_file " / dev / tty " in let input = Reader . fd tty_reader in let output = Writer . fd tty_writer in let % bind original = Configure_terminal . get_current_settings ~ input ~ output ( ) in let restore ( ) = Configure_terminal . set original ~ input ~ output in let % bind dimensions = let % map output = Process . run_exn ( ) ~ prog " : stty " ~ args [ : " size " ; " - F " ; " / dev / tty " ] in match output |> String . strip |> String . split ~ on ' : ' with | [ height ; width ] -> { Screen_dimensions . height = Int . of_string height ; width = Int . of_string width } | _ -> raise_s [ % message " Could not determine terminal size " ] in Monitor . protect ~ finally : restore ( fun ( ) -> let t = { dimensions ; writer = tty_writer } in let % bind ( ) = Configure_terminal . to_drawing original |> Configure_terminal . set ~ input ~ output in let % bind ( ) = do_action tty_writer Switch_to_alternate_buffer in let % bind ( ) = do_action tty_writer Clear_screen in let user_input = Pipe . create_reader ~ close_on_exception : true ( fun w -> let repeat x = let % bind ( ) = Pipe . write w x in return ( ` Repeat ( ) ) in let b = Bytes . create 1 in Deferred . repeat_until_finished ( ) ( fun ( ) -> match % bind Reader . really_read ~ len : 1 tty_reader b with | ` Eof _ -> return ( ` Finished ( ) ) | ` Ok -> match Char . to_int ( Bytes . get b 0 ) with | 3 -> let % bind ( ) = Pipe . write w User_input . Ctrl_c in return ( ` Finished ( ) ) | 0O177 -> repeat Backspace | 0O015 -> repeat Return | 0O33 -> repeat Escape | _ -> repeat ( Char ( Bytes . get b 0 ) ) ) ) in let % bind to_return = f ( user_input , t ) in let % bind ( ) = restore ( ) in let % bind ( ) = stop_rendering t . writer in return to_return ) ; ;
module Widget = struct type t = | Text of string | Group_horizontally of t list | Stack_vertically of t list let text text = Text text let horizontal_group ts = Group_horizontally ts let vertical_group ts = Stack_vertically ts let render elts writer = let rec process = function | Text x -> Writer . writef writer " % s " x | Group_horizontally xs -> List . iter xs ~ f : process | Stack_vertically xs -> xs |> List . map ~ f ( : fun x -> ( fun ( ) -> process x ) ) |> List . intersperse ~ sep ( : fun ( ) -> Writer . writef writer " { !% Action } { % Action } " Erase_to_end_of_line Next_line ) |> List . iter ~ f ( : fun f -> f ( ) ) in Writer . writef writer " { !% Action } " Move_cursor_to_home ; process elts ; Writer . writef writer " { !% Action } " Erase_to_end_of_line ; Writer . flushed writer end
let render t w = Widget . render w t . writer ; ;
type kind = Tap | Tun -> int -> int -> Unix . file_descr * string = " tun_opendev_byte " " tun_opendev "
let open_ kind ( ? pi = false ) false ? persist ( ? user = - 1 ) 1 ( ? group = - 1 ) 1 ( ? devname ) " " = ( ) = let persist_int = match persist with | None -> - 1 | Some false -> 0 | Some true -> 1 in opentun_stub devname kind pi persist_int user group
let opentun = open_ Tun
let opentap = open_ Tap
let closetun devname = ignore ( opentun ~ devname ~ persist : false ( ) )
let closetap devname = ignore ( opentap ~ devname ~ persist : false ( ) )
let set_ipv4 ( ? netmask = Ipaddr . V4 . Prefix . global ) global devname v4addr = let open Ipaddr . V4 in set_ipv4 devname ( to_octets v4addr ) v4addr ( to_octets ( Prefix . netmask netmask ) netmask ) netmask
let get_macaddr iface = Macaddr . of_octets_exn ( get_macaddr iface ) iface
module Opt = struct let ( ) >|= x f = match x with Some v -> Some ( f v ) v | None -> None let run = function | Some x -> x | None -> raise Not_found end
module Struct_ifaddrs = struct type t = { name : string ; sa_family : int ; addr : string option ; mask : string option ; brd : string option ; } type ptr_t external getifaddrs_stub : unit -> ptr_t option = " getifaddrs_stub " external freeifaddrs_stub : ptr_t -> unit = " freeifaddrs_stub " external iface_get : ptr_t -> t = " iface_get " external iface_next : ptr_t -> ptr_t option = " iface_next " let to_t ' t = let open Ipaddr in let open Opt in match t . sa_family with | 0 -> let address = run ( t . addr >|= fun v -> V4 . of_octets_exn v ) v and netmask = run ( t . mask >|= fun v -> V4 . of_octets_exn v ) v in Some ( t . name , ` V4 ( V4 . Prefix . of_netmask_exn ~ netmask ~ address ) address ) address | 1 -> let address = run ( t . addr >|= fun v -> V6 . of_octets_exn v ) v and netmask = run ( t . mask >|= fun v -> V6 . of_octets_exn v ) v in Some ( t . name , ` V6 ( V6 . Prefix . of_netmask_exn ~ netmask ~ address ) address ) address | _ -> None end
let getifaddrs ( ) = let open Struct_ifaddrs in match getifaddrs_stub ( ) with | None -> [ ] | Some start -> let rec loop acc ptr = let acc = match to_t ' ( iface_get ptr ) ptr with | None -> acc | Some t ' -> t ' :: acc in match iface_next ptr with | None -> freeifaddrs_stub start ; acc | Some p -> loop acc p in loop [ ] start
let filter_map f l = List . fold_left ( fun a v -> match f v with Some v ' -> v ' :: a | None -> a ) a [ ] l
let getifaddrs_v4 ( ) = filter_map ( function ( ifn , ` V4 a ) a -> Some ( ifn , a ) a | _ -> None ) None @@ getifaddrs ( )
let getifaddrs_v6 ( ) = filter_map ( function ( ifn , ` V6 a ) a -> Some ( ifn , a ) | _ -> None ) None @@ getifaddrs ( )
let addrs_of_ifname ifname = filter_map ( fun ( ifn , a ) a -> if ifn = ifname then Some a else None ) None @@ getifaddrs ( )
let v4_of_ifname ifname = filter_map ( fun ( ifn , a ) a -> if ifn = ifname then Some a else None ) None @@ getifaddrs_v4 ( )
let v6_of_ifname ifname = filter_map ( fun ( ifn , a ) a -> if ifn = ifname then Some a else None ) None @@ getifaddrs_v6 ( )
let first ( a , _ ) = a
module type MAGMA_F = functor ( First : MAGMA ) ( Second : MAGMA ) -> MAGMA with type t = First . t * Second . t
module type SEMIGROUP_F = functor ( First : SEMIGROUP ) ( Second : SEMIGROUP ) -> SEMIGROUP with type t = First . t * Second . t
module type MONOID_F = functor ( First : MONOID ) ( Second : MONOID ) -> MONOID with type t = First . t * Second . t