exprCreator.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!env python
  2. # vim: set fileencoding=utf8
  3. # Created: April 22, 2008
  4. #
  5. # Expression creator
  6. # Currently produces list of functions to apply.
  7. # Use get_unary to find a unary funcion.
  8. # ie. get_unary("ii0")
  9. # Use get_dyadic to find a dyadic funcion.
  10. # ie. get_dyadic("ii0111001")
  11. # Returns: Returns a tuple. True or false (if function was not found)
  12. # and a list of functions to apply to get the desired function.
  13. # The list is read from left to right.
  14. import sys, os
  15. import Trits
  16. sd = Trits.Trits("ii0") # shift down
  17. su = Trits.Trits("011") # shift up
  18. s01 = Trits.Trits("i10") # swap 0/1
  19. si0 = Trits.Trits("0i1") # swap i/0
  20. ru = Trits.Trits("01i") # rotate up
  21. rd = Trits.Trits("1i0") # rotate down
  22. inv = Trits.Trits("10i") # inverter
  23. buf = Trits.Trits("i01") # identity
  24. ci = Trits.Trits("iii") # constant i
  25. c0 = Trits.Trits("000") # constant 0
  26. c1 = Trits.Trits("111") # constant 1
  27. valid_chars = ("i", "0", "1")
  28. map_t = {False:0, None:1, True:2}
  29. map_str = {False:"i", None:"0", True:"1"}
  30. basic = {"}":sd, "{":su, ">":s01, "<":si0, "[":ru,
  31. "]":rd, "/":inv}
  32. easy = {"B":buf, "i":ci, "0":c0, "1":c1}
  33. def get_dyadic(desired):
  34. ''' get_dyadic: get expression for a dyadic function
  35. desired: string representation of desired function
  36. returns: Returns a tuple. True or false (if function was not found).
  37. A list of expressions for each unary function needed to build
  38. the desired function.
  39. '''
  40. if len(desired) != 9:
  41. print "truth table must be 9 chars wide"
  42. raise SystemExit
  43. for i in range(0, 9):
  44. if not desired[i] in valid_chars:
  45. print "%s not a valid character" % desired[i]
  46. raise SystemExit
  47. goal_1 = desired[0:3]
  48. goal_2 = desired[3:6]
  49. goal_3 = desired[6:9]
  50. result_1, gates_1 = get_unary(goal_1)
  51. result_2, gates_2 = get_unary(goal_2)
  52. result_3, gates_3 = get_unary(goal_3)
  53. if result_1 and result_2 and result_3:
  54. return True, gates_1, gates_2, gates_3
  55. return False, [], [], []
  56. def get_unary(desired):
  57. ''' get_unary: get expression for a unary function
  58. desired: string representation of desired function
  59. returns: Returns a tuple. True or false (if function was not found)
  60. and a list of functions to apply to get the desired function.
  61. The list is read from left to right.
  62. '''
  63. # do some input error checking
  64. if len(desired) != 3:
  65. print "truth table must be 3 chars wide"
  66. raise SystemExit
  67. for i in range(0, 3):
  68. if not desired[i] in valid_chars:
  69. print "%s not a valid character" % desired[i]
  70. raise SystemExit
  71. goal = Trits.Trits(desired)
  72. crnt = Trits.Trits("i01")
  73. l_gates = []
  74. for i in easy:
  75. if goal.equals(easy[i]):
  76. return True, [i]
  77. count = 0
  78. end_cond = True
  79. while end_cond and count < 4:
  80. end_cond, l_gates = recurse_unary(crnt, goal, [], count)
  81. end_cond = not end_cond
  82. count = count + 1
  83. return (not end_cond, l_gates)
  84. def recurse_unary(crnt, goal, l_gates, depth):
  85. ''' recurse_unary: attempt to locate a function sequence that makes the
  86. goal function
  87. crnt: current function result
  88. goal: the desired function result
  89. l_gates: list of gates to apply to get to crnt
  90. count: the number of gate levels
  91. returns: Returns a tuple. True or false (if function was not found)
  92. and a list of functions to apply to get the desired function.
  93. The list is read from left to right.
  94. '''
  95. # check the basic gates for an answer
  96. if depth == 0:
  97. return test_all(crnt, goal, l_gates)
  98. else:
  99. # for each basic gate attempt to find the desired function
  100. for i in basic:
  101. cndt = eval_one(crnt, i)
  102. l_gates.append(i)
  103. if cndt.equals(goal):
  104. return True, l_gates
  105. status, gates = recurse_unary(cndt, goal, l_gates, depth - 1)
  106. if status:
  107. return True, gates
  108. l_gates.pop()
  109. return (False, [])
  110. def test_all(crnt, goal, l_gates):
  111. ''' test_all: attempt to find a basic function that fulfills the desired
  112. gate.
  113. crnt: the current evaluation of the function
  114. goal: the desired function result
  115. l_gates: list of gates currently applied to crnt
  116. returns: true and list of gates if goal is met, else false and []
  117. '''
  118. for i in basic:
  119. cndt = eval_one(crnt, i)
  120. # if desired function is found then return true and the list of gates
  121. # to apply
  122. if cndt.equals(goal):
  123. l_gates.append(i)
  124. return True, l_gates
  125. return False, []
  126. def eval_one(crnt, func):
  127. ''' eval_one: the input given in crnt with function 'func'
  128. crnt: Trits object as input
  129. func: function to apply
  130. return: Trist object containing result
  131. '''
  132. next = ""
  133. for i in range(0, 3):
  134. next = next + map_str[basic[func][map_t[crnt[i]]]]
  135. return Trits.Trits(next)
  136. if __name__ == "__main__":
  137. result, gates = get_unary("11i")
  138. if result:
  139. print gates
  140. result, gates1, gates2, gates3 = get_dyadic("i0111000i")
  141. if result:
  142. print gates1, gates2, gates3
  143. print "At prompt '>>' enter unary (010) or dyadic (1010i1ii0)"
  144. print "Will return list of unary funtions to sequentially apply to a"
  145. print "signal starting from the left."
  146. while True:
  147. valid = True
  148. print ">> ",
  149. line = sys.stdin.readline()
  150. line = line.strip()
  151. print
  152. if len(line) == 3 or len(line) == 9:
  153. for i in range(0, len(line)):
  154. if not line[i] in valid_chars:
  155. valid = False
  156. if valid:
  157. if len(line) == 3:
  158. result, gates = get_unary(line)
  159. print gates
  160. if len(line) == 9:
  161. result, gates1, gates2, gates3 = get_dyadic(line)
  162. print gates1, gates2, gates3