Arrays Ada和SPARK标识符“State”未声明或此时不可见

Arrays Ada和SPARK标识符“State”未声明或此时不可见,arrays,function,ada,spark-ada,Arrays,Function,Ada,Spark Ada,我正在Ada上用SPARK方法进行列车自动防护。这是我在SPARK中的规范: package Sensors --# own State,Pointer,State1,State2; --# initializes State,Pointer,State1,State2; is type Sensor_Type is (Proceed, Caution, Danger, Undef); subtype Sensor_Index_Type is Integer range

我正在Ada上用SPARK方法进行列车自动防护。这是我在SPARK中的规范:

package Sensors
--# own State,Pointer,State1,State2;
--# initializes State,Pointer,State1,State2;
  is
    type Sensor_Type is (Proceed, Caution, Danger, Undef);
       subtype Sensor_Index_Type is Integer range 1..3;


  procedure Write_Sensors(Value_1, Value_2, Value_3: in Sensor_Type);
  --# global in out State,Pointer;
  --# derives State from State,Value_1, Value_2, Value_3,Pointer &
  --#                        Pointer from Pointer;
  function Read_Sensor(Sensor_Index: in Sensor_Index_Type) return Sensor_Type;

  function Read_Sensor_Majority return Sensor_Type;

  end Sensors;
这是我的Ada:

   package body Sensors is
      type Vector is array(Sensor_Index_Type) of Sensor_Type;
      State: Vector;

      Pointer:Integer;
      State1:Sensor_Type;
      State2:Sensor_Type;

      procedure Write_Sensors(Value_1, Value_2, Value_3: in Sensor_Type) is
      begin
         State(Pointer):=Value_1;
         Pointer:= Pointer + 1;
         State(Pointer):=Value_2;
         Pointer:= Pointer + 1;
         State(Pointer):=Value_3;
      end Write_Sensors;

      function Read_Sensor (Sensor_Index: in Sensor_Index_Type) return Sensor_Type
      is
         State1:Sensor_Type;
      begin
         State1:=Proceed;
         if  Sensor_Index=1 then
            State1:=Proceed;
         elsif Sensor_Index=2 then
            State1:=Caution;
         elsif Sensor_Index=3 then
            State1:=Danger;
         end if;
         return State1;
      end Read_Sensor;

      function Read_Sensor_Majority return Sensor_Type is
         State2:Sensor_Type;      
      begin
         State2 := state(1);
         return State2;
      end Read_Sensor_Majority;

   begin -- initialization
      State:=Vector'(Sensor_Index_Type =>Proceed);  
      pointer:= 0; 
      State1:=Proceed;
      State2:=Proceed;
   end Sensors;
我想知道为什么在函数Read_Sensor_mainter中不能使用State(1)或任何State()数组值。如果有办法使用它们,我是否应该在SPARK的规格中添加任何东西来实现它

它显示的错误有:

1)Expression contains referenced to variable state which has an undefined value flow error 20
2)the variable state is nether imported nor defined flow error 32
3)the undefined initial value of state maybe used in the derivation of the function value flow error 602

呵呵。嗯,这些绝对是SPARK错误,而不是“花园式”编译器错误

如果能看到这些错误的实际剪切粘贴版本(以及它们所指的行的指示),而不仅仅是一个不完美的抄本,那就太好了。然而,我确实意识到,出于安全/连接的原因,这并不总是可能的

看起来这三个人都在抱怨通过系统的数据流。在不知道它们指的是哪一行的情况下,我能建议的最好方法是尝试手动跟踪系统中的数据流,以查看它们的问题是什么

如果我必须对这里的信息进行粗略猜测,我会说,在例程
Read\u Sensor\u Majority
中,您从
状态(1)
读取值可能有问题,因为它无法知道您之前已将值放入该数组位置

程序包的
begin…end
主体区域中的代码应该注意这一点,除非它本身似乎有编译错误,正如Simon在评论中指出的那样如果您解决了该问题,SPARK可能会了解发生了什么,不再抱怨您的控制流


如果SPARK喜欢在没有通过Ada编译器的代码上吐出“我很困惑”的错误,那么在要求SPARK检查代码之前,最好确保Ada编译器喜欢代码中的纯Ada部分。

您需要将规范更改为只读

function Read_Sensor_Majority return Sensor_Type;
--# global in State;
正如我在上面的评论中所说,我对

State := Vector'(Sensor_Index_Type => Proceed);
但是编译器接受它,所以它必须是正常的。一个小测试表明,它与

State := Vector'(others => Proceed);

还高兴地报告,SPARK GPL 2011工具集现在可用于Mac OS X

(1)我不知道SPARK工具集,但是错误消息没有给您一些提示吗?也许如果你把它们贴出来,我们会帮助你。(2) SPARK Ada必须是合法的Ada,但你最后的州初始化不是。我想你的意思是说
State:=Vector'(其他=>继续)。(3) 你的代码已经变得相当混乱,如果它更整洁,它将帮助我们帮助你!在这里支持西蒙。我怀疑您的“我不能使用状态数组值”等同于编译错误。在没有看到问题中的错误的情况下,我们真的没有任何事情可以继续。好的,我把我得到的错误添加到问题的末尾