If p → q then q → p? - boolean-logic

I'm trying to get back into boolean algebra after many years without it, I'm currently working on an exercise that asks to verify if p → q or q → p are tautologies, p and q being very long expressions hard to simplify, yet p → q is very easy to prove a tautology using a truth table while q → p takes a lot longer to verify using a truth table.
Is the statement p → q ≡ q → p correct? I can't find concise info on this proposition but building the truth table makes it seem like it is correct.
If it is I could answer that since p → q is a tautology q → p is too.

When I understand your question right, then a look at the truth-table shows the following:
a -> b = c | b -> a = c
0 -> 0 = 1 | 0 -> 0 = 1
0 -> 1 = 1 | 0 -> 1 = 0
1 -> 0 = 0 | 1 -> 0 = 1
1 -> 1 = 1 | 1 -> 1 = 1
This show that a->b is not euqal to b->a.
I hope this help a little bit.

Related

How to find maximum of function outputs with multipe inputs in one function?

I want a function maxfunct, with input f (a function) and input n (int), that computes all outputs of function f with inputs 0 to n, and checks for the max value of the output.
I am quite new to haskell, what I tried is something like that:
maxfunct f n
| n < 0 = 0
| otherwise = maximum [k | k <- [\(f, x)-> f x], x<- [0..n]]
Idea is that I store every output of f in a list, and check for the maximum in this list.
How can I achieve that?
You're close. First, let's note the type of the function we're trying to write. Starting with the type, in addition to helping you get a better feel for the function, also lets the compiler give us better error messages. It looks like you're expecting a function and an integer. The result of the function should be compatible with maximum (i.e. should satisfy Ord) and also needs to have a reasonable "zero" value (so we'll just say it needs Num, for simplicity's sake; in reality, we might consider using Bounded or Monoid or something, depending on your needs, but Num will suffice for now).
So here's what I propose as the type signature.
maxfunct :: (Num a, Ord a) => (Int -> a) -> Int -> a
Technically, we could generalize a bit more and make the Int a type argument as well (requires Num, Enum, and Ord), but that's probably overkill. Now, let's look at your implementation.
maxfunct f n
| n < 0 = 0
| otherwise = maximum [k | k <- [\(f, x)-> f x], x<- [0..n]]
Not bad. The first case is definitely good. But I think you may have gotten a bit confused in the list comprehension syntax. What we want to say is: take every value from 0 to n, apply f to it, and then maximize.
maxfunct :: (Num a, Ord a) => (Int -> a) -> Int -> a
maxfunct f n
| n < 0 = 0
| otherwise = maximum [f x | x <- [0..n]]
and there you have it. For what it's worth, you can also do this with map pretty easily.
maxfunct :: (Num a, Ord a) => (Int -> a) -> Int -> a
maxfunct f n
| n < 0 = 0
| otherwise = maximum $ map f [0..n]
It's just a matter of which you find more easily readable. I'm a map / filter guy myself, but lots of folks prefer list comprehensions, so to each his own.

Using nested functions to find product of numbers

I need to make a function that given natural number n, calculates the product
of the numbers below n that are not divisible by
2 or by 3 im confused on how to use nested functions in order to solve this problem (also new to sml ) here is my code so far
fun countdown(x : int) =
if x=0
then []
else x :: countdown(x-1)
fun check(countdown : int list) =
if null countdown
then 0
else
It is not clear from the question itself (part of an exercise in some class?) how we are supposed to use nested functions since there are ways to write the function without nesting, for example like
fun p1 n =
if n = 1 then 1 else
let val m = n - 1
in (if m mod 2 = 0 orelse m mod 3 = 0 then 1 else m) * p1 m
end
and there are also many ways to write it with nested functions, like
fun p2 n =
if n = 1 then 1 else
let val m = n - 1
fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
in (if check m then 1 else m) * p2 m
end
or
fun p3 n =
let fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
fun loop m =
if m = n then 1 else
(if check m then 1 else m) * loop (m + 1)
in loop 1
end
or like the previous answer by #coder, just to give a few examples. Of these, p3 is somewhat special in that the inner function loop has a "free variable" n, which refers to a parameter of the outer p3.
Using the standard library, a function that produces the numbers [1; n-1],
fun below n = List.tabulate (n-1, fn i => i+1);
a function that removes numbers divisible by 2 or 3,
val filter23 = List.filter (fn i => i mod 2 <> 0 andalso i mod 3 <> 0)
a function that calculates the product of its input,
val product = List.foldl op* 1
and sticking them all together,
val f = product o filter23 o below
This generates a list, filters it and collapses it. This wastes more memory than necessary. It would be more efficient to do what #FPstudent and #coder do and generate the numbers and immediately either make them a part of the end product, or throw them away if they're divisible by 2 or 3. Two things you could do in addition to this is,
Make the function tail-recursive, so it uses less stack space.
Generalise the iteration / folding into a common pattern.
For example,
fun folditer f e i j =
if i < j
then folditer f (f (i, e)) (i+1) j
else e
fun accept i = i mod 2 <> 0 andalso i mod 3 <> 0
val f = folditer (fn (i, acc) => if accept i then i*acc else acc) 1 1
This is similar to Python's xrange.

Consider the 4-to-1 multiplexer shown below. What values must inputs A, B, C, D be so that the multiplexer implements the function

I am completing some practice quizzes online in preperation for a uni exam next week, and I am a bit stumped as to how to solve this problem:
Consider the 4-to-1 multiplexer shown below. What values must inputs
A, B, C, D be so that the multiplexer implements the function
If anyone would'nt mind giving me a few tips as to how to solve this, I would much appreciate it.
Thanks
Corey
I'll assume: X = S1 * ~S0 + S0 * ~S1 as your posted picture is unreadable.
First you need to know the multiplexor transfer:
S1 S0 X
0 0 A
0 1 B
1 0 D
1 1 C
Then, apply the function to all your possible input combinations.
f(s1,s0)=S1*~S0+~S1*S0
f(0,0) = 0
f(0,1) = 1
f(1,0) = 1
f(1,1) = 0
And finally fill the table with X=f(s1,s0)
S1 S0 X f(s1,s0)
0 0 A 0
0 1 B 1
1 0 D 1
1 1 C 0

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.

Mysql Precedence Logic

Any explanation to the following queries :
Select x FROM y WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
why it doesn't return the correct info while this return the correct info :
Select x FROM y WHERE (a = 1 OR a = 2) AND (b = 1 OR b = 2)
Am i missing something here ?
X Y (X OR Y) X OR Y
1 0 1 1
0 1 1 1
1 1 1 1
0 0 0 0
I know in term of precedence the () have priority , but why should i add them the the first part of the query ?
Correct me if I'm wrong
Thank you
AND has a higher precedence than OR so your first query is equivalent to this:
Select x FROM y WHERE a = 1 OR a = 3 OR (a = 2 AND (b = 1 OR b = 2))
Which is not equivalent to
Select x FROM y WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
I guess you forgot the a = 3 part in your first query.
Operator precedence in MySQL
Because ambiguity is an undesirable trait?
Also, the optimizer will re-order your WHERE Conditions if it thinks it will perform better. Your ambiguity will, therefore, cause different results depending on how/what it evaluates first.
Always be explicit with your intentions.
Using parentheses in your WHERE clause does not just affect precedence but also groups predicates together. In your example the difference in results is more a matter of grouping rather than precedence.
You could think of this: (pN = predicate expression)
WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
as:
WHERE p1 OR p2 AND p3
And this:
WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
as:
WHERE p1 AND p2
and so it becomes clear that the results could be quite different.