2 # vim: set fileencoding=utf8
3 # Created: April 22, 2008
6 # Currently produces list of functions to apply.
7 # Use get_unary to find a unary funcion.
9 # Use get_dyadic to find a dyadic funcion.
10 # ie. get_dyadic("ii0111001")
11 # Todo: add utf support names and return string expression
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
24 buf
= Trits
.Trits("i01") # identity
25 ci
= Trits
.Trits("iii") # constant i
26 c0
= Trits
.Trits("000") # constant 0
27 c1
= Trits
.Trits("111") # constant 1
29 valid_chars
= ("i", "0", "1")
30 map_t
= {False:0, None:1, True:2}
31 map_str
= {False:"i", None:"0", True:"1"}
33 basic
= {"}":sd
, "{":su
, ">":s01
, "<":si0
, "[":ru
,
35 easy
= {"B":buf
, "i":ci
, "0":c0
, "1":c1
}
38 def get_dyadic(desired
):
39 ''' get_dyadic: get expression for a dyadic function
40 desired: string representation of desired function
41 returns: Returns a tuple. True or false (if function was not found).
42 A list of expressions for each unary function needed to build
47 print "truth table must be 9 chars wide"
51 if not desired
[i
] in valid_chars
:
52 print "%s not a valid character" % desired
[i
]
59 result_1
, gates_1
= get_unary(goal_1
)
60 result_2
, gates_2
= get_unary(goal_2
)
61 result_3
, gates_3
= get_unary(goal_3
)
63 if result_1
and result_2
and result_3
:
64 return True, gates_1
, gates_2
, gates_3
66 return False, [], [], []
69 def get_unary(desired
):
70 ''' get_unary: get expression for a unary function
71 desired: string representation of desired function
72 returns: Returns a tuple. True or false (if function was not found)
73 and a list of functions to apply to get the desired function.
74 The list is read from left to right.
77 # do some input error checking
79 print "truth table must be 3 chars wide"
83 if not desired
[i
] in valid_chars
:
84 print "%s not a valid character" % desired
[i
]
87 goal
= Trits
.Trits(desired
)
88 crnt
= Trits
.Trits("i01")
92 if goal
.equals(easy
[i
]):
97 while end_cond
and count
< 4:
98 end_cond
, l_gates
= recurse_unary(crnt
, goal
, [], count
)
99 end_cond
= not end_cond
102 return (not end_cond
, l_gates
)
104 def recurse_unary(crnt
, goal
, l_gates
, depth
):
105 ''' recurse_unary: attempt to locate a function sequence that makes the
107 crnt: current function result
108 goal: the desired function result
109 l_gates: list of gates to apply to get to crnt
110 count: the number of gate levels
111 returns: true and list of gates if goal is met, else false and []
114 # check the basic gates for an answer
116 return test_all(crnt
, goal
, l_gates
)
119 # for each basic gate attempt to find the desired function
121 cndt
= eval_one(crnt
, i
)
124 if cndt
.equals(goal
):
127 status
, gates
= recurse_unary(cndt
, goal
, l_gates
, depth
- 1)
136 def test_all(crnt
, goal
, l_gates
):
137 ''' test_all: attempt to find a basic function that fulfills the desired
139 crnt: the current evaluation of the function
140 goal: the desired function result
141 l_gates: list of gates currently applied to crnt
142 returns: true and list of gates if goal is met, else false and []
146 cndt
= eval_one(crnt
, i
)
148 # if desired function is found then return true and the list of gates
150 if cndt
.equals(goal
):
156 def eval_one(crnt
, func
):
157 ''' eval_one: the input given in crnt with function 'func'
158 crnt: Trits object as input
159 func: function to apply
160 return: Trist object containing result
164 for i
in range(0, 3):
165 next
= next
+ map_str
[basic
[func
][map_t
[crnt
[i
]]]]
167 return Trits
.Trits(next
)
169 if __name__
== "__main__":
170 result
, gates
= get_unary("11i")
174 result
, gates1
, gates2
, gates3
= get_dyadic("i0111000i")
176 print gates1
, gates2
, gates3
178 print "At prompt '>>' type unary (010) or dyadic (1010i1ii0)"
183 line
= sys
.stdin
.readline()
187 if len(line
) == 3 or len(line
) == 9:
189 for i
in range(0, len(line
)):
190 if not line
[i
] in valid_chars
:
195 result
, gates
= get_unary(line
)
198 result
, gates1
, gates2
, gates3
= get_dyadic(line
)
199 print gates1
, gates2
, gates3