示例#1
0
文件: coretypes.py 项目: WuCPMark/TFJ
def quantify_type(t, bound=None, quantified=None):
    """
    If t is a type containing free type variables not occuring in bound,
    then this function will return a Polytype quantified over those
    variables.  Otherwise, it returns t itself.

    If an optional quantified dictionary is provided, that dictionary
    will be used to map free variables to quantifiers.  Any free
    variables not found in that dictionary will be mapped to fresh
    quantifiers, and the dictionary will be augmented with these new
    mappings.
    """

    if bound is None:
        free = list(free_in_type(t))
    else:
        free = list(ifilter(lambda t: t not in bound, free_in_type(t)))

    if not free:
        return t
    else:
        supply = name_supply()
        if quantified is None: quantified = dict()

        for v in free:
            if v not in quantified:
                quantified[v] = supply.next()

        quantifiers = sorted([quantified[v] for v in set(free)])
        return Polytype(quantifiers, substituted_type(t, quantified))
示例#2
0
 def _Procedure(self, proc):
     names = pltools.name_supply(stems=['tuple'], drop_zero=False)
     disassembly = []
     def make_name(arg):
         if not isinstance(arg, S.Tuple):
             return arg
         else:
             made_name = S.Name(names.next())
             assembled = S.Bind(S.Tuple(*[make_name(x) for x in arg]),
                                made_name)
             disassembly.insert(0, assembled)
             return made_name
     new_variables = map(make_name, proc.formals())
     return S.Procedure(proc.name(), new_variables, disassembly + proc.parameters)
示例#3
0
文件: inference.py 项目: WuCPMark/TFJ
    def __init__(self, 
                 typings=None,       # typings of bound identifiers
                 environment=None,   # mapping of type names to types
                 tvsupply=None,
                 globals=None):

        # Initialize environment with empty mappings if none provided
        self.typings = typings or Environment()
        self.environment = environment or Environment()

        # Since Copperhead is embedded within Python, we need
        # visibility into a Python procedures namespace to resolve what
        # it's identifiers are referring to.  This is where we record
        # that global namespace (if provided).  It should never be modified.
        self.globals = globals or dict()

        # Supply of unique type variable names: #tv0, #tv1, #tv2, ...
        # They're known to be unique because they are illegal identifiers.
        self._tvsupply = tvsupply or name_supply(['#tv'], drop_zero=False)
示例#4
0
    def __init__(self,
                 globals=None,
                 tvsupply=None):

        # Record the global Python namespace (if any) in which code is
        # defined.  This maps identifiers to values; by convention
        # Copperhead objects have a 'cu_type' slot that we will
        # use for typing information.
        #
        # This dictionary should never be modified.
        self.globals = globals or dict()

        # Supply of unique type variable names: #tv0, #tv1, #tv2, ...
        # They're known to be unique because they are illegal identifiers.
        self._tvsupply = tvsupply or name_supply(['#tv'], drop_zero=False)

        # The typings environment maps local identifiers to their
        # corresponding types.
        self.typings = Environment()

        # Type variables associated with formal parameters, both in
        # lambdas and procedures, are required to be monomorphic.  This
        # set records all introduced type variables that are known to be
        # monomorphic.  Since we make all internal variables unique, we
        # only need a single set for this, rather than a hierarchical
        # environment structure as with self.typings.
        self.monomorphs = set()

        # During inference, we accumulate a set of freely occurring
        # identifiers.  This set contains AST nodes, rather than names.
        # Thus, multiple occurrences of a given name (e.g., 'x') may be
        # found in this set if they occurred at separate source
        # locations.  For example, the expression 'x+x' will introduce
        # two free occurrences, not one.
        self.free_occurrences = set()

        # The inference system builds up a set of assumptions about the
        # types of identifiers occurring outside the current compilation
        # unit and which have no known 'cu_type' attribute.
        # This table provides a mapping from type variables created for
        # free occurrences to the AST node at which they occur.
        self.assumptions = dict()
示例#5
0
    def __init__(self, globals=None, tvsupply=None):

        # Record the global Python namespace (if any) in which code is
        # defined.  This maps identifiers to values; by convention
        # Copperhead objects have a 'cu_type' slot that we will
        # use for typing information.
        #
        # This dictionary should never be modified.
        self.globals = globals or dict()

        # Supply of unique type variable names: #tv0, #tv1, #tv2, ...
        # They're known to be unique because they are illegal identifiers.
        self._tvsupply = tvsupply or name_supply(['#tv'], drop_zero=False)

        # The typings environment maps local identifiers to their
        # corresponding types.
        self.typings = Environment()

        # Type variables associated with formal parameters, both in
        # lambdas and procedures, are required to be monomorphic.  This
        # set records all introduced type variables that are known to be
        # monomorphic.  Since we make all internal variables unique, we
        # only need a single set for this, rather than a hierarchical
        # environment structure as with self.typings.
        self.monomorphs = set()

        # During inference, we accumulate a set of freely occurring
        # identifiers.  This set contains AST nodes, rather than names.
        # Thus, multiple occurrences of a given name (e.g., 'x') may be
        # found in this set if they occurred at separate source
        # locations.  For example, the expression 'x+x' will introduce
        # two free occurrences, not one.
        self.free_occurrences = set()

        # The inference system builds up a set of assumptions about the
        # types of identifiers occurring outside the current compilation
        # unit and which have no known 'cu_type' attribute.
        # This table provides a mapping from type variables created for
        # free occurrences to the AST node at which they occur.
        self.assumptions = dict()
示例#6
0
 def __init__(self):
     self.procedures = {}
     self.name_supply = pltools.name_supply(stems=['_'], drop_zero=False)
示例#7
0
 def __init__(self):
     self.stmts = [list()]
     self.names = pltools.name_supply(stems=['e'], drop_zero=False)
示例#8
0
 def __init__(self):
     # Collect lifted Lambdas as Procedures
     self.proclist = []
     self.names = pltools.name_supply(stems=['_lambda'], drop_zero=False)
示例#9
0
 def __init__(self):
     self.stmts = [list()]
     self.names = pltools.name_supply(stems=['e'], drop_zero=False)
示例#10
0
 def __init__(self):
     # Collect lifted Lambdas as Procedures 
     self.proclist = []
     self.names = pltools.name_supply(stems=['_lambda'], drop_zero=False)
示例#11
0
    name = assign.value().function().id
    return intrinsic_call(name, assign)

boilerplateStatements = [assign(declaration(C.tileIndexType, C.tileIndex), C.threadIdx),
               assign(declaration(C.tileIdType, C.tileId), C.blockIdx),
               convert(assign(declaration(C.tileBeginType, C.tileBegin), S.Apply(S.Name('op_mul'), [C.tileId, C.blockDim]))),
               convert(assign(declaration(C.globalIndexType, C.globalIndex), S.Apply(S.Name('op_add'), [C.tileBegin, C.tileIndex])))]


boilerplate = [B.CCommentBlock('Copperhead boilerplate', 'Generally useful indices.', boilerplateStatements)]

syncthreads = S.Apply(S.Name('__syncthreads'), [])

headers = [B.CInclude('copperhead.h')]

intrinsic_name_supply = P.name_supply()


def _len(bind, typings, write_out):
    app = bind.value()
    sequence = app.parameters[1]
    size = B.CMember(sequence, S.Apply(S.Name("size"), []))
    bind.parameters[0] = size
    return bind

def _range(bind, typings, write_out):
    typings[bind.binder().id] = T.Monotype("index_sequence")
    bound = apply.parameters[1]
    bind.parameters[0] = S.Apply(S.Name("index_sequence"), [bound])
    return bind
    
示例#12
0
 def __init__(self):
     self.procedures = {}
     self.name_supply = pltools.name_supply(stems=['_'], drop_zero=False)