Created
October 28, 2014 19:45
-
-
Save player999/a5fefaa2546d10aeddcf to your computer and use it in GitHub Desktop.
Compositions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from functools import partial,reduce | |
T = 1 | |
F = 0 | |
def CheckArgs(*args): | |
for entry in args: | |
if type(entry) is not Elem: | |
raise Exception("Wrong arguments! Type: %s"%type(entry)) | |
class Elem: | |
_k = 0 | |
_v = 0 | |
def __init__(self, k = 0, v = 0): | |
if type(k) is not int: | |
raise Exception("Can not make elem of key %d", k) | |
if type(v) is not int: | |
raise Exception("Can not make elem of value %d", v) | |
self._k = k | |
self._v = v | |
def __str__(self): | |
return "Key: %d,Value: %d"%(self._k, self._v) | |
def GetValue(self): | |
return self._v | |
def GetName(self): | |
return self._k | |
def toLogic(a): | |
if a > 0: | |
return T | |
else: | |
return F | |
#1. Get name of variable | |
def GetName(*args): | |
CheckArgs(*args) | |
k1 = args[0].GetName() | |
kn = args[-1].GetName() | |
return Elem(kn,k1) | |
#2. Create veriable with name | |
def PutName(*args): | |
CheckArgs(*args) | |
v1 = args[0].GetValue() | |
vn = args[-1].GetValue() | |
return Elem(vn,v1) | |
#3. Zero Value | |
def Zero(*args): | |
CheckArgs(*args) | |
k1 = args[0].GetName() | |
return Elem(k1,0) | |
#4. Increment | |
def Increment(*args): | |
CheckArgs(*args) | |
k1 = args[0].GetName() | |
v1 = args[0].GetValue() | |
return Elem(k1,v1+1) | |
#5. Summa | |
def Summa(*args): | |
CheckArgs(*args) | |
v = 0 | |
for entry in args: | |
v = v + entry.GetValue() | |
k1 = args[0].GetName() | |
return Elem(k1,v) | |
#6. Equality | |
def Equals(*args): | |
CheckArgs(*args) | |
for i in range(0, len(args)-1): | |
if args[i].GetValue() != args[i+1].GetValue(): | |
return F | |
return T | |
#7. Selector | |
def Im(m, *args): | |
return args[m] | |
def I(m): | |
return partial(Im, m) | |
#8. Negation | |
def Neg(*args): | |
if type(args[0]) != int: | |
raise Exception("Wrong arguments! Type: %s"%type(args[0])) | |
if args[0] == T: | |
return F | |
else: | |
return T | |
#9. And | |
def And(*args): | |
a = reduce(lambda x, y: x and y, args) | |
if a: | |
return T | |
else: | |
return F | |
#Composition Superposition | |
def S(function, *args): | |
return lambda *x: function(*tuple(map(lambda y: y(*x), args))) | |
#Composition IF | |
def If(pred, true_value, false_value): | |
return lambda *x: true_value(*x) if pred(*x) else false_value(*x) | |
#Composition FOR | |
def For(pred, *args): | |
def func(*a): | |
if len(a) != len(args): | |
raise Exception("Argument count mismatch") | |
while pred(*a): | |
a = tuple(map(lambda y: y(*a), args)) | |
return a[0] | |
return func | |
if __name__ == "__main__": | |
#a = 7; b = 2; a - b | |
#c = For(S(And, S(Neg, S(Equals, I(1), I(3))), S(Neg, S(Equals, I(2), I(3)))), Increment, I(1), I(2), S(Increment, I(3))) | |
subtract = If( | |
S( | |
Equals, | |
For( | |
S(And, | |
S(Neg, S(Equals, I(1), I(3))), | |
S(Neg, S(Equals, I(0), S(Summa, I(1), I(2)))) | |
), | |
Increment, | |
I(1), | |
I(2), | |
S(Increment, I(3)) | |
), | |
S(Summa, I(1), I(2)) | |
), | |
Zero, | |
For( | |
S(And, | |
S(Neg, S(Equals, I(1), I(3))), | |
S(Neg, S(Equals, I(0), S(Summa, I(1), I(2)))) | |
), | |
Increment, | |
I(1), | |
I(2), | |
S(Increment, I(3)) | |
) | |
) | |
zero = Elem(1,0) | |
a = Elem(2,6) | |
b = Elem(3,1) | |
print(c(zero, a, b, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment