@@ -533,6 +533,12 @@ private Expr<?> getDefaultValueForType(CelType type) {
533533 if (type .equals (SimpleType .UINT )) {
534534 return typeSystem .mkUint (0 );
535535 }
536+ if (type .equals (SimpleType .TIMESTAMP )) {
537+ return typeSystem .wrapTimestamp (ctx .mkInt (0 ));
538+ }
539+ if (type .equals (SimpleType .DURATION )) {
540+ return typeSystem .wrapDuration (ctx .mkInt (0 ));
541+ }
536542 if (type instanceof ListType ) {
537543 if (emptyListCache == null ) {
538544 emptyListCache = typeSystem .mkListRefConst (EMPTY_LIST_PREFIX );
@@ -735,8 +741,10 @@ private TranslatedValue translateCall(CelExpr expr, CelAbstractSyntaxTree ast) {
735741 typeConstraints .add (ctx .mkNot (typeSystem .isUnknown (callRes )));
736742 typeConstraints .add (ctx .mkNot (typeSystem .isError (callRes )));
737743
744+ boolean isDynamic = ast .getTypeOrThrow (exprId ).equals (SimpleType .DYN );
745+ BoolExpr isApprox = ctx .mkBool (!isDynamic );
738746 return TranslatedValue .propagateStrict (
739- ctx , typeSystem , callRes , Optional .of (expr ), ctx . mkTrue () , args );
747+ ctx , typeSystem , callRes , Optional .of (expr ), isApprox , args );
740748 });
741749 }
742750
@@ -869,10 +877,6 @@ private TranslatedValue translateDynamicComprehension(
869877 ArrayExpr mapPresence =
870878 isMap ? (ArrayExpr ) typeSystem .getMapPresence (typeSystem .getMapRef (iterRange )) : null ;
871879
872- if (isMap ) {
873- applyBoundedMapBijection (mapPresence , seq , lengthExpr );
874- }
875-
876880 BoolExpr isTruncated = ctx .mkGt (lengthExpr , ctx .mkInt (comprehensionUnrollLimit ));
877881 truncationConditions .add (isTruncated );
878882
@@ -885,14 +889,15 @@ private TranslatedValue translateDynamicComprehension(
885889 }
886890 }
887891
888- private void applyBoundedMapBijection (
892+ private BoolExpr getBoundedMapBijection (
889893 ArrayExpr mapPresence , SeqExpr <?> seq , ArithExpr lengthExpr ) {
894+ List <BoolExpr > constraints = new ArrayList <>();
890895 for (int i = 0 ; i < comprehensionUnrollLimit ; i ++) {
891896 for (int j = i + 1 ; j < comprehensionUnrollLimit ; j ++) {
892897 BoolExpr validPair = ctx .mkLt (ctx .mkInt (j ), lengthExpr );
893898 BoolExpr notEqual =
894899 ctx .mkNot (ctx .mkEq (ctx .mkNth (seq , ctx .mkInt (i )), ctx .mkNth (seq , ctx .mkInt (j ))));
895- typeConstraints .add (ctx .mkImplies (validPair , notEqual ));
900+ constraints .add (ctx .mkImplies (validPair , notEqual ));
896901 }
897902 }
898903
@@ -907,7 +912,8 @@ private void applyBoundedMapBijection(
907912 ctx .mkStore (seqMap , ctx .mkNth (seq , ctx .mkInt (i )), ctx .mkTrue ()),
908913 seqMap );
909914 }
910- typeConstraints .add (ctx .mkImplies (isNotTruncated , ctx .mkEq (mapPresence , seqMap )));
915+ constraints .add (ctx .mkImplies (isNotTruncated , ctx .mkEq (mapPresence , seqMap )));
916+ return CelZ3TypeSystem .mkAndFlattened (ctx , constraints );
911917 }
912918
913919 private TranslatedValue [] evaluateLoopCondAndStep (
@@ -1239,9 +1245,10 @@ private BoolExpr createTypeConstraintForType(Expr<?> val, CelType type) {
12391245 }
12401246 Expr <?> optRef = typeSystem .getOptionalRef (val );
12411247 BoolExpr hasValue = typeSystem .optHasValue (optRef );
1242- BoolExpr valConstraint =
1243- createTypeConstraintForType (typeSystem .getOptionalValue (optRef ), paramType );
1244- return ctx .mkAnd (isOpt , ctx .mkImplies (hasValue , valConstraint ));
1248+ Expr <?> optVal = typeSystem .getOptionalValue (optRef );
1249+ BoolExpr optValNotError = ctx .mkNot (typeSystem .isError (optVal ));
1250+ BoolExpr valConstraint = createTypeConstraintForType (optVal , paramType );
1251+ return ctx .mkAnd (isOpt , ctx .mkImplies (hasValue , ctx .mkAnd (optValNotError , valConstraint )));
12451252 }
12461253 if (type .equals (SimpleType .BOOL )) {
12471254 return (BoolExpr ) ctx .mkApp (typeSystem .boolCons ().getTesterDecl (), val );
@@ -1269,16 +1276,25 @@ private BoolExpr createTypeConstraintForType(Expr<?> val, CelType type) {
12691276 if (type .equals (SimpleType .BYTES )) {
12701277 return (BoolExpr ) ctx .mkApp (typeSystem .bytesCons ().getTesterDecl (), val );
12711278 }
1279+ if (type .equals (SimpleType .TIMESTAMP )) {
1280+ IntExpr seconds = typeSystem .getTimestamp (val );
1281+ return ctx .mkAnd (
1282+ typeSystem .isTimestamp (val ), ctx .mkNot (typeSystem .checkTimestampOverflow (seconds )));
1283+ }
1284+ if (type .equals (SimpleType .DURATION )) {
1285+ IntExpr seconds = typeSystem .getDuration (val );
1286+ return ctx .mkAnd (
1287+ typeSystem .isDuration (val ), ctx .mkNot (typeSystem .checkDurationOverflow (seconds )));
1288+ }
1289+
12721290 if (type instanceof ListType ) {
1273- // Lists are explicitly bounded (sequence theory). We're safe in using for-all quantifiers
1274- // here.
1291+ // Constrain list elements using bounded unrolling up to comprehensionUnrollLimit rather
1292+ // than Z3 forall quantifiers to prevent MBQI quantifier instantiation loops.
1293+ // Assert: isList(val) ∧ for all unrolled 0 <= i < length: ¬isError(seq[i]) ∧
1294+ // typeConstraint(seq[i])
12751295 BoolExpr isList = typeSystem .isList (val );
12761296 CelType elemType = ((ListType ) type ).elemType ();
1277- if (elemType .equals (SimpleType .DYN )) {
1278- return isList ;
1279- }
12801297
1281- // isList(val) ∧ ∀i. (0 <= i < length) ⇒ elemType(seq[i])
12821298 Expr <?> listRef = typeSystem .getListRef (val );
12831299 SeqExpr seq = typeSystem .getSeq (listRef );
12841300 Expr length = ctx .mkLength (seq );
@@ -1288,20 +1304,70 @@ private BoolExpr createTypeConstraintForType(Expr<?> val, CelType type) {
12881304 for (int i = 0 ; i < comprehensionUnrollLimit ; i ++) {
12891305 IntExpr idx = ctx .mkInt (i );
12901306 Expr elem = ctx .mkNth (seq , idx );
1291- BoolExpr elemConstraint = createTypeConstraintForType (elem , elemType );
12921307 BoolExpr validIndex = ctx .mkLt (idx , length );
1293- boundsAndTypes .add (ctx .mkImplies (validIndex , elemConstraint ));
1294- BoolExpr outOfBounds = ctx .mkGe (idx , length );
1295- boundsAndTypes .add (ctx .mkImplies (outOfBounds , ctx .mkEq (elem , typeSystem .mkUnknown ())));
1308+ // Assert ¬isError(elem) as a domain invariant so Z3 never synthesizes an Error element in
1309+ // list(dyn). For concrete types, this is already implied by createTypeConstraintForType.
1310+ boundsAndTypes .add (ctx .mkImplies (validIndex , ctx .mkNot (typeSystem .isError (elem ))));
1311+ // Short-circuit DYN element types to prevent generating redundant validIndex ⇒ TRUE
1312+ // clauses.
1313+ if (!elemType .equals (SimpleType .DYN )) {
1314+ BoolExpr elemConstraint = createTypeConstraintForType (elem , elemType );
1315+ boundsAndTypes .add (ctx .mkImplies (validIndex , elemConstraint ));
1316+ }
12961317 }
12971318
12981319 return CelZ3TypeSystem .mkAndFlattened (ctx , boundsAndTypes );
12991320 }
13001321 if (type instanceof MapType ) {
1301- // Do NOT emit a for-all quantifier over map keys here.
1302- // Doing so forces MBQI into an infinite loop. Structural equivalence of dynamic keys is
1303- // naturally constrained by the primitive key assertions in getStructuralEquality().
1304- return typeSystem .isMap (val );
1322+ // Do NOT emit a for-all quantifier over map keys or values here.
1323+ // Doing so forces MBQI into an infinite loop. Instead, constrain keys and values using
1324+ // bounded unrolling over the key sequence up to comprehensionUnrollLimit.
1325+ // Assert: isMap(val) ∧ for all unrolled 0 <= i < length: isPrimitiveKey(key) ∧ ¬isError(key)
1326+ // ∧ (presence(key) ⇒ ¬isError(val) ∧ typeConstraint(val))
1327+ BoolExpr isMap = typeSystem .isMap (val );
1328+ MapType mapType = (MapType ) type ;
1329+ CelType keyType = mapType .keyType ();
1330+ CelType valType = mapType .valueType ();
1331+
1332+ Expr <?> mapRef = typeSystem .getMapRef (val );
1333+ SeqExpr seq = typeSystem .getMapKeys (mapRef );
1334+ Expr length = ctx .mkLength (seq );
1335+ ArrayExpr mapValues = (ArrayExpr ) typeSystem .getMapValues (mapRef );
1336+ ArrayExpr mapPresence = (ArrayExpr ) typeSystem .getMapPresence (mapRef );
1337+
1338+ List <BoolExpr > boundsAndTypes = new ArrayList <>();
1339+ boundsAndTypes .add (isMap );
1340+ boundsAndTypes .add (getBoundedMapBijection (mapPresence , seq , (ArithExpr ) length ));
1341+
1342+ for (int i = 0 ; i < comprehensionUnrollLimit ; i ++) {
1343+ IntExpr idx = ctx .mkInt (i );
1344+ Expr key = ctx .mkNth (seq , idx );
1345+ BoolExpr validIndex = ctx .mkLt (idx , length );
1346+
1347+ BoolExpr isKeyPrim = typeSystem .isPrimitiveKey (key );
1348+ BoolExpr keyNotError = ctx .mkNot (typeSystem .isError (key ));
1349+ // Assert isKeyPrim ∧ ¬isError(key) so Z3 never synthesizes a non-primitive or Error key in
1350+ // map(dyn, ...). For concrete map types, this is already implied by keyType constraints.
1351+ boundsAndTypes .add (ctx .mkImplies (validIndex , ctx .mkAnd (isKeyPrim , keyNotError )));
1352+ // Short-circuit DYN key types to prevent generating redundant validIndex ⇒ TRUE clauses.
1353+ if (!keyType .equals (SimpleType .DYN )) {
1354+ boundsAndTypes .add (ctx .mkImplies (validIndex , createTypeConstraintForType (key , keyType )));
1355+ }
1356+
1357+ BoolExpr presence = (BoolExpr ) ctx .mkSelect (mapPresence , key );
1358+ BoolExpr validEntry = ctx .mkAnd (validIndex , presence );
1359+
1360+ Expr mapVal = ctx .mkSelect (mapValues , key );
1361+ BoolExpr valNotError = ctx .mkNot (typeSystem .isError (mapVal ));
1362+ boundsAndTypes .add (ctx .mkImplies (validEntry , valNotError ));
1363+ // Short-circuit DYN value types to prevent generating redundant validEntry ⇒ TRUE clauses.
1364+ if (!valType .equals (SimpleType .DYN )) {
1365+ boundsAndTypes .add (
1366+ ctx .mkImplies (validEntry , createTypeConstraintForType (mapVal , valType )));
1367+ }
1368+ }
1369+
1370+ return CelZ3TypeSystem .mkAndFlattened (ctx , boundsAndTypes );
13051371 }
13061372 if (type .kind () == CelKind .STRUCT ) {
13071373 return ctx .mkAnd (
0 commit comments