2 # vim: set fileencoding=utf8
3 # Created: April 22, 2008
6 # Currently produces list of functions to apply.
7 # Todo: add utf support names and return string expression
12 sd_t
= Trits
.Trits("ii0") # shift down
13 su_t
= Trits
.Trits("011") # shift up
14 s01_t
= Trits
.Trits("i10") # swap 0/1
15 si0_t
= Trits
.Trits("0i1") # swap i/0
16 ru_t
= Trits
.Trits("01i") # rotate up
17 rd_t
= Trits
.Trits("1i0") # rotate down
18 inv_t
= Trits
.Trits("10i") # inverter
20 buf_t
= Trits
.Trits("i01") # identity
21 ci_t
= Trits
.Trits("iii") # constant i
22 c0_t
= Trits
.Trits("000") # constant 0
23 c1_t
= Trits
.Trits("111") # constant 1
25 valid_chars
= ("i", "0", "1")
26 map_t
= {False:0, None:1, True:2}
27 map_str
= {False:"i", None:"0", True:"1"}
29 basic_f_t
= {"sd":sd_t
, "su":su_t
, "s01":s01_t
, "si0":si0_t
, "ru":ru_t
,
30 "rd":rd_t
, "inv":inv_t
}
31 easy_f_t
= {"buf":buf_t
, "ci":ci_t
, "c0":c0_t
, "c1":c1_t
}
34 def get_dyadic(desired
):
35 ''' get_dyadic: get expression for a dyadic function
36 desired: string representation of desired function
37 returns: Returns a tuple. True or false (if function was not found).
38 A list of expressions for each unary function needed to build
43 print "truth table must be 9 chars wide"
47 if not desired
[i
] in valid_chars
:
48 print "%s not a valid character" % desired
[i
]
55 result_1
, gates_1
= get_unary(goal_1
)
56 result_2
, gates_2
= get_unary(goal_2
)
57 result_3
, gates_3
= get_unary(goal_3
)
59 if result_1
and result_2
and result_3
:
60 return True, gates_1
, gates_2
, gates_3
62 return False, [], [], []
65 def get_unary(desired
):
66 ''' get_unary: get expression for a unary function
67 desired: string representation of desired function
68 returns: Returns a tuple. True or false (if function was not found)
69 and a list of functions to apply to get the desired function.
70 The list is read from left to right.
73 # do some input error checking
75 print "truth table must be 3 chars wide"
79 if not desired
[i
] in valid_chars
:
80 print "%s not a valid character" % desired
[i
]
83 goal
= Trits
.Trits(desired
)
84 crnt
= Trits
.Trits("i01")
88 if goal
.equals(easy_f_t
[i
]):
93 while end_cond
and count
< 4:
94 end_cond
, l_gates
= recurse_unary(crnt
, goal
, [], count
)
95 end_cond
= not end_cond
98 return (not end_cond
, l_gates
)
100 def recurse_unary(crnt
, goal
, l_gates
, depth
):
101 ''' recurse_unary: attempt to locate a function sequence that makes the
103 crnt: current function result
104 goal: the desired function result
105 l_gates: list of gates to apply to get to crnt
106 count: the number of gate levels
107 returns: true and list of gates if goal is met, else false and []
110 # check the basic gates for an answer
112 return test_all(crnt
, goal
, l_gates
)
115 # for each basic gate attempt to find the desired function
117 cndt
= eval_one(crnt
, i
)
120 if cndt
.equals(goal
):
123 status
, gates
= recurse_unary(cndt
, goal
, l_gates
, depth
- 1)
132 def test_all(crnt
, goal
, l_gates
):
133 ''' test_all: attempt to find a basic function that fulfills the desired
135 crnt: the current evaluation of the function
136 goal: the desired function result
137 l_gates: list of gates currently applied to crnt
138 returns: true and list of gates if goal is met, else false and []
142 cndt
= eval_one(crnt
, i
)
144 # if desired function is found then return true and the list of gates
146 if cndt
.equals(goal
):
152 def eval_one(crnt
, func
):
153 ''' eval_one: the input given in crnt with function 'func'
154 crnt: Trits object as input
155 func: function to apply
156 return: Trist object containing result
160 for i
in range(0, 3):
161 next
= next
+ map_str
[basic_f_t
[func
][map_t
[crnt
[i
]]]]
163 return Trits
.Trits(next
)
165 if __name__
== "__main__":
166 result
, gates
= get_unary("11i")
170 result
, gates1
, gates2
, gates3
= get_dyadic("i0111000i")
172 print gates1
, gates2
, gates3