Skip to content

Commit

Permalink
Removed trailing whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnamtran committed Jul 17, 2018
1 parent 81eabde commit 6cad486
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
74 changes: 37 additions & 37 deletions cozy/cost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ def prioritized_order(*funcs):
return Order.EQUAL

def unprioritized_order(funcs):
"""Determine the Order between two maintenance cost of two expressions or
"""Determine the Order between two maintenance cost of two expressions or
AMBIGUOUS at the first ambiguous result
Each argument should be a function that takes no argument and returns an
Order.
Each argument should be a function that takes no argument and returns an
Order.
"""
def flip(order):
""" Flips the direction of the Order """
if order == Order.LT:
if order == Order.LT:
return Order.GT
if order == Order.GT:
if order == Order.GT:
return Order.LT
return order

Expand All @@ -65,13 +65,13 @@ def flip(order):
if op == Order.EQUAL: # Ignores equals
continue
# Immediately returns ambiguous, change to `continue` if want to ignore
if op == Order.AMBIGUOUS:
return Order.AMBIGUOUS
# Check if we've seen both < and >
if op == Order.AMBIGUOUS:
return Order.AMBIGUOUS
# Check if we've seen both < and >
if flip(op) in orders_seen:
return Order.AMBIGUOUS
orders_seen.add(op)

return orders_seen.pop() if orders_seen else Order.EQUAL

def order_objects(x, y) -> Order:
Expand All @@ -84,11 +84,11 @@ def order_objects(x, y) -> Order:

class CostModel(object):

def __init__(self,
assumptions : Exp = T,
examples = (),
funcs = (),
freebies : [Exp] = [],
def __init__(self,
assumptions : Exp = T,
examples = (),
funcs = (),
freebies : [Exp] = [],
ops : [Op] = []):
self.solver = ModelCachingSolver(vars=(), funcs=funcs, examples=examples, assumptions=assumptions)
self.assumptions = assumptions
Expand Down Expand Up @@ -142,28 +142,28 @@ def compare(self, e1 : Exp, e2 : Exp, context : Context, pool : Pool) -> Order:
lambda: unprioritized_order(
[lambda: prioritized_order(
lambda: self._compare(
max_storage_size(e1, self.freebies),
max_storage_size(e1, self.freebies),
max_storage_size(e2, self.freebies), context),
lambda: self._compare(rt(e1), rt(e2), context))] +
lambda: self._compare(rt(e1), rt(e2), context))] +
[lambda op=op: self._compare(
maintenance_cost(e1, self.solver, op, self.freebies),
maintenance_cost(e2, self.solver, op, self.freebies),
maintenance_cost(e1, self.solver, op, self.freebies),
maintenance_cost(e2, self.solver, op, self.freebies),
context) for op in self.ops]),
lambda: order_objects(e1.size(), e2.size()))
lambda: order_objects(e1.size(), e2.size()))
else:
return prioritized_order(
lambda: unprioritized_order(
[lambda: prioritized_order(
lambda: self._compare(
max_storage_size(e1, self.freebies),
max_storage_size(e1, self.freebies),
max_storage_size(e2, self.freebies), context),
lambda: self._compare(rt(e1), rt(e2), context))] +
lambda: self._compare(rt(e1), rt(e2), context))] +
[lambda op=op: self._compare(
maintenance_cost(e1, self.solver, op, self.freebies),
maintenance_cost(e2, self.solver, op, self.freebies),
maintenance_cost(e1, self.solver, op, self.freebies),
maintenance_cost(e2, self.solver, op, self.freebies),
context) for op in self.ops]),
lambda: order_objects(e1.size(), e2.size()))
else:
else:
if pool == RUNTIME_POOL:
return prioritized_order(
lambda: order_objects(asymptotic_runtime(e1), asymptotic_runtime(e2)),
Expand Down Expand Up @@ -289,11 +289,11 @@ def _maintenance_cost(e : Exp, solver : ModelCachingSolver, op : Op, freebies :

h = extension_handler(type(e.type))
if h is not None:
return h.maintenance_cost(e,
return h.maintenance_cost(e,
solver=solver,
op=op,
op=op,
freebies=freebies,
storage_size=storage_size,
storage_size=storage_size,
maintenance_cost=_maintenance_cost)

if is_scalar(e.type):
Expand All @@ -310,33 +310,33 @@ def _maintenance_cost(e : Exp, solver : ModelCachingSolver, op : Op, freebies :
elif isinstance(e.type, TMap):
keys = EMapKeys(e).with_type(TBag(e.type.k))
vals = EMap(
keys,
keys,
mk_lambda(
e.type.k,
e.type.k,
lambda k: EMapGet(e, k).with_type(e.type.v))).with_type(TBag(e.type.v))

keys_prime = EMapKeys(e_prime).with_type(TBag(e_prime.type.k))
vals_prime = EMap(
keys_prime,
keys_prime,
mk_lambda(
e_prime.type.k,
e_prime.type.k,
lambda k: EMapGet(e_prime, k).with_type(e_prime.type.v))).with_type(TBag(e_prime.type.v))

keys_added = storage_size(
EBinOp(keys_prime, "-", keys).with_type(keys.type), freebies).with_type(INT)
EBinOp(keys_prime, "-", keys).with_type(keys.type), freebies).with_type(INT)
keys_rmved = storage_size(
EBinOp(keys, "-", keys_prime).with_type(keys.type), freebies).with_type(INT)
EBinOp(keys, "-", keys_prime).with_type(keys.type), freebies).with_type(INT)

vals_added = storage_size(
EBinOp(vals_prime, "-", vals).with_type(vals.type), freebies).with_type(INT)
EBinOp(vals_prime, "-", vals).with_type(vals.type), freebies).with_type(INT)
vals_rmved = storage_size(
EBinOp(vals, "-", vals_prime).with_type(vals.type), freebies).with_type(INT)
EBinOp(vals, "-", vals_prime).with_type(vals.type), freebies).with_type(INT)

keys_difference = ESum([keys_added, keys_rmved])
vals_difference = ESum([vals_added, vals_rmved])
return EBinOp(keys_difference, "*", vals_difference).with_type(INT)

else:
else:
raise NotImplementedError(repr(e.type))

def maintenance_cost(e : Exp, solver : ModelCachingSolver, op : Op, freebies : [Exp] = []):
Expand Down
16 changes: 8 additions & 8 deletions cozy/structures/heaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ def storage_size(self, e : Exp, storage_size) -> Exp:
assert type(e.type) in (TMinHeap, TMaxHeap)
return storage_size(EHeapElems(e).with_type(TBag(e.type.elem_type)))

def maintenance_cost(self,
e : Exp,
def maintenance_cost(self,
e : Exp,
solver,
op : Op,
storage_size,
maintenance_cost,
storage_size,
maintenance_cost,
freebies : [Exp] = []):
assert type(e.type) in (TMinHeap, TMaxHeap)

Expand All @@ -150,7 +150,7 @@ def maintenance_cost(self,
old_elems = EHeapElems(old_value).with_type(t)
new_elems = EHeapElems(new_value).with_type(t)

# Add these
# Add these
elems_added = storage_size(
EBinOp(new_elems, "-", old_elems).with_type(t), freebies).with_type(INT)
elems_rmved = storage_size(
Expand All @@ -164,13 +164,13 @@ def maintenance_cost(self,
new_v_key = f2.apply_to(v)

modified_elems = EFilter(old_elems, ELambda(v, EAll([EIn(v, new_elems), ENot(EEq(new_v_key, old_v_key))]))).with_type(new_elems.type)

modified_cost = EUnaryOp(
UOp.Sum,
EMap(
modified_elems,
modified_elems,
ELambda(
v,
v,
maintenance_cost(
new_v_key, solver, op, freebies)).with_type(INT)).with_type(INT)).with_type(INT_BAG)

Expand Down
4 changes: 2 additions & 2 deletions cozy/synthesis/high_level_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def run(self):
funcs=self.funcs)

cost_model = CostModel(
funcs=ctx.funcs(),
assumptions=EAll(self.assumptions),
funcs=ctx.funcs(),
assumptions=EAll(self.assumptions),
freebies=self.freebies,
ops=self.ops)

Expand Down
8 changes: 4 additions & 4 deletions tests/cost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_int_arith_state_vs_non_int(self):
state x : Int

op o()
x = x + 1;
x = x + 1;
"""
)
s1 = EBinOp(EVar("x").with_type(INT), "+", ONE)
Expand All @@ -272,7 +272,7 @@ def test_len_in_state_vs_recalc(self):
state xs : List<Int>

op o(xs : List <Int>, i : Int)
xs = xs[:i] + xs[i+1:];
xs = xs[:i] + xs[i+1:];
"""
)
xs1 = EVar("xs").with_type(TBag(INT))
Expand All @@ -285,7 +285,7 @@ def test_len_in_state_vs_recalc(self):
cost2 = cost_of(e2)
assert_cmp(e1, cost1, e2, cost2, Cost.BETTER)
assert_cmp(e1, cost1, e2, cost2, Cost.BETTER, ops=ops)

def test_state_list_vs_int(self):
ops = get_ops(
"""
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_filter_sliced_list(self):
state xs : List<Int>

op o(xs : List <Int>, i : Int)
xs = xs[:i] + xs[i+1:];
xs = xs[:i] + xs[i+1:];
"""
)
for op in ops:
Expand Down

0 comments on commit 6cad486

Please sign in to comment.