text
stringlengths 0
601k
|
---|
let resolve_all ( defns : Unresolved . t list ) = let names = name_table defns in let defns = String . Map . values names in let rename_change_field = object inherit Unresolved . map as super method ! field x = if x . name = " change " then match x . data with | Single { typ = Ident " number " ; optional = true } -> let typ = Unresolved . Ident " TextDocumentSyncKind " in let data = Unresolved . Single { typ ; optional = true } in super # field { x with data } | _ -> super # field x else super # field x end in let unresolved = List . map defns ~ f : rename_change_field # t in Ts_types . resolve_all unresolved ~ names |
let test_snippets s = let fails , succs = List . partition_map s ~ f ( : fun s -> let lexbuf = Lexing . from_string s in try Right ( Ts_parser . main Ts_lexer . token lexbuf ) with | exn -> Left { snippet = s ; loc = lexbuf . lex_curr_p ; exn } ) in ignore ( resolve_all ( List . concat succs ) ) ; fails |
let pp_results ppf tests = List . iteri tests ~ f ( : fun i { snippet ; loc ; exn } -> Format . pp_print_string ppf snippet ; Format . fprintf ppf " line : % d char : % d . " @ loc . pos_lnum ( loc . pos_cnum - loc . pos_bol ) ; Format . fprintf ppf " % d . exn : % s . " @%! ( i + 1 ) ( Printexc . to_string exn ) ; Format . fprintf ppf " . . " ) @---@ |
let of_snippets s = List . concat_map s ~ f ( : fun s -> let lexbuf = Lexing . from_string s in try Ts_parser . main Ts_lexer . token lexbuf with | _exn -> [ ] ) |
module Type_var_names = struct let last_char : char ref = ref ' a ' let table : ( Types . type_expr , char ) Hashtbl . t = Hashtbl . create 1024 let reset ( ) = last_char := ' a ' let find t = match Hashtbl . find_opt table t with | None -> Hashtbl . add table t ! last_char ; let ret = String . make 1 ! last_char in last_char := Char . chr ( Char . code ! last_char + 1 ) ; ret | Some v -> String . make 1 v end |
module Fun = struct let rec mk_type_expr : Types . type_expr -> Erlang . Ast . type_expr = fun type_expr -> match type_expr . desc with | Tarrow ( _ , _ , _ , _ ) -> ( match Uncurry . from_type_expr type_expr with | ` Uncurried ( args , return ) -> let args = List . map mk_type_expr args in let return = mk_type_expr return in Type . fun_ ~ args ~ return | ` Not_a_function -> Format . fprintf Format . std_formatter " Tried to uncurry a non - function type !\ n " ; %! Printtyp . type_expr Format . std_formatter type_expr ; Format . fprintf Format . std_formatter " \ n " ; %! Error . unsupported_feature ` Uncurryable_functions ) | Tconstr ( p , args , _ ) -> let name , args = Names . type_name_of_path p ~ args in let args = List . map mk_type_expr args in Type . apply ~ name ~ args | Ttuple els -> let parts = els |> List . map mk_type_expr in Type . tuple parts | Tlink t -> mk_type_expr ( Btype . repr t ) | Tvar ( Some name ) -> Type . var ( Name . var ( " _ " ^ name ) ) | Tvar None -> Type . var ( Name . var ( Type_var_names . find type_expr ) ) | Tnil -> Type . apply ~ name ( : Name . atom ( Atom . mk " list " ) ) ~ args [ ] : | Tvariant { row_fields ; _ } -> let row_field_to_type_expr = function | Rpresent ( Some texpr ) -> [ mk_type_expr texpr ] | Rpresent None -> [ ] | Reither ( _ , args , _ , _ ) -> List . map mk_type_expr args | _ -> Format . fprintf Format . std_formatter " Tried to build a type expr for an odd variant constructor !\ n " ; %! Printtyp . type_expr Format . std_formatter type_expr ; Format . fprintf Format . std_formatter " \ n " ; %! Printtyp . raw_type_expr Format . std_formatter type_expr ; Format . fprintf Format . std_formatter " \ n " ; %! Error . unsupported_feature ` Absent_polymorphic_variants in let row_field_to_constr ( name , args ) = let name = Const . atom ( Atom . lowercase ( Atom . mk name ) ) in let args = row_field_to_type_expr args in match args with | [ ] -> Type . const name | _ -> Type . tuple ( Type . const name :: args ) in List . map row_field_to_constr row_fields |> Type . variant | Tpoly ( _ , _ ) | Tfield ( _ , _ , _ , _ ) | Tsubst _ | Tunivar _ | Tobject _ | Tpackage _ -> Format . fprintf Format . std_formatter " Tried to build a type expr for an unsupported feature !\ n " ; %! Printtyp . type_expr Format . std_formatter type_expr ; Format . fprintf Format . std_formatter " \ n " ; %! Printtyp . raw_type_expr Format . std_formatter type_expr ; Format . fprintf Format . std_formatter " \ n " ; %! Error . unsupported_feature ` Type_constructs let mk_spec : Types . value_description -> Erlang . Ast . type_expr option = fun { val_type ; _ } -> Type_var_names . reset ( ) ; match Uncurry . from_type_expr val_type with | ` Uncurried ( args , return ) -> let args = List . map mk_type_expr args in let return = mk_type_expr return in let fun_ = Type . fun_ ~ args ~ return in let clean_fun = Type . clean_unbound_named_vars fun_ in Some clean_fun | ` Not_a_function -> None let find_spec : typedtree : Typedtree . structure -> Erlang . Ast . atom -> Erlang . Ast . type_expr option = fun ~ typedtree name -> match List . filter_map ( function | Types . Sig_value ( id , vd , _ ) -> ( let type_name = Names . atom_of_ident id in match Atom . equal type_name name with | true -> mk_spec vd | false -> None ) | _ -> None ) typedtree . str_type with | [ x ] -> Some x | _ -> None end |
let rec is_unit ( t : Types . type_expr ) = match t . desc with | Tconstr ( p , _ , _ ) -> Path . same p Predef . path_unit | Tlink t ' -> is_unit ( Btype . repr t ' ) | _ -> false |
let is_opaque_in_signature type_decl signature = match signature with | None -> Type . type_ | Some sign -> List . fold_left ( fun kind sig_item -> match sig_item with | Sig_type ( name , { type_manifest = None ; _ } , _ , _ ) when Ident . name name = Ident . name type_decl . typ_id -> Type . opaque | _ -> kind ) Type . type_ sign |
let rec mk_type_expr core_type = match core_type . ctyp_desc with | Ttyp_any -> Some Type . any | Ttyp_var var_name -> Some ( Type . var ( Name . var var_name ) ) | Ttyp_arrow ( _ , param , out ) -> let rec args t acc = match t . ctyp_desc with | Ttyp_arrow ( _ , p , t ' ) -> args t ' ( p :: acc ) | _ -> t :: acc in let args = args out [ param ] |> List . filter_map mk_type_expr in let return = List . hd args in let args = List . rev ( List . tl args ) in Some ( Type . fun_ ~ args ~ return ) | Ttyp_constr ( _ , { txt ; _ } , args ) -> let name , args = Names . longident_to_type_name ~ args txt in let args = List . filter_map mk_type_expr args in Some ( Type . apply ~ args ~ name ) | Ttyp_tuple els -> let parts = List . filter_map mk_type_expr els in Some ( Type . tuple parts ) | Ttyp_variant ( rows , _closed , _labels ) -> let rec all_rows rs acc = match rs with | [ ] -> acc |> List . rev | r :: rs ' -> ( match r . rf_desc with | Ttag ( { txt ; _ } , _ , core_types ) -> let name = Const . atom ( Atom . lowercase ( Atom . mk txt ) ) in let args = core_types |> List . filter_map mk_type_expr in let variant = match args with | [ ] -> Type . const name | _ -> Type . tuple ( Type . const name :: args ) in all_rows rs ' ( variant :: acc ) | Tinherit { ctyp_desc = Ttyp_constr ( _ , { txt ; _ } , args ) ; _ } -> let name , args = Names . longident_to_type_name ~ args txt in let args = List . filter_map mk_type_expr args in let t = Type . apply ~ name ~ args in all_rows rs ' ( t :: acc ) | _ -> all_rows rs ' acc ) in let constructors = all_rows rows [ ] in Some ( Type . variant constructors ) | Ttyp_poly ( _names , follow ) -> mk_type_expr follow | Ttyp_alias ( follow , _ ) -> mk_type_expr follow | Ttyp_object _ | Ttyp_class _ | Ttyp_package _ -> Error . unsupported_feature ` Type_objects_and_packages |
let mk_record labels = let mk_field Typedtree . { ld_id ; ld_type ; _ } = let rf_name = Names . atom_of_ident ld_id in let rf_type = match mk_type_expr ld_type with Some t -> t | None -> Type . any in Type . map_field ~ presence : Mandatory ( Type . const ( Const . atom rf_name ) ) rf_type in let fields = List . map mk_field labels in Type . map fields |
let mk_abstract name params type_decl core_type signature = match mk_type_expr core_type with | Some expr -> let kind = is_opaque_in_signature type_decl signature in Some ( Type . mk ~ name ~ params ~ expr ~ kind ) | None -> None |
let mk_type_params params = params |> List . filter_map ( fun ( core_type , _ ) -> match core_type . ctyp_desc with | Ttyp_var name -> Some ( Name . var name ) | _ -> None ) |
let mk_type type_decl ~ signature = let name = Names . atom_of_ident type_decl . typ_id in let params = mk_type_params type_decl . typ_params in let params = List . map Type . var params in match type_decl . typ_kind with | Ttype_abstract -> ( match type_decl . typ_manifest with | Some abs -> mk_abstract name params type_decl abs signature | None -> let expr = Type . apply ~ args [ ] : ~ name ( : Name . atom ( Atom . mk " reference " ) ) in Some ( Type . mk ~ name ~ params ~ expr ~ kind : Opaque ) ) | Ttype_record labels -> let expr = mk_record labels in Some ( Type . mk ~ name ~ params ~ expr ~ kind : Type ) | Ttype_variant constructors -> let mk_constr Typedtree . { cd_id ; cd_args ; _ } = let args = match cd_args with | Cstr_tuple core_types -> core_types |> List . filter_map mk_type_expr | Cstr_record labels -> [ mk_record labels ] in let name = Const . atom ( Names . atom_of_ident cd_id ) in match args with | [ ] -> Type . const name | _ -> Type . tuple ( Type . const name :: args ) in let constructors = List . map mk_constr constructors in Some ( Type . mk ~ name ~ params ~ expr ( : Type . variant constructors ) ~ kind : Type ) | Ttype_open -> Some ( Type . mk ~ name ~ params ~ expr : Type . any ~ kind : Type ) |
let mk_types : Typedtree . structure -> Types . signature option -> Erlang . Ast . type_decl list = fun typedtree signature -> typedtree . str_items |> List . concat_map ( fun item -> match item . str_desc with Tstr_type ( _ , tys ) -> tys | _ -> [ ] ) |> List . filter_map ( mk_type ~ signature ) |
type inner_curve_var = Tick . Field . t Snarky_backendless . Cvar . t * Tick . Field . t Snarky_backendless . Cvar . t |
module Basic = struct type ( ' var , ' value , ' n1 , ' n2 ) ' n2 t = { max_proofs_verified : ( module Nat . Add . Intf with type n = ' n1 ) ' n1 ; value_to_field_elements : ' value -> Impls . Step . Field . Constant . t array ; var_to_field_elements : ' var -> Impls . Step . Field . t array ; typ : ( ' var , ' value ) ' value Impls . Step . Typ . t ; branches : ' n2 Nat . t ; wrap_domains : Domains . t ; wrap_key : Tick . Inner_curve . Affine . t Plonk_verification_key_evals . t ; wrap_vk : Impls . Wrap . Verification_key . t } end |
module Side_loaded = struct module Ephemeral = struct type t = { index : [ ` In_circuit of Side_loaded_verification_key . Checked . t | ` In_prover of Side_loaded_verification_key . t ] } end module Permanent = struct type ( ' var , ' value , ' n1 , ' n2 ) ' n2 t = { max_proofs_verified : ( module Nat . Add . Intf with type n = ' n1 ) ' n1 ; value_to_field_elements : ' value -> Impls . Step . Field . Constant . t array ; var_to_field_elements : ' var -> Impls . Step . Field . t array ; typ : ( ' var , ' value ) ' value Impls . Step . Typ . t ; branches : ' n2 Nat . t } end type ( ' var , ' value , ' n1 , ' n2 ) ' n2 t = { ephemeral : Ephemeral . t option ; permanent : ( ' var , ' value , ' n1 , ' n2 ) ' n2 Permanent . t } type packed = | T : ( ' var , ' value , ' n1 , ' n2 ) ' n2 Tag . tag * ( ' var , ' value , ' n1 , ' n2 ) ' n2 t -> packed let to_basic { permanent = { max_proofs_verified ; value_to_field_elements ; var_to_field_elements ; typ ; branches } ; ephemeral } = let wrap_key , wrap_vk = match ephemeral with | Some { index = ` In_prover i } -> ( i . wrap_index , i . wrap_vk ) wrap_vk | _ -> failwithf " Side_loaded . to_basic : Expected ` In_prover ( % s ) s " __LOC__ ( ) in let proofs_verified = Nat . to_int ( Nat . Add . n max_proofs_verified ) max_proofs_verified in let wrap_vk = Option . value_exn ~ here [ :% here ] here wrap_vk in { Basic . max_proofs_verified ; wrap_vk ; value_to_field_elements ; var_to_field_elements ; typ ; branches ; wrap_domains = Common . wrap_domains ~ proofs_verified ; wrap_key } end |
module Compiled = struct type f = Impls . Wrap . field type ( ' a_var , ' a_value , ' max_proofs_verified , ' branches ) ' branches basic = { typ : ( ' a_var , ' a_value ) ' a_value Impls . Step . Typ . t ; proofs_verifieds : ( int , ' branches ) ' branches Vector . t ; var_to_field_elements : ' a_var -> Impls . Step . Field . t array ; value_to_field_elements : ' a_value -> Tick . Field . t array ; wrap_domains : Domains . t ; step_domains : ( Domains . t , ' branches ) ' branches Vector . t } type ( ' a_var , ' a_value , ' max_proofs_verified , ' branches ) ' branches t = { branches : ' branches Nat . t ; max_proofs_verified : ( module Nat . Add . Intf with type n = ' max_proofs_verified ) ' max_proofs_verified ; proofs_verifieds : ( int , ' branches ) ' branches Vector . t ; typ : ( ' a_var , ' a_value ) ' a_value Impls . Step . Typ . t ; value_to_field_elements : ' a_value -> Tick . Field . t array ; var_to_field_elements : ' a_var -> Impls . Step . Field . t array ; wrap_key : Tick . Inner_curve . Affine . t Plonk_verification_key_evals . t Lazy . t ; wrap_vk : Impls . Wrap . Verification_key . t Lazy . t ; wrap_domains : Domains . t ; step_domains : ( Domains . t , ' branches ) ' branches Vector . t } type packed = | T : ( ' var , ' value , ' n1 , ' n2 ) ' n2 Tag . tag * ( ' var , ' value , ' n1 , ' n2 ) ' n2 t -> packed let to_basic { branches ; max_proofs_verified ; proofs_verifieds ; typ ; value_to_field_elements ; var_to_field_elements ; wrap_vk ; wrap_domains ; step_domains ; wrap_key } = { Basic . max_proofs_verified ; wrap_domains ; value_to_field_elements ; var_to_field_elements ; typ ; branches = Vector . length step_domains ; wrap_key = Lazy . force wrap_key ; wrap_vk = Lazy . force wrap_vk } end |
module For_step = struct type ( ' a_var , ' a_value , ' max_proofs_verified , ' branches ) ' branches t = { branches : ' branches Nat . t ; max_proofs_verified : ( module Nat . Add . Intf with type n = ' max_proofs_verified ) ' max_proofs_verified ; proofs_verifieds : ( Impls . Step . Field . t , ' branches ) ' branches Vector . t ; typ : ( ' a_var , ' a_value ) ' a_value Impls . Step . Typ . t ; value_to_field_elements : ' a_value -> Tick . Field . t array ; var_to_field_elements : ' a_var -> Impls . Step . Field . t array ; wrap_key : inner_curve_var Plonk_verification_key_evals . t ; wrap_domains : Domains . t ; step_domains : [ ` Known of ( Domains . t , ' branches ) ' branches Vector . t | ` Side_loaded of ( Impls . Step . Field . t Side_loaded_verification_key . Domain . t Side_loaded_verification_key . Domains . t , ' branches ) Vector . t ] ; max_width : Side_loaded_verification_key . Width . Checked . t option } let of_side_loaded ( type a b c d ) d ( { ephemeral ; permanent = { branches ; max_proofs_verified ; typ ; value_to_field_elements ; var_to_field_elements } } : ( a , b , c , d ) d Side_loaded . t ) : ( a , b , c , d ) d t = let index = match ephemeral with | Some { index = ` In_circuit i } -> i | _ -> failwithf " For_step . side_loaded : Expected ` In_circuit ( % s ) s " __LOC__ ( ) in let T = Nat . eq_exn branches Side_loaded_verification_key . Max_branches . n in { branches ; max_proofs_verified ; proofs_verifieds = Vector . map index . step_widths ~ f : Side_loaded_verification_key . Width . Checked . to_field ; typ ; value_to_field_elements ; var_to_field_elements ; wrap_key = index . wrap_index ; wrap_domains = Common . wrap_domains ~ proofs_verified ( : Nat . to_int ( Nat . Add . n max_proofs_verified ) max_proofs_verified ) max_proofs_verified ; step_domains = ` Side_loaded index . step_domains ; max_width = Some index . max_width } let of_compiled ( { branches ; max_proofs_verified ; proofs_verifieds ; typ ; value_to_field_elements ; var_to_field_elements ; wrap_key ; wrap_domains ; step_domains } : _ Compiled . t ) = { branches ; max_width = None ; max_proofs_verified ; proofs_verifieds = Vector . map proofs_verifieds ~ f : Impls . Step . Field . of_int ; typ ; value_to_field_elements ; var_to_field_elements ; wrap_key = Plonk_verification_key_evals . map ( Lazy . force wrap_key ) wrap_key ~ f : Step_main_inputs . Inner_curve . constant ; wrap_domains ; step_domains = ` Known step_domains } end |
type t = { compiled : Compiled . packed Type_equal . Id . Uid . Table . t ; side_loaded : Side_loaded . packed Type_equal . Id . Uid . Table . t } |
let univ : t = { compiled = Type_equal . Id . Uid . Table . create ( ) ; side_loaded = Type_equal . Id . Uid . Table . create ( ) } |
let find t k = match Hashtbl . find t k with None -> failwith " key not found " | Some x -> x |
let lookup_compiled : type a b n m . ( a , b , n , m ) m Tag . tag -> ( a , b , n , m ) m Compiled . t = fun t -> let ( T ( other_id , d ) d ) d = find univ . compiled ( Type_equal . Id . uid t ) t in let T = Type_equal . Id . same_witness_exn t other_id in d |
let lookup_side_loaded : type a b n m . ( a , b , n , m ) m Tag . tag -> ( a , b , n , m ) m Side_loaded . t = fun t -> let ( T ( other_id , d ) d ) d = find univ . side_loaded ( Type_equal . Id . uid t ) t in let T = Type_equal . Id . same_witness_exn t other_id in d |
let lookup_basic : type a b n m . ( a , b , n , m ) m Tag . t -> ( a , b , n , m ) m Basic . t = fun t -> match t . kind with | Compiled -> Compiled . to_basic ( lookup_compiled t . id ) id | Side_loaded -> Side_loaded . to_basic ( lookup_side_loaded t . id ) id |
let lookup_step_domains : type a b n m . ( a , b , n , m ) m Tag . t -> ( Domain . t , m ) m Vector . t = fun t -> let f = Vector . map ~ f : Domains . h in match t . kind with | Compiled -> f ( lookup_compiled t . id ) id . step_domains | Side_loaded -> ( let t = lookup_side_loaded t . id in match t . ephemeral with | Some { index = ` In_circuit _ } | None -> failwith __LOC__ | Some { index = ` In_prover k } -> let a = At_most . to_array ( At_most . map k . step_data ~ f ( : fun ( ds , _ ) _ -> ds . h ) h ) h in Vector . init t . permanent . branches ~ f ( : fun i -> try a ( . i ) i with _ -> Domain . Pow_2_roots_of_unity 0 ) ) |
let max_proofs_verified : type n1 . ( _ , _ , n1 , _ ) _ Tag . t -> ( module Nat . Add . Intf with type n = n1 ) n1 = fun tag -> match tag . kind with | Compiled -> ( lookup_compiled tag . id ) id . max_proofs_verified | Side_loaded -> ( lookup_side_loaded tag . id ) id . permanent . max_proofs_verified |
let typ : type var value . ( var , value , _ , _ ) _ Tag . t -> ( var , value ) value Impls . Step . Typ . t = fun tag -> match tag . kind with | Compiled -> ( lookup_compiled tag . id ) id . typ | Side_loaded -> ( lookup_side_loaded tag . id ) id . permanent . typ |
let value_to_field_elements : type a . ( _ , a , _ , _ ) _ Tag . t -> a -> Tick . Field . t array = fun t -> match t . kind with | Compiled -> ( lookup_compiled t . id ) id . value_to_field_elements | Side_loaded -> ( lookup_side_loaded t . id ) id . permanent . value_to_field_elements |
let lookup_map ( type a b c d ) d ( t : ( a , b , c , d ) d Tag . t ) t ~ self ~ default ( ~ f : [ ` Compiled of ( a , b , c , d ) d Compiled . t | ` Side_loaded of ( a , b , c , d ) d Side_loaded . t ] -> _ ) = match Type_equal . Id . same_witness t . id self with | Some _ -> default | None -> ( match t . kind with | Compiled -> let ( T ( other_id , d ) d ) d = find univ . compiled ( Type_equal . Id . uid t . id ) id in let T = Type_equal . Id . same_witness_exn t . id other_id in f ( ` Compiled d ) d | Side_loaded -> let ( T ( other_id , d ) d ) d = find univ . side_loaded ( Type_equal . Id . uid t . id ) id in let T = Type_equal . Id . same_witness_exn t . id other_id in f ( ` Side_loaded d ) d ) |
let add_side_loaded ~ name permanent = let id = Type_equal . Id . create ~ name sexp_of_opaque in Hashtbl . add_exn univ . side_loaded ~ key ( : Type_equal . Id . uid id ) id ~ data ( : T ( id , { ephemeral = None ; permanent } ) ) ; { Tag . kind = Side_loaded ; id } |
let set_ephemeral { Tag . kind ; id } eph = ( match kind with Side_loaded -> ( ) | _ -> failwith " Expected Side_loaded ) " ; Hashtbl . update univ . side_loaded ( Type_equal . Id . uid id ) id ~ f ( : function | None -> assert false | Some ( T ( id , d ) d ) d -> T ( id , { d with ephemeral = Some eph } ) ) |
let add_exn ( type a b c d ) d ( tag : ( a , b , c , d ) d Tag . t ) t ( data : ( a , b , c , d ) d Compiled . t ) t = Hashtbl . add_exn univ . compiled ~ key ( : Type_equal . Id . uid tag . id ) id ~ data ( : Compiled . T ( tag . id , data ) data ) data |
let test ( ) = print_endline " Types tests " ; let open EH1 in let open Global in init ( ) ; let bool_t = Type . bool ( ) in let int_t = Type . int ( ) in assert ( not Type . ( equal bool_t int_t ) ) ; let real_t = Type . real ( ) in assert ( not Type . ( equal real_t bool_t ) ) ; assert ( not Type . ( equal real_t int_t ) ) ; let bv_t = Type . bv 8 in let scal_t = Type . new_scalar ~ card : 12 in let unint_t = Type . new_uninterpreted ( ) in let tup1_t = Type . tuple [ bool_t ] in let tup2_t = Type . tuple [ int_t ; real_t ] in let tup3_t = Type . tuple [ bv_t ; scal_t ; unint_t ] in let ta4 = [ bool_t ; tup1_t ; tup2_t ; tup3_t ] in let tup4_t = Type . tuple ta4 in let fun1_t = Type . func [ int_t ] bool_t in let _fun2_t = Type . func [ real_t ; bv_t ] scal_t in let fun3_t = Type . func [ tup1_t ; tup2_t ; tup3_t ] fun1_t in let fun4_t = Type . func ta4 fun3_t in assert ( Type . is_bool bool_t ) ; assert ( not ( Type . is_bool int_t ) ) ; assert ( Type . is_int int_t ) ; assert ( Type . is_real real_t ) ; assert ( Type . is_arithmetic real_t ) ; assert ( Type . is_bitvector bv_t ) ; assert ( Type . is_tuple tup1_t ) ; assert ( Type . is_function fun4_t ) ; assert ( Type . is_scalar scal_t ) ; assert ( Type . is_uninterpreted unint_t ) ; assert ( Type . test_subtype int_t real_t ) ; assert ( not ( Type . test_subtype real_t int_t ) ) ; assert ( Types . equal_ytype ( Type . reveal bv_t ) ( BV 8 ) ) ; assert ( Type . scalar_card scal_t = 12 ) ; assert ( Type . num_children ( tup3_t ) = 3 ) ; assert ( Type . child tup3_t 1 = scal_t ) ; let type_v = Type . children tup4_t in assert ( List . length type_v = 4 ) ; assert ( List . nth type_v 0 = bool_t ) ; assert ( List . nth type_v 1 = tup1_t ) ; assert ( List . nth type_v 2 = tup2_t ) ; assert ( List . nth type_v 3 = tup3_t ) ; print_endline " Done with Types tests " ; exit ( ) |
type error = | Unbound_type_var of string | Wrong_number_args of Path . t * int * int | Expected_type_var of type_expr | Constraints_not_satisfied of type_expr * type_decl | Opaque_type_in_prover_mode of type_expr | Convertible_arities_differ of string * int * string * int | GADT_in_nonrec_type | Repeated_row_label of Ident . t | Missing_row_label of Ident . t | Expected_row_type of type_expr |
let type0 { Typedast . type_type ; _ } = type_type |
let unify = ref ( fun ~ loc : _ _env _typ1 _typ2 -> failwith " Undefined " ) |
module Type = struct open Type let opaque = Ident . create ~ mode : Checked " opaque " let mk_poly ~ loc ~ mode vars typ env = { Typedast . type_desc = Ttyp_poly ( vars , typ ) ; type_loc = loc ; type_type = Mk . poly ~ mode ( List . map ~ f : type0 vars ) ( type0 typ ) env } let mk_tuple ~ loc ~ mode typs env = { Typedast . type_desc = Ttyp_tuple typs ; type_loc = loc ; type_type = Mk . tuple ~ mode ( List . map ~ f : type0 typs ) env } let mk_arrow ~ loc ~ mode ( ? explicit = Explicit ) ( ? label = Nolabel ) typ1 typ2 env = { Typedast . type_desc = Ttyp_arrow ( typ1 , typ2 , explicit , label ) ; type_loc = loc ; type_type = Mk . arrow ~ mode ~ explicit ~ label ( type0 typ1 ) ( type0 typ2 ) env } let mk_prover ~ loc ~ mode typ = { Typedast . type_desc = Ttyp_prover typ ; type_loc = loc ; type_type = Type1 . get_mode mode typ . type_type } let mk_conv ~ loc ~ mode typ1 typ2 env = { Typedast . type_desc = Ttyp_conv ( typ1 , typ2 ) ; type_loc = loc ; type_type = Envi . Type . Mk . conv ~ mode ( type0 typ1 ) ( type0 typ2 ) env } let mk_opaque ~ loc typ env = { Typedast . type_desc = Ttyp_opaque typ ; type_loc = loc ; type_type = Envi . Type . Mk . opaque ~ mode : Checked ( type0 typ ) env } let rec import ? must_find ( typ : type_expr ) env : Typedast . type_expr * env = let mode = Envi . current_mode env in let import ' = import in let import = import ? must_find in let loc = typ . type_loc in match typ . type_desc with | Ptyp_var None -> ( match must_find with | Some true -> raise ( Error ( loc , Unbound_type_var " _ " ) ) | _ -> ( { type_desc = Ttyp_var None ; type_loc = loc ; type_type = mkvar ~ mode None env } , env ) ) | Ptyp_var ( Some { txt = x ; _ } as name ) -> let var = match must_find with | Some true -> let var = find_type_variable ~ mode x env in if Option . is_none var then raise ( Error ( loc , Unbound_type_var x ) ) ; var | Some false -> None | None -> find_type_variable ~ mode x env in let type_type , env = match var with | Some var -> ( var , env ) | None -> let var = mkvar ~ mode ( Some x ) env in ( var , add_type_variable x var env ) in ( { type_desc = Ttyp_var name ; type_loc = loc ; type_type } , env ) | Ptyp_poly ( vars , typ ) -> let env = open_expr_scope env in let env , vars = List . fold_map vars ~ init : env ~ f ( : fun e t -> let t , e = import ' ~ must_find : false t e in ( e , t ) ) in let typ , env = import typ env in let env = close_expr_scope env in ( mk_poly ~ loc ~ mode vars typ env , env ) | Ptyp_ctor { var_ident = { txt = Lident " opaque " ; _ } as var_ident ; var_params } when not ( Envi . has_type_declaration ~ mode var_ident env ) -> ( match var_params with | [ typ ' ] -> import { typ with type_desc = Ptyp_opaque typ ' } env | _ -> raise ( Error ( loc , Wrong_number_args ( Path . Pident opaque , List . length var_params , 1 ) ) ) ) | Ptyp_ctor variant -> let { var_ident ; var_params } = variant in let var_ident , decl = raw_find_type_declaration ~ mode var_ident env in let var_ident = Location . mkloc var_ident variant . var_ident . loc in let given_args_length = List . length var_params in let expected_args_length = List . length decl . tdec_params in if not ( Int . equal given_args_length expected_args_length ) then raise ( Error ( loc , Wrong_number_args ( var_ident . txt , given_args_length , expected_args_length ) ) ) ; let env , var_params = List . fold_map ~ init : env var_params ~ f ( : fun env param -> let param , env = import param env in ( env , param ) ) in let typ = Envi . Type . instantiate decl . tdec_params ( List . map ~ f : type0 var_params ) ( Type1 . get_mode mode decl . tdec_ret ) env in ( { type_desc = Ttyp_ctor { var_params ; var_ident } ; type_loc = loc ; type_type = typ } , env ) | Ptyp_tuple typs -> let env , typs = List . fold_map typs ~ init : env ~ f ( : fun e t -> let t , e = import t e in ( e , t ) ) in ( mk_tuple ~ loc ~ mode typs env , env ) | Ptyp_arrow ( typ1 , typ2 , explicit , label ) -> let typ1 , env = import typ1 env in let typ2 , env = import typ2 env in ( mk_arrow ~ loc ~ mode ~ explicit ~ label typ1 typ2 env , env ) | Ptyp_prover typ -> let env = open_expr_scope ~ mode : Prover env in let typ , env = import typ env in let env = let scope , env = pop_expr_scope env in join_expr_scope env scope in ( mk_prover ~ loc ~ mode typ , env ) | Ptyp_conv ( typ1 , typ2 ) -> let env = open_expr_scope ~ mode : Checked env in let typ1 , env = import typ1 env in let env = let scope , env = pop_expr_scope env in join_expr_scope env scope in let env = open_expr_scope ~ mode : Prover env in let typ2 , env = import typ2 env in let env = let scope , env = pop_expr_scope env in join_expr_scope env scope in ( mk_conv ~ loc ~ mode typ1 typ2 env , env ) | Ptyp_opaque typ ' -> if equal_mode Prover mode then raise ( Error ( loc , Opaque_type_in_prover_mode typ ) ) ; let env = open_expr_scope ~ mode : Prover env in let typ , env = import typ ' env in let env = let scope , env = pop_expr_scope env in join_expr_scope env scope in ( mk_opaque ~ loc typ env , env ) | Ptyp_alias ( typ , name ) -> let var = match must_find with | Some true -> let var = find_type_variable ~ mode name . txt env in if Option . is_none var then raise ( Error ( loc , Unbound_type_var name . txt ) ) ; var | Some false -> None | None -> find_type_variable ~ mode name . txt env in let var , env = match var with | Some var -> ( var , env ) | None -> let var = mkvar ~ mode ( Some name . txt ) env in ( var , add_type_variable name . txt var env ) in let typ , env = import typ env in ! unify ~ loc env typ . type_type var ; ( { Typedast . type_desc = Ttyp_alias ( typ , name ) ; type_loc = loc ; type_type = typ . type_type } , env ) | Ptyp_row ( tags , row_closed , min_tags ) -> let pres = match ( row_closed , min_tags ) with | Closed , Some _ -> Type0 . RpMaybe | Open , _ | Closed , None -> Type0 . RpPresent in let ( env , row_tags ) , tags = List . fold_map ~ init ( : env , Ident . Map . empty ) tags ~ f ( : fun ( env , row_tags ) { rtag_ident ; rtag_arg ; rtag_loc } -> let env , rtag_arg = List . fold_map rtag_arg ~ init : env ~ f ( : fun e t -> let t , e = import t e in ( e , t ) ) in let rtag_ident = map_loc ~ f : Ident . create_row rtag_ident in let args = List . map ~ f : type0 rtag_arg in let row_tags = match Map . add row_tags ~ key : rtag_ident . txt ~ data ( : Path . Pident rtag_ident . txt , Type1 . mk_rp pres , args ) with | ` Duplicate -> raise ( Error ( rtag_ident . loc , Repeated_row_label rtag_ident . txt ) ) | ` Ok row_tags -> row_tags in ( ( env , row_tags ) , { Typedast . rtag_ident ; rtag_arg ; rtag_loc } ) ) in let min_tags = Option . map ~ f ( : List . map ~ f ( : map_loc ~ f : Ident . create_row ) ) min_tags in let set_present tag = match Map . find row_tags tag . Location . txt with | None -> raise ( Error ( tag . loc , Missing_row_label tag . txt ) ) | Some ( _path , pres , _args ) -> pres . Type0 . rp_desc <- RpPresent in Option . iter ~ f ( : List . iter ~ f : set_present ) min_tags ; let row_rest = Envi . Type . mkvar ~ mode None env in let type_type = Envi . Type . Mk . row ~ mode { row_tags ; row_closed ; row_rest ; row_presence_proxy = Type1 . mk_rp RpPresent } env in ( { Typedast . type_desc = Ttyp_row ( tags , row_closed , min_tags ) ; type_loc = loc ; type_type } , env ) | Ptyp_row_subtract ( typ , tags ) -> let typ ' , env = import typ env in let tags = List . map ~ f ( : map_loc ~ f : Ident . create_row ) tags in let type_type = let typ ' = Type1 . repr typ ' . type_type in let ( row_tags , row_rest , row_closed ) , row_presence_proxy = match typ ' . type_desc with | Tvar name -> let row_tags = Ident . Map . empty in let row_closed = Open in let row_rest = Type1 . Mk . var ~ mode : typ ' . type_mode typ ' . type_depth name in let row_presence_proxy = Type1 . mk_rp RpPresent in let row = { Type0 . row_tags ; row_closed ; row_rest ; row_presence_proxy } in let instance_typ = Type1 . Mk . row ~ mode : typ ' . type_mode typ ' . type_depth row in let instance_typ = if phys_equal typ ' typ ' . type_alternate . type_alternate then instance_typ . type_alternate . type_alternate else instance_typ in Type1 . add_instance ~ unify ( :! unify ~ loc env ) typ ' instance_typ ; ( ( row_tags , row_rest , row_closed ) , row_presence_proxy ) | Trow row -> ( Type1 . row_repr row , row . row_presence_proxy ) | _ -> raise ( Error ( typ . type_loc , Expected_row_type typ ) ) in match row_closed with | Open -> let row_tags = List . fold ~ init : row_tags tags ~ f ( : fun row_tags { txt = tag ; _ } -> match Map . find row_tags tag with | Some ( path , pres , args ) -> Map . set row_tags ~ key : tag ~ data ( : path , Type1 . mk_rp ( RpSubtract pres ) , args ) | None -> Map . set row_tags ~ key : tag ~ data : ( Path . Pident tag , Type1 . mk_rp ( RpSubtract ( Type1 . mk_rp RpAny ) ) , [ ] ) ) in Envi . Type . Mk . row ~ mode { row_tags ; row_closed ; row_rest ; row_presence_proxy } env | Closed -> let row_tags = List . fold ~ init : row_tags tags ~ f ( : fun row_tags { txt = tag ; loc } -> match Map . find row_tags tag with | Some ( path , pres , args ) -> Map . set row_tags ~ key : tag ~ data ( : path , Type1 . mk_rp ( RpSubtract pres ) , args ) | None -> raise ( Error ( loc , Missing_row_label tag ) ) ) in Envi . Type . Mk . row ~ mode { row_tags ; row_closed ; row_rest ; row_presence_proxy } env in ( { Typedast . type_desc = Ttyp_row_subtract ( typ ' , tags ) ; type_loc = loc ; type_type } , env ) let fold ~ init ~ f typ = match typ . type_desc with | Ptyp_var _ -> init | Ptyp_tuple typs -> List . fold ~ init ~ f typs | Ptyp_arrow ( typ1 , typ2 , _ , _ ) -> let acc = f init typ1 in f acc typ2 | Ptyp_ctor variant -> List . fold ~ init ~ f variant . var_params | Ptyp_poly ( typs , typ ) -> let acc = List . fold ~ init ~ f typs in f acc typ | Ptyp_prover typ -> f init typ | Ptyp_conv ( typ1 , typ2 ) -> let acc = f init typ1 in f acc typ2 | Ptyp_opaque typ -> f init typ | Ptyp_alias ( typ , _ ) -> f init typ | Ptyp_row ( tags , _closed , _min_tags ) -> List . fold ~ init tags ~ f ( : fun acc { rtag_arg ; _ } -> List . fold ~ f ~ init : acc rtag_arg ) | Ptyp_row_subtract ( typ , _tags ) -> f init typ let iter ~ f = fold ~ init ( ) : ~ f ( : fun ( ) -> f ) let map ~ loc ~ f typ = match typ . type_desc with | Ptyp_var _ -> { typ with type_loc = loc } | Ptyp_tuple typs -> let typs = List . map ~ f typs in { type_desc = Ptyp_tuple typs ; type_loc = loc } | Ptyp_arrow ( typ1 , typ2 , explicit , label ) -> { type_desc = Ptyp_arrow ( f typ1 , f typ2 , explicit , label ) ; type_loc = loc } | Ptyp_ctor variant -> let variant = { variant with var_params = List . map ~ f variant . var_params } in { type_desc = Ptyp_ctor variant ; type_loc = loc } | Ptyp_poly ( typs , typ ) -> let typs = List . map ~ f typs in { type_desc = Ptyp_poly ( typs , f typ ) ; type_loc = loc } | Ptyp_prover typ -> { type_desc = Ptyp_prover ( f typ ) ; type_loc = loc } | Ptyp_conv ( typ1 , typ2 ) -> { type_desc = Ptyp_conv ( f typ1 , f typ2 ) ; type_loc = loc } | Ptyp_opaque typ -> { type_desc = Ptyp_opaque ( f typ ) ; type_loc = loc } | Ptyp_alias ( typ , name ) -> { type_desc = Ptyp_alias ( f typ , name ) ; type_loc = loc } | Ptyp_row ( tags , closed , min_tags ) -> { type_desc = Ptyp_row ( List . map tags ~ f ( : fun tag -> { tag with rtag_arg = List . map ~ f tag . rtag_arg } ) , closed , min_tags ) ; type_loc = loc } | Ptyp_row_subtract ( typ , tags ) -> { type_desc = Ptyp_row_subtract ( f typ , tags ) ; type_loc = loc } end |
module TypeDecl = struct open TypeDecl let generalise decl = let poly_name = map_loc decl . tdec_ident ~ f ( : fun name -> if name = " t " then " poly " else name ^ " _poly " ) in match decl . tdec_desc with | Pdec_record fields -> let field_vars = List . map fields ~ f ( : fun { fld_ident ; fld_type = _ ; fld_loc } -> { type_desc = Ptyp_var ( Some fld_ident ) ; type_loc = fld_loc } ) in let poly_decl = { tdec_ident = poly_name ; tdec_params = field_vars ; tdec_desc = Pdec_record ( List . map2_exn fields field_vars ~ f ( : fun fld fld_type -> { fld with fld_type } ) ) ; tdec_loc = decl . tdec_loc } in let alias_typ = { type_desc = Ptyp_ctor { var_ident = map_loc poly_name ~ f ( : fun name -> Longident . Lident name ) ; var_params = List . map fields ~ f ( : fun { fld_type ; _ } -> fld_type ) } ; type_loc = decl . tdec_loc } in ( poly_decl , { decl with tdec_desc = Pdec_alias alias_typ } ) | Pdec_variant _ -> assert false | _ -> assert false let predeclare env { Parsetypes . tdec_ident ; tdec_params ; tdec_desc ; tdec_loc = loc } = match tdec_desc with | Pdec_extend _ -> env | _ -> let mode = Envi . current_mode env in let ident = Ident . create ~ mode tdec_ident . txt in let params = List . map tdec_params ~ f ( : fun _ -> Envi . Type . mkvar ~ mode None env ) in let decl = { Type0 . tdec_params = params ; tdec_desc = TAbstract ; tdec_id = next_id ( ) ; tdec_ret = Envi . Type . Mk . ctor ~ mode ( Path . Pident ident ) params env } in Envi . TypeDecl . predeclare ident decl env ; map_current_scope ~ f ( : Scope . add_type_declaration ~ loc ident decl ) env let import_field ? must_find env { fld_ident ; fld_type ; fld_loc } = let mode = Envi . current_mode env in let fld_type , env = Type . import ? must_find fld_type env in let fld_ident = map_loc ~ f ( : Ident . create ~ mode ) fld_ident in ( env , { Typedast . fld_ident ; fld_type ; fld_loc ; fld_fld = { Type0 . fld_ident = fld_ident . txt ; fld_type = fld_type . type_type } } ) let import_ctor env ctor = let mode = current_mode env in let scope , env = pop_expr_scope env in let ctor_ret , env , must_find = match ctor . ctor_ret with | Some ret -> let env = open_expr_scope env in let ret , env = Type . import ~ must_find : false ret env in ( Some ret , env , None ) | None -> ( None , push_scope scope env , Some true ) in let env , ctor_args = match ctor . ctor_args with | Ctor_tuple args -> let env , args = List . fold_map ~ init : env args ~ f ( : fun env arg -> let arg , env = Type . import ? must_find arg env in ( env , arg ) ) in ( env , Typedast . Tctor_tuple args ) | Ctor_record fields -> let env , fields = List . fold_map ~ init : env fields ~ f ( : import_field ? must_find ) in ( env , Typedast . Tctor_record fields ) in let ctor_ident = map_loc ~ f ( : Ident . create ~ mode ) ctor . ctor_ident in let env , type0_ctor_args = match ctor_args with | Tctor_tuple args -> ( env , Type0 . Ctor_tuple ( List . map ~ f : type0 args ) ) | Tctor_record fields -> let params = List . fold ~ init : Typeset . empty fields ~ f ( : fun set { fld_fld = { fld_type ; _ } ; _ } -> Set . union set ( Type1 . type_vars fld_type ) ) |> Set . to_list in let decl = mk ~ name : ctor_ident . txt ~ params ( TRecord ( List . map ~ f ( : fun { fld_fld = f ; _ } -> f ) fields ) ) in let scope , env = Envi . pop_scope env in let env = map_current_scope ~ f : ( Scope . add_type_declaration ~ loc : ctor . ctor_loc ~ may_shadow : true ctor_ident . txt decl ) env in let env = Envi . push_scope scope env in ( env , Type0 . Ctor_record decl ) in let env = push_scope scope ( close_expr_scope env ) in ( env , { Typedast . ctor_ident ; ctor_args ; ctor_ret ; ctor_loc = ctor . ctor_loc ; ctor_ctor = { Type0 . ctor_ident = ctor_ident . txt ; ctor_args = type0_ctor_args ; ctor_ret = Option . map ~ f : type0 ctor_ret } } ) let import ? name ? other_name ? tri_stitched ( ? newtype = false ) ~ recursive decl ' env = let mode = Envi . current_mode env in let { tdec_ident ; tdec_params ; tdec_desc ; tdec_loc } = decl ' in let tdec_ident , path , tdec_id = match IdTbl . find_name ~ modes ( : modes_of_mode mode ) tdec_ident . txt env . resolve_env . type_env . predeclared_types with | Some ( ident , id ) -> ( map_loc ~ f ( : fun _ -> ident ) tdec_ident , Path . Pident ident , id ) | None -> ( match tdec_desc with | Pdec_extend ( path , _ ) -> let tdec_ident , _ = Envi . raw_get_type_declaration ~ loc : path . loc path . txt env in ( map_loc ~ f ( : fun _ -> tdec_ident ) path , path . txt , next_id ( ) ) | _ -> let ident = match name with | Some name -> map_loc ~ f ( : fun _ -> name ) tdec_ident | None -> map_loc ~ f ( : Ident . create ~ mode ) tdec_ident in ( ident , Path . Pident ident . txt , next_id ( ) ) ) in let env = open_expr_scope env in let import_params env = List . fold_map ~ init : env ~ f ( : fun env param -> match param . type_desc with | Ptyp_var _ -> let var , env = Type . import ~ must_find : false param env in ( env , var ) | _ -> raise ( Error ( param . type_loc , Expected_type_var param ) ) ) in let env , tdec_params = import_params env tdec_params in let params = List . map ~ f : type0 tdec_params in let tdec_ret = match ( other_name , tri_stitched ) with | Some _ , Some _ -> assert false | Some other_path , None -> Type1 . Mk . ctor ~ mode 10000 path ~ other_path params | None , Some tri_typ -> assert ( equal_mode mode Checked ) ; let tri_typ = tri_typ env params in assert ( equal_mode tri_typ . Type0 . type_mode Prover ) ; let typ = Type1 . mk ' ~ mode : Checked 10000 ( Tctor { var_ident = path ; var_params = params } ) in typ . type_alternate <- tri_typ ; typ | None , None -> ( let opaque = let ctor = Type1 . Mk . ctor ~ mode 10000 path params in Type1 . Mk . opaque ~ mode 10000 ( Type1 . get_mode Prover ctor ) in match mode with | Prover -> opaque | Checked -> let typ = Type1 . mk ' ~ mode : Checked 10000 ( Tctor { var_ident = path ; var_params = params } ) in typ . type_alternate <- Type1 . get_mode Prover opaque ; typ ) in let decl = Type0 . { tdec_params = params ; tdec_desc = TAbstract ; tdec_id ; tdec_ret } in let typedast_decl = { Typedast . tdec_ident ; tdec_params ; tdec_desc = Tdec_abstract ; tdec_loc ; tdec_tdec = decl } in let decl , env = match tdec_desc with | Pdec_abstract -> ( typedast_decl , env ) | Pdec_alias typ -> let typ , env = Type . import ~ must_find : true typ env in let decl = { decl with tdec_desc = TAlias typ . type_type } in let typedast_decl = { typedast_decl with tdec_desc = Tdec_alias typ ; tdec_tdec = decl } in ( typedast_decl , env ) | Pdec_open -> let decl = { decl with tdec_desc = TOpen } in let typedast_decl = { typedast_decl with tdec_desc = Tdec_open ; tdec_tdec = decl } in ( typedast_decl , env ) | Pdec_record fields -> let env , fields = List . fold_map ~ init : env fields ~ f ( : import_field ~ must_find : true ) in let decl = { decl with tdec_desc = TRecord ( List . map ~ f ( : fun { fld_fld = f ; _ } -> f ) fields ) } in let typedast_decl = { typedast_decl with tdec_desc = Tdec_record fields ; tdec_tdec = decl } in ( typedast_decl , env ) | Pdec_variant ctors | Pdec_extend ( _ , ctors ) -> let name = match tdec_desc with | Pdec_variant _ -> Path . Pident tdec_ident . txt | Pdec_extend ( lid , _ ) -> lid . txt | _ -> failwith " Could not find name for TVariant / TExtend . " in let env , ctors = List . fold_map ~ init : env ctors ~ f ( : fun env ctor -> let ret = ctor . ctor_ret in if ( not recursive ) && Option . is_some ctor . ctor_ret then raise ( Error ( ctor . ctor_loc , GADT_in_nonrec_type ) ) ; let env , ctor = import_ctor env ctor in ( match ( ctor . ctor_ret , ret ) with | Some { type_desc = Ttyp_ctor { var_ident = path ; _ } ; _ } , _ when Path . compare path . txt name = 0 -> ( ) | Some _ , Some ret -> raise ( Error ( ret . type_loc , Constraints_not_satisfied ( ret , decl ' ) ) ) | Some _ , None -> assert false | _ -> ( ) ) ; ( env , ctor ) ) in let typedast_tdec_desc , tdec_desc = match tdec_desc with | Pdec_variant _ -> ( Typedast . Tdec_variant ctors , Type0 . TVariant ( List . map ~ f ( : fun { ctor_ctor = c ; _ } -> c ) ctors ) ) | Pdec_extend ( id , _ ) -> ( Typedast . Tdec_extend ( id , ctors ) , Type0 . TExtend ( id . txt , List . map ~ f ( : fun { ctor_ctor = c ; _ } -> c ) ctors ) ) | _ -> failwith " Expected a TVariant or a TExtend " in let decl = { decl with tdec_desc } in let typedast_decl = { typedast_decl with tdec_desc = typedast_tdec_desc ; tdec_tdec = decl } in ( typedast_decl , env ) in let env = close_expr_scope env in let env = map_current_scope ~ f : ( Scope . register_type_declaration ~ may_shadow : newtype ~ loc : tdec_ident . loc tdec_ident . txt decl . tdec_tdec ) env in ( decl , env ) let import_convertible decl type_conv env = let mode = current_mode env in assert ( equal_mode mode Checked ) ; match type_conv with | Ptconv_with ( other_mode , conv_decl ) -> ( let decl_len = List . length decl . tdec_params in let conv_len = List . length conv_decl . tdec_params in if not ( Int . equal decl_len conv_len ) then raise ( Error ( conv_decl . tdec_loc , Convertible_arities_differ ( decl . tdec_ident . txt , decl_len , conv_decl . tdec_ident . txt , conv_len ) ) ) ; let name = match other_mode with | Prover when decl . tdec_ident . txt = conv_decl . tdec_ident . txt -> let name = Ident . create ~ mode ~ ocaml : true decl . tdec_ident . txt in let name ' = decl . tdec_ident . txt in Option . iter ( Ident . ocaml_name_ref name ) ~ f ( : fun name -> name := if name ' = " t " then " var " else name ' ^ " _var " ) ; name | _ -> Ident . create ~ mode decl . tdec_ident . txt in let other_name = Ident . create ~ mode : other_mode conv_decl . tdec_ident . txt in let decl , env = import ~ name ~ other_name ( : Path . Pident other_name ) ~ recursive : false decl env in match other_mode with | Checked -> let tri_stitched env params = Envi . Type . instantiate decl . tdec_tdec . tdec_params params ( Type1 . get_mode Prover decl . tdec_tdec . tdec_ret ) env in let conv_decl , env = import ~ name : other_name ~ tri_stitched ~ recursive : false conv_decl env in ( decl , Typedast . Ttconv_with ( other_mode , conv_decl ) , env ) | Prover -> let env = Envi . open_mode_module_scope other_mode env in let conv_decl , env = import ~ name : other_name ~ other_name ( : Path . Pident name ) ~ recursive : false conv_decl env in let env = Envi . open_mode_module_scope mode env in ( decl , Typedast . Ttconv_with ( other_mode , conv_decl ) , env ) ) | Ptconv_to typ -> let typ ' = ref None in let tri_stitched env _params = let env = Envi . open_expr_scope ~ mode : Prover env in let typ , _env = Type . import ~ must_find : true typ env in typ ' := Some typ ; typ . type_type in let decl , env = import ~ tri_stitched ~ recursive : false decl env in let typ = Option . value_exn ! typ ' in ( decl , Ttconv_to typ , env ) let import_rec decls env = let env = List . fold ~ f : predeclare ~ init : env decls in let env , decls = List . fold_map ~ init : env decls ~ f ( : fun env decl -> let decl , env = import ~ recursive : true decl env in ( env , decl ) ) in Envi . TypeDecl . clear_predeclared env ; ( decls , env ) let import ? name ? other_name ? tri_stitched = import ? name ? other_name ? tri_stitched ~ recursive : false end |
let pp_decl_typ ppf decl = pp_typ ppf { type_desc = Ptyp_ctor { var_ident = mk_lid decl . tdec_ident ; var_params = decl . tdec_params } ; type_loc = Location . none } |
let report_error ppf = function | Unbound_type_var name -> let quot = match name with " _ " -> " " | _ -> " ' " in fprintf ppf " [ @< hov > Unbound type parameter @ % s % s . ] " @ quot name | Wrong_number_args ( path , given , expected ) -> fprintf ppf " [ @ The type constructor [ @< h >% a ] @ expects % d argument ( s ) @ but is here \ applied to % d argument ( s ) . ] " @ Path . pp path expected given | Expected_type_var typ -> fprintf ppf " [ @< hov > Syntax error : Expected a type parameter , but got [ @< h >% a ] . ] " @@ pp_typ typ | Constraints_not_satisfied ( typ , decl ) -> fprintf ppf " [ @< hov > Constraints are not satisfied in this type . @ Type [ @< h >% a ] @ \ should be an instance of [ @< h >% a ] . ] " @@ pp_typ typ pp_decl_typ decl | Opaque_type_in_prover_mode typ -> fprintf ppf " [ @< hov > The type [ @< h >% a ] @ is not valid in this mode :@ opaque types \ cannot be created in Prover mode . ] " @ pp_typ typ | Convertible_arities_differ ( name , len , conv_name , conv_len ) -> fprintf ppf " [ @< hov > Cannot associate type % s of arity % i @ with type % s of arity \ % i :@ their arities must be equal . ] " @ name len conv_name conv_len | GADT_in_nonrec_type -> fprintf ppf " [ @< hov > GADT case syntax cannot be used in a non - recursive type . @ To \ use this syntax , use ' type rec ' for this type definition . ] . " @@ | Repeated_row_label label -> fprintf ppf " [ @< hov > The constructor % a has already appeared in this row . ] . " @@ Ident . pprint label | Missing_row_label label -> fprintf ppf " [ @< hov > The constructor % a is not present in this row . ] . " @@ Ident . pprint label | Expected_row_type typ -> fprintf ppf " [ @< hov > The type % a was expected to be a row . ] . " @@ pp_typ typ |
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( Location . error_of_printer ~ loc report_error err ) | _ -> None ) |
let empty_head variable = { Type . Callable . head = [ ] ; variable } |
let ( ! ) concretes = List . map concretes ~ f ( : fun single -> Type . Parameter . Single single ) |
let make_callable_from_arguments annotations = Type . Callable . Defined ( List . mapi ~ f ( : fun index annotation -> Type . Callable . RecordParameter . PositionalOnly { index ; annotation ; default = false } ) annotations ) |
let assert_create ( ? aliases = fun _ -> None ) source annotation = assert_equal ~ printer : Type . show ~ cmp : Type . equal annotation ( Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) ) |
let test_create _ = assert_create " foo " ( Type . Primitive " foo " ) ; assert_create " foo . bar " ( Type . Primitive " foo . bar " ) ; assert_create " object " ( Type . Primitive " object " ) ; assert_create " foo [ bar ] " ( Type . parametric " foo " [ ! Type . Primitive " bar " ] ) ; assert_create " foo [ bar , baz ] " ( Type . parametric " foo " [ ! Type . Primitive " bar " ; Type . Primitive " baz " ] ) ; assert_create " typing . List . __getitem__ ( int ) " ( Type . list Type . integer ) ; assert_create " typing . Dict . __getitem__ ( ( int , str ) ) " ( Type . dictionary ~ key : Type . integer ~ value : Type . string ) ; assert_create " typing . Counter " ( Type . Primitive " collections . Counter " ) ; assert_create " typing . Counter [ int ] " ( Type . parametric " collections . Counter " [ ! Type . integer ] ) ; assert_create " typing . ChainMap " ( Type . Primitive " collections . ChainMap " ) ; assert_create " typing . ChainMap [ int ] " ( Type . parametric " collections . ChainMap " [ ! Type . integer ] ) ; assert_create " typing . Deque " ( Type . Primitive " collections . deque " ) ; assert_create " typing . Deque [ int ] " ( Type . parametric " collections . deque " [ ! Type . integer ] ) ; assert_create " typing_extensions . Protocol [ int ] " ( Type . parametric " typing . Protocol " [ ! Type . integer ] ) ; assert_create " typing_extensions . Protocol " ( Type . Primitive " typing . Protocol " ) ; assert_create " typing . List [ int ] " ( Type . list Type . integer ) ; assert_create " typing . List " ( Primitive " list " ) ; assert_create " typing . Set " ( Primitive " set " ) ; assert_create " typing . DefaultDict [ int , str ] " ( Type . parametric " collections . defaultdict " [ ! Type . integer ; Type . string ] ) ; assert_create " typing . Dict [ int , str ] " ( Type . dictionary ~ key : Type . integer ~ value : Type . string ) ; assert_create " typing . Tuple [ int , str ] " ( Type . tuple [ Type . integer ; Type . string ] ) ; assert_create " typing . Tuple [ int , . . . ] " ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ; assert_create " typing . Tuple [ ( ) ] " ( Type . tuple [ ] ) ; assert_create " tuple " ( Type . Primitive " tuple " ) ; assert_create " typing . Any " Type . Any ; assert_create " typing . Optional [ int ] " ( Type . optional Type . integer ) ; assert_create " typing . Optional . __getitem__ ( int ) " ( Type . optional Type . integer ) ; assert_create " typing . Set [ int ] " ( Type . set Type . integer ) ; assert_create " typing . Union [ int , str ] " ( Type . union [ Type . integer ; Type . string ] ) ; assert_create " typing . Union [ int , typing . Any ] " ( Type . union [ Type . integer ; Type . Any ] ) ; assert_create " typing . Union [ int , None ] " ( Type . optional Type . integer ) ; assert_create " typing . Union [ int , None , str , typing . Tuple [ int , str ] ] " ( Type . union [ Type . NoneType ; Type . integer ; Type . string ; Type . tuple [ Type . integer ; Type . string ] ] ) ; assert_create " typing . Union [ typing . Optional [ int ] , typing . Optional [ str ] ] " ( Type . union [ Type . NoneType ; Type . integer ; Type . string ] ) ; assert_create " typing . Union [ typing . Optional [ int ] , str ] " ( Type . union [ Type . NoneType ; Type . integer ; Type . string ] ) ; assert_create " typing . Annotated [ int ] " ( Type . annotated Type . integer ) ; assert_create " typing . Annotated [ int , Derp ( ) ] " ( Type . annotated Type . integer ) ; assert_create " typing_extensions . Annotated [ int ] " ( Type . annotated Type . integer ) ; assert_create " typing_extensions . Annotated [ int , Derp ( ) ] " ( Type . annotated Type . integer ) ; assert_create " typing . Set [ typing . Any ] " ( Type . set Type . Any ) ; assert_create " typing . Dict [ str , typing . Any ] " ( Type . dictionary ~ key : Type . string ~ value : Type . Any ) ; assert_create " typing . TypeVar ( ' _T ' ) " ( Type . variable " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , covariant = True ) " ( Type . variable ~ variance : Covariant " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , covariant = False ) " ( Type . variable " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , contravariant = True ) " ( Type . variable ~ variance : Contravariant " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , contravariant = False ) " ( Type . variable " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , int ) " ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . integer ] ) " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , name = int ) " ( Type . variable " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , bound = int ) " ( Type . variable ~ constraints ( : Type . Variable . Bound Type . integer ) " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , bound ' = C ' ) " ( Type . variable ~ constraints ( : Type . Variable . Bound ( Type . Primitive " C " ) ) " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , ' C ' , X ) " ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . Primitive " C " ; Type . Primitive " X " ] ) " _T " ) ; assert_create " typing . TypeVar ( ' _T ' , int , name = float ) " ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . integer ] ) " _T " ) ; assert_create " typing . TypeVar ( ' _CallableT ' , bound ' = typing . Callable ' ) " ( Type . variable ~ constraints ( : Type . Variable . Bound ( Type . Primitive " typing . Callable " ) ) " _CallableT " ) ; assert_create " ' foo ' " ( Type . Primitive " foo " ) ; assert_create " ' foo . bar ' " ( Type . Primitive " foo . bar " ) ; assert_create " foo [ ' bar ' ] " ( Type . parametric " foo " [ ! Type . Primitive " bar " ] ) ; assert_create " ' Type [ str ] ' " ( Type . parametric " Type " [ ! Type . Primitive " str " ] ) ; assert_create " ' Type [ [ [ ] str ] ' " ( Type . Primitive " Type [ [ [ ] str ] " ) ; assert_create " typing_extensions . Literal [ ' foo ' ] " ( Type . literal_string " foo " ) ; assert_create " typing_extensions . Literal [ u ' foo ' ] " ( Type . literal_string " foo " ) ; assert_create " typing_extensions . Literal [ b ' foo ' ] " ( Type . literal_bytes " foo " ) ; assert_create " typing_extensions . Literal [ u ' foo ' , b ' foo ' ] " ( Type . union [ Type . literal_string " foo " ; Type . literal_bytes " foo " ] ) ; assert_create " typing_extensions . Literal [ Foo . ONE ] " ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " Foo " ; member_name = " ONE " } ) ) ; assert_create " typing_extensions . Literal [ Foo . ONE , Foo . TWO ] " ( Type . union [ Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " Foo " ; member_name = " ONE " } ) ; Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " Foo " ; member_name = " TWO " } ) ; ] ) ; assert_create " typing_extensions . Literal [ ONE ] " Type . Top ; assert_create " typing_extensions . Literal [ None ] " Type . none ; assert_create " typing_extensions . Literal [ str ] " ( Type . Literal ( Type . String AnyLiteral ) ) ; assert_create " _NotImplementedType " Type . Any ; assert_create " ParamSpecClass [ [ int , str ] , [ bool ] ] " ( Type . parametric " ParamSpecClass " [ CallableParameters ( Defined [ PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; PositionalOnly { index = 1 ; annotation = Type . string ; default = false } ; ] ) ; CallableParameters ( Defined [ PositionalOnly { index = 0 ; annotation = Type . bool ; default = false } ] ) ; ] ) ; ( ) |
let test_create_callable _ = let default_overload = { Type . Callable . annotation = Type . Top ; parameters = Undefined } in let open Type . Callable in assert_create " typing . Callable " ( Type . Primitive " typing . Callable " ) ; assert_create " typing . Callable [ . . . , int ] " ( Type . Callable . create ~ annotation : Type . integer ( ) ) ; assert_create " typing . Callable . __getitem__ ( ( . . . , int ) ) " ( Type . Callable . create ~ annotation : Type . integer ( ) ) ; assert_create " typing . Callable [ ( . . . , int ) ] . __getitem__ ( __getitem__ ( ( . . . , str ) ) [ ( . . . , int ) ] ) " ( Type . Callable . create ~ overloads : [ { Type . Callable . annotation = Type . string ; parameters = Type . Callable . Undefined } ; { Type . Callable . annotation = Type . integer ; parameters = Type . Callable . Undefined } ; ] ~ annotation : Type . integer ( ) ) ; assert_create " typing . Callable [ . . . , int ] [ [ . . . , str ] ] " ( Type . Callable . create ~ overloads [ { : default_overload with annotation = Type . string } ] ~ annotation : Type . integer ( ) ) ; assert_create " typing . Callable ( ' name ' ) [ . . . , int ] " ( Type . Callable { kind = Type . Callable . Named " !& name " ; implementation = { default_overload with annotation = Type . integer } ; overloads = [ ] ; } ) ; assert_create " typing . Other ( ' name ' ) [ . . . , int ] " Type . Top ; assert_create " typing . Callable [ [ int , str ] , int ] " ( Type . Callable { kind = Type . Callable . Anonymous ; implementation = { annotation = Type . integer ; parameters = Defined [ Parameter . PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; Parameter . PositionalOnly { index = 1 ; annotation = Type . string ; default = false } ; ] ; } ; overloads = [ ] ; } ) ; assert_create " typing . Callable [ [ int , Named ( a , int ) , Variable ( ) , Keywords ( ) ] , int ] " ( Type . Callable { kind = Anonymous ; implementation = { annotation = Type . integer ; parameters = Defined [ Parameter . PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; Parameter . Named { name = " a " ; annotation = Type . integer ; default = false } ; Parameter . Variable ( Concrete Type . Top ) ; Parameter . Keywords Type . Top ; ] ; } ; overloads = [ ] ; } ) ; assert_create " typing . Callable [ [ int , Variable ( int ) , Keywords ( str ) ] , int ] " ( Type . Callable { kind = Anonymous ; implementation = { annotation = Type . integer ; parameters = Defined [ Parameter . PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; Parameter . Variable ( Concrete Type . integer ) ; Parameter . Keywords Type . string ; ] ; } ; overloads = [ ] ; } ) ; assert_create " typing . Callable [ [ Named ( a , int , default ) ] , int ] " ( Type . Callable { kind = Anonymous ; implementation = { annotation = Type . integer ; parameters = Defined [ Parameter . Named { name = " a " ; annotation = Type . integer ; default = true } ] ; } ; overloads = [ ] ; } ) ; assert_create " typing . Callable [ int ] " ( Type . Callable . create ~ annotation : Type . Top ( ) ) ; assert_create " typing . Callable [ int , str ] " ( Type . Callable . create ~ annotation : Type . Top ( ) ) ; assert_create " function " ( Type . Callable . create ~ annotation : Type . Any ( ) ) ; assert_create " typing . Callable [ . . . , function ] " ( Type . Callable . create ~ annotation ( : Type . Callable . create ~ annotation : Type . Any ( ) ) ( ) ) ; ( ) |
let test_create_alias _ = let assert_alias source resolved = let aliases primitive = Identifier . Table . of_alist_exn [ " Alias " , Type . Primitive " Aliased " ; " IntList " , Type . list Type . integer ; ( " _Future " , Type . union [ Type . parametric " Future " [ ! Type . integer ; Type . variable " _T " ] ; Type . awaitable ( Type . variable " _T " ) ; ] ) ; " baz . Callable " , Type . Callable . create ~ annotation : Type . Any ~ parameters : Undefined ( ) ; ( " Predicate " , Type . Callable . create ~ annotation : Type . integer ~ parameters : ( Defined [ PositionalOnly { index = 0 ; annotation = Type . variable " T " ; default = false } ] ) ( ) ) ; ] |> ( fun table -> Identifier . Table . find table primitive ) >>| fun alias -> Type . TypeAlias alias in assert_create ~ aliases source resolved in assert_alias " Alias " ( Type . Primitive " Aliased " ) ; assert_alias " Aliased " ( Type . Primitive " Aliased " ) ; assert_alias " typing . Optional [ Alias ] " ( Type . optional ( Type . Primitive " Aliased " ) ) ; assert_alias " Parametric [ Alias ] " ( Type . parametric " Parametric " [ ! Type . Primitive " Aliased " ] ) ; assert_alias " Alias [ int ] " ( Type . parametric " Aliased " [ ! Type . integer ] ) ; assert_alias " IntList " ( Type . list Type . integer ) ; assert_alias " IntList [ str ] " ( Type . list Type . integer ) ; assert_alias " _Future [ int ] " ( Type . union [ Type . parametric " Future " [ ! Type . integer ; Type . integer ] ; Type . awaitable Type . integer ] ) ; assert_alias " baz . Callable [ [ int ] , str ] " ( Type . Callable { kind = Type . Callable . Anonymous ; implementation = { annotation = Type . string ; parameters = Defined [ PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ] ; } ; overloads = [ ] ; } ) ; assert_alias " Predicate [ str ] " ( Type . Callable { kind = Type . Callable . Anonymous ; implementation = { annotation = Type . integer ; parameters = Defined [ PositionalOnly { index = 0 ; annotation = Type . string ; default = false } ] ; } ; overloads = [ ] ; } ) ; let aliases = function | " A " -> Some ( Type . Primitive " A " ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " A " ( Type . Primitive " A " ) ; let aliases = function | " A " -> Some ( Type . list ( Type . Primitive " A " ) ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " A " ( Type . list ( Type . Primitive " A " ) ) ; let aliases = function | " A " -> Some ( Type . list ( Type . Primitive " B " ) ) | " B " -> Some ( Type . Primitive " C " ) | " X " -> Some ( Type . Callable . create ~ annotation ( : Type . Primitive " A " ) ( ) ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " A " ( Type . list ( Type . Primitive " C " ) ) ; assert_create ~ aliases " X " ( Type . Callable . create ~ annotation ( : Type . list ( Type . Primitive " C " ) ) ( ) ) ; let aliases = function | " A " -> Some ( Type . Primitive " B " ) | " module . R " -> Some ( Type . Primitive " module . R . R " ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " A " ( Type . Primitive " B " ) ; assert_create ~ aliases " A . InnerClass " ( Type . Primitive " B . InnerClass " ) ; assert_create ~ aliases " A . InnerClass [ int ] " ( Type . parametric " B . InnerClass " [ ! Type . integer ] ) ; assert_create ~ aliases " module . R . R " ( Type . Primitive " module . R . R . R " ) ; assert_create ~ aliases " A . InnerClass . InnerInnerClass " ( Type . Primitive " B . InnerClass . InnerInnerClass " ) ; let aliases = function | " A " -> Some ( Type . union [ Type . string ; Type . bytes ] ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " typing . Union [ A , str ] " ( Type . union [ Type . string ; Type . bytes ] ) ; ( ) |
let test_create_type_operator _ = let assert_create ( ? aliases = fun _ -> None ) source annotation = assert_equal ~ printer : Type . show ~ cmp : Type . equal annotation ( Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) ) in let variable = Type . Variable . Unary . create " T " in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_create { | pyre_extensions . Compose [ typing . Callable [ [ int ] , str ] , typing . Callable [ [ str ] , bool ] , typing . Callable [ [ bool ] , typing . Tuple [ int , int ] ] ] } | ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concrete [ Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . integer ] ) ~ annotation : Type . string ( ) ; Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . string ] ) ~ annotation : Type . bool ( ) ; Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . bool ] ) ~ annotation ( : Type . tuple [ Type . integer ; Type . integer ] ) ( ) ; ] ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Compose [ int , pyre_extensions . Unpack [ Ts ] , str ] } | ( Type . TypeOperation ( Type . TypeOperation . Compose ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ) ) ; assert_create " pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , . . . ] " ( Type . TypeOperation ( Type . TypeOperation . Compose ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ( Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . integer ] ) ~ annotation : Type . integer ( ) ) ) ) ) ) ; let aliases = function | " T " -> Some ( Type . Variable variable ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases " pyre_extensions . Compose [ T , Foo [ int ] , typing . Callable [ [ str ] , bool ] ] " ( Type . TypeOperation ( Type . TypeOperation . Compose ( Concrete [ Type . Variable variable ; Type . Parametric { name = " Foo " ; parameters = [ Type . Parameter . Single Type . integer ] } ; Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . string ; default = false } ; ] ) ~ annotation : Type . bool ( ) ; ] ) ) ) ; assert_create " pyre_extensions . Compose [ typing . Tuple [ bool , str ] , . . . ] " Type . Top ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Compose [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 3 ] ] ] , typing . Callable [ [ int ] , int ] ] } | Type . Top ; assert_create " pyre_extensions . Compose [ typing . Tuple [ bool , str ] , typing . Callable [ [ int ] , str ] ] " Type . Top ; assert_create { | pyre_extensions . Compose [ pyre_extensions . Compose [ pyre_extensions . Compose [ typing . Callable [ [ bool ] , bytes ] , typing . Callable [ [ bytes ] , int ] ] , typing . Callable [ [ int ] , float ] ] , typing . Callable [ [ float ] , str ] ] } | ( Type . TypeOperation ( Type . TypeOperation . Compose ( Concrete [ Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . bool ; default = false } ; ] ) ~ annotation : Type . bytes ( ) ; Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . bytes ; default = false } ; ] ) ~ annotation : Type . integer ( ) ; Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; ] ) ~ annotation : Type . float ( ) ; Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . float ; default = false } ; ] ) ~ annotation : Type . string ( ) ; ] ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Compose [ pyre_extensions . Compose [ pyre_extensions . Compose [ pyre_extensions . Unpack [ Ts ] , typing . Callable [ [ bytes ] , int ] ] , pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , . . . ] ] , typing . Callable [ [ float ] , str ] ] } | Type . Top ; assert_create { | pyre_extensions . Subtract [ typing_extensions . Literal [ 3 ] , typing_extensions . Literal [ 2 ] ] } | ( Type . literal_integer 1 ) ; let variable = Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " N " in assert_create ~ aliases ( : function | " N " -> Some ( TypeAlias ( Type . Variable variable ) ) | _ -> None ) { | pyre_extensions . Subtract [ N , typing_extensions . Literal [ 1 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . subtract ~ compare_t : Type . compare ( Type . Polynomial . create_from_variable variable ) ( Type . Polynomial . create_from_int 1 ) ) ) ; assert_create { | pyre_extensions . Subtract [ typing_extensions . Literal [ 3 ] , str ] } | ( Type . Parametric { name = " pyre_extensions . Subtract " ; parameters = [ Single ( Type . literal_integer 3 ) ; Single Type . string ] ; } ) ; assert_create { | pyre_extensions . Product [ typing_extensions . Literal [ 3 ] , typing_extensions . Literal [ 2 ] ] } | ( Type . literal_integer 6 ) ; assert_create ~ aliases ( : function | " N " -> Some ( TypeAlias ( Type . Variable variable ) ) | _ -> None ) { | pyre_extensions . Product [ N , typing_extensions . Literal [ 2 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . multiply ~ compare_t : Type . compare ( Type . Polynomial . create_from_variable variable ) ( Type . Polynomial . create_from_int 2 ) ) ) ; assert_create { | pyre_extensions . Product [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 3 ] , typing_extensions . Literal [ 4 ] ] } | ( Type . literal_integer 24 ) ; let variable2 = Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " N2 " in let aliases = function | " N " -> Some ( Type . Variable variable ) | " N2 " -> Some ( Type . Variable variable2 ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , N , N2 , N ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_variables_list ~ compare_t : Type . compare [ 2 , [ variable , 2 ; variable2 , 1 ] ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Product [ str , pyre_extensions . Unpack [ Ts ] ] } | Type . Top ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 0 ] , pyre_extensions . Unpack [ Ts ] ] } | ( Type . literal_integer 0 ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Product [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 1 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 1 ] , ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) , 1 ) ; ] ) ; ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Product [ int , pyre_extensions . Unpack [ Ts ] , ] } | ( Type . Primitive " int " ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) { | pyre_extensions . Product [ typing . Any , pyre_extensions . Unpack [ Ts ] , ] } | Type . Any ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ pyre_extensions . Product [ N2 , N2 ] , typing_extensions . Literal [ 4 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] , N , typing_extensions . Literal [ 3 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 24 , [ Type . Monomial . create_variable variable , 1 ; Type . Monomial . create_variable variable2 , 2 ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) , 1 ) ; ] ) ; ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ N2 , N2 , typing_extensions . Literal [ 4 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] , N , typing_extensions . Literal [ 3 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 24 , [ Type . Monomial . create_variable variable , 1 ; Type . Monomial . create_variable variable2 , 2 ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) , 1 ) ; ] ) ; ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ pyre_extensions . Product [ N2 , pyre_extensions . Unpack [ Ts ] ] , typing_extensions . Literal [ 4 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] , N , typing_extensions . Literal [ 3 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 24 , [ Type . Monomial . create_variable variable , 1 ; Type . Monomial . create_variable variable2 , 1 ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) , 2 ) ; ] ) ; ] ) ) ; assert_create { | pyre_extensions . Product [ pyre_extensions . Unpack [ typing . Tuple [ typing_extensions . Literal [ 2 ] , . . . ] ] ] } | Type . integer ; assert_create { | pyre_extensions . Product [ pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] } | Type . integer ; assert_create ~ aliases { | pyre_extensions . Product [ pyre_extensions . Unpack [ typing . Tuple [ pyre_extensions . Add [ N , typing_extensions . Literal [ 1 ] ] , . . . ] ] ] } | Type . integer ; assert_create { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , pyre_extensions . Unpack [ typing . Tuple [ typing_extensions . Literal [ 0 ] , . . . ] ] ] } | ( Type . literal_integer 0 ) ; assert_create { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , pyre_extensions . Unpack [ typing . Tuple [ typing_extensions . Literal [ 1 ] , . . . ] ] , typing_extensions . Literal [ 3 ] , ] } | ( Type . literal_integer 6 ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ N , N2 , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] ] ] , N2 , typing_extensions . Literal [ 5 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 5 , [ Type . Monomial . create_variable variable , 1 ; Type . Monomial . create_variable variable2 , 2 ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concrete_against_concatenation ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 2 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) , 1 ) ; ] ) ; ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ N , N2 , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ int , . . . ] ] ] , N2 , typing_extensions . Literal [ 5 ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 5 , [ Type . Monomial . create_variable variable , 1 ; Type . Monomial . create_variable variable2 , 2 ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . integer ) ( Type . OrderedTypes . Concatenation . create variadic ) ) , 1 ) ; ] ) ; ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , pyre_extensions . Unpack [ Ts ] ] } | Type . Top ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | other -> aliases other ) { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) , 2 ) ; ] ) ; ] ) ) ; ( ) |
let test_create_variadic_tuple _ = let assert_create ( ? aliases = fun _ -> None ) source annotation = assert_equal ~ printer : Type . show ~ cmp : Type . equal annotation ( Type . create ~ aliases ( : fun ? replace_unbound_parameters_with_any : _ -> aliases ) ( parse_single_expression ~ preprocess : true source ) ) in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " Foo [ pyre_extensions . Unpack [ Ts ] ] " ( Type . parametric " Foo " [ Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " Foo [ int , pyre_extensions . Unpack [ Ts ] , str ] " ( Type . parametric " Foo " [ Single Type . integer ; Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ; Single Type . string ; ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " Foo [ pyre_extensions . Unpack [ typing . Tuple [ int , str ] ] ] " ( Type . parametric " Foo " [ Single Type . integer ; Single Type . string ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " Foo [ int , pyre_extensions . Unpack [ typing . Tuple [ str , pyre_extensions . Unpack [ Ts ] ] ] ] " ( Type . parametric " Foo " [ Single Type . integer ; Single Type . string ; Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ; ] ) ; assert_create " Foo [ pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ( Type . parametric " Foo " [ Unpacked ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] " ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] " ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ pyre_extensions . Unpack [ Ts ] , pyre_extensions . Unpack [ Ts ] ] " Type . Top ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ pyre_extensions . Unpack [ typing . Tuple [ int , str ] ] ] " ( Type . tuple [ Type . integer ; Type . string ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Tuple [ bool , pyre_extensions . Unpack [ typing . Tuple [ int , \ pyre_extensions . Unpack [ typing . Tuple [ int , str ] ] ] ] ] " ( Type . tuple [ Type . bool ; Type . integer ; Type . integer ; Type . string ] ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Callable [ [ int , pyre_extensions . Unpack [ Ts ] , str ] , int ] " ( Type . Callable . create ~ parameters : ( Defined [ Variable ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ; ] ) ~ annotation : Type . integer ( ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | _ -> None ) " typing . Callable [ [ int , pyre_extensions . Unpack [ typing . Tuple [ bool , pyre_extensions . Unpack [ Ts ] , \ bool ] ] , str ] , int ] " ( Type . Callable . create ~ parameters : ( Defined [ Variable ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ; Type . bool ] ~ suffix [ : Type . bool ; Type . string ] variadic ) ) ; ] ) ~ annotation : Type . integer ( ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None ) " typing . Callable [ [ Variable ( int , pyre_extensions . Unpack [ Ts ] , str ) ] , \ typing . Callable [ [ Variable ( int , pyre_extensions . Unpack [ Ts2 ] , str ) ] , int ] ] " ( Type . Callable . create ~ parameters : ( Defined [ Variable ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ; ] ) ~ annotation : ( Type . Callable . create ~ parameters : ( Defined [ Variable ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic2 ) ) ; ] ) ~ annotation : Type . integer ( ) ) ( ) ) ; let variable_t1 = Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " T1 " in let variable_t2 = Type . Variable . Unary . create ~ constraints ( : Type . Record . Variable . Bound ( Type . Primitive " int " ) ) " T2 " in let literal_tuple = List . map ~ f : Type . literal_integer in assert_create { | pyre_extensions . Broadcast [ pyre_extensions . Broadcast [ typing . Tuple [ int , . . . ] , typing . Tuple [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 2 ] ] , ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] ] , ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . integer ) ) ) ; assert_create { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 5 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 1 ] ] ] } | ( Type . tuple ( literal_tuple [ 5 ; 2 ] ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None ) { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete ( : literal_tuple [ 1 ; 2 ] ) ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None ) { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ) ; assert_create ~ aliases ( : function | " Ts " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None ) { | typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) ) ; let aliases = function | " T1 " -> Some ( Type . Variable variable_t1 ) | " T2 " -> Some ( Type . Variable variable_t2 ) | _ -> None in let aliases = create_type_alias_table aliases in assert_create ~ aliases { | pyre_extensions . Broadcast [ typing . Tuple [ T1 , typing_extensions . Literal [ 5 ] ] , typing . Tuple [ T2 , typing_extensions . Literal [ 5 ] ] , ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : Type . Variable variable_t1 ; Type . literal_integer 5 ] ~ right [ : Type . Variable variable_t2 ; Type . literal_integer 5 ] ) ) ) ; assert_create ~ aliases { | pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , T1 ] , ] } | ( Type . Tuple ( Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : Type . Variable variable_t1 ; Type . Variable variable_t2 ] ~ right [ : Type . Variable variable_t2 ; Type . Variable variable_t1 ] ) ) ) ; assert_create ~ aliases { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 3 ] ] , ] } | ( Type . Parametric { name = " pyre_extensions . BroadcastError " ; parameters = [ Type . Parameter . Single ( Type . tuple [ Type . literal_integer 2 ] ) ; Type . Parameter . Single ( Type . tuple [ Type . literal_integer 3 ] ) ; ] ; } ) ; assert_create ~ aliases { | pyre_extensions . Broadcast [ typing . Tuple [ T1 , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ T2 , typing_extensions . Literal [ 3 ] ] , ] } | ( Type . Parametric { name = " pyre_extensions . BroadcastError " ; parameters = [ Type . Parameter . Single ( Type . tuple [ Type . Variable variable_t1 ; Type . literal_integer 2 ] ) ; Type . Parameter . Single ( Type . tuple [ Type . Variable variable_t2 ; Type . literal_integer 3 ] ) ; ] ; } ) ; assert_create { | pyre_extensions . Broadcast [ 1 , 2 ] } | Type . Bottom ; ( ) |
let test_resolve_aliases _ = let assert_resolved ~ aliases annotation expected = let aliases ? replace_unbound_parameters_with_any : _ = aliases in assert_equal ~ printer : Type . show ~ cmp : Type . equal expected ( Type . resolve_aliases ~ aliases annotation ) in let aliases = function | " MyInt " -> Some ( Type . TypeAlias Type . integer ) | " IntList " -> Some ( Type . TypeAlias ( Type . list Type . integer ) ) | _ -> None in assert_resolved ~ aliases ( Type . Primitive " NotAlias " ) ( Type . Primitive " NotAlias " ) ; assert_resolved ~ aliases ( Type . Primitive " MyInt " ) Type . integer ; assert_resolved ~ aliases ( Type . Primitive " IntList " ) ( Type . list Type . integer ) ; assert_resolved ~ aliases ( Type . parametric " IntList " [ Single Type . string ] ) ( Type . list Type . integer ) ; let variable_t = Type . Variable ( Type . Variable . Unary . create " T " ) in let variable_k = Type . Variable ( Type . Variable . Unary . create " K " ) in let variable_v = Type . Variable ( Type . Variable . Unary . create " V " ) in let aliases = function | " IntList " -> Some ( Type . TypeAlias ( Type . list Type . integer ) ) | " foo . Optional " -> Some ( Type . TypeAlias ( Type . optional variable_t ) ) | " foo . Dict " -> Some ( Type . TypeAlias ( Type . dictionary ~ key : variable_k ~ value : variable_v ) ) | _ -> None in assert_resolved ~ aliases ( Type . parametric " foo . Optional " [ Single ( Type . parametric " foo . Dict " [ Single Type . string ; Single Type . integer ] ) ] ) ( Type . optional ( Type . dictionary ~ key : Type . string ~ value : Type . integer ) ) ; let tree_body = Type . union [ Type . integer ; Type . list ( Type . Primitive " Tree " ) ] in let aliases ? replace_unbound_parameters_with_any : _ name = match name with | " Tree " -> Some ( Type . TypeAlias ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ) | _ -> None in assert_resolved ~ aliases ( Type . Primitive " Tree " ) ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ; assert_resolved ~ aliases ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ; assert_resolved ~ aliases ( Type . list ( Type . Primitive " Tree " ) ) ( Type . list ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ) ; assert_resolved ~ aliases ( Type . parametric " Tree " [ Single Type . integer ] ) ( Type . RecursiveType . create ~ name " : Tree " ~ body : tree_body ) ; let aliases = function | " MyDict " -> Some ( Type . TypeAlias ( Type . dictionary ~ key : variable_t ~ value : variable_k ) ) | _ -> None in assert_resolved ~ aliases ( Type . parametric " MyDict " [ ! Type . integer ; Type . string ] ) ( Type . dictionary ~ key : Type . integer ~ value : Type . string ) ; let aliases = function | " Mix " -> Some ( Type . TypeAlias ( Type . parametric " Foo " [ ! variable_t ; Type . list variable_v ; Type . union [ variable_k ; variable_v ] ] ) ) | _ -> None in assert_resolved ~ aliases ( Type . parametric " Mix " [ ! Type . integer ; Type . string ; Type . bool ] ) ( Type . parametric " Foo " [ ! Type . integer ; Type . list Type . string ; Type . union [ Type . bool ; Type . string ] ] ) ; let aliases = function | " Foo " -> Some ( Type . TypeAlias ( Type . parametric " Bar " [ ! variable_t ; variable_v ] ) ) | _ -> None in assert_resolved ~ aliases ( Type . Primitive " Foo " ) ( Type . parametric " Bar " [ ! Type . Any ; Type . Any ] ) ; let parameter_variadic = Type . Variable . Variadic . Parameters . create " TParams " in let aliases = function | " TParams " -> Some ( Type . VariableAlias ( Type . Variable . ParameterVariadic parameter_variadic ) ) | " FooParamSpec " -> Some ( Type . TypeAlias ( Type . parametric " Bar " [ CallableParameters ( Type . Variable . Variadic . Parameters . self_reference parameter_variadic ) ; ] ) ) | _ -> None in assert_resolved ~ aliases ( Type . parametric " FooParamSpec " [ ! Type . integer ; Type . string ] ) ( Type . parametric " Bar " [ CallableParameters ( Defined [ PositionalOnly { index = 0 ; annotation = Type . integer ; default = false } ; PositionalOnly { index = 1 ; annotation = Type . string ; default = false } ; ] ) ; ] ) ; assert_resolved ~ aliases ( Type . Primitive " FooParamSpec " ) ( Type . parametric " Bar " [ CallableParameters Undefined ] ) ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let aliases = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " FloatTensor " -> Some ( Type . TypeAlias ( Type . parametric " Tensor " [ Single Type . float ; Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ; ] ) ) | _ -> None in assert_resolved ~ aliases ( Type . parametric " FloatTensor " [ ! Type . integer ; Type . string ] ) ( Type . parametric " Tensor " [ Single Type . float ; Single Type . integer ; Single Type . string ] ) ; assert_resolved ~ aliases ( Type . Primitive " FloatTensor " ) ( Type . parametric " Tensor " [ Single Type . float ; Unpacked ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . Any ) ; ] ) ; ( ) |
let test_instantiate _ = let assert_instantiate mappings ~ generic ~ expected = let map = Type . Map . of_alist_exn mappings in assert_equal ~ printer : Type . show ~ cmp : Type . equal expected ( Type . instantiate ~ constraints ( : Map . find map ) generic ) in assert_instantiate [ ] ~ generic ( : Type . Primitive " foo " ) ~ expected ( : Type . Primitive " foo " ) ; assert_instantiate [ Type . variable " _T " , Type . integer ; Type . variable " _VT " , Type . NoneType ] ~ generic ( : Type . Union [ Type . variable " _T " ; Type . variable " _VT " ] ) ~ expected ( : Type . optional Type . integer ) |
let test_expression _ = let assert_expression annotation expression = assert_equal ~ printer : Expression . show ~ cmp ( : fun left right -> Expression . location_insensitive_compare left right = 0 ) ( parse_single_expression ~ coerce_special_methods : true expression ) ( Type . expression annotation ) in assert_expression ( Type . Primitive " foo " ) " foo " ; assert_expression ( Type . Primitive " . . . " ) " . . . " ; assert_expression ( Type . Primitive " foo . bar " ) " foo . bar " ; assert_expression ( Type . parametric " foo . bar " [ ! Type . Primitive " baz " ] ) " foo . bar . __getitem__ ( baz ) " ; assert_expression ( Type . Tuple ( Type . OrderedTypes . Concrete [ Type . integer ; Type . string ] ) ) " typing . Tuple . __getitem__ ( ( int , str ) ) " ; assert_expression ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) " typing . Tuple . __getitem__ ( ( int , . . . ) ) " ; assert_expression ( Type . parametric " list " [ ! Type . integer ] ) " typing . List . __getitem__ ( int ) " ; let open Type . Callable in assert_expression ( Type . Callable . create ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( . . . , int ) ) " ; assert_expression ( Type . Callable . create ~ name " :!& name " ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( . . . , int ) ) " ; assert_expression ( Type . Callable . create ~ overloads : [ { Type . Callable . annotation = Type . string ; parameters = Type . Callable . Undefined } ; { Type . Callable . annotation = Type . integer ; parameters = Type . Callable . Undefined } ; ] ~ annotation : Type . integer ( ) ) " typing . Callable [ ( . . . , int ) ] . __getitem__ ( __getitem__ ( ( . . . , str ) ) [ ( . . . , int ) ] ) " ; assert_expression ( Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Parameter . Named { name = " __0 " ; annotation = Type . integer ; default = false } ; Parameter . Named { name = " __1 " ; annotation = Type . string ; default = false } ; ] ) ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( [ Named ( __0 , int ) , Named ( __1 , str ) ] , int ) ) " ; assert_expression ( Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Parameter . Named { name = " a " ; annotation = Type . integer ; default = false } ; Parameter . Named { name = " b " ; annotation = Type . string ; default = false } ; ] ) ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( [ Named ( a , int ) , Named ( b , str ) ] , int ) ) " ; assert_expression ( Type . Callable . create ~ parameters : ( Type . Callable . Defined [ Parameter . Named { name = " a " ; annotation = Type . integer ; default = true } ] ) ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( [ Named ( a , int , default ) ] , int ) ) " ; assert_expression ( Type . parametric " G " [ CallableParameters ( Type . Variable . Variadic . Parameters . self_reference ( Type . Variable . Variadic . Parameters . create " TParams " ) ) ; ] ) " G [ TParams ] " ; assert_expression ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " test . MyEnum " ; member_name = " ONE " } ) ) " typing_extensions . Literal [ test . MyEnum . ONE ] " ; assert_expression ( Type . Literal ( Type . String AnyLiteral ) ) " typing_extensions . Literal [ str ] " ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_expression ( Type . parametric " Foo " [ Unpacked ( Type . OrderedTypes . Concatenation . create_unpackable variadic ) ] ) " Foo [ pyre_extensions . Unpack [ Ts ] ] " ; assert_expression ( Type . Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : Type . literal_integer 5 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) ) { | typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ ( typing . Tuple [ typing_extensions . Literal [ 5 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ) ] ] ] } ; | assert_expression ( Type . Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_concatenation_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create variadic ) ( Type . OrderedTypes . Concatenation . create variadic ) ) ) ) { | typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ ( typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ) ] ] ] } ; | assert_expression ( Type . Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : Type . variable " T1 " ] ~ right [ : Type . variable " T2 " ] ) ) ) { | typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ ( typing . Tuple [ T2 ] , typing . Tuple [ T1 ] ) ] ] ] } ; | assert_expression ( Type . Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ) " typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] " ; assert_expression ( Type . Callable . create ~ parameters : ( Defined [ Parameter . Variable ( Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . integer ] ~ suffix [ : Type . string ] variadic ) ) ; ] ) ~ annotation : Type . integer ( ) ) " typing . Callable . __getitem__ ( ( [ Variable ( int , pyre_extensions . Unpack [ Ts ] , str ) ] , int ) ) " ; let callable1 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . integer ] ) ~ annotation : Type . string ( ) in let callable2 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . string ] ) ~ annotation : Type . bool ( ) in assert_expression ( Type . TypeOperation ( Compose ( Type . OrderedTypes . Concrete [ callable1 ; callable2 ] ) ) ) " pyre_extensions . Compose [ ( typing . Callable [ ( [ PositionalOnly ( int ) ] , str ) ] , \ typing . Callable [ ( [ PositionalOnly ( str ) ] , bool ) ] ) ] " ; ( ) |
let test_concise _ = let assert_concise annotation expected = assert_equal ~ printer ( : fun annotation -> annotation ) expected ( Type . show_concise annotation ) in assert_concise Type . Bottom " " ; ? assert_concise Type . Top " unknown " ; assert_concise ( Type . Callable . create ~ name " :!& foo " ~ annotation : Type . integer ~ parameters : Type . Callable . Undefined ( ) ) " ( . . . ) -> int " ; assert_concise ( Type . Callable . create ~ name " :!& foo " ~ annotation : Type . integer ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . Named { name = " x " ; annotation = Type . Any ; default = false } ; Type . Callable . Parameter . Named { name = " y " ; annotation = Type . float ; default = false } ; ] ) ( ) ) " ( x : Any , y : float ) -> int " ; assert_concise ( Type . Callable . create ~ name " :!& foo " ~ annotation : Type . integer ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . PositionalOnly { index = 0 ; annotation = Type . Any ; default = true } ; ] ) ( ) ) " ( Any . . . ) = -> int " ; assert_concise ( Type . Callable . create ~ name " :!& foo " ~ annotation : Type . integer ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . Named { name = " x " ; annotation = Type . Any ; default = true } ] ) ( ) ) " ( x : Any = . . . ) -> int " ; assert_concise ( Type . Callable . create ~ name " :!& foo " ~ annotation : Type . integer ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . Named { name = " callable " ; default = false ; annotation = Type . Callable . create ~ name " :!& bar " ~ annotation : Type . float ~ parameters : ( Type . Callable . Defined [ Type . Callable . Parameter . Named { name = " x " ; annotation = Type . integer ; default = false } ; ] ) ( ) ; } ; ] ) ( ) ) " ( callable : ( x : int ) -> float ) -> int " ; assert_concise Type . Any " Any " ; assert_concise Type . NoneType " None " ; assert_concise ( Type . optional Type . integer ) " Optional [ int ] " ; assert_concise ( Type . parametric " parametric " [ ! Type . Top ; Type . Top ] ) " parametric [ ] " ; assert_concise ( Type . parametric " parametric " [ ! Type . Top ; Type . float ] ) " parametric [ unknown , float ] " ; assert_concise ( Type . Primitive " a . b . c " ) " c " ; assert_concise ( Type . tuple [ Type . integer ; Type . Any ] ) " Tuple [ int , Any ] " ; assert_concise ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) " Tuple [ int , . . . ] " ; assert_concise ( Type . union [ Type . integer ; Type . string ] ) " Union [ int , str ] " ; assert_concise ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . Top ] ) " T " ) " T " ; assert_concise ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " test . MyEnum " ; member_name = " ONE " } ) ) " typing_extensions . Literal [ test . MyEnum . ONE ] " ; assert_concise ( Type . Literal ( Type . String AnyLiteral ) ) " typing_extensions . LiteralString " ; ( ) |
let test_weaken_literals _ = let assert_weakened_literal literal expected = assert_equal ~ printer : Type . show ~ cmp : Type . equal expected ( Type . weaken_literals literal ) in assert_weakened_literal ( Type . literal_integer 1 ) Type . integer ; assert_weakened_literal ( Type . literal_string " foo " ) Type . string ; assert_weakened_literal ( Type . Literal ( Type . String AnyLiteral ) ) Type . string ; assert_weakened_literal ( Type . literal_bytes " foo " ) Type . bytes ; assert_weakened_literal ( Type . Literal ( Type . Boolean true ) ) Type . bool ; assert_weakened_literal ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " test . MyEnum " ; member_name = " ONE " } ) ) ( Type . Primitive " test . MyEnum " ) ; assert_weakened_literal ( Type . list ( Type . literal_integer 1 ) ) ( Type . list Type . integer ) ; ( ) |
let test_union _ = let assert_union arguments expected = assert_equal ~ printer : Type . show ~ cmp : Type . equal expected ( Type . union arguments ) in assert_union [ Type . string ; Type . optional ( Type . Union [ Type . integer ; Type . string ] ) ] ( Type . Union [ Type . none ; Type . integer ; Type . string ] ) ; assert_union [ Type . string ; Type . float ] ( Type . Union [ Type . float ; Type . string ] ) ; assert_union [ Type . float ; Type . string ] ( Type . Union [ Type . float ; Type . string ] ) ; assert_union [ Type . optional Type . string ; Type . float ] ( Type . Union [ Type . NoneType ; Type . float ; Type . string ] ) ; assert_union [ Type . float ; Type . string ; Type . optional Type . float ] ( Type . Union [ Type . NoneType ; Type . float ; Type . string ] ) ; assert_union [ Type . float ; Type . Any ] Type . Any ; assert_union [ Type . float ; Type . Top ] Type . Top ; assert_union [ Type . string ; Type . float ] ( Type . Union [ Type . float ; Type . string ] ) ; assert_union [ Type . float ; Type . string ] ( Type . Union [ Type . float ; Type . string ] ) ; assert_union [ Type . float ] Type . float ; assert_union [ Type . float ; Type . Bottom ] Type . float ; assert_union [ Type . Bottom ; Type . Bottom ] Type . Bottom ; assert_union [ Type . float ; Type . union [ Type . string ; Type . bytes ] ] ( Type . Union [ Type . bytes ; Type . float ; Type . string ] ) ; assert_union [ Type . optional ( Type . list Type . integer ) ; Type . list Type . integer ] ( Type . optional ( Type . list Type . integer ) ) ; assert_union [ Type . optional ( Type . variable " A " ) ; Type . variable " A " ] ( Type . optional ( Type . variable " A " ) ) ; assert_union [ Type . string ; Type . optional ( Type . Union [ Type . integer ; Type . string ] ) ] ( Type . Union [ Type . NoneType ; Type . integer ; Type . string ] ) ; ( ) |
let test_primitives _ = assert_equal [ ] ( Type . primitives ( Type . Callable . create ~ annotation : Type . Top ( ) ) ) ; assert_equal [ Type . integer ] ( Type . primitives ( Type . Callable . create ~ annotation : Type . integer ( ) ) ) ; assert_equal [ ] ( Type . primitives ( Type . optional Type . Top ) ) ; assert_equal [ Type . integer ] ( Type . primitives ( Type . optional Type . integer ) ) ; assert_equal [ ] ( Type . primitives ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . Top ) ) ) ; assert_equal [ Type . integer ] ( Type . primitives ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) ; assert_equal [ ] ( Type . primitives ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . Top ] ) " T " ) ) ; assert_equal [ Type . integer ] ( Type . primitives ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . integer ] ) " T " ) ) ; assert_equal [ Type . integer ] ( Type . primitives ( Type . parametric " parametric " [ ! Type . integer ; Type . Top ] ) ) ; assert_equal [ Type . integer ; Type . string ] ( Type . primitives ( Type . parametric " parametric " [ ! Type . integer ; Type . string ] ) ) ; assert_equal [ Type . string ] ( Type . primitives ( Type . tuple [ Type . Top ; Type . string ] ) ) ; assert_equal [ Type . integer ; Type . string ] ( Type . primitives ( Type . tuple [ Type . integer ; Type . string ] ) ) ; assert_equal [ Type . integer ; Type . string ] ( Type . primitives ( Type . union [ Type . integer ; Type . string ] ) ) ; assert_equal [ ] ( Type . primitives Type . Top ) ; assert_equal [ ] ( Type . primitives Type . Bottom ) ; assert_equal [ Type . integer ] ( Type . primitives Type . integer ) ; assert_equal [ ] ( Type . primitives Type . Any ) ; ( ) |
let test_elements _ = let assert_equal = assert_equal ~ printer ( : List . to_string ~ f : Fn . id ) in assert_equal [ " typing . Callable " ] ( Type . elements ( Type . Callable . create ~ annotation : Type . Top ( ) ) ) ; assert_equal [ " int " ; " typing . Callable " ] ( Type . elements ( Type . Callable . create ~ annotation : Type . integer ( ) ) ) ; assert_equal [ " int " ; " typing . Optional " ] ( Type . elements ( Type . optional Type . integer ) ) ; assert_equal [ " tuple " ] ( Type . elements ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . Top ) ) ) ; assert_equal [ " int " ; " tuple " ] ( Type . elements ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) ; assert_equal [ ] ( Type . elements ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . Top ] ) " T " ) ) ; assert_equal [ " int " ] ( Type . elements ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . integer ] ) " T " ) ) ; assert_equal [ " int " ; " parametric " ] ( Type . elements ( Type . parametric " parametric " [ ! Type . integer ; Type . Top ] ) ) ; assert_equal [ " int " ; " str " ; " parametric " ] ( Type . elements ( Type . parametric " parametric " [ ! Type . integer ; Type . string ] ) ) ; assert_equal [ " str " ; " tuple " ] ( Type . elements ( Type . tuple [ Type . Top ; Type . string ] ) ) ; assert_equal [ " int " ; " str " ; " tuple " ] ( Type . elements ( Type . tuple [ Type . integer ; Type . string ] ) ) ; assert_equal [ " int " ; " str " ; " typing . Union " ] ( Type . elements ( Type . union [ Type . integer ; Type . string ] ) ) ; assert_equal [ ] ( Type . elements Type . Top ) ; assert_equal [ ] ( Type . elements Type . Bottom ) ; assert_equal [ " int " ] ( Type . elements Type . integer ) ; assert_equal [ ] ( Type . elements Type . Any ) ; assert_equal [ " int " ; " tuple " ] ( Type . elements ( Type . RecursiveType . create ~ name " : Tree " ~ body ( : Type . tuple [ Type . integer ; Type . Primitive " Tree " ] ) ) ) ; ( ) ; assert_equal [ " typing_extensions . Literal " ] ( Type . elements ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " A . B . C . MyEnum " ; member_name = " ONE " } ) ) ) ; assert_equal [ " str " ; " typing_extensions . Literal " ] ( Type . elements ( Type . Literal ( Type . String AnyLiteral ) ) ) ; ( ) |
let test_exists _ = let top_exists = Type . exists ~ predicate ( : function | Type . Top -> true | _ -> false ) in assert_true ( top_exists ( Type . Callable . create ~ annotation : Type . Top ( ) ) ) ; assert_false ( top_exists ( Type . Callable . create ~ annotation : Type . integer ( ) ) ) ; assert_true ( top_exists ( Type . optional Type . Top ) ) ; assert_false ( top_exists ( Type . optional Type . integer ) ) ; assert_true ( top_exists ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . Top ) ) ) ; assert_false ( top_exists ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) ; assert_true ( top_exists ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . Top ] ) " T " ) ) ; assert_false ( top_exists ( Type . variable ~ constraints ( : Type . Variable . Explicit [ Type . integer ] ) " T " ) ) ; assert_true ( top_exists ( Type . parametric " parametric " [ ! Type . integer ; Type . Top ] ) ) ; assert_false ( top_exists ( Type . parametric " parametric " [ ! Type . integer ; Type . string ] ) ) ; assert_true ( top_exists ( Type . tuple [ Type . Top ; Type . string ] ) ) ; assert_false ( top_exists ( Type . tuple [ Type . integer ; Type . string ] ) ) ; assert_true ( top_exists ( Type . union [ Type . integer ; Type . Top ] ) ) ; assert_false ( top_exists ( Type . union [ Type . integer ; Type . string ] ) ) ; assert_true ( top_exists Type . Top ) ; assert_false ( top_exists Type . Bottom ) ; assert_false ( top_exists Type . integer ) ; assert_false ( top_exists Type . Any ) |
let test_is_iterator _ = assert_true ( Type . is_iterator ( Type . iterator Type . string ) ) ; assert_false ( Type . is_iterator Type . string ) ; assert_false ( Type . is_iterator ( Type . Primitive " typing . Iterator " ) ) |
let test_contains_callable _ = assert_true ( Type . contains_callable ( Type . Callable . create ~ annotation : Type . integer ( ) ) ) ; assert_true ( Type . contains_callable ( Type . optional ( Type . Callable . create ~ annotation : Type . integer ( ) ) ) ) ; assert_true ( Type . contains_callable ( Type . union [ Type . string ; Type . Callable . create ~ annotation : Type . integer ( ) ] ) ) ; assert_false ( Type . contains_callable ( Type . Primitive " foo " ) ) |
let test_map_callable_annotation _ = let assert_mapped ~ f callable expected = assert_equal ~ cmp : Type . Callable . equal ~ printer : Type . Callable . show expected ( Type . Callable . map_annotation ~ f callable ) in let callable = { Type . Record . Callable . kind = Type . Record . Callable . Named ( Reference . create " foo " ) ; implementation = { Type . Record . Callable . annotation = Type . union [ Type . string ; Type . integer ] ; parameters = Type . Record . Callable . Defined [ ] ; } ; overloads = [ { Type . Record . Callable . annotation = Type . string ; parameters = Type . Record . Callable . Defined [ ] ; } ; ] ; } in let mapped_callable = { Type . Record . Callable . kind = Type . Record . Callable . Named ( Reference . create " foo " ) ; implementation = { Type . Record . Callable . annotation = Type . bool ; parameters = Type . Record . Callable . Defined [ ] ; } ; overloads = [ { Type . Record . Callable . annotation = Type . bool ; parameters = Type . Record . Callable . Defined [ ] ; } ; ] ; } in assert_mapped ~ f ( : fun _ -> Type . bool ) callable mapped_callable ; ( ) |
let test_type_parameters_for_bounded_tuple_union _ = let assert_type_parameters actual expected = assert_equal ~ cmp [ :% equal : Type . t list option ] ~ printer [ :% show : Type . t list option ] expected ( Type . type_parameters_for_bounded_tuple_union actual ) in assert_type_parameters Type . integer None ; assert_type_parameters ( Type . union [ Type . tuple [ Type . integer ; Type . string ] ; Type . tuple [ Type . bool ] ] ) None ; assert_type_parameters ( Type . union [ Type . tuple [ Type . integer ; Type . string ] ; Type . tuple [ Type . bool ; Type . list Type . integer ] ] ) ( Some [ Type . union [ Type . integer ; Type . bool ] ; Type . union [ Type . string ; Type . list Type . integer ] ] ) ; ( ) |
let test_contains_any _ = assert_true ( Type . contains_any Type . Any ) |
let test_expression_contains_any _ = assert_true ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Dict [ typing . Any , typing . Any ] " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " dict " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Type " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Callable " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Tuple " ) ) ; assert_true ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Union [ typing . Any , None ] " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " typing . Union [ typing . Callable ] " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " pyre_extensions . type_variable_operators . Map [ typing . List , int ] " ) ) ; assert_false ( Type . expression_contains_any ( parse_single_expression ~ preprocess : true " foo [ pyre_extensions . type_variable_operators . Concatenate [ int , bool , Ts ] ] " ) ) ; ( ) |
let test_is_concrete _ = assert_true ( Type . is_concrete Type . none ) ; assert_true ( Type . is_concrete ( Type . parametric " typing . Optional " [ ! Type . Bottom ] ) ) ; assert_true ( Type . is_concrete ( Type . Callable . create ~ annotation : Type . none ( ) ) ) ; assert_false ( Type . is_concrete ( Type . Callable . create ~ annotation ( : Type . list Type . Bottom ) ( ) ) ) ; ( ) |
let test_is_not_instantiated _ = assert_true ( Type . is_not_instantiated Type . Bottom ) ; assert_true ( Type . is_not_instantiated ( Type . dictionary ~ key : Type . Bottom ~ value : Type . Bottom ) ) ; assert_false ( Type . is_not_instantiated Type . NoneType ) ; assert_false ( Type . is_not_instantiated Type . Top ) ; assert_true ( Type . is_not_instantiated ( Type . variable " _T " ) ) |
let test_is_meta _ = assert_true ( Type . is_meta ( Type . parametric " type " [ ! Type . integer ] ) ) ; assert_false ( Type . is_meta Type . integer ) ; assert_false ( Type . is_meta ( Type . parametric " typing . Type " [ ! Type . integer ] ) ) |
let test_is_none _ = assert_false ( Type . is_none ( Type . Primitive " None " ) ) ; assert_false ( Type . is_none Type . integer ) ; assert_false ( Type . is_none ( Type . Primitive " foo " ) ) ; assert_true ( Type . is_none Type . NoneType ) |
let test_is_type_alias _ = assert_true ( Type . is_type_alias ( Type . Primitive " typing_extensions . TypeAlias " ) ) ; assert_false ( Type . is_type_alias ( Type . Primitive " typing . TypeAlias " ) ) ; assert_false ( Type . is_type_alias ( Type . parametric " typing_extensions . TypeAlias " [ ! Type . Top ] ) ) |
let test_create_recursive_type _ = let tree_name , tree_body = " Tree " , Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Foo " ; Type . Primitive " Tree " ] ] in let tree_annotation = Type . RecursiveType . create ~ name : tree_name ~ body : tree_body in assert_raises ( Failure " Body of recursive type contains a recursive type with the same name " ) ( fun ( ) -> Type . RecursiveType . create ~ name : tree_name ~ body : tree_annotation ) ; ( ) |
let test_unfold_recursive_type _ = let assert_unfolded recursive_type expected = assert_equal ~ cmp : Type . equal ~ printer : Type . show expected ( match recursive_type with | Type . RecursiveType record -> Type . RecursiveType . unfold_recursive_type record | _ -> failwith " expected RecursiveType " ) in let tree_name , tree_body = " Tree " , Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Foo " ; Type . Primitive " Tree " ] ] in let tree_annotation = Type . RecursiveType . create ~ name : tree_name ~ body : tree_body in assert_unfolded ( Type . RecursiveType . create ~ name : tree_name ~ body : tree_body ) ( Type . union [ Type . integer ; Type . tuple [ Type . Primitive " Foo " ; tree_annotation ] ] ) ; ( ) |
let test_contains_unknown _ = assert_true ( Type . contains_unknown Type . Top ) ; assert_false ( Type . contains_unknown Type . Bottom ) ; assert_false ( Type . contains_unknown Type . Any ) ; assert_true ( Type . contains_unknown ( Type . optional Type . Top ) ) ; assert_false ( Type . contains_unknown ( Type . optional Type . integer ) ) ; assert_true ( Type . contains_unknown ( Type . optional ( Type . parametric " foo " [ ! Type . integer ; Type . Top ] ) ) ) ; assert_true ( Type . contains_unknown ( Type . parametric " foo " [ ! Type . integer ; Type . Top ] ) ) ; assert_false ( Type . contains_unknown ( Type . parametric " foo " [ ! Type . integer ] ) ) ; assert_false ( Type . contains_unknown Type . integer ) ; assert_true ( Type . contains_unknown Type . Top ) ; assert_true ( Type . contains_unknown ( Type . Union [ Type . integer ; Type . Top ] ) ) ; assert_false ( Type . contains_unknown ( Type . Union [ Type . integer ; Type . string ] ) ) ; assert_false ( Type . contains_unknown ( Type . variable " derp " ) ) ; assert_true ( Type . contains_unknown ( Type . Tuple ( Concrete [ Type . integer ; Type . Top ] ) ) ) ; assert_false ( Type . contains_unknown ( Type . Tuple ( Concrete [ Type . integer ; Type . string ] ) ) ) ; assert_true ( Type . contains_unknown ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . Top ) ) ) ; assert_false ( Type . contains_unknown ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) |
let test_contains_undefined _ = assert_true ( Type . contains_undefined Type . Bottom ) ; assert_false ( Type . contains_undefined Type . Top ) ; assert_false ( Type . contains_undefined Type . Any ) ; assert_true ( Type . contains_undefined ( Type . optional Type . Bottom ) ) ; assert_false ( Type . contains_undefined ( Type . optional Type . integer ) ) ; assert_true ( Type . contains_undefined ( Type . optional ( Type . parametric " foo " [ ! Type . integer ; Type . Bottom ] ) ) ) ; assert_true ( Type . contains_undefined ( Type . parametric " foo " [ ! Type . integer ; Type . Bottom ] ) ) ; assert_false ( Type . contains_undefined ( Type . parametric " foo " [ ! Type . integer ] ) ) ; assert_false ( Type . contains_undefined Type . integer ) ; assert_true ( Type . contains_undefined Type . Bottom ) ; assert_true ( Type . contains_undefined ( Type . Union [ Type . integer ; Type . Bottom ] ) ) ; assert_false ( Type . contains_undefined ( Type . Union [ Type . integer ; Type . string ] ) ) ; assert_false ( Type . contains_undefined ( Type . variable " derp " ) ) ; assert_true ( Type . contains_undefined ( Type . Tuple ( Concrete [ Type . integer ; Type . Bottom ] ) ) ) ; assert_false ( Type . contains_undefined ( Type . Tuple ( Concrete [ Type . integer ; Type . string ] ) ) ) ; assert_true ( Type . contains_undefined ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . Bottom ) ) ) ; assert_false ( Type . contains_undefined ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ) |
let test_is_resolved _ = assert_false ( Type . Variable . all_variables_are_resolved ( Type . variable " _T " ) ) ; assert_false ( Type . Variable . all_variables_are_resolved ( Type . union [ Type . integer ; Type . variable " _T " ] ) ) ; assert_true ( Type . Variable . all_variables_are_resolved Type . integer ) ; assert_true ( Type . Variable . all_variables_are_resolved ( Type . union [ Type . integer ; Type . string ] ) ) ; let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in assert_false ( Type . Variable . all_variables_are_resolved ( Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) ) ) ; let parameter_variadic = parameter_variadic |> Type . Variable . Variadic . Parameters . mark_as_bound in assert_true ( Type . Variable . all_variables_are_resolved ( Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) ) ) ; ( ) |
let test_class_name _ = let assert_class_name annotation expected = assert_equal ~ cmp : Reference . equal ~ printer : Reference . show ( Reference . create expected ) ( Type . class_name annotation ) in assert_class_name ( Type . Primitive " qualified . primitive " ) " qualified . primitive " ; assert_class_name ( Type . list Type . integer ) " list " ; assert_class_name ( Type . union [ Type . string ; Type . integer ] ) " typing . Union " ; assert_class_name ( Type . variable " _T " ) " _T " |
let test_optional_value _ = assert_equal ( Option . value_exn ( Type . optional_value ( Type . optional ( Type . parametric " foo " [ ! Type . integer ; Type . Top ] ) ) ) ) ( Type . parametric " foo " [ ! Type . integer ; Type . Top ] ) ; assert_true ( Option . is_none ( Type . optional_value ( Type . parametric " foo " [ ! Type . integer ; Type . Top ] ) ) ) |
let test_dequalify _ = let map = { | from typing import ( Optional , List , ) import typing from A . B import C import d as e } | in let assert_dequalify source expected = assert_equal ~ cmp : Type . equal ~ printer ( : Format . asprintf " % a " Type . pp ) ~ pp_diff ( : diff ~ print : Type . pp ) ( Type . dequalify ( Preprocessing . dequalify_map ( parse map ) ) source ) expected in let create name = Type . Primitive name in assert_dequalify ( Type . optional Type . string ) ( Type . parametric " Optional " [ ! Type . string ] ) ; assert_dequalify ( Type . parametric " list " [ ! Type . string ] ) ( Type . parametric " List " [ ! Type . string ] ) ; assert_dequalify ( Type . Union [ Type . string ; create " A . B . C " ] ) ( Type . parametric " typing . Union " [ ! create " C " ; Type . string ] ) ; assert_dequalify ( create " d " ) ( create " e " ) ; assert_dequalify ( Type . parametric " A . B . C " [ ! Type . optional Type . integer ] ) ( Type . parametric " C " [ ! Type . parametric " Optional " [ ! Type . integer ] ] ) ; assert_dequalify ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " A . B . C . MyEnum " ; member_name = " ONE " } ) ) ( Type . Literal ( Type . EnumerationMember { enumeration_type = Type . Primitive " C . MyEnum " ; member_name = " ONE " } ) ) ; let assert_dequalify_variable source expected = assert_equal ~ cmp : Type . Variable . equal ~ printer ( : Format . asprintf " % a " Type . Variable . pp ) ~ pp_diff ( : diff ~ print : Type . Variable . pp ) ( Type . Variable . dequalify ( Preprocessing . dequalify_map ( parse map ) ) source ) expected in assert_dequalify_variable ( Type . Variable . Unary ( Type . Variable . Unary . create " A . B . C " ) ) ( Type . Variable . Unary ( Type . Variable . Unary . create " C " ) ) ; assert_dequalify_variable ( Type . Variable . ParameterVariadic ( Type . Variable . Variadic . Parameters . create " A . B . C " ) ) ( Type . Variable . ParameterVariadic ( Type . Variable . Variadic . Parameters . create " C " ) ) ; ( ) |
let test_from_overloads _ = let assert_create ( ? aliases = Type . empty_aliases ) sources expected = let merged = let parse_callable source = match Type . create ~ aliases ( parse_single_expression source ) with | Type . Callable callable -> callable | _ -> failwith ( " Could not extract callable from " ^ source ) in sources |> List . map ~ f : parse_callable |> Type . Callable . from_overloads >>| ( fun callable -> Type . Callable callable ) |> Option . value ~ default : Type . Top in assert_equal ~ printer : Type . show ~ cmp : Type . equal ( Type . create ~ aliases ( parse_single_expression expected ) ) merged in assert_create [ " typing . Callable ( ' foo ' ) [ . . . , int ] " ; " typing . Callable ( ' foo ' ) [ . . . , str ] " ] " typing . Callable ( ' foo ' ) [ . . . , str ] " ; assert_create [ " typing . Callable ( ' foo ' ) [ . . . , int ] " ; " typing . Callable ( ' foo ' ) [ [ int , str ] , str ] " ; " typing . Callable ( ' foo ' ) [ [ int , str , str ] , int ] " ; ] " typing . Callable ( ' foo ' ) [ [ int , str , str ] , int ] " ; ( ) |
let test_with_return_annotation _ = let assert_with_return_annotation annotation callable expected = let callable = match Type . create ~ aliases : Type . empty_aliases ( parse_single_expression callable ) with | Type . Callable callable -> callable | _ -> failwith ( " Could not extract callable from " ^ callable ) in assert_equal ~ cmp : Type . equal ~ printer : Type . show ( Type . create ~ aliases : Type . empty_aliases ( parse_single_expression expected ) ) ( Type . Callable ( Type . Callable . with_return_annotation ~ annotation callable ) ) in assert_with_return_annotation Type . string " typing . Callable ( ' foo ' ) [ . . . , int ] " " typing . Callable ( ' foo ' ) [ . . . , str ] " ; assert_with_return_annotation Type . string " typing . Callable ( ' foo ' ) [ . . . , int ] [ [ [ int ] , int ] ] " " typing . Callable ( ' foo ' ) [ . . . , str ] [ [ [ int ] , str ] ] " |
let test_overload_parameters _ = let assert_parameters callable expected = let { Type . Callable . overloads ; _ } = Type . create ~ aliases : Type . empty_aliases ( parse_single_expression callable ) |> function | Type . Callable callable -> callable | _ -> failwith ( " Could not extract callable from " ^ callable ) in let parameters = List . hd_exn overloads |> Type . Callable . Overload . parameters |> Option . value ~ default [ ] : |> List . map ~ f ( : function | Type . Callable . Parameter . PositionalOnly { annotation ; _ } -> annotation | _ -> failwith " impossible " ) |> List . map ~ f : Type . show in assert_equal parameters expected in assert_parameters " typing . Callable ( ' foo ' ) [ . . . , Unknown ] [ [ [ int ] , str ] ] " [ " int " ] ; assert_parameters " typing . Callable ( ' foo ' ) [ . . . , Unknown ] [ [ [ int , str ] , str ] ] " [ " int " ; " str " ] ; assert_parameters " typing . Callable ( ' foo ' ) [ . . . , Unknown ] [ [ [ ] , str ] ] " [ ] |
let test_variables _ = let assert_variables source expected = let aliases = let aliases = Identifier . Map . of_alist_exn [ " T " , Type . variable " T " ; " S " , Type . variable " S " ] in Map . find aliases in let aliases = create_type_alias_table aliases in let variables = Type . create ~ aliases ( parse_single_expression source ) |> Type . Variable . all_free_variables |> List . filter_map ~ f ( : function | Type . Variable . Unary variable -> Some variable | _ -> None ) |> List . map ~ f ( : fun variable -> Type . Variable variable ) in assert_equal ( List . map expected ~ f : Type . variable ) variables in assert_variables " T " [ " T " ] ; assert_variables " Parametric [ int , T ] " [ " T " ] ; assert_variables " Parametric [ T , S ] " [ " T " ; " S " ] ; assert_variables " typing . Callable [ . . . , int ] " [ ] ; assert_variables " typing . Callable [ . . . , T ] " [ " T " ] ; assert_variables " typing . Callable [ [ T , int ] , str ] " [ " T " ] ; let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in let unary = Type . Variable . Unary . create " T " in assert_equal [ Type . Variable . Unary unary ; Type . Variable . ParameterVariadic parameter_variadic ] ( Type . Variable . all_free_variables ( Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation ( : Type . Variable unary ) ( ) ) ) ; ( ) |
let test_lambda _ = assert_true ( Type . equal ( parse_callable " typing . Callable [ [ Named ( x , str ) ] , int ] " ) ( Type . lambda ~ parameters [ " : x " , Type . string ] ~ return_annotation : Type . integer ) ) ; assert_true ( Type . equal ( parse_callable " typing . Callable [ [ Keywords ( str ) ] , int ] " ) ( Type . lambda ~ parameters [ " :** kwargs " , Type . string ] ~ return_annotation : Type . integer ) ) ; assert_true ( Type . equal ( parse_callable " typing . Callable [ [ Variable ( str ) ] , int ] " ) ( Type . lambda ~ parameters [ " :* args " , Type . string ] ~ return_annotation : Type . integer ) ) |
let test_visit _ = let create source = Type . create ~ aliases : Type . empty_aliases ( parse_single_expression source ) in let assert_types_equal annotation expected = assert_equal ~ printer : Type . show ~ cmp : Type . equal expected annotation in let module CountTransform = Type . Transform . Make ( struct type state = int let visit state _ = { Type . Transform . transformed_annotation = Type . integer ; new_state = state + 1 } let visit_children_before _ _ = true let visit_children_after = false end ) in let end_state , transformed = CountTransform . visit 0 ( create " typing . List [ int ] " ) in assert_types_equal transformed Type . integer ; assert_equal ~ printer : string_of_int 2 end_state ; let end_state , transformed = CountTransform . visit 0 ( create " Foo [ Bar [ Baz , Bop ] , Bang ] " ) in assert_types_equal transformed Type . integer ; assert_equal ~ printer : string_of_int 5 end_state ; let end_state , transformed = CountTransform . visit 0 ( create " typing . Literal [ test . MyEnum . ONE ] " ) in assert_types_equal transformed Type . integer ; assert_equal ~ printer : string_of_int 2 end_state ; let ts = Type . Variable . Variadic . Tuple . create " Ts " in let mixed_t = Type . OrderedTypes . Concatenation . create_from_unpackable ( Type . OrderedTypes . Concatenation . create_unpackable_from_concrete_against_concatenation ~ concrete [ : Type . literal_integer 1 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create ts ) ) in let end_state , _ = CountTransform . visit 0 ( Tuple ( Type . OrderedTypes . Concatenation mixed_t ) ) in assert_equal ~ printer : string_of_int 2 end_state ; let end_state , _ = CountTransform . visit 0 ( Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unpackable ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create ts ) ( Type . OrderedTypes . Concatenation . create ts ) ) ) ) ) in assert_equal ~ printer : string_of_int 1 end_state ; let end_state , _ = CountTransform . visit 0 ( Tuple ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unpackable ( Type . OrderedTypes . Concatenation . create_unpackable_from_concrete_against_concatenation ~ concrete [ : Type . literal_integer 1 ] ~ concatenation : mixed_t ) ) ) ) in assert_equal ~ printer : string_of_int 3 end_state ; let broadcast_unaries = Type . OrderedTypes . Concatenation . create_from_concrete_against_concrete ~ prefix [ ] : ~ suffix [ ] : ~ compare_t : Type . compare ~ left [ : Type . variable " T1 " ] ~ right [ : Type . variable " T2 " ] in let end_state , _ = CountTransform . visit 0 ( Tuple ( Type . OrderedTypes . Concatenation broadcast_unaries ) ) in assert_equal ~ printer : string_of_int 3 end_state ; let callable1 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . integer ] ) ~ annotation : Type . string ( ) in let callable2 = Type . Callable . create ~ parameters ( : make_callable_from_arguments [ Type . string ] ) ~ annotation : Type . bool ( ) in let end_state , _ = CountTransform . visit 0 ( TypeOperation ( Compose ( Type . OrderedTypes . Concrete [ callable1 ; callable2 ] ) ) ) in assert_equal ~ printer : string_of_int 7 end_state ; let end_state , _ = CountTransform . visit 0 ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) , 1 ) ; ] ) ; ] ) ) in assert_equal ~ printer : string_of_int 2 end_state ; let variable = Type . Variable . Unary . create " T " in let end_state , _ = CountTransform . visit 0 ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ 1 , [ Type . Monomial . create_variable variable , 1 ] ] ) ) in assert_equal ~ printer : string_of_int 1 end_state ; let variable = Type . Variable . Unary . create " T " in let end_state , _ = CountTransform . visit 0 ( Type . IntExpression . create ( Type . Polynomial . divide ~ compare_t : Type . compare ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) , 1 ) ; ] ) ; ] ) ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ 1 , [ Type . Monomial . create_variable variable , 1 ] ] ) ) ) in assert_equal ~ printer : string_of_int 2 end_state ; let end_state , _ = CountTransform . visit 0 ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) , 1 ) ; ] ) ; ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) , 1 ) ; ] ) ; ] ) ) in assert_equal ~ printer : string_of_int 3 end_state ; let end_state , _ = CountTransform . visit 0 ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . integer ) , 1 ) ; ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable Type . string ) , 1 ) ; ] ) ; ] ) ) in assert_equal ~ printer : string_of_int 3 end_state ; let module SubstitutionTransform = Type . Transform . Make ( struct type state = int let visit state annotation = let new_state , transformed_annotation = match annotation with | Type . Primitive integer when String . equal integer " int " && state > 0 -> state - 1 , Type . string | _ -> state , annotation in { Type . Transform . transformed_annotation ; new_state } let visit_children_before _ = function | Type . Union [ Type . NoneType ; _ ] | Type . Union [ _ ; Type . NoneType ] -> false | _ -> true let visit_children_after = false end ) in let end_state , transformed = SubstitutionTransform . visit 1 ( create " typing . Callable [ [ int ] , int ] " ) in assert_types_equal transformed ( create " typing . Callable [ [ str ] , int ] " ) ; assert_equal ~ printer : string_of_int 0 end_state ; let end_state , transformed = SubstitutionTransform . visit 1 ( create " typing . Callable [ [ typing . Optional [ int ] , int ] , int ] " ) in assert_types_equal transformed ( create " typing . Callable [ [ typing . Optional [ int ] , str ] , int ] " ) ; assert_equal ~ printer : string_of_int 0 end_state ; let module ConcatenateTransform = Type . Transform . Make ( struct type state = string let visit state annotation = let new_state , transformed_annotation = match annotation with | Type . Primitive primitive -> state ^ primitive , annotation | Type . Parametric { name ; parameters } -> " " , Type . parametric ( name ^ state ) parameters | _ -> state , annotation in { Type . Transform . transformed_annotation ; new_state } let visit_children_before _ _ = true let visit_children_after = false end ) in let end_state , transformed = ConcatenateTransform . visit " " ( create " Foo [ Bar [ Baz , Bop ] , Bro [ Loop , typing . Optional [ Land ] ] ] " ) in assert_types_equal transformed ( create " Foo [ BarBazBop [ Baz , Bop ] , BroLoopLand [ Loop , typing . Optional [ Land ] ] ] " ) ; assert_equal " " end_state ; let module TopDownConcatenateTransform = Type . Transform . Make ( struct type state = string let visit state annotation = let new_state , transformed_annotation = match annotation with | Type . Primitive primitive -> " " , Type . Primitive ( state ^ primitive ) | Type . Parametric { name ; parameters } -> state ^ name , Type . parametric name parameters | _ -> state , annotation in { Type . Transform . transformed_annotation ; new_state } let visit_children_before _ _ = false let visit_children_after = true end ) in let end_state , transformed = TopDownConcatenateTransform . visit " " ( create " Foo [ Bar [ Bro [ typing . Optional [ Land ] ] ] ] " ) in assert_types_equal transformed ( create " Foo [ Bar [ Bro [ typing . Optional [ FooBarBroLand ] ] ] ] " ) ; assert_equal " " end_state ; ( ) |
let test_collapse_escaped_variable_unions _ = let assert_types_equal annotation expected = assert_equal ~ printer : Type . show ~ cmp : Type . equal expected annotation in let escaped = Type . Variable . mark_all_free_variables_as_escaped ( Type . variable " Escapee " ) in assert_types_equal ( Type . union [ Type . integer ; escaped ] ) ( Type . Union [ Type . integer ; escaped ] ) ; assert_types_equal ( Type . Variable . collapse_all_escaped_variable_unions ( Type . Union [ Type . integer ; escaped ] ) ) Type . integer ; let unescaped = Type . variable " NotEscaped " in assert_types_equal ( Type . Variable . collapse_all_escaped_variable_unions ( Type . Union [ Type . integer ; unescaped ] ) ) ( Type . Union [ Type . integer ; unescaped ] ) ; ( ) |
let test_namespace_insensitive_compare _ = let no_namespace_variable = Type . Variable . Unary . create " A " in let namespaced_variable_1 = let namespace = Type . Variable . Namespace . create_fresh ( ) in Type . Variable { no_namespace_variable with namespace } in let namespaced_variable_2 = let namespace = Type . Variable . Namespace . create_fresh ( ) in Type . Variable { no_namespace_variable with namespace } in assert_false ( Type . compare namespaced_variable_1 namespaced_variable_2 == 0 ) ; assert_equal ( Type . namespace_insensitive_compare namespaced_variable_1 namespaced_variable_2 ) 0 ; assert_equal ( Type . namespace_insensitive_compare ( Type . list namespaced_variable_1 ) ( Type . list namespaced_variable_2 ) ) 0 ; ( ) |
let test_namespace _ = let no_namespace_variable = Type . Variable . Unary . create " A " in let namespaced_variable_1 = let namespace = Type . Variable . Namespace . create_fresh ( ) in Type . Variable . Unary { no_namespace_variable with namespace } in let namespace_2 = Type . Variable . Namespace . create_fresh ( ) in let namespaced_variable_2 = Type . Variable . Unary { no_namespace_variable with namespace = namespace_2 } in assert_equal ( Type . Variable . namespace namespaced_variable_1 ~ namespace : namespace_2 ) namespaced_variable_2 ; ( ) |
let test_mark_all_variables_as_bound _ = let variable = Type . Variable ( Type . Variable . Unary . create " T " ) in assert_false ( Type . Variable . all_variables_are_resolved variable ) ; let variable = Type . Variable . mark_all_variables_as_bound variable in assert_true ( Type . Variable . all_variables_are_resolved variable ) ; let callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in assert_false ( Type . Variable . all_variables_are_resolved callable ) ; let callable = Type . Variable . mark_all_variables_as_bound callable in assert_true ( Type . Variable . all_variables_are_resolved callable ) ; ( ) |
let test_mark_all_variables_as_free _ = let variable = Type . Variable ( Type . Variable . Unary . create " T " ) |> Type . Variable . mark_all_variables_as_bound in assert_true ( Type . Variable . all_variables_are_resolved variable ) ; let variable = Type . Variable . mark_all_variables_as_free variable in assert_false ( Type . Variable . all_variables_are_resolved variable ) ; let callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) |> Type . Variable . mark_all_variables_as_bound in assert_true ( Type . Variable . all_variables_are_resolved callable ) ; let callable = Type . Variable . mark_all_variables_as_free callable in assert_false ( Type . Variable . all_variables_are_resolved callable ) ; ( ) |
let test_namespace_all_free_variables _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let bound_variable = Type . Variable . Unary . create " T2 " |> Type . Variable . Unary . mark_as_bound |> fun variable -> Type . Variable variable in let annotation = Type . parametric " p " [ ! free_variable ; bound_variable ] in let namespace = Type . Variable . Namespace . create_fresh ( ) in let namespaced_free = Type . Variable . Unary . create " T " |> Type . Variable . Unary . namespace ~ namespace |> fun variable -> Type . Variable variable in assert_equal ( Type . Variable . namespace_all_free_variables annotation ~ namespace ) ( Type . parametric " p " [ ! namespaced_free ; bound_variable ] ) ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let bound_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . mark_as_bound in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let annotation = Type . parametric " p " [ ! free_variable_callable ; bound_variable_callable ] in let namespace = Type . Variable . Namespace . create_fresh ( ) in let namespaced_free_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . namespace ~ namespace in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in assert_equal ( Type . Variable . namespace_all_free_variables annotation ~ namespace ) ( Type . parametric " p " [ ! namespaced_free_callable ; bound_variable_callable ] ) ; ( ) |
let test_mark_all_free_variables_as_escaped _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let bound_variable = Type . Variable . Unary . create " T2 " |> Type . Variable . Unary . mark_as_bound |> fun variable -> Type . Variable variable in let annotation = Type . parametric " p " [ ! free_variable ; bound_variable ] in Type . Variable . Namespace . reset ( ) ; let escaped_free = let namespace = Type . Variable . Namespace . create_fresh ( ) in Type . Variable . Unary . create " T " |> Type . Variable . Unary . mark_as_escaped |> Type . Variable . Unary . namespace ~ namespace |> fun variable -> Type . Variable variable in Type . Variable . Namespace . reset ( ) ; assert_equal ( Type . Variable . mark_all_free_variables_as_escaped annotation ) ( Type . parametric " p " [ ! escaped_free ; bound_variable ] ) ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let bound_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . mark_as_bound in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let annotation = Type . parametric " p " [ ! free_variable_callable ; bound_variable_callable ] in Type . Variable . Namespace . reset ( ) ; let escaped_free_callable = let namespace = Type . Variable . Namespace . create_fresh ( ) in let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . mark_as_escaped |> Type . Variable . Variadic . Parameters . namespace ~ namespace in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in Type . Variable . Namespace . reset ( ) ; assert_equal ( Type . Variable . mark_all_free_variables_as_escaped annotation ) ( Type . parametric " p " [ ! escaped_free_callable ; bound_variable_callable ] ) ; ( ) |
let test_contains_escaped_free_variable _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in assert_false ( Type . Variable . contains_escaped_free_variable free_variable ) ; let escaped_free = Type . Variable . Unary . create " T " |> Type . Variable . Unary . mark_as_escaped |> fun variable -> Type . Variable variable in assert_true ( Type . Variable . contains_escaped_free_variable escaped_free ) ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in assert_false ( Type . Variable . contains_escaped_free_variable free_variable_callable ) ; let escaped_free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . mark_as_escaped in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in assert_true ( Type . Variable . contains_escaped_free_variable escaped_free_variable_callable ) ; ( ) |
let polynomial_create_from_variables_list = Type . Polynomial . create_from_variables_list ~ compare_t : Type . compare |
let test_convert_all_escaped_free_variables_to_anys _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let escaped_free = Type . Variable . Unary . create " T " |> Type . Variable . Unary . mark_as_escaped |> fun variable -> Type . Variable variable in let annotation = Type . parametric " p " [ ! free_variable ; escaped_free ] in assert_equal ( Type . Variable . convert_all_escaped_free_variables_to_anys annotation ) ( Type . parametric " p " [ ! free_variable ; Type . Any ] ) ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let escaped_free_callable = let namespace = Type . Variable . Namespace . create_fresh ( ) in let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " |> Type . Variable . Variadic . Parameters . mark_as_escaped |> Type . Variable . Variadic . Parameters . namespace ~ namespace in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let annotation = Type . parametric " p " [ ! free_variable_callable ; escaped_free_callable ] in let any_callable = Type . Callable . create ~ parameters : Type . Callable . Undefined ~ annotation : Type . integer ( ) in assert_equal ( Type . Variable . convert_all_escaped_free_variables_to_anys annotation ) ( Type . parametric " p " [ ! free_variable_callable ; any_callable ] ) ; ( ) |
let test_int_expression_create _ = let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let assert_create_list_type given expected = assert_equal ~ printer [ :% show : Type . type_t ] ~ cmp [ :% equal : Type . type_t ] ( Type . IntExpression . create ( polynomial_create_from_variables_list given ) ) expected in let assert_create_list_list given expected = match Type . IntExpression . create ( polynomial_create_from_variables_list given ) with | Type . IntExpression ( Data result_polynomial ) -> assert_equal ~ printer [ :% show : Type . type_t Type . Polynomial . t ] ~ cmp [ :% equal : Type . type_t Type . Polynomial . t ] result_polynomial ( polynomial_create_from_variables_list expected ) | _ -> assert false in assert_create_list_list [ 1 , [ x , 1 ] ; 1 , [ ] ; - 1 , [ ] ] [ 1 , [ x , 1 ] ; 1 , [ ] ; - 1 , [ ] ] ; assert_create_list_list [ 1 , [ y , 1 ; y , 1 ] ] [ 1 , [ y , 1 ; y , 1 ] ] ; assert_create_list_type [ 7 , [ ] ] ( Type . literal_integer 7 ) ; assert_create_list_type [ ] ( Type . literal_integer 0 ) ; assert_create_list_type [ 1 , [ x , 1 ] ] ( Type . Variable x ) ; assert_create_list_list [ 0 , [ x , 1 ] ; 0 , [ ] ; 1 , [ x , 1 ; y , 1 ] ] [ 1 , [ x , 1 ; y , 1 ] ] ; assert_create_list_list [ 1 , [ x , 1 ] ; 1 , [ y , 1 ] ] [ 1 , [ y , 1 ] ; 1 , [ x , 1 ] ] ; assert_create_list_list [ 1 , [ x , 1 ; y , 1 ] ] [ 1 , [ y , 1 ; x , 1 ] ] ; ( ) |
let test_replace_all _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let annotation = Type . parametric " p " [ ! free_variable ; Type . integer ] in let assert_equal actual expected = assert_equal ~ cmp : Type . equal ~ printer : Type . show expected actual in assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . integer ) annotation ) ( Type . parametric " p " [ ! Type . integer ; Type . integer ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . integer ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation free_variable ) ) ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation Type . integer ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> None ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation free_variable ) ) ) ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation free_variable ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun variable -> Some ( Type . Annotated ( Type . Variable variable ) ) ) free_variable ) ( Type . annotated free_variable ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . float ) ( Type . union [ Type . literal_integer 2 ; Type . integer ; free_variable ] ) ) ( Type . union [ Type . literal_integer 2 ; Type . integer ; Type . float ] ) ; let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in let w = Type . Variable . Unary . create " w " in let variable_list_to_type variable_list = polynomial_create_from_variables_list variable_list |> Type . polynomial_to_type in let divide_to_type numerator denominator = Type . IntExpression . create ( Type . Polynomial . divide ~ compare_t : Type . compare numerator denominator ) in assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . Any ) ( variable_list_to_type [ 1 , [ ] ; 1 , [ x , 1 ] ] ) ) Type . Any ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . integer ) ( variable_list_to_type [ 1 , [ ] ; 1 , [ x , 1 ] ] ) ) Type . integer ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 7 ) ) ( divide_to_type ( Type . Polynomial . create_from_int 7 ) ( Type . Polynomial . create_from_variable x ) ) ) ( Type . IntExpression . create ( polynomial_create_from_variables_list [ 1 , [ ] ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 6 ) ) ( divide_to_type ( Type . Polynomial . create_from_int 10 ) ( Type . Polynomial . create_from_variable x ) ) ) ( Type . IntExpression . create ( polynomial_create_from_variables_list [ 1 , [ ] ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when [ % equal : Type . Variable . Unary . t ] variable x -> Some ( divide_to_type ( Type . Polynomial . create_from_int 7 ) ( polynomial_create_from_variables_list [ 1 , [ y , 1 ] ] ) ) | _ -> None ) ( variable_list_to_type [ 1 , [ x , 1 ; y , 1 ] ] ) ) ( Type . IntExpression . create ( Type . Polynomial . multiply ~ compare_t : Type . compare ( Type . Polynomial . create_from_variable y ) ( Type . Polynomial . divide ~ compare_t : Type . compare ( Type . Polynomial . create_from_int 7 ) ( Type . Polynomial . create_from_variable y ) ) ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when not ( [ % equal : Type . Variable . Unary . t ] variable z ) -> Some ( Type . Variable z ) | _ -> None ) ( Type . IntExpression . create ( Type . Polynomial . add ~ compare_t : Type . compare ( Type . Polynomial . divide ~ compare_t : Type . compare ( polynomial_create_from_variables_list [ 7 , [ x , 1 ] ] ) ( polynomial_create_from_variables_list [ 1 , [ z , 3 ] ] ) ) ( Type . Polynomial . divide ~ compare_t : Type . compare ( Type . Polynomial . create_from_int 7 ) ( polynomial_create_from_variables_list [ 1 , [ y , 1 ; z , 1 ] ] ) ) ) ) ) ( Type . IntExpression . create ( Type . Polynomial . multiply ~ compare_t : Type . compare ( Type . Polynomial . create_from_int 2 ) ( Type . Polynomial . divide ~ compare_t : Type . compare ( Type . Polynomial . create_from_int 7 ) ( polynomial_create_from_variables_list [ 1 , [ z , 2 ] ] ) ) ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when [ % equal : Type . Variable . Unary . t ] variable x -> Some ( Type . Variable y ) | _ -> None ) ( Type . IntExpression . create ( Type . Polynomial . add ~ compare_t : Type . compare ( Type . Polynomial . create_from_int 1 ) ( Type . Polynomial . divide ~ compare_t : Type . compare ( Type . Polynomial . create_from_variable x ) ( Type . Polynomial . create_from_variable y ) ) ) ) ) ( Type . IntExpression . create ( polynomial_create_from_variables_list [ 2 , [ ] ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 0 ) ) ( divide_to_type ( Type . Polynomial . create_from_int 7 ) ( Type . Polynomial . create_from_variable x ) ) ) Type . Bottom ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 4 ) ) ( variable_list_to_type [ 2 , [ x , 3 ] ; 3 , [ y , 1 ; z , 1 ] ] ) ) ( Type . IntExpression . create ( polynomial_create_from_variables_list [ 176 , [ ] ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when [ % equal : Type . Variable . Unary . t ] variable x -> Some ( Type . literal_integer 5 ) | _ -> None ) ( variable_list_to_type [ 3 , [ x , 2 ] ; 5 , [ y , 1 ] ] ) ) ( variable_list_to_type [ 75 , [ ] ; 5 , [ y , 1 ] ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( variable_list_to_type [ 3 , [ x , 2 ] ; 5 , [ y , 2 ; z , 7 ] ] ) ) ( variable_list_to_type [ 15150 , [ ] ] ) ) ( variable_list_to_type [ 15150 , [ ] ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some Type . integer ) ( variable_list_to_type [ 2 , [ x , 3 ] ] ) ) Type . integer ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . Variable x ) ) ( variable_list_to_type [ 2 , [ x , 1 ; y , 2 ] ; 3 , [ x , 2 ; y , 1 ] ] ) ) ( variable_list_to_type [ 5 , [ x , 3 ] ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( variable_list_to_type [ 3 , [ y , 2 ; z , 1 ] ; 5 , [ y , 1 ; z , 3 ] ] ) ) ( variable_list_to_type [ 2 , [ x , 2 ] ; 5 , [ x , 1 ] ] ) ) ( variable_list_to_type [ 18 , [ y , 4 ; z , 2 ] ; 60 , [ y , 3 ; z , 4 ] ; 50 , [ y , 2 ; z , 6 ] ; 15 , [ y , 2 ; z , 1 ] ; 25 , [ y , 1 ; z , 3 ] ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( variable_list_to_type [ 3 , [ w , 1 ] ; 1 , [ z , 2 ] ] ) ) ( variable_list_to_type [ 2 , [ x , 1 ] ; 1 , [ y , 1 ] ] ) ) ( variable_list_to_type [ 9 , [ w , 1 ] ; 3 , [ z , 2 ] ] ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( variable_list_to_type [ 1 , [ x , 1 ; y , 1 ] ] ) ) ( variable_list_to_type [ 1 , [ x , 1 ] ; 1 , [ y , 1 ] ] ) ) ( variable_list_to_type [ 1 , [ x , 1 ; y , 1 ] ; 1 , [ x , 2 ; y , 1 ] ] ) ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 7 ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concrete_against_concatenation ~ concrete [ : Type . literal_integer 2 ; Type . Variable x ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) , 1 ) ; ] ) ; ] ) ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concrete_against_concatenation ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 7 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) , 1 ) ; ] ) ; ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 7 ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : Type . literal_integer 2 ; Type . Variable y ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : Type . literal_integer 2 ; Type . Variable x ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 7 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ ] : ~ suffix [ ] : ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 7 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when [ % equal : Type . Variable . Unary . t ] variable x -> Some ( Type . literal_integer 1 ) | _ -> Some ( Type . literal_integer 2 ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . Variable x ] ~ suffix [ : Type . Variable y ] ~ concrete [ : Type . literal_integer 2 ; Type . Variable y ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . Variable y ] ~ suffix [ : Type . Variable x ] ~ concrete [ : Type . literal_integer 2 ; Type . Variable x ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . literal_integer 1 ] ~ suffix [ : Type . literal_integer 2 ] ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 2 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . literal_integer 2 ] ~ suffix [ : Type . literal_integer 1 ] ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 1 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( fun _ -> Some ( Type . literal_integer 7 ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable ( Type . tuple [ Type . Variable x ; Type . Variable y ; Type . literal_integer 2 ] ) ) , 1 ) ; ] ) ; ] ) ) ) ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable ( Type . tuple [ Type . literal_integer 7 ; Type . literal_integer 7 ; Type . literal_integer 2 ] ) ) , 1 ) ; ] ) ; ] ) ) ; assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all ( function | variable when [ % equal : Type . Variable . Unary . t ] variable x -> Some ( Type . literal_integer 1 ) | _ -> Some ( Type . literal_integer 2 ) ) ( divide_to_type ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . Variable x ] ~ suffix [ : Type . Variable y ] ~ concrete [ : Type . literal_integer 2 ; Type . Variable y ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . Variable y ] ~ suffix [ : Type . Variable x ] ~ concrete [ : Type . literal_integer 2 ; Type . Variable x ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable ( Type . tuple [ Type . Variable x ; Type . Variable y ; Type . literal_integer 2 ] ) ) , 1 ) ; ] ) ; ] ) ) ) ( divide_to_type ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable_from_concatenation_against_concatenation ~ compare_t : Type . compare ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . literal_integer 1 ] ~ suffix [ : Type . literal_integer 2 ] ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 2 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ( Type . OrderedTypes . Concatenation . create_from_concrete_against_concatenation ~ prefix [ : Type . literal_integer 2 ] ~ suffix [ : Type . literal_integer 1 ] ~ concrete [ : Type . literal_integer 2 ; Type . literal_integer 1 ] ~ concatenation ( : Type . OrderedTypes . Concatenation . create variadic ) ) ) , 1 ) ; ] ) ; ] ) ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable ( Type . tuple [ Type . literal_integer 1 ; Type . literal_integer 2 ; Type . literal_integer 2 ] ) ) , 1 ) ; ] ) ; ] ) ) ; let unary1 = Type . Variable . Unary . create ~ constraints ( : Bound Type . integer ) " T1 " in let unary2 = Type . Variable . Unary . create ~ constraints ( : Bound Type . integer ) " T2 " in let unary3 = Type . Variable . Unary . create ~ constraints ( : Bound Type . integer ) " T3 " in let unary4 = Type . Variable . Unary . create ~ constraints ( : Bound Type . integer ) " T4_not_replaced " in let assert_replaced ~ replace annotation expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " T1 " -> Some ( Type . TypeAlias ( Type . Variable unary1 ) ) | " T2 " -> Some ( Type . TypeAlias ( Type . Variable unary2 ) ) | " T3 " -> Some ( Type . TypeAlias ( Type . Variable unary3 ) ) | " T4_not_replaced " -> Some ( Type . TypeAlias ( Type . Variable unary4 ) ) | _ -> None in let parse annotation = parse_single_expression ~ preprocess : true annotation in assert_equal ( Type . Variable . GlobalTransforms . Unary . replace_all replace ( Type . create ~ aliases ( parse annotation ) ) ) ( Type . create ~ aliases ( parse expected ) ) in let replace_with_literals = function | variable when Type . Variable . Unary . equal variable unary1 -> Some ( Type . literal_integer 5 ) | variable when Type . Variable . Unary . equal variable unary2 -> Some ( Type . literal_integer 5 ) | variable when Type . Variable . Unary . equal variable unary3 -> Some ( Type . literal_integer 42 ) | _ -> None in assert_replaced ~ replace : replace_with_literals { | pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , T1 ] , ] } | " typing . Tuple [ typing_extensions . Literal [ 5 ] , typing_extensions . Literal [ 5 ] ] " ; assert_replaced ~ replace : replace_with_literals { | pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , T3 ] , ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 5 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 42 ] ] ] " ; assert_replaced ~ replace : replace_with_literals { | typing . Tuple [ typing_extensions . Literal [ 99 ] , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , T1 ] , ] ] , typing_extensions . Literal [ 99 ] , ] } | " typing . Tuple [ typing_extensions . Literal [ 99 ] , typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 5 ] , typing_extensions . Literal [ 99 ] ] " ; assert_replaced ~ replace : replace_with_literals { | typing . Tuple [ typing_extensions . Literal [ 99 ] , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , typing_extensions . Literal [ 99 ] ] , ] ] , typing_extensions . Literal [ 99 ] , ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 5 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 99 ] ] ] " ; assert_replaced ~ replace : replace_with_literals { | typing . Tuple [ typing_extensions . Literal [ 99 ] , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ T1 , T2 ] , typing . Tuple [ T2 , T4_not_replaced ] , ] ] , typing_extensions . Literal [ 99 ] , ] } | " typing . Tuple [ typing_extensions . Literal [ 99 ] , \ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 5 ] , \ typing_extensions . Literal [ 5 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] , T4_not_replaced ] ] ] , \ typing_extensions . Literal [ 99 ] ] " ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in let no_parameter_callable = Type . Callable . create ~ parameters ( : Type . Callable . Defined [ ] ) ~ annotation : Type . integer ( ) in assert_equal ( Type . Variable . GlobalTransforms . ParameterVariadic . replace_all ( fun _ -> Some ( Type . Callable . Defined [ ] ) ) ( Type . parametric " p " [ ! Type . integer ; free_variable_callable ] ) ) ( Type . parametric " p " [ ! Type . integer ; no_parameter_callable ] ) ; assert_equal ( Type . Variable . GlobalTransforms . ParameterVariadic . replace_all ( fun _ -> Some ( Type . Callable . Defined [ Named { name = " p " ; annotation = Type . integer ; default = false } ] ) ) ( Type . parametric " G " [ CallableParameters ( Type . Variable . Variadic . Parameters . self_reference ( Type . Variable . Variadic . Parameters . create " TParams " ) ) ; ] ) ) ( Type . parametric " G " [ CallableParameters ( Defined [ Named { name = " p " ; annotation = Type . integer ; default = false } ] ) ; ] ) ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let assert_replaced ~ replace annotation expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None in assert_equal ( Type . Variable . GlobalTransforms . TupleVariadic . replace_all replace ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true annotation ) ) ) ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true expected ) ) in let replace_with_concrete given = Option . some_if ( Type . Variable . Variadic . Tuple . equal given variadic ) ( Type . OrderedTypes . Concrete [ Type . bool ; Type . bool ] ) in let replace_with_concatenation _ = Some ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . bool ] ~ suffix [ : Type . bool ] variadic ) ) in assert_replaced ~ replace : replace_with_concrete " Foo [ int , pyre_extensions . Unpack [ Ts ] , str ] " " Foo [ int , bool , bool , str ] " ; assert_replaced ~ replace : replace_with_concatenation " Foo [ int , pyre_extensions . Unpack [ Ts ] , str ] " " Foo [ int , bool , pyre_extensions . Unpack [ Ts ] , bool , str ] " ; assert_replaced ~ replace : replace_with_concrete " Foo [ Bar [ int , pyre_extensions . Unpack [ Ts ] , str ] , Bar [ int , pyre_extensions . Unpack [ Ts2 ] , str ] ] " " Foo [ Bar [ int , bool , bool , str ] , Bar [ int , pyre_extensions . Unpack [ Ts2 ] , str ] ] " ; assert_replaced ~ replace : replace_with_concrete " typing . Tuple [ int , str ] " " typing . Tuple [ int , str ] " ; assert_replaced ~ replace : replace_with_concrete " typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] " " typing . Tuple [ int , bool , bool , str ] " ; assert_replaced ~ replace : replace_with_concatenation " typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] " " typing . Tuple [ int , bool , pyre_extensions . Unpack [ Ts ] , bool , str ] " ; assert_replaced ~ replace : replace_with_concrete " typing . Callable [ [ int , pyre_extensions . Unpack [ Ts ] , str ] , None ] " " typing . Callable [ [ int , bool , bool , str ] , None ] " ; assert_replaced ~ replace : replace_with_concatenation " typing . Callable [ [ int , pyre_extensions . Unpack [ Ts ] , str ] , None ] " " typing . Callable [ [ int , bool , pyre_extensions . Unpack [ Ts ] , bool , str ] , None ] " ; let replace_with_concrete = function | variable when Type . Variable . Variadic . Tuple . equal variable variadic -> Some ( Type . OrderedTypes . Concrete [ Type . literal_integer 5 ] ) | variable when Type . Variable . Variadic . Tuple . equal variable variadic2 -> Some ( Type . OrderedTypes . Concrete [ Type . literal_integer 1 ; Type . literal_integer 4 ] ) | _ -> None in let replace_with_concatenation = function | variable when Type . Variable . Variadic . Tuple . equal variable variadic -> Some ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element Type . integer ) ) | variable when Type . Variable . Variadic . Tuple . equal variable variadic2 -> Some ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) | _ -> None in assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] , ] } | " typing . Tuple [ typing_extensions . Literal [ 5 ] ] " ; assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] , ] } | " typing . Tuple [ int , . . . ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | { | typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 5 ] , ] } ; | assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | " typing . Tuple [ typing_extensions . Literal [ 5 ] ] " ; assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] } | " typing . Tuple [ int , . . . ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] , ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 1 ] , \ typing_extensions . Literal [ 4 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 6 ] , typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 5 ] , ] , pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] , typing_extensions . Literal [ 5 ] ] , ] ] } | { | typing . Tuple [ typing_extensions . Literal [ 6 ] , typing_extensions . Literal [ 4 ] , typing_extensions . Literal [ 5 ] , ] } ; | assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 6 ] , typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 5 ] , ] , pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] , ] ] } | { | pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 6 ] , typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 5 ] ] , pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ int , . . . ] ] ] } ; | assert_replaced ~ replace : replace_with_concrete { | Foo [ int , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] } | " Foo [ int , typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 5 ] ] " ; assert_replaced ~ replace : replace_with_concatenation { | Foo [ int , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] } | " Foo [ int , pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] " ; assert_replaced ~ replace : replace_with_concrete { | Foo [ int , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] " ; assert_replaced ~ replace : replace_with_concrete { | List [ Tensor [ int , pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] , ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] " ; assert_replaced ~ replace : replace_with_concrete { | typing . Callable [ [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] , int ] } | { | typing . Callable [ [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 5 ] ] , int ] } ; | assert_replaced ~ replace : replace_with_concatenation { | typing . Callable [ [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] , int ] } | { | typing . Callable [ [ pyre_extensions . Unpack [ typing . Tuple [ int , . . . ] ] ] , int ] } ; | assert_replaced ~ replace : replace_with_concrete { | typing . Callable [ [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] ] , int ] } | { | pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] } ; | assert_replaced ~ replace : replace_with_concrete { | typing . Callable [ [ Named ( x , pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ) ] , int ] } | { | typing . Callable [ [ Named ( x , pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] ) ] , int ] } ; | assert_replaced ~ replace : replace_with_concrete { | typing . Callable [ [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , ] ] , int ] } | { | typing . Callable [ [ pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 5 ] ] ] ] , int ] } ; | let parse_string string = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None in Type . create ~ aliases ( parse_single_expression ~ preprocess : true string ) in let replace_with_concrete = function | variable when Type . Variable . Variadic . Tuple . equal variable variadic -> Some ( Type . OrderedTypes . Concrete [ parse_string " typing . Callable [ [ int ] , str ] " ; parse_string " typing . Callable [ [ str ] , bool ] " ; ] ) | variable when Type . Variable . Variadic . Tuple . equal variable variadic2 -> Some ( Type . OrderedTypes . Concrete [ parse_string " typing . Callable [ [ float ] , bytes ] " ; parse_string " typing . Callable [ [ bytes ] , int ] " ; ] ) | _ -> None in let replace_with_concatenation = function | variable when Type . Variable . Variadic . Tuple . equal variable variadic -> Some ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create_from_unbounded_element ( parse_string " typing . Callable [ [ int ] , int ] " ) ) ) | variable when Type . Variable . Variadic . Tuple . equal variable variadic2 -> Some ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create variadic ) ) | _ -> None in assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Compose [ typing . Callable [ [ float ] , int ] , pyre_extensions . Unpack [ Ts ] ] } | " pyre_extensions . Compose [ typing . Callable [ [ float ] , int ] , typing . Callable [ [ int ] , str ] , \ typing . Callable [ [ str ] , bool ] ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Compose [ pyre_extensions . Compose [ pyre_extensions . Unpack [ Ts ] , typing . Callable [ [ bool ] , bool ] ] , typing . Callable [ [ bool ] , float ] , ] } | { | pyre_extensions . Compose [ typing . Callable [ [ int ] , str ] , typing . Callable [ [ str ] , bool ] , typing . Callable [ [ bool ] , bool ] , typing . Callable [ [ bool ] , float ] ] } ; | assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , pyre_extensions . Unpack [ Ts ] ] } | { | pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , pyre_extensions . Unpack [ typing . Tuple [ typing . Callable [ [ int ] , int ] , . . . ] ] ] } ; | assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , pyre_extensions . Unpack [ Ts2 ] ] } | { | pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , pyre_extensions . Unpack [ Ts ] ] } ; | ( ) |
let test_product_replace_variadic _ = let assert_equal actual expected = assert_equal ~ cmp : Type . equal ~ printer : Type . show expected actual in let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let variable = Type . Variable . Unary . create " N " in let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | " N " -> Some ( TypeAlias ( Type . Variable variable ) ) | _ -> None in let assert_replaced_type ~ replace annotation expected = assert_equal ( Type . Variable . GlobalTransforms . TupleVariadic . replace_all replace ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true annotation ) ) ) expected in let assert_replaced ~ replace annotation expected = assert_replaced_type ~ replace annotation ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true expected ) ) in let replace_with_concrete given = Option . some_if ( Type . Variable . Variadic . Tuple . equal given variadic ) ( Type . OrderedTypes . Concrete [ Type . literal_integer 2 ; Type . literal_integer 3 ] ) in let replace_with_concatenation given = Option . some_if ( Type . Variable . Variadic . Tuple . equal given variadic ) ( Type . OrderedTypes . Concatenation ( Type . OrderedTypes . Concatenation . create ~ prefix [ : Type . literal_integer 2 ] ~ suffix [ ] : variadic2 ) ) in assert_replaced ~ replace : replace_with_concrete " pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] ] " " typing_extensions . Literal [ 6 ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 4 ] ] } | " typing_extensions . Literal [ 48 ] " ; assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 4 ] ] } | { | pyre_extensions . Product [ typing_extensions . Literal [ 16 ] , pyre_extensions . Unpack [ Ts2 ] ] } ; | assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 1 ] ] ] } | " typing_extensions . Literal [ 36 ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] } | " typing_extensions . Literal [ 6 ] " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ typing . Tuple [ pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 1 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] , . . . ] ] ] } | " int " ; let replace_with_ones given = Option . some_if ( Type . Variable . Variadic . Tuple . equal given variadic ) ( Type . OrderedTypes . Concrete [ Type . literal_integer 1 ; Type . literal_integer 1 ] ) in assert_replaced ~ replace : replace_with_ones { | pyre_extensions . Product [ pyre_extensions . Unpack [ typing . Tuple [ pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 1 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] , . . . ] ] ] } | " int " ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 3 ] ] ] " ; assert_replaced ~ replace : replace_with_concatenation { | pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] } | { | pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , pyre_extensions . Unpack [ Ts2 ] ] ] ] ] } ; | assert_replaced_type ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 6 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unpackable variadic2 ) , 2 ) ; ] ) ; ] ) ) ; let replace_with_both = function | variable when Type . Variable . Variadic . Tuple . equal variable variadic -> Some ( Type . OrderedTypes . Concrete [ Type . literal_integer 1 ; Type . literal_integer 4 ] ) | variable when Type . Variable . Variadic . Tuple . equal variable variadic2 -> Some ( Type . OrderedTypes . Concrete [ Type . literal_integer 5 ; Type . literal_integer 2 ] ) | _ -> None in assert_replaced_type ~ replace : replace_with_both { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] ] ] } | ( Type . literal_integer 400 ) ; assert_replaced_type ~ replace : replace_with_both { | pyre_extensions . Add [ pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , ] ] } | ( Type . literal_integer 200 ) ; assert_replaced_type ~ replace : replace_with_both { | pyre_extensions . Add [ pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] , ] ] } | ( Type . literal_integer 200 ) ; assert_replaced_type ~ replace : replace_with_both { | pyre_extensions . Product [ N , pyre_extensions . Unpack [ Ts2 ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] ] } | ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ 100 , [ Type . Monomial . create_variable variable , 1 ] ] ) ) ; assert_replaced ~ replace : replace_with_concrete { | pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] } | " pyre_extensions . BroadcastError [ typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 2 ] ] , typing . Tuple [ typing_extensions . Literal [ 2 ] , \ typing_extensions . Literal [ 3 ] ] ] " ; assert_replaced ~ replace : replace_with_both { | pyre_extensions . Divide [ pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 3 ] , typing_extensions . Literal [ 4 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] ] } | " typing_extensions . Literal [ 1 ] " ; assert_replaced ~ replace : replace_with_both { | pyre_extensions . Divide [ pyre_extensions . Product [ pyre_extensions . Divide [ pyre_extensions . Product [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 4 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] , typing_extensions . Literal [ 4 ] ] , typing_extensions . Literal [ 8 ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] ] ] } | " typing_extensions . Literal [ 1 ] " ; ( ) |
let test_less_or_equal _ = let assert_solve ~ left ~ right expected = let impossible = [ ] in let rec solve ~ left ~ right = match left , right with | Type . Variable variable1 , Type . Variable variable2 -> List . sort [ [ variable1 , Type . Variable variable2 ] ; [ variable2 , Type . Variable variable1 ] ] ~ compare [ :% compare : ( Type . Variable . Unary . t * Type . t ) list ] | Type . Variable variable , bound | bound , Type . Variable variable -> [ [ variable , bound ] ] | IntExpression _ , _ | _ , IntExpression _ -> Type . solve_less_or_equal_polynomial ~ left ~ right ~ solve ~ impossible | _ when Type . equal left right -> [ [ ] ] | _ -> impossible in assert_equal ~ cmp [ :% equal : ( Type . t Type . Variable . Unary . record * Type . t ) list list ] ~ printer [ :% show : ( Type . t Type . Variable . Unary . record * Type . t ) list list ] expected ( solve ~ left ~ right ) ; assert_equal ~ cmp [ :% equal : ( Type . t Type . Variable . Unary . record * Type . t ) list list ] ~ printer [ :% show : ( Type . t Type . Variable . Unary . record * Type . t ) list list ] expected ( solve ~ left : right ~ right : left ) in let assert_impossible ~ left ~ right = assert_solve ~ left ~ right [ ] in let x = Type . Variable . Unary . create " x " in let y = Type . Variable . Unary . create " y " in let z = Type . Variable . Unary . create " z " in let variable_list_to_type variable_list = polynomial_create_from_variables_list variable_list |> Type . polynomial_to_type in let divide_to_type numerator denominator = Type . IntExpression . create ( Type . Polynomial . divide ~ compare_t : Type . compare numerator denominator ) in let very_complicated_type = Type . tuple [ Type . Any ; Type . Union [ Type . Bottom ; Type . bool ] ; Type . Variable x ; variable_list_to_type [ 3 , [ x , 2 ; y , 1 ; z , 42 ] ] ; ] in assert_solve ~ left ( : Type . literal_integer 6 ) ~ right ( : variable_list_to_type [ 1 , [ ] ; 1 , [ x , 1 ] ] ) [ [ x , Type . literal_integer 5 ] ] ; assert_solve ~ left ( : Type . literal_integer 1 ) ~ right ( : variable_list_to_type [ 6 , [ ] ; 1 , [ x , 1 ] ] ) [ [ x , Type . literal_integer ( - 5 ) ] ] ; assert_impossible ~ left ( : variable_list_to_type [ 1 , [ x , 2 ] ] ) ~ right ( : variable_list_to_type [ 1 , [ x , 3 ] ] ) ; assert_solve ~ left : very_complicated_type ~ right ( : Type . Variable x ) [ [ x , very_complicated_type ] ] ; assert_impossible ~ left ( : Type . literal_integer 5 ) ~ right : very_complicated_type ; assert_impossible ~ left ( : variable_list_to_type [ 1 , [ x , 1 ] ; 1 , [ y , 1 ] ] ) ~ right ( : Type . literal_integer 5 ) ; assert_impossible ~ left ( : variable_list_to_type [ 1 , [ x , 1 ; y , 1 ] ] ) ~ right ( : Type . literal_integer 5 ) ; assert_solve ~ left ( : variable_list_to_type [ 6 , [ x , 1 ] ] ) ~ right ( : Type . literal_integer 18 ) [ [ x , Type . literal_integer 3 ] ] ; assert_solve ~ left ( : variable_list_to_type [ 2 , [ ] ; 4 , [ x , 1 ] ] ) ~ right ( : Type . literal_integer 18 ) [ [ x , Type . literal_integer 4 ] ] ; assert_impossible ~ left ( : variable_list_to_type [ 1 , [ x , 2 ] ] ) ~ right ( : Type . literal_integer 9 ) ; assert_solve ~ left ( : variable_list_to_type [ 1 , [ x , 1 ] ] ) ~ right ( : variable_list_to_type [ 1 , [ y , 1 ] ] ) [ [ x , Type . Variable y ] ; [ y , Type . Variable x ] ] ; assert_impossible ~ left ( : variable_list_to_type [ 2 , [ ] ; 3 , [ x , 1 ] ] ) ~ right ( : variable_list_to_type [ 2 , [ ] ; 9 , [ y , 1 ] ] ) ; assert_impossible ~ left : ( divide_to_type ( polynomial_create_from_variables_list [ 1 , [ x , 1 ] ] ) ( Type . Polynomial . create_from_int 7 ) ) ~ right : ( divide_to_type ( polynomial_create_from_variables_list [ 1 , [ y , 1 ] ] ) ( Type . Polynomial . create_from_int 7 ) ) ; assert_impossible ~ left : ( divide_to_type ( Type . Polynomial . create_from_int 1 ) ( polynomial_create_from_variables_list [ 3 , [ x , 1 ] ] ) ) ~ right : ( divide_to_type ( Type . Polynomial . create_from_int 1 ) ( polynomial_create_from_variables_list [ 3 , [ x , 1 ] ] ) ) ; ( ) |
let test_collect_all _ = let free_variable = Type . Variable ( Type . Variable . Unary . create " T " ) in let annotation = Type . parametric " p " [ ! free_variable ; Type . integer ] in assert_equal ( Type . Variable . GlobalTransforms . Unary . collect_all annotation ) [ Type . Variable . Unary . create " T " ] ; assert_equal ( Type . Variable . GlobalTransforms . Unary . collect_all ( Type . Tuple ( Type . OrderedTypes . create_unbounded_concatenation free_variable ) ) ) [ Type . Variable . Unary . create " T " ] ; let unary1 = Type . Variable . Unary . create " T1 " in let unary2 = Type . Variable . Unary . create " T2 " in let assert_collected annotation expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " T1 " -> Some ( Type . TypeAlias ( Type . Variable unary1 ) ) | " T2 " -> Some ( Type . TypeAlias ( Type . Variable unary2 ) ) | _ -> None in assert_equal ~ printer [ :% show : Type . Variable . Unary . t list ] expected ( Type . Variable . GlobalTransforms . Unary . collect_all ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true annotation ) ) ) in assert_collected { | typing . Tuple [ pyre_extensions . Broadcast [ typing . Tuple [ T1 , int ] , typing . Tuple [ T2 , int ] , ] , ] } | [ unary1 ; unary2 ] ; let free_variable_callable = let parameter_variadic = Type . Variable . Variadic . Parameters . create " T " in Type . Callable . create ~ parameters ( : Type . Callable . ParameterVariadicTypeVariable ( empty_head parameter_variadic ) ) ~ annotation : Type . integer ( ) in assert_equal ( Type . Variable . GlobalTransforms . ParameterVariadic . collect_all ( Type . parametric " p " [ ! Type . integer ; free_variable_callable ] ) ) [ Type . Variable . Variadic . Parameters . create " T " ] ; assert_equal ( Type . Variable . GlobalTransforms . ParameterVariadic . collect_all ( Type . parametric " G " [ CallableParameters ( Type . Variable . Variadic . Parameters . self_reference ( Type . Variable . Variadic . Parameters . create " TParams " ) ) ; ] ) ) [ Type . Variable . Variadic . Parameters . create " TParams " ] ; let variadic = Type . Variable . Variadic . Tuple . create " Ts " in let variadic2 = Type . Variable . Variadic . Tuple . create " Ts2 " in let assert_collected annotation expected = let aliases ? replace_unbound_parameters_with_any : _ = function | " Ts " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic ) ) | " Ts2 " -> Some ( Type . VariableAlias ( Type . Variable . TupleVariadic variadic2 ) ) | _ -> None in assert_equal ~ printer [ :% show : Type . Variable . Variadic . Tuple . t list ] expected ( Type . Variable . GlobalTransforms . TupleVariadic . collect_all ( Type . create ~ aliases ( parse_single_expression ~ preprocess : true annotation ) ) ) in assert_collected " typing . Tuple [ int , str ] " [ ] ; assert_collected " typing . Tuple [ int , pyre_extensions . Unpack [ Ts ] , str ] " [ variadic ] ; assert_collected " Foo [ int , pyre_extensions . Unpack [ Ts ] , str ] " [ variadic ] ; assert_collected " Foo [ Bar [ int , pyre_extensions . Unpack [ Ts ] , str ] , Baz [ pyre_extensions . Unpack [ Ts2 ] ] ] " [ variadic ; variadic2 ] ; assert_collected " typing . Callable [ [ Variable ( int , pyre_extensions . Unpack [ Ts ] , str ) ] , typing . Tuple [ int , \ pyre_extensions . Unpack [ Ts2 ] , str ] ] " [ variadic2 ; variadic ] ; assert_collected " typing . Callable [ [ Variable ( int , pyre_extensions . Unpack [ Ts ] , str ) ] , \ typing . Callable [ [ Variable ( int , pyre_extensions . Unpack [ Ts2 ] , str ) ] , bool ] ] " [ variadic2 ; variadic ] ; assert_collected { | typing . Tuple [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ typing_extensions . Literal [ 1 ] ] , ] , ] } | [ variadic ] ; assert_collected { | typing . Tuple [ pyre_extensions . Broadcast [ typing . Tuple [ int , . . . ] , typing . Tuple [ typing_extensions . Literal [ 1 ] ] , ] , ] } | [ ] ; assert_collected { | typing . Tuple [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] , ] , ] } | [ variadic2 ; variadic ] ; assert_collected { | typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ typing_extensions . Literal [ 1 ] , typing_extensions . Literal [ 2 ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] ] ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] ] ] ] } | [ variadic ; variadic2 ] ; assert_collected { | typing . Callable [ [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] ] ] ] ] , str ] } | [ variadic2 ; variadic ; variadic ] ; assert_collected { | Foo [ pyre_extensions . Unpack [ pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , pyre_extensions . Broadcast [ typing . Tuple [ pyre_extensions . Unpack [ Ts ] ] , typing . Tuple [ pyre_extensions . Unpack [ Ts2 ] ] ] ] ] ] } | [ variadic2 ; variadic ; variadic ] ; assert_collected { | pyre_extensions . Compose [ pyre_extensions . Unpack [ Ts ] , typing . Callable [ [ int ] , int ] ] } | [ variadic ] ; assert_collected { | pyre_extensions . Compose [ pyre_extensions . Compose [ typing . Callable [ [ int ] , int ] , pyre_extensions . Unpack [ Ts ] , ] , typing . Callable [ [ int ] , int ] ] } | [ variadic ] ; let variable = Type . Variable . Unary . create " T " in assert_equal ( Type . Variable . GlobalTransforms . Unary . collect_all ( Type . IntExpression . create ( Type . Polynomial . create_from_monomial_variables_list ~ compare_t : Type . compare [ ( 1 , [ ( Type . Monomial . create_product ( Type . OrderedTypes . Concatenation . create_unbounded_unpackable ( Type . Variable variable ) ) , 1 ) ; ] ) ; ] ) ) ) [ variable ] ; assert_collected { | pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] ] } | [ variadic ] ; assert_collected { | pyre_extensions . Product [ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 3 ] ] } | [ ] ; assert_collected { | pyre_extensions . Product [ pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 3 ] ] ] } | [ variadic ] ; assert_collected { | pyre_extensions . Product [ pyre_extensions . Divide [ pyre_extensions . Product [ pyre_extensions . Unpack [ Ts ] , typing_extensions . Literal [ 2 ] ] , pyre_extensions . Product [ pyre_extensions . Unpack [ Ts2 ] , typing_extensions . Literal [ 3 ] ] ] ] } | [ variadic2 ; variadic ] ; ( ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.