-1
$\begingroup$

I would like to express, using propositional logic, that the statement S will execute at least once in the following nested for-loops:

for(i=L1;i<=H1;i++) for(j=L2;j<=H2;j++) statement S

Here i,j are integer-valued loop iterators. L1,H1,L2,H2 are integer constants or integer variables. I thought that the following formula is valid then statement S will execute at least once.

$\exists(j)(j=L1\wedge i\geq L1\wedge i\leq H1\implies j\leq H2)$

but this formula is not valid for

for(i=1;i<=5;i++) for(j=2;j<=i;j++) statement S

Sometimes j can depend on i and some time it can depend on some other variable also. How does one include all these conditions?

1 Answers 1

0

OK, as I understand it, each of $L1$,$H1$,$L2$, and $H2$ is either some specific integer, or some variable (but not some function defined over them, e.g. you can't have that $H2 = 2*i$).

I'll also assume that L1 and H1 cannot be $i$ or $j$, and that $L2$ and $H2$ cannot be $j$, otherwise you get a really weird program that probably won't even compile.

Finally, while each of each of $L1$,$H1$,$L2$, and $H2$ can be some variable other than $i$ and $j$, it's easiest to treat any such case as if it were a number, since the value of such a variable is not affected by this loop.

In fact, one way to do this is to convert $L1$ to $L1'$, where $L1'=c$ if $L1$ is a specific number $c$, and where $L1' = val(v)$ if $L1$ is a variable $v$ other than $i$ and $j$, and we can convert the others likewise, and we'll say that $L2'=i$ if $L2=i$ and that $H2'=i$ if $H2=i$ .

So, let's first focus on $L1'$ and $H1'$. To get to the inside loop, we simply need $L1' \leq H1'$

OK, so let's assume that $L1' \leq H1'$, and focus on $L2'$ and $H2'$. There are 4 different cases to consider:

  1. $L2'$ and $H2'$ are both numbers. OK, so then we need $L2' \leq H2'$

  2. $L2'$ is a number and $H2'=i$. Then S will be called at least once if it will be called for the highest value of $i$, i.e. if $L2' \leq H1'$

  3. $L2'=i$ and $H2'$ is some number. Then S will be called at least once if it is called for the lowest value of $i$, i.e. we need $L1' \leq H2'$

  4. $L2'=H2'=i$ Well, now S will get called, so no further requirements.

So, to sum up, we can say that S gets called if (where ($int(X)$ returns true if $X$ is a specific integer, $var(X)$ return true if $X$ is a variable, and where $val(X)$ returns the value of $X$ if $X$ is a variable):

$((L1' = L1 \land int(L1)) \lor (L1' = val(L1) \land var(L1) \land L1 \not = i)) \land $

$((H1' = H1 \land int(H1)) \lor (H1' = val(H1) \land var(H1) \land H1 \not = i)) \land $

$((L2' = L2 \land int(L2)) \lor (L2' = val(L2) \land var(L2) \land L2 \not = i)) \land (L2' = L2 \land L2 = i)) \land$

$((H2' = H2 \land int(H2)) \lor (H2' = val(H2) \land var(H2) \land H2 \not = i)) \land (H2' = H2 \land H2 = i)) \land$

(conversion done ... now finally comes the interesting part)

$L1' \leq H1' \land $

$((L2' \not = i \land H2' \not = i \land L2' \leq H2) \lor $ (case 1)

$(L2' \not = i \land H2' = i \land L2' \leq H1') \lor $ (case 2)

$(L2' = i \land H2' \not = i \land L1' \leq H2') \lor $ (case 3)

$(L2' = i \land H2' = i))$ (case 4)

  • 0
    this will not work for `for(i=1;i<=5;i++) for(j=2;j<=i;j++) statement S` for i=2 statement S will execute.2017-01-18
  • 0
    @Ramanuj Right, you said this already in your post. Sorry, I didn't read it carefully! OK, I changed my answer.2017-01-18