コード例 #1
0
  def step(goals):

    def undo():
      l = len(vs) - vtop
      while (l):
        vs.pop()
        l -= 1
      while trail:
        v = trail.pop()
        if v < vtop: vs[v] = v

        # fresh copy of term, with vars >=vtop
    def relocate(t):
          if isvar(t):
            newt = vtop + t
            if newt >= len(vs):
              extendTo(newt, vs)
            return newt
          elif not istuple(t):
            return t
          else:
            return tuple(map(relocate, t))

    def unfold(g, gs):
      for cs in css:
        h, bs = cs
        if not unifyWithEnv(relocate(h), g, vs, trail=trail, ocheck=False):
          undo()
          continue  # FAILURE
        else:
          bs1 = relocate(bs)
          bsgs = gs
          for b1 in reversed(bs1) :
            bsgs=(b1,bsgs)
          yield bsgs  # SUCCESS

    # step
    if goals == ():
      yield extractTerm(goal, vs)
    else:
      trail = []
      vtop = len(vs)
      g, goals = goals
      if db and g and g[0]=='#':
        g=extractTerm(g[1:],vs)
        #print('  DB_CALL:',g,'LEN0',len(trail),goals)
        for ok in db.unify_with_fact(g,vs,trail):
          if not ok:
            #print('    FAIL', g,len(trail))
            undo()
            continue
          #print('  BIND',extractTerm(g,vs),'-->',ok,trail,vs,goals)
          yield from step(goals)
          undo()
      else :
        for newgoals in unfold(g, goals):
          yield from step(newgoals)
          undo()
コード例 #2
0
 def unfold(l, b, gs):
     g0, gs0 = gs
     for cs0 in css:
         newl, cs = relocate(l, cs0)  # term, sorted list
         h, bs0 = cs
         vs = []
         if not unifyWithEnv(h, g0, vs, trail=None, ocheck=True):
             continue  # FAILURE
         else:
             newb = extractTerm(b, vs)
             g = extractTerm(g0, vs)
             bs = fromList(bs0)
             bsgs = concat(bs, gs0)
             newgs = extractTerm(bsgs, vs)
             r = newl, (newb, newgs)
             yield r  # SUCCESS
コード例 #3
0
    def step(g, goals):
        vtop = len(vs)

        def undo(vtop):
            top = len(vs)
            l = len(vs) - vtop
            while (l):
                vs.pop()
                l -= 1
            top = len(trail)
            while trail:
                v = trail.pop()
                if v < vtop: vs[v] = v

        def unfold(b, gs, vtop):
            # fresh copy of term, with vars >=vtop
            def relocate(t):
                if isvar(t):
                    newt = vtop + t
                    if newt >= len(vs):
                        extendTo(newt, vs)
                    return newt
                elif not istuple(t):
                    return t
                else:
                    return tuple(map(relocate, t))

            g0, gs0 = gs
            for cs0 in css:
                h0, bs0 = cs0
                h = relocate(h0)
                if not unifyWithEnv(h, g0, vs, trail=trail, ocheck=False):
                    undo(vtop)
                    continue  # FAILURE
                else:
                    bs1 = relocate(bs0)
                    bsgs = gs0
                    for b1 in reversed(bs1):
                        bsgs = (b1, bsgs)
                    yield bsgs  # SUCCESS

        trail = []
        if goals == ():
            yield extractTerm(g, vs)
        else:
            for newggs in unfold(g, goals, vtop):
                goals = newggs
                yield from step(g, goals)
                undo(vtop)
コード例 #4
0
ファイル: natlog7.py プロジェクト: ptarau/pypro
    def step(goals):
        def undo():
            l = len(vs) - vtop
            while (l):
                vs.pop()
                l -= 1
            while trail:
                v = trail.pop()
                if v < vtop: vs[v] = v

        def unfold(g, gs):
            # fresh copy of term, with vars >=vtop
            def relocate(t):
                if isvar(t):
                    newt = vtop + t
                    if newt >= len(vs):
                        extendTo(newt, vs)
                    return newt
                elif not istuple(t):
                    return t
                else:
                    return tuple(map(relocate, t))

            for cs in css:
                h, bs = cs
                if not unifyWithEnv(
                        relocate(h), g, vs, trail=trail, ocheck=False):
                    undo()
                    continue  # FAILURE
                else:
                    bs1 = relocate(bs)
                    bsgs = gs
                    for b1 in reversed(bs1):
                        bsgs = (b1, bsgs)
                    yield bsgs  # SUCCESS

        # step
        if goals == ():
            yield extractTerm(goal, vs)
        else:
            trail = []
            vtop = len(vs)
            g, goals = goals

            for newgoals in unfold(g, goals):
                yield from step(newgoals)
                undo()
コード例 #5
0
ファイル: natlog12.py プロジェクト: ptarau/pypro
  def step(goals):

    # undoes bindings of variables contained in the trail
    def undo():
      l = len(vs) - vtop
      while (l):
        vs.pop()
        l -= 1
      while trail:
        v = trail.pop()
        if v < vtop: vs[v] = v

    # fresh copy of term, with vars >=vtop
    def relocate(t):
          if isvar(t):
            newt = vtop + t
            if newt >= len(vs):
              extendTo(newt, vs)
            return newt
          elif not istuple(t):
            return t
          else:
            return tuple(map(relocate, t))

    # unfolds a goal using matching clauses
    def unfold(g, gs):
      for cs in css:
        h, bs = cs
        if not unifyWithEnv(relocate(h), g, vs, trail=trail, ocheck=False):
          undo()
          continue  # FAILURE
        else:
          bs1 = relocate(bs)
          bsgs = gs
          for b1 in reversed(bs1) :
            bsgs=(b1,bsgs)
          yield bsgs  # SUCCESS

    # special operators
    def db_call(g,goals) :
      for ok in db.unify_with_fact(g, vs, trail):
        if not ok:  # FAILURE
          undo()
          continue
        yield from step(goals)  # SUCCESS
        undo()
    def python_call(g,goals):
      f=eval(g[0])
      f(g[1:])



    # step
    if goals == ():
      yield extractTerm(goal, vs)
    else:
      trail = []
      vtop = len(vs)
      g, goals = goals
      op=g[0]
      if op in ["~","`","``","^"] :
         g = extractTerm(g[1:], vs)
         if op=='~': # matches against database of facts
           yield from db_call(g,goals)
         elif op=='`' :
          python_call(g,goals)
          yield from step(goals)
          undo()
         elif op=='^' :
           yield g
           yield from step(goals)
           undo()

      else :
        for newgoals in unfold(g, goals):
          yield from step(newgoals)
          undo()
コード例 #6
0
ファイル: natlog13.py プロジェクト: ptarau/pypro
    def step(goals):

        # undoes bindings of variables contained in the trail
        def undo():
            l = len(vs) - vtop
            while (l):
                vs.pop()
                l -= 1
            while trail:
                v = trail.pop()
                if v < vtop: vs[v] = v

        # fresh copy of term, with vars >=vtop
        def relocate(t):
            if isvar(t):
                newt = vtop + t
                if newt >= len(vs):
                    extendTo(newt, vs)
                return newt
            elif not istuple(t):
                return t
            else:
                return tuple(map(relocate, t))

        # unfolds a goal using matching clauses
        def unfold(g, gs):
            for cs in css:
                h, bs = cs
                if not unifyWithEnv(
                        relocate(h), g, vs, trail=trail, ocheck=False):
                    undo()
                    continue  # FAILURE
                else:
                    bs1 = relocate(bs)
                    bsgs = gs
                    for b1 in reversed(bs1):
                        bsgs = (b1, bsgs)
                    yield bsgs  # SUCCESS

        ## special operators

        # yields facts matching g in db
        def db_call(g, goals):
            for ok in db.unify_with_fact(g, vs, trail):
                if not ok:  # FAILURE
                    undo()
                    continue
                yield from step(goals)  # SUCCESS
                undo()

        # simple call to Python (e.g., print)
        def python_call(g, goals):
            f = eval(g[0])
            f(*g[1:])

        # unifies with last arg yield from a generator
        # and first args, assumed ground, passed to it
        def gen_call(g, goals):
            gen = eval(g[0])
            g = g[1:]
            v = g[-1:]
            args = g[:-1]
            for r in gen(*args):
                yield r
                if not unifyWithEnv(v, r, vs, trail=trail, ocheck=False):
                    undo()
                else:
                    yield from step(goals)
                    undo()

        def dispatch_call(op, g, goals):
            if op == '~':  # matches against database of facts
                yield from db_call(g, goals)
            elif op == '^':
                yield g
                yield from step(goals)
                undo()
            elif op == '`':
                python_call(g, goals)
                yield from step(goals)
                undo()
            else:  # ^^ generator call
                yield from gen_call(g, goals)

        # step
        if goals == ():
            yield extractTerm(goal, vs)
        else:
            trail = []
            vtop = len(vs)
            g, goals = goals
            op = g[0]

            if op in ["~", "`", "``", "^"]:
                g = extractTerm(g[1:], vs)
                yield from dispatch_call(op, g, goals)
            else:
                for newgoals in unfold(g, goals):
                    yield from step(newgoals)
                    undo()