Example #1
0
def ActualTableList(l, atab, ftab):
    yl = pop.Empty
    while l != pop.Empty:
        e, l = pop.ConsD(l)
        atab, ftab, ye = ActualTable(e, atab, ftab)
        yl = pop.Append(yl, pop.List1(ye))
    return atab, ftab, yl
def Eprogram(pg):
    """ return pg with only where clauses for function defs left """
    """ assume function defs at top level only """
    subj, wk, body = WhereD(pg)
    b = body
    ebody = pop.Empty
    while not pop.EmptyP(b):  #run through dfs in body
        df, b = pop.ConsD(b)  #get current def and advance
        lhs, es, rhs = DefinitionD(df)  #dismantle current def
        if not WhereP(rhs):  # no where's
            ebody = pop.AddElement(ebody, df)  #add current def unchanged
            #print('ebody is',pio.Items(ebody));exit()
            continue
        rsubj, rwk, rbody = WhereD(rhs)  #dismantle rhs
        if VarDefinitionP(df):  #a var defined by a where
            rhs1 = Eprogram(rhs)  #flatten rhs
            r1subj, rwk1, r1body = WhereD(
                rhs1)  #dismantle new flat whereclause
            ebody = pop.Append(
                ebody, r1body)  #add the emptied defs to the program body
            df1 = DefinitionC(lhs, es,
                              r1subj)  #change df so var is equated to subject
            ebody = pop.AddElement(ebody, df1)  #add simplified def
            continue
        #a function where definition
        rhsf = Eprogram(rhs)  #flatten the rhs where clause
        df1 = DefinitionC(lhs, es,
                          rhsf)  #construct new definition with flattened rhs
        ebody = pop.AddElement(ebody, df1)
    return WhereC(subj, wk, ebody)
Example #3
0
def Rename(t, m):
    """ return t renamed according to map m """
    if LiteralP(t): return t
    if VarP(t):
        v = Var(t)
        ok, w = map.Apply(m, v)
        if not ok:
            print('Undefined variable ' + pio.Items(v))
            exit()
        return VarC(w)
    if OperationP(t):
        o, opds = OperationD(t)
        ropds = pop.ConsAll2(opds, Rename, m)
        return OperationC(o, ropds)
    if WhereP(t):
        subj, wk, body = WhereD(t)
        l = WhereLocalsL(t)
        n = NewMap(l)
        m1 = map.Update(m, n)
        rsubj = Rename(subj, m1)
        rbody = pop.Empty
        while body != pop.Empty:  #run through definitions in body
            df, body = pop.ConsD(body)
            rdf = Rdefinition(df, m, m1)
            rbody = pop.Append(rbody, List1(rdf))
        return WhereC(rsubj, wk, rbody)

    if CallP(t):
        fun, actsl = CallD(t)
        rfun = Rename(fun, m)
        ractsl = pop.ConsAll2(actsl, Rename, m)
        return CallC(rfun, ractsl)
    assert False, 'rename a strange term ' + str(t)
Example #4
0
def Awhere(wc):
    """ atomize subject and rhs of where clause adding generated defs to body """
    subject, wk,body = exp.WhereD(wc)
    atomicsubject, atomicdefs = Aexpr(subject)
    assert pop.ListP(atomicdefs), 'oops'
    while body != pop.Empty:
        mdef ,body = pop.DeCons(body)
        newdef, eqns = Adefinition(mdef); 
        assert pop.ListP(eqns), 'arg'
        atomicdefs = pop.Append(atomicdefs,eqns)
        atomicdefs = pop.Cons(newdef,atomicdefs)
    return exp.WhereC(atomicsubject,wk,atomicdefs),pop.Empty
Example #5
0
def ActualTable(e, atab, ftab):
    """ collects a table of actuals in the order of appearance
        returns table and yaghified program"""
    if LiteralP(e): return atab, ftab, e
    if VarP(e): return atab, ftab, e
    if OperationP(e):
        o, opds = OperationD(e)
        if pop.ListP(o):
            print(e)
            print(o)
            exit()
        tab, ftab, yopds = ActualTableList(opds, atab, ftab)
        return tab, ftab, OperationC(o, yopds)
    if WhereP(e):
        subj, wk, body = WhereD(e)
        atab, ftab, ysubj = ActualTable(subj, atab, ftab)
        atab, ftab, ybody = ActualTableList(body, atab, ftab)
        return atab, ftab, WhereC(ysubj, wk, ybody)
    if VarDefinitionP(e):
        v, es, rhs = VarDefinitionD(e)
        atab, ftab, yrhs = ActualTable(rhs, atab, ftab)
        return atab, ftab, DefinitionC(VarC(v), es, yrhs)
    if FunDefinitionP(e):
        f, formals, es, rhs = FunDefinitionD(e)
        atab, ftab, yrhs = ActualTable(rhs, atab, ftab)
        ftab = map.Extend(ftab, f, formals)
        return atab, ftab, DefinitionC(VarC(f), es, yrhs)
    if CallP(e):
        fun, actuals = CallD(e)
        funvar = Var(fun)
        atab, ftab, yactuals = ActualTableList(actuals, atab, ftab)
        ok, actlist = map.Apply(atab, funvar)
        if not ok: actlist = pop.Empty
        newactlist = pop.Append(actlist, pop.List1(yactuals))
        atab = map.MapS(atab, funvar, newactlist)
        return atab, ftab, OperationC(
            YCALLWord,
            pop.List2(fun, LiteralC(pop.NumC(pop.Length(newactlist) - 1))))
Example #6
0
def Aoperation(e):
    """ returns e1 dl where dl is the list of atomic definitions generated
        and e1 is the new atomic expression """
    o = exp.OperationSymbol(e)    #operator
    l = exp.OperationOperandL(e)  #operands
    vl = pop.Empty                #new variables introduced
    aeqs = pop.Empty              #list of equations generated
    while l != pop.Empty:          #iterate down operand list
        opd = pop.Head(l);l = pop.Tail(l)  #get next operand and advance
        if exp.VarP(opd):      #if it's a var nothing to do
            vl = pop.Cons(opd,vl)
            continue
        ae, dl = Aexpr(opd)         # process the operand
        aeqs = pop.Append(dl,aeqs)     # collect equations generated
        if not exp.VarP(ae):
            nv = VarGen()               # generate a new variable
            vl = pop.Cons(nv,vl)           # save it  
            d = exp.DefinitionC(nv,pop.EQUALWord,ae)      # generate new atomic definition
            aeqs = pop.Cons(d,aeqs) 
        else:
            vl = pop.Cons(ae,vl)
    vl = pop.Reverse(vl)               # variables accumulated in reverse order
    e1 = exp.OperationC(o,vl)           # new atomic expression
    return e1, aeqs                # return the atomic expression and equations generated
Example #7
0
def Compile(f):
    """ compile the program in file f to atomic equations """
    pio.parsing = True
    pg = prs.ParseFile(f)
    print('Program is ')
    #pio.WriteItemln(pg)
    prp.Termln(pg)
    print('Defs to import ', prs.defined)
    defs = pop.Empty
    for w in prs.defined:  #read parse and add defs of defined operators like wvr
        wname = WordName(w)
        f = open("defs/" + wname + ".lu", "r")
        sourcedef = f.read()
        f.close()
        ig = pio.ItemGenStringC(sourcedef)
        df = prs.Definition(ig)
        #print('Adding df')
        #prp.Termln(df);exit()
        defs = pop.AddElement(defs, df)
    subj, wk, body = exp.WhereD(pg)
    body = pop.Append(body, defs)  #add imported definitions
    pg = exp.WhereC(subj, wk, body)
    print('Expanded program')
    prp.Termln(pg)
    #print("check locals")
    pre.CheckLocals(pg)
    print("check dups")
    pre.CheckDups(pg)
    print("check embeddings")
    pre.ProgramEmbedded(pg)
    print('renamed program ')
    rpg = ren.Rename(pg, pop.EmptySet)
    prp.Termln(rpg)

    m = ari.Aritytab(rpg, map.EmptyMap)
    ok, var, arity = ari.ArityCheck(rpg, m)
    if ok:
        k = 1
        print('arities check out ')
    else:
        prp.printf("Variable ")
        pio.WriteItem(var)
        prp.printf(" should have arity ")
        print(arity)
        exit()
    lpg = elp.Eloop(rpg, map.EmptyMap)
    print('Delooped program ')
    prp.Termln(lpg)

    fpg = ewh.Eprogram(lpg)
    print('flattened program')
    pio.WriteItemln(fpg)
    prp.Termln(fpg)

    atab, ftab, ypg = ali.ActualTable(fpg, map.EmptyMap, map.EmptyMap)
    print('alified program')
    prp.Termln(ypg)
    defs = ali.ActualDefs(atab, ftab)
    subj, wk, body = exp.WhereD(ypg)
    body = pop.Append(body, defs)  #add ali defs
    ypg = exp.WhereC(subj, wk, body)
    #print('globalized program')
    ypg = glb.Gprogram(ypg)
    #prp.Termln(ypg)
    print('reflattend program')
    ypg = ewh.Eprogram(ypg)
    prp.Termln(ypg)
    print('atomized program')
    apg, dfs = atz.Aexpr(ypg)
    prp.Termln(apg)
    return apg