I'm confused how expression is different PROJECT A(r − s) and PROJECT A(r) − PROJECT A(s) is in RDBMS. Can anyone show an example to prove above?
s = [x y]
0 0
1 1
2 2
r = [x y]
0 0
0 1
0 2
1 1
1 2
2 2
A = {y}
PROJECT A(r − s)=[y]
1
2
PROJECT A(r) − PROJECT A(s)=[y]
Here is how to generate (counter)examples like this. The OP problem translates into QBQL assertion:
a <AND> TABLE_DUM = a & r <AND> TABLE_DUM =s <AND> TABLE_DUM
->
a v ( r <AND> <NOT> s ) = (a v r) <AND> <NOT> (a v s).
Where all operations, except "v" (generalized projection) are from D&D Algebra A. The first two implication conditions require relation a to be empty, and relations r and s to have the same header. It outputs:
s = [p]
0
;
r = [p]
1
;
a = [r]
;
*** False Assertion ***
Although this is legitimate answer, some may feel uncomfortable with an idea of projecting relation onto an attribute which doesn't belong to the header. One needs to add one more condition for a and r headers to overlap: (a v s) <AND> TABLE_DUM != TABLE_DUM. Then, the attributes {p,r} should be manually translated into OP's {x,y}. (It is where the bug noticed by Erwin has been introduced).
r = { TUPLE {X 1 , Y 1} }
s = { TUPLE {X 1 , Y 2} }
r MINUS s = r = { TUPLE {X 1 , Y 1} }
Take projections over X (A = {X} ???)
r PROJECT {X} === { TUPLE {X 1} }
s PROJECT {X} === { TUPLE {X 1} }
(r PROJECT {X}) MINUS (s PROJECT {X}) === { }
(r MINUS s) PROJECT {X} === { TUPLE {X 1} }
It's the Y 2 part in s. That's the difference between the tuples in r and s. If you "project away that difference" first, then whatever remains after that is equal, and computing the difference between things that are equal is of course nothing at all.
But if you first compute the difference, then the Y 2 part in the tuple in s causes the tuple in r (sloppily speaking) to not be discarded from the result, and then taking a projection on that result of course produces itself something nonempty.
Related
I would like to use Scala's property-based testing tool ScalaCheck to express a property
if an integer n * n = 0 then n = 0
How can I write this property in ScalaCheck? I know for example
val myprop = forAll {(n: Int) => n + 1 - 1 = n}
But I do not know how to express "A implies B" in ScalaCheck (without reducing it to Not-A or B, which can look clumsy).
Use ==> (implication operator)
val prop = forAll { n: Int =>
(n * n == 0) ==> n == 0
}
(see their User Guide )
the catch is: in this particular example the condition is very hard to satisfy so ScalaCheck will give up after several tries (but at least it does tell you so, otherwise you get a false positive because your necessary condition was never checked). In that case you can provide a custom generator so that it will generate values that satisfy your condition.
For a project I'm coding group theory through Coq, obviously associatvity of 3 elements is a given, however I'm struggling to prove it holds for a string of length n. That is, (x1 * ... * xn) is always the same regardless of how many brackets are in there, or there placement.
The relevant group code is
Structure group :=
{
e : G;
Op : G -> G -> G;
Inv : G -> G;
Associativity : forall (x y z : G), Op x (Op y z) = Op (Op x y) z;
LeftInverse : forall (x : G), Op (Inv x) x = e;
LeftIdentity : forall (x : G), Op e x = x;
}.
It's not the proof itself I have the issue with but how to code it. I can see at the very least I'll need a further function that allows me to operate on strings rather than just elements, but i've no idea how to get started. Any pointers?
Operating on strings directly is certainly possible, but cumbersome. When reasoning about languages, it is much more convenient to use abstract syntax trees instead. For your statement, we only want to consider combinations of elements with some binary operation, so a binary tree suffices:
Inductive tree T :=
| Leaf : T -> tree T
| Node : tree T -> tree T -> tree T.
For concreteness, I'll only consider the natural numbers under addition, but this generalizes to any other monoid (and thus any other group). We can write a function that sums all the elements of a tree:
Fixpoint sum_tree t : nat :=
match t with
| Leaf n => n
| Node t1 t2 => sum_tree t1 + sum_tree t2
end.
We can also write a function that flattens the tree, collecting all of its elements in a list
Fixpoint elements {T} (t : tree T) : list T :=
match t with
| Leaf x => [x]
| Node t1 t2 => elements t1 ++ elements t2
end.
With these ingredients, we can formulate the statement that you were looking for: if two trees (that is, two ways of putting parenthesis in an expression) have the same sequences of elements, then they must add up to the same number.
Lemma eq_sum_tree t1 t2 :
elements t1 = elements t2 -> sum_tree t1 = sum_tree t2.
I'll leave the proof of this statement to you. ;)
Inductive ty: Set :=
| I
| O.
Definition f (x: ty) (y: ty): nat :=
if x = y then 0 else 1.
I want the function f to compare two terms of type ty but it does not compile and I see this error:
The term x = y has type Prop which is not a (co-)inductive type.
You need to prove that equality is decidable for ty (which can be done automatically using decide equality) and then use that definition in the if ... then ... else ... statement. Concretely:
Inductive ty: Set :=
| I
| O.
Definition ty_eq_dec : forall (x y : ty), { x = y } + { x <> y }.
Proof.
decide equality.
Defined.
Definition f (x: ty) (y: ty): nat :=
if ty_eq_dec x y then 0 else 1.
You can use match to compare the elements of inductive data types.
Definition f x y := match x,y with I, I | O, O => 0 | _,_ => 1 end.
decide equality is a more general tactic and works for infinite sets, but it is good to know that it is match that is doing the real work.
I was trying to decipher what this actually means
check { (n: Int) =>
n > 1 ==> n / 2 > 0
}
from http://www.scalatest.org/user_guide/writing_scalacheck_style_properties
I am first trying to decipher whether that body is
((n: Int) => n > 1) ==> n/2 > 0
or
(n: Int) => (n > 1 ==> n/2 > 0)
Which one would it be? I am guessing the latter since check method takes a function though the method '==>' could be returning a function as well?
Next, I look at the check method signature at https://searchcode.com/codesearch/view/12336175/
def check[A1,P](f: A1 => P)
(implicit
p: P => Prop,
a1: Arbitrary[A1], s1: Shrink[A1]
) {
check(Prop.property(f)(p, a1, s1))
}
I believe A1 would be the Int unless ==> returns a function and changes the return type(doubtful though I think). I am not sure how to find the implicit function P => Prop in the scalacheck library.
I do notice there is an ExtendedBoolean that has a function ==> https://github.com/rickynils/scalacheck/blob/master/src/main/scala/org/scalacheck/Prop.scala
Perhaps (n > 1) was converted to an ExtendedBoolean assuming the function was (n: Int) => (n > 1 ==> n/2 > 0) and then we get
ExtendedBoolean( n > 1 ).==>( n / 2 > 0 ) is called then.
Since the implementation of ==> for ExtendedBoolean is Prop(b) ==> p, I end up with
Prop( n > 1 ) ==> (n / 2 > 0)
I get really confused here as in call by name, the values are usually captured except there is no value for n at this point since it was originally a function. ignoring my confusion for a second, the implementation of ==> for Prop is thus
def ==>(p: => Prop): Prop = flatMap { r1 =>
if(r1.proved) p map { r2 => mergeRes(r1,r2,r2.status) }
else if(!r1.success) Prop(r1.copy(status = Undecided))
else p map { r2 => provedToTrue(mergeRes(r1,r2,r2.status)) }
}
so, we are calling this like so I think with another implicit version to Prop here
Prop( n > 1 ) ==> Prop(n / 2 > 0)
ok, and flatmap is
Prop(prms => f(this(prms))(prms))
hmmm, there must have been another conversion to Prop . I start to trace prms and then look at the object Prop apply method for Booleans which is
def apply(b: Boolean): Prop = if(b) proved else falsified
but I can't resolve b to true or false since those functions have not been evaluated yet. How is all this working together? I think I am missing a few concepts here just barely. Can someone explain this a bit better?
Yours is really a ScalaCheck question, but one way to avoid the whole question is to use ScalaTest's PropertyChecks syntax instead of ScalaCheck's (which is supported in ScalaTest by Checkers):
import org.scalatest.prop.Checkers._
check { (n: Int) =>
n > 1 ==> n / 2 > 0
}
becomes:
import org.scalatest.MustMatchers._
import org.scalatest.prop.PropertyChecks._
forAll { (n: Int) =>
whenever (n > 1) {
n / 2 must be > 0
}
}
How can I use a comparison of to rational numbers in an if-statement?
if 1 = 2 then 1 else 2
1 = 2 is of course Prop and not bool.
I don't understand how dfan's answer is related to the question...
Of course, 1 = 2 is a Prop, it is the statement that 1 is equal to 2. Hopefully you don't have a proof of this statement...
What you want is a function that, given two natural numbers, 1 and 2, returns true if they are equal, and false if they aren't.
The library Coq.Arith.EqNat gives you such a function, named beq_nat.
In fact, you might want something even better, a function that returns a proof of equality or a proof of difference:
(* In Coq.Arith.Peano_dec *)
Theorem eq_nat_dec : forall n m, {n = m} + {n <> m}.
(* ^ a proof that n = m
^ or a proof that n <> m *)
if is overloaded to work with such things, so you can even write:
if eq_nat_dec 2 3 then ... else ...
Qeq_bool indeed takes two rationals and produces a bool.
Require Export QArith_base.
Eval compute in Qeq_bool (3#2) (3#2). = true: bool
Eval compute in Qeq_bool (3#2) (5#2). = false: bool
Eval compute in (if Qeq_bool (3#2) (5#2) then 7 else 9). = 9: nat