def make_wrap_type(wrapinv, opertype): """Given an operand type, determine the corresponding wrap or unwrap type. """ if wrapinv.unwrap: top_opertype = T.Set(T.Tuple([T.Top])) bottom_opertype = T.Set(T.Tuple([T.Bottom])) norm_type = opertype.join(bottom_opertype) well_typed = norm_type.issmaller(top_opertype) if well_typed: assert (isinstance(norm_type, T.Set) and isinstance(norm_type.elt, T.Tuple) and len(norm_type.elt.elts) == 1) return T.Set(norm_type.elt.elts[0]) else: return T.Set(T.Top) else: top_opertype = T.Set(T.Top) bottom_opertype = T.Set(T.Bottom) norm_type = opertype.join(bottom_opertype) well_typed = norm_type.issmaller(top_opertype) if well_typed: assert isinstance(norm_type, T.Set) return T.Set(T.Tuple([norm_type.elt])) else: return T.Set(T.Top)
def make_setfrommap_type(mask, maptype): """Given a mask and a map type, determine the corresponding relation type. We obtain by lattice join the smallest map type that is at least as big as the given map type and that has the correct key tuple arity. This should have the form {(K1, ..., Kn): V}. The relation type is then a set of tuples of these types interleaved according to the mask. If no such type exists, e.g. if the given relation type is {Top: Top} or the key is not a tuple of correct arity, we instead give the relation type {Top}. """ nb = mask.m.count('b') assert mask.m.count('u') == 1 bottom_maptype = T.Map(T.Tuple([T.Bottom] * nb), T.Bottom) top_maptype = T.Map(T.Tuple([T.Top] * nb), T.Top) norm_type = maptype.join(bottom_maptype) well_typed = norm_type.issmaller(top_maptype) if well_typed: assert (isinstance(norm_type, T.Map) and isinstance(norm_type.key, T.Tuple) and len(norm_type.key.elts) == nb) t_elts = L.combine_by_mask(mask, norm_type.key.elts, [norm_type.value]) rel_type = T.Set(T.Tuple(t_elts)) else: rel_type = T.Set(T.Top) return rel_type
def make_auxmap_type(auxmapinv, reltype): """Given a mask and a relation type, determine the corresponding auxiliary map type. We obtain by lattice join the smallest relation type that is at least as big as the given relation type and that has the correct arity. This should have the form {(T1, ..., Tn)}. The map type is then from a tuple of some Ts to a set of tuples of the remaining Ts. If no such type exists, e.g. if the given relation type is {Top} or a set of tuples of incorrect arity, we instead give the map type {Top: Top}. """ mask = auxmapinv.mask arity = len(mask.m) bottom_reltype = T.Set(T.Tuple([T.Bottom] * arity)) top_reltype = T.Set(T.Tuple([T.Top] * arity)) norm_type = reltype.join(bottom_reltype) well_typed = norm_type.issmaller(top_reltype) if well_typed: assert (isinstance(norm_type, T.Set) and isinstance(norm_type.elt, T.Tuple) and len(norm_type.elt.elts) == arity) t_bs, t_us = L.split_by_mask(mask, norm_type.elt.elts) t_key = t_bs[0] if auxmapinv.unwrap_key else T.Tuple(t_bs) t_value = t_us[0] if auxmapinv.unwrap_value else T.Tuple(t_us) map_type = T.Map(t_key, T.Set(t_value)) else: map_type = T.Map(T.Top, T.Top) return map_type
def rewrite_comp(self, symbol, name, comp): # No effect if it's already a tuple. if isinstance(comp.resexp, L.Tuple): return affected_queries.add(name) comp = comp._replace(resexp=L.Tuple([comp.resexp])) t = symbol.type t = t.join(T.Set(T.Bottom)) assert t.issmaller(T.Set(T.Top)) symbol.type = T.Set(T.Tuple([t.elt])) return comp
def visit_IndefImgset(self, cost): # Check for constant-time relations. if cost.rel in const_rels: return Unit() # Field lookups are constant time. if N.is_F(cost.rel) and cost.mask == L.mask('bu'): return Unit() sym = symtab.get_symbols().get(cost.rel, None) if sym is None: return cost # Get types for unbound components. t = sym.type if t is None: return cost if not (isinstance(t, T.Set) and isinstance(t.elt, T.Tuple) and len(t.elt.elts) == len(cost.mask.m)): return cost mask = cost.mask elts = t.elt.elts # Process out aggregate SetFromMap result components, # which are functionally determined by the map keys. if N.is_SA(cost.rel) and mask.m[-1] == 'u': mask = mask._replace(m=mask.m[:-1]) elts = elts[:-1] _b_elts, u_elts = L.split_by_mask(mask, elts) new_cost = type_to_cost(T.Tuple(u_elts)) new_cost = normalize(new_cost) if not isinstance(new_cost, Unknown): cost = new_cost return cost
def incrementalize_aggr(tree, symtab, query, result_var): # Form the invariant. aggrinv = aggrinv_from_query(symtab, query, result_var) handler = aggrinv.get_handler() # Transform to maintain it. trans = AggrMaintainer(symtab.fresh_names.vars, aggrinv) tree = trans.process(tree) symtab.maint_funcs.update(trans.maint_funcs) # Transform occurrences of the aggregate. zero = None if aggrinv.uses_demand else handler.make_zero_expr() state_expr = L.DictLookup(L.Name(aggrinv.map), L.tuplify(aggrinv.params), zero) lookup_expr = handler.make_projection_expr(state_expr) class AggrExpander(S.QueryRewriter): expand = True def rewrite_aggr(self, symbol, name, expr): if name == query.name: return lookup_expr tree = AggrExpander.run(tree, symtab) # Determine the result map's type and define its symbol. t_rel = get_rel_type(symtab, aggrinv.rel) btypes, _ = L.split_by_mask(aggrinv.mask, t_rel.elt.elts) t_key = T.Tuple(btypes) t_val = handler.result_type(t_rel) t_map = T.Map(t_key, t_val) symtab.define_map(aggrinv.map, type=t_map) symtab.stats['aggrs_transformed'] += 1 return tree