text
stringlengths
12
786k
let failwith s = raise ( Failure s )
let invalid_arg s = raise ( Invalid_argument s )
type ' a t = | Empty | Node of { l : ' a t ; v : key ; d : ' a ; r : ' a t ; h : int }
let rec merge f s1 s2 = match s1 , s2 with | Empty , Empty -> Empty | Node { l = l1 ; v = v1 ; d = d1 ; r = r1 ; h = h1 } , _ when h1 >= match s2 with Empty -> 0 | Node _ -> 1 -> merge f l1 r1 | _ , Node { l = l2 ; v = v2 ; d = d2 ; r = r2 } -> merge f l2 r2 | _ ...
module IntMap = Map . Make ( struct type t = int let compare x y = x - y end )
let m1 = IntMap . add 0 " A " ( IntMap . add 4 " Y " ( IntMap . singleton 3 " X1 " ) )
let m2 = IntMap . add 0 " B " ( IntMap . add 4 " Y " ( IntMap . singleton 5 " X2 " ) )
let show m = IntMap . iter ( fun k v -> Printf . printf " % d % s \ n " k v ) m
let ( ) = print_endline " Union + concat " ; let f1 _ l r = match l , r with | Some x , None | None , Some x -> Some x | Some x , Some y when x = y -> None | Some x , Some y -> Some ( x ^ y ) | _ -> assert false in show ( IntMap . merge f1 m1 m2 ) ; print_endline " Inte...
let show m = IntMap . iter ( fun k v -> Printf . printf " % d -> % d \ n " k v ) m
let update x f m = let yp = IntMap . find_opt x m in let y = f yp in match yp , y with | _ , None -> IntMap . remove x m | None , Some z -> IntMap . add x z m | Some zp , Some z -> if zp == z then m else IntMap . add x z m
let ( ) = print_endline " Update " ; let rec init m = function | - 1 -> m | n -> init ( IntMap . add n n m ) ( n - 1 ) in let n = 9 in let m = init IntMap . empty n in for i = 0 to n + 1 do for j = 0 to n + 1 do List . iter ( function ( k , f ) -> let m1 = update i...
let module T = Map . Test in let t = [ 1 , " 1 " ; 2 , " 2 " ; 3 , " 3 " ] in Test_lib . test_encode ( module T ) t ; [ % expect { | m { key : 1 value : " 1 " } m { key : 2 value : " 2 " } m { key : 3 value : " 3 " } } ] | let mo...
type extern_flags = No_sharing | Closures | Compat_32 = " caml_output_value " = " caml_output_value_to_bytes " = " caml_output_value_to_string " bytes -> int -> int -> ' a -> extern_flags list -> int = " caml_output_value_to_buffer "
let to_buffer buff ofs len v flags = if ofs < 0 || len < 0 || ofs > Bytes . length buff - len then invalid_arg " Marshal . to_buffer : substring out of bounds " else to_buffer_unsafe buff ofs len v flags
let data_size buff ofs = if ofs < 0 || ofs > Bytes . length buff - header_size then invalid_arg " Marshal . data_size " else data_size_unsafe buff ofs
let total_size buff ofs = header_size + data_size buff ofs
let from_bytes buff ofs = if ofs < 0 || ofs > Bytes . length buff - header_size then invalid_arg " Marshal . from_bytes " else begin let len = data_size_unsafe buff ofs in if ofs > Bytes . length buff - ( header_size + len ) then invalid_arg " Marshal . from_bytes " else from_bytes_unsafe ...
let from_string buff ofs = from_bytes ( Bytes . unsafe_of_string buff ) ofs
module MayCompat = Parmatch . Compat ( struct let equal = Types . may_equal_constr end )
let string_of_lam lam = Printlambda . lambda Format . str_formatter lam ; Format . flush_str_formatter ( )
let all_record_args lbls = match lbls with | [ ] -> fatal_error " Matching . all_record_args " | ( _ , { lbl_all } , _ ) :: _ -> let t = Array . map ( fun lbl -> ( mknoloc ( Longident . Lident " ? temp " ) , ? lbl , Patterns . omega ) ) lbl_all in List . it...
let expand_record_head h = let open Patterns . Head in match h . pat_desc with | Record [ ] -> fatal_error " Matching . expand_record_head " | Record ( { lbl_all } :: _ ) -> { h with pat_desc = Record ( Array . to_list lbl_all ) } | _ -> h
let head_loc ~ scopes head = Scoped_location . of_location ~ scopes head . pat_loc
type ' a clause = ' a * lambda
let map_on_row f ( row , action ) = ( f row , action )
let map_on_rows f = List . map ( map_on_row f )
module General = struct include Patterns . General type nonrec clause = pattern Non_empty_row . t clause end
module Half_simple : sig include module type of Patterns . Half_simple type nonrec clause = pattern Non_empty_row . t clause val of_clause : arg : lambda -> General . clause -> clause include Patterns . Half_simple type nonrec clause = pattern Non_empty_row . t clause let rec simpl_under_orpat p = mat...
module Simple : sig include module type of Patterns . Simple type nonrec clause = pattern Non_empty_row . t clause val head : pattern -> Patterns . Head . t val explode_or_pat : Half_simple . pattern * Typedtree . pattern list -> arg_id : Ident . t option -> mk_action ( : vars : Ident . t l...
let expand_record_simple : Simple . pattern -> Simple . pattern = fun p -> match p . pat_desc with | ` Record ( l , _ ) -> { p with pat_desc = ` Record ( all_record_args l , Closed ) } | _ -> p
type initial_clause = pattern list clause
type matrix = pattern list list
let add_omega_column pss = List . map ( fun ps -> Patterns . omega :: ps ) pss
let rec rev_split_at n ps = if n <= 0 then ( [ ] , ps ) else match ps with | p :: rem -> let left , right = rev_split_at ( n - 1 ) rem in ( p :: left , right ) | _ -> assert false
let matcher discr ( p : Simple . pattern ) rem = let discr = expand_record_head discr in let p = expand_record_simple p in let omegas = Patterns . ( omegas ( Head . arity discr ) ) in let ph , args = Patterns . Head . deconstruct p in let yes ( ) = args @ rem in let no ( ) = rai...
let ncols = function | [ ] -> 0 | ps :: _ -> List . length ps
module Context : sig type t val empty : t val is_empty : t -> bool val start : int -> t val eprintf : t -> unit val specialize : Patterns . Head . t -> t -> t val lshift : t -> t val rshift : t -> t val rshift_num : int -> t -> t val lub : pattern -> t -> t val matches : t -> matrix -> bool ...
let rec flatten_pat_line size p k = match p . pat_desc with | Tpat_any -> Patterns . omegas size :: k | Tpat_tuple args -> args :: k | Tpat_or ( p1 , p2 , _ ) -> flatten_pat_line size p1 ( flatten_pat_line size p2 k ) | Tpat_alias ( p , _ , _ ) -> flatten_pat_line size p k | _ -...
let flatten_matrix size pss = List . fold_right ( fun ps r -> match ps with | [ p ] -> flatten_pat_line size p r | _ -> fatal_error " Matching . flatten_matrix " ) pss [ ]
module Default_environment : sig type t val is_empty : t -> bool val pop : t -> ( ( matrix * int ) * t ) option val empty : t val cons : matrix -> int -> t -> t val specialize : Patterns . Head . t -> t -> t val pop_column : t -> t val pop_compat : pattern -> t -> t val flatten : int -...
module Jumps : sig type t val is_empty : t -> bool val empty : t val singleton : int -> Context . t -> t val add : int -> Context . t -> t -> t val union : t -> t -> t val unions : t list -> t val map : ( Context . t -> Context . t ) -> t -> t val remove : int -> t -> t val extract :...
type ' row pattern_matching = { mutable cases : ' row list ; args : ( lambda * let_kind ) list ; default : Default_environment . t }
type handler = { provenance : matrix ; exit : int ; vars : ( Ident . t * Lambda . value_kind ) list ; pm : initial_clause pattern_matching }
type ' head_pat pm_or_compiled = { body : ' head_pat Non_empty_row . t clause pattern_matching ; handlers : handler list ; or_matrix : matrix }
type pm_half_compiled = | PmOr of Simple . pattern pm_or_compiled | PmVar of { inside : pm_half_compiled } | Pm of Simple . clause pattern_matching
type pm_half_compiled_info = { me : pm_half_compiled ; matrix : matrix ; top_default : Default_environment . t }
let erase_cases f cases = List . map ( fun ( ( p , ps ) , act ) -> ( f p :: ps , act ) ) cases
let erase_pm pm = { pm with cases = erase_cases General . erase pm . cases }
let pretty_cases cases = List . iter ( fun ( ps , _l ) -> List . iter ( fun p -> Format . eprintf " % a " %! top_pretty p ) ps ; Format . eprintf " \ n " ) cases
let pretty_pm pm = pretty_cases pm . cases ; if not ( Default_environment . is_empty pm . default ) then Default_environment . pp pm . default
let rec pretty_precompiled = function | Pm pm -> Format . eprintf " ++++ PM ++++\ n " ; pretty_pm ( erase_pm pm ) | PmVar x -> Format . eprintf " ++++ VAR ++++\ n " ; pretty_precompiled x . inside | PmOr x -> Format . eprintf " ++++ OR ++++\ n " ; pretty_pm ( erase_pm x . ...
let pretty_precompiled_res first nexts = pretty_precompiled first ; List . iter ( fun ( e , pmh ) -> eprintf " ** DEFAULT % d **\ n " e ; pretty_precompiled pmh ) nexts
module StoreExp = Switch . Store ( struct type t = lambda type key = lambda let compare_key = Stdlib . compare let make_key = Lambda . make_key end )
let make_exit i = Lstaticraise ( i , [ ] )
let make_catch kind d k = match d with | Lstaticraise ( _ , [ ] ) -> k d | _ -> let e = next_raise_count ( ) in Lstaticcatch ( k ( make_exit e ) , ( e , [ ] ) , d , kind )
let rec as_simple_exit = function | Lstaticraise ( i , [ ] ) -> Some i | Llet ( Alias , _k , _ , _ , e ) -> as_simple_exit e | _ -> None
let make_catch_delayed kind handler = match as_simple_exit handler with | Some i -> ( i , fun act -> act ) | None -> ( let i = next_raise_count ( ) in ( i , fun body -> match body with | Lstaticraise ( j , _ ) -> if i = j then handler else body | _ -> Lstaticcatch ( body , ( i...
let raw_action l = match make_key l with | Some l -> l | None -> l
let same_actions = function | [ ] -> None | [ ( _ , act ) ] -> Some act | ( _ , act0 ) :: rem -> ( match make_key act0 with | None -> None | key0_opt -> let same_act ( _ , act ) = make_key act = key0_opt in if List . for_all same_act rem then Some act0 else None )
let safe_before ( ( p , ps ) , act_p ) l = let same_actions act1 act2 = match ( make_key act1 , make_key act2 ) with | Some key1 , Some key2 -> key1 = key2 | None , _ | _ , None -> false in List . for_all ( fun ( ( q , qs ) , act_q ) -> same_actions act_p act_q || not...
let half_simplify_nonempty ~ arg ( cls : Typedtree . pattern Non_empty_row . t clause ) : Half_simple . clause = cls |> map_on_row ( Non_empty_row . map_first General . view ) |> Half_simple . of_clause ~ arg
let half_simplify_clause ~ arg ( cls : Typedtree . pattern list clause ) = cls |> map_on_row Non_empty_row . of_initial |> half_simplify_nonempty ~ arg
let rec what_is_cases ~ skip_any cases = match cases with | [ ] -> Patterns . Head . omega | ( ( p , _ ) , _ ) :: rem -> ( let head = Simple . head p in match head . pat_desc with | Patterns . Head . Any when skip_any -> what_is_cases ~ skip_any rem | _ -> head )
let what_is_first_case = what_is_cases ~ skip_any : false
let what_is_cases = what_is_cases ~ skip_any : true
let pm_free_variables { cases } = List . fold_right ( fun ( _ , act ) r -> Ident . Set . union ( free_variables act ) r ) cases Ident . Set . empty
let can_group discr pat = let open Patterns . Head in match ( discr . pat_desc , ( Simple . head pat ) . pat_desc ) with | Any , Any | Constant ( Const_int _ ) , Constant ( Const_int _ ) | Constant ( Const_char _ ) , Constant ( Const_char _ ) | Constant ( Const_string ...
let is_or p = match p . pat_desc with | Tpat_or _ -> true | _ -> false
let rec omega_like p = match p . pat_desc with | Tpat_any | Tpat_var _ -> true | Tpat_alias ( p , _ , _ ) -> omega_like p | Tpat_or ( p1 , p2 , _ ) -> omega_like p1 || omega_like p2 | _ -> false
let simple_omega_like p = match ( Simple . head p ) . pat_desc with | Any -> true | _ -> false
let equiv_pat p q = le_pat p q && le_pat q p
let rec extract_equiv_head p l = match l with | ( ( ( q , _ ) , _ ) as cl ) :: rem -> if equiv_pat p ( General . erase q ) then let others , rem = extract_equiv_head p rem in ( cl :: others , rem ) else ( [ ] , l ) | _ -> ( [ ] , l )
module Or_matrix = struct let disjoint p q = not ( may_compat p q ) let safe_below ( ps , act ) qs = ( not ( is_guarded act ) ) && Parmatch . le_pats ps qs let safe_below_or_matrix l ( q , qs ) = List . for_all ( fun ( ( p , ps ) , act_p ) -> let p = General . erase p ...
let as_matrix cases = get_mins le_pats ( List . map ( fun ( ( p , ps ) , _ ) -> General . erase p :: ps ) cases )
let rec split_or ~ arg_id ( cls : Half_simple . clause list ) args def = let rec do_split ( rev_before : Simple . clause list ) rev_ors rev_no = function | [ ] -> cons_next ( List . rev rev_before ) ( List . rev rev_ors ) ( List . rev rev_no ) | cl :: rem when not ( safe_bef...
let dbg_split_and_precompile pm next nexts = if dbg && ( nexts <> [ ] || match next with | PmOr _ -> true | _ -> false ) then ( Format . eprintf " ** SPLIT **\ n " ; pretty_pm ( erase_pm pm ) ; pretty_precompiled_res next nexts )
let split_and_precompile_simplified pm = let { me = next } , nexts = split_no_or pm . cases pm . args pm . default [ ] in dbg_split_and_precompile pm next nexts ; ( next , nexts )
let split_and_precompile_half_simplified ~ arg_id pm = let { me = next } , nexts = split_or ~ arg_id pm . cases pm . args pm . default in dbg_split_and_precompile pm next nexts ; ( next , nexts )
let split_and_precompile ~ arg_id ~ arg_lambda pm = let pm = { pm with cases = List . map ( half_simplify_clause ~ arg : arg_lambda ) pm . cases } in split_and_precompile_half_simplified ~ arg_id pm
type cell = { pm : initial_clause pattern_matching ; ctx : Context . t ; discr : Patterns . Head . t }
let make_matching get_expr_args head def ctx = function | [ ] -> fatal_error " Matching . make_matching " | arg :: rem -> let def = Default_environment . specialize head def and args = get_expr_args head arg rem and ctx = Context . specialize head ctx in { pm = { cases = [ ] ; args ; ...
let make_line_matching get_expr_args head def = function | [ ] -> fatal_error " Matching . make_line_matching " | arg :: rem -> { cases = [ ] ; args = get_expr_args head arg rem ; default = Default_environment . specialize head def }
type ' a division = { args : ( lambda * let_kind ) list ; cells : ( ' a * cell ) list }
let add_in_div make_matching_fun eq_key key patl_action division = let cells = match List . find_opt ( fun ( k , _ ) -> eq_key key k ) division . cells with | None -> let cell = make_matching_fun division . args in cell . pm . cases <- [ patl_action ] ; ( key , cell ) :: divisio...
let divide get_expr_args eq_key get_key get_pat_args ctx ( pm : Simple . clause pattern_matching ) = let add ( ( p , patl ) , action ) division = let ph = Simple . head p in let p = General . erase p in add_in_div ( make_matching get_expr_args ph pm . default ctx ) eq_key ( get_key p ...
let add_line patl_action pm = pm . cases <- patl_action :: pm . cases ; pm
let divide_line make_ctx get_expr_args get_pat_args discr ctx ( pm : Simple . clause pattern_matching ) = let add ( ( p , patl ) , action ) submatrix = let p = General . erase p in add_line ( get_pat_args p patl , action ) submatrix in let pm = List . fold_right add pm . cases ( mak...
let drop_pat_arg _p rem = rem
let drop_expr_arg _head _arg rem = rem
let get_key_constant caller = function | { pat_desc = Tpat_constant cst } -> cst | p -> Format . eprintf " BAD : % s " caller ; pretty_pat p ; assert false
let divide_constant ctx m = divide get_expr_args_constant ( fun c d -> const_compare c d = 0 ) ( get_key_constant " divide " ) get_pat_args_constant ctx m
let get_key_constr = function | { pat_desc = Tpat_construct ( _ , cstr , _ ) } -> cstr | _ -> assert false
let get_pat_args_constr p rem = match p with | { pat_desc = Tpat_construct ( _ , _ , args ) } -> args @ rem | _ -> assert false
let get_expr_args_constr ~ scopes head ( arg , _mut ) rem = let cstr = match head . pat_desc with | Patterns . Head . Construct cstr -> cstr | _ -> fatal_error " Matching . get_expr_args_constr " in let loc = head_loc ~ scopes head in let make_field_accesses binding_kind first_pos last_pos ...
let divide_constructor ~ scopes ctx pm = divide ( get_expr_args_constr ~ scopes ) ( fun cstr1 cstr2 -> Types . equal_tag cstr1 . cstr_tag cstr2 . cstr_tag ) get_key_constr get_pat_args_constr ctx pm
let nonconstant_variant_field index = Lambda . Pfield ( index , Reads_agree )
let get_expr_args_variant_nonconst ~ scopes head ( arg , _mut ) rem = let loc = head_loc ~ scopes head in let field_prim = nonconstant_variant_field 1 in ( Lprim ( field_prim , [ arg ] , loc ) , Alias ) :: rem
let divide_variant ~ scopes row ctx { cases = cl ; args ; default = def } = let row = Btype . row_repr row in let rec divide = function | [ ] -> { args ; cells = [ ] } | ( ( p , patl ) , action ) :: rem -> ( let lab , pato = match p . pat_desc with | ` Variant (...
let divide_var ctx pm = divide_line Context . lshift get_expr_args_var get_pat_args_var Patterns . Head . omega ctx pm