Composed relation of 2 relations on the set A - set

I'm struck with this problem, and i'm wondering if I'm wrong or the answer sheet I have is wrong. I've included the problem description and answer below:
The problem is with 2 and 3. First I construct the matrix for R and S:
Then I can compute the 5th row of the R ◦ S using this equation:
When i try with (5, 4) as shown in the solution i get 0, how ever if i try with 3 i get 1.
Same happens in the 3rd problem where I get the result 4, but I should get 3. Have I swapped something along the way since my to answers are just the opposite answer?

You should read the text of the formula again:
We get the matrix for S ○ R by taking the matrix product [R] * [S] ...
So if you want to get R ○ S you have to calculate [S] * [R] (note: the position is swapped).
R ○ S ⇒ [S] * [R] (5,4) = (0 ∧ 0) ∨ (0 ∧ 0) ∨ (1 ∧ 1) ∨ (0 ∧ 0) ∨ (0 ∧ 0) = 1

Related

How do you use the Ring solver in Cubical Agda?

I have started playing around with Cubical Agda. Last thing I tried doing was building the type of integers (assuming the type of naturals is already defined) in a way similar to how it is done in classical mathematics (see the construction of integers on wikipedia). This is
data dInt : Set where
_⊝_ : ℕ → ℕ → dInt
canc : ∀ a b c d → a + d ≡ b + c → a ⊝ b ≡ c ⊝ d
trunc : isSet (dInt)
After doing that, I wanted to define addition
_++_ : dInt → dInt → dInt
(x ⊝ z) ++ (u ⊝ v) = (x + u) ⊝ (z + v)
(x ⊝ z) ++ canc a b c d u i = canc (x + a) (z + b) (x + c) (z + d) {! !} i
...
I am now stuck on the part between the two braces. A term of type x + a + (z + d) ≡ z + b + (x + c) is asked. Not wanting to prove this by hand, I wanted to use the ring solver made in Cubical Agda. But I could never manage to make it work, even trying to set it up for simple ring equalities like x + x + x ≡ 3 * x.
How can I make it work ? Is there a minimal example to make it work for naturals ? There is a file NatExamples.agda in the library, but it makes you have to rewrite your equalities in a convoluted way.
You can see how the solver for natural numbers is supposed to be used in this file in the cubical library:
Cubical/Tactics/NatSolver/Examples.agda
Note that this solver is different from the solver for commutative rings, which is designed for proving equations in abstract rings and is explained here:
Cubical/Tactics/CommRingSolver/Examples.agda
However, if I read your problem correctly, the equality you want to prove requires the use of other propositional equalities in Nat. This is not supported by any solver in the cubical library (as far as I know, also the standard library doesn't support it). But of course, you can use the solver for all the steps that don't use other equalities.
Just in case you didn't spot this: here is a definition of the integers in math-style using the SetQuotients of the cubical library. SetQuotients help you to avoid the work related to your third constructor trunc. This means you basically just need to show some constructions are well defined as you would in 'normal' math.
I've successfully used the ring solver for exactly the same problem: defining Int as a quotient of ℕ ⨯ ℕ. You can find the complete file here, the relevant parts are the following:
Non-cubical propositional equality to path equality:
open import Cubical.Core.Prelude renaming (_+_ to _+̂_)
open import Relation.Binary.PropositionalEquality renaming (refl to prefl; _≡_ to _=̂_) using ()
fromPropEq : ∀ {ℓ A} {x y : A} → _=̂_ {ℓ} {A} x y → x ≡ y
fromPropEq prefl = refl
An example of using the ring solver:
open import Function using (_$_)
import Data.Nat.Solver
open Data.Nat.Solver.+-*-Solver
using (prove; solve; _:=_; con; var; _:+_; _:*_; :-_; _:-_)
reorder : ∀ x y a b → (x +̂ a) +̂ (y +̂ b) ≡ (x +̂ y) +̂ (a +̂ b)
reorder x y a b = fromPropEq $ solve 4 (λ x y a b → (x :+ a) :+ (y :+ b) := (x :+ y) :+ (a :+ b)) prefl x y a b
So here, even though the ring solver gives us a proof of _=̂_, we can use _=̂_'s K and _≡_'s reflexivity to turn that into a path equality which can be used further downstream to e.g. prove that Int addition is representative-invariant.

n-Queens Problem: why the expression written next can assert there is at most one queen in each row if the expression is True?

I can't understand the expression although I have tried to understand it by making model.
Thanks in advance!
The statement is wrong. Consider the 2x2 case where the top row contains two queens and the bottom row is empty.
Then Q2 (which is supposed to assert there is at most 1 queen in each row) expands to:
(¬p(1, 1) ∨ ¬p(2, 1)) ∧
(¬p(2, 1) ∨ ¬p(2, 1))
Now the bottom row is empty so ¬p(2, 1) is true, satisfying both conditions. This means that Q2 is satisfied when it shouldn't be.
If you're not convinced by the above expansion, run the following Python code to see for yourself:
n = 2
for i in range(1, 1+n):
for j in range(1, 1+(n-1)):
for k in range(j+1, 1+n):
print(f"!p({i}, {j}) v !p({k}, {j})")

can you help me to transform forall FO logical formula to it equivalent not exist formula?

i have this formula
∀x ∀y (A(x,y) V A(y,x) → B(y,c1) ∧ B(x,c2) ∧ c1≠c2)
to the equivalent formula that by using existential quantifier
?
∀x ∀y X is the same as ¬∃(x, y) ¬X
'X → Y' is the same as 'There is no counterexample when X but not Y'
¬(A(x,y) V A(y,x) → B(x,c1) ∧ B(x,c2) ∧ c1≠c2) = (A(x,y) V A(y,x)) ∧ ¬(B(x,c1) ∧ B(x,c2) ∧ c1≠c2)) - our counterexample. If we put negation of the second part in it and collect everything together, we get:
¬∃(x, y) (A(x,y) V A(y,x)) ∧ (¬B(x,c1) v ¬B(x,c2) v c1 = c2)
Update: replaced ¬∃x ¬∃ y with ¬∃(x, y). I suppose that's what you originally meant, right?
When you want to make that change, you basically want to find the opposite of what the inner statement says , because if a statement is true **for every ** x, then it means the opposite of it does not happen, ever; not exist means just that, there is no x such that makes the statement true.

Euclid's algorithm using until

I'm a beginner in Haskell, just started now learning about folds and what not, in college, first year.
One of the problems I'm facing now is to define Euclid's algorithm using the until function.
Here's the Euclid's recursive definition (EDIT: just to show how euclid works, I'm trying to define euclid's without the recursive. Just using until):
gcd a b = if b == 0 then a else gcd b (a `mod` b)
Here's what i have using until:
gcd a b = until (==0) (mod a ) b
Obviously this doesn't make any sense since it's always going to return 0, as that is my stopping point instead of printing the value of a when b == 0. I can't for the life of me though figure out how to get the value of a.
Any help is appreciated.
Thank you in advance guys.
Hints:
Now
until :: (a -> Bool) -> (a -> a) -> a -> a
so we need a function that we can apply repeatedly until a condition holds, but we have two numbers a and b, so how can we do that?
The solution is to make the two numbers into one value, (a,b), so think of gcd this way:
uncurriedGCD (a,b) = if b == 0 then (a,a) else uncurriedGCD (b,a `mod` b)
Now you can make two functions, next & check and use them with until.
Helpers for until:
next (a,b) = (b,a `mod` b)
check (a,b) = b == 0
This means that we now could have written uncurriedGCD using until.
Answer:
For example:
ghci> until check next (6,4)
(2,0)
ghci> until check next (12,18)
(6,0)
So we can define:
gcd a b = c where (c,_) = until check next (a,b)
giving:
ghci> gcd 20 44
4
ghci> gcd 60 108
12
What the Euclid's algorithm says is this: for (a, b), computing (b, mod a b) until (the new) b equals zero. This can be translated directly to an implementation using until like this:
myGcd a b = until (\(x, y) -> y == 0) (\(x, y) -> (y, x `mod` y)) (a, b)

Find unique tuples in a relation represented by a BDD

Let's suppose I use a BDD to represent the set of tuples in a relation. For simplicity consider tuples with 0 or 1 values. For instance:
R = {<0,0,1>, <1,0,1>, <0,0,0>} represent a ternary relation in a BDD with three variables, say x, y and z (one for each tuple's element). What I want is to implement an operation that given a bdd like for R and a cube C returns the subset of R that contains unique tuples when the variables in C are abstracted.
For instance, if C contains the variable x (which represents the first element in each tuple) the result of the function must be {<0,0,0>}. Notice that when x is abstracted away tuples <0,0,1> and <1,0,1> become "the same".
Now suppose R = {<0,0,1>, <1,0,1>, <0,0,0>, <1,0,0>} and we want to abstract x again. In this case I would expect the constant false as result because there is no unique tuple in R after abstracting x.
Any help is highly appreciated.
This could be done in three simple steps:
Make two BDDs with restricted value of the variable you want to abstract:
R[x=0] = restrict R with x = 0
R[x=1] = restrict R with x = 1
Apply XOR operation to this new BDDs
Q = R[x=0] xor R[x=1]
Enumerate all models of Q
Applying this to your examples:
R = {<0,0,1>, <1,0,1>, <0,0,0>} = (¬x ∧ ¬y ∧ z) ∨ (x ∧ ¬y ∧ z) ∨ (¬x ∧ ¬y ∧ ¬z)
R[x=1] = {<0,1>} = (¬y ∧ z)
R[x=0] = {<0,1>,<0,0>} = (¬y ∧ z) ∨ (¬y ∧ ¬z)
Q = R[x=1] xor R[x=0] = (¬y ∧ ¬z)
Intuition here is that XOR will cancel entries that occur in both BDDs.
This is easily (but with exponential complexity) generalized to the case with several abstracted variables.

Resources