예제 #1
0
파일: _sum.py 프로젝트: cyrus-/typy
    def _normalize_user_idx(cls, provided_idx):
        if not isinstance(provided_idx, tuple):
            provided_idx = (provided_idx, )
        idx = _util.odict()
        for item in provided_idx:
            if isinstance(item, six.string_types):
                lbl, ty = (item, _product.unit)
            elif isinstance(item, slice):
                lbl, ty = item.start, item.stop
                if item.step is not None:
                    raise typy.TypeFormationError("Invalid variant: " +
                                                  str(lbl))
            else:
                raise typy.TypeFormationError("Invalid variant definition.")

            if (not isinstance(lbl, six.string_types)
                    or not typy._is_Name_constructor_id(lbl)):
                raise typy.TypeFormationError(
                    "Label '" + str(lbl) +
                    "' is not a non-empty string with initial capital.")
            if lbl in idx:
                raise typy.TypeFormationError("Duplicate label '" + lbl + "'.")
            if not isinstance(ty, typy.Type):
                raise typy.TypeFormationError("Payload for label '" + lbl +
                                              "' is not a type.")
            idx[lbl] = ty
        return idx
예제 #2
0
 def init_idx(cls, idx):
     if isinstance(idx, tuple):
         idx = cls._normalize_fn_idx(idx)
         (arg_types, return_type) = idx
         for arg_type in arg_types:
             if not isinstance(arg_type, typy.Type):
                 raise typy.TypeFormationError(
                     "Argument type is not a type.")
         if not isinstance(return_type, typy.Type):
             raise typy.TypeFormationError(
                 "Return type is not a type.")
     else:
         raise typy.TypeFormationError(
             "Function type index is not a pair.")
     return idx
예제 #3
0
파일: isorec.py 프로젝트: cyrus-/typy
 def unfolded(self):
     _unfolded = self._unfolded
     if _unfolded is None:
         self._unfolded = _unfolded = self.idx(self)
         if _unfolded is self:
             raise typy.TypeFormationError("Invalid unfolding.")
     return _unfolded
예제 #4
0
 def init_inc_idx(cls, inc_idx):
     if inc_idx == Ellipsis: 
         # if no index is provided, we will extract signature from the fn body
         pass
     elif isinstance(inc_idx, tuple):
         inc_idx = cls._normalize_fn_idx(inc_idx)
         (arg_types, return_type) = inc_idx
         for arg_type in arg_types:
             if not isinstance(arg_type, typy.Type):
                 raise typy.TypeFormationError(
                     "Argument type is not a type.")
         if return_type != Ellipsis:
             raise typy.TypeFormationError(
                 "Return type for an incomplete fn type must be elided.")
     else:
         raise typy.TypeFormationError(
             "Incomplete fn type index is not an ellipsis or pair.")
     return inc_idx
예제 #5
0
 def _normalize_fn_idx(idx):
     len_idx = len(idx)
     if len_idx < 2:
         raise typy.TypeFormationError(
             "Function type index missing argument and return types.")
     elif len_idx == 2:
         arg_types = idx[0]
         return_type = idx[1]
         if not isinstance(arg_types, tuple):
             if isinstance(arg_types, typy.Type):
                 idx = ((arg_types,), return_type)
             else: 
                 raise typy.TypeFormationError(
                     "Argument signature is not a tuple.") 
     elif len_idx > 2:
         arg_types = idx[0:-1]
         return_type = idx[-1]
         idx = (arg_types, return_type)
     return idx
예제 #6
0
def _normalize_tuple_idx(idx):
    if not isinstance(idx, tuple):
        idx = (idx, )
    used_labels = set()
    for i, component in enumerate(idx):
        if isinstance(component, typy.TyExpr):  # TODO KIND
            if i in used_labels:
                raise typy.TypeFormationError("Duplicate label: " + str(i))
            used_labels.add(i)
            yield (i, component)
            continue
        elif isinstance(component, slice):
            label = component.start
            ty = component.stop
            if component.step is not None:
                raise typy.TypeFormationError("Invalid tuple component: " +
                                              str(label))
        elif isinstance(component, tuple):
            if len(component) != 2:
                raise typy.TypeFormationError(
                    "Tuple component must have two components.")
            label = component[0]
            ty = component[1]
        else:
            raise typy.TypeFormationError("Invalid component definition: " +
                                          str(component))

        if isinstance(label, six.string_types):
            if len(label) == 0:
                raise typy.TypeFormationError(
                    "String label must be non-empty.")
        elif isinstance(label, (int, long)):
            if label < 0:
                raise typy.TypeFormationError(
                    "Integer label must be non-negative.")
        else:
            raise typy.TypeFormationError("Label must be a string or integer.")
        if label in used_labels:
            raise typy.TypeFormationError("Duplicate label: " + str(i))
        used_labels.add(label)

        if not isinstance(ty, typy.Type):
            raise typy.TypeFormationError("Component labeled " + label +
                                          " has invalid type specification.")

        yield (label, ty)
예제 #7
0
 def init_inc_idx(cls, inc_idx):
     if inc_idx != () and inc_idx != Ellipsis:
         raise typy.TypeFormationError(
             "Incomplete index of cplx_ type must be () or Ellipsis.")
     return inc_idx
예제 #8
0
 def init_idx(cls, idx):
     if idx != ():
         raise typy.TypeFormationError("Index of cplx_ type must be ().")
     return idx
예제 #9
0
 def init_inc_idx(cls, inc_idx):
     if inc_idx == Ellipsis:
         return inc_idx
     else:
         raise typy.TypeFormationError(
             "Incomplete tuple type must have Ellipsis index.")
예제 #10
0
파일: _sum.py 프로젝트: cyrus-/typy
 def init_inc_idx(cls, inc_idx):
     raise typy.TypeFormationError("finsum types cannot be incomplete.")
예제 #11
0
파일: isorec.py 프로젝트: cyrus-/typy
 def init_inc_idx(cls, inc_idx):
     raise typy.TypeFormationError(
         "Cannot construct an incomplete rec type.")
예제 #12
0
파일: isorec.py 프로젝트: cyrus-/typy
 def init_idx(cls, idx):
     if not isinstance(idx, _py_fn_class):
         raise typy.TypeFormationError(
             "Index of rec must be a (pure, total) Python function.")
     return idx