text
stringlengths 0
601k
|
---|
let bin_reader_int_64bit = { read = Read . bin_read_int_64bit ; vtag_read = variant_wrong_type " int_64bit " } ; ; |
let bin_int_64bit = { shape = bin_shape_int_64bit ; writer = bin_writer_int_64bit ; reader = bin_reader_int_64bit } ; ; |
let bin_writer_int64_bits = { size = Size . bin_size_int64_bits ; write = Write . bin_write_int64_bits } ; ; |
let bin_reader_int64_bits = { read = Read . bin_read_int64_bits ; vtag_read = variant_wrong_type " int64_bits " } ; ; |
let bin_int64_bits = { shape = bin_shape_int64_bits ; writer = bin_writer_int64_bits ; reader = bin_reader_int64_bits } ; ; |
let bin_writer_network16_int = { size = Size . bin_size_network16_int ; write = Write . bin_write_network16_int } ; ; |
let bin_reader_network16_int = { read = Read . bin_read_network16_int ; vtag_read = variant_wrong_type " network16_int " } ; ; |
let bin_network16_int = { shape = bin_shape_network16_int ; writer = bin_writer_network16_int ; reader = bin_reader_network16_int } ; ; |
let bin_writer_network32_int = { size = Size . bin_size_network32_int ; write = Write . bin_write_network32_int } ; ; |
let bin_reader_network32_int = { read = Read . bin_read_network32_int ; vtag_read = variant_wrong_type " network32_int " } ; ; |
let bin_network32_int = { shape = bin_shape_network32_int ; writer = bin_writer_network32_int ; reader = bin_reader_network32_int } ; ; |
let bin_writer_network32_int32 = { size = Size . bin_size_network32_int32 ; write = Write . bin_write_network32_int32 } ; ; |
let bin_reader_network32_int32 = { read = Read . bin_read_network32_int32 ; vtag_read = variant_wrong_type " network32_int32 " } ; ; |
let bin_network32_int32 = { shape = bin_shape_network32_int32 ; writer = bin_writer_network32_int32 ; reader = bin_reader_network32_int32 } ; ; |
let bin_writer_network64_int = { size = Size . bin_size_network64_int ; write = Write . bin_write_network64_int } ; ; |
let bin_reader_network64_int = { read = Read . bin_read_network64_int ; vtag_read = variant_wrong_type " network64_int " } ; ; |
let bin_network64_int = { shape = bin_shape_network64_int ; writer = bin_writer_network64_int ; reader = bin_reader_network64_int } ; ; |
let bin_writer_network64_int64 = { size = Size . bin_size_network64_int64 ; write = Write . bin_write_network64_int64 } ; ; |
let bin_reader_network64_int64 = { read = Read . bin_read_network64_int64 ; vtag_read = variant_wrong_type " network64_int64 " } ; ; |
let bin_network64_int64 = { shape = bin_shape_network64_int64 ; writer = bin_writer_network64_int64 ; reader = bin_reader_network64_int64 } ; ; |
let bin_writer_array_no_length bin_writer_el = { size = ( fun v -> ( Size . bin_size_array_no_length [ @ warning " - 3 " ] ) bin_writer_el . size v ) ; write = ( fun buf ~ pos v -> ( Write . bin_write_array_no_length [ @ warning " - 3 " ] ) bin_writer_el . write buf ~ pos v ) } ; ; |
let cnv_writer cnv tp_class = { size = ( fun v -> tp_class . size ( cnv v ) ) ; write = ( fun buf ~ pos v -> tp_class . write buf ~ pos ( cnv v ) ) } ; ; |
let cnv_reader cnv tp_class = { read = ( fun buf ~ pos_ref -> cnv ( tp_class . read buf ~ pos_ref ) ) ; vtag_read = ( fun buf ~ pos_ref vtag -> cnv ( tp_class . vtag_read buf ~ pos_ref vtag ) ) } ; ; |
let cnv for_shape for_writer for_reader tp_class = { shape = for_shape tp_class . shape ; writer = cnv_writer for_writer tp_class . writer ; reader = cnv_reader for_reader tp_class . reader } ; ; |
let { Logger . log } = Logger . for_section log_section |
type type_info = | Modtype of Env . t * Types . module_type | Type of Env . t * Types . type_expr | Type_decl of Env . t * Ident . t * Types . type_declaration | String of string |
type typed_enclosings = ( Location . t * type_info * Query_protocol . is_tail_position ) list |
let from_nodes ~ path = let aux ( env , node , tail ) = let open Browse_raw in let ret x = Some ( Mbrowse . node_loc node , x , tail ) in match [ @ ocaml . warning " - 9 " ] node with | Expression { exp_type = t } | Pattern { pat_type = t } | Core_type { ctyp_type = t } | Value_description { val_desc = { ctyp_type = t } } -> ret ( Type ( env , t ) ) | Type_declaration { typ_id = id ; typ_type = t } -> ret ( Type_decl ( env , id , t ) ) | Module_expr { mod_type = m } | Module_type { mty_type = m } | Module_binding { mb_expr = { mod_type = m } } | Module_declaration { md_type = { mty_type = m } } | Module_type_declaration { mtd_type = Some { mty_type = m } } | Module_binding_name { mb_expr = { mod_type = m } } | Module_declaration_name { md_type = { mty_type = m } } | Module_type_declaration_name { mtd_type = Some { mty_type = m } } -> ret ( Modtype ( env , m ) ) | _ -> None in List . filter_map ~ f : aux path |
let from_reconstructed ~ nodes ~ cursor ~ verbosity exprs = let open Browse_raw in let env , node = Mbrowse . leaf_node nodes in log ~ title " : from_reconstructed " " node = % s \ nexprs = [ % s ] " ( Browse_raw . string_of_node node ) ( String . concat ~ sep " ; " : ( List . map exprs ~ f ( : fun l -> l . Location . txt ) ) ) ; let include_lident = match node with | Pattern _ -> false | _ -> true in let include_uident = match node with | Module_binding _ | Module_binding_name _ | Module_declaration _ | Module_declaration_name _ | Module_type_declaration _ | Module_type_declaration_name _ -> false | _ -> true in let get_context lident = Context . inspect_browse_tree ~ cursor ( Longident . parse lident ) [ nodes ] in let f = fun { Location . txt = source ; loc } -> let context = get_context source in Option . iter context ~ f ( : fun ctx -> log ~ title " : from_reconstructed " " source = % s ; context = % s " source ( Context . to_string ctx ) ) ; match context with | Some ( Context . Constructor cd ) -> log ~ title " : from_reconstructed " " ctx : constructor % s " cd . cstr_name ; Some ( Mbrowse . node_loc node , Type ( env , cd . cstr_res ) , ` No ) | _ -> let context = Option . value ~ default : Context . Expr context in match source with | " " -> log ~ title " : from_reconstructed " " no reconstructed identifier " ; None | source when not include_lident && Char . is_lowercase source . [ 0 ] -> log ~ title " : from_reconstructed " " skipping lident " ; None | source when not include_uident && Char . is_uppercase source . [ 0 ] -> log ~ title " : from_reconstructed " " skipping uident " ; None | source -> try let ppf , to_string = Format . to_string ( ) in if Type_utils . type_in_env ~ verbosity ~ context env ppf source then ( log ~ title " : from_reconstructed " " typed % s " source ; Some ( loc , String ( to_string ( ) ) , ` No ) ) else ( log ~ title " : from_reconstructed " " FAILED to type % s " source ; None ) with _ -> None in List . filter_map exprs ~ f |
type ( ' a , ' b ) t = T : ( ' a , ' a ) t [ @@ deriving_inline sexp_of ] |
let sexp_of_t : ' a ' b . ( ' a -> Sexplib0 . Sexp . t ) -> ( ' b -> Sexplib0 . Sexp . t ) -> ( ' a , ' b ) t -> Sexplib0 . Sexp . t = fun ( type a__003_ b__004_ ) : ( ( a__003_ -> Sexplib0 . Sexp . t ) -> ( b__004_ -> Sexplib0 . Sexp . t ) -> ( a__003_ , b__004_ ) t -> Sexplib0 . Sexp . t ) -> fun _of_a__001_ _of_b__002_ T -> Sexplib0 . Sexp . Atom " T " ; ; [ @@@ end ] |
type ( ' a , ' b ) equal = ( ' a , ' b ) t |
let sym ( type a b ) ( T : ( a , b ) t ) : ( b , a ) t = T |
let trans ( type a b c ) ( T : ( a , b ) t ) ( T : ( b , c ) t ) : ( a , c ) t = T |
let conv ( type a b ) ( T : ( a , b ) t ) ( a : a ) : b = a |
module Lift ( X : sig type ' a t end ) = struct let lift ( type a b ) ( T : ( a , b ) t ) : ( a X . t , b X . t ) t = T end |
module Lift2 ( X : sig type ( ' a1 , ' a2 ) t end ) = struct let lift ( type a1 b1 a2 b2 ) ( T : ( a1 , b1 ) t ) ( T : ( a2 , b2 ) t ) : ( ( a1 , a2 ) X . t , ( b1 , b2 ) X . t ) t = T ; ; end |
module Lift3 ( X : sig type ( ' a1 , ' a2 , ' a3 ) t end ) = struct let lift ( type a1 b1 a2 b2 a3 b3 ) ( T : ( a1 , b1 ) t ) ( T : ( a2 , b2 ) t ) ( T : ( a3 , b3 ) t ) : ( ( a1 , a2 , a3 ) X . t , ( b1 , b2 , b3 ) X . t ) t = T ; ; end |
let detuple2 ( type a1 a2 b1 b2 ) ( T : ( a1 * a2 , b1 * b2 ) t ) : ( a1 , b1 ) t * ( a2 , b2 ) t = T , T ; ; |
let tuple2 ( type a1 a2 b1 b2 ) ( T : ( a1 , b1 ) t ) ( T : ( a2 , b2 ) t ) : ( a1 * a2 , b1 * b2 ) t = T |
module type Injective = sig type ' a t val strip : ( ' a t , ' b t ) equal -> ( ' a , ' b ) equal end |
module type Injective2 = sig type ( ' a1 , ' a2 ) t val strip : ( ( ' a1 , ' a2 ) t , ( ' b1 , ' b2 ) t ) equal -> ( ' a1 , ' b1 ) equal * ( ' a2 , ' b2 ) equal end |
module Composition_preserves_injectivity ( M1 : Injective ) ( M2 : Injective ) = struct type ' a t = ' a M1 . t M2 . t let strip e = M1 . strip ( M2 . strip e ) end |
module Obj = struct module Extension_constructor = struct [ @@@ ocaml . warning " - 3 " ] let id = Caml . Obj . extension_id let of_val = Caml . Obj . extension_constructor end end |
module Id = struct module Uid = Int module Witness = struct module Key = struct type _ t = . . type type_witness_int = [ ` type_witness of int ] [ @@ deriving_inline sexp_of ] let sexp_of_type_witness_int = ( fun ( ` type_witness v__005_ ) -> Sexplib0 . Sexp . List [ Sexplib0 . Sexp . Atom " type_witness " ; sexp_of_int v__005_ ] : type_witness_int -> Sexplib0 . Sexp . t ) ; ; [ @@@ end ] let sexp_of_t _sexp_of_a t = ` type_witness ( Obj . Extension_constructor . id ( Obj . Extension_constructor . of_val t ) ) |> sexp_of_type_witness_int ; ; end module type S = sig type t type _ Key . t += Key : t Key . t end type ' a t = ( module S with type t = ' a ) let sexp_of_t ( type a ) sexp_of_a ( module M : S with type t = a ) = M . Key |> Key . sexp_of_t sexp_of_a ; ; let create ( type t ) ( ) = let module M = struct type nonrec t = t type _ Key . t += Key : t Key . t end in ( module M : S with type t = t ) ; ; let uid ( type a ) ( module M : S with type t = a ) = Obj . Extension_constructor . id ( Obj . Extension_constructor . of_val M . Key ) ; ; let some_t = Some T let same ( type a b ) ( a : a t ) ( b : b t ) : ( a , b ) equal option = let module A = ( val a : S with type t = a ) in let module B = ( val b : S with type t = b ) in match A . Key with | B . Key -> some_t | _ -> None ; ; end type ' a t = { witness : ' a Witness . t ; name : string ; to_sexp : ' a -> Sexp . t } let sexp_of_t _ { witness ; name ; to_sexp } : Sexp . t = if am_testing then Atom name else List [ List [ Atom " name " ; Atom name ] ; List [ Atom " witness " ; witness |> Witness . sexp_of_t to_sexp ] ] ; ; let to_sexp t = t . to_sexp let name t = t . name let create ~ name to_sexp = { witness = Witness . create ( ) ; name ; to_sexp } let uid t = Witness . uid t . witness let hash t = uid t let hash_fold_t s t = hash_fold_int s ( uid t ) let same_witness t1 t2 = Witness . same t1 . witness t2 . witness let same t1 t2 = Option . is_some ( same_witness t1 t2 ) let same_witness_exn t1 t2 = match same_witness t1 t2 with | Some w -> w | None -> Error . raise_s ( Sexp . message " Type_equal . Id . same_witness_exn got different ids " [ ( " " , sexp_of_pair ( sexp_of_t sexp_of_opaque ) ( sexp_of_t sexp_of_opaque ) ( t1 , t2 ) ) ] ) ; ; end |
[ %% expect { | ^ bool because it is in the condition of an if - statement } ] ; ; | [ %% expect { | ^ bool because it is in the condition of an if - statement } ] ; ; | [ %% expect { | ^ int } ] ; ; | [ %% expect { | ^ bool because it is in the condition of an if - statement } ] ; ; | [ %% expect { | ^ bool because it is in the condition of an if - statement } ] ; ; | [ %% expect { | ^ unit because it is in the result of a conditional with no else branch } ] ; ; | [ %% expect { | ^^^^^^^^^^^^ bool because it is in the condition of an if - statement } ] ; ; | [ %% expect { | ^^ bool because it is in the condition of a while - loop } ] ; ; | [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^ unit because it is in the body of a while - loop } ] ; ; | [ %% expect { | ^^ int because it is in a for - loop start index } ] ; ; | [ %% expect { | ^^ int because it is in a for - loop stop index } ] ; ; | [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^ unit because it is in the body of a for - loop } ] ; ; | [ %% expect { | ^^ bool because it is in the condition of an assertion } ] ; ; | [ %% expect { | ^^^^^^^^^^^^^^^^^^ unit because it is in the left - hand side of a sequence } ] ; ; | |
let ordered_list_with x y = if x <= y then [ x ; y ] else if x > y then [ y ; x ] [ %% expect { | ^^^^ because it is in the result of a conditional with no else branch The constructor :: does not belong to type unit } ] ; ; | ( function | y when y + 1 -> ( ) | _ -> ( ) ) ; ; [ %% expect { | ^^^^^ bool because it is in a when - guard } ] ; ; | |
module Helper ( A : Variant_and_record_intf . S ) ( B : Variant_and_record_intf . S ) = struct type map = { map : ' a . ' a A . t -> ' a B . t } let map_variant ( type variant ) { map } ( variant : variant A . Variant . t ) = let map_create = function | A . Tag . Args fct -> B . Tag_internal . Args fct | A . Tag . Const k -> B . Tag_internal . Const k in let map_tag tag = match tag with | A . Variant . Tag tag -> let label = A . Tag . label tag in let rep = map ( A . Tag . traverse tag ) in let arity = A . Tag . arity tag in let args_labels = A . Tag . args_labels tag in let index = A . Tag . index tag in let ocaml_repr = A . Tag . ocaml_repr tag in let tyid = A . Tag . tyid tag in let create = map_create ( A . Tag . create tag ) in B . Variant_internal . Tag ( B . Tag . internal_use_only { B . Tag_internal . label ; rep ; arity ; args_labels ; index ; ocaml_repr ; tyid ; create } ) in let typename = A . Variant . typename_of_t variant in let polymorphic = A . Variant . is_polymorphic variant in let tags = Array . init ( A . Variant . length variant ) ( fun index -> map_tag ( A . Variant . tag variant index ) ) in let value ( a : variant ) = match A . Variant . value variant a with | A . Variant . Value ( atag , a ) -> ( fun ( type args ) ( atag : ( variant , args ) A . Tag . t ) ( a : args ) -> let ( B . Variant_internal . Tag btag ) = tags . ( A . Tag . index atag ) in ( fun ( type ex ) ( btag : ( variant , ex ) B . Tag . t ) -> let Type_equal . T = Typename . same_witness_exn ( A . Tag . tyid atag ) ( B . Tag . tyid btag ) in let btag = ( btag : ( variant , args ) B . Tag . t ) in B . Variant_internal . Value ( btag , a ) ) btag ) atag a in B . Variant . internal_use_only { B . Variant_internal . typename ; tags ; polymorphic ; value } ; ; let map_record ( type record ) { map } ( record : record A . Record . t ) = let map_field field = match field with | A . Record . Field field -> let label = A . Field . label field in let rep = map ( A . Field . traverse field ) in let index = A . Field . index field in let is_mutable = A . Field . is_mutable field in let tyid = A . Field . tyid field in let get = A . Field . get field in B . Record_internal . Field ( B . Field . internal_use_only { B . Field_internal . label ; rep ; index ; is_mutable ; tyid ; get } ) in let typename = A . Record . typename_of_t record in let has_double_array_tag = A . Record . has_double_array_tag record in let fields = Array . init ( A . Record . length record ) ( fun index -> map_field ( A . Record . field record index ) ) in let create { B . Record_internal . get } = let get ( type a ) ( afield : ( _ , a ) A . Field . t ) = match fields . ( A . Field . index afield ) with | B . Record_internal . Field bfield -> ( fun ( type ex ) ( bfield : ( record , ex ) B . Field . t ) -> let Type_equal . T = Typename . same_witness_exn ( A . Field . tyid afield ) ( B . Field . tyid bfield ) in let bfield = ( bfield : ( record , a ) B . Field . t ) in get bfield ) bfield in A . Record . create record { A . Record . get } in B . Record . internal_use_only { B . Record_internal . typename ; fields ; has_double_array_tag ; create } ; ; end |
module type Named = sig type ' a computation module Context : sig type t val create : unit -> t end type ' a t val init : Context . t -> ' a Typename . t -> ' a t val get_wip_computation : ' a t -> ' a computation val set_final_computation : ' a t -> ' a computation -> ' a computation val share : _ Typerep . t -> bool end |
module type Computation = sig type ' a t include Variant_and_record_intf . S with type ' a t := ' a t val int : int t val int32 : int32 t val int64 : int64 t val nativeint : nativeint t val char : char t val float : float t val string : string t val bytes : bytes t val bool : bool t val unit : unit t val option : ' a t -> ' a option t val list : ' a t -> ' a list t val array : ' a t -> ' a array t val lazy_t : ' a t -> ' a lazy_t t val ref_ : ' a t -> ' a ref t val function_ : ' a t -> ' b t -> ( ' a -> ' b ) t val tuple2 : ' a t -> ' b t -> ( ' a * ' b ) t val tuple3 : ' a t -> ' b t -> ' c t -> ( ' a * ' b * ' c ) t val tuple4 : ' a t -> ' b t -> ' c t -> ' d t -> ( ' a * ' b * ' c * ' d ) t val tuple5 : ' a t -> ' b t -> ' c t -> ' d t -> ' e t -> ( ' a * ' b * ' c * ' d * ' e ) t val record : ' a Record . t -> ' a t val variant : ' a Variant . t -> ' a t module Named : Named with type ' a computation := ' a t end |
module Make_named_for_closure ( X : sig type ' a input type ' a output type ' a t = ' a input -> ' a output end ) = struct module Context = struct type t = unit let create = ignore end type ' a t = { runtime_dereference : ' a X . t ; runtime_reference : ' a X . t ref ; compiletime_dereference : ' a X . t option ref } exception Undefined of string let init ( ) name = let path = Typename . Uid . name ( Typename . uid name ) in let r = ref ( fun _ -> raise ( Undefined path ) ) in { runtime_dereference = ( fun input -> ! r input ) ; runtime_reference = r ; compiletime_dereference = ref None } ; ; let get_wip_computation shared = match shared . compiletime_dereference . contents with | Some clos -> clos | None -> shared . runtime_dereference ; ; let set_final_computation shared computation = let compiletime_dereference = shared . compiletime_dereference in match compiletime_dereference . contents with | Some _ -> assert false | None -> if Base . phys_equal shared . runtime_dereference computation then assert false ; compiletime_dereference := Some computation ; shared . runtime_reference := computation ; computation ; ; let share _ = true end |
module Ident = struct type t = { name : string ; implements : Typename . Uid . t -> bool } exception Broken_dependency of string let check_dependencies name required = match required with | [ ] -> fun _ -> ( ) | _ -> fun uid -> List . iter ( fun { name = name ' ; implements } -> if not ( implements uid ) then ( let message = Printf . sprintf " Type_generic % S requires % S for uid % S \ n " name name ' ( Typename . Uid . name uid ) in prerr_endline message ; raise ( Broken_dependency message ) ) ) required ; ; end |
module type Extending = sig type ' a t type ' a computation = ' a t val ident : Ident . t exception Not_implemented of string * string module type S = sig type t include Typerepable . S with type t := t val compute : t computation end module type S1 = sig type ' a t include Typerepable . S1 with type ' a t := ' a t val compute : ' a computation -> ' a t computation end module type S2 = sig type ( ' a , ' b ) t include Typerepable . S2 with type ( ' a , ' b ) t := ( ' a , ' b ) t val compute : ' a computation -> ' b computation -> ( ' a , ' b ) t computation end module type S3 = sig type ( ' a , ' b , ' c ) t include Typerepable . S3 with type ( ' a , ' b , ' c ) t := ( ' a , ' b , ' c ) t val compute : ' a computation -> ' b computation -> ' c computation -> ( ' a , ' b , ' c ) t computation end module type S4 = sig type ( ' a , ' b , ' c , ' d ) t include Typerepable . S4 with type ( ' a , ' b , ' c , ' d ) t := ( ' a , ' b , ' c , ' d ) t val compute : ' a computation -> ' b computation -> ' c computation -> ' d computation -> ( ' a , ' b , ' c , ' d ) t computation end module type S5 = sig type ( ' a , ' b , ' c , ' d , ' e ) t include Typerepable . S5 with type ( ' a , ' b , ' c , ' d , ' e ) t := ( ' a , ' b , ' c , ' d , ' e ) t val compute : ' a computation -> ' b computation -> ' c computation -> ' d computation -> ' e computation -> ( ' a , ' b , ' c , ' d , ' e ) t computation end val register0 : ( module S ) -> unit val register1 : ( module S1 ) -> unit val register2 : ( module S2 ) -> unit val register3 : ( module S3 ) -> unit val register4 : ( module S4 ) -> unit val register5 : ( module S5 ) -> unit val register : ' a Typerep . t -> ' a computation -> unit end |
module type S_implementation = sig include Extending val raise_not_implemented : string -> ' a type implementation = { generic : ' a . ' a Typerep . t -> ' a computation } val _using_extended_implementation : implementation -> ' a Typerep . Named . t -> ' a Typerep . t lazy_t option -> ' a computation val find_extended_implementation : implementation -> ' a Typerep . Named . t -> ' a computation option end |
module type S = sig include Extending val of_typerep : ' a Typerep . t -> [ ` generic of ' a computation ] module Computation : Computation with type ' a t = ' a t end |
module Make_S_implementation ( X : sig type ' a t val name : string val required : Ident . t list end ) : S_implementation with type ' a t = ' a X . t = struct type ' a t = ' a X . t type ' a computation = ' a t include Type_generic_intf . M ( struct type ' a t = ' a computation end ) module Uid_table = struct include Hashtbl . Make ( Typename . Uid ) let find table key = if Lazy . is_val table then ( let table = Lazy . force table in try Some ( find table key ) with | Base . Not_found_s _ | Caml . Not_found -> None ) else None ; ; let check_dependencies = Ident . check_dependencies X . name X . required let replace table key value = check_dependencies key ; replace ( Lazy . force table ) key value ; ; let mem table key = if Lazy . is_val table then ( let table = Lazy . force table in mem table key ) else false ; ; end let size = 256 let table0 = lazy ( Uid_table . create size ) let table1 = lazy ( Uid_table . create size ) let table2 = lazy ( Uid_table . create size ) let table3 = lazy ( Uid_table . create size ) let table4 = lazy ( Uid_table . create size ) let table5 = lazy ( Uid_table . create size ) let is_registered uid = Uid_table . mem table0 uid || Uid_table . mem table1 uid || Uid_table . mem table2 uid || Uid_table . mem table3 uid || Uid_table . mem table4 uid || Uid_table . mem table5 uid ; ; let ident = { Ident . name = X . name ; implements = is_registered } module Find0 ( T : Typerep . Named . T0 ) : sig val compute : unit -> T . named computation option end = struct let compute ( ) = match Uid_table . find table0 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S = ( val rep : S ) in let witness = Typename . same_witness_exn S . typename_of_t T . typename_of_named in let module L = Type_equal . Lift ( struct type ' a t = ' a computation end ) in Some ( Type_equal . conv ( L . lift witness ) S . compute ) ; ; end module Find1 ( T : Typerep . Named . T1 ) : sig val compute : unit -> ( T . a computation -> T . a T . named computation ) option end = struct let compute ( ) = match Uid_table . find table1 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S1 = ( val rep : S1 ) in let module Conv = Typename . Same_witness_exn_1 ( S1 ) ( struct type ' a t = ' a T . named let typename_of_t = T . typename_of_named end ) in let module L = Type_equal . Lift ( struct type ' a t = T . a computation -> ' a computation end ) in Some ( Type_equal . conv ( L . lift Conv . ( witness . eq ) ) S1 . compute ) ; ; end module Find2 ( T : Typerep . Named . T2 ) : sig val compute : unit -> ( T . a computation -> T . b computation -> ( T . a , T . b ) T . named computation ) option end = struct let compute ( ) = match Uid_table . find table2 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S2 = ( val rep : S2 ) in let module Conv = Typename . Same_witness_exn_2 ( S2 ) ( struct type ( ' a , ' b ) t = ( ' a , ' b ) T . named let typename_of_t = T . typename_of_named end ) in let module L = Type_equal . Lift ( struct type ' a t = T . a computation -> T . b computation -> ' a computation end ) in Some ( Type_equal . conv ( L . lift Conv . ( witness . eq ) ) S2 . compute ) ; ; end module Find3 ( T : Typerep . Named . T3 ) : sig val compute : unit -> ( T . a computation -> T . b computation -> T . c computation -> ( T . a , T . b , T . c ) T . named computation ) option end = struct let compute ( ) = match Uid_table . find table3 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S3 = ( val rep : S3 ) in let module Conv = Typename . Same_witness_exn_3 ( S3 ) ( struct type ( ' a , ' b , ' c ) t = ( ' a , ' b , ' c ) T . named let typename_of_t = T . typename_of_named end ) in let module L = Type_equal . Lift ( struct type ' a t = T . a computation -> T . b computation -> T . c computation -> ' a computation end ) in Some ( Type_equal . conv ( L . lift Conv . ( witness . eq ) ) S3 . compute ) ; ; end module Find4 ( T : Typerep . Named . T4 ) : sig val compute : unit -> ( T . a computation -> T . b computation -> T . c computation -> T . d computation -> ( T . a , T . b , T . c , T . d ) T . named computation ) option end = struct let compute ( ) = match Uid_table . find table4 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S4 = ( val rep : S4 ) in let module Conv = Typename . Same_witness_exn_4 ( S4 ) ( struct type ( ' a , ' b , ' c , ' d ) t = ( ' a , ' b , ' c , ' d ) T . named let typename_of_t = T . typename_of_named end ) in let module L = Type_equal . Lift ( struct type ' a t = T . a computation -> T . b computation -> T . c computation -> T . d computation -> ' a computation end ) in Some ( Type_equal . conv ( L . lift Conv . ( witness . eq ) ) S4 . compute ) ; ; end module Find5 ( T : Typerep . Named . T5 ) : sig val compute : unit -> ( T . a computation -> T . b computation -> T . c computation -> T . d computation -> T . e computation -> ( T . a , T . b , T . c , T . d , T . e ) T . named computation ) option end = struct let compute ( ) = match Uid_table . find table5 ( Typename . uid T . typename_of_t ) with | None -> None | Some rep -> let module S5 = ( val rep : S5 ) in let module Conv = Typename . Same_witness_exn_5 ( S5 ) ( struct type ( ' a , ' b , ' c , ' d , ' e ) t = ( ' a , ' b , ' c , ' d , ' e ) T . named let typename_of_t = T . typename_of_named end ) in let module L = Type_equal . Lift ( struct type ' a t = T . a computation -> T . b computation -> T . c computation -> T . d computation -> T . e computation -> ' a computation end ) in Some ( Type_equal . conv ( L . lift Conv . ( witness . eq ) ) S5 . compute ) ; ; end let unit = Typename . static let register0 compute = let module S = ( val compute : S ) in let uid = Typename . uid S . typename_of_t in Uid_table . replace table0 uid compute ; ; let register1 compute = let module S1 = ( val compute : S1 ) in let uid = Typename . uid ( S1 . typename_of_t unit ) in Uid_table . replace table1 uid compute ; ; let register2 compute = let module S2 = ( val compute : S2 ) in let uid = Typename . uid ( S2 . typename_of_t unit unit ) in Uid_table . replace table2 uid compute ; ; let register3 compute = let module S3 = ( val compute : S3 ) in let uid = Typename . uid ( S3 . typename_of_t unit unit unit ) in Uid_table . replace table3 uid compute ; ; let register4 compute = let module S4 = ( val compute : S4 ) in let uid = Typename . uid ( S4 . typename_of_t unit unit unit unit ) in Uid_table . replace table4 uid compute ; ; let register5 compute = let module S5 = ( val compute : S5 ) in let uid = Typename . uid ( S5 . typename_of_t unit unit unit unit unit ) in Uid_table . replace table5 uid compute ; ; let register ( type a ) typerep_of_a compute = let module S = struct type t = a let typename_of_t = Typerep . typename_of_t typerep_of_a let typerep_of_t = typerep_of_a let compute = compute end in register0 ( module S : S ) ; ; type implementation = { generic : ' a . ' a Typerep . t -> ' a computation } let find_extended_implementation ( type a ) aux = function | Typerep . Named . T0 rep -> let module T = ( val rep : Typerep . Named . T0 with type t = a ) in let module Custom = Find0 ( T ) in ( match Custom . compute ( ) with | Some custom -> let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) | Typerep . Named . T1 rep -> let module T = ( val rep : Typerep . Named . T1 with type t = a ) in let module Custom = Find1 ( T ) in ( match Custom . compute ( ) with | Some custom -> let custom = ( custom ( aux . generic T . a ) : T . a T . named computation ) in let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) | Typerep . Named . T2 rep -> let module T = ( val rep : Typerep . Named . T2 with type t = a ) in let module Custom = Find2 ( T ) in ( match Custom . compute ( ) with | Some custom -> let custom = ( custom ( aux . generic T . a ) ( aux . generic T . b ) : ( T . a , T . b ) T . named computation ) in let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) | Typerep . Named . T3 rep -> let module T = ( val rep : Typerep . Named . T3 with type t = a ) in let module Custom = Find3 ( T ) in ( match Custom . compute ( ) with | Some custom -> let custom = ( custom ( aux . generic T . a ) ( aux . generic T . b ) ( aux . generic T . c ) : ( T . a , T . b , T . c ) T . named computation ) in let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) | Typerep . Named . T4 rep -> let module T = ( val rep : Typerep . Named . T4 with type t = a ) in let module Custom = Find4 ( T ) in ( match Custom . compute ( ) with | Some custom -> let custom = ( custom ( aux . generic T . a ) ( aux . generic T . b ) ( aux . generic T . c ) ( aux . generic T . d ) : ( T . a , T . b , T . c , T . d ) T . named computation ) in let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) | Typerep . Named . T5 rep -> let module T = ( val rep : Typerep . Named . T5 with type t = a ) in let module Custom = Find5 ( T ) in ( match Custom . compute ( ) with | Some custom -> let custom = ( custom ( aux . generic T . a ) ( aux . generic T . b ) ( aux . generic T . c ) ( aux . generic T . d ) ( aux . generic T . e ) : ( T . a , T . b , T . c , T . d , T . e ) T . named computation ) in let Type_equal . T = T . witness in Some ( custom : a computation ) | None -> None ) ; ; exception Not_implemented of string * string let raise_not_implemented string = raise ( Not_implemented ( X . name , string ) ) let _using_extended_implementation aux rep content = match find_extended_implementation aux rep with | Some computation -> computation | None -> ( match content with | Some ( lazy content ) -> aux . generic content | None -> let typename = Typerep . Named . typename_of_t rep in let name = Typename . Uid . name ( Typename . uid typename ) in raise_not_implemented name ) ; ; end |
module _ = Hashtbl . Make ( Typename . Key ) |
module Make ( X : sig type ' a t val name : string val required : Ident . t list include Computation with type ' a t := ' a t end ) = struct module Computation = X include Make_S_implementation ( X ) module Memo = Typename . Table ( struct type ' a t = ' a X . Named . t end ) module Helper = Helper ( Typerep ) ( Computation ) let of_typerep rep = let context = X . Named . Context . create ( ) in let memo_table = Memo . create 32 in let rec of_typerep : type a . a Typerep . t -> a t = function | Typerep . Int -> X . int | Typerep . Int32 -> X . int32 | Typerep . Int64 -> X . int64 | Typerep . Nativeint -> X . nativeint | Typerep . Char -> X . char | Typerep . Float -> X . float | Typerep . String -> X . string | Typerep . Bytes -> X . bytes | Typerep . Bool -> X . bool | Typerep . Unit -> X . unit | Typerep . Option rep -> X . option ( of_typerep rep ) | Typerep . List rep -> X . list ( of_typerep rep ) | Typerep . Array rep -> X . array ( of_typerep rep ) | Typerep . Lazy rep -> X . lazy_t ( of_typerep rep ) | Typerep . Ref rep -> X . ref_ ( of_typerep rep ) | Typerep . Function ( dom , rng ) -> X . function_ ( of_typerep dom ) ( of_typerep rng ) | Typerep . Tuple tuple -> ( match tuple with | Typerep . Tuple . T2 ( a , b ) -> let ra = of_typerep a in let rb = of_typerep b in X . tuple2 ra rb | Typerep . Tuple . T3 ( a , b , c ) -> let ra = of_typerep a in let rb = of_typerep b in let rc = of_typerep c in X . tuple3 ra rb rc | Typerep . Tuple . T4 ( a , b , c , d ) -> let ra = of_typerep a in let rb = of_typerep b in let rc = of_typerep c in let rd = of_typerep d in X . tuple4 ra rb rc rd | Typerep . Tuple . T5 ( a , b , c , d , e ) -> let ra = of_typerep a in let rb = of_typerep b in let rc = of_typerep c in let rd = of_typerep d in let re = of_typerep e in X . tuple5 ra rb rc rd re ) | Typerep . Record record -> X . record ( Helper . map_record { Helper . map = of_typerep } record ) | Typerep . Variant variant -> X . variant ( Helper . map_variant { Helper . map = of_typerep } variant ) | Typerep . Named ( named , content ) -> let typename = Typerep . Named . typename_of_t named in ( match Memo . find memo_table typename with | Some shared -> X . Named . get_wip_computation shared | None -> ( match find_extended_implementation { generic = of_typerep } named with | Some computation -> computation | None -> ( match content with | None -> let name = Typename . Uid . name ( Typename . uid typename ) in raise_not_implemented name | Some ( lazy content ) -> if X . Named . share content then ( let shared = X . Named . init context typename in Memo . set memo_table typename shared ; let computation = of_typerep content in X . Named . set_final_computation shared computation ) else of_typerep content ) ) ) in let computation = of_typerep rep in ` generic computation ; ; end |
module M ( X : sig type ' a t end ) = struct module type S = sig type t include Typerepable . S with type t := t val compute : t X . t end module type S1 = sig type ' a t include Typerepable . S1 with type ' a t := ' a t val compute : ' a X . t -> ' a t X . t end module type S2 = sig type ( ' a , ' b ) t include Typerepable . S2 with type ( ' a , ' b ) t := ( ' a , ' b ) t val compute : ' a X . t -> ' b X . t -> ( ' a , ' b ) t X . t end module type S3 = sig type ( ' a , ' b , ' c ) t include Typerepable . S3 with type ( ' a , ' b , ' c ) t := ( ' a , ' b , ' c ) t val compute : ' a X . t -> ' b X . t -> ' c X . t -> ( ' a , ' b , ' c ) t X . t end module type S4 = sig type ( ' a , ' b , ' c , ' d ) t include Typerepable . S4 with type ( ' a , ' b , ' c , ' d ) t := ( ' a , ' b , ' c , ' d ) t val compute : ' a X . t -> ' b X . t -> ' c X . t -> ' d X . t -> ( ' a , ' b , ' c , ' d ) t X . t end module type S5 = sig type ( ' a , ' b , ' c , ' d , ' e ) t include Typerepable . S5 with type ( ' a , ' b , ' c , ' d , ' e ) t := ( ' a , ' b , ' c , ' d , ' e ) t val compute : ' a X . t -> ' b X . t -> ' c X . t -> ' d X . t -> ' e X . t -> ( ' a , ' b , ' c , ' d , ' e ) t X . t end end |
module type S = sig type ' a t include module type of M ( struct type ' a computation = ' a t type ' a t = ' a computation end ) end |
module Make ( D : Debugger . S ) ( Cmt_cache : Cmt_cache_intf . S ) = struct let type_and_env_from_dwarf_type0 ~ dwarf_type ~ cmt_cache = let cmt_file_and_ident_name = match Dwarf_name_laundry . split_base_type_die_name dwarf_type with | None -> None | Some { compilation_unit ; ident_name ; ident_stamp ; is_parameter ; } -> let compilation_unit = Compilation_unit . create ( Ident . create_persistent compilation_unit ) ( Linkage_name . create ( Compilenv . make_symbol ~ unitname : compilation_unit None ) ) in match Cmt_cache . load cmt_cache compilation_unit with | None -> None | Some cmt -> Some ( cmt , ident_name , ident_stamp , is_parameter ) in match cmt_file_and_ident_name with | None -> None | Some ( cmt_file , name , stamp , is_parameter ) -> match Cmt_file . type_of_ident cmt_file ~ name ~ stamp with | None -> None | Some ( ty , env ) -> Some ( ty , env , is_parameter ) let type_is_polymorphic env ty = Btype . fold_type_expr ( fun is_poly ty -> is_poly || Btype . is_Tvar ( Ctype . expand_head env ty ) ) false ty let rec type_and_env_from_dwarf_type ~ dwarf_type ~ cmt_cache frame = if Monda_debug . debug then begin Printf . fprintf stdout " Checking DWARF type % s \ n " dwarf_type end ; match type_and_env_from_dwarf_type0 ~ dwarf_type ~ cmt_cache with | None -> if Monda_debug . debug then begin Printf . fprintf stdout " Couldn ' t get OCaml type + env \ n " end ; None | ( Some ( ty , env , is_parameter ) ) as result -> match ty with | Module _ -> result | Core ty -> let ty = Ctype . expand_head env ty in let normal_case ( ) = Some ( Cmt_file . Core ty , env , is_parameter ) in if type_is_polymorphic env ty && not ( D . Frame . inlined frame ) then begin if Monda_debug . debug then begin Printf . fprintf stdout " Type is polymorphic . \ n " end ; match is_parameter with | Local -> if Monda_debug . debug then begin Printf . fprintf stdout " Not a parameter . \ n " end ; normal_case ( ) | Parameter { index ; } -> if Monda_debug . debug then begin Printf . fprintf stdout " Type is polymorphic and that of a parameter . \ n " end ; match D . Frame . caller frame with | No_caller -> if Monda_debug . debug then begin Printf . fprintf stdout " Couldn ' t find caller frame . \ n " end ; normal_case ( ) | Caller ( caller_frame , call_site ) -> if Monda_debug . debug then begin Printf . fprintf stdout " Found caller frame . \ n " end ; match D . Call_site . dwarf_type_of_argument call_site ~ index with | None -> if Monda_debug . debug then begin Printf . fprintf stdout " Couldn ' t find DWARF arg type . \ n " end ; begin match D . Call_site . line_number call_site , D . Call_site . column_number call_site with | None , None | None , Some _ | Some _ , None -> if Monda_debug . debug then begin Printf . fprintf stdout " Couldn ' t find call site location . \ n " end ; normal_case ( ) | Some line , Some column -> match D . Call_site . ocaml_specific_compilation_unit_info call_site with | None -> if Monda_debug . debug then begin Printf . fprintf stdout " Couldn ' t find unit info . \ n " end ; normal_case ( ) | Some unit_info -> let comp_unit = Compilation_unit . create unit_info . unit_name ( Linkage_name . create ( Compilenv . make_symbol ~ unitname ( : Ident . name unit_info . unit_name ) None ) ) in match Cmt_cache . load cmt_cache comp_unit with | None -> if Monda_debug . debug then begin Printf . fprintf stdout " . cmt load failed \ n " end ; normal_case ( ) | Some call_site_cmt -> match Cmt_file . type_of_call_site_argument call_site_cmt ~ line ~ column ~ index with | None -> if Monda_debug . debug then begin Printf . fprintf stdout " type lookup failed ( line % d , column % d ) \ n " line column end ; normal_case ( ) | Some ( ty , env ) -> if Monda_debug . debug then begin Printf . fprintf stdout " type lookup ok ( line % d , column % d ) \ n " line column end ; Some ( ty , env , is_parameter ) end | Some dwarf_type -> type_and_env_from_dwarf_type ~ dwarf_type ~ cmt_cache caller_frame end else begin if Monda_debug . debug then begin Printf . fprintf stdout " Type is not polymorphic . \ n " end ; normal_case ( ) end end |
let helpers_error msg = raise ( Type_helpers_error msg ) |
let michelson_type_to_ex_ty ( typ : Alpha_context . Script . expr ) ( ctxt : Alpha_context . t ) = Script_ir_translator . parse_ty ctxt ~ legacy : false ~ allow_lazy_storage : false ~ allow_operation : false ~ allow_contract : false ~ allow_ticket : false ( Micheline . root typ ) |> Environment . wrap_tzresult |> function | Ok ( ex_ty , _ctxt ) -> ex_ty | Error errs -> let msg = Format . asprintf " Michelson_generation . michelson_type_to_ex_ty ( % a ) " Error_monad . pp_print_trace errs in helpers_error msg |
let rec michelson_type_list_to_ex_stack_ty ( stack_ty : Alpha_context . Script . expr list ) ctxt = let open Script_ir_translator in let open Script_typed_ir in match stack_ty with | [ ] -> Ex_stack_ty Bot_t | hd :: tl -> ( let ex_ty = michelson_type_to_ex_ty hd ctxt in match ex_ty with | Ex_ty ty -> ( let ex_stack_ty = michelson_type_list_to_ex_stack_ty tl ctxt in match ex_stack_ty with | Ex_stack_ty tl -> Ex_stack_ty ( Item_t ( ty , tl ) ) ) ) |
let base_type_to_michelson_type ( typ : Type . Base . t ) = let typ = Mikhailsky . map_var ( fun _ -> Mikhailsky . unit_ty ) typ in Mikhailsky . to_michelson typ |
let rec stack_type_to_michelson_type_list ( typ : Type . Stack . t ) = let node = typ . node in match node with | Type . Stack . Stack_var_t _ -> helpers_error " stack_type_to_michelson_type_list : bug found " | Type . Stack . Empty_t -> [ ] | Type . Stack . Item_t ( ty , tl ) -> base_type_to_michelson_type ty :: stack_type_to_michelson_type_list tl |
let base_type_to_ex_ty ty = michelson_type_to_ex_ty ( base_type_to_michelson_type ty ) |
module Key = struct type t = int [ @@ deriving compare , sexp_of ] let hash x = x end |
module Allowed_ints = struct type t = | None | All | Hash_set of Hash_set . M ( Key ) . t | From_zero_to of int let _invariant = function | None | All | Hash_set _ -> ( ) | From_zero_to n -> assert ( n >= 0 ) ; ; let int_is_value t i = match t with | None -> false | All -> true | Hash_set hash_set -> Hash_set . mem hash_set i | From_zero_to n -> 0 <= i && i <= n ; ; end |
module Immediacy = struct type t = | Always | Sometimes | Never [ @@ deriving compare ] let equal = [ % compare . equal : t ] let to_string = function | Always -> " Always " | Sometimes -> " Sometimes " | Never -> " Never " ; ; end |
module T : sig type ' a t val create : ' a Typename . t -> Immediacy . t -> Allowed_ints . t -> ' a t val create_with_name : string -> Immediacy . t -> Allowed_ints . t -> _ t val immediacy : _ t -> Immediacy . t val allowed_ints : _ t -> Allowed_ints . t val typename : _ t -> string module Never_values : sig val int32 : int32 t val int64 : int64 t val nativeint : nativeint t val float : float t val string : string t val bytes : bytes t val array : _ array t val ref_ : _ ref t val tuple2 : ( _ * _ ) t val tuple3 : ( _ * _ * _ ) t val tuple4 : ( _ * _ * _ * _ ) t val tuple5 : ( _ * _ * _ * _ * _ ) t val function_ : ( _ -> _ ) t end val never : ' a Typename . t -> ' a t val option : _ t val list : _ t val magic : _ t -> _ t type t_ = { immediacy : Immediacy . t ; allowed_ints : Allowed_ints . t ; typename : string } type ' a t = t_ let create_with_name typename immediacy allowed_ints = { immediacy ; allowed_ints ; typename } ; ; let create typename immediacy allowed_ints = create_with_name ( Typename . name typename ) immediacy allowed_ints ; ; let immediacy t = t . immediacy let allowed_ints t = t . allowed_ints let typename t = t . typename let magic t = t let never_with_name name = create_with_name name Never Allowed_ints . None let never typename = create typename Never Allowed_ints . None let option = create_with_name " option " Sometimes ( Allowed_ints . From_zero_to 0 ) let list = create_with_name " list " Sometimes ( Allowed_ints . From_zero_to 0 ) module Never_values = struct let int32 = never typename_of_int32 let int64 = never typename_of_int64 let nativeint = never typename_of_nativeint let float = never typename_of_float let string = never typename_of_string let bytes = never typename_of_bytes let array = never_with_name " array " let ref_ = never_with_name " ref " let tuple2 = never_with_name " tuple2 " let tuple3 = never_with_name " tuple3 " let tuple4 = never_with_name " tuple4 " let tuple5 = never_with_name " tuple5 " let function_ = never_with_name " function " end end |
let int = create typename_of_int Always Allowed_ints . All |
let unit = create typename_of_unit Always ( Allowed_ints . From_zero_to 0 ) |
let bool = create typename_of_bool Always ( Allowed_ints . From_zero_to 1 ) |
let char = create typename_of_char Always ( Allowed_ints . From_zero_to 255 ) |
module Computation_impl = struct type nonrec ' a t = ' a t include Type_generic . Variant_and_record_intf . M ( struct type nonrec ' a t = ' a t end ) include Never_values let ref_ _ = ref_ let array _ = array let tuple2 _ _ = tuple2 let tuple3 _ _ _ = tuple3 let tuple4 _ _ _ _ = tuple4 let tuple5 _ _ _ _ _ = tuple5 let function_ _ _ = function_ let int = int let char = char let bool = bool let unit = unit let option _ = option let list _ = list let lazy_t t = let immediacy = match immediacy t with | Never -> Never | Sometimes | Always -> Sometimes in create_with_name " lazy_t " immediacy ( allowed_ints t ) ; ; let record r = never ( Record . typename_of_t r ) let variant variant = let no_arg_list = List . rev ( Variant . fold variant ~ init [ ] : ~ f ( : fun list tag -> match tag with | Variant . Tag t -> if Tag . arity t = 0 then tag :: list else list ) ) in let no_arg_count = List . length no_arg_list in if no_arg_count = 0 then never ( Variant . typename_of_t variant ) else ( let allowed_ints = if not ( Variant . is_polymorphic variant ) then Allowed_ints . From_zero_to ( no_arg_count - 1 ) else ( let hash_set = Hash_set . create ( module Key ) ~ size ( : no_arg_count * 2 ) in List . iter no_arg_list ~ f ( : function Variant . Tag tag -> ( match Tag . create tag with | Tag . Const _ -> Hash_set . add hash_set ( Tag . ocaml_repr tag ) | Tag . Args _ -> assert false ) ) ; Allowed_ints . Hash_set hash_set ) in let immediacy = if Variant . length variant > no_arg_count then Immediacy . Sometimes else Immediacy . Always in create ( Variant . typename_of_t variant ) immediacy allowed_ints ) ; ; let name = " is_immediate " let required = [ ] module Named = struct module Context = struct type t = unit let create ( ) = ( ) end type nonrec ' a t = ' a t ref let init _ name = ref ( create name Sometimes Allowed_ints . None ) let get_wip_computation comp = ! comp let set_final_computation r t = r := t ; t ; ; let share _ = true end end |
module Generic = Type_generic . Make ( Computation_impl ) |
let of_typerep typerep = let ( ` generic t ) = Generic . of_typerep typerep in t ; ; |
module For_all_parameters ( M : sig val immediacy : Immediacy . t end ) = struct let witness typerep1 typerep2 = let t1 = of_typerep typerep1 in let t2 = of_typerep typerep2 in let i1 = immediacy t1 in let i2 = immediacy t2 in if not ( Immediacy . equal i1 i2 ) then failwith ( sprintf " type % s is not independent of its arguments " ( Typename . name ( Typerep . typename_of_t typerep1 ) ) ) else if not ( Immediacy . equal i1 M . immediacy ) then failwith ( sprintf " type % s does not have desired immediacy : wanted % s but got % s " ( Typename . name ( Typerep . typename_of_t typerep1 ) ) ( Immediacy . to_string M . immediacy ) ( Immediacy . to_string i1 ) ) else t1 ; ; let ra = Typerep . Int let rn = Typerep . String module For_all_parameters_S1 ( X : Typerepable . S1 ) = struct let t = witness ( X . typerep_of_t ra ) ( X . typerep_of_t rn ) let witness ( ) = magic t end module For_all_parameters_S2 ( X : Typerepable . S2 ) = struct let t = witness ( X . typerep_of_t ra ra ) ( X . typerep_of_t rn rn ) let witness ( ) = magic t end module For_all_parameters_S3 ( X : Typerepable . S3 ) = struct let t = witness ( X . typerep_of_t ra ra ra ) ( X . typerep_of_t rn rn rn ) let witness ( ) = magic t end module For_all_parameters_S4 ( X : Typerepable . S4 ) = struct let t = witness ( X . typerep_of_t ra ra ra ra ) ( X . typerep_of_t rn rn rn rn ) let witness ( ) = magic t end module For_all_parameters_S5 ( X : Typerepable . S5 ) = struct let t = witness ( X . typerep_of_t ra ra ra ra ra ) ( X . typerep_of_t rn rn rn rn rn ) let witness ( ) = magic t end end |
let int_is_value t int = Allowed_ints . int_is_value ( allowed_ints t ) int |
let int_as_value ( type a ) ( t : a t ) int = if int_is_value t int then Some ( Obj . magic ( int : int ) : a ) else None ; ; |
let int_as_value_exn ( type a ) ( t : a t ) int = if int_is_value t int then ( Obj . magic ( int : int ) : a ) else failwith ( sprintf " Immediate . int_as_value_exn : typename :% S int :% d " ( T . typename t ) int ) ; ; |
let value_as_int_exn ( type a ) ( t : a t ) a = if Obj . is_int ( Obj . repr a ) then ( Obj . magic ( a : a ) : int ) else failwith ( sprintf " Immediate . value_as_int_exn : typename :% S " ( T . typename t ) ) ; ; |
let value_as_int ( type a ) ( _ : a t ) a = if Obj . is_int ( Obj . repr a ) then Some ( Obj . magic ( a : a ) : int ) else None ; ; |
let value_is_int ( type a ) ( _ : a t ) a = Obj . is_int ( Obj . repr a ) |
module Always = struct type nonrec ' a t = ' a t include For_all_parameters ( struct let immediacy = Always end ) let of_typerep typerep = let t = of_typerep typerep in match immediacy t with | Always -> Some t | Never | Sometimes -> None ; ; let of_typerep_exn here typerep = Option . value_exn ~ here ( of_typerep typerep ) let int_as_value = int_as_value let int_as_value_exn = int_as_value_exn let int_is_value = int_is_value let [ @ inline always ] value_as_int ( type a ) _ a = a |> ( Obj . magic : a -> int ) let int = int let char = char let bool = bool let unit = unit end |
module Sometimes = struct type nonrec ' a t = ' a t include For_all_parameters ( struct let immediacy = Sometimes end ) let of_typerep typerep = let t = of_typerep typerep in match immediacy t with | Sometimes -> Some t | Always | Never -> None ; ; let of_typerep_exn here typerep = Option . value_exn ~ here ( of_typerep typerep ) let int_as_value = int_as_value let int_as_value_exn = int_as_value_exn let int_is_value = int_is_value let value_as_int = value_as_int let value_as_int_exn = value_as_int_exn let value_is_int = value_is_int let option = option let list = list end |
module Never = struct type nonrec ' a t = ' a t include For_all_parameters ( struct let immediacy = Never end ) let of_typerep typerep = let t = of_typerep typerep in match immediacy t with | Never -> Some t | Always | Sometimes -> None ; ; let of_typerep_exn here typerep = Option . value_exn ~ here ( of_typerep typerep ) include Never_values end |
type ' a dest = | Always of ' a Always . t | Sometimes of ' a Sometimes . t | Never of ' a Never . t |
let dest t = let module I = Immediacy in match immediacy t with | I . Always -> Always t | I . Sometimes -> Sometimes t | I . Never -> Never t ; ; |
( module struct module Typerep_obj = Typerep_lib . Std . Typerep_obj type ' a t = { int_as_value : int -> ' a option ; int_as_value_exn : int -> ' a ; int_is_value : int -> bool ; value_as_int : ' a -> int option ; value_as_int_exn : ' a -> int ; value_is_int : ' a -> bool } let get_conv typerep_of_t = let immediacy = Type_immediacy . of_typerep typerep_of_t in let int_as_value i = Type_immediacy . int_as_value immediacy i in let int_as_value_exn i = Type_immediacy . int_as_value_exn immediacy i in let int_is_value i = Type_immediacy . int_is_value immediacy i in let value_as_int a = Type_immediacy . value_as_int immediacy a in let value_as_int_exn a = Type_immediacy . value_as_int_exn immediacy a in let value_is_int a = Type_immediacy . value_is_int immediacy a in { int_as_value ; int_as_value_exn ; int_is_value ; value_as_int ; value_as_int_exn ; value_is_int } ; ; For [ Convertible ( v , i ) ] , we test the conversion invariants mentioned in 1 ) , i . e . we a ) check that [ v ] is immediate , and that it ' s underlying [ int ] representation is [ i ] b ) check that [ v ] converts to [ i ] , using [ value_is_int ] , and [ value_as_int ( _exn ) ] ? c ) check that [ i ] converts to [ v ] , using [ int_is_value ] , and [ int_as_value ( _exn ) ] ? | Val_does_not_convert of ' a For [ Val_does_not_convert v ] , we test that a ) [ v ] is boxed b ) the conversion functions return the appropriate results as described in 3 ) | Int_does_not_convert of ' a For [ Int_does_not_convert i ] , we test that the conversion functions return the appropriate results as described in II ) | Boxed_value_converts of ( ' a lazy_t * ' a * int ) Since case 2 ) is only relevant if the toplevel type is [ lazy_t ] , we have specialized this case . For [ Boxed_value_converts ( lazy_v , v , i ) ] , we test that a ) [ lazy_v ] is boxed . b ) [ lazy_v ] converts to [ i ] when the conversion functions are called . c ) [ Lazy . force lazy_v == Lazy . force v ] d ) If we test [ Convertible ( v , i ) ] , the test passes . ( We also implicitly have the property that [ Lazy . force lazy_v ] is immediate and internally represented by [ i ] , which follows from b ) , c ) and d ) above . ) The following functions provide tests for the four cases . ) * let is_immediate ( type a ) ( v : a ) : bool = Obj . is_int ( Obj . repr v ) let to_int ( type a ) ( v : a ) : int = if is_immediate v then ( Obj . magic ( v : a ) : int ) else failwith " Can ' t convert a boxed value to an int . " ; ; let test_convertible_single ( type a ) ( conv : a t ) ( ( v , i ) : a * int ) : bool = to_int v = i && conv . value_is_int v && conv . int_is_value i && i = conv . value_as_int_exn v && phys_equal v ( conv . int_as_value_exn i ) && ( match conv . value_as_int v with | Some i ' -> i = i ' | None -> false ) && match conv . int_as_value i with | Some v ' -> phys_equal v v ' | None -> false ; ; let test_convertibles ( type a ) ( conv : a t ) ( test_cases : ( a * int ) list ) : bool = List . for_all test_cases ~ f ( : test_convertible_single conv ) ; ; let test_inconvertible_values ( type a ) ( conv : a t ) ( test_cases : a list ) : bool = List . for_all test_cases ~ f ( : fun v -> ( not ( is_immediate v ) ) && ( not ( conv . value_is_int v ) ) && ( match conv . value_as_int v with | Some _ -> false | None -> true ) && try let _i = conv . value_as_int_exn v in false with | _ -> true ) ; ; let test_inconvertible_ints ( type a ) ( conv : a t ) ( test_cases : int list ) : bool = List . for_all test_cases ~ f ( : fun i -> ( not ( conv . int_is_value i ) ) && ( match conv . int_as_value i with | Some _ -> false | None -> true ) && try let _v = conv . int_as_value_exn i in false with | _ -> true ) ; ; let no_int_but_zero_converts = [ 1 ; 2 ; 3 ; - 1 ; Int . max_value ; Int . min_value ] let no_int_converts = 0 :: no_int_but_zero_converts module type S = sig type t [ @@ deriving typerep ] end let is_never ( type a ) ( module M : S with type t = a ) examples = let conv = get_conv M . typerep_of_t in test_inconvertible_values conv examples && test_inconvertible_ints conv no_int_converts ; ; let % test ( _ [ @ tags " no - js " ] ) = is_never ( module struct type t = int32 [ @@ deriving typerep ] end ) [ Int32 . zero ; Int32 . one ; Int32 . minus_one ; Int32 . max_value ; Int32 . min_value ; Int32 . of_int_exn 328121 ] ; ; let % test _ = is_never ( module struct type t = int64 [ @@ deriving typerep ] end ) [ Int64 . zero ; Int64 . one ; Int64 . minus_one ; Int64 . max_value ; Int64 . min_value ] ; ; let % test ( _ [ @ tags " no - js " ] ) = is_never ( module struct type t = nativeint [ @@ deriving typerep ] end ) [ Nativeint . zero ; Nativeint . one ; Nativeint . minus_one ; Nativeint . max_value ; Nativeint . min_value ] ; ; let % test _ = let module M = struct type t = char [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ ' a ' , 97 ; ' A ' , 65 ; ' 0 ' , 48 ; ' 9 ' , 57 ; ' \ t ' , 9 ; ' \ n ' , 10 ; Char . of_int_exn 0 , 0 ; Char . of_int_exn 127 , 127 ] && test_inconvertible_ints conv [ 256 ; - 1 ; 500 ; Int . max_value ; Int . min_value ] ; ; let % test ( _ [ @ tags " no - js " ] ) = is_never ( module struct type t = float [ @@ deriving typerep ] end ) [ 1 . 1 ; 0 . 0 ; - 3 . 3 ; Float . nan ; Float . infinity ; Float . neg_infinity ] ; ; let % test _ = is_never ( module struct type t = string [ @@ deriving typerep ] end ) [ " " ; " Hello world " ! ] ; ; let % test _ = let module M = struct type t = bool [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ true , 1 ; false , 0 ] && test_inconvertible_ints conv [ 2 ; 3 ; - 1 ; - 2 ; 50 ; Int . max_value ; Int . min_value ] ; ; let % test _ = let module M = struct type t = unit [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ ( ) , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let module M = struct type t = unit option [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ None , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts && test_inconvertible_values conv [ Some ( ) ] ; ; let % test _ = let module M = struct type t = int ref option [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ None , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts && test_inconvertible_values conv [ Some ( ref 0 ) ; Some ( ref 1 ) ] ; ; let % test _ = let module M = struct type t = int ref list [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ [ ] , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts && test_inconvertible_values conv [ [ ref 0 ] ; [ ref 1 ] ] ; ; let % test _ = let module M = struct type t = unit list [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ [ ] , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts && test_inconvertible_values conv [ [ ( ) ] ] ; ; let % test _ = is_never ( module struct type t = int ref array [ @@ deriving typerep ] end ) [ [ ] ; || [ | ref 1 ] | ] ; ; let % test _ = is_never ( module struct type t = unit array [ @@ deriving typerep ] end ) [ [ ] ; || [ | ( ) ] ; | [ | ( ) ; ( ) ] | ] ; ; let % test _ = is_never ( module struct type t = unit ref [ @@ deriving typerep ] end ) [ ref ( ) ] ; ; let % test _ = is_never ( module struct type t = int ref [ @@ deriving typerep ] end ) [ ref 1 ; ref 2 ; ref ( - 1 ) ] ; ; let % test_module _ = ( module struct type ' a t = ' a lazy_t [ @@ deriving typerep ] let % test _ = let conv = get_conv ( typerep_of_t typerep_of_unit ) in test_inconvertible_values conv [ lazy ( ( fun ( ) -> ( ) ) ( ) ) ] && test_convertibles conv [ Lazy . from_val ( ) , 0 ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let conv = get_conv ( typerep_of_t typerep_of_int ) in test_convertibles conv [ lazy 0 , 0 ; lazy ( - 1 ) , - 1 ; Lazy . from_val Int . max_value , Int . max_value ] && test_inconvertible_values conv [ lazy ( 1 + 2 ) ; lazy ( List . length [ ] ) ] ; ; let % test _ = let conv = get_conv ( typerep_of_t ( typerep_of_list typerep_of_int ) ) in let l1 = lazy ( List . filter [ 1 ; 2 ; 3 ; 4 ] ~ f ( : fun _ -> false ) ) in let l2 = lazy ( List . filter [ ] ~ f ( : fun _ -> true ) ) in let l3 = lazy ( List . filter [ 1 ] ~ f ( : fun _ -> true ) ) in let empty = Lazy . from_val [ ] in test_convertibles conv [ empty , 0 ] && test_inconvertible_values conv [ Lazy . from_val [ 1 ] ; l1 ; l2 ; l3 ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let conv = get_conv ( typerep_of_t ( typerep_of_t typerep_of_int ) ) in let l1 = lazy ( lazy ( 1 + 2 ) ) in test_convertibles conv [ Lazy . from_val ( Lazy . from_val 3 ) , 3 ] && test_inconvertible_values conv [ l1 ] ; ; end ) ; ; let % test _ = let module M = struct type t = { foo : unit } [ @@ deriving typerep ] end in is_never ( module M ) [ { M . foo = ( ) } ] ; ; let % test _ = is_never ( module struct type t = unit * unit [ @@ deriving typerep ] end ) [ ( ) , ( ) ] ; ; let % test _ = is_never ( module struct type t = unit * unit * unit [ @@ deriving typerep ] end ) [ ( ) , ( ) , ( ) ] ; ; let % test _ = is_never ( module struct type t = unit * unit * unit * unit [ @@ deriving typerep ] end ) [ ( ) , ( ) , ( ) , ( ) ] ; ; let % test _ = is_never ( module struct type t = unit * unit * unit * unit * unit [ @@ deriving typerep ] end ) [ ( ) , ( ) , ( ) , ( ) , ( ) ] ; ; let % test _ = let module M = struct type t = | Foo | Bar of unit [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ M . Foo , 0 ] && test_inconvertible_values conv [ M . Bar ( ) ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let module M = struct type t = | Foo | Bar | Baz [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ M . Foo , 0 ; M . Bar , 1 ; M . Baz , 2 ] && test_inconvertible_ints conv [ 3 ; 4 ; 5 ; 500 ; 232 ; - 1 ; - 2 ; Int . max_value ; Int . min_value ] ; ; let % test _ = let module M = struct type t = | Foo of unit | Bar of unit | Baz of unit [ @@ deriving typerep ] end in is_never ( module M ) [ M . Foo ( ) ; M . Bar ( ) ; M . Baz ( ) ] ; ; let filter_variants variants list = let variants = List . filter_map variants ~ f ( : fun v -> if Obj . is_int ( Obj . repr v ) then Some ( Obj . magic ( v : [ > ] ) : int ) else None ) in List . filter list ~ f ( : fun int -> List . for_all variants ~ f ( : fun variant -> variant <> int ) ) ; ; let % test _ = let module M = struct type t = [ ` Foo | ` Bar of unit ] [ @@ deriving typerep , enumerate ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ ` Foo , Typerep_obj . repr_of_poly_variant ` Foo ] && test_inconvertible_values conv [ ` Bar ( ) ] && test_inconvertible_ints conv ( filter_variants M . all [ 0 ; 1 ; 2 ; 3 ; - 1 ; - 2 ; 563 ; Int . max_value ; Int . min_value ; Typerep_obj . repr_of_poly_variant ` Bar ] ) ; ; let % test _ = let module M = struct type t = [ ` Foo | ` Bar | ` Baz ] [ @@ deriving typerep , enumerate ] end in let conv = get_conv M . typerep_of_t in test_convertibles conv [ ` Foo , Typerep_obj . repr_of_poly_variant ` Foo ; ` Bar , Typerep_obj . repr_of_poly_variant ` Bar ; ` Baz , Typerep_obj . repr_of_poly_variant ` Baz ] && test_inconvertible_ints conv ( filter_variants M . all [ 0 ; 1 ; 2 ; 3 ; - 1 ; - 2 ; 563 ; Int . max_value ; Int . min_value ] ) ; ; let % test _ = let module M = struct type t = [ ` Foo of unit | ` Bar of unit | ` Baz of unit ] [ @@ deriving typerep ] end in let conv = get_conv M . typerep_of_t in test_inconvertible_values conv [ ` Foo ( ) ; ` Bar ( ) ; ` Baz ( ) ] && test_inconvertible_ints conv ( [ Typerep_obj . repr_of_poly_variant ` Foo ; Typerep_obj . repr_of_poly_variant ` Bar ; Typerep_obj . repr_of_poly_variant ` Baz ] @ no_int_converts ) ; ; let % test _ = let module M = struct type ' a t = | Nil | Cons of ' a * ' a t [ @@ deriving typerep ] end in let conv = get_conv ( M . typerep_of_t typerep_of_unit ) in test_convertibles conv [ M . Nil , 0 ] && test_inconvertible_values conv [ M . Cons ( ( ) , M . Nil ) ; M . Cons ( ( ) , M . Cons ( ( ) , M . Nil ) ) ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let module M = struct type ' a t = | A | B of ' a t lazy_t | C of ' a * ' a t [ @@ deriving typerep ] end in let conv = get_conv ( M . typerep_of_t typerep_of_int ) in test_convertibles conv [ M . A , 0 ] && test_inconvertible_values conv [ M . B ( lazy M . A ) ; M . C ( 1 , M . A ) ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; let % test _ = let module M = struct type ' a t = | A | B of ( ' a t * ' a t ) | C of ' a [ @@ deriving typerep ] end in let conv = get_conv ( M . typerep_of_t typerep_of_unit ) in test_convertibles conv [ M . A , 0 ] && test_inconvertible_values conv [ M . B ( M . A , M . A ) ; M . C ( ) ] && test_inconvertible_ints conv no_int_but_zero_converts ; ; end ) ; ; |
( module struct type t = | Always | Sometimes | Never let imm_t = typerep_of_unit let box_t = typerep_of_ref typerep_of_int let is_imm v = Obj . is_int ( Obj . repr v ) let check expected typerep list = if List . length list = 0 then failwith " Must provide at least one test case . " ; let actual = ( Type_immediacy . dest ( Type_immediacy . of_typerep typerep ) , Type_immediacy . Always . of_typerep typerep , Type_immediacy . Sometimes . of_typerep typerep , Type_immediacy . Never . of_typerep typerep ) in let has_imm = List . exists ~ f : is_imm list in let has_boxed = List . exists ~ f ( : fun x -> not ( is_imm x ) ) list in match has_imm , has_boxed , expected , actual with | true , false , Always , ( Type_immediacy . Always _ , Some _ , None , None ) -> true | true , true , Sometimes , ( Type_immediacy . Sometimes _ , None , Some _ , None ) -> true | false , true , Never , ( Type_immediacy . Never _ , None , None , Some _ ) -> true | _ , _ , _ , _ -> false ; ; module T : sig val check_a : ' a Typerep . t -> ' a list -> bool val check_s : ' a Typerep . t -> ' a list -> bool val check_n : ' a Typerep . t -> ' a list -> bool end = struct let check_a typerep list = check Always typerep list let check_s typerep list = check Sometimes typerep list let check_n typerep list = check Never typerep list end include T let % test _ = let module M = struct type t = int [ @@ deriving typerep ] end in check_a M . typerep_of_t [ 0 ; 1 ] ; ; let % test ( _ [ @ tags " no - js " ] ) = let module M = struct type t = int32 [ @@ deriving typerep ] end in check_n M . typerep_of_t [ Int32 . zero ; Int32 . one ; Int32 . minus_one ; Int32 . of_int_exn 32580 ] ; ; let % test _ = let module M = struct type t = int64 [ @@ deriving typerep ] end in check_n M . typerep_of_t [ Int64 . zero ] ; ; let % test ( _ [ @ tags " no - js " ] ) = let module M = struct type t = nativeint [ @@ deriving typerep ] end in check_n M . typerep_of_t [ Nativeint . zero ] ; ; let % test _ = let module M = struct type t = char [ @@ deriving typerep ] end in check_a M . typerep_of_t [ ' a ' ] ; ; let % test ( _ [ @ tags " no - js " ] ) = let module M = struct type t = float [ @@ deriving typerep ] end in check_n M . typerep_of_t [ 1 . 1 ; 0 . 0 ; - 3 . 3 ; Float . nan ; Float . infinity ; Float . neg_infinity ; Float . max_value ; Float . min_value ] ; ; let % test _ = let module M = struct type t = string [ @@ deriving typerep ] end in check_n M . typerep_of_t [ " " ; " Hello world " ! ] ; ; let % test _ = let module M = struct type t = bool [ @@ deriving typerep ] end in check_a M . typerep_of_t [ true ; false ] ; ; let % test _ = let module M = struct type t = unit [ @@ deriving typerep ] end in check_a M . typerep_of_t [ ( ) ] ; ; let % test _ = let module M = struct type ' a t = ' a option [ @@ deriving typerep ] end in check_s ( M . typerep_of_t imm_t ) [ None ; Some ( ) ] && check_s ( M . typerep_of_t box_t ) [ None ; Some ( ref 1 ) ] ; ; let % test _ = let module M = struct type ' a t = ' a list [ @@ deriving typerep ] end in check_s ( M . typerep_of_t imm_t ) [ [ ] ; [ ( ) ] ] && check_s ( M . typerep_of_t box_t ) [ [ ] ; [ ref 1 ] ] ; ; let static_empty_array = [ ] || let % test _ = let module M = struct type ' a t = ' a array [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t ) [ static_empty_array ; [ ] ; || [ | ( ) ; ( ) ] | ] ; ; let % test _ = let module M = struct type ' a t = ' a ref [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t ) [ ref ( ) ] ; ; let % test _ = let module M = struct type ' a t = ' a lazy_t [ @@ deriving typerep ] end in check_s ( M . typerep_of_t typerep_of_int ) [ lazy 1 ; lazy ( 1 + 2 ) ] && check_n ( M . typerep_of_t typerep_of_float ) [ lazy 0 . 0 ; lazy ( 1 . 0 . + - 3 . 3 ) ] ; ; let % test _ = let module M = struct type t = { foo : unit } [ @@ deriving typerep ] end in check_n M . typerep_of_t [ { M . foo = ( ) } ] ; ; let % test _ = let module M = struct type ( ' a , ' b ) t = ' a * ' b [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t imm_t ) [ ( ) , ( ) ] ; ; let % test _ = let module M = struct type ( ' a , ' b , ' c ) t = ' a * ' b * ' c [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t imm_t imm_t ) [ ( ) , ( ) , ( ) ] ; ; let % test _ = let module M = struct type ( ' a , ' b , ' c , ' d ) t = ' a * ' b * ' c * ' d [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t imm_t imm_t imm_t ) [ ( ) , ( ) , ( ) , ( ) ] ; ; let % test _ = let module M = struct type ( ' a , ' b , ' c , ' d , ' e ) t = ' a * ' b * ' c * ' d * ' e [ @@ deriving typerep ] end in check_n ( M . typerep_of_t imm_t imm_t imm_t imm_t imm_t ) [ ( ) , ( ) , ( ) , ( ) , ( ) ] ; ; let % test _ = let module M = struct type t = | Foo | Bar of unit [ @@ deriving typerep ] end in check_s M . typerep_of_t [ M . Foo ; M . Bar ( ) ] ; ; let % test _ = let module M = struct type t = | Foo | Bar | Baz [ @@ deriving typerep ] end in check_a M . typerep_of_t [ M . Foo ; M . Bar ; M . Baz ] ; ; let % test _ = let module M = struct type t = | Foo of unit | Bar of unit | Baz of unit [ @@ deriving typerep ] end in check_n M . typerep_of_t [ M . Foo ( ) ; M . Bar ( ) ; M . Baz ( ) ] ; ; let % test _ = let module M = struct type t = Foo of unit [ @@ deriving typerep ] end in check_n M . typerep_of_t [ M . Foo ( ) ] ; ; let % test _ = let module M = struct type t = [ ` Foo | ` Bar of unit ] [ @@ deriving typerep ] end in check_s M . typerep_of_t [ ` Foo ; ` Bar ( ) ] ; ; let % test _ = let module M = struct type t = [ ` Foo | ` Bar | ` Baz ] [ @@ deriving typerep ] end in check_a M . typerep_of_t [ ` Foo ; ` Bar ; ` Baz ] ; ; let % test _ = let module M = struct type t = [ ` Foo of unit | ` Bar of unit | ` Baz of unit ] [ @@ deriving typerep ] end in check_n M . typerep_of_t [ ` Foo ( ) ; ` Bar ( ) ; ` Baz ( ) ] ; ; let % test _ = let module M = struct type t = [ ` Foo of unit ] [ @@ deriving typerep ] end in check_n M . typerep_of_t [ ` Foo ( ) ] ; ; let % test _ = let module M = struct type ' a t = | Nil | Cons of ' a * ' a t [ @@ deriving typerep ] end in check_s ( M . typerep_of_t typerep_of_int ) [ M . Nil ; M . Cons ( 1 , M . Cons ( 2 , M . Nil ) ) ] ; ; let % test _ = let module M = struct type ' a t = | A | B of ' a t lazy_t | C of ' a * ' a t [ @@ deriving typerep ] end in check_s ( M . typerep_of_t typerep_of_int ) [ M . A ; M . B ( lazy M . A ) ; M . C ( 1 , M . A ) ] ; ; let % test _ = let module M = struct type ' a t = | A | B of ( ' a t * ' a t ) | C of ' a [ @@ deriving typerep ] end in check_s ( M . typerep_of_t typerep_of_int ) [ M . A ; M . B ( M . A , M . C 1 ) ] ; ; let % test _ = let module M = struct type ' a t = | A | B | C [ @@ deriving typerep ] end in let module X = Type_immediacy . Always . For_all_parameters_S1 ( M ) in true ; ; let % test _ = let module M = struct type ' a t = ' a option lazy_t [ @@ deriving typerep ] end in let module X = Type_immediacy . Sometimes . For_all_parameters_S1 ( M ) in true ; ; let % test _ = let module M = struct type ' a t = ' a array lazy_t [ @@ deriving typerep ] end in let module X = Type_immediacy . Never . For_all_parameters_S1 ( M ) in true ; ; let % test _ = let module M = struct type ' a t = ' a lazy_t [ @@ deriving typerep ] end in try let module X = Type_immediacy . Sometimes . For_all_parameters_S1 ( M ) in false with | _ -> ( try let module X = Type_immediacy . Never . For_all_parameters_S1 ( M ) in false with | _ -> ( try let module X = Type_immediacy . Always . For_all_parameters_S1 ( M ) in false with | _ -> true ) ) ; ; end ) ; ; |
let hash_string_hex s = let md5_hex = Digest . string s |> Digest . to_hex in assert ( String . length md5_hex = 32 ) ; String . sub md5_hex 0 7 |
let is_alpha c = match c with | ' A ' . . ' Z ' | ' a ' . . ' z ' -> true | _ -> false |
let force_capitalize s = let need_prefix = s = " " || not ( is_alpha s . [ 0 ] ) in if need_prefix then capital_prefix ^ s else String . capitalize_ascii s |
let name_of_token ( token : token ) = Punct . to_alphanum token . name |
let encode_prec_name name = for i = 0 to String . length name - 1 do match name . [ i ] with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' -> ( ) | c -> failwith ( sprintf " Unsupported character % C in named precedence level % S " c name ) done ; name |
let name_of_num_prec n = if n >= 0 then sprintf " p % d " n else sprintf " n % d " ( abs n ) |
let name_of_prec_value ( p : Tree_sitter_t . prec_value ) = match p with | Num_prec n -> name_of_num_prec n | Named_prec name -> sprintf " x % s " ( encode_prec_name name ) |
let name_of_opt_prec_value p = match p with | None -> " 0 " | Some p -> " x " ^ name_of_prec_value p |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.