So I have this boolean logic case.
three variables x, y, and z. The (ternary) parity function p(x, y, z) is a boolean function with value
• F, if an even number of the inputs x, y, and z have truth value T
• T, if an odd number of inputs have truth value T
this is my truth table (ill put just a few case) below
x y z - p(x, y, z)
F F F (?)
F F T evaluates to T because there is one T which is odd
F T T evaluates to F because there is two T which is even
my question is that, what if all of the three input evaluates to F. It is neither T odd or T false. So what would it evaluates to?
The definition of an even number x is that it is divisible by 2 with no remainder. 0/2 = 0, so if there are zero T, it will evaluate to F
Related
So I drew a simple function in prolog that counts how many of a variable are in a matrix.
And it goes like this :
:-use_module(library(lists)).
:-use_module(library(clpfd)).
countOccurrences([],Y,0).
countOccurrences([X|T], Y, Z):-
countOccur(X, Y, N),
Acc #=N + Z,
countOccurrences(T, Y, Acc).
countOccur([],X,0).
countOccur([X|T],X,Y):- countOccur(T,X,Z), Y is 1+Z.
countOccur([X1|T],X,Z):- X1\=X,countOccur(T,X,Z).
countOccur seems to be working fine and returns the right value. The problem is countOccurences is returning the right absolute value but negative. I Just dont seem to understand why that is happening.
Can someone enlighten me ?
An input of [[a,a,a,a]] looking for a does countOccurrences of a in [] and sets Acc = 0 and then countOccur of a in [a,a,a,a] sets N=4 then Acc #= N + Z becomes 0 #= 4 + Z and so the result must be Z = -4.
Like the title states, I am trying to return the sum of the returned values from sub predicates but it's not working. Here is my code:
addlistnum([],[],X).
addlistnum(digits(Y,[A|T]),digits(F,[B|T]),X) :-
X is Y + F.
digits(Num, List) :-
digits(0, List, Num).
digits(Num, [], Num).
digits(N, [A|As], Num) :-
N1 is N * 10 + A,
digits(N1, As, Num).
The sub predicate works fine. It converts list to an integer. Now I want to sum the converted values.
Example:
?- digits(X,[3,3,3]).
X = 333. % works as expected
Building on that, addlistnum([3,3,3,3],[2,2,2],X) is supposed to produce X = 3555 (as 3555 is 3333 + 222), but I get false instead.
I also tried:
addlistnum([],[],X).
addlistnum([A|T],[B|T],X) :-
X is Y + F,
digits(Y,[A|T]),
digits(F,[B|T]).
It simply returns false, which gives no information about is wrong.
Problem is at these rules:
addlistnum([],[],X).
addlistnum(digits(Y,[A|T]),digits(F,[B|T]),X) :-
X is Y + F.
Second one is, "addition of two list is the addition of the integer conversion of these list":
addlistnum(A,B,X) :-
digits(NA,A),
digits(NB,B),
X is NA + NB.
first one is not necessary, "digits" for an empty list is zero, thus, this rule also covers "addition of two empty list is zero"
My aim is to take the numbers between X and Y and produce Z.
num_between(3,6, All)
For example, if X is 3 and Y is 6 then Z is a list of the numbers between X and Y inclusive. Something like num_between(3,6,[3,4,5,6]) should evaluate as true. Here's what I have so far:
num_between(0,0, []).
num_between(X,Y, All) :-
increase(X, New) , % increase number X++
\+(X = Y) , % check if X is not equal to Y
num_between(New,Y,[All|X]) . % requestion ???
increase(F,N) :- N is F+1 .
increase/1 is working and returns number that is required, but
when recursion is gone through num_between/3 it goes unlit: X is 6 then it fails as I want,
but I can not manage to keep numbers or to return them. All = [3,4,5,6].
All = All + F. Could anyone help please.
Your base clause is incorrect: since you never decrease X or Y, they would never get to zero (unless Y starts at zero, and X starts at a non-positive value). The base clause should look like this:
num_between(X, Y, []) :- X > Y.
This ensures that you get an empty result when the user enters an invalid "backward" range (say, from 6 to 3).
Now to the main clause: all you need to do is to check that the range is valid, get the next value, and make a recursive call, like this:
num_between(X, Y, [X|Tail]) :-
X =< Y,
Next is X + 1,
num_between(Next, Y, Tail).
Demo.
Your original code made an error when constructing a list - it tried to use X as the "tail" of the list, which is incorrect:
num_between(New,Y,[All|X]).
you pass on All, the result after an "expansion", down through the recursive chain of invocation. It should be the other way around - you need to pass in a Tail to collect the result, and then pre-pend X to it when the recursive invocation is over.
You have to change both your base case and your recursive clause:
num_between(X, X, [X]).
num_between(X, Y, [X|L]):-
X < Y,
increase(X, New),
num_between(New, Y, L).
First clause is the base case, it states that the number ranging from X and X is just [X].
The recursive clause states that a number X which is less than a number Y should have it in the output list (thus the [X|L] in the third argument of the head), then it increases the value (i'm just using your helper procedure for that) and recursively calling itself now with the New value for the first argument.
I would write this along these lines:
numbers_between( X , X , [X] ) . % if X and Y have converged, we have the empty list
numbers_between( X , Y , [X|Zs] ) :- % otherwise, add X to the result list
X < Y , % - assuming X is less than Y
X1 is X+1 , % - increment X
numbers_between(X1,Y,Zs) % - recurse down
. %
numbers_between( X , Y , [X|Zs] ) :- % otherwise, add X to the result list
X > Y , % - assuming X > Y
X1 is X-1 , % - decrement X
numbers_between(X1,Y,Zs) % - recurse down
. %
Given the following two functions in C language:
int f(int x, int y, int z) {
return (x & y) | ((~x) & z);
}
int g(int x, int y, int z) {
return z ^ (x & (y ^ z));
}
The results of the two functions are equal for any valid integer.
I just wonder the mathematics between the two expressions.
I've first seen the expression for function f in the SHA-1 algorithm on wikipedia.
http://en.wikipedia.org/wiki/Sha1
In the "SHA-1 pseudocode" part, inside the Main loop:
if 0 ≤ i ≤ 19 then
f = (b and c) or ((not b) and d)
k = 0x5A827999
...
In some open source implementation, it uses the form in function g: z ^ (x & (y ^ z)).
I write a program and iterate all the possible values for x, y, z, and all the results are equal.
How to deduce the form
(x & y) | ((~x) & z)
to the form
z ^ (x & (y ^ z))
in mathematics? Not just only proving the equality.
Since bitwise operations are equivalent to boolean operations on the individual bits, you can prove the equivalence simply by enumerating the eight assignments of the {x, y, z} three-tuples.
Fill out the truth tables for each of these two functions, and then compare the eight positions to each other. If all eight positions match, the two functions are equivalent; otherwise, the functions are different.
You do not need to do it manually either: plug in both functions in three nested loops that give x, y, and z values from zero to one, inclusive, and compare the results of invoking f(x,y,z) to g(x,y,z).
You can do this using a Karnaugh Map. Given the truth table for z ^ (x & (y ^ z)), the Karnaugh map is:
As can be seen, you can make two groups from the diagram, giving you (x & y) | (~x & z)
here is the plus code that i don't understand
plus(0,X,X):-natural_number(X).
plus(s(X),Y,s(Z)) :- plus(X,Y,Z).
while given :
natural_number(0).
natural_number(s(X)) :- natural_number(X).
I don't understand this recursion. If I have plus(s(0),s(s(s(0))),Z) how can i get the answer of 1+3=4?
I need some explanation for the first code. I try that plus(0,X,X) will stop the recursion but I think that I do it wrong.
So, let's start with natural_number(P). Read this as "P is a natural number". We're given natural_number(0)., which tells us that 0 is always a natural number (i.e. there are no conditions that must be met for it to be a fact). natural_number(s(X)) :- natural_number(X). tells us that s(X) is a natural number if X is a natural number. This is the normal inductive definition of natural numbers, but written "backwards" as we read Prolog "Q := P" as "Q is true if P is true".
Now we can look at plus(P, Q, R). Read this as "plus is true if P plus Q equals R". We then look at the cases we're given:
plus(0,X,X) :- natural_number(X).. Read as Adding 0 to X results in X if X is a natural number. This is our inductive base case, and is the natural definition of addition.
plus(s(X),Y,s(Z)) :- plus(X,Y,Z). Read as "Adding the successor of X to Y results in the successor Z if adding X to Y is Z'. If we change the notation, we can read it algebraically as "X + 1 + Y = Z + 1 if X + Y = Z", which is very natural again.
So, to answer you direct question "If I have plus(s(0),s(s(s(0))),z), how can i get the answer of 1+3=4?", let's consider how we can unify something with z each step of the induction
Apply the second definition of plus, as it's the only one that unifies with the query. plus(s(0),s(s(s(0))), s(z')) is true if plus(0, s(s(s(0))), z') is true for some z
Now apply the first definition of plus, as it's the only unifying definition: plus(0, s(s(s(0))), z') if z' is s(s(s(0))) and s(s(s(0))) is a natural number.
Unwind the definition of natural_number a few times on s(s(s(0))) to see that is true.
So the overall statement is true, if s(s(s(0))) is unified with z' and s(z') is unified with z.
So the interpreter returns true, with z' = s(s(s(0))) and z = s(z'), i.e. z = s(s(s(s(0)))). So, z is 4.
That code is a straightforward implementation of addition in Peano arithmetic.
In Peano arithmetic, natural numbers are represented using the constant 0 and the unary function s. So s(0) is a representation of 1, s(s(s(0))) is representation of 3. And plus(s(0),s(s(s(0))),Z) will give you Z = s(s(s(s(0)))), which is a representation of 4.
You won't get numerical terms like 1+3=4, all you get is the term s/1 which can embed itself to any depth and thus can represent any natural number. You can combine such terms (using plus/3) and thereby achieve summing.
Note that your definition of plus/3 has nothing to do with SWI-Prolog's built-in plus/3 (which works with integers and not with the s/1 terms):
?- help(plus).
plus(?Int1, ?Int2, ?Int3)
True if Int3 = Int1 + Int2.
At least two of the three arguments must be instantiated to integers.