Ada Planet

🔒
❌ About FreshRSS
There are new articles available, click to refresh the page.
Yesterday — 28 July 2021News from the Ada programming language world

Return an array with one element in Ada

I'm trying to return an array which only has one element from a function. I've tried a few flavors and still can't find what it wants me to do:

with Ada.Text_IO; use Ada.Text_IO;

procedure Beer_Lists is
    type Beer is (Guinness, PBR, Budweiser);
    type Beer_List is array (Positive range <>) of Beer;

    function Beer_I_Like return Beer_List;

    function Beer_I_Like return Beer_List is
    begin
        -- error: Positional aggregate cannot have one component
        -- error: write instead "Beer_List'First => ..."
        return (Guinness);

        -- error: expected type "Beer_List"
        -- error: found type "Beer"
        -- return Guinness;

        -- error: prefix for "First" attribute must be constrained array
        -- return (Beer_List'First => Guinness);

        -- error: named association cannot follow positional association
        -- error: "=>" expected (positional association cannot followed named association)
        -- return (Beer_List'First => Guinness, Guinness);

        -- error: named association cannot follow positional association
        -- return (Guinness, Beer_List'First => Guinness);

        -- error: prefix of attribute must be a name
        -- error: qualify expression to turn it into a name
        -- return (Guinness)'First => Guinness;

        -- error: prefix for "First" attribute must be contrained array
        -- error: missing ";"
        -- return Beer_List'First => Guinness;
    end Beer_I_Like;
begin
    for B of Beer_I_Like loop
        Put_Line (B'Image);
    end loop;
end Beer_Lists;
Before yesterdayNews from the Ada programming language world

AWS, default project, problem with wblocks.adb

I am simply starting a default full AWS app in Gnat Studio and I get an error that wblocks.adb is not found.

It highlights:

with WBlocks; 

As the error.

submitted by /u/linuxman1929
[link] [comments]

ADA Spark, is it open source or not?

This has never been clear to me. Is it possible to do ADA Spark for an open source project without paying?

Also, as a second question and maybe a far-fetched thing, could I write an ADA spark compiler and distribute it without breaking the license?

submitted by /u/linuxman1929
[link] [comments]

The HAC scripts invasion (follow-up)

26 July 2021 at 19:18

As a follow-up of another post about converting bash (Linux) or cmd (Windows) scripts to HAC scripts, here is a fresh example.

I needed to improve an existing cmd script for benchmarking compression software, with the possibility of switching various subsets separately: that is re-run this subset of methods, or that other subset, or the full tests (long!), etc.

Of course, if you use a real language instead of command-line interpreter ones, there is an obvious solution: you can define a set and you can programmatically flip the membership switches.

In Ada, it looks like

  type Category is (
    Reduce_Shrink,
    Deflate,
    Deflate_External,
    BZip2_External,
    PPMd_External,
    LZMA_7z,
    LZMA_3,
    TAR,
    Preselection
  );

  cat_set : array (Category) of Boolean;
The good news is that you can run an Ada program exactly like a script by using HAC (the HAC Ada Compiler). That is, it runs immediately (with HAC), and HAC doesn't drop .ali, .o, .bexch, .tmp, .exe files which are too much waste for the sake of running a small script-like job.

Below are screenshots of the quick development of bench.adb using the LEA editor, where you can punch F4 to check eventual errors. If there is one, you get instantly to the offending line / column point.

This script is part of the Zip-Ada project and is very helpful for developing and testing new compression methods.

 

Click to enlarge


Click to enlarge


Ada language understanding: a child of a renamed package

Let's have, say, package with child package(s):

-- a.ads
package A is
end A;

-- a-b.ads
package A.B is
end A.B;

Now I want to rename A to X:

-- x.ads
package X renames A;

The question one is: will a child package A.B be accessible as being renamed to X.B too?

-- test.adb
with X.B; -- Is it ok? Why?
...

The question two is: is it legal to make a child of a renamed package, say X.C?

It looks like in GNAT it is renamed, but is it standard behaviour? Some reading of a 2005 and 2012 LRM doesn't clear that for me. Can you please point me to sections in LRM answering my questions?

Idiomatic Way to Pass an Empty Enumeration to a Generic in Ada

I'm instantiating a generic package with an enumeration to access one of multiple values and use in subprogram overloading. I want to have a well-defined, compile-time checked set of values I can use and look up.

generic
    -- Different types because we don't want to ensure we never put
    -- beer in a wine class, or wine in a beer stein.  Our inventory
    -- never changes, for... reasons.
    type Wine is (<>);
    type Beer is (<>);
package Bar is
    type Wine_Glass is null record;
    type Beer_Stein is null record;

    -- Unopened cases/bottles of each.
    type Wine_Inventory is array (Wine) of Natural;
    type Beer_Inventory is array (Beer) of Natural;

    procedure Pour (G : in out Wine_Glass; W : in Wine);
    procedure Pour (S : in out Beer_Stein; B : in Beer);
end Bar;

What's the idiomatic to describe an empty enumeration?

with Bar;
procedure Wine_Tasting is
    type Sampling_Wine is (Tempranillo, Zinfandel, Merlot);
    pragma Unreferenced (Tempranillo, Zinfandel, Merlot);

    type No_Beer is (None);
    package Wine_Tasting_Bar is new Bar(Wine => Sampling_Wine, Beer => No_Beer);
    Stein : Wine_Tasting_Bar.Beer_Stein;
begin
    Wine_Tasting_Bar.Pour (Stein, None); -- legal!
end Wine_Tasting;

Is there a way to describe this such that Beer is an enumeration with no values, so that Pour can never been called with a Beer?

[Generics] Default formal type?

[SOLVED: see answer by /u/OneWingedShark]


Is it possible to define a default for generic formal types? For example, in the following code, if U is not provided, then it should default to T. Thank you.

generic type T (<>); type U (<>); package P is end P; 
submitted by /u/Taikal
[link] [comments]

Gnat bug or mistaken program?

20 July 2021 at 13:02
I get this error on my program from the menu build => check semantic exponent must be of type Natural, found type "Standard.Float". Should not 10 ** (log10 a + log10 b) = a*b? This is my compiler build: GNAT Studio Community 2020 (20200427) hosted on x86_64-pc-mingw32 GNAT Community 2020
  • 20 July 2021 at 13:02

Pico Ada Libraries Release 0.5.0

20 July 2021 at 04:58

I’ve tagged release 0.5.0 of the rp2040_hal, pico_bsp, and pico_examples libraries for the Raspberry Pi Pico.

In addition to this month’s library updates, I’ve begun making YouTube videos about developing Ada on the Raspberry Pi Pico, starting with a debug setup and serial console library.

rp2040_hal 0.5.0

New features

UART enhancements

RP.UART now allows configuration of baud, word size, parity, and stop bits via the UART_Configuration record. The default values for the UART_Configuration record represent the typical 115200 8n1 setup.

The UART now has a Send_Break procedure, which holds TX in an active state (usually low) for at least two frame periods. Some protocols use the UART break condition to indicate the start of a new packet.

RP.UART.Receive now sets Status = Busy and returns immediately if a break condition is detected.

UART Transmit and Receive procedures now return as soon as all words have been delivered to the FIFO. FIFO status is exposed by the Transmit_Status and Receive_Status functions. This interface is the same as the I2C and SPI drivers.

The uart_echo example has been updated to demonstrate these new features.

RTC driver

The real time clock is now exposed by the RP.RTC package. It implements the HAL.Real_Time_Clock interface for getting and setting the date and time. An example project demonstrates use of the RTC. RTC alarm interrupts are not yet implemented.

Interpolator driver

The RP2040 has two interpolators per core embedded in the SIO peripheral. The RP.Interpolator package make their registers available. Some of the registers in this block support single-cycle operation, so it would be counter productive to wrap them up in procedures that may not be inlined by the compiler. There are examples in the datasheet for working with the interpolators, but I’m still trying to wrap my head around it, so there is no example here yet.

Breaking changes

UART.Enable is replaced with UART.Configure

To match the nomenclature of the other serial drivers (SPI, I2C), RP.UART now has a Configure procedure instead of Enable.

I2C addresses should include the R/W bit

The RP.I2C driver was expecting 7-bit I2C addresses to not include the R/W bit in the LSB. This was inconsistent with the other HAL.I2C implementations and would result in incorrect I2C addressing. Now, 7-bit I2C addresses should be represented as a UInt8 with the LSB set to 0. If this breaks your code, shift your I2C address left by one bit.

Bugs fixed

Improper use of the Pack clause

The Pack clause was used to enforce the memory layout of some records.

It is important to realize that pragma Pack must not be used to specify the exact representation of a data type, but to help the compiler to improve the efficiency of the generated code. Source

The Pack clause has been replaced with Component_Size and Size clauses where necessary. Thanks to @onox for pointing this out!

Use of access PIO_Device as a type discriminant

Projects depending on pico_bsp failed gnatprove in SPARK mode as the Pico.Audio_I2S package was using not null access PIO_Device as a discriminant. PIO_Device is now tagged and Pico.Audio_I2S uses not null access PIO_Device'Class, which is valid under SPARK. gnatprove still throws many warnings about side effects in the rp2040_hal drivers, but no fatal errors.

RP.ADC.Read_Microvolts was rounding incorrectly

Read_Microvolts was using Integer arithmetic to calculate VREF / Analog_Value'Last, which does not divide evenly for common VREF values. When that value was multiplied by an ADC reading, Read_Microvolts would return lower than expected results. Read_Microvolts now uses floating point to multiply ADC counts before converting the return value to Integer.

UART Transmit and Receive did not respect Timeout

The UART driver has been modified to use RP.Timer to implement timeouts and monitor FIFO status, similar to RP.SPI and RP.I2C.

SPI Transmit was nonblocking

The SPI Transmit procedure would return immediately after the last byte was written to the FIFO, but before the FIFO became empty. This behavior breaks some drivers that depend on all bytes being clocked out before proceeding. A configuration flag for Blocking behavior has been added and defaults to True.

Calling protected base class' constructor?

[SOLVED: see answer by OneWingedShark]


How would you call a protected base constructor in Ada? Like in the C++ code below. Thank you.


class A { int _i; protected: A (int i) { _i = i; } }; class B : public A { public: B () : A (1) {} }; 
submitted by /u/Taikal
[link] [comments]

[Generics] Working around premature usage of incomplete type?

The code below does not compile because of a premature usage of an incomplete type. Fair, but... can it be fixed, while accepting any type? I was thinking about moving the "offending" function to a child package, to be instantiated by client code after T has been defined, but I have no idea if and how that can be done. Any help? Thank you.


EDIT: Actually, I don't know if T could be any type, because F returns a copy, and that excludes limited types, doesn't it? I was thinking with a C++ mindset, where F would have returned a reference instead of a copy.


generic type T (<>); type Access_T is access T; package P is type R (<>) is private; function F (Y : R) return T; private type R is record A : Access_T; end record; function F (Y : R) return T -- error: return type cannot be a formal incomplete type is (Y.A.all); end P; 
submitted by /u/Taikal
[link] [comments]

Generics: `type T (<>);`?

[SOLVED: see answer by OneWingedShark]


What does the formal type of the second generic mean? Thank you.

generic type T (<>) is private; -- Any nonlimited type. package P is end P; generic type T (<>); -- ??? package Q is end Q; 
submitted by /u/Taikal
[link] [comments]

Which Editor/IDE/Environment(s) do you use?

This doesn't seem to get talked about much and no tutorials seem to mention it, so I'm curious what people are writing Ada in, especially after seeing the Jetbrain's State of Developer Ecosystem report come out.

View Poll

submitted by /u/rad_pepper
[link] [comments]

Architecture Detection in Ada

I'm using Alire, and in general, I'm trying to figure out how to detect which machine architecture is being built.

I'm going to need this information for several other things, but for this case, I want to make sure that my inline assembly is going to work:

with System.Machine_Code; procedure Breakpoint is begin System.Machine_Code.Asm("int3", Volatile => True); end Breakpoint; 
submitted by /u/rad_pepper
[link] [comments]
❌