Converting phrases to First Order Logic

109 Views Asked by At

Assume $a$ is an array of integers of length $100$.

Example: All elements of a are 0.

Solution: ∀i.((0 ≤ i < 100) → a[i] = 0).

(1) For all i in 0..99, the ith element of a is the square of i.
(2) All the elements of a with index a multiple of 3 are 0.
(3) At least one element of a is positive.
(4) At least two elements of a are positive.
(5) Either all elements of a are positive or all elements of a are negative.
(6) The elements of a are non-decreasing. (As an example, the sequence 1,2,2,3,3,4 is non-decreasing
(7) Each element of a (except for the first two elements) is the sum of the previous two elements.

The answers below are my own attempts. I'm not sure that they are correct and I wasn't able to work out anything for problems $2$ and $4$. I'm still learning FOL and I'm having trouble doing the translations.

1.
∀((0 ≤ i < 100 → a[i] = i^2))
2
))
3.
∃i((0 ≤ i < 100) ∧ (a[i] > 0))
5.
∀i((0 ≤ i < 100) ∨ (0 > i > −100) → (a[i] > 0) ∨ (a[i] < 0)
6.
∀i((0 ≤ i < 99) → (a[i + 1]) > a[i]))
1
7.
∀i((2 ≤ i < 100) → a[i] = (a[i − 1] + a[i − 2]))
1

There are 1 best solutions below

2
On BEST ANSWER

For 1) you need $i^2$. Also, don't forget the $i$ after the universal quantifier. And add a closing parenthesis.

For 3) why not use $a[i]>0$? And you need a $\land$ rather than a $\rightarrow$

For 5) Use a disjunction of two universal statements.

For 5) through 7): using $0 \le i < i <100$ makes no sense, since you have $i<i$ as part of that. Just use $0 \le i <100$ ... except for 6), where you should use $0 \le i <99$ since you make reference to $i+1$

For 6) it should be $a[i+1]>a[i]$

7) since you use $i-2$, you should start $i$ with $2$, i.e. Use $2 \le i <100$