text
stringlengths
0
601k
let selector_of : type a . a def -> a selector = fun d -> let module D = ( val d ) in D . Me
let name : type a . a def -> string = fun d -> let module D = ( val d ) in D . name
let doc : type a . a def -> string = fun d -> let module D = ( val d ) in D . doc
let printer : type a . a def -> Format . formatter -> a -> unit = fun d -> let module D = ( val d ) in D . pp
let pp_def ppf d = Format . fprintf ppf " tag :% s " ( name d )
module Key = struct type t = V : ' a def -> t type s = S : ' a selector -> s let compare ( V k0 ) ( V k1 ) = compare ( S ( selector_of k0 ) ) ( S ( selector_of k1 ) ) end
module TagSet = Map . Make ( Key )
type t = V : ' a def * ' a -> t
type set = binding TagSet . t
let pp ppf ( V ( tag , v ) ) = Format . fprintf ppf " [ @< 1 ( >% a @ [ @% a ] ) ] " @@ pp_def tag ( printer tag ) v
let option_map f = function None -> None | Some v -> Some ( f v )
let option_bind f = function None -> None | Some v -> f v
let reveal2 : type a b . a def -> b def -> b -> a option = fun t u v -> match maybe_eq t u with None -> None | Some Refl -> Some v
let reveal : ' a . ' a def -> binding -> ' a option = fun tag -> function V ( another , v ) -> reveal2 tag another v
let unveil : ' a . ' a def -> binding option -> ' a option = fun tag -> option_bind @@ reveal tag
let conceal : ' a . ' a def -> ' a -> binding = fun tag v -> V ( tag , v )
let veil : ' a . ' a def -> ' a option -> binding option = fun tag -> option_map @@ conceal tag
let mem tag = TagSet . mem ( Key . V tag )
let add tag v = TagSet . add ( Key . V tag ) ( V ( tag , v ) )
let update tag f = TagSet . update ( Key . V tag ) ( fun b -> veil tag @@ f @@ unveil tag b )
let singleton tag v = TagSet . singleton ( Key . V tag ) ( V ( tag , v ) )
let remove tag = TagSet . remove ( Key . V tag )
type merger = { merger : ' a . ' a def -> ' a option -> ' a option -> ' a option }
let merge f = TagSet . merge @@ function | Key . V tag -> fun a b -> veil tag @@ f . merger tag ( unveil tag a ) ( unveil tag b )
type unioner = { unioner : ' a . ' a def -> ' a -> ' a -> ' a }
let union f = merge { merger = ( fun tag a b -> match ( a , b ) with | ( Some aa , Some bb ) -> Some ( f . unioner tag aa bb ) | ( Some _ , None ) -> a | ( None , _ ) -> b ) ; }
let iter f = TagSet . iter ( fun _ -> f )
let fold f = TagSet . fold ( fun _ -> f )
let for_all p = TagSet . for_all ( fun _ -> p )
let exists p = TagSet . exists ( fun _ -> p )
let filter p = TagSet . filter ( fun _ -> p )
let partition p = TagSet . partition ( fun _ -> p )
let bindings s = List . map snd @@ TagSet . bindings s
let min_binding s = snd @@ TagSet . min_binding s
let min_binding_opt s = option_map snd @@ TagSet . min_binding_opt s
let max_binding s = snd @@ TagSet . max_binding s
let max_binding_opt s = option_map snd @@ TagSet . max_binding_opt s
let choose s = snd @@ TagSet . choose s
let choose_opt s = option_map snd @@ TagSet . choose_opt s
let split tag s = ( fun ( l , m , r ) -> ( l , unveil tag m , r ) ) @@ TagSet . split ( Key . V tag ) s
let find tag s = option_bind ( reveal tag ) @@ TagSet . find_opt ( Key . V tag ) s
let find_opt tag s = option_bind ( reveal tag ) @@ TagSet . find_opt ( Key . V tag ) s
let get tag s = find_opt tag s |> function | None -> invalid_arg ( Format . asprintf " tag named % s not found in set " ( name tag ) ) | Some v -> v
let find_first p s = snd @@ TagSet . find_first p s
let find_first_opt p s = option_map snd @@ TagSet . find_first_opt p s
let find_last p s = snd @@ TagSet . find_last p s
let find_last_opt p s = option_map snd @@ TagSet . find_last_opt p s
let pp_set ppf s = Format . ( fprintf ppf " [ @< 1 { " > ; pp_print_list pp ppf ( bindings s ) ; Format . fprintf ppf " } ] " ) @
module DSL = struct type ( _ , _ , _ , _ ) arg = | A : ( ' x def * ' x ) -> ( ( ' b -> ' x -> ' c ) -> ' x -> ' d , ' b , ' c , ' d ) arg | S : ( ' x def * ' x ) -> ( ' x -> ' d , ' b , ' c , ' d ) arg | T : ( ' x def * ' x ) -> ( ' d , ' b , ' c , ' d ) arg let a tag v = A ( tag , v ) let s tag v = S ( tag , v ) let t...
type t = | Bool | Int | Real | Id | String
let rec has ( tags : tag list ) ( id : Id . t ) = match tags with | [ ] -> false | TId ( name , _ ) :: _ when id = name -> true | TFun ( name , _ , _ ) :: _ when id = name -> true | _ :: t -> has t id
let is_empty ( tags : tag list ) = tags = [ ]
let getLocation ( attr : tag ) : Loc . t = match attr with | TBool ( _ , loc ) -> loc | TInt ( _ , loc ) -> loc | TId ( _ , loc ) -> loc | TReal ( _ , loc ) -> loc | TFun ( _ , _ , loc ) -> loc | TString ( _ , loc ) -> loc
let getType ( attr : tag ) : string = match attr with | TBool ( _ , _ ) -> " integer " | TInt ( _ , _ ) -> " integer " | TId ( _ , _ ) -> " identifier " | TReal ( _ , _ ) -> " real " | TFun ( _ , _ , _ ) -> " tag " | TString ( _ , _ ) -> " string "
let getTypeLiteral ( t : t ) : string = match t with | Bool -> " bool " | Int -> " integer " | Real -> " real " | Id -> " identifier " | String -> " string "
let rec getParam ( remaining : ( Id . t * tag ) list ) ( args : ( Id . t * tag ) list ) ( id : string ) = match args with | [ ] -> remaining , None | ( name , value ) :: t when name = [ id ] -> remaining @ t , Some value | h :: t -> getParam ( h :: remaining ) t id
let getTypedParam ( args : ( Id . t * tag ) list ) ( id , typ ) = let lattr loc = { emptyAttr with loc } in match getParam [ ] args id with | r , Some ( TReal ( value , loc ) ) when typ = Real -> r , Some ( PReal ( float_of_string value , Float , lattr loc ) ) | r , Some ( TInt ( value , loc ) ) when typ = Int -> r , S...
let getParameterList ( args : ( Id . t * tag ) list ) ( params : ( string * t ) list ) : ( Id . t * tag ) list * exp option list = let rec loop remaning found params = match params with | [ ] -> remaning , List . rev found | h :: t -> let remaining , value = getTypedParam remaning h in loop remaining ( value :: found )...
let getTableIndividualParams ( loc : Loc . t ) params args = let remaining , found = getParameterList args params in match remaining with | _ :: _ -> let params_s = List . map ( fun ( id , _ ) -> PrintProg . identifierStr id ) remaining |> String . concat " , " in let msg = " The following arguments are unknown for the...
let rec getTableParams ( fname : string ) ( params : ( string * t ) list ) ( msg : string ) ( attr : tag list ) = match attr with | [ ] -> None | TFun ( name , args , loc ) :: _ when name = [ fname ] -> Some ( loc , getTableIndividualParams loc params args ) | _ :: t -> getTableParams fname params msg t
let rec removeAttrFunc ( fname : string ) ( attr : tag list ) : tag list = match attr with | [ ] -> [ ] | TFun ( name , _ , _ ) :: t when name = [ fname ] -> removeAttrFunc fname t | h :: t -> h :: removeAttrFunc fname t
type ( ' tag , ' a ) node = Node of ' a * ( ' tag * ( ' tag , ' a ) node ) LazyList . t
module type Tree = sig type tag include Monad . BaseCollectionM include Applicative . Base with type ' a m := ' a m val root : ' a m -> ' a LazyList . t val children : ' a m -> ( tag * ' a m ) LazyList . t val count : ' a m -> int val to_list : ' a m -> ' a LazyList . t val collapse : ' a m -> ' a m val with_tags : ' a...
module Make ( Tag : sig type t val cmp : t -> t -> bool val print : t -> unit struct open LazyList open Printf type tag = Tag . t type ' a m = ( Tag . t , ' a LazyList . t ) node let root ( Node ( x , _ ) ) = x let children ( Node ( _ , cs ) ) = cs let return x = Node ( singleton x , nil ) let rec to_list ( Node ( x , ...
module A : sig type t type t end
module rec A : sig type t type t end type t type t end
module type A = B
module type A = sig type t end
module A : sig type t B
type t = t object method f = 0 inherit a val x = 1 constraint ' a = [ > ] initializer do_init ( ) end type t end
module A : sig type t type t end
module rec A : sig type t type t end type t type t end
module type A = B
module type A = sig type t end
module A : sig type t B
type t = t object method f = 0 inherit a val x = 1 constraint ' a = [ > ] initializer do_init ( ) end
type t = { seconds : uint64 ; nanoseconds : uint32 }
let base = Uint64 . of_string " 0x400000000000000a "
let billion = Uint64 . of_int 1000000000
let time_to_t time = let since_epoch = Time_ns . to_span_since_epoch time in let ns_since_epoch = Time_ns . Span . to_int63_ns since_epoch |> Int63 . to_int64 |> Uint64 . of_int64 in let seconds , nanoseconds = Uint64 . ( base + ( ns_since_epoch / billion ) , rem ns_since_epoch billion ) in { seconds ; nanoseconds = Ui...
let epoch = time_to_t Time_ns . epoch
let to_bytes { seconds ; nanoseconds } = let buf = Bytes . create 12 in Uint64 . to_bytes_big_endian seconds buf 0 ; Uint32 . to_bytes_big_endian nanoseconds buf 8 ; buf
let of_bytes buf = let secs = Uint64 . of_bytes_big_endian buf 0 in let ns = Uint32 . of_bytes_big_endian buf 8 in { seconds = secs ; nanoseconds = ns }
let to_time t = let ns_total = Uint64 . ( ( billion * t . seconds ) + of_uint32 t . nanoseconds ) |> Uint64 . to_int64 |> Int63 . of_int64_exn in Time_ns . of_int63_ns_since_epoch ns_total
let diff t1 t2 = let time_1 , time_2 = ( to_time t1 , to_time t2 ) in Time_ns . diff time_1 time_2
let whitener_mask = Uint32 . of_int ( 0x1000000 - 1 )
let whiten { seconds ; nanoseconds } = let nanoseconds = Uint32 . logand nanoseconds ( Uint32 . lognot whitener_mask ) in { seconds ; nanoseconds }
let get_timestamp time = time |> time_to_t |> whiten
let now ( ) = get_timestamp ( Time_ns . now ( ) )
let since t = diff ( now ( ) ) t
let after t1 t2 = let b1 , b2 = ( to_bytes t1 , to_bytes t2 ) in Bytes . compare b1 b2 > 0
let sleep_period = Time_ns . Span . to_sec ( Time_ns . Span . of_int_ns ( Uint32 . to_int whitener_mask ) ) let old = ref ( now ( ) ) in for _ = 0 to 50 do let next = now ( ) in if after next ! old then print_s [ % message " whitening insufficient " ] ; Unix . nanosleep sleep_period |> ignore ; let next = now ( ) in if...
let rec gcd ( m , n ) = if m = n then m else if m < n then gcd ( m , n - m ) else gcd ( m - n , n )
let l1 = List . map gcd [ ( 24 , 638 ) ; ( 17 , 289 ) ; ( 31 , 1889 ) ]
let gcd_iter ( m , n ) = let rm = ref m in let rn = ref n in let result = ref 0 in let not_done = ref true in let _ = while ! not_done do if ! rm = ! rn then ( not_done := false ; result := ! rm ) else if ! rm < ! rn then rn := ! rn - ! rm else rm := ! rm - ! rn done in ! result
let l2 = List . map gcd_iter [ ( 24 , 638 ) ; ( 17 , 289 ) ; ( 31 , 1889 ) ]
let rec my_while ( test , body ) ( ) = if test ( ) then my_while ( test , body ) ( body ( ) )
let my_while_iter ( test , body ) ( ) = while test ( ) do body ( ) done
let prog = { | let log_success ( ) = print_endline " Success " ! let log_failure = Printf . printf " Failure ! % s " let fun1 ( ) = let rec odd x = if x = 0 then false else even ( x - 1 ) and even x = if x = 0 then true else odd ( x - 1 ) in assert ( odd 1 <> even 1 ) ; try ignore ( odd 5000 ) ; log_success ( ) with _ ...
let get_backtrace ( ) = raw_backtrace_entries ( get_callstack 10 )