This is a request for review:

Size of patch: 2 / 7
Level of difficulty: 1 / 7

As proposed yesterday, this patch adds support for logical negation for int types. Since yesterday's mail, I've also added 'param' overloads, tests to check the behavior, and documentation to the spec (though I'm noting that the spec doesn't seem to document the param versions of the ! operator and did not fix that here; instead, I added a note to the TODO section, as I'm not sure what approach we want to take for these cases).

A proposed commit message is just below.

Thanks,
-Brad

-----

Add support for logical negation of integral types

Prior to this patch, the ! operator was only supported for bool types. This patch extends that support to also support integral types with the obvious interpretation (zero => true, non-zero => false). This implementation adds that support in both param and non-param versions. It also adds a few tests to lock in the behavior and documentation to the spec for the non-param versions (the existing param version did not seem to be documented in the spec, so I followed suit here for now, and added a TODO to the spec TODO to figure out how we want to document these in the future).

I believe that the rationale for the past behavior has probably been "it's what C# does." I believe C#'s rationale is to avoid making typos like the following legal programs:

        if (!(x=5)) ...

where the user probably meant to type:

        if (!(x==5)) ...

Chapel prevents these typos another way -- by only permitting assignments at the statement level, so that may be a reason to feel OK diverging from C#'s practice.


Note that Chapel does permit integer expressions in conditionals, e.g.:

        var x: int;
        ...

        if (x) ...

which is why it seemed suprising to me to find that we do not support:

        if (!x)

Proposing this on chapel-developers yesterday, there were no objections, and more positive response than most requests for comment generate.

Index: test/expressions/bradc/notIntParam.chpl
===================================================================
--- test/expressions/bradc/notIntParam.chpl	(revision 0)
+++ test/expressions/bradc/notIntParam.chpl	(revision 0)
@@ -0,0 +1,47 @@
+config param nonzero = 42,
+             zero = 0;
+
+proc testType(param size) {
+  param ti: int(size) = nonzero: int(size),
+        tu: uint(size) = nonzero: uint(size),
+        fi: int(size) = zero: int(size),
+        fu: uint(size) = 0: uint(size);
+
+  if (!ti) {
+    compilerWarning("ti is zero");
+    writeln("ti is zero");
+  } else {
+    writeln("ti is nonzero");
+    compilerWarning("ti is nonzero");
+  }
+
+  if (!tu) {
+    writeln("tu is zero");
+    compilerWarning("tu is zero");
+  } else {
+    writeln("tu is nonzero");
+    compilerWarning("tu is nonzero");
+  }
+
+  if (!fi) {
+    writeln("fi is zero");
+    compilerWarning("fi is zero");
+  } else {
+    writeln("fi is nonzero");
+    compilerWarning("fi is nonzero");
+  }
+
+  if (!fu) {
+    writeln("fu is zero");
+    compilerWarning("fu is zero");
+  } else {
+    writeln("fu is nonzero");
+    compilerWarning("fu is nonzero");
+  }
+}
+
+testType(8);
+testType(16);
+testType(32);
+testType(64);
+
Index: test/expressions/bradc/notIntParam.good
===================================================================
--- test/expressions/bradc/notIntParam.good	(revision 0)
+++ test/expressions/bradc/notIntParam.good	(revision 0)
@@ -0,0 +1,32 @@
+notIntParam.chpl:43: warning: ti is nonzero
+notIntParam.chpl:43: warning: tu is nonzero
+notIntParam.chpl:43: warning: fi is zero
+notIntParam.chpl:43: warning: fu is zero
+notIntParam.chpl:44: warning: ti is nonzero
+notIntParam.chpl:44: warning: tu is nonzero
+notIntParam.chpl:44: warning: fi is zero
+notIntParam.chpl:44: warning: fu is zero
+notIntParam.chpl:45: warning: ti is nonzero
+notIntParam.chpl:45: warning: tu is nonzero
+notIntParam.chpl:45: warning: fi is zero
+notIntParam.chpl:45: warning: fu is zero
+notIntParam.chpl:46: warning: ti is nonzero
+notIntParam.chpl:46: warning: tu is nonzero
+notIntParam.chpl:46: warning: fi is zero
+notIntParam.chpl:46: warning: fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
Index: test/expressions/bradc/notInt.chpl
===================================================================
--- test/expressions/bradc/notInt.chpl	(revision 0)
+++ test/expressions/bradc/notInt.chpl	(revision 0)
@@ -0,0 +1,39 @@
+config const nonzero = 42,
+             zero = 0;
+
+proc testType(param size) {
+  var ti: int(size) = nonzero: int(size),
+      tu: uint(size) = nonzero: uint(size),
+      fi: int(size) = zero: int(size),
+      fu: uint(size) = 0: uint(size);
+
+  if (!ti) {
+    writeln("ti is zero");
+  } else {
+    writeln("ti is nonzero");
+  }
+
+  if (!tu) {
+    writeln("tu is zero");
+  } else {
+    writeln("tu is nonzero");
+  }
+
+  if (!fi) {
+    writeln("fi is zero");
+  } else {
+    writeln("fi is nonzero");
+  }
+
+  if (!fu) {
+    writeln("fu is zero");
+  } else {
+    writeln("fu is nonzero");
+  }
+}
+
+testType(8);
+testType(16);
+testType(32);
+testType(64);
+
Index: test/expressions/bradc/notInt.good
===================================================================
--- test/expressions/bradc/notInt.good	(revision 0)
+++ test/expressions/bradc/notInt.good	(revision 0)
@@ -0,0 +1,16 @@
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
+ti is nonzero
+tu is nonzero
+fi is zero
+fu is zero
Index: spec/Expressions.tex
===================================================================
--- spec/Expressions.tex	(revision 23709)
+++ spec/Expressions.tex	(working copy)
@@ -1034,11 +1034,17 @@
 \index{\!@\chpl{!}}
 \index{operators!\!@\chpl{!}}
 
-The logical negation operator is predefined as follows:
+The logical negation operator is predefined for booleans and integers
+as follows:
+
 \begin{chapel}
 proc !(a: bool): bool
+proc !(a: int(?w)): bool
+proc !(a: uint(?w)): bool
 \end{chapel}
-The result is the logical negation of the operand.
+For the boolean form, the result is the logical negation of the
+operand.  For the integer forms, the result is true if the operand is
+zero and false otherwise.
 
 \subsection{The Logical And Operator}
 \label{Logical_And_Operators}
Index: spec/TODO
===================================================================
--- spec/TODO	(revision 23709)
+++ spec/TODO	(working copy)
@@ -23,7 +23,10 @@
 * add a syntax production for index-expressions that duplicates the
   logic of call-expressions
 
+* add param prototypes for ! operator (and other operators whose
+  param versions aren't documented?)
 
+
 Ranges
 ------
 * should we define ranges/domains as being array-like where the
Index: modules/internal/ChapelBase.chpl
===================================================================
--- modules/internal/ChapelBase.chpl	(revision 23709)
+++ modules/internal/ChapelBase.chpl	(working copy)
@@ -468,6 +468,8 @@
   // logical operations on primitive types
   //
   inline proc !(a: bool) return __primitive("!", a);
+  inline proc !(a: int(?w)) return (a == 0);
+  inline proc !(a: uint(?w)) return (a == 0);
   
   inline proc isTrue(a: bool) return a;
   inline proc isTrue(param a: bool) param return a;
@@ -475,6 +477,8 @@
   proc isTrue(a: integral) { compilerError("short-circuiting logical operators not supported on integers"); }
   
   inline proc !(param a: bool) param return __primitive("!", a);
+  inline proc !(param a: int(?w)) param return __primitive("!", a);
+  inline proc !(param a: uint(?w)) param return __primitive("!", a);
   
   //
   // bitwise operations on primitive types
------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers

Reply via email to