在SPARK Ada中创建泛型约束数组类型

在SPARK Ada中创建泛型约束数组类型,ada,spark-ada,Ada,Spark Ada,我想制定一个程序来接受通用约束数组,即ECGREADING和EEGREADING: Types declarations: subtype ecgReadingsSize is Natural range 1..3; subtype eegReadingsSize is Natural range 1..10; subtype eegReading is Natural range 0..1; -- eegRReading is 0 or 1 subtype ecgRea

我想制定一个程序来接受通用约束数组,即ECGREADING和EEGREADING:

Types declarations:
   subtype ecgReadingsSize is Natural range 1..3;
   subtype eegReadingsSize is Natural range 1..10;
   subtype eegReading is Natural range 0..1; -- eegRReading is 0 or 1
   subtype ecgReading is Natural range 2..600; -- Max heart rate 220

   
   type ecgReadings is array (ecgReadingsSize) of ecgReading;
   type eegReadings is array (eegReadingsSize) of eegReading;
   type eegPartialSums is array (eegReadingsSize) of Natural;
尝试创建通用过程:

package iocontroller is

   
   generic 
      type ItemType is private;
      type Index_Type is (<>);  -- Any discrete type
      type Array_Type is array (Index_Type range <>) of ItemType;
  procedure startIO(inFileName : String; outFileName : String; List: 
  Array_Type);
包iocontroller是
通用的
类型ItemType是私有的;
类型索引_类型为();--任何离散类型
类型数组\类型是ItemType的数组(索引\类型范围);
过程开始(填充名称:字符串;输出名称:字符串;列表:
阵列(U型);
测试通用产品

procedure Main is --change name

type MyArray is array (Natural range <>) of Natural;

procedure ecgIO is new startIO(
        ItemType => Natural,
        Index_Type => Natural,
        Array_Type => MyArray
    );
过程主要是--更改名称
MyArray类型是Natural的数组(自然范围);
ecgIO程序是新的开始(
ItemType=>Natural,
索引类型=>Natural,
数组类型=>MyArray
);

我认为您只需要约束
Start\u IO
的泛型类型参数
Array\u type
(参见下面的示例)

注意:虽然它不是强制性的,但在这种情况下,您可能希望声明类型而不是子类型,以防止从心电图读数到脑电图读数的意外(隐式)转换,即声明

而不是

EEG\u Reading\u Index
EEG\u Reading
类似

但除此之外:

main.adb

with IO_Controller;

procedure Main is
   
   subtype ECG_Reading_Index is Natural range 1 .. 3;
   subtype ECG_Reading       is Natural range 2 .. 600;
   type    ECG_Readings      is array (ECG_Reading_Index) of ECG_Reading;
   
   procedure Start_ECG_IO is new IO_Controller.Start_IO
     (Item_Type  => ECG_Reading,
      Index_Type => ECG_Reading_Index,
      Array_Type => ECG_Readings);   
   
   
   subtype EEG_Reading_Index is Natural range 1 .. 10;
   subtype EEG_Reading       is Natural range 0 .. 1;
   type    EEG_Readings      is array (EEG_Reading_Index) of EEG_Reading;
   
   procedure Start_EEG_IO is new IO_Controller.Start_IO
     (Item_Type  => EEG_Reading,
      Index_Type => EEG_Reading_Index,
      Array_Type => EEG_Readings);
   

   ECG : ECG_Readings := (others => <>);
   EEG : EEG_Readings := (others => <>);
   
begin
   Start_ECG_IO ("ecg_in", "ecg_out", ECG);
   Start_EEG_IO ("eeg_in", "eeg_out", EEG);
end Main;
带IO_控制器;
主要程序是
亚型心电图读数指数为自然范围1。。3.
亚型ECG_读数为自然范围2。。600;
类型ECG_读数是ECG_读数的数组(ECG_读数索引);
程序启动\u ECG\u IO是新的IO控制器。启动\u IO
(项目类型=>心电图读数,
索引类型=>心电图读数索引,
阵列_类型=>ECG_读数);
亚型脑电图读数指数为自然范围1。。10;
子类型EEG_读数的自然范围为0。。1.
类型EEG_读数是EEG_读数的数组(EEG_读数索引);
程序启动\u EEG\u IO是新的IO控制器。启动\u IO
(项目类型=>脑电图读数,
索引类型=>脑电图读数索引,
阵列类型=>脑电图读数);
心电图:心电图读数:=(其他=>);
脑电图:脑电图读数:=(其他=>);
开始
启动ECG IO(“ECG输入”、“ECG输出”、ECG);
启动脑电图IO(“脑电图输入”,“脑电图输出”,脑电图);
端干管;
io_控制器。ads

package IO_Controller is   
   
   generic 
      type Item_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type) of Item_Type;   --  remove "range <>"
   procedure Start_IO
     (FileName_In  : String; 
      Filename_Out : String;
      List         : Array_Type);
 
end IO_Controller;
程序包IO\U控制器不可用
通用的
类型项\类型为私有;
类型索引_type为();
类型数组\类型是项\类型的数组(索引\类型)—删除“范围”
程序启动
(文件名_In:String;
文件名:字符串;
列表:数组(U类型);
终端IO_控制器;

我认为您只需要约束
Start\u IO
的泛型类型参数
Array\u type
(参见下面的示例)

注意:虽然它不是强制性的,但在这种情况下,您可能希望声明类型而不是子类型,以防止从心电图读数到脑电图读数的意外(隐式)转换,即声明

而不是

EEG\u Reading\u Index
EEG\u Reading
类似

但除此之外:

main.adb

with IO_Controller;

procedure Main is
   
   subtype ECG_Reading_Index is Natural range 1 .. 3;
   subtype ECG_Reading       is Natural range 2 .. 600;
   type    ECG_Readings      is array (ECG_Reading_Index) of ECG_Reading;
   
   procedure Start_ECG_IO is new IO_Controller.Start_IO
     (Item_Type  => ECG_Reading,
      Index_Type => ECG_Reading_Index,
      Array_Type => ECG_Readings);   
   
   
   subtype EEG_Reading_Index is Natural range 1 .. 10;
   subtype EEG_Reading       is Natural range 0 .. 1;
   type    EEG_Readings      is array (EEG_Reading_Index) of EEG_Reading;
   
   procedure Start_EEG_IO is new IO_Controller.Start_IO
     (Item_Type  => EEG_Reading,
      Index_Type => EEG_Reading_Index,
      Array_Type => EEG_Readings);
   

   ECG : ECG_Readings := (others => <>);
   EEG : EEG_Readings := (others => <>);
   
begin
   Start_ECG_IO ("ecg_in", "ecg_out", ECG);
   Start_EEG_IO ("eeg_in", "eeg_out", EEG);
end Main;
带IO_控制器;
主要程序是
亚型心电图读数指数为自然范围1。。3.
亚型ECG_读数为自然范围2。。600;
类型ECG_读数是ECG_读数的数组(ECG_读数索引);
程序启动\u ECG\u IO是新的IO控制器。启动\u IO
(项目类型=>心电图读数,
索引类型=>心电图读数索引,
阵列_类型=>ECG_读数);
亚型脑电图读数指数为自然范围1。。10;
子类型EEG_读数的自然范围为0。。1.
类型EEG_读数是EEG_读数的数组(EEG_读数索引);
程序启动\u EEG\u IO是新的IO控制器。启动\u IO
(项目类型=>脑电图读数,
索引类型=>脑电图读数索引,
阵列类型=>脑电图读数);
心电图:心电图读数:=(其他=>);
脑电图:脑电图读数:=(其他=>);
开始
启动ECG IO(“ECG输入”、“ECG输出”、ECG);
启动脑电图IO(“脑电图输入”,“脑电图输出”,脑电图);
端干管;
io_控制器。ads

package IO_Controller is   
   
   generic 
      type Item_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type) of Item_Type;   --  remove "range <>"
   procedure Start_IO
     (FileName_In  : String; 
      Filename_Out : String;
      List         : Array_Type);
 
end IO_Controller;
程序包IO\U控制器不可用
通用的
类型项\类型为私有;
类型索引_type为();
类型数组\类型是项\类型的数组(索引\类型)—删除“范围”
程序启动
(文件名_In:String;
文件名:字符串;
列表:数组(U类型);
终端IO_控制器;

很抱歉,但是-您需要弄清楚您的实际问题是什么。尤其是因为您的“测试通用过程”片段编译正常。你看到了帮助吗?对不起,但是-你需要弄清楚你的问题到底是什么。尤其是因为您的“测试通用过程”片段编译正常。你看到帮助了吗?是的,我看到这种方法更干净,更简单。非常感谢你的帮助!SPARK Ada是一种非常有挑战性但很有价值的语言。上帝保佑:)是的,我知道这条路是多么的干净和简单。非常感谢你的帮助!SPARK Ada是一种非常有挑战性但很有价值的语言。上帝保佑:)