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 # 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.
18 sd
= Trits
.Trits("ii0") # shift down
19 su
= Trits
.Trits("011") # shift up
20 s01
= Trits
.Trits("i10") # swap 0/1
21 si0
= Trits
.Trits("0i1") # swap i/0
22 ru
= Trits
.Trits("01i") # rotate up
23 rd
= Trits
.Trits("1i0") # rotate down
24 inv
= Trits
.Trits("10i") # inverter
26 buf
= Trits
.Trits("i01") # identity
27 ci
= Trits
.Trits("iii") # constant i
28 c0
= Trits
.Trits("000") # constant 0
29 c1
= Trits
.Trits("111") # constant 1
31 valid_chars
= ("i", "0", "1")
32 map_t
= {False:0, None:1, True:2}
33 map_str
= {False:"i", None:"0", True:"1"}
35 basic
= {"}":sd
, "{":su
, ">":s01
, "<":si0
, "[":ru
,
37 easy
= {"B":buf
, "i":ci
, "0":c0
, "1":c1
}
40 def get_dyadic(desired
):
41 ''' get_dyadic: get expression for a dyadic function
42 desired: string representation of desired function
43 returns: Returns a tuple. True or false (if function was not found).
44 A list of expressions for each unary function needed to build
49 print "truth table must be 9 chars wide"
53 if not desired
[i
] in valid_chars
:
54 print "%s not a valid character" % desired
[i
]
61 result_1
, gates_1
= get_unary(goal_1
)
62 result_2
, gates_2
= get_unary(goal_2
)
63 result_3
, gates_3
= get_unary(goal_3
)
65 if result_1
and result_2
and result_3
:
66 return True, gates_1
, gates_2
, gates_3
68 return False, [], [], []
71 def get_unary(desired
):
72 ''' get_unary: get expression for a unary function
73 desired: string representation of desired function
74 returns: Returns a tuple. True or false (if function was not found)
75 and a list of functions to apply to get the desired function.
76 The list is read from left to right.
79 # do some input error checking
81 print "truth table must be 3 chars wide"
85 if not desired
[i
] in valid_chars
:
86 print "%s not a valid character" % desired
[i
]
89 goal
= Trits
.Trits(desired
)
90 crnt
= Trits
.Trits("i01")
94 if goal
.equals(easy
[i
]):
99 while end_cond
and count
< 4:
100 end_cond
, l_gates
= recurse_unary(crnt
, goal
, [], count
)
101 end_cond
= not end_cond
104 return (not end_cond
, l_gates
)
106 def recurse_unary(crnt
, goal
, l_gates
, depth
):
107 ''' recurse_unary: attempt to locate a function sequence that makes the
109 crnt: current function result
110 goal: the desired function result
111 l_gates: list of gates to apply to get to crnt
112 count: the number of gate levels
113 returns: Returns a tuple. True or false (if function was not found)
114 and a list of functions to apply to get the desired function.
115 The list is read from left to right.
118 # check the basic gates for an answer
120 return test_all(crnt
, goal
, l_gates
)
123 # for each basic gate attempt to find the desired function
125 cndt
= eval_one(crnt
, i
)
128 if cndt
.equals(goal
):
131 status
, gates
= recurse_unary(cndt
, goal
, l_gates
, depth
- 1)
140 def test_all(crnt
, goal
, l_gates
):
141 ''' test_all: attempt to find a basic function that fulfills the desired
143 crnt: the current evaluation of the function
144 goal: the desired function result
145 l_gates: list of gates currently applied to crnt
146 returns: true and list of gates if goal is met, else false and []
150 cndt
= eval_one(crnt
, i
)
152 # if desired function is found then return true and the list of gates
154 if cndt
.equals(goal
):
160 def eval_one(crnt
, func
):
161 ''' eval_one: the input given in crnt with function 'func'
162 crnt: Trits object as input
163 func: function to apply
164 return: Trist object containing result
168 for i
in range(0, 3):
169 next
= next
+ map_str
[basic
[func
][map_t
[crnt
[i
]]]]
171 return Trits
.Trits(next
)
173 if __name__
== "__main__":
174 result
, gates
= get_unary("11i")
178 result
, gates1
, gates2
, gates3
= get_dyadic("i0111000i")
180 print gates1
, gates2
, gates3
182 print "At prompt '>>' enter unary (010) or dyadic (1010i1ii0)"
183 print "Will return list of unary funtions to sequentially apply to a"
184 print "signal starting from the left."
189 line
= sys
.stdin
.readline()
193 if len(line
) == 3 or len(line
) == 9:
195 for i
in range(0, len(line
)):
196 if not line
[i
] in valid_chars
:
201 result
, gates
= get_unary(line
)
204 result
, gates1
, gates2
, gates3
= get_dyadic(line
)
205 print gates1
, gates2
, gates3