text
stringlengths
12
786k
let close_module_type muc = match ( muc . muc_import , muc . muc_export , muc . muc_prefix , muc . muc_sigs ) with | _ :: i1 :: il , e0 :: e1 :: el , p0 :: pl , _ :: sl -> { muc with muc_prefix = pl ; muc_import = ns_add_tns ~ allow_duplicate : true i1 p0 e0 :: il ; muc_exp...
let get_top_sigs muc = match muc . muc_sigs with s0 :: _ -> List . rev s0 | _ -> assert false
let get_top_import muc = match muc . muc_import with i0 :: _ -> i0 | _ -> assert false
let get_top_export muc = match muc . muc_export with e0 :: _ -> e0 | _ -> assert false
let add_sig_contents muc sig_ = let muc = add_sig muc sig_ in let get_cs_pjs = function | Pty_abstract -> [ ] | Pty_variant cdl -> List . map ( fun cd -> cd . cd_cs ) cdl | Pty_record rd -> rd . rd_cs :: List . map ( fun ld -> ld . ld_field ) rd . rd_ldl in match sig_ . sig_desc wi...
let init_muc s = { muc_nm = Ident . create ~ loc : Location . none s ; muc_sigs = [ [ ] ] ; muc_prefix = [ s ] ; muc_import = [ ns_with_primitives ] ; muc_export = [ empty_ns ] ; muc_files = Mstr . empty ; muc_kid = Mid . empty ; muc_crcm = Coercion . empty ; }...
let wrap_up_muc ( muc : module_uc ) = match ( muc . muc_export , muc . muc_sigs ) with | [ e ] , [ s ] -> { fl_nm = muc . muc_nm ; fl_sigs = List . rev s ; fl_export = e } | _ -> assert false
let rec tree_ns f fmt ns = Mstr . iter ( fun s ns -> if f ns = Mstr . empty then pp fmt " [ @% s @\ n ] " @ s else pp fmt " [ @% s :@\ n @ [ @% a ] @@\ n ] " @ s ( tree_ns f ) ( f ns ) ) ns
let ns_names nsm = List . map fst ( Mstr . bindings nsm )
let print_mstr_vals printer fmt m = let print_elem e = pp fmt " [ @% a ] @@\ n " printer e in Mstr . iter ( fun _ -> print_elem ) m
let rec print_nested_ns fmt ns = let print_elem nm e = pp fmt " [ @% a ] @@\ n " ( print_ns nm ) e in Mstr . iter ( fun nm ns -> print_elem nm ns ) ns pp fmt " [ [ @@< hv2 [ >@ Namespace : % s ] @@\ n \ [ @< hv2 > Type symbols @\ n \ % a ] @@\ n \ [ @< hv2 > Logic S...
let print_file fmt { fl_nm ; fl_sigs ; fl_export } = pp fmt " [ @ module % a @\ n [ @< h2 >@\ n % a @\ n [ @< hv2 > Signatures @\ n % a ] ] ] . " @@@@ Ident . pp fl_nm ( print_ns fl_nm . id_str ) fl_export print_signature fl_sigs
let rand_gen = lazy ( Random . State . make_self_init ( ) )
let rand_path dir pat = let rand = Random . State . bits ( Lazy . force rand_gen ) land 0xFFFFFF in Fpath . ( dir / Fmt . str pat ( Fmt . str " % 06x " rand ) )
let default_dir_init = let from_env var ~ absent = match try Some ( Sys . getenv var ) with Not_found -> None with | None -> absent | Some v -> match Fpath . of_string v with | Error ( ` Msg err ) -> failwith err | Ok v -> v in if Sys . os_type = " Win32 " then from_env " TEMP " ~ ...
let default_dir = ref default_dir_init
let set_default_dir p = default_dir := p
let default_dir ( ) = ! default_dir
let create_tmp_path mode dir pat = let err ( ) = R . error_msgf " create temporary file % s in % a : too many failing attemps " ( Fmt . str pat " XXXXXX " ) Fpath . pp dir in let rec go count = if count < 0 then err ( ) else let file = rand_path dir pat in let sfile = Fpath . to_st...
type pattern = ( string -> string , Format . formatter , unit , string ) format4
let tmp ( ? mode = 0o644 ) ? dir pat = let dir = match dir with | None -> default_dir ( ) | Some d -> d in create_tmp_path mode dir pat >>= fun ( file , fd ) -> let rec close fd = try Unix . close fd with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> close fd | Unix . U...
type t = heading list
let to_string ( H ( size , text ) ) = " H " ^ string_of_int size ^ " " ^ text
let get_int = function H ( i , _ ) -> i
let toc doc = let rec loop acc = function | [ ] -> List . rev acc | ( b : Omd . block ) :: bs -> ( match b . bl_desc with | Omd . Heading ( s , il ) -> ( match il . il_desc with | Omd . Text heading -> loop ( H ( s , heading ) :: acc ) bs | _ -> loop acc bs ) |...
let text_to_id s = let replace on sep s = String . concat ~ sep ( String . split ~ on s ) in String . lowercase ( replace ' ' " " - s )
let transform doc = let f ( b : Omd . block ) = match b . bl_desc with | Omd . Heading ( _s , il ) -> ( match il . il_desc with | Omd . Text heading -> { b with bl_attributes = ( " id " , text_to_id heading ) :: b . bl_attributes ; } | _ -> b ) | _ -> b in List ...
type ' a tree = Br of ' a * ' a tree list
let rec pre ppf tree = match tree with | Br ( t , lst ) -> Format . pp_print_string ppf ( to_string t ) ; List . iter ~ f ( : pre ppf ) lst
let to_tree lst = let arr = Array . init 7 ~ f ( : fun i -> Br ( H ( i , " " ) , [ ] ) ) in let rec tidy arr until = function | n when n <= until -> ( ) | n -> let t = arr . ( n ) in let ( Br ( v , lst ) ) = arr . ( n - 1 ) in arr . ( n - 1 ) ...
let map_to_item h = let to_elt cl link txt = [ % html " < a class " = cl " href " = ( " " # ^ link ) " " > [ Html . txt txt ] " </ a " ] > in match h with | H ( i , txt ) -> if String . equal txt " " then [ ] else [ to_elt [ " toc - link " ; " ...
let rec preorder = function | Br ( v , [ ] ) -> [ % html " < ul class ' = toc ' >< li class ' = toc - li ' " > ( map_to_item v ) " </ li ></ ul " ] > | Br ( v , lst ) -> [ % html " < ul class ' = toc ' >< li " > ( map_to_item v @ List . fold_lef...
let pp ppf t = List . iter ~ f ( : fun h -> Format . pp_print_string ppf ( to_string h ^ " \ n " ) ) t
let rec accessibility = function | x :: y :: ys -> let a = get_int x in let b = get_int y in if get_int x < get_int y then if b - a = 1 then accessibility ( y :: ys ) else raise ( Failure " Failed because of inproper heading nesting " ) | _ -> assert true
let to_html toc = ( try accessibility toc with Failure t -> print_endline " == Failed Heading List " ; == pp Format . std_formatter toc ; raise ( Failure t ) ) ; let tree = to_tree toc in preorder tree
let equal = Stdlib . ( = )
type toc_settings = { min_level : int ; max_level : int ; max_heading_link_level : int ; toc_class : string option ; toc_class_levels : bool ; numbered_list : bool ; link_here : bool ; link_here_text : string ; link_here_class : string option ; link_here_append : bool ; use_text : bool...
let make_counter seed = let counter = ref seed in fun ( ) -> incr counter ; ! counter
let get_heading_id settings counter heading = let id = Soup . attribute " id " heading in match id with | Some id -> id | None -> if not ( settings . use_slugs || settings . use_text ) then counter ( ) |> string_of_int else let text = Html_utils . get_element_text heading in begin match tex...
let make_toc_class settings level = match settings . toc_class with | None -> None | Some _class -> let _class = if settings . toc_class_levels then Printf . sprintf " % s -% d " _class level else _class in Some _class
let add_item settings heading container = let li = Soup . create_element " li " in let heading_id = Soup . attribute " id " heading |> Option . get in let h_link = Soup . create_element ~ attributes [ " : href " , " " # ^ heading_id ] " a " in let h_content = Html_utils . c...
let make_heading_linkable settings counter heading = if ( Html_utils . get_heading_level heading ) <= settings . max_heading_link_level then let heading_id = get_heading_id settings counter heading in Soup . set_attribute " id " heading_id heading
let add_section_link settings heading = let heading_level = Html_utils . get_heading_level heading in if ( heading_level <= settings . max_heading_link_level ) && ( heading_level >= settings . min_level ) then let heading_id = Soup . attribute " id " heading |> Option . get in let link_text...
let make_toc_container settings level = let tag = if settings . numbered_list then " ol " else " ul " in let toc_list = Soup . create_element tag in let toc_class = make_toc_class settings level in Html_utils . add_class toc_class toc_list ; toc_list
let level_matches settings h = let level = Html_utils . get_heading_level h in ( level <= settings . max_level ) && ( level >= settings . min_level )
let ignored_heading settings soup h = let res = Html_utils . matches_any_of settings . ignore_heading_selectors soup h in match res with | true -> let ( ) = Logs . debug @@ fun m -> m " Heading % s is ignored due to ignore_selector settings " ( Soup . to_string h ) in true | false -> false
let rec _make_toc settings depth counter parent tree = let heading = Rose_tree . ( tree . value ) in let children = Rose_tree . ( tree . children ) in let level = Html_utils . get_heading_level heading in if level > settings . max_level then Logs . debug @@ fun m -> m " Heading % s is i...
let toc _ config soup = let valid_options = List . append Config . common_widget_options [ " selector " ; " action " ; " strip_tags " ; " min_level " ; " max_level " ; " max_heading_link_level " ; " toc_list_class " ; " toc_class_levels " ; " numbered_list " ...
module Model = struct type filter = | All | Active | Completed let string_of_filter : filter -> string = function | All -> " All " | Active -> " Active " | Completed -> " Completed " type t = { input : string ; items : ( string * bool ) list ; filter : filter ; editing : ( ...
module Event = struct type t = | AddInput | ChangeInput of string | Delete of int | Edit of int | ChangeEdit of string | CommitEdit | CancelEdit | Check of int | Uncheck of int | Filter of Model . filter | Toggle let handle t m = match t with | AddInput -> Model . add_item m | ChangeInput i -> Mod...
let ifilter_map ~ f xs = let rec loop f i xs = match xs with | [ ] -> [ ] | x :: xs ' -> begin match f x i with | None -> loop f ( i + 1 ) xs ' | Some x ' -> x ' :: ( loop f ( i + 1 ) xs ' ) end in loop f 0 xs ; ;
module View = struct open D3 type item = { text : string ; completed : bool ; editing : bool } let items_of_model m = let display = let open Model in match m . filter with | All -> fun _ -> true | Active -> fun x -> not x | Completed -> fun x -> x in ifilter_map m . Model . items ~ f ( ...
module Storage : sig val get : unit -> ( string * bool ) list val set : ( string * bool ) list -> unit open Js let key = string " todos - ocaml - d3 " let storage = Optdef . case ( Dom_html . window . ## localStorage ) ( fun ( ) -> None ) ( fun s -> Some s ) let get ( )...
let main_lazy ( ) = let model = ref { Model . init with Model . items = Storage . get ( ) } in let rec go ( ) = D3 . run ~ node ( : Dom_html . document . ## body ) ( Lazy . force view ) ! model and view = lazy ( View . make ( fun e -> model := Event . handle e !...
let main_lwt ( ) = let stream , push , _ = let stream , push = Lwt_stream . create ( ) in stream , ( fun x -> push ( Some x ) ) , ( fun ( ) -> push None ) in let view = View . make push in let init = { Model . init with Model . items = Storage . get ( ) } in ...
let _ = Lwt_js_events . async main_lwt
let add_cors_headers ( headers : Cohttp . Header . t ) : Cohttp . Header . t = Cohttp . Header . add_list headers [ ( " access - control - allow - origin " , " " ) ; * ( " access - control - allow - headers " , " Accept , Content - Type " ) ; ( " access...
let allow_cors = let filter handler req = handler req >>| fun response -> response |> Response . headers |> add_cors_headers |> Field . fset Response . Fields . headers response in Rock . Middleware . create ~ name ( : Info . of_string " allow cors " ) ~ filter
type todo = { id : int ; url : string ; title : string ; completed : bool ; order : int ; }
let stored_todos = ref TodoStorage . empty
let get_or_else t path default = let open Ezjsonm in if ( mem t path ) then ( find t path ) else default
let todo_of_json json id = let open Ezjsonm in { id = id ; url = " http :// 54 . 72 . 243 . 203 : 3000 / todos " / ^ string_of_int id ; title = get_string ( find json [ " title " ] ) ; completed = get_bool ( get_or_else json [ " completed " ] ( bool false ) ) ...
let updated_todo todo json = let open Ezjsonm in { id = todo . id ; url = " http :// 54 . 72 . 243 . 203 : 3000 / todos " / ^ string_of_int todo . id ; title = get_string ( get_or_else json [ " title " ] ( string todo . title ) ) ; completed = get_bool ( get_or_e...
let todo_of_json json = todo_of_json ( Ezjsonm . value json )
let json_of_todo { title ; url ; completed ; order ; _ } : Ezjsonm . t = let open Ezjsonm in dict [ " title " , ( string title ) ; " url " , ( string url ) ; " completed " , ( bool completed ) ; " order " , ( int order ) ]
let json_of_todos ( todos : todo list ) : Ezjsonm . t = ` A ( List . map todos ~ f ( : fun json -> Ezjsonm . value ( json_of_todo json ) ) )
let get_todos = get " / todos " begin fun _ -> let todos = TodoStorage . fold ( fun _ value acc -> value :: acc ) ! stored_todos [ ] ; in ` Json ( json_of_todos todos ) |> respond ' end
let get_todo = get " / todos /: id " begin fun req -> let todo = TodoStorage . find ( int_of_string ( param req " id " ) ) ! stored_todos ; in ` Json ( json_of_todo todo ) |> respond ' end
let patch_todo = App . patch " / todos /: id " begin fun request -> App . json_of_body_exn request >>| fun json -> let todo = TodoStorage . find ( int_of_string ( param request " id " ) ) ! stored_todos in let updated = updated_todo todo ( Ezjsonm . value json ) in stored_todos := ...
let delete_todo = App . delete " / todos /: id " begin fun request -> stored_todos := TodoStorage . remove ( int_of_string ( param request " id " ) ) ! stored_todos ; respond ' ( ` String " OK " ) end
let post_todos = post " / todos " begin fun request -> App . json_of_body_exn request >>| fun json -> let id = ( Random . int 100000 ) in let todo = todo_of_json json id in stored_todos := TodoStorage . add id todo ! stored_todos ; ` Json ( json_of_todo todo ) |> respond end
let delete_todos = delete " / todos " begin fun _ -> stored_todos := TodoStorage . empty ; respond ' ( ` String " OK " ) end
let accept_options = App . options " " ** begin fun _ -> respond ' ( ` String " OK " ) end
let _ = App . empty |> middleware allow_cors |> accept_options |> get_todos |> get_todo |> patch_todo |> delete_todo |> delete_todos |> post_todos |> App . run_command
let ( . ) <> f g x = f ( g x )
type action = | Add | Set of int * TodoList . status | Clear | UpdatePending of string
let string_of_action = function | Add -> " Add " | Set ( i , TodoList . TODO ) -> sprintf " Set ( % d , TODO ) " i | Set ( i , TodoList . DONE ) -> sprintf " Set ( % d , DONE ) " i | Clear -> " Clear " | UpdatePending s -> sprintf " UpdatePending % S " s
let render_item idx { TodoList . label = l ; status } = let id = sprintf " checkbox % d " idx in let open Html in let open TodoList in let checkbox ~ id ~ onchange ~ state = let attrs = [ A . type_ " checkbox " ; A . id id ; E . onchange ( fun ~ value -> onchange ) ] in let ...
let render_items = let open Html in function | [ ] -> em ( text " Items will appear here " ) | items -> ul ~ attrs [ : A . class_ " no - bullet " ] begin of_list ( List . mapi ( fun i -> li . <> render_item i ) items ) end
let render { TodoList . items ; pending_item } = let open Html in let div ~ classes html = div ~ attrs [ : A . class_ ( String . concat " " classes ) ] html in let text_input ~ classes ~ onenter ~ oninput ~ placeholder value = input ~ attrs [ : A . class_ ( String . concat " ...
let update = let open TodoList in function | Add -> add_pending_item | Set ( idx , status ) -> set idx status | Clear -> clear | UpdatePending text -> update_pending text
module ActionFilter = struct type t = action let relevant = function | UpdatePending _ -> false | _ -> true end
module Storage = struct let storage = match Js_browser . Window . local_storage Js_browser . window with | None -> failwith " Storage is not supported by this browser " | Some v -> v let key = " ocaml - vdom - todo - state " let find ( ) = Js_browser . Storage . get_item storage key let...
module Model = struct type visibility = Completed | Active | All [ @@ deriving json ] type task = { description : string ; backup : string ; completed : bool ; editing : bool ; id : int ; } [ @@ deriving json ] type t = { tasks : task list ; field : string ; uid : int ; vis...
module Action = struct type action = | Update_field of string | Editing_task of ( int * bool ) | Update_task of ( int * string ) | Add | Delete of int | Delete_complete | Check of ( int * bool ) | Check_all of bool | Change_visibility of Model . visibility | Escape of int | Nop end
module Controller = struct let update m a = let open Action in let open Model in let m = match a with | Add -> let uid = m . uid + 1 in let tasks = let v = String . trim m . field in if v = " " then m . tasks else ( new_task v m . uid ) :: m . tasks in { m with uid = uid ; field = ...
module View = struct open Model open Action let task_input m = Vdom . ( input ~ a [ : class_ " new - todo " ; str_prop " placeholder " " What needs to be done " ? ; autofocus ; value m . field ; oninput ( fun s -> Update_field s ) ; onkeydown ( fun e -> match e . which with...
let app = let m = Model . from_json @@ Storage . init @@ Model . to_json Model . empty in let m = match Js_browser . Location . get_hash ( ) with | " " -> m | hash -> match hash with | " " #/ -> { m with Model . visibility = Model . All } | " #/ active " -> { m with...
let run ( ) = Vdom_blit . run app |> Vdom_blit . dom |> Js_browser . Element . append_child ( Js_browser . Document . body Js_browser . document )
let ( ) = Js_browser . Window . set_onload Js_browser . window run
module type S = sig type todo = { id : int ; content : string ; } type error = | Database_error val migrate : unit -> ( unit , error ) result Lwt . t val rollback : unit -> ( unit , error ) result Lwt . t val get_all : unit -> ( todo list , error ) result Lwt . t val add : s...
module Make ( Db : module type of Db ) = struct let pool = Db . create_pool ( ) type todo = { id : int ; content : string ; } type error = | Database_error module Q = struct let create_todos_table = Caqti_request . exec Caqti_type . unit { | CREATE TABLE todos ( id SERIAL NOT NULL P...
type rounding_mode = FE_DOWNWARD | FE_TONEAREST | FE_TOWARDZERO | FE_UPWARD
let hex_of_float f = format_float " % a " f
let test1 ( mant : int64 ) ( exp : int ) = let expected = ldexp ( Int64 . to_float mant ) exp in let actual = Z . to_float ( Z . shift_left ( Z . of_int64 mant ) exp ) in if actual = expected then true else begin printf " % Ld * 2 ^% d : expected % s , got % s \ n " mant ...
let rnd64 ( ) = let m1 = Random . bits ( ) in let m2 = Random . bits ( ) in let m3 = Random . bits ( ) in Int64 . ( logor ( of_int m1 ) ( logor ( shift_left ( of_int m2 ) 30 ) ( shift_left ( of_int m3 ) 60 ) ) )
let testN numrounds = printf " ( % d tests ) . . . " %! numrounds ; let errors = ref 0 in for i = 1 to numrounds do let m = Random . int64 Int64 . max_int in if not ( test1 m 0 ) then incr errors ; if not ( test1 ( Int64 . neg m ) 0 ) then incr errors done ; for i = 1 t...
let testQ1 ( mant1 : int64 ) ( exp1 : int ) ( mant2 : int64 ) ( exp2 : int ) = let expected = ldexp ( Int64 . to_float mant1 ) exp1 . / ldexp ( Int64 . to_float mant2 ) exp2 in let actual = Q . to_float ( Q . make ( Z . shift_left ( Z . of_int64 mant1 ) exp1 ) ( ...
let testQN numrounds = printf " ( % d tests ) . . . " %! numrounds ; let errors = ref 0 in if not ( testQ1 0L 0 1L 0 ) then incr errors ; if not ( testQ1 1L 0 0L 0 ) then incr errors ; if not ( testQ1 ( - 1L ) 0 0L 0 ) then incr errors ; if not ( testQ1 0L 0 0...
let _ = let numrounds = if Array . length Sys . argv >= 2 then int_of_string Sys . argv . ( 1 ) else 100_000 in printf " Default rounding mode ( Z ) " ; testN numrounds ; printf " Default rounding mode ( Q ) " ; testQN numrounds ; if setround FE_TOWARDZERO then begin printf " ...