@@ -44,7 +44,8 @@ let get_type_in_context (t : recursive_type)
44
44
build_recursive_type new_union (recursive_context @ new_context)
45
45
46
46
(* Converts a pair of recursive types into a pair of union types that share a context *)
47
- let get_unified_type_context_pair (typea : recursive_type ) (typeb : recursive_type ) =
47
+ let get_unified_type_context_pair (typea : recursive_type )
48
+ (typeb : recursive_type ) =
48
49
let recontextualized_typeb = get_type_in_context typeb typea.context in
49
50
let new_typeb = recontextualized_typeb.union in
50
51
((typea.union, new_typeb), recontextualized_typeb.context)
@@ -55,30 +56,47 @@ let get_unified_type_context (types : recursive_type list) =
55
56
let new_unions_rev, new_context =
56
57
List. fold_left
57
58
(fun (acc_union , acc_context ) next_type ->
58
- let recontextualized_next_union = get_type_in_context next_type acc_context in
59
+ let recontextualized_next_union =
60
+ get_type_in_context next_type acc_context
61
+ in
59
62
let new_acc_union = recontextualized_next_union.union :: acc_union in
60
63
let new_acc_context = recontextualized_next_union.context in
61
64
(new_acc_union, new_acc_context))
62
- ([] , [] ) types in
65
+ ([] , [] ) types
66
+ in
63
67
(* We must reverse the list of unions since we fold left but want to keep the types in the right order *)
64
68
let new_unions = List. rev new_unions_rev in
65
- new_unions, new_context
69
+ ( new_unions, new_context)
66
70
67
71
(* TODO: consider writing more dedicated logic for this rather than the showving intermediate into intersection *)
68
72
(* Takes a list of arg types and their corresponding body types, and joined them into
69
73
a single recursive type for the intersection of the functions *)
70
- let unify_function_types (arg_types : recursive_type list ) (body_types : recursive_type list ) =
74
+ let unify_function_types (arg_types : recursive_type list )
75
+ (body_types : recursive_type list ) =
71
76
(* First, build individual unary function types for each arg/body pair *)
72
- let unary_types = List. map2 (fun arg_type body_type ->
73
- let (new_arg_type, new_body_type), new_context = get_unified_type_context_pair arg_type body_type in
74
- build_recursive_type [ Intersection [(new_arg_type, new_body_type )]] new_context
75
- ) arg_types body_types in
77
+ let unary_types =
78
+ List. map2
79
+ (fun arg_type body_type ->
80
+ let (new_arg_type, new_body_type), new_context =
81
+ get_unified_type_context_pair arg_type body_type
82
+ in
83
+ build_recursive_type
84
+ [ Intersection [ (new_arg_type, new_body_type) ] ]
85
+ new_context)
86
+ arg_types body_types
87
+ in
76
88
(* Then, rectonextualize all of them so we can prepare to join them into a single type *)
77
89
let new_unary_unions, new_context = get_unified_type_context unary_types in
78
90
(* Then destructure all of the unary types to build a single intersection type *)
79
- let unary_list = List. fold_left (fun acc_func_types next_union ->
80
- match next_union with
81
- | [ Intersection [ next_unary ]] -> next_unary::acc_func_types
82
- | _ -> raise (Failure " there was a problem destructuring the unary function types" )
83
- ) [] new_unary_unions in
84
- build_recursive_type [ Intersection unary_list ] new_context
91
+ let unary_list =
92
+ List. fold_left
93
+ (fun acc_func_types next_union ->
94
+ match next_union with
95
+ | [ Intersection [ next_unary ] ] -> next_unary :: acc_func_types
96
+ | _ ->
97
+ raise
98
+ (Failure
99
+ " there was a problem destructuring the unary function types" ))
100
+ [] new_unary_unions
101
+ in
102
+ build_recursive_type [ Intersection unary_list ] new_context
0 commit comments