Given some functions:
define foo(x,y) (x > y) end.
define bar(x) (foo x x) end.
define baz(x,y) (foo x y) end.
I am not interested in the exact values that a function takes/returns however I would like to know about how the sign of an input affects the output.
To model the greater than function I use the atoms zero_int which is interpreted as 0, pos_int which is interpreted as all integers greater than 0 and neg_int interpreted as all integers less than 0.
%% pos_int greater then ...
%% i.e forall n,m : (n > 0) & (m = 0) => n is_greater_than m
gt(pos_int,zero_int,true).
gt(pos_int,neg_int,true).
gt(pos_int, pos_int, X) :- boolean(X). % return either true or false.
%% zero greater than ...
gt(zero_int, pos_int, false).
gt(zero_int,zero_int,false).
gt(zero_int, neg_int, true).
%% neg int greater than...
gt(neg_int, pos_int, false).
gt(neg_int, zero_int, false).
gt(neg_int, neg_int, X) :- boolean(X).
the boolean/1 predicate is used to return either true or false when there is a choice point. i.e comparing n > m where n > 0 and m > 0 may be be either true or false. As we do not know the actual (integer) values of n and m assume that both cases are true.
%% define booleans
boolean(true).
boolean(false).
Now I encode the functions in the following way:
foo(X,Y,Return) :- gt(X,Y,Return).
bar(X,Return) :- foo(X,X,Return).
baz(X,Y,Return) :- foo(X,Y,Return).
testing foo (and baz) we get expected results:
?- foo(X,Y,Return).
X = pos_int,
Y = zero_int,
Return = true ;
X = pos_int,
Y = neg_int,
Return = true ;
X = Y, Y = pos_int,
Return = true ;
X = Y, Y = pos_int,
Return = false ;
X = zero_int,
Y = pos_int,
Return = false ;
...
My issue is that because bar calls gt with a single value - I would like for it to always return false as it is never the case than n > n
?- bar(X,Return).
X = pos_int,
Return = true ;
X = pos_int,
Return = false ;
X = zero_int,
Return = false ;
X = neg_int,
Return = true ;
X = neg_int,
Return = false.
I am interested in how one might encode this. I have explored using pairs of variables as inputs that would allow for labels which could be compared.
You could try something like
boolean(true).
boolean(false).
gt2((_, pos_int), (_, zero_int), (_,true)).
gt2((_, pos_int), (_, neg_int), (_,true)).
gt2((L1, pos_int), (L2, pos_int), (_,X)) :- not(L1 == L2), boolean(X).
%% zero greater than ...
gt2((_, zero_int), (_, pos_int), (_,false)).
gt2((_, zero_int), (_, zero_int), (_,false)).
gt2((_, zero_int), (_, neg_int), (_,true)).
%% neg int greater than...
gt2((_, neg_int), (_, pos_int), (_,false)).
gt2((_, neg_int), (_, zero_int), (_,false)).
gt2((L1, neg_int), (L2, neg_int), (_,X)) :- not(L1 == L2), boolean(X).
gt2((L,pos_int),(L,pos_int),(_,false)).
gt2((L,neg_int),(L,neg_int),(_,false)).
foo(X,Y,Return) :- gt2(X,Y,Return).
bar(X,Return) :- foo(X,X,Return).
baz(X,Y,Return) :- foo(X,Y,Return).
This uses the idea of labels which allow us to tag inputs such that we can encode equality. Basically if the labels match then the input is strictly equal. (ie given two inputs that within the set of positive integers, if they have the same label, then they are the same integer.
which would give the results:
?- bar(X,R).
X = (_31408, zero_int),
R = (_31414, false) ;
X = (_31408, pos_int),
R = (_31414, false) ;
X = (_31408, neg_int),
R = (_31414, false).
?- foo(X,Y,R).
X = (_31852, pos_int),
Y = (_31858, zero_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, pos_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, zero_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, neg_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, neg_int),
Y = (_31858, zero_int),
R = (_31864, false) ;
X = (_31852, neg_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, neg_int),
Y = (_31858, neg_int),
R = (_31864, false) ;
X = Y, Y = (_31852, pos_int),
R = (_31864, false) ;
X = Y, Y = (_31852, neg_int),
R = (_31864, false).
notice that foo generates an extra input for when the labels are the same.
I wonder if there is a quick way to check whether 2 datatypes are the same.
Example - let's say I have a list like this:
[odd(1), odd(3), even(2), odd(5), even(4)]
Now if I pull 2 elements from the list (index 0 and index 1) I want to know if they are both of the same data type or different (i.e. odd()).
If X and Y are pulled from the list, you can check literally,
X = odd(_), Y = odd(_) ; X = even(_), Y = even(_).
Or more generally, you can check...
functor(X, Functor, ArgCount),
functor(Y, Functor, ArgCount).
The above will succeed if X and Y have the same functor (even or odd, for example) and whether they have the same number of arguments. functor/3 is an ISO Prolog predicate, so it works on any ISO compliant Prolog.
You can do a little test at the Prolog prompt that illustrates. Note that only successful results are when the "data types" of the chosen list members match.
| ?- List = [odd(1), odd(3), even(2), odd(5), even(4)], member(X, List), member(Y, List), functor(X, F, A), functor(Y, F, A).
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(1) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(3) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(5) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(1) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(3) ? a
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(5)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(2)
Y = even(2)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(2)
Y = even(4)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(1)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(3)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(5)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(4)
Y = even(2)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(4)
Y = even(4)
yes
| ?-
Here is My Question :
?- rule7(X,Y,t3).
X = s10,
Y = s11 ;
X = s12,
Y = s10 ;
X = s12,
Y = s10 ;
X = s12,
Y = s11 ;
X = s12,
Y = s11 ;
X = s12,
Y = s13 ;
X = s12,
Y = s13 .....
It will repeat it lot of times. How can I reduce these repeatings...
The Code :
rule7(X,Y,T) :-
course(C,X),
course(C1,Y),
course_lec(C1,T),
course_lec(C,T),
copy_rec(Y,X).
Every approximately second(sometime twice a second) I have a data from COM-port, like: [1 2 3 1].
This list mean a part of image - first vertical line.
I want draw in real-time whole image, like scanner.
for example:
First data coming:
x
x
x
x
Second
x x
x x
x x
x x
..
After N - data tacking:
x x x ... x
x x x ... x
x x x ... x
x x x ... x
How can I do that?
I think, I need to accamulate array, like:
Array=list()
new_data=..
Array.append(new_data)
*Create_image*
[EDITED] Try this:
from tkinter import *
root = Tk()
cans=Canvas(root,height=500,width=600)
cans.pack()
delay = 1 # milliseconds
array_list=[] # this will be list of arrays
def get_new_data():
new_data=[]
new_data = #whatever write code here how you get next new list from somewhere
array_list.append(raw_data)
return array_list
def get_lits(x):
get_new_data()
return array_list[x-2]
def get_color(y, x):
max_num = 15
raw_array=get_data(x)
c = raw_array[y] * 255 / max_num
colorname = '#%02x%02x%02x' % (c, c, c)
return colorname
def draw_line(i=2, j=0):
if i <= 600:
if j<=500:
cans.create_line(i,j,i+1,j, fill=get_color(j, i))
root.after(delay, draw_line, i+0, j+1)
elif j>150:
j=0
root.after(delay, draw_line, i+1)
draw_line()
root.mainloop()
Just write your code how to get new array in get_new_data(), also change max_num =12 to your maximum value of array.
I want to implement the following function f(x,y) in Prolog
f(x,y) = a*x+b*y
where a = 1 if x > 0; a = -1 if x < 0; a = 0 if x = 0
and b = -1 if y > 0; b = 1 if y < 0; b = 0 if y = 0
For example,
f(2,-1) = 1*2 + 1*(-1) = 1
f(-2,-1) = (-1)*(-2) + (-1)*1 = 1
f(0,0) = 0*0 + 0*0 = 0
Any one can help?
How about using the following formulation?
f(X,Y,Result) :-
Result is abs(X) - abs(Y).
Let's run some queries:
?- f(0,0,0).
true.
?- f(-2,-1,1).
true.
?- f(2,-1,1).
true.
(Assuming you have a typo when defining y, y>0 not y>=0.)
You need to define a relation between the input vars and the result of the function. Prolog can then answer yes/true with substitutions or no/false.
f(X,Y,Answer):-
a_is(X,A),
b_is(Y,B),
Answer is A*X+B*Y.
a_is(X,1):-
X>0.
a_is(X,-1):-
X<0.
a_is(0,0).
b_is(Y,1):-
Y<0.
b_is(Y,-1):-
Y>0.
b_is(0,0).
Example:
?-f(2,-1,Answer).
Answer =1;
false.
Shouldn't be much more complicated than this one-liner:
f(X,Y,Z) :- Z is sign(X)*X + -sign(Y)*Y