Commit 6d3704e
Changed
Summary:
+ We would like to move towards an abstract type for `IntExpression`s to make sure that all polynomials are only expressed in fully normalized form.
+ Currently, an `IntExpression` is of `'a Polynomial.t`, which is transparent. This allows for all kinds of redundant states, like the monomial list (informally) `[1, [x, 1]]`, which is better represented as a `Variable x`, and `[5, []]`, which is better represented as `Literal (Integer 5)`. This is also a gigantic pain because these errors tend to be "hidden", and the error messages you get for mistaking alternate representations are terrible.
+ We have added an abstract type `'a RecordIntExpression.t`, which is the new input to an `IntExpression`. Everywhere the `IntExpression` constructor was used, we now use `IntExpression.create`, which implicitly constructs proper values of the abstract type for use.
It is a common occurrence to pattern match on an `IntExpression _` to extract out the polynomial, and perform some polynomial-only operation on it, so in addition we have added support for the private type `'a RecordIntExpression.t`, which is now supported at all call-sites. This means that we may deconstruct an `IntExpression` to obtain a `Polynomial.t`, but we cannot construct an `IntExpression` using the constructor itself (preserving normalization).
Currently, no normalization is happening, but this change is to stage for later normalization features. In addition, we have a sum type `'a variant`, which is necessary because `IntExpression` regards the type of `RecordIntExpression.t` as abstract, so we must have `RecordIntExpression.t` implement its own normalizing constructor, with a sum to tag whether it is actually a polynomial, or some other type. This will be expanded on in future diffs.
There's some redundant stuff that will need to be taken away in a future diff. For instance, we should get rid of some useless branches and re-evaluate everything to do with `type_to_int_expression`.
- **Why `module IntExpression`?**
Firstly, we need some function of signature `'a Polynomial.t -> type_t` - this function will take a polynomial and return its normalized form (which may be `IntExpression`, `Literal`, or `Variable`). Necessarily, since `RecordIntExpression` comes *before* `type_t` and we are not using recursive modules, this must be in a different module. `IntExpression` is this module.
- **Why `'a variant`?**
`'a variant` is here because, as stated before, `IntExpression` contains the function `create : 'a Polynomial.t -> type_t`. However, the type of `RecordIntExpression` is *abstract* to the module `IntExpression`, because we want to enforce that the entirety of `type.ml` regards `RecordIntExpression.t` as abstract, so we don't have any representation invariant violations. Thus, `RecordIntExpression` must itself implement its own normalization function, with a way of signalling to `IntExpression` whether the output it produced is normalized as a polynomial, or needs to be changed to a `Literal` or something.
- **Why not just have `RecordIntExpression` have a function that turns a polynomial into a pseudo-normalized polynomial, and then have `IntExpression` pattern match on it to produce the proper value?**
In other words, why not have `RecordIntExpression.normalize : 'a Polynomial.t -> 'a Polynomial.t`, and have `create` do the work of recognizing if its a literal or not.
The problem is that `RecordIntExpression` must produce the abstract type that `IntExpression` (the constructor) takes in, since it comes before the type definition of `type_t`. Then, `IntExpression.create` has the burden of creating the value of type `RecordIntExpression.t` - which it can't, since it's an abstract type.
So `RecordIntExpression.t` *has* to give a way to produce values of type `RecordIntExpression.t`, and because those values are abstract, it must tag them with whether or not they should be normalized.
- **Why the `private` constructor for `RecordIntExpression.t`?**
It is entirely possible to implement `RecordIntExpression.polynomial_of : 'a t -> 'a Polynomial.t`. However, this induces extreme headache in the existing codebase when trying to pattern match on `IntExpression` variants of the `type_t` type.
Without a private constructor, this means that to pattern match on an empty polynomial, you must do `| IntExpression int_expression -> match int_expression of | [] -> ...`, or something similar, as opposed to `| IntExpression (Data []) -> ...`. It's just a lot of syntactic burden. This can be alleviated later by reducing the amount of points where we have to pattern match on `IntExpressions`, by changing `solve_less_or_equal` to do all of its simplification first, rather than recursively inside of the function. Right now, however, this is less bloat.
Reviewed By: grievejia
Differential Revision: D28890690
fbshipit-source-id: ef1d81aec74922d9c74f4e34b998d5e07f0fd63dIntExpression to take in abstract type1 parent 842f400 commit 6d3704e
File tree
4 files changed
+97
-51
lines changed- source/analysis
- test
4 files changed
+97
-51
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1996 | 1996 | | |
1997 | 1997 | | |
1998 | 1998 | | |
1999 | | - | |
| 1999 | + | |
2000 | 2000 | | |
2001 | 2001 | | |
2002 | 2002 | | |
| |||
2014 | 2014 | | |
2015 | 2015 | | |
2016 | 2016 | | |
2017 | | - | |
| 2017 | + | |
2018 | 2018 | | |
2019 | 2019 | | |
2020 | 2020 | | |
2021 | 2021 | | |
2022 | 2022 | | |
2023 | 2023 | | |
2024 | | - | |
| 2024 | + | |
2025 | 2025 | | |
2026 | 2026 | | |
2027 | 2027 | | |
| |||
2032 | 2032 | | |
2033 | 2033 | | |
2034 | 2034 | | |
2035 | | - | |
| 2035 | + | |
2036 | 2036 | | |
2037 | 2037 | | |
2038 | 2038 | | |
| |||
2045 | 2045 | | |
2046 | 2046 | | |
2047 | 2047 | | |
2048 | | - | |
| 2048 | + | |
2049 | 2049 | | |
2050 | 2050 | | |
2051 | 2051 | | |
| |||
2056 | 2056 | | |
2057 | 2057 | | |
2058 | 2058 | | |
2059 | | - | |
| 2059 | + | |
2060 | 2060 | | |
2061 | 2061 | | |
2062 | 2062 | | |
| |||
2069 | 2069 | | |
2070 | 2070 | | |
2071 | 2071 | | |
2072 | | - | |
| 2072 | + | |
2073 | 2073 | | |
2074 | 2074 | | |
2075 | 2075 | | |
2076 | 2076 | | |
2077 | 2077 | | |
2078 | 2078 | | |
2079 | 2079 | | |
2080 | | - | |
| 2080 | + | |
2081 | 2081 | | |
2082 | 2082 | | |
2083 | 2083 | | |
| |||
2088 | 2088 | | |
2089 | 2089 | | |
2090 | 2090 | | |
2091 | | - | |
| 2091 | + | |
2092 | 2092 | | |
2093 | 2093 | | |
2094 | 2094 | | |
| |||
2256 | 2256 | | |
2257 | 2257 | | |
2258 | 2258 | | |
2259 | | - | |
| 2259 | + | |
2260 | 2260 | | |
2261 | 2261 | | |
2262 | 2262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
979 | 979 | | |
980 | 980 | | |
981 | 981 | | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
982 | 1000 | | |
983 | 1001 | | |
984 | 1002 | | |
| |||
1031 | 1049 | | |
1032 | 1050 | | |
1033 | 1051 | | |
1034 | | - | |
| 1052 | + | |
1035 | 1053 | | |
1036 | 1054 | | |
1037 | 1055 | | |
1038 | 1056 | | |
1039 | 1057 | | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
1040 | 1069 | | |
1041 | 1070 | | |
1042 | 1071 | | |
| |||
1046 | 1075 | | |
1047 | 1076 | | |
1048 | 1077 | | |
1049 | | - | |
1050 | | - | |
1051 | 1078 | | |
1052 | 1079 | | |
1053 | 1080 | | |
1054 | 1081 | | |
1055 | 1082 | | |
1056 | 1083 | | |
1057 | 1084 | | |
1058 | | - | |
| 1085 | + | |
1059 | 1086 | | |
1060 | 1087 | | |
1061 | 1088 | | |
1062 | 1089 | | |
1063 | | - | |
| 1090 | + | |
1064 | 1091 | | |
1065 | | - | |
| 1092 | + | |
1066 | 1093 | | |
1067 | | - | |
| 1094 | + | |
1068 | 1095 | | |
1069 | | - | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
1070 | 1099 | | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
1074 | 1105 | | |
1075 | 1106 | | |
1076 | 1107 | | |
1077 | 1108 | | |
1078 | 1109 | | |
1079 | | - | |
| 1110 | + | |
1080 | 1111 | | |
1081 | | - | |
| 1112 | + | |
1082 | 1113 | | |
1083 | 1114 | | |
1084 | 1115 | | |
1085 | 1116 | | |
1086 | 1117 | | |
1087 | 1118 | | |
1088 | | - | |
1089 | | - | |
1090 | | - | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
1091 | 1122 | | |
1092 | 1123 | | |
1093 | 1124 | | |
1094 | | - | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
1095 | 1128 | | |
1096 | 1129 | | |
1097 | 1130 | | |
1098 | 1131 | | |
1099 | 1132 | | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
1103 | 1136 | | |
1104 | 1137 | | |
1105 | 1138 | | |
| |||
1109 | 1142 | | |
1110 | 1143 | | |
1111 | 1144 | | |
1112 | | - | |
| 1145 | + | |
1113 | 1146 | | |
1114 | 1147 | | |
1115 | 1148 | | |
| |||
1123 | 1156 | | |
1124 | 1157 | | |
1125 | 1158 | | |
1126 | | - | |
| 1159 | + | |
1127 | 1160 | | |
1128 | 1161 | | |
1129 | 1162 | | |
1130 | | - | |
| 1163 | + | |
1131 | 1164 | | |
1132 | 1165 | | |
1133 | 1166 | | |
| |||
1143 | 1176 | | |
1144 | 1177 | | |
1145 | 1178 | | |
1146 | | - | |
| 1179 | + | |
1147 | 1180 | | |
1148 | | - | |
| 1181 | + | |
1149 | 1182 | | |
1150 | 1183 | | |
1151 | 1184 | | |
| |||
1165 | 1198 | | |
1166 | 1199 | | |
1167 | 1200 | | |
1168 | | - | |
| 1201 | + | |
1169 | 1202 | | |
1170 | 1203 | | |
1171 | 1204 | | |
| |||
1455 | 1488 | | |
1456 | 1489 | | |
1457 | 1490 | | |
1458 | | - | |
| 1491 | + | |
1459 | 1492 | | |
1460 | | - | |
| 1493 | + | |
1461 | 1494 | | |
1462 | 1495 | | |
1463 | 1496 | | |
| |||
1542 | 1575 | | |
1543 | 1576 | | |
1544 | 1577 | | |
1545 | | - | |
| 1578 | + | |
1546 | 1579 | | |
1547 | | - | |
| 1580 | + | |
1548 | 1581 | | |
1549 | 1582 | | |
1550 | 1583 | | |
| |||
1906 | 1939 | | |
1907 | 1940 | | |
1908 | 1941 | | |
1909 | | - | |
| 1942 | + | |
1910 | 1943 | | |
1911 | | - | |
| 1944 | + | |
1912 | 1945 | | |
1913 | 1946 | | |
1914 | 1947 | | |
| |||
1946 | 1979 | | |
1947 | 1980 | | |
1948 | 1981 | | |
1949 | | - | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
1950 | 1986 | | |
1951 | 1987 | | |
1952 | 1988 | | |
| |||
1957 | 1993 | | |
1958 | 1994 | | |
1959 | 1995 | | |
1960 | | - | |
| 1996 | + | |
1961 | 1997 | | |
1962 | 1998 | | |
1963 | 1999 | | |
| |||
3911 | 3947 | | |
3912 | 3948 | | |
3913 | 3949 | | |
3914 | | - | |
| 3950 | + | |
3915 | 3951 | | |
3916 | 3952 | | |
3917 | 3953 | | |
3918 | 3954 | | |
3919 | 3955 | | |
3920 | | - | |
3921 | | - | |
| 3956 | + | |
| 3957 | + | |
3922 | 3958 | | |
3923 | 3959 | | |
3924 | 3960 | | |
3925 | 3961 | | |
3926 | 3962 | | |
3927 | 3963 | | |
3928 | | - | |
| 3964 | + | |
3929 | 3965 | | |
3930 | 3966 | | |
3931 | 3967 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
265 | 269 | | |
266 | 270 | | |
267 | 271 | | |
| |||
304 | 308 | | |
305 | 309 | | |
306 | 310 | | |
307 | | - | |
| 311 | + | |
308 | 312 | | |
309 | 313 | | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
310 | 318 | | |
311 | 319 | | |
312 | 320 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
| 183 | + | |
183 | 184 | | |
184 | | - | |
| 185 | + | |
| 186 | + | |
185 | 187 | | |
186 | 188 | | |
187 | 189 | | |
| |||
0 commit comments