Boolean function simplifier? - boolean-logic

x = (a & b & d) | ~(a | ~b | c) | (~c & ~d & a) | (c & d)
~ = not
& = and
| = or
How do I simplify a function like this, with what should I start?
I've tried some simplifying programs but I don't understand them.

You should write out a truth table for the variables involved and the eventual output.
Then, for each of the rows in the truth table that turn out to be true, you write a logic equation based upon the variables' states to reproduce that logic "one", usually an AND function of the appropriate inputs and inverse inputs.
Say only 3 of the rows have a true or logic one output.
That would mean you'd have three logic equations.
You would complete the job by connecting those three equations together with OR operators.
By looking at the truth table, you might be able to notice that the output of the logical true lines do not depend on all of the variables. This is one way of simplifying the expression.
Solving an equation similar to the one you put above
(a & b & d) | (~a | b | ~c) | (~c & ~d & a) | (c & d)
I get the following result
x = 1 except for one case, i.,e., (a b c d) = (1 0 1 0), in which case it is zero.
Thus x = ~( a & ~b & c & ~d) or x = ~a | b | ~c | d
How to do this?
To make it easier to do this, you can rewrite your equation as
x = A | B | C | D, where
A = (a & b & d)
B = (~a | b | ~c)
C = ~c & ~d & a
D = c & d
variable B = 1 for all but two sets of inputs of (abcd) namely (1010) and (1011).
variable A = 1 for only only two input sets, which B already covers.
similarly with variable C.
Variable D = 1 for one of the two sets of inputs B didn't make = 1, namely (1011).
Thus x = 0 only when the inputs are exactly a=1, b=0, c=1, d=0, but we want to write it as an equation that is True (=1) when those inputs are given, so we write
x = ~(a & ~b & c & ~d) or x = ~a | b | ~c | d
So that is one way of simplifying. I'll add a second technique in a separate answer.
sorry it took so long to spell it out, but perhaps others will find it useful.
The original equation of the OP is fairly simplified as is. The truth table has nearly equal T and F entries, and thus doesn't lend itself well to a demonstration of the technique. One could rewrite it as
x = (a & b & d) | (~a & b & ~c) | (a & ~c & ~d) | (c & d)
which is fairly compact but could be written slightly differently combining the 1st and last terms and the middle two terms:
x = ((a & b | c) & d) | ((~a & b | a & ~d) & ~c)
see 2nd proposed answer below for a further explanation

Related

What are the logical and arithmetical functions from the 74LS181

i want to use the 74ls181 in an Project of mine but i can not understand all of the functions of it mentioned in its datasheet.
Could someone please explain this boolean-mess?
EDIT:
Based on the very helpful answer from Axel Kemper i created this:
Your table was taken from the Texas Instruments 74ls181 datasheet?
Assuming from your question tags that you are asking about the logical functions
(explained from top to bottom as in the table):
F = NOT(A) set output to inverse of all A bits
F = NAND(A, B) inverse AND of inputs
F = OR(NOT(A), B)
F = 1 set all output bits to 1
F = NOR(A, B)
F = NOT(B) feed inverse B bits to output
F = NOT(EXOR(A, B))
F = OR(A, NOT(B))
F = AND(NOT(A), B)
F = EXOR(A, B) output is exclusive or of inputs
F = B feed B inputs bits to outputs
F = OR(A, B) bitwise disjunction
F = 0 set all output bits to 0
F = AND(A, NOT(B))
F = AND(A, B) bitwise conjuction
F = A
All functions are implemented 4-bit parallel.
A, B and F each have four signal lines.
A and B are the four-bit inputs. F is the four-bit output.
So, A=0 for example means A0=0, A1=0, A2=0, A3=0
There is a total of 16 different logical functions possible to implement with two inputs and one output. 74ls181 implements all of them.
A truth-table with two inputs and one output has four rows.
Each of the rows has output value 0 or 1. Therefore, a four-bit number defines the function described by the truth-table.
With four bits, 16 functions are possible.
There is a very instructive YouTube video available on the 74ls181.

Experimenting with cong in the Idris REPL

TL;DR: I'd like an example or two of using cong in the Idris REPL to help me understand it better.
The generic equality type is conceptually defined like so:
data (=) : a -> b -> Type where
Refl : x = x
When I first encountered this, I was very confused by the syntax. (I kept thinking of = as an operator rather than a type.) But playing around with it in the REPL helped me to understand. For example, we can declare types to represent false equalities:
λ> 2 + 2 = 5
4 = 5 : Type
λ> 2 = "wombat"
2 = "wombat" : Type
However, the only constructor for the = is Refl, and we can only use it when the two arguments are equal.
λΠ> the (4 = 4) Refl
Refl : 4 = 4
λΠ> the (4 = 5) Refl
... type mismatch
So now I'm trying to understand cong by experimenting with it in the REPL.
The function cong proves that if two values are equal, applying a
function to them produces an equal result. I found the definition.
cong : {f : t -> u} -> (a = b) -> f a = f b
cong Refl = Refl
So, for example, if I define...
λ> :let twoEqTwo = the (2 = 2) Refl
defined
...then I expected to be able to show that adding 1 to both sides results in another equality.
λΠ> :let ty = (S 2 = S 2)
defined
λΠ> the ty (cong twoEqTwo)
...type mismatch
Can anyone show me an example or two of using cong in the REPL?
The 2s are of the wrong type. They have defaulted to the type Integer in twoEqTwo, because they have no other constraints. See, an unconstrained 2:
idris> 2
2 : Integer
Yet, in ty, you say S 2. The S forces the whole thing to work in Nat:
idris> S 2
3 : Nat
Make twoEqTwo work in Nat, too:
idris> :let twoEqTwo = the (the Nat 2 = 2) Refl
idris> the (S 2 = S 2) twoEqTwo
Refl : 3 = 3
Note that type inference can sort this out itself if you let it see the entire expression:
idris> the (S 2 = S 2) (cong (the (2 = 2) Refl))
Refl : 3 = 3

MySql Query Table of Masks

I have a table that is filled with a variety of "masks" such at this:
Type Mask1 Mask2 Mask3
0 fff fff ff
1 aff fff ff
2 aff fff 92
3 001 fff 00
And basically I want to query the database and see if a particular query matches, say a00-111-12. Anywhere there is an f (this is all in hex) I want to say there is a match. So I take the value a00-111-12 and it should match with rows 0 and 1 but not 2 and 3 because in row 0, all f's appear and thus a value AND'd with them would result in that same value. BUT, AND-ing does not work since if testing with row 2, Mask3 column value 92 AND'd with 12 results in 12, however I don't want that row to be a match.
I find this a difficult question to ask, it may not be possible with a few MySQL Queries but I want to avoid importing the entire table into PHP and then finding the correct rows from there.
An idea of a query would be:
SELECT * FROM TABLE WHERE Mask1 = a00 AND Mask2 = 111 AND ...
However some operation would need to be done on either Mask1, 2, 3 or the value being sent to the query.
The end goal is to get the Type from the matching rows. If you need more information please ask.
Create a submasks table to make your job easier, add one row
z1 : z2 : z3
0xf : 0xf0 : 0xf00
Then use the following query
Select
t.*
from Table t
inner join submasks s
on (
((t.Mask1 & s.z1) = s.z1 || (t.Mask1 & s.z1) = (a00 & s.z1)) &&
((t.Mask1 & s.z2) = s.z2 || (t.Mask1 & s.z2) = (a00 & s.z2)) &&
((t.Mask1 & s.z2) = s.z2 || (t.Mask1 & s.z2) = (a00 & s.z2)) &&
((t.Mask2 & s.z1) = s.z1 || (t.Mask2 & s.z1) = (111 & s.z1)) &&
((t.Mask2 & s.z2) = s.z2 || (t.Mask2 & s.z2) = (111 & s.z2)) &&
((t.Mask2 & s.z2) = s.z2 || (t.Mask2 & s.z2) = (111 & s.z2)) &&
((t.Mask3 & s.z1) = s.z1 || (t.Mask3 & s.z1) = (12 & s.z1)) &&
((t.Mask3 & s.z2) = s.z2 || (t.Mask3 & s.z2) = (12 & s.z2))
)
The way this works is by comparing individual hex digits by performing a bitwise AND with z1,z2 and z2 to get each of the 3 digits respectively.
so
<any value> & z1 sets all hex digits except the last to 0, ie 0x123 becomes 0x003
<any value> & z2 sets all hex digits except the second from last to 0, ie 0x123 becomes 0x020
<any value> & z3 sets all hex digits except the third from last to 0, ie 0x123 becomes 0x100
Using this filter the test for each digit can be built as
((mask & filter) = filter) // is the digit f
|| // OR
((mask & filter) = (test & filter)) // is the digit the same.
Repeat the test for each of z1,z2 and z3 (ie 0x00f, 0x0f0, and 0xf00) combine the results with an and condition and you can check all 3 hex digits of the mask are either f or exactly the test value.
This is then repeated for Mask2 and Mask3 (but only z1,z2 as Mask3 is 2 digits).
By using inner join with the submasks table the result will only include the values from Table where the mask conditions are true.
UPDATE - you may want to perform select distinct instead of just select as if two masks match a single row in Table then 2 results will be returned.
Don't know if I explained my question well enough but I ended up coming to the conlusion that this works best:
val1 = 0xa00
val2 = 0x111
val3 = 0x12
SELECT * FROM TABLE WHERE
((Mask1 | val1)=val1 OR (Mask1 | val1)=0xfff) AND
((Mask1 | val2)=val2 OR (Mask1 | val2)=0xfff) AND
((Mask1 | val3)=val3 OR (Mask1 | val2)=0xfff);
The only problem is that val1=a00 will not match with Mask1=aff although I would like it to. Still working on it...

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.