Instantiation of package for a Generic type is abandoned by compiler in Ada - instantiation

Ok so I trying to run my code which calls a package dirforinv.adb:
WITH Text_IO;
WITH Ada.Numerics.Generic_Real_Arrays;
WITH Ada.Numerics.Generic_Elementary_functions;
WITH Ada.Strings.Fixed;
WITH dirforinv;
PROCEDURE levfordir IS
J : CONSTANT Integer := 100;
TYPE Real IS DIGITS 13;
PACKAGE Real_IO IS NEW Text_IO.Float_IO (Real);
PACKAGE Int_IO IS NEW Text_IO.Integer_IO (Integer);
TYPE Gen_arr IS ARRAY (INTEGER RANGE <>, INTEGER RANGE <>) OF Real;
TYPE var_array IS ARRAY (INTEGER RANGE <>) OF Real;
PACKAGE Real_arrays IS NEW Ada.numerics.Generic_Real_Arrays (Real);
USE Real_arrays;
PACKAGE sdirect IS NEW dirforinv (var_array, Gen_arr);
PACKAGE Math IS NEW Ada.numerics.Generic_Elementary_functions (Real);
USE Math;
But I get the following error.
PACKAGE sdirect IS NEW dirforinv (var_array, Gen_arr);
|
>>> expect float type in instantiation of "Real"
>>> instantiation abandoned
I don't understand this as dirforinv expects 5 arrays, 1 of var_array and 4 of Gen_arr. The .ads for dirforinv is below.
WITH Profiles,
Profiles.Inputs,
Profiles.Conversion,
Profiles.Provide_Grids,
Profiles.Multipolar,
Profiles.Unno_Solver,
Profiles.Outputs,
Text_IO, Ada.Numerics.Generic_Elementary_Functions;
USE Profiles;
GENERIC
TYPE Real IS DIGITS <>;
TYPE Gen_arr IS ARRAY (INTEGER RANGE <>, INTEGER RANGE <>) OF REAL;
TYPE var_array IS ARRAY (INTEGER RANGE <>) OF REAL;
PACKAGE dirforinv IS
PROCEDURE direct (Variables : IN var_array;
StoI, StoQ, StoU, StoV : OUT Gen_arr );
END dirforinv;
Thanks for any advice.

The declaration of dirforinv (horrid name, by the way) is
GENERIC
TYPE Real IS DIGITS <>;
TYPE Gen_arr IS ARRAY (INTEGER RANGE <>, INTEGER RANGE <>) OF REAL;
TYPE var_array IS ARRAY (INTEGER RANGE <>) OF REAL;
PACKAGE dirforinv IS
which has three generic formal parameters; your attempted instantiation is
PACKAGE sdirect IS NEW dirforinv (var_array, Gen_arr);
which only has two.

Related

Julia: ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

I am trying to multiply two array via the Einsum package (uses Meta Programming). I get the following error if I use the #einsum macro with elements of a struct but not if I copy the element beforehand. Can someone explain?
using Einsum
struct MyStruct
a::Array
end
s1 = MyStruct(rand(5, 2))
s2 = MyStruct(rand(6, 2))
# does not work (ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol)
#einsum result[i, j] := s1.a[i, k] * s2.a[j, k]
# does work
s1_a = s1.a
s2_a = s2.a
#einsum result[i, j] := s1_a[i, k] * s2_a[j, k]
The macro #einsum assumes, in the function extractindices which it uses, that the arrays are simple names (i.e. Symbols like s1_a), not expressions like s1.a or function calls or some such thing. It has simply not been written to accomodate indexing of expressions. The package Einsum has not been updated in 4 years, there might be other packages that can achieve this.

Converting arrays from signed to integer in VHDL?

I have declared an array
type datastream is array(0 to 10) of signed (5 downto 0);
For simulation, I want to display the array as integer-numbers
So I created
type datastream_int is array(0 to 10) of integer;
and
signal DIN_ARRAY: datastream;
signal DIN_ARRAY_int: datastream_int;
...
DIN_ARRAY_real <= datastream_int(DIN_ARRAY);
But it fails. How to convert it? Dont want to use a for loop
The numeric_std package, that I assume you are using, provides a to_integer function to convert from a single signed value to a single integer object. For an array, you're going to have to use a for loop. Something like this:
for i in DIN_ARRAY'range loop
DIN_ARRAY_int <= to_integer(DIN_ARRAY(i));
end loop;
You could also provide a conversion function (it will also contain a for loop)
function datastream_to_datastream_int( d : datastream ) return datastream_int is
variable r : datastream_int;
begin
for i in d'range loop
r(i) := to_integer(d(i));
end loop;
return r;
end function;
....
--no loop required
DIN_ARRAY_int <= datastream_to_datastream_int(DIN_ARRAY);
So, there will be a for loop somewhere.
Your code fails because you have attempted a type conversion, which is only allowed between similar types - ie. array or record types where the element types match between the two types.
PS. VHDL 2008 provides an integer_vector type in the std.standard library (which is included by default) which may help by allowing you to do this:
signal DIN_ARRAY_int: integer_vector(DIN_ARRAY'range);
If you did decide to keep datastream_int as per your original, you could type convert it to an integer_vector, because the types are similar:
my_iv <= integer_vector(DIN_ARRAY_int);

"operator symbol not allowed for generic subprogram" from Ada

I want to make subprogram for adding array's elements with Ada.
subprogram "Add_Data" have 3 parameters-
first parameter = generic type array (array of INTEGER or array of REAL)
second parameter = INTEGER (size of array)
third parameter = generic type sum (array of INTEGER -> sum will be INTEGER, array of REAL -> sum will be REAL)
I programmed it from ideone.com.
(I want to see just result by array of INTEGER. After that, I will test by array of REAL)
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
procedure test is
generic
type T is private;
type Tarr is array (INTEGER range <>) of T;
--function "+" (A,B : T) return T;
--function "+" (A, B : T) return T is
--begin
-- return (A+B);
--end "+";
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T);
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T) is
temp : T;
count : INTEGER;
begin
count := 1;
loop
temp :=temp+ X(count); //<-This is problem.
count := count + 1;
if count > Y then
exit;
end if;
end loop;
Z:=temp;
end Add_Data;
type intArray is array (INTEGER range <>) of INTEGER;
intArr : intArray := (1=>2, 2=>10, 3=>20, 4=>30, 5=>8);
sum : INTEGER;
procedure intAdd is new Add_Data(Tarr=>intArray, T=>INTEGER);
begin
sum := 0;
intAdd(intArr, 5, sum);
put (sum);
end test;
when I don't overload operator "+", It makes error.
"There is no applicable operator "+" for private type "T" defined."
What can I do for this?
If a generic’s formal type is private, then nothing in the generic can assume anything about the type except that it can be assigned (:=) and that it can be compared for equality (=) and inequality (/=). In particular, no other operators (e.g. +) are available in the generic unless you provide them.
The way to do that is
generic
type T is private;
with function "+" (L, R : T) return T is <>;
This tells the compiler that (a) there is a function "+" which takes two T’s and returns a T; and (b) if the actual T has an operator "+" which matches that profile, to allow it as the default.
So, you could say
procedure intAdd is new Add_Data (T => Integer, ...
or, if you didn’t feel like using the default,
procedure intAdd is new Add_Data (T => Integer, "+" => "+", ...
In addition to not knowing how to declare a generic formal subprogram (Wright has shown how to do this for functions), your code has a number of other issues that, if addressed, might help you move from someone who thinks in another language and translates it into Ada into someone who actually uses Ada. Presuming that you want to become such a person, I will point some of these out.
You declare your array types using Integer range <>. It's more common in Ada to use Positive range <>, because people usually refer to positions starting from 1: 1st, 2nd, 3rd, ...
Generics are used for code reuse, and in real life, such code is often used by people other than the original author. It is good practice not to make unstated assumptions about the values clients will pass to your operations. You assume that, for Y > 0, for all I in 1 .. Y => I in X'range and for Y < 1, 1 in X'range. While this is true for the values you use, it's unlikely to be true for all uses of the procedure. For example, when an array is used as a sequence, as it is here, the indices are immaterial, so it's more natural to write your array aggreate as (2, 10, 20, 30, 8). If I do that, Intarr'First = Integer'First and Intarr'Last = Integer'First + 4, both of which are negative. Attempting to index this with 1 will raise Constraint_Error.
Y is declared as Integer, which means that zero and negative values are acceptable. What does it mean to pass -12 to Y? Ada's subtypes help here; if you declare Y as Positive, trying to pass non-positive values to it will fail.
Z is declared mode in out, but the input value is not referenced. This would be better as mode out.
Y is not needed. Ada has real arrays; they carry their bounds around with them as X'First, X'Last, and X'Length. Trying to index an array outside its bounds is an error (no buffer overflow vulnerabilities are possible). The usual way to iterate over an array is with the 'range attribute:
for I in X'range loop
This ensures that I is always a valid index into X.
Temp is not initialized, so it will normally be initialized to "stack junk". You should expect to get different results for different calls with the same inputs.
Instead of
if count > Y then
exit;
end if;
it's more usual to write exit when Count > Y;
Since your procedure produces a single, scalar output, it would be more natural for it to be a function:
generic -- Sum
type T is private;
Zero : T;
type T_List is array (Positive range <>) of T;
with function "+" (Left : T; Right : T) return T is <>;
function Sum (X : T_List) return T;
function Sum (X : T_List) return T is
Result : T := Zero;
begin -- Sum
Add_All : for I in X'range loop
Result := Result + X (I);
end loop Add_All;
return Result;
end Sum;
HTH

Problems with record call in Dymola

The following package 'RecordTest' (example to reproduce an error of a bigger model) contains a Record to define the structure of some data. Further in package 'DataDefintion' two sets of data are defined. Finally this data should be used in package 'UseOfData'. Herein the data sets are read and the sum of all arrays A is evaluated in the function 'FunctionWithData'.
The simulation of model 'FunctionCall' works fine in OpenModelica. In Dymola I get the error: 'For variable package constant RecordTest.UseOfData.ReadData[1].A the subscript RecordTest.UseOfData.ReadData.Index of an array variable is not an integer.'
Do I miss anything? The constant 'Index' is defined as an integer in record 'DataStructure'. Further the model runs in OpenModelica. I don't understand the error of Dymola.
Thanks in advance.
package RecordTest
record DataStructure
constant Integer Index;
Real A[Index];
end DataStructure;
package DataDefinition
constant DataStructure Set1(Index=2, A={1,2});
constant DataStructure Set2(Index=2, A={3,4});
end DataDefinition;
package UseOfData
constant Integer N=2;
constant DataStructure ReadData[N]={DataDefinition.Set1, DataDefinition.Set2};
function FunctionWithData
input Real b;
output Real Result;
protected
Real Array[2];
algorithm
Array := {sum(ReadData[1].A), sum(ReadData[2].A)};
Result := b*sum(Array);
end FunctionWithData;
model FunctionCall
parameter Real b=2;
Real FunctionResult;
equation
FunctionResult = FunctionWithData(b);
end FunctionCall;
end UseOfData;
end RecordTest;
A work-around is to rewrite the package as follows:
package RecordTest
record DataStructure
constant Integer Index;
Real A[:];
end DataStructure;
package DataDefinition
constant DataStructure Set1=DataStructure(Index=2, A={1.0,2.0});
constant DataStructure Set2=DataStructure(Index=2, A={3.0,4.0});
end DataDefinition;
package UseOfData
constant Integer N=2;
constant DataStructure ReadData[N]={DataDefinition.Set1, DataDefinition.Set2};
function FunctionWithData
input Real b;
output Real Result;
protected
Real Array[2];
algorithm
Array := {sum(ReadData[1].A), sum(ReadData[2].A)};
Result := b*sum(Array);
end FunctionWithData;
model FunctionCall
parameter Real b=2;
Real FunctionResult;
equation
FunctionResult = FunctionWithData(b);
end FunctionCall;
end UseOfData;
end RecordTest;
The issues are the "Index" used in A inside a package-constant array of records, and the modifiers instead of binding equation for the package-constant records Set1 and Set2.
(It will also be handled in a future version of Dymola, and I understand the answer is a bit late.)

Integer to real conversion function

Is there a common conversion function to convert a integer type object to a real type in VHDL?
This is for a testbench so synthesizability is a non-issue.
You can convert integer to real as follows:
signal i: integer;
signal R: Real;
...
R <= Real(i);