def expandToFuncApp(lib: FnLibrary, term: PPTerm, ntId: int, fname: str) -> Optional[PPTerm]: resTerm = None # Not needed now as there are no lambda terms # libItem = ScopeUtils.getAVarInScope(lib, term, ntId, fname) libItem = lib.getWithLibPrefix(fname) assert libItem nt = ASTUtils.getNthNT(term, ntId) libItem = alphaConvertLibItem(libItem, term) # TODO: expandToVar passed nt.sort as second argument subst = unifyOne(nt.sort, libItem.sort.rtpe) if not gUseTypes: if subst is None: subst = [] if subst is not None: nts = [PPTermNT('Z', arg_sort) for arg_sort in libItem.sort.args] fnApp = PPFuncApp(PPVar(libItem.name), nts) termUnified = applySubst(subst, term) fnAppUnified = applySubst(subst, fnApp) resTerm = ReprUtils.replaceNthNT(termUnified, ntId, fnAppUnified) return resTerm
def displayUnifyListH(xs, ys): subst = unifyLists(xs, ys) row1 = ['xs'] + list(map(simplerep, xs)) row2 = ['ys'] + list(map(simplerep, ys)) row3 = ['xs+'] + list(map(simplerep, applySubst(subst, xs))) row4 = ['ys+'] + list(map(simplerep, applySubst(subst, ys))) tab1 = mkTable([row1, row2, row3, row4]) rows = [] for p, q in subst: row = [p, q] row = [simplerep(d) for d in row] rows.append(row) tab2 = mkTable(rows) return tab1 + '<br>' + tab2
def displayUnifyList(xs, ys): subst = unifyLists(xs, ys) rows = [] for x, y in zip(xs, ys): row = [x, y, applySubst(subst, x), applySubst(subst, y)] row = [simplerep(d) for d in row] rows.append(row) tab1 = mkTable(rows) rows = [] for p, q in subst: row = [p, q] row = [simplerep(d) for d in row] rows.append(row) tab2 = mkTable(rows) return tab1 + tab2
def test0(): tensorA3 = mkTensorSort(PPInt(), ['A', 3]) tensor2B = mkTensorSort(PPInt(), [2, 'B']) l1 = [tensorA3] l2 = [tensor2B] subst = unifyLists(l1, l2) print(subst) # [(PPDimVar(name='A'), PPDimConst(value=2)), (PPDimVar(name='B'), PPDimConst(value=3))] r1 = [applySubst(subst, x) for x in l1] r2 = [applySubst(subst, x) for x in l2] assert r1 == [ PPTensorSort(param_sort=PPInt(), shape=[PPDimConst(value=2), PPDimConst(value=3)]) ] assert r1 == r2
def test1(): listOfInt = PPListSort(PPInt()) listOfT1 = PPListSort(PPSortVar('T1')) listOfT2 = PPListSort(PPSortVar('T2')) l1 = [listOfInt, listOfT1] l2 = [listOfInt, listOfT2] subst = unifyLists(l1, l2) print(subst) # [(PPSortVar(name='T1'), PPSortVar(name='T2'))] r1 = [applySubst(subst, x) for x in l1] r2 = [applySubst(subst, x) for x in l2] assert r1 == [ PPListSort(param_sort=PPInt()), PPListSort(param_sort=PPSortVar(name='T2')) ] assert r1 == r2
def test3(): graphOfA = PPGraphSort(PPSortVar('A')) graphOfB = PPGraphSort(PPSortVar('B')) graphOfT1 = PPGraphSort(PPSortVar('T1')) graphOfT2 = PPGraphSort(PPSortVar('T2')) l1 = [graphOfA, graphOfB] l2 = [graphOfT1, graphOfT2] subst = unifyLists(l1, l2) print(subst) # [(PPSortVar(name='A'), PPSortVar(name='T1')), (PPSortVar(name='B'), PPSortVar(name='T2'))] r1 = [applySubst(subst, x) for x in l1] r2 = [applySubst(subst, x) for x in l2] assert r1 == [ PPGraphSort(param_sort=PPSortVar(name='T1')), PPGraphSort(param_sort=PPSortVar(name='T2')) ] assert r1 == r2
def expandToVar(lib: FnLibrary, term: PPTerm, ntId: int, vname: str) -> Optional[PPTerm]: """ Generate a new term by replacing a "ntId"th NT from a "term" with a variable (in scope) with name "fname" """ nt = ASTUtils.getNthNT(term, ntId) # libItem = ScopeUtils.getAVarInScope(lib, term, ntId, vname) libItem = lib.getWithLibPrefix(vname) assert libItem libItem = alphaConvertLibItem(libItem, term) subst = unifyOne(libItem.sort, nt.sort) if not gUseTypes: if subst is None: subst = [] termExpanded = None if subst is not None: termUnified = applySubst(subst, term) termExpanded = ReprUtils.replaceNthNT(termUnified, ntId, PPVar(libItem.name)) return termExpanded