How to determine keys from functional dependencies - relational-database

I'm trying to find the keys for the relation
R = ABCDE, with functional dependencies
AB->C, C->D, C->E, D->A, E->B
I know how to find the keys when on the right side of the dependencies there are some attributes missing.
But here all attributes appear on the right side.
I'm not sure how to find the keys and I couldn't find an explanation for this specific example.

My answer is based on https://stackoverflow.com/a/43467969/206413 and https://djitz.com/neu-mscs/answers-to-candidate-key-hard-questions/.
(1) AB -> C
(2) C -> D
(3) C -> E
(4) D -> A
(5) E -> B
AB+ = (1) ABC = (2, 3) ABCDE
C+ = (2, 3) CDE = (4) ACDE = (5) ABCDE
D+ = = (4) AD
E+ = = (5) BE
So far we have as candidates (AB, C).
Exploring further:
D+ = AD => DAB+ = ABCDE
E+ = BE => EAB+ = ABCDE
Hence our candidates are (AB, C, DAB, EAB)

Related

Anova reject null hypothesis but T-test accept null hypothesis

I tested my data using anova and the result is reject the null hypothesis, meaning that there are significance in my data. So, I tested the same data again using T-test but it then the result said that the null hypothesis is accepted. My data has 3 column (a, b and c). Is it logic for the result to show like that? anova rejected null hypothesis and t test accepted the null hypothesis? If it is logic, how should i conclude the result? I'm using 90% confidence interval.
For anova test :
F stats = 28126.8
P-value = 0
F crit = 1.42693
For T-test :
a vs b
t stat = 0.44965721
P two tail = 0.964191412
t crit two tail = 1.654554875
a vs c
t stat = -0.508842709
P two tail = 0.611586028
t crit two tail = 1.654743774
b vs c
t stat = -0.552393274
P two tail = 0.581474613
t crit two tail = 1.654743774

Dont understand whats on in this haskell code

I have some haskell code Im trying to work my way thourgh but I dont have understand what is going in it.
type Bag a = a -> Int
emptyB :: Bag a
emptyB = \e -> 0
countB :: Eq a => Bag a -> a -> Int
countB b e = b e
I understand that the Bag type is a function that takes in a generic object and returns a Int and countB is basically a wrapper for Bag that gets the number of generic objects in that Bag. But I dont really understand anything past that. How do I modify whats in the bag? Or the bag itself? From what I figure adding to the bag would be something like
addB :: Eq a => Bag a -> a -> Bag a
addB bag num = bag (num+bag)
But this returns a int, when the add function requires a bag be returned. Can anyone explain to me how this works?
Terms and Discussion
type Bag a = a -> Int
Here Bag is not an object. It is just a type - an alias for a -> Int. If you have a value of type a it will compute and return a value of type Int. That's it. There is no Bag, no structure to which you can add things. It would be better to not even call this a Bag.
emptyB :: Bag a
emptyB = \e -> 0
A function from any type to the constant number zero.
countB :: Eq a => Bag a -> a -> Int
countB b e = b e
In short, this is just function application. Apply the function named b to the input e.
Rewriting for fun and learning
I appreciate that you can use functions to imitate structures - it's a common programming language class assignment. You can take a Bag a and another Bag a then union them, such as returning a new countB by adding the counts of the two individual bags - cool.
... but this seems too much. Before moving on with your assignment (did I guess that right?) you should probably become slightly more comfortable with the basics.
It might be easier if you rewrite the functions without the type alias:
emptyB :: a -> Int
emptyB = \e -> 0
-- or: emptyB e = 0
-- or: emptyB _ = 0
-- or: emptyB = const 0
Bag or no bag, it's just a function.
countB :: Eq a => (a -> Int) -> a -> Int
countB b e = b e
A function that takes an a and produces an Int can... be given a value (the variable e is of type a) and produce an Int.

Call comparison operators in Haskell

i need to find out if the difference from differenceAB is the smallest:
smallestDifference3 :: Int -> Int -> Int -> Int
smallestDifference a b c
| differenceAB < differenceBC < differenceAC = differenceAB
| otherwise = differenceAB
where differenceAB
| a < b = -(a - b)
| otherwise = a - b
differenceBC
| b < c = -(b - c)
| otherwise = b - c
differenceAC
| a < c = -(a - c)
| otherwise = a - c
but i get this error:
cannot mix `<' [infix 4] and `<' [infix 4] in the same infix expression
how to solve my problem? anybody know´s? greetingS!
There are a couple of problems here.
You have 3 exactly identical functions. Probably you want values:
smallestDifference a b c = ....
where
diffAC = abs(a-c)
diffAB = abs(a-b)
diffBC = abs(b-c)
Now for the expression, you can't write
diffAC < diffAB < diffBC
since (<) is a non-associative operator. Which means you must write explicit parentheses:
(diffAC < diffAB) < diffBC
But this doesnt type check, because for the second (<) the left hand side is Bool, but the right hand side is Int. What you want is
(diffAC < diffAB) && (diffAB < diffBC)
i.e. if ac is lower then ab and ab is lower than bc
If you just want to find the smallest difference, following will work.
smallestdiff a b c = minimum [abs $ a-b, abs $ b-c, abs $a-c]
There is no such thing as chaining operators in Haskell. You should use explicit logical operations:
smallestDifference3 :: Int -> Int -> Int -> Int
smallestDifference a b c
| (differenceAB < differenceBC) && (differenceBC < differenceAC) = differenceAB
| otherwise = differenceAB
BTW, your code is weird, you return differenceAB from both guard clauses. It is not clear what you want to achieve, so I cannot help further.

Order-dependant Bit Fields

How would one go about storing positional information in bit fields (the order in which the fields are OR'd or otherwise)?
Background: It popped into my head last night while writing a part of my game engine. Let's say that we are trying to describe a colour, and as part of that we have the colours that are present in the descriptor (and their order). For example we have the following colour orders on most graphics cards today:
RGBA
BGRA
The following flags can be used to describe colours that are supported:
None = 0x0
A = 0x1
R = 0x2
G = 0x4
B = 0x8
However, by using those fields A | R | G | B is the same thing as B | G | R | A. How would you design the flags and/or operations that can be used to add positional dependence? Bonus marks for adding exclusivity (you can't have R and G in position 1, for example) and for utility (some clever way that it could be used, possibly in this case scenario).
You can shift the bit field before adding each flag, by the number of bits required for each unique flag. The following flags would be used:
None = 0x0
A = 0x1
R = 0x2
G = 0x4
B = 0x8
Shift = 0x4
Mask = 0xF (A | R | G | B)
On a little-endian system you would shift it left by Shift (<<) before each OR. The shift left on None can be eliminated because 0 << x = 0. Given the original example:
A1 = A
A1R2 = (A1 << Shift) | R
A1R2G3 = (A1R1 << Shift) | G
A1R2G3B4 = (A1R1G3 << Shift) | B
B1 = B
B1G2 = (B1 << Shift) | G
B1G2R3 = (B1G2 << Shift) | R
B1G2R3A4 = (B1G2R3 << Shift) | A
To extract the position of each you would repeatedly shift it right (little-endian) and AND it with Mask. Repeating this until the current value reaches None would give you the reverse order.
let cur = the bit field we want to check
loop until cur = None:
let val = cur AND Mask
emit the name of val
let cur = cur >> Shift
This does not offer exclusivity (you can easily do a AAGB) and it doesn't look like it has any utility.

How do I create Haskell functions that return functions?

I would like to create three Haskell functions: a, b, and c.
Each function is to have one argument. The argument is one of the three functions.
I would like function a to have this behavior:
if the argument is function a then return function a.
if the argument is function b then return function b.
if the argument is function c then return function a.
Here's a recap of the behavior I desire for function a:
a a = a
a b = c
a c = a
And here's the behavior I desire for the other two functions:
b a = a
b b = a
b c = c
c a = c
c b = b
c c = c
Once created, I would like to be able to compose the functions in various ways, for example:
a (c b)
= a (b)
= c
How do I create these functions?
Since you have given no criteria for how you are going to observe the results, then a = b = c = id satisfies your criteria. But of course that is not what you want. But the idea is important: it doesn't just matter what behavior you want your functions to have, but how you are going to observe that behavior.
There is a most general model if you allow some freedom in the notation, and you get this by using an algebraic data type:
data F = A | B | C
deriving (Eq, Show) -- ability to compare for equality and print
infixl 1 %
(%) :: F -> F -> F
A % A = A
A % B = C
A % C = A
B % A = A
...
and so on. Instead of saying a b, you have to say A % B, but that is the only difference. You can compose them:
A % (C % B)
= A % B
= B
and you can turn them into functions by partially applying (%):
a :: F -> F
a = (A %)
But you cannot compare this a, as ehird says. This model is equivalent to the one you specified, it just looks a little different.
This is impossible; you can't compare functions to each other, so there's no way to check if your argument is a, b, c or something else.
Indeed, it would be impossible for Haskell to let you check whether two functions are the same: since Haskell is referentially transparent, substituting two different implementations of the same function should have no effect. That is, as long as you give the same input for every output, the exact implementation of a function shouldn't matter, and although proving that \x -> x+x and \x -> x*2 are the same function is easy, it's undecidable in general.
Additionally, there's no possible type that a could have if it's to take itself as an argument (sure, id id types, but id can take anything as its first argument — which means it can't examine it in the way you want to).
If you're trying to achieve something with this (rather than just playing with it out of curiosity — which is fine, of course), then you'll have to do it some other way. It's difficult to say exactly what way that would be without concrete details.
Well, you can do it like this:
{-# LANGUAGE MagicHash #-}
import GHC.Prim
import Unsafe.Coerce
This function is from ehird's answer here:
equal :: a -> a -> Bool
equal x y = x `seq` y `seq`
case reallyUnsafePtrEquality# x y of
1# -> True
_ -> False
Now, let's get to business. Notice that you need to coerce the arguments and the return values as there is no possible type these functions can really have, as ehird pointed out.
a,b,c :: x -> y
a x | unsafeCoerce x `equal` a = unsafeCoerce a
| unsafeCoerce x `equal` b = unsafeCoerce c
| unsafeCoerce x `equal` c = unsafeCoerce a
b x | unsafeCoerce x `equal` a = unsafeCoerce a
| unsafeCoerce x `equal` b = unsafeCoerce a
| unsafeCoerce x `equal` c = unsafeCoerce c
c x | unsafeCoerce x `equal` a = unsafeCoerce c
| unsafeCoerce x `equal` b = unsafeCoerce b
| unsafeCoerce x `equal` c = unsafeCoerce c
Finally, some tests:
test = a (c b) `equal` c -- Evaluates to True
test' = a (c b) `equal` a -- Evaluates to False
Ehh...
As noted, functions can't be compared for equality. If you simply want functions that satisfy the algebraic laws in your specificiation, making them all equal to the identity function will do nicely.
I hope you are aware that if you post a homework-related question to Stack Overflow, the community expects you to identify it as such.