Пример #1
0
 def __rxor__(self, item):
     """``^`` is the ``cons`` operator (equivalent to ``:`` in Haskell)."""
     from hask3.lang.type_system import typeof
     from hask3.lang.hindley_milner import ListType, unify
     unify(self.__type__(), ListType(typeof(item)))
     if self.__is_evaluated:
         return List(head=[item] + self.__head)
     return List(head=[item] + self.__head, tail=self.__tail)
Пример #2
0
 def __next(self):
     """Evaluate the next element of the tail, and add it to the head."""
     from hask3.lang.type_system import typeof
     from hask3.lang.hindley_milner import unify
     if self.__is_evaluated:
         raise StopIteration
     else:
         try:
             next_iter = next(self.__tail)
             if len(self.__head) > 0:
                 unify(typeof(self[0]), typeof(next_iter))
             self.__head.append(next_iter)
         except StopIteration:
             self.__is_evaluated = True
Пример #3
0
    def __add__(self, other):
        """``(+) :: [a] -> [a] -> [a]``

        ``+`` is the list concatenation operator, equivalent to ``++`` in
        Haskell and + for Python lists

        """
        from itertools import chain
        from hask3.lang.type_system import typeof
        from hask3.lang.hindley_milner import unify
        unify(self.__type__(), typeof(other))
        if self.__is_evaluated and other.__is_evaluated:
            return List(head=self.__head + other.__head)
        elif self.__is_evaluated and not other.__is_evaluated:
            return List(head=self.__head + other.__head, tail=other.__tail)
        else:
            return List(head=self.__head, tail=chain(self.__tail, other))
Пример #4
0
 def __init__(self, head=None, tail=None):
     from itertools import chain
     from hask3.lang.type_system import typeof
     from hask3.lang.hindley_milner import unify
     if head is not None:
         count = len(head)
         if count > 0:
             fst = head[0]
             i = 1
             while i < count:
                 unify(typeof(fst), typeof(head[i]))
                 i += 1
         self.__head = list(head)
     else:
         self.__head = []
     self.__is_evaluated = tail is None
     self.__tail = chain([] if self.__is_evaluated else tail)
Пример #5
0
    def __call__(self, *args, **kwargs):
        # the environment contains the type of the function and the types
        # of the arguments
        from functools import partial
        from hask3.lang.hindley_milner import Var, App
        from hask3.lang.hindley_milner import unify
        # Using 'id' is an issue could produce errors.
        env = {id(self): self.fn_type}
        env.update({id(arg): typeof(arg) for arg in args})
        ap = Var(id(self))
        for arg in args:
            if isinstance(arg, Undefined):
                return arg
            ap = App(ap, Var(id(arg)))
        result_type = ap.analyze(env)

        if len(self.fn_args) - 1 == len(args):
            result = self.func(*args)
            unify(result_type, typeof(result))
            return result
        else:
            return TypedFunc(partial(self.func, *args, **kwargs),
                             self.fn_args[len(args):], result_type)
Пример #6
0
 def __contains__(self, x):
     from hask3.hack import isin
     from hask3.lang.type_system import typeof
     from hask3.lang.hindley_milner import ListType, unify
     unify(self.__type__(), ListType(typeof(x)))
     return isin(x, iter(self))
Пример #7
0
 def index(self, x):
     from hask3.lang.type_system import typeof
     from hask3.lang.hindley_milner import ListType, unify
     unify(self.__type__(), ListType(typeof(x)))
     self.__evaluate()
     return self.__head.index(x)
Пример #8
0
 def typecheck(self, expr, expr_type):
     """Typecheck succeeded using our toy environment"""
     self.assertIsNone(unify(expr.analyze(self.env), expr_type))
     return
Пример #9
0
 def unified(self, t1, t2):
     """Two types are able to be unified"""
     self.assertIsNone(unify(t1, t2))
     return