Skip to content

Commit 8a355b9

Browse files
Brandon Wufacebook-github-bot
authored andcommitted
Added basic normalization for IntExpressions
Summary: + There is no normalization on top of the abstract type support currently implemented. + We do not do any normalization, we just return the same polynomial. + Implemented just the basic polynomial normalization. Added some test cases showing what it does and does not do. Currently, it eliminates zero-coefficient terms, and does not regard the order of monomials or terms in the monomial. It does not collect like terms, either monomials or monomial factors. Reviewed By: pradeep90 Differential Revision: D28890914 fbshipit-source-id: 8a887b961e94fcdfbb9ddf8c12d744778f3e7547
1 parent 6d3704e commit 8a355b9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

source/analysis/test/typeTest.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,27 @@ let test_convert_all_escaped_free_variables_to_anys _ =
19581958
()
19591959
19601960
1961+
let test_int_expression_create _ =
1962+
let x = Type.Variable.Unary.create "x" in
1963+
let y = Type.Variable.Unary.create "y" in
1964+
let assert_create_list_list given expected =
1965+
match Type.IntExpression.create (polynomial_create_from_variables_list given) with
1966+
| Type.IntExpression (Data result_polynomial) ->
1967+
assert_equal
1968+
~printer:[%show: Type.type_t Type.Polynomial.t]
1969+
~cmp:[%equal: Type.type_t Type.Polynomial.t]
1970+
result_polynomial
1971+
(polynomial_create_from_variables_list expected)
1972+
| _ -> assert false
1973+
in
1974+
assert_create_list_list [1, [x, 1]; 1, []; -1, []] [1, [x, 1]; 1, []; -1, []];
1975+
assert_create_list_list [1, [y, 1; y, 1]] [1, [y, 1; y, 1]];
1976+
assert_create_list_list [0, [x, 1; y, 1]; 0, []; 1, []] [1, []];
1977+
assert_create_list_list [1, [x, 1]; 1, [y, 1]] [1, [y, 1]; 1, [x, 1]];
1978+
assert_create_list_list [1, [x, 1; y, 1]] [1, [y, 1; x, 1]];
1979+
()
1980+
1981+
19611982
let test_replace_all _ =
19621983
let free_variable = Type.Variable (Type.Variable.Unary.create "T") in
19631984
let annotation = Type.parametric "p" ![free_variable; Type.integer] in
@@ -3572,6 +3593,7 @@ let () =
35723593
"contains_escaped_free_variable" >:: test_contains_escaped_free_variable;
35733594
"convert_all_escaped_free_variables_to_anys"
35743595
>:: test_convert_all_escaped_free_variables_to_anys;
3596+
"int_expression_create" >:: test_int_expression_create;
35753597
"replace_all" >:: test_replace_all;
35763598
"less_or_equal_polynomial" >:: test_less_or_equal;
35773599
"collect_all" >:: test_collect_all;

source/analysis/type.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ and Polynomial : sig
728728

729729
val create_from_int : int -> 'a t
730730

731+
val normalize : compare_t:('a -> 'a -> int) -> 'a t -> 'a t
732+
731733
val create_from_variables_list
732734
: compare_t:('a -> 'a -> int) ->
733735
(int * ('a Record.Variable.RecordUnary.record * int) list) list ->
@@ -994,7 +996,8 @@ end = struct
994996
| NonPolynomial of 'a Polynomial.t
995997
| Polynomial of 'a t
996998

997-
let normalize_variant ~compare_t:_ polynomial = Polynomial (Data polynomial)
999+
let normalize_variant ~compare_t polynomial =
1000+
Polynomial (Data (Polynomial.normalize ~compare_t polynomial))
9981001
end
9991002

10001003
open Record.Callable

0 commit comments

Comments
 (0)