text
stringlengths
12
786k
let package_signatures units = let units_with_ids = List . map ( fun ( name , sg ) -> let oldid = Ident . create_persistent name in let newid = Ident . create_local name in ( oldid , newid , sg ) ) units in let subst = List . fold_left ( fun acc ( oldid , newid , _ ) -> Subst...
let package_units initial_env objfiles cmifile modulename = let units = List . map ( fun f -> let pref = chop_extensions f in let modname = String . capitalize_ascii ( Filename . basename pref ) in let sg = Env . read_signature modname ( pref ^ " . cmi " ) in if Filename . check_suffix ...
let report_error ppf = function Cannot_apply mty -> fprintf ppf " [ @ This module is not a functor ; it has type @ % a ] " @ modtype mty | Not_included errs -> fprintf ppf " [ @< v > Signature mismatch :@ % a ] " @ Includemod . report_error errs | Cannot_eliminate_dependency mty -> fp...
let report_error env ppf err = Printtyp . wrap_printing_env ~ error : true env ( fun ( ) -> report_error ppf err )
let ( ) = Location . register_error_of_exn ( function | Error ( loc , env , err ) -> Some ( Location . error_of_printer ~ loc ( report_error env ) err ) | Error_forward err -> Some err | _ -> None )
let should_hide ( x : Typedtree . module_binding ) = match x . mb_attributes with | [ ] -> false | { attr_name = { txt = " internal . local " ; _ } ; _ } :: _ -> true | _ :: rest -> Ext_list . exists rest ( fun { attr_name = x ; _ } -> x . txt = " internal...
let attrs : Parsetree . attributes = [ { attr_name = { txt = " internal . local " ; loc = Location . none } ; attr_payload = PStr [ ] ; attr_loc = Location . none ; } ; ]
let no_type_defined ( x : Parsetree . structure_item ) = match x . pstr_desc with | Pstr_eval _ | Pstr_value _ | Pstr_primitive _ | Pstr_typext _ | Pstr_exception _ -> true | Pstr_include { pincl_mod = { pmod_desc = Pmod_constraint ( { pmod_desc = Pmod_structure [ { pstr_desc = Pst...
let check ( x : Parsetree . structure ) = Ext_list . iter x ( fun x -> if not ( no_type_defined x ) then Location . raise_errorf ~ loc : x . pstr_loc " the structure is not supported in local extension " )
module List = struct include List let compare cmp a b = let rec loop a b = match a , b with | [ ] , [ ] -> 0 | [ ] , _ -> - 1 | _ , [ ] -> 1 | x :: xs , y :: ys -> let n = cmp x y in if n = 0 then loop xs ys else n in loop a b ; ; end
module Uid : sig type t val compare : t -> t -> int val equal : t -> t -> bool val next : string -> t val hash : t -> int val name : t -> string val static : t type t = { code : int ; name : string } let compare a b = compare ( a . code : int ) b . code let equal a b = ( a . code ...
module Key = struct type t = { uid : Uid . t ; params : t list } let rec compare k1 k2 = if k1 == k2 then 0 else ( let cmp = Uid . compare k1 . uid k2 . uid in if cmp <> 0 then cmp else List . compare compare k1 . params k2 . params ) ; ; let equal a b = compare a b = 0 let hash...
type ' a t = Key . t
type ' a typename = ' a t
let key t = t
let uid t = t . Key . uid
let name t = Uid . name t . Key . uid
let create ( ? name = " Typename . create " ) ( ) = { Key . uid = Uid . next name ; params = [ ] } let same ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = Key . compare nm1 nm2 = 0 let same_witness ( type a b ) ( nm1 : a t ) ( nm2 : b t ) = if Key . compa...
module type S0 = sig type t val typename_of_t : t typename end
module type S1 = sig type ' a t val typename_of_t : ' a typename -> ' a t typename end
module type S2 = sig type ( ' a , ' b ) t val typename_of_t : ' a typename -> ' b typename -> ( ' a , ' b ) t typename end
module type S3 = sig type ( ' a , ' b , ' c ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ( ' a , ' b , ' c ) t typename end
module type S4 = sig type ( ' a , ' b , ' c , ' d ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ( ' a , ' b , ' c , ' d ) t typename end
module type S5 = sig type ( ' a , ' b , ' c , ' d , ' e ) t val typename_of_t : ' a typename -> ' b typename -> ' c typename -> ' d typename -> ' e typename -> ( ' a , ' b , ' c , ' d , ' e ) t typename end
module Make0 ( X : Named_intf . S0 ) = struct let uid = Uid . next X . name let typename_of_t = { Key . uid ; params = [ ] } end
module Make1 ( X : Named_intf . S1 ) = struct let uid = Uid . next X . name let typename_of_t a = { Key . uid ; params = [ a ] } end
module Make2 ( X : Named_intf . S2 ) = struct let uid = Uid . next X . name let typename_of_t a b = { Key . uid ; params = [ a ; b ] } end
module Make3 ( X : Named_intf . S3 ) = struct let uid = Uid . next X . name let typename_of_t a b c = { Key . uid ; params = [ a ; b ; c ] } end
module Make4 ( X : Named_intf . S4 ) = struct let uid = Uid . next X . name let typename_of_t a b c d = { Key . uid ; params = [ a ; b ; c ; d ] } end
module Make5 ( X : Named_intf . S5 ) = struct let uid = Uid . next X . name let typename_of_t a b c d e = { Key . uid ; params = [ a ; b ; c ; d ; e ] } end
module Key_table = Hashtbl . Make ( Key )
module Table ( X : sig type ' a t end ) = struct type data = Data : ' a t * ' a X . t -> data type t = data Key_table . t let create int = Key_table . create int let mem table name = Key_table . mem table ( key name ) let set table name data = Key_table . replace table ( key name ) ...
let fail uid_a uid_b = let msg = Printf . sprintf " Typename . Same_witness_exn % S % S " ( Uid . name uid_a ) ( Uid . name uid_b ) in failwith msg ; ;
module Same_witness_exn_1 ( A : S1 ) ( B : S1 ) = struct type t = { eq : ' a . ( ' a A . t , ' a B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static ) in let uid_b = uid ( B . typename_of_t static ) in if Uid . equal uid_a uid_b then { ...
module Same_witness_exn_2 ( A : S2 ) ( B : S2 ) = struct type t = { eq : ' a ' b . ( ( ' a , ' b ) A . t , ( ' a , ' b ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static ) in let uid_b = uid ( B . typename_of_t s...
module Same_witness_exn_3 ( A : S3 ) ( B : S3 ) = struct type t = { eq : ' a ' b ' c . ( ( ' a , ' b , ' c ) A . t , ( ' a , ' b , ' c ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static static static ) in let uid_b...
module Same_witness_exn_4 ( A : S4 ) ( B : S4 ) = struct type t = { eq : ' a ' b ' c ' d . ( ( ' a , ' b , ' c , ' d ) A . t , ( ' a , ' b , ' c , ' d ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . typename_of_t static stat...
module Same_witness_exn_5 ( A : S5 ) ( B : S5 ) = struct type t = { eq : ' a ' b ' c ' d ' e . ( ( ' a , ' b , ' c , ' d , ' e ) A . t , ( ' a , ' b , ' c , ' d , ' e ) B . t ) Type_equal . t } let witness = let uid_a = uid ( A . ...
type t = { env : Env . t ; get_search_dirs : string -> string list } let from_summary ~ get_search_dirs summary subst = Persistent_env . get_search_dirs := get_search_dirs ; Envaux . reset_cache ( ) ; let env = Envaux . env_from_summary ( Obj . magic summary ) summary subst in { env ;...
type callable_and_self_argument = { callable : Type . Callable . t ; self_argument : Type . t option ; }
module TypeOperation = struct module Compose = struct let apply_callable ~ signature_select input_annotation ~ callable_and_self { : callable ; self_argument } = match signature_select ~ arguments : [ { AttributeResolution . Argument . expression = None ; kind = Ast . Expression . Call . ...
let to_defined parameters = Type . Callable . Defined ( List . mapi ~ f ( : fun i element -> Type . Callable . Parameter . PositionalOnly { index = i ; annotation = element ; default = false } ) parameters )
let make_overload ~ parameters ~ return = Some { Type . Callable . annotation = return ; parameters = to_defined parameters }
let test_compose_list _ = let assert_compose_list ( ? aliases = fun _ -> None ) ~ signature_select given expected = let parse source = Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) in let merge...
let ( ) = " typeOperation " >::: [ " compose_list " >:: test_compose_list ] |> Test . run
let scrape_ty env ty = let ty = Ctype . expand_head_opt env ( Ctype . correct_levels ty ) in match ty . desc with | Tconstr ( p , _ , _ ) -> begin match Env . find_type p env with | { type_unboxed = { unboxed = true ; _ } ; _ } -> begin match Typedecl . get_unboxed_type_rep...
let scrape env ty = ( scrape_ty env ty ) . desc
let is_function_type env ty = match scrape env ty with | Tarrow ( _ , lhs , rhs , _ ) -> Some ( lhs , rhs ) | _ -> None
let is_base_type env ty base_ty_path = match scrape env ty with | Tconstr ( p , _ , _ ) -> Path . same p base_ty_path | _ -> false
let maybe_pointer_type env ty = let ty = scrape_ty env ty in if Ctype . maybe_pointer_type env ty then Pointer else Immediate
let maybe_pointer exp = maybe_pointer_type exp . exp_env exp . exp_type
type classification = | Int | Float | Lazy | Addr | Any
let classify env ty = let ty = scrape_ty env ty in if maybe_pointer_type env ty = Immediate then Int else match ty . desc with | Tvar _ | Tunivar _ -> Any | Tconstr ( p , _args , _abbrev ) -> if Path . same p Predef . path_float then Float else if Path . same p Predef . path_lazy_t then La...
let array_type_kind env ty = match scrape env ty with | Tconstr ( p , [ elt_ty ] , _ ) | Tpoly ( { desc = Tconstr ( p , [ elt_ty ] , _ ) } , _ ) when Path . same p Predef . path_array -> begin match classify env elt_ty with | Any -> if Config . flat_float_array then Pge...
let array_kind exp = array_type_kind exp . exp_env exp . exp_type
let array_pattern_kind pat = array_type_kind pat . pat_env pat . pat_type
let bigarray_decode_type env ty tbl dfl = match scrape env ty with | Tconstr ( Pdot ( Pident mod_id , type_name ) , [ ] , _ ) when Ident . name mod_id = " Stdlib__bigarray " -> begin try List . assoc type_name tbl with Not_found -> dfl end | _ -> dfl
let kind_table = [ " float32_elt " , Pbigarray_float32 ; " float64_elt " , Pbigarray_float64 ; " int8_signed_elt " , Pbigarray_sint8 ; " int8_unsigned_elt " , Pbigarray_uint8 ; " int16_signed_elt " , Pbigarray_sint16 ; " int16_unsigned_elt " , Pbigarray_uint16 ; " in...
let layout_table = [ " c_layout " , Pbigarray_c_layout ; " fortran_layout " , Pbigarray_fortran_layout ]
let bigarray_type_kind_and_layout env typ = match scrape env typ with | Tconstr ( _p , [ _caml_type ; elt_type ; layout_type ] , _abbrev ) -> ( bigarray_decode_type env elt_type kind_table Pbigarray_unknown , bigarray_decode_type env layout_type layout_table Pbigarray_unknown_layout ) | _ ...
let value_kind env ty = match scrape env ty with | Tconstr ( p , _ , _ ) when Path . same p Predef . path_int -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_char -> Pintval | Tconstr ( p , _ , _ ) when Path . same p Predef . path_float -> Pfloatval ...
let function_return_value_kind env ty = match is_function_type env ty with | Some ( _lhs , rhs ) -> value_kind env rhs | None -> Pgenval
let lazy_val_requires_forward env ty = match classify env ty with | Any | Lazy -> true | Float -> Config . flat_float_array | Addr | Int -> false
let classify_lazy_argument : Typedtree . expression -> [ ` Constant_or_function ` | Float_that_cannot_be_shortcut ` | Identifier of [ ` Forward_value ` | Other ] ` | Other ] = fun e -> match e . exp_desc with | Texp_constant ( Const_int _ | Const_char _ | Const_string _ | Const_...
let value_kind_union k1 k2 = if k1 = k2 then k1 else Pgenval
module type FullOrderTypeWithoutT = sig val always_less_or_equal : order -> left : Type . t -> right : Type . t -> bool val meet : order -> Type . t -> Type . t -> Type . t val join : order -> Type . t -> Type . t -> Type . t end
module type FullOrderType = sig type t = order include FullOrderTypeWithoutT end
module type OrderedConstraintsType = TypeConstraints . OrderedConstraintsType with type order = order
module OrderImplementation = struct module Make ( OrderedConstraintsSet : ConstraintsSet . OrderedConstraintsSetType ) = struct type t = order let rec always_less_or_equal order ~ left ~ right = OrderedConstraintsSet . add ConstraintsSet . empty ~ new_constraint : ( LessOrEqual { left = Type . ...
module rec Constraints : OrderedConstraintsType = TypeConstraints . OrderedConstraints ( Implementation ) ConstraintsSet . Make ( Constraints )
module IncludableImplementation : FullOrderTypeWithoutT = Implementation
let rec is_compatible_with order ~ left ~ right = let fallback ( ) = always_less_or_equal order ~ left ~ right in match left , right with | _ , Type . Any | Type . Any , _ -> true | _ , Type . Top -> true | Type . Top , _ -> false | Type . NoneType , Type . NoneType -> tru...
let widen order ~ widening_threshold ~ previous ~ next ~ iteration = if iteration > widening_threshold then Type . Top else join order previous next
let ( ! ) concretes = List . map concretes ~ f ( : fun single -> Type . Parameter . Single single )
let environment ? source context = let { ScratchProject . BuiltGlobalEnvironment . global_environment ; _ } = let sources = Option . value_map source ~ f ( : fun source -> [ " test . py " , source ] ) ~ default [ ] : in ScratchProject . setup ~ context sources |> ScratchPro...
let resolution ? source context = let environment = environment ? source context in AnnotatedGlobalEnvironment . read_only environment |> GlobalResolution . create
let concrete_connect ? parameters = let parameters = parameters >>| List . map ~ f ( : fun single -> Type . Parameter . Single single ) in MockClassHierarchyHandler . connect ? parameters
let make_attributes ~ class_name = let parse_attribute ( name , annotation ) = Attribute . create ~ annotation ~ original_annotation : annotation ~ uninstantiated_annotation ( : Some annotation ) ~ visibility : ReadWrite ~ abstract : false ~ async_property : false ~ class_variable : false ~...
let parse_attributes ~ class_name ~ parse_annotation attributes = List . map attributes ~ f ( : fun ( name , annotation ) -> name , parse_annotation annotation ) |> make_attributes ~ class_name
let get_typed_dictionary _ = None
let hierarchy class_hierarchy_handler = { ConstraintsSet . instantiate_successors_parameters = ClassHierarchy . instantiate_successors_parameters class_hierarchy_handler ; is_transitive_successor = ClassHierarchy . is_transitive_successor class_hierarchy_handler ; variables = ClassHierarchy . variable...
let attribute_from_attributes attributes = let attribute annotation ~ assumptions ~ name = let find attribute = String . equal ( Annotated . Attribute . name attribute ) name in attributes annotation ~ assumptions >>= List . find ~ f : find in attribute
let less_or_equal ( ? attributes = fun _ ~ assumptions : _ -> None ) ( ? is_protocol = fun _ ~ protocol_assumptions : _ -> false ) handler = always_less_or_equal { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = attributes ; attribute = attribute_from_attributes...
let is_compatible_with handler = is_compatible_with { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = ( fun _ ~ assumptions : _ -> None ) ; attribute = ( fun _ ~ assumptions : _ ~ name : _ -> None ) ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> ...
let join ( ? attributes = fun _ ~ assumptions : _ -> None ) handler = join { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = attributes ; attribute = attribute_from_attributes attributes ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> false ) ; assumpti...
let meet handler = meet { ConstraintsSet . class_hierarchy = hierarchy handler ; all_attributes = ( fun _ ~ assumptions : _ -> None ) ; attribute = ( fun _ ~ assumptions : _ ~ name : _ -> None ) ; is_protocol = ( fun _ ~ protocol_assumptions : _ -> false ) ; assumptions =...
let order = let bottom = " bottom " in let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order bottom ; insert order " 0 " ; insert order " 1 " ; insert order " 2 " ; insert order " 3 " ; insert order " 4 " ; insert order...
let disconnected_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " A " ; insert order " B " ; handler order
let variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " object " ; insert order " bool " ; insert order " str " ; insert order " int " ; insert order " float " ; connect order ~ predecessor " : int " ~ s...
let multiplane_variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " str " ; insert order " int " ; insert order " float " ; insert order " object " ; insert order " bool " ; connect order ~ predecessor " : i...
let parallel_planes_variance_order = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " str " ; insert order " int " ; insert order " float " ; insert order " object " ; connect order ~ predecessor " : int " ~ successor " ...
let default = let order = MockClassHierarchyHandler . create ( ) in let open MockClassHierarchyHandler in insert order " typing . Generic " ; insert order " int " ; insert order " str " ; insert order " bytes " ; insert order " bool " ; insert order " float " ; insert order...
let ( !! ) name = Type . Primitive name
let test_less_or_equal context = assert_true ( less_or_equal order ~ left : Type . Bottom ~ right : Type . Top ) ; assert_false ( less_or_equal order ~ left : Type . Top ~ right : Type . Bottom ) ; assert_true ( less_or_equal order ~ left " :!! 0 " ~ right " :!! 0 " ) ; ...
let test_is_compatible_with _ = let assert_is_compatible ( ? order = default ) left right = assert_true ( is_compatible_with order ~ left ~ right ) in let assert_not_compatible ( ? order = default ) left right = assert_false ( is_compatible_with order ~ left ~ right ) in let list_of_integer ...
let test_less_or_equal_variance _ = let assert_strict_less ~ order ~ right ~ left = assert_equal ~ printer ( : fun pair -> Format . sprintf " % B , % B " ( fst pair ) ( snd pair ) ) ( true , false ) ( less_or_equal order ~ left ~ right , less_or_equal order ~ left : right ~...
let test_join context = let assert_join ( ? order = default ) ( ? aliases = Type . empty_aliases ) left right expected = let parse_annotation = function | " $ bottom " -> Type . Bottom | _ as source -> parse_single_expression source |> Type . create ~ aliases in let attributes annotatio...
let test_meet _ = let assert_meet ( ? order = default ) ( ? aliases = Type . empty_aliases ) left right expected = let parse_annotation = function | " $ bottom " -> Type . Bottom | _ as source -> parse_single_expression source |> Type . create ~ aliases in assert_type_equal ( parse_a...
let test_meet_callable _ = let assert_meet ( ? order = default ) left right expected = assert_type_equal expected ( meet order left right ) in let named_int_to_int = Type . Callable . create ~ name ( : Reference . create " name " ) ~ parameters : ( Defined [ Type . Callable . Par...
let ( ) = " order " >::: [ " join " >:: test_join ; " less_or_equal " >:: test_less_or_equal ; " less_or_equal_variance " >:: test_less_or_equal_variance ; " is_compatible_with " >:: test_is_compatible_with ; " meet " >:: test_meet ; " meet_callable " >:: test_meet_ca...