OBNC Sets incorrect output
If you build OBNC on a 64 bit system with --c-int-type=long you can use integers between 0-63 in sets. I confirmed this using the code provided on the FAQ page at the OBNC site (slightly modified because I removed the procedure): MODULE setSize; IMPORT Out; VAR x : INTEGER; s : SET; BEGIN x := -1; s :=-{}; REPEAT INC(x); EXCL(s,x) UNTIL s = {}; Out.Int(x,4); Out.Ln END setSize. The output is 63. However, the following code should produce the output : 31 32. MODULE testsets; IMPORT Out; VAR i : INTEGER; x,y,z : SET; BEGIN x := {31,32,33}; y := {33,34,35}; z := x - y; FOR i := 31 TO 35 DO IF i IN z THEN Out.Int(i,4) END END; Out.Ln END testsets. But it doesn't. The output is 31. Other set operations also produce output limited to a maximum set size of 31. -- Sent with https://mailfence.com Secure and private email
If the largest set element is 31, the set size is 32 {0..31} Jörg
Am 20.03.2022 um 10:37 schrieb Joe Turner <medianjoe@mailfence.com>:
If you build OBNC on a 64 bit system with --c-int-type=long you can use integers between 0-63 in sets. I confirmed this using the code provided on the FAQ page at the OBNC site (slightly modified because I removed the procedure):
MODULE setSize; IMPORT Out; VAR x : INTEGER; s : SET; BEGIN x := -1; s :=-{}; REPEAT INC(x); EXCL(s,x) UNTIL s = {}; Out.Int(x,4); Out.Ln END setSize.
The output is 63.
However, the following code should produce the output : 31 32.
MODULE testsets; IMPORT Out; VAR i : INTEGER; x,y,z : SET; BEGIN x := {31,32,33}; y := {33,34,35}; z := x - y; FOR i := 31 TO 35 DO IF i IN z THEN Out.Int(i,4) END END; Out.Ln END testsets.
But it doesn't. The output is 31. Other set operations also produce output limited to a maximum set size of 31. -- Sent with https://mailfence.com Secure and private email -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
Hi Jorg, Sorry, I don't understand your point. I know that the set size is 32 {0..31} but if if OBNC is built with --c-int-type=long it should be 64 {0..63}. So why is my code not giving the correct output for sets which contain elements > 31? March 20, 2022 12:20:44 PM CET "Jörg" <joerg.straube@iaeth.ch> wrote:If the largest set element is 31, the set size is 32 {0..31} Jörg Am 20.03.2022 um 10:37 schrieb Joe Turner <medianjoe@mailfence.com>: If you build OBNC on a 64 bit system with --c-int-type=long you can use integers between 0-63 in sets. I confirmed this using the code provided on the FAQ page at the OBNC site (slightly modified because I removed the procedure): MODULE setSize; IMPORT Out; VAR x : INTEGER; s : SET; BEGIN x := -1; s :=-{}; REPEAT INC(x); EXCL(s,x) UNTIL s = {}; Out.Int(x,4); Out.Ln END setSize. The output is 63. However, the following code should produce the output : 31 32. MODULE testsets; IMPORT Out; VAR i : INTEGER; x,y,z : SET; BEGIN x := {31,32,33}; y := {33,34,35}; z := x - y; FOR i := 31 TO 35 DO IF i IN z THEN Out.Int(i,4) END END; Out.Ln END testsets. But it doesn't. The output is 31. Other set operations also produce output limited to a maximum set size of 31. -- Sent with https://mailfence.com Secure and private email-- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon -- Sent with https://mailfence.com Secure and private email
On Sun, Mar 20, 2022, at 1:03 PM, Joe Turner wrote:
Sorry, I don't understand your point. I know that the set size is 32 {0..31} but if if OBNC is built with --c-int-type=long it should be 64 {0..63}. So why is my code not giving the correct output for sets which contain elements > 31?
I don't use that compiler but I guess it's a bug of OBNC and you should contact the author. The SET type is a special one and it's not enough to change the underlying type to support 64 bit values because there are also SET constants like {0, 1, 4 .. 7}. This means also the code generator should be adapted to support 64 bit SET (and it's a non trivial change). If you switched to long type just for the SET type, it's better that you use a library (based on SET). -- Diego Sardina
Hi Diego, Thanks. I just assumed that if using long type I would be able to use SET up to 63, but it seems it's not the case. Not sure if it's what the author intended or not. I don't need sets with more than 64 elements, but more than 32, so it would have been handy. Anyway, I'll look around for a library, or I guess it wouldn't be too hard to write one myself using an array of boolean. March 20, 2022 4:01:34 PM CET Diego Sardina <dsar@eml.cc> wrote:On Sun, Mar 20, 2022, at 1:03 PM, Joe Turner wrote:
Sorry, I don't understand your point. I know that the set size is 32 {0..31}
but if if OBNC is built with --c-int-type=long it should be 64 {0..63}. So why is my code not giving the correct output for sets which contain elements > 31?
I don't use that compiler but I guess it's a bug of OBNC and you should contact the author. The SET type is a special one and it's not enough to change the underlying type to support 64 bit values because there are also SET constants like {0, 1, 4 .. 7}. This means also the code generator should be adapted to support 64 bit SET (and it's a non trivial change). If you switched to long type just for the SET type, it's better that you use a library (based on SET). -- Diego Sardina -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon -- Sent with https://mailfence.com Secure and private email
On Sun, Mar 20, 2022, at 4:44 PM, Joe Turner wrote:
[...] Anyway, I'll look around for a library, or I guess it wouldn't be too hard to write one myself using an array of boolean.
Or an array of SET ;-) MODULE Sets; IMPORT SYSTEM; CONST size = SYSTEM.SIZE(SET) * 8; PROCEDURE Clear* (VAR s: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s)-1 DO s[i] := {} END END Clear; PROCEDURE Fill* (VAR s: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s)-1 DO s[i] := {0 .. size-1} END END Fill; PROCEDURE Include* (VAR s: ARRAY OF SET; x: INTEGER); BEGIN (* ASSERT(x DIV size < LEN(s)); *) INCL(s[x DIV size], x MOD size) END Include; PROCEDURE Exclude* (VAR s: ARRAY OF SET; x: INTEGER); BEGIN (* ASSERT(x DIV size < LEN(s)); *) EXCL(s[x DIV size], x MOD size) END Exclude; PROCEDURE In* (s: ARRAY OF SET; x: INTEGER): BOOLEAN; BEGIN (* ASSERT(x DIV size < LEN(s)); *) RETURN x MOD size IN s[x DIV size] END In; PROCEDURE Subset* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) <= LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] + s2[i] = s1[i]) DO INC(i) END; RETURN i = LEN(s1) END Subset; PROCEDURE Count* (s: ARRAY OF SET): INTEGER; VAR i, n: INTEGER; BEGIN n := 0; FOR i := 0 TO (LEN(s)*size)-1 DO IF (i MOD size) IN s[i DIV size] THEN INC(n) END END; RETURN n END Count; PROCEDURE Empty* (s: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN i := 0; WHILE (i < LEN(s)) & (s[i] = {}) DO INC(i) END; RETURN i = LEN(s) END Empty; PROCEDURE Same* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) = LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] = s2[i]) DO INC(i) END; RETURN i = LEN(s1) END Same; PROCEDURE Distinct* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) = LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] * s2[i] = {}) DO INC(i) END; RETURN i = LEN(s1) END Distinct; PROCEDURE Union* (VAR s1, s2: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1)-1 DO s1[i] := s1[i] + s2[i] END END Union; PROCEDURE Difference* (VAR s1, s2: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1) DO s1[i] := s1[i] - s2[i] END END Difference; PROCEDURE Intersection* (VAR s1, s2, s3: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1) DO s3[i] := s1[i] * s2[i] END END Intersection; END Sets.
Diego, Many thanks! That's saved me some work. :-). Joe March 20, 2022 5:58:11 PM CET Diego Sardina <dsar@eml.cc> wrote:On Sun, Mar 20, 2022, at 4:44 PM, Joe Turner wrote:
[...] Anyway, I'll look around for a library, or I guess it wouldn't be too
hard to write one myself using an array of boolean.
Or an array of SET ;-) MODULE Sets; IMPORT SYSTEM; CONST size = SYSTEM.SIZE(SET) * 8; PROCEDURE Clear* (VAR s: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s)-1 DO s[i] := {} END END Clear; PROCEDURE Fill* (VAR s: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s)-1 DO s[i] := {0 .. size-1} END END Fill; PROCEDURE Include* (VAR s: ARRAY OF SET; x: INTEGER); BEGIN (* ASSERT(x DIV size < LEN(s)); *) INCL(s[x DIV size], x MOD size) END Include; PROCEDURE Exclude* (VAR s: ARRAY OF SET; x: INTEGER); BEGIN (* ASSERT(x DIV size < LEN(s)); *) EXCL(s[x DIV size], x MOD size) END Exclude; PROCEDURE In* (s: ARRAY OF SET; x: INTEGER): BOOLEAN; BEGIN (* ASSERT(x DIV size < LEN(s)); *) RETURN x MOD size IN s[x DIV size] END In; PROCEDURE Subset* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) <= LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] + s2[i] = s1[i]) DO INC(i) END; RETURN i = LEN(s1) END Subset; PROCEDURE Count* (s: ARRAY OF SET): INTEGER; VAR i, n: INTEGER; BEGIN n := 0; FOR i := 0 TO (LEN(s)*size)-1 DO IF (i MOD size) IN s[i DIV size] THEN INC(n) END END; RETURN n END Count; PROCEDURE Empty* (s: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN i := 0; WHILE (i < LEN(s)) & (s[i] = {}) DO INC(i) END; RETURN i = LEN(s) END Empty; PROCEDURE Same* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) = LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] = s2[i]) DO INC(i) END; RETURN i = LEN(s1) END Same; PROCEDURE Distinct* (s1, s2: ARRAY OF SET): BOOLEAN; VAR i: INTEGER; BEGIN (* ASSERT(LEN(s1) = LEN(s2)); *) i := 0; WHILE (i < LEN(s1)) & (s1[i] * s2[i] = {}) DO INC(i) END; RETURN i = LEN(s1) END Distinct; PROCEDURE Union* (VAR s1, s2: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1)-1 DO s1[i] := s1[i] + s2[i] END END Union; PROCEDURE Difference* (VAR s1, s2: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1) DO s1[i] := s1[i] - s2[i] END END Difference; PROCEDURE Intersection* (VAR s1, s2, s3: ARRAY OF SET); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s1) DO s3[i] := s1[i] * s2[i] END END Intersection; END Sets. -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon -- Sent with https://mailfence.com Secure and private email
It must be a bug, otherwise why would the output of the first module be correct and the second one not? March 20, 2022 4:01:34 PM CET Diego Sardina <dsar@eml.cc> wrote:On Sun, Mar 20, 2022, at 1:03 PM, Joe Turner wrote:
Sorry, I don't understand your point. I know that the set size is 32 {0..31}
but if if OBNC is built with --c-int-type=long it should be 64 {0..63}. So why is my code not giving the correct output for sets which contain elements > 31?
I don't use that compiler but I guess it's a bug of OBNC and you should contact the author. The SET type is a special one and it's not enough to change the underlying type to support 64 bit values because there are also SET constants like {0, 1, 4 .. 7}. This means also the code generator should be adapted to support 64 bit SET (and it's a non trivial change). If you switched to long type just for the SET type, it's better that you use a library (based on SET). -- Diego Sardina -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon -- Sent with https://mailfence.com Secure and private email
participants (3)
-
Diego Sardina -
Joe Turner -
Jörg