Skip to content

Commit

Permalink
Changed ALL .t to .elem_type
Browse files Browse the repository at this point in the history
- This addresses #45. I will also try to do the rest of the issue too.
  • Loading branch information
anhnamtran authored and Calvin-L committed Aug 3, 2018
1 parent abded6d commit 35a267d
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 173 deletions.
60 changes: 30 additions & 30 deletions cozy/codegen/cxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def visit_TBool(self, t, name):
return "bool {}".format(name)

def visit_TRef(self, t, name):
return self.visit(t.t, "&{}".format(name))
return self.visit(t.elem_type, "&{}".format(name))

def visit_THandle(self, t, name):
return "{} *{}".format(self.typename(t), name)
Expand Down Expand Up @@ -93,13 +93,13 @@ def visit_TSet(self, t, name):
return self.visit_TNativeSet(t, name)

def visit_TNativeList(self, t, name):
return "std::vector< {} > {}".format(self.visit(t.t, ""), name)
return "std::vector< {} > {}".format(self.visit(t.elem_type, ""), name)

def visit_TNativeSet(self, t, name):
return "std::unordered_set< {}, {} > {}".format(self.visit(t.t, ""), self._hasher(t.t), name)
return "std::unordered_set< {}, {} > {}".format(self.visit(t.elem_type, ""), self._hasher(t.elem_type), name)

def visit_TArray(self, t, name):
return "std::vector< {} > {}".format(self.visit(t.t, ""), name)
return "std::vector< {} > {}".format(self.visit(t.elem_type, ""), name)

def visit_Type(self, t, name):
h = extension_handler(type(t))
Expand All @@ -124,7 +124,7 @@ def visit_ELet(self, e):
return self.visit(e.f.apply_to(v))

def visit_TVector(self, t, name):
return "{}[{}]".format(self.visit(t.t, name), t.n)
return "{}[{}]".format(self.visit(t.elem_type, name), t.n)

def visit_EVectorGet(self, e):
v = self.visit(e.e)
Expand Down Expand Up @@ -177,7 +177,7 @@ def visit_Query(self, q):
ret_exp = q.ret
ret_type = ret_exp.type
if is_collection(ret_type):
x = self.fv(ret_type.t, "x")
x = self.fv(ret_type.elem_type, "x")
if q.docstring:
self.write(indent_lines(q.docstring, self.get_indent()), "\n")
self.begin_statement()
Expand Down Expand Up @@ -255,14 +255,14 @@ def construct_concrete(self, t : Type, e : Exp, out : Exp):
return t.construct_concrete(e, out)
elif isinstance(t, TBag) or isinstance(t, TList):
assert out not in free_vars(e)
x = self.fv(t.t, "x")
x = self.fv(t.elem_type, "x")
return SSeq(
self.initialize_native_list(out),
SForEach(x, e, SCall(out, "add", [x])))
elif isinstance(t, TSet):
if isinstance(e, EUnaryOp) and e.op == UOp.Distinct:
return self.construct_concrete(t, e.e, out)
x = self.fv(t.t, "x")
x = self.fv(t.elem_type, "x")
return SSeq(
self.initialize_native_set(out),
SForEach(x, e, SCall(out, "add", [x])))
Expand Down Expand Up @@ -305,7 +305,7 @@ def initialize_native_map(self, e) -> Stm:

# def visit_EListGet(self, e, indent):
# assert type(e.e.type) is TList
# return self.visit(EEscape("{l}[{i}]", ["l", "i"], [e.e, e.index]).with_type(e.e.type.t))
# return self.visit(EEscape("{l}[{i}]", ["l", "i"], [e.e, e.index]).with_type(e.e.type.elem_type))

def visit_ENative(self, e):
assert e.e == ENum(0)
Expand Down Expand Up @@ -451,9 +451,9 @@ def visit_EBoolToInt(self, e):
# return True

def histogram(self, e) -> EVar:
t = TMap(e.type.t, INT)
t = TMap(e.type.elem_type, INT)
hist = self.fv(t, "hist")
x = self.fv(e.type.t)
x = self.fv(e.type.elem_type)
val = self.fv(INT, "val")
self.declare(hist)
self.visit(self.initialize_native_map(hist))
Expand Down Expand Up @@ -483,7 +483,7 @@ def visit_EBinOp(self, e):
elif op == "-" and is_collection(e.type):
t = e.type
v = self.fv(t, "v")
x = self.fv(t.t, "x")
x = self.fv(t.elem_type, "x")
self.declare(v, e.e1)
self.visit(SForEach(x, e.e2, SCall(v, "remove", [x])))
return v.id
Expand Down Expand Up @@ -521,7 +521,7 @@ def for_each(self, iterable : Exp, body) -> str:
elif isinstance(iterable, ESingleton):
return self.visit(SScoped(body(iterable.e)))
elif isinstance(iterable, ECond):
v = self.fv(iterable.type.t, "v")
v = self.fv(iterable.type.elem_type, "v")
new_body = body(v)
assert isinstance(new_body, Stm)
return self.visit(SIf(iterable.cond,
Expand All @@ -532,7 +532,7 @@ def for_each(self, iterable : Exp, body) -> str:
iterable.e,
lambda v: body(iterable.f.apply_to(v)))
elif isinstance(iterable, EUnaryOp) and iterable.op == UOp.Distinct:
tmp = self.fv(TSet(iterable.type.t), "tmp")
tmp = self.fv(TSet(iterable.type.elem_type), "tmp")
self.declare(tmp)
self.visit(self.initialize_native_set(tmp))
self.for_each(iterable.e, lambda x: SIf(
Expand All @@ -545,11 +545,11 @@ def for_each(self, iterable : Exp, body) -> str:
self.for_each(iterable.e1, body)
self.for_each(iterable.e2, body)
elif isinstance(iterable, EBinOp) and iterable.op == "-":
t = TList(iterable.type.t)
t = TList(iterable.type.elem_type)
e = self.visit(EBinOp(iterable.e1, "-", iterable.e2).with_type(t))
return self.for_each(EEscape(e, (), ()).with_type(t), body)
elif isinstance(iterable, EFlatMap):
v = self.fv(iterable.type.t)
v = self.fv(iterable.type.elem_type)
new_body = body(v)
assert isinstance(new_body, Stm)
return self.for_each(iterable.e,
Expand All @@ -561,7 +561,7 @@ def for_each(self, iterable : Exp, body) -> str:
self.declare(s, iterable.start)
self.declare(e, iterable.end)
return self.visit(SWhile(ELt(s, e), SSeq(
body(EEscape("{l}[{i}]", ("l", "i"), (iterable.e, s)).with_type(iterable.type.t)),
body(EEscape("{l}[{i}]", ("l", "i"), (iterable.e, s)).with_type(iterable.type.elem_type)),
SAssign(s, EBinOp(s, "+", ONE).with_type(INT)))))
elif isinstance(iterable, ECall) and iterable.func in self.queries:
q = self.queries[iterable.func]
Expand All @@ -570,7 +570,7 @@ def for_each(self, iterable : Exp, body) -> str:
return self.for_each(iterable.f.apply_to(iterable.e), body)
else:
assert is_collection(iterable.type), repr(iterable)
x = self.fv(iterable.type.t, "x")
x = self.fv(iterable.type.elem_type, "x")
return self.for_each_native(x, iterable, body(x))

def for_each_native(self, x : EVar, iterable, body):
Expand Down Expand Up @@ -599,9 +599,9 @@ def visit_SForEach(self, for_each):
self.for_each(iter, lambda x: SSeq(SDecl(id.id, x), body))

def find_one(self, iterable):
v = self.fv(iterable.type.t, "v")
v = self.fv(iterable.type.elem_type, "v")
label = fresh_name("label")
x = self.fv(iterable.type.t, "x")
x = self.fv(iterable.type.elem_type, "x")
decl = SDecl(v.id, evaluation.construct_value(v.type))
find = SEscapableBlock(label,
SForEach(x, iterable, seq([
Expand All @@ -617,9 +617,9 @@ def min_or_max(self, op, e, f):
EBinOp(f.apply_to(e.e1.e), op, f.apply_to(e.e2.e)).with_type(BOOL),
e.e1.e,
e.e2.e).with_type(e.e1.e.type))
out = self.fv(e.type.t, "min" if op == "<" else "max")
out = self.fv(e.type.elem_type, "min" if op == "<" else "max")
first = self.fv(BOOL, "first")
x = self.fv(e.type.t, "x")
x = self.fv(e.type.elem_type, "x")
decl1 = SDecl(out.id, evaluation.construct_value(out.type))
decl2 = SDecl(first.id, T)
find = SForEach(x, e,
Expand Down Expand Up @@ -656,27 +656,27 @@ def visit_EUnaryOp(self, e):
if op == UOp.The:
return self.find_one(e.e)
elif op == UOp.Sum:
type = e.e.type.t
type = e.e.type.elem_type
res = self.fv(type, "sum")
x = self.fv(type, "x")
self.visit(seq([
SDecl(res.id, ENum(0).with_type(type)),
SForEach(x, e.e, SAssign(res, EBinOp(res, "+", x).with_type(type)))]))
return res.id
elif op == UOp.Length:
arg = EVar("x").with_type(e.e.type.t)
arg = EVar("x").with_type(e.e.type.elem_type)
return self.visit(EUnaryOp(UOp.Sum, EMap(e.e, ELambda(arg, ONE)).with_type(INT_BAG)).with_type(INT))
elif op == UOp.All:
arg = EVar("x").with_type(e.e.type.t)
arg = EVar("x").with_type(e.e.type.elem_type)
return self.visit(EUnaryOp(UOp.Empty, EFilter(e.e, ELambda(arg, ENot(arg))).with_type(INT_BAG)).with_type(INT))
elif op == UOp.Any:
arg = EVar("x").with_type(e.e.type.t)
arg = EVar("x").with_type(e.e.type.elem_type)
return self.visit(EUnaryOp(UOp.Exists, EFilter(e.e, ELambda(arg, arg)).with_type(INT_BAG)).with_type(INT))
elif op == UOp.Empty:
iterable = e.e
v = self.fv(BOOL, "v")
label = fresh_name("label")
x = self.fv(iterable.type.t, "x")
x = self.fv(iterable.type.elem_type, "x")
decl = SDecl(v.id, T)
find = SEscapableBlock(label,
SForEach(x, iterable, seq([
Expand All @@ -695,9 +695,9 @@ def visit_EUnaryOp(self, e):
self.declare(v, e)
return v.id
elif op == UOp.AreUnique:
s = self.fv(TSet(e.e.type.t), "unique_elems")
s = self.fv(TSet(e.e.type.elem_type), "unique_elems")
u = self.fv(BOOL, "is_unique")
x = self.fv(e.e.type.t)
x = self.fv(e.e.type.elem_type)
label = fresh_name("label")
self.visit(seq([
SDecl(s.id, EEmptyList().with_type(s.type)),
Expand Down Expand Up @@ -787,7 +787,7 @@ def visit_SNoOp(self, s):
# def copy_to(self, lhs, rhs):
# if isinstance(lhs.type, TBag):
# cl, el = self.visit(lhs, indent)
# x = self.fv(lhs.type.t, "x")
# x = self.fv(lhs.type.elem_type, "x")
# # TODO: hacky use of EVar ahead! We need an EEscape, like SEscape
# return cl + self.visit(SForEach(x, rhs, SCall(EVar(el).with_type(lhs.type), "add", [x])), indent=indent)
# else:
Expand Down
36 changes: 18 additions & 18 deletions cozy/codegen/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def visit_Query(self, q):
return ""
ret_type = q.ret.type
if isinstance(ret_type, TBag):
x = EVar(self.fn("x")).with_type(ret_type.t)
x = EVar(self.fn("x")).with_type(ret_type.elem_type)
def body(x):
return SEscape("{indent}_callback.accept({x});\n", ["x"], [x])
if q.docstring:
self.write(indent_lines(q.docstring, self.get_indent()), "\n")
self.begin_statement()
self.write("public ", self.visit(ret_type, q.name), "(")
self.visit_args(itertools.chain(q.args, [("_callback", TNative("java.util.function.Consumer<{t}>".format(t=self.visit(ret_type.t, ""))))]))
self.visit_args(itertools.chain(q.args, [("_callback", TNative("java.util.function.Consumer<{t}>".format(t=self.visit(ret_type.elem_type, ""))))]))
self.write(") ")
with self.block():
self.visit(SForEach(x, q.ret, SEscape("{indent}_callback({x});\n", ["x"], [x])))
Expand Down Expand Up @@ -444,19 +444,19 @@ def visit_TString(self, t, name):
return "String {}".format(name)

def visit_TNativeList(self, t, name):
if self.boxed or not self.is_primitive(t.t):
return "java.util.ArrayList<{}> {}".format(self.visit(t.t, ""), name)
if self.boxed or not self.is_primitive(t.elem_type):
return "java.util.ArrayList<{}> {}".format(self.visit(t.elem_type, ""), name)
else:
return "gnu.trove.list.array.T{}ArrayList {}".format(
self.trovename(t.t),
return "gnu.trove.list.array.elem_type{}ArrayList {}".format(
self.trovename(t.elem_type),
name)

def visit_TNativeSet(self, t, name):
if self.boxed or not self.is_primitive(t.t):
return "java.util.HashSet< {} > {}".format(self.visit(t.t, ""), name)
if self.boxed or not self.is_primitive(t.elem_type):
return "java.util.HashSet< {} > {}".format(self.visit(t.elem_type, ""), name)
else:
return "gnu.trove.set.hash.T{}HashSet {}".format(
self.trovename(t.t),
return "gnu.trove.set.hash.elem_type{}HashSet {}".format(
self.trovename(t.elem_type),
name)

def visit_TBag(self, t, name):
Expand Down Expand Up @@ -501,17 +501,17 @@ def find_one_native(self, iterable):
"({it}.hasNext() ? {it}.next() : null)".format(it=it))

def visit_TVector(self, t, name):
return "{}[] {}".format(self.visit(t.t, ""), name)
return "{}[] {}".format(self.visit(t.elem_type, ""), name)

def use_trove(self, t):
if isinstance(t, TMap):
return not (self.boxed or (not self.is_primitive(t.k) and not self.is_primitive(t.v)))
return False

def visit_TArray(self, t, name):
if t.t == BOOL:
if t.elem_type == BOOL:
return "long[] {}".format(name)
return "{}[] {}".format(self.visit(t.t, name=""), name)
return "{}[] {}".format(self.visit(t.elem_type, name=""), name)

def visit_TNativeMap(self, t, name):
if self.use_trove(t):
Expand All @@ -520,7 +520,7 @@ def visit_TNativeMap(self, t, name):
x = self.troveargs(x)
if x is not None:
args.append(x)
return "gnu.trove.map.hash.T{k}{v}HashMap{targs} {name}".format(
return "gnu.trove.map.hash.elem_type{k}{v}HashMap{targs} {name}".format(
k=self.trovename(t.k),
v=self.trovename(t.v),
targs="<{}>".format(", ".join(args)) if args else "",
Expand All @@ -532,13 +532,13 @@ def visit_TNativeMap(self, t, name):
name)

def visit_TRef(self, t, name):
return self.visit(t.t, name)
return self.visit(t.elem_type, name)

def for_each_native(self, x, iterable, body):
if not self.boxed and self.troveargs(x.type) is None:
setup, iterable_src = self.visit(iterable)
itname = fresh_name("iterator")
return "{setup}{indent}gnu.trove.iterator.T{T}Iterator {it} = {iterable}.iterator();\n{indent}while ({it}.hasNext()) {{\n{indent2}{decl} = {it}.next();\n{body}{indent}}}\n".format(
return "{setup}{indent}gnu.trove.iterator.elem_type{T}Iterator {it} = {iterable}.iterator();\n{indent}while ({it}.hasNext()) {{\n{indent2}{decl} = {it}.next();\n{body}{indent}}}\n".format(
setup=setup,
iterable=iterable_src,
it=itname,
Expand Down Expand Up @@ -587,15 +587,15 @@ def visit_SMapDel(self, update):
def visit_SArrayAlloc(self, s):
lval = self.visit(s.a)
cap = self.visit(s.capacity)
elem_type = s.a.type.t
elem_type = s.a.type.elem_type
tname = self.strip_generics(self.visit(elem_type, name=""))
self.write_stmt(lval, " = new ", tname, "[", cap, "];")

def visit_SEnsureCapacity(self, s):
return self.array_resize_for_index(s.a.type.t, s.a, s.capacity)

def visit_SArrayReAlloc(self, s):
return self.array_resize_for_index(s.a.type.t, s.a, s.new_capacity)
return self.array_resize_for_index(s.a.type.elem_type, s.a, s.new_capacity)

def visit_EArrayIndexOf(self, e):
# TODO: faster implementation that does not involve this intermediate list?
Expand Down
2 changes: 1 addition & 1 deletion cozy/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def __init__(self, parent : Context, v : EVar, bag : Exp, bag_pool : Pool):
`v` must not already be described by the parent context.
"""
assert v.type == bag.type.t
assert v.type == bag.type.elem_type
assert parent.legal_for(free_vars(bag)), "cannot create context for {} in {}, {}".format(v.id, pprint(bag), parent)
assert not any(v == vv for vv, p in parent.vars()), "binder {} already free in {}".format(v.id, parent)
self._parent = parent
Expand Down
2 changes: 1 addition & 1 deletion cozy/cost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def storage_size(e, freebies : [Exp] = []):
elif isinstance(e.type, TRecord):
return ESum([storage_size(EGetField(e, f).with_type(t)) for (f, t) in e.type.fields])
elif is_collection(e.type):
v = fresh_var(e.type.t, omit=free_vars(e))
v = fresh_var(e.type.elem_type, omit=free_vars(e))
return ESum([
FOUR,
EUnaryOp(UOp.Sum, EMap(e, ELambda(v, storage_size(v))).with_type(INT_BAG)).with_type(INT)])
Expand Down
4 changes: 2 additions & 2 deletions cozy/desugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def visit_clauses(self, clauses, final, res_type, i=0):
clause = clauses[i]
if isinstance(clause, CPull):
bag = clause.e
arg = EVar(clause.id).with_type(bag.type.t)
arg = EVar(clause.id).with_type(bag.type.elem_type)
rest, guards, pulls = self.visit_clauses(clauses, final, res_type, i + 1)
if guards:
guard = guards[0]
Expand Down Expand Up @@ -79,7 +79,7 @@ def convert_sets_to_bags(spec : Spec) -> Spec:
for i in range(len(spec.statevars)):
v, t = spec.statevars[i]
if isinstance(t, TSet):
t = TBag(t.t)
t = TBag(t.elem_type)
spec.statevars[i] = (v, t)
v = EVar(v).with_type(t)
spec.assumptions.append(EUnaryOp(UOp.AreUnique, v).with_type(BOOL))
Expand Down
10 changes: 5 additions & 5 deletions cozy/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _uneval(t, value):
elif is_collection(t):
e = EEmptyList().with_type(t)
for x in value:
e = EBinOp(e, "+", ESingleton(uneval(t.t, x)).with_type(t)).with_type(t)
e = EBinOp(e, "+", ESingleton(uneval(t.elem_type, x)).with_type(t)).with_type(t)
return e
elif isinstance(t, TString):
return EStr(value).with_type(t)
Expand Down Expand Up @@ -517,9 +517,9 @@ def make_native(stk):
elif e.op == UOp.Length:
out.append(unaryop_len)
elif e.op == UOp.AreUnique:
out.append(unaryop_areunique(e.e.type.t))
out.append(unaryop_areunique(e.e.type.elem_type))
elif e.op == UOp.Distinct:
out.append(unaryop_distinct(e.e.type.t))
out.append(unaryop_distinct(e.e.type.elem_type))
elif e.op == UOp.The:
out.append(unaryop_the(default=mkval(e.type)))
elif e.op == UOp.Reversed:
Expand Down Expand Up @@ -547,9 +547,9 @@ def make_native(stk):
out.append(binaryop_mul)
elif e.op == "-":
if isinstance(e.type, TBag) or isinstance(e.type, TSet):
out.append(binaryop_sub_bags(e.type.t))
out.append(binaryop_sub_bags(e.type.elem_type))
elif isinstance(e.type, TList):
out.append(binaryop_sub_lists(e.type.t))
out.append(binaryop_sub_lists(e.type.elem_type))
else:
out.append(binaryop_sub)
elif e.op == "==":
Expand Down
Loading

0 comments on commit 35a267d

Please sign in to comment.