Skip to content

Commit 3e1ffd2

Browse files
committed
#18 Computation order according to the start position
1 parent e98c700 commit 3e1ffd2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Diff for: src/greedy/greedy_new_version.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ def compute_instr(self, instr: instr_JSON_T, position_to_start_computing: cstack
894894
seq = []
895895

896896
# Decide in which order computations must be done (after computing the subterms)
897-
input_vars = self._computation_order(instr, cstate)
897+
input_vars = self._computation_order(instr, position_to_start_computing, cstate)
898898
self.debug_logger.debug_message(f"Fixed elements {self.fixed_elements} {cstate}", depth)
899899

900900
for i, stack_var in enumerate(input_vars):
@@ -917,24 +917,26 @@ def compute_instr(self, instr: instr_JSON_T, position_to_start_computing: cstack
917917

918918
return seq
919919

920-
def _computation_order(self, instr: instr_JSON_T, cstate: SymbolicState) -> List[var_id_T]:
920+
def _computation_order(self, instr: instr_JSON_T, start_position: cstack_pos_T,
921+
cstate: SymbolicState) -> List[var_id_T]:
921922
"""
922923
Decides in which order the arguments of the instruction must be computed
923924
"""
924-
if instr['commutative']:
925+
self.debug_logger.debug_message(f"{start_position} {len(cstate.stack)}")
926+
if instr['commutative'] and -len(cstate.stack) <= start_position:
925927
# If it's commutative, study its dependencies.
926928
if self.debug_mode:
927929
assert len(instr['inpt_sk']) == 2, \
928930
f'Commutative instruction {instr["id"]} has arity != 2'
929931

930932
# Condition: the top of the stack can be reused
931-
topmost_element = cstate.top_stack()
933+
first_consumed_element = cstate.stack[start_position]
932934
first_arg_instr = self._var2instr.get(instr['inpt_sk'][0], None)
933935

934936
# Condition1: the topmost element can be reused by the first argument instruction or is the first argument
935-
condition1 = (topmost_element is not None and topmost_element in self._top_can_be_used[instr["id"]] and
936-
first_arg_instr is not None and (first_arg_instr["outpt_sk"][0] == topmost_element or
937-
topmost_element in self._top_can_be_used[first_arg_instr["id"]]))
937+
condition1 = (first_consumed_element is not None and first_consumed_element in self._top_can_be_used[instr["id"]] and
938+
first_arg_instr is not None and (first_arg_instr["outpt_sk"][0] == first_consumed_element or
939+
first_consumed_element in self._top_can_be_used[first_arg_instr["id"]]))
938940

939941
# Condition2: the first argument just needs to be swapped
940942
condition2 = cstate.stack_var_copies_needed[instr['inpt_sk'][0]] == 0
@@ -944,6 +946,7 @@ def _computation_order(self, instr: instr_JSON_T, cstate: SymbolicState) -> List
944946
input_vars = list(reversed(instr['inpt_sk']))
945947
else:
946948
input_vars = list(reversed(instr['inpt_sk']))
949+
947950
return input_vars
948951

949952
def decide_fixed_elements(self, cstate: SymbolicState, instr: instr_JSON_T) -> Tuple[int, cstack_pos_T]:

0 commit comments

Comments
 (0)