コード例 #1
0
ファイル: lambda_calc.py プロジェクト: hooops/nlp_tools
  def __init__(self, sexp, types = None):
    self.sexp = sexp
    if isinstance(sexp, list):
      toks = list(flatten(sexp))
    else:
      toks = [sexp]
    #varz = [int(t[1:]) for t in toks if t[0] == '$']
    if types:
      self.types = dict(types)
    else:
      self.types = {}
    self.next_var = 0
    for i in range(len(toks)):
      tok = toks[i]
      if tok[0] == '$' and tok not in self.types:
        self.next_var = max(self.next_var, int(tok[1:]) + 1)
        if toks[i-1] == self.LAMBDA:
          self.types[tok] = toks[i+1]
        elif toks[i-1] in self.SPECIALS_E_IN:
          self.types[tok] = 'e'
        else:
          #logging.warn(types)
          #logging.warn('unknown type: %s', tok)
          self.types[tok] = 'UNK'
          #print self.sexp
          #print self.types
          #print 'WHAT?'
          #exit()

    self.typ = self.compute_type(sexp)
コード例 #2
0
    def __init__(self, sexp, types=None):
        self.sexp = sexp
        if isinstance(sexp, list):
            toks = list(flatten(sexp))
        else:
            toks = [sexp]
        #varz = [int(t[1:]) for t in toks if t[0] == '$']
        if types:
            self.types = dict(types)
        else:
            self.types = {}
        self.next_var = 0
        for i in range(len(toks)):
            tok = toks[i]
            if tok[0] == '$' and tok not in self.types:
                self.next_var = max(self.next_var, int(tok[1:]) + 1)
                if toks[i - 1] == self.LAMBDA:
                    self.types[tok] = toks[i + 1]
                elif toks[i - 1] in self.SPECIALS_E_IN:
                    self.types[tok] = 'e'
                else:
                    #logging.warn(types)
                    #logging.warn('unknown type: %s', tok)
                    self.types[tok] = 'UNK'
                    #print self.sexp
                    #print self.types
                    #print 'WHAT?'
                    #exit()

        self.typ = self.compute_type(sexp)
コード例 #3
0
  def split_on(self, expr):
    #print self
    expr = LambdaExpr(expr, types = dict(self.types))

    fvars = sorted(expr.free_vars())

    nvar = '$%d' % self.next_var

    if fvars:
      nsubexpr = (nvar,) + tuple(fvars)
    else:
      nsubexpr = nvar


    nexpr = expr.sexp
    nexpr_vars = []
    for fvar in fvars:
      #print
      #print fvar
      #print nexpr
      #print self.types
      nexpr = (
          self.LAMBDA,
          fvar,
          self.types[fvar],
          nexpr
      )
      nexpr_vars.append(self.types[fvar])
    nexpr = LambdaExpr(nexpr)

    if nexpr.typ == 'UNK' or isinstance(nexpr.typ, tuple) and 'UNK' in flatten(nexpr.typ):
      nexpr.typ = self.compute_fn_type_in(nexpr.sexp, self.sexp)
    assert nexpr.typ != 'UNK' or isinstance(nexpr.typ, tuple) and 'UNK' not in flatten(nexpr.typ)

    #print nexpr.typ
    #print nexpr_vars
    #print '-'

    nself = self.__replace_in(self.sexp, expr.sexp, nsubexpr)
    if nself == self.sexp:
      print self.sexp
      print expr.sexp
      print nsubexpr
      assert False
    nself = (
        self.LAMBDA,
        nvar,
        nexpr.typ,
        nself
    )

    nself = LambdaExpr(nself)

    nself = nself.normalize_vars()
    nexpr = nexpr.normalize_vars()

    #print self
    #print nself
    #print nexpr
    #print
    return (nself, nexpr)