Arithmetic: Move implementation of type rules to cpp. (#6419)
authorAina Niemetz <aina.niemetz@gmail.com>
Wed, 21 Apr 2021 22:57:49 +0000 (15:57 -0700)
committerGitHub <noreply@github.com>
Wed, 21 Apr 2021 22:57:49 +0000 (22:57 +0000)
src/CMakeLists.txt
src/theory/arith/theory_arith_type_rules.cpp [new file with mode: 0644]
src/theory/arith/theory_arith_type_rules.h

index 052e65adb61cefecf1aea1c426152fb9ee0364a5..7d7aca8e8b74d1c306fe380c49dd346d14ff599e 100644 (file)
@@ -436,6 +436,7 @@ libcvc5_add_sources(
   theory/arith/theory_arith.h
   theory/arith/theory_arith_private.cpp
   theory/arith/theory_arith_private.h
+  theory/arith/theory_arith_type_rules.cpp
   theory/arith/theory_arith_type_rules.h
   theory/arith/type_enumerator.h
   theory/arrays/array_info.cpp
diff --git a/src/theory/arith/theory_arith_type_rules.cpp b/src/theory/arith/theory_arith_type_rules.cpp
new file mode 100644 (file)
index 0000000..ba4c7dc
--- /dev/null
@@ -0,0 +1,158 @@
+/******************************************************************************
+ * Top contributors (to current version):
+ *   Andrew Reynolds, Gereon Kremer, Dejan Jovanovic
+ *
+ * This file is part of the cvc5 project.
+ *
+ * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
+ * in the top-level source directory and their institutional affiliations.
+ * All rights reserved.  See the file COPYING in the top-level source
+ * directory for licensing information.
+ * ****************************************************************************
+ *
+ * Typing and cardinality rules for theory arithmetic.
+ */
+
+#include "theory/arith/theory_arith_type_rules.h"
+
+namespace cvc5 {
+namespace theory {
+namespace arith {
+
+TypeNode ArithConstantTypeRule::computeType(NodeManager* nodeManager,
+                                            TNode n,
+                                            bool check)
+{
+  Assert(n.getKind() == kind::CONST_RATIONAL);
+  if (n.getConst<Rational>().isIntegral())
+  {
+    return nodeManager->integerType();
+  }
+  else
+  {
+    return nodeManager->realType();
+  }
+}
+
+TypeNode ArithOperatorTypeRule::computeType(NodeManager* nodeManager,
+                                            TNode n,
+                                            bool check)
+{
+  TypeNode integerType = nodeManager->integerType();
+  TypeNode realType = nodeManager->realType();
+  TNode::iterator child_it = n.begin();
+  TNode::iterator child_it_end = n.end();
+  bool isInteger = true;
+  Kind k = n.getKind();
+  for (; child_it != child_it_end; ++child_it)
+  {
+    TypeNode childType = (*child_it).getType(check);
+    if (!childType.isInteger())
+    {
+      isInteger = false;
+      if (!check)
+      {  // if we're not checking, nothing left to do
+        break;
+      }
+    }
+    if (check)
+    {
+      if (!childType.isReal())
+      {
+        throw TypeCheckingExceptionPrivate(n,
+                                           "expecting an arithmetic subterm");
+      }
+      if (k == kind::TO_REAL && !childType.isInteger())
+      {
+        throw TypeCheckingExceptionPrivate(n, "expecting an integer subterm");
+      }
+    }
+  }
+  switch (k)
+  {
+    case kind::TO_REAL:
+    case kind::CAST_TO_REAL: return realType;
+    case kind::TO_INTEGER: return integerType;
+    default:
+    {
+      bool isDivision = k == kind::DIVISION || k == kind::DIVISION_TOTAL;
+      return (isInteger && !isDivision ? integerType : realType);
+    }
+  }
+}
+
+TypeNode RealNullaryOperatorTypeRule::computeType(NodeManager* nodeManager,
+                                                  TNode n,
+                                                  bool check)
+{
+  // for nullary operators, we only computeType for check=true, since they are
+  // given TypeAttr() on creation
+  Assert(check);
+  TypeNode realType = n.getType();
+  if (realType != NodeManager::currentNM()->realType())
+  {
+    throw TypeCheckingExceptionPrivate(n, "expecting real type");
+  }
+  return realType;
+}
+
+TypeNode IAndOpTypeRule::computeType(NodeManager* nodeManager,
+                                     TNode n,
+                                     bool check)
+{
+  if (n.getKind() != kind::IAND_OP)
+  {
+    InternalError() << "IAND_OP typerule invoked for IAND_OP kind";
+  }
+  TypeNode iType = nodeManager->integerType();
+  std::vector<TypeNode> argTypes;
+  argTypes.push_back(iType);
+  argTypes.push_back(iType);
+  return nodeManager->mkFunctionType(argTypes, iType);
+}
+
+TypeNode IAndTypeRule::computeType(NodeManager* nodeManager,
+                                   TNode n,
+                                   bool check)
+{
+  if (n.getKind() != kind::IAND)
+  {
+    InternalError() << "IAND typerule invoked for IAND kind";
+  }
+  if (check)
+  {
+    TypeNode arg1 = n[0].getType(check);
+    TypeNode arg2 = n[1].getType(check);
+    if (!arg1.isInteger() || !arg2.isInteger())
+    {
+      throw TypeCheckingExceptionPrivate(n, "expecting integer terms");
+    }
+  }
+  return nodeManager->integerType();
+}
+
+TypeNode IndexedRootPredicateTypeRule::computeType(NodeManager* nodeManager,
+                                                   TNode n,
+                                                   bool check)
+{
+  if (check)
+  {
+    TypeNode t1 = n[0].getType(check);
+    if (!t1.isBoolean())
+    {
+      throw TypeCheckingExceptionPrivate(
+          n, "expecting boolean term as first argument");
+    }
+    TypeNode t2 = n[1].getType(check);
+    if (!t2.isReal())
+    {
+      throw TypeCheckingExceptionPrivate(
+          n, "expecting polynomial as second argument");
+    }
+  }
+  return nodeManager->booleanType();
+}
+
+}  // namespace arith
+}  // namespace theory
+}  // namespace cvc5
index 2d56a26c74a1f266a8b1171369aa912a4d70cacf..f7ef64e2d3d62fee82193c4efbe0b092d2b5256c 100644 (file)
@@ -10,9 +10,7 @@
  * directory for licensing information.
  * ****************************************************************************
  *
- * [[ Add brief comments here ]]
- *
- * [[ Add file-specific comments here ]]
+ * Typing and cardinality rules for theory arithmetic.
  */
 
 #include "cvc5_private.h"
 #ifndef CVC5__THEORY__ARITH__THEORY_ARITH_TYPE_RULES_H
 #define CVC5__THEORY__ARITH__THEORY_ARITH_TYPE_RULES_H
 
+#include "expr/node.h"
+#include "expr/type_node.h"
+
 namespace cvc5 {
 namespace theory {
 namespace arith {
 
+class ArithConstantTypeRule
+{
+ public:
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
-class ArithConstantTypeRule {
-public:
-  inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check)
-  {
-    Assert(n.getKind() == kind::CONST_RATIONAL);
-    if(n.getConst<Rational>().isIntegral()){
-      return nodeManager->integerType();
-    }else{
-      return nodeManager->realType();
-    }
-  }
-};/* class ArithConstantTypeRule */
-
-class ArithOperatorTypeRule {
-public:
-  inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check)
-  {
-    TypeNode integerType = nodeManager->integerType();
-    TypeNode realType = nodeManager->realType();
-    TNode::iterator child_it = n.begin();
-    TNode::iterator child_it_end = n.end();
-    bool isInteger = true;
-    Kind k = n.getKind();
-    for(; child_it != child_it_end; ++child_it) {
-      TypeNode childType = (*child_it).getType(check);
-      if (!childType.isInteger()) {
-        isInteger = false;
-        if( !check ) { // if we're not checking, nothing left to do
-          break;
-        }
-      }
-      if( check ) {
-        if(!childType.isReal()) {
-          throw TypeCheckingExceptionPrivate(n, "expecting an arithmetic subterm");
-        }
-        if (k == kind::TO_REAL && !childType.isInteger())
-        {
-          throw TypeCheckingExceptionPrivate(n, "expecting an integer subterm");
-        }
-      }
-    }
-    switch (k)
-    {
-      case kind::TO_REAL:
-      case kind::CAST_TO_REAL: return realType;
-      case kind::TO_INTEGER: return integerType;
-      default:
-      {
-        bool isDivision = k == kind::DIVISION || k == kind::DIVISION_TOTAL;
-        return (isInteger && !isDivision ? integerType : realType);
-      }
-    }
-  }
-};/* class ArithOperatorTypeRule */
+class ArithOperatorTypeRule
+{
+ public:
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
-class RealNullaryOperatorTypeRule {
-public:
-  inline static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check)
-  {
-    // for nullary operators, we only computeType for check=true, since they are given TypeAttr() on creation
-    Assert(check);
-    TypeNode realType = n.getType();
-    if(realType!=NodeManager::currentNM()->realType()) {
-      throw TypeCheckingExceptionPrivate(n, "expecting real type");
-    }
-    return realType;
-  }
-};/* class RealNullaryOperatorTypeRule */
+class RealNullaryOperatorTypeRule
+{
+ public:
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
 class IAndOpTypeRule
 {
  public:
-  inline static TypeNode computeType(NodeManager* nodeManager,
-                                     TNode n,
-                                     bool check)
-  {
-    if (n.getKind() != kind::IAND_OP)
-    {
-      InternalError() << "IAND_OP typerule invoked for IAND_OP kind";
-    }
-    TypeNode iType = nodeManager->integerType();
-    std::vector<TypeNode> argTypes;
-    argTypes.push_back(iType);
-    argTypes.push_back(iType);
-    return nodeManager->mkFunctionType(argTypes, iType);
-  }
-}; /* class IAndOpTypeRule */
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
 class IAndTypeRule
 {
  public:
-  inline static TypeNode computeType(NodeManager* nodeManager,
-                                     TNode n,
-                                     bool check)
-  {
-    if (n.getKind() != kind::IAND)
-    {
-      InternalError() << "IAND typerule invoked for IAND kind";
-    }
-    if (check)
-    {
-      TypeNode arg1 = n[0].getType(check);
-      TypeNode arg2 = n[1].getType(check);
-      if (!arg1.isInteger() || !arg2.isInteger())
-      {
-        throw TypeCheckingExceptionPrivate(n, "expecting integer terms");
-      }
-    }
-    return nodeManager->integerType();
-  }
-}; /* class BitVectorConversionTypeRule */
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
 class IndexedRootPredicateTypeRule
 {
  public:
-  inline static TypeNode computeType(NodeManager* nodeManager,
-                                     TNode n,
-                                     bool check)
-  {
-    if (check)
-    {
-      TypeNode t1 = n[0].getType(check);
-      if (!t1.isBoolean())
-      {
-        throw TypeCheckingExceptionPrivate(
-            n, "expecting boolean term as first argument");
-      }
-      TypeNode t2 = n[1].getType(check);
-      if (!t2.isReal())
-      {
-        throw TypeCheckingExceptionPrivate(
-            n, "expecting polynomial as second argument");
-      }
-    }
-    return nodeManager->booleanType();
-  }
-}; /* class IndexedRootPredicateTypeRule */
+  static TypeNode computeType(NodeManager* nodeManager, TNode n, bool check);
+};
 
 }  // namespace arith
 }  // namespace theory