如何在特定保留下的列表中生成项目

How to generate items in a list under specific keeps

我想在特定限制下生成列表中的项目。例如:

  dup_offset: list of uint(bits:5);
  keep dup_offset.size() == 3;

  foo_gen() is { 
     for each in dup_offset {
        gen dup_offset[index] keeping {
            read_only(index == 0) => it <= 21;
            read_only(index == 1) => it < dup_offset[0];
            read_only(index == 2) => it < dup_offset[0];
            read_only(index == 2) => it < dup_offset[1];
        };
     };
  }; 

列表是一个全局变量,生成是在一个gen_foo()中完成的。 它没有按照我写的方式编译。 谁能建议我该怎么做?

使用您编写的约束,您的代码可以重写为:

!dup_offset: list of uint(bits:5);
keep dup_offset.size() == 3;
keep for each in dup_offset {
    index == 0 => it <= 21;
    index == 1 => it < prev;
    index == 2 => it < dup_offset[0];
    index == 2 => it < prev;
};    

foo_gen() is { 
    gen dup_offset;
}; 

请注意,您可以删除 keep for each 中的第三个约束,因为它会立即从第二个和第四个约束的组合中隐含。

但是,如果我正确理解您的要求——您需要小于 21 的递减序列——那么约束可以重写如下:

!dup_offset: list of uint(bits:5);
keep dup_offset.size() == 3;
keep for each (a) using index (ind1) in dup_offset {
    ind1 == 0 => a <= 21;
    for each (b) using index (ind2) in dup_offset {
        ind1 > ind2 => a < b;
    };
};


foo_gen() is { 
    gen dup_offset;
}; 

请注意,现在模型不再依赖于列表的大小 - 使列表变大不需要添加更多约束。

'gen keeping' 构造不能用于列表项。 您仍然可以使用关键字 'prev' 对约束进行建模。 如果我没理解错的话,你希望你所有的项目的值都小于或等于 21,并且每个项目都比前面的项目小。

  !dup_offset: list of uint(bits:5);

  foo_gen() is { 
     gen dup_offset keeping {
         it.size() == 3;
         for each in it {
             it <= 21; //all items will be <= 21
             (prev == 0 ? (it == 0) : (it < prev)); // each items is smaller than the previous item
         };
     };
  };