Пример #1
0
 def type(self, val):
     "Get the Python type of a Scheme value."
     if expressions.isCompoundProcedure(val):
         return schemepy.types.Lambda
     if expressions.isPrimitiveProcedure(val):
         fun = expressions.primitiveImplementation(val)
         orig = fun.__dict__.get("_orig", None)
         if orig and callable(orig):
             return types.FunctionType
         return schemepy.types.Lambda
     if symbol.isSymbol(val):
         if val == symbol.true or val == symbol.false:
             return bool
         return schemepy.types.Symbol
     if pair.isNull(val):
         return list
     if isAList(val):
         return dict
     if pair.isList(val):
         return list
     if pair.isPair(val):
         return schemepy.types.Cons
     t = type(val)
     if t not in (int, complex, float, long, str, unicode):
         return object
     return t
Пример #2
0
 def type(self, val):
     "Get the Python type of a Scheme value."
     if expressions.isCompoundProcedure(val):
         return schemepy.types.Lambda
     if expressions.isPrimitiveProcedure(val):
         fun = expressions.primitiveImplementation(val)
         orig = fun.__dict__.get("_orig", None)
         if orig and callable(orig):
             return types.FunctionType
         return schemepy.types.Lambda
     if symbol.isSymbol(val):
         if val == symbol.true or val == symbol.false:
             return bool
         return schemepy.types.Symbol
     if pair.isNull(val):
         return list
     if isAList(val):
         return dict
     if pair.isList(val):
         return list
     if pair.isPair(val):
         return schemepy.types.Cons
     t = type(val)
     if t not in (int, complex, float, long, str, unicode):
         return object
     return t
Пример #3
0
 def __init__(self, bv, body, env):
     super(Closure, self).__init__()
     if not isSymbol(bv):
         raise AttributeError('1st arg in Closure must be the Symbol class')
     self.bv = bv
     self.body = body
     self.env = env
Пример #4
0
def find_var_in_env(var, envs):
    for env in envs:
        if isSymbol(var):
            if var.isEqual(env.variable):
                return env.content

    return var
Пример #5
0
 def lambdanize(self, data_list):
     if isSymbol(data_list):
         return data_list
     else:
         if len(data_list) == 3:
             if isLambdaSymbol(data_list[0]):
                 return LambdaEq(data_list[1], self.lambdanize(data_list[2]))
             else:
                 return [self.lambdanize(x) for x in data_list]
         else:
             return [self.lambdanize(x) for x in data_list]
Пример #6
0
 def combine(self, data_list):
     if isSymbol(data_list):
         return data_list
     elif isLambdaEq(data_list):
         return data_list
     else:
         if len(data_list) == 2:
             if isLambdaEq(data_list[0]):
                 return Combination(data_list[0], self.combine(data_list[1]))
             else:
                 return [self.combine(x) for x in data_list]
         else:
             return [self.combine(x) for x in data_list]
Пример #7
0
 def loop(p, v):
     if isinstance(p, _Matchable):
         matched = p.match(v)
         # matched :: False | Dict
         if matched != False:
             for k, v in matched:
                 _updateResult(result, k, v)
         else:
             return False
     elif isSymbol(p):
         _updateResult(result, p, v)
     else:
         if p != v:
             return False
Пример #8
0
def matchList(self: _Matchable, value):

    if not isinstance(value, list):
        return False

    if len(self._Matchable__params) == 0:
        return {} if len(value) == 0 else False

    head = sublist(self._Matchable__params, 0,
                   len(self._Matchable__params) - 1)
    tail = self._Matchable__params[-1]
    if not isinstance(tail, (list, Symbol)):
        raise SyntaxError("last element of pattern must be list or symbol")

    headValue = sublist(value, 0, len(head))
    tailValue = sublist(value, len(headValue))

    result = {}

    def loop(p, v):
        if isinstance(p, _Matchable):
            matched = p.match(v)
            # matched :: False | Dict
            if matched != False:
                for k, v in matched:
                    _updateResult(result, k, v)
            else:
                return False
        elif isSymbol(p):
            _updateResult(result, p, v)
        else:
            if p != v:
                return False

    # match head
    for p, v in zip_longest(head, headValue):
        if loop(p, v) == False:
            return False
    # match tail
    if isSymbol(tail):
        _updateResult(result, tail, tailValue)
    else:
        for p, v in zip_longest(tail, tailValue):
            if loop(p, v) == False:
                return False

    return result
Пример #9
0
Файл: secd.py Проект: ganow/secd
    def update(self):

        if self.c == []:
            hd_d = hd(self.d)
            if isD0(hd_d):
                print 'reduction finished'
                return
            else:
                self.s = cons(hd(self.s), hd_d.s)
                self.e = hd_d.e
                self.c = hd_d.c
                self.d = hd_d.d
        else:

            hd_c = hd(self.c)

            if isSymbol(hd_c):
                self.s = [find_var_in_env(hd_c, self.e)]
                self.c = tl(self.c)
            elif isList(hd_c):
                self.s = [find_list_in_env(hd_c, self.e)]
                self.c = tl(self.c)
            elif isLambdaEq(hd_c):
                self.s = cons(Closure(hd_c.bv, hd_c.body, self.e), self.s)
                self.c = tl(self.c)
            elif isAP(hd_c):
                hd_s = hd(self.s)
                if isClosure(hd_s):

                    self.d = SECDMachine(tl(tl(self.s)),
                                         self.e,
                                         tl(self.c),
                                         self.d)

                    s_2nd = hd(tl(self.s))
                    self.s = []
                    self.e = derive(assoc(hd_s.bv, s_2nd), self.e)
                    self.c = unitlist(hd_s.body)
                else:
                    self.s = cons(hd_s(hd(tl(self.s))), tl(tl(self.s)))
            elif isCombination(hd_c):
                self.c = cons([hd_c.operand, hd_c.operator, AP()],
                               tl(self.c))

        print self
        self.update()
Пример #10
0
 def fromscheme(self, val, shallow=False):
     "Convert a Scheme value to a Python value."
     if expressions.isCompoundProcedure(val):
         return schemepy.types.Lambda(val, self, shallow)
     if expressions.isPrimitiveProcedure(val):
         fun = expressions.primitiveImplementation(val)
         orig = fun.__dict__.get("_orig", None)
         if orig and callable(orig):
             return orig
         return schemepy.types.Lambda(val, self, shallow)
     if symbol.isSymbol(val):
         if val == symbol.true:
             return True
         if val == symbol.false:
             return False
         return schemepy.types.Symbol(str(val))
     if pair.isNull(val):
         return []
     if isAList(val):
         dic = {}
         while not pair.isNull(val):
             el = pair.car(val)
             key = self.fromscheme(pair.car(el))
             value = pair.cdr(el)
             if not shallow:
                 value = self.fromscheme(value)
             dic[key] = value
             val = pair.cdr(val)
         return dic
     if pair.isList(val):
         lst = []
         while not pair.isNull(val):
             el = pair.car(val)
             if not shallow:
                 el = self.fromscheme(el)
             lst.append(el)
             val = pair.cdr(val)
         return lst
     if pair.isPair(val):
         car = pair.car(val)
         cdr = pair.cdr(val)
         if not shallow:
             car = self.fromscheme(car)
             cdr = self.fromscheme(cdr)
         return schemepy.types.Cons(car, cdr)
     return val
Пример #11
0
 def fromscheme(self, val, shallow=False):
     "Convert a Scheme value to a Python value."
     if expressions.isCompoundProcedure(val):
         return schemepy.types.Lambda(val, self, shallow)
     if expressions.isPrimitiveProcedure(val):
         fun = expressions.primitiveImplementation(val)
         orig = fun.__dict__.get("_orig", None)
         if orig and callable(orig):
             return orig
         return schemepy.types.Lambda(val, self, shallow)
     if symbol.isSymbol(val):
         if val == symbol.true:
             return True
         if val == symbol.false:
             return False
         return schemepy.types.Symbol(str(val))
     if pair.isNull(val):
         return []
     if isAList(val):
         dic = {}
         while not pair.isNull(val):
             el = pair.car(val)
             key = self.fromscheme(pair.car(el))
             value = pair.cdr(el)
             if not shallow:
                 value = self.fromscheme(value)
             dic[key] = value
             val = pair.cdr(val)
         return dic
     if pair.isList(val):
         lst = []
         while not pair.isNull(val):
             el = pair.car(val)
             if not shallow:
                 el = self.fromscheme(el)
             lst.append(el)
             val = pair.cdr(val)
         return lst
     if pair.isPair(val):
         car = pair.car(val)
         cdr = pair.cdr(val)
         if not shallow:
             car = self.fromscheme(car)
             cdr = self.fromscheme(cdr)
         return schemepy.types.Cons(car, cdr)
     return val
Пример #12
0
def matchTuple(self: _Matchable, value):
    if not isinstance(value, tuple):
        return False
    if len(self._Matchable__params) != len(value):
        return False
    result = {}
    for p, v in zip(self._Matchable__params, value):
        if isinstance(p, _Matchable):
            matched = p.match(v)
            # matched :: False | Dict
            if matched != False:
                for k, v in matched:
                    _updateResult(result, k, v)
            else:
                return False
        elif isSymbol(p):
            _updateResult(result, p, v)
        else:
            if v != p:
                return False
    return result
Пример #13
0
 def get_keyword_tag(self, expr):
     """Tries to return the keyword.  If no keyword exists, returns
     None."""
     if pair.isPair(expr) and symbol.isSymbol(pair.car(expr)):
         return str(pair.car(expr)).lower()
     return None
Пример #14
0
def definitionVariable(exp):
    if isSymbol(pair.cadr(exp)):
        return pair.cadr(exp)
    return pair.car(pair.cadr(exp))
Пример #15
0
 def __init__(self, variable, content):
     super(Environment, self).__init__()
     if not isSymbol(variable):
         raise AttributeError('1st arg of Environment must be the Symbol class.')
     self.variable = variable
     self.content = content
def isVariable(exp):
    """Returns true if the expression looks like a symbol."""
    return isSymbol(exp)
Пример #17
0
Файл: secd.py Проект: ganow/secd
    def update(self):

        if self.c == []:
            hd_d = hd(self.d)
            if isD0(hd_d):
                print 'reduction finished'
                if len(self.s) >= 2:
                    return self.s
                else:
                    return self.s[0]
            else:
                self.s = cons(hd(self.s), hd_d.s)
                self.e = hd_d.e
                self.c = hd_d.c
                self.d = hd_d.d
        else:

            hd_c = hd(self.c)

            if isSymbol(hd_c):
                self.s = [find_var_in_env(hd_c, self.e)]
                self.c = tl(self.c)
            elif isList(hd_c):
                # ここが本来のsecd未定義の処理
                # self.s = [find_list_in_env(hd_c, self.e)]
                # self.c = cons(AP(), tl(self.c))

                # う、動かない.... > out2.txt
                print
                print '### child start ###'
                new_hd_c = []
                for i, elem in enumerate(hd_c):
                    child = SECDMachine(c=[elem],e=self.e)
                    new_hd_c.append(child.update())
                    print '### %d th child end ###' % i
                    print
                print '###  child end  ###'
                print
                self.s = [find_list_in_env(new_hd_c, self.e)]
                self.c = tl(self.c)

            elif isLambdaEq(hd_c):
                self.s = cons(Closure(hd_c.bv, hd_c.body, self.e), self.s)
                self.c = tl(self.c)
            elif isAP(hd_c):
                hd_s = hd(self.s)
                if isClosure(hd_s):

                    self.d = SECDMachine(tl(tl(self.s)),
                                         self.e,
                                         tl(self.c),
                                         self.d)

                    s_2nd = hd(tl(self.s))
                    self.s = []
                    self.e = derive(assoc(hd_s.bv, s_2nd), self.e)
                    self.c = unitlist(hd_s.body)
                else:
                    self.s = cons(hd_s(hd(tl(self.s))), tl(tl(self.s)))

            elif isCombination(hd_c):
                self.c = cons([hd_c.operand, hd_c.operator, AP()],
                               tl(self.c))

        print self
        return self.update()
def definitionValue(exp):
    if isSymbol(pair.cadr(exp)):
        return pair.caddr(exp)
    return makeLambda(pair.cdr(pair.cadr(exp)), pair.cddr(exp))
def definitionVariable(exp):
    if isSymbol(pair.cadr(exp)):
        return pair.cadr(exp)
    return pair.car(pair.cadr(exp))
Пример #20
0
def definitionValue(exp):
    if isSymbol(pair.cadr(exp)):
        return pair.caddr(exp)
    return makeLambda(pair.cdr(pair.cadr(exp)),
                      pair.cddr(exp))
Пример #21
0
def isVariable(exp):
    """Returns true if the expression looks like a symbol."""
    return isSymbol(exp)
Пример #22
0
def isLambdaSymbol(symbol):
    if not isSymbol(symbol):
        return False
    else:
        return symbol.isEqual(Symbol('lambda'))