# Perplexion with Reduction expressions

**URL:** <https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648>\
**Category:** General\
**Tags:** ada\
**Created:** [January 13, 2024, 9:29pm UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648 "2024-01-13T21:29:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 13, 2024, 9:29pm UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/1 "2024-01-13T21:29:23Z")

</div>

Not sure why this works:

```ada
   function Hash (S : String) return Character is
      (S'Reduce (Character'Max, 'a'));

```

…and this works:

```ada
   type Hash_Tracker is record
      Value : Hash_Value;
   end record;

   function Hash_Character (Accumulator: Hash_Value; Symbol: Character) return Hash_Value is
      (Accumulator : Hash_Tracker; Symbol : Character) return Hash_Tracker is
      ((Value => ((Accumulator.Value + Character'Pos (Symbol)) * 17) mod 256));

   function Hash (S : String) return Hash_Value is
      (Hash_Tracker'(S'Reduce (Hash_Character, (Value => 0))).Value);

```

…when this does not:

```ada
   function Hash_Character (Accumulator: Hash_Value; Symbol: Character) return Hash_Value is
      (((Accumulator + Character'Pos (Symbol)) * 17) mod 256);

   function Hash (S : String) return Hash_Value is
      (S'Reduce (Hash_Character, 0));

```

gnat 13.2.1 complains: (file, lines, column omitted)

```
[...] error: missing argument for parameter "Accumulator" in call to "Hash_Character" declared at line [...]

```

The only thing that’s changed is the type passed to `Hash_Character`, and it’s consistent. What am I doing wrong?

_edited an oops_

---

<div class="post-metadata">

**Author:** ![jcmoyer](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jcmoyer/32/373_2.png) [@jcmoyer](https://forum.ada-lang.io/u/jcmoyer)\
**Post date:** [January 14, 2024, 5:03pm UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/2 "2024-01-14T17:03:06Z")

</div>

Works for me on msys2 gnat 13.2.0, godbolt gnat 13.1, godbolt gnat 13.2, but doesn’t appear to work on godbolt trunk. Maybe a compiler regression?

---

<div class="post-metadata">

**Author:** ![anhvofrcaus](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/anhvofrcaus/32/254_2.png) [@anhvofrcaus](https://forum.ada-lang.io/u/anhvofrcaus)\
**Post date:** [January 17, 2024, 11:56pm UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/3 "2024-01-17T23:56:23Z")

</div>

Just wonder where Hash\_Value was defined?

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 18, 2024, 12:15am UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/4 "2024-01-18T00:15:37Z")

</div>

I didn’t think it mattered, but it was defined earlier:

```ada
subtype Hash_Value is Natural range 0 .. 255;

```

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 18, 2024, 12:16am UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/5 "2024-01-18T00:16:12Z")

</div>

I was wondering that. Didn’t think of using Godbold to look into it. Guess I’ll report it via the usual channel.

---

<div class="post-metadata">

**Author:** ![anhvofrcaus](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/anhvofrcaus/32/254_2.png) [@anhvofrcaus](https://forum.ada-lang.io/u/anhvofrcaus)\
**Post date:** [January 18, 2024, 1:49am UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/6 "2024-01-18T01:49:15Z")

</div>

It works fine on my Windows 10 running GNAT Community 2021 (20210519-103) targeting x86\_64-w64-mingw32

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 22, 2024, 5:28am UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/7 "2024-01-22T05:28:31Z")

</div>

I discovered that the problem did not appear when I ran it with `gnat`, only with `alr`. The error is caused by the switch `-gnatVa`, which `alr` activates by default. Godbolt suggests the error goes back at least to 10.2 (the point where I quit trying – I had to switch from `pragma Ada_2022` to `-gnatX` somewhere in there).

According to the gnat manual, `-gnatVa` performs all validity checks. The one that sets this off is  
`-gnatVo`  
` ‘Validity checks for operator and attribute operands.’`  
`Scalar arguments for predefined operators and for attributes are validity checked. This includes all operators in package Standard, the shift operators defined as intrinsic in package Interfaces and operands for attributes such as Pos. Checks are also made on individual component values for composite comparisons, and on the expressions in type conversions and qualified expressions. Checks are also made on explicit ranges using .. (e.g., slices, loops etc). `  
I’ll report that this is the error.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [January 22, 2024, 11:51am UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/8 "2024-01-22T11:51:17Z")

</div>

GCC 14.0.1 of 20240114 fails, without `-gnatVa`, with

```ada
    10. function Hash (S : String) return Hash_Value is
    11. (S'Reduce (Hash_Character, 0));
               |
        >>> error: expected type "Hash_Value" defined at line 4
        >>> error: found type "Standard.Character"

```

GCC 14.0.0 of 20231120 didn’t fail, with or without `-gnatVa`.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [January 22, 2024, 6:39pm UTC](https://forum.ada-lang.io/t/perplexion-with-reduction-expressions/648/9 "2024-01-22T18:39:56Z")

</div>

Thanks; I updated the bug report.
