user3444042
user3444042

Reputation: 21

using macros to constrain lists

I am trying to constrain my list items to be equal to certain values under certain conditions. For that I have devised a define as computed macro that

define <num_prob_constraints'struct_member> "CHECK_and_SET_CONSTRAINTS <lst'exp>" as computed {

    //var cur : list of uint = <lst'exp>.as_a(list of uint);
    var t : uint =  <lst'exp>.as_a(list of uint).size(); 
    print t;
    for i from 1 to 4 {
        result = append(result,"keep ",<lst'exp>,"[",i,"]==",i,"=> ",<lst'exp>,"[",i,"]==389; \n");
    };
};

and in my code I use this macro like this:

struct schedule{
    n : uint;
    sched_w : list of list of int;
    CHECK_and_SET_CONSTRAINTS sched_w;
};

But this does not work. First, it prints some random size (From the macro) instead of the list’s real size. Secondly, I get errors of this sort:

*** Error: '1' is of type 'int', while expecting type 'list of int'.
                      in code generated by macro defined at line 3 in
        sports_sched_macro.e

    keep sched_w[1]==1=> sched_w[1]==389;
             expanded at line 8 in sports_sched.e
CHECK_and_SET_CONSTRAINTS sched_w;

Any ideas on what is wrong here?

Upvotes: 0

Views: 73

Answers (2)

yuvalg
yuvalg

Reputation: 331

Macros are simply code substituts. Their function is simply to replace some string with another (calculated or not) during the parsing phase. This means that the macro will be deployed where you used it in the parsing phase which precedes the generation phase. So, in fact, the list does not exist yet, and you cannot access it’s size and items. More specifically, your macro is deployed this way:

struct schedule {
    n : uint;
    sched_w : list of list of int;
    keep sched_w[1]==2=> sched_w[1]==389;
    keep sched_w[2]==2=> sched_w[2]==389;   
    ...
    ...
};

The error message you received tell you that you cannot access specific list items explicitly (since the list size and item’s values are yet undetermined). If you want to keep your list with size 4, and if the value is 2 you want to replace it with 389, you may need to use the post_generate() method, as you are trying to access values that are already assigned to the list items:

keep sched_w.size()==4;
post_generate() is also{
    for each in sched_w  {
        if (it==2) {it=389};    
    };
}; 

Upvotes: 1

Thorsten
Thorsten

Reputation: 700

Are you sure you want to constrain a 2-dimensional list? This looks a bit different. E.g. for an array schedule[4][1]:

schedule: list of list of int;
keep schedule.size() == 4;
keep for each (sublist) in schedule {
   sublist.size() == 1;
   for each (elem) in sublist { 
      ...
   };
};

Upvotes: 0

Related Questions