private K addFreshCell(K body) { if (freshVars.size() == 0) { return body; } KApply cellTerm = IncompleteCellUtils.make(KLabels.GENERATED_COUNTER_CELL, false, KRewrite(FRESH, KApply(KLabel("_+Int_"), FRESH, KToken(Integer.toString(freshVars.size()), Sorts.Int()))), false); return KApply(KLabels.CELLS, body, cellTerm); }
private Stream<? extends Sentence> genKore(Sort sort) { if (sort.equals(Sorts.K())) { return Stream.of(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable("K")), BooleanUtils.TRUE), BooleanUtils.TRUE, BooleanUtils.TRUE)); } else { List<Sentence> res = new ArrayList<>(); res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable(sort.name(), Att().add(Sort.class, sort))), BooleanUtils.TRUE), BooleanUtils.TRUE, BooleanUtils.TRUE)); res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KVariable("K")), BooleanUtils.FALSE), BooleanUtils.TRUE, BooleanUtils.TRUE, Att().add("owise"))); return res.stream(); } }
@Override public K apply(KRewrite k) { k = (KRewrite) super.apply(k); return KRewrite(k.left(), k.right(), k.att()); }
@Override public K apply(KRewrite k) { K left = super.apply(k.left()); return KRewrite(left, k.right(), k.att()); } }.apply(term);
private Rule funRule(KLabel fun, K k, Att att) { K resolved = transform(k); K withAnonVars = new ResolveAnonVar().resolveK(resolved); List<K> klist = new ArrayList<>(); klist.add(RewriteToTop.toLeft(withAnonVars)); klist.addAll(closure(k)); return Rule(KRewrite(KApply(fun, KList(klist)), RewriteToTop.toRight(withAnonVars)), BooleanUtils.TRUE, BooleanUtils.TRUE, att); }
@Override public K apply(KRewrite k) { K l = apply(k.left()); K r = apply(k.right()); if (l != k.left() || r != k.right()) { return KRewrite(l, r, k.att()); } else { return k; } }
@Override public K apply(KApply k) { if (KLabels.KREWRITE.equals(k.klabel())) { return KRewrite(apply(k.klist().items().get(0)), apply(k.klist().items().get(1)), k.att()); } else { k = (KApply) super.apply(k); return KApply(apply(k.klabel()), k.klist(), k.att()); } }
@Test public void testThreeRewritesSplit() { K term = cell("<T>", KRewrite(cells(cell("<k>"),cell("<env>")), cells()), KRewrite(cell("<env>"), cell("<k>")), KRewrite(cell("<k>"), cell("<k>"))); K expected = cell("<T>", cell("<ts>", cell("<t>", KRewrite(cells(cell("<k>"),cell("<env>")), cells())), cell("<t>", KRewrite(cell("<env>"), cell("<k>"))), cell("<t>", KRewrite(cell("<k>"), cell("<k>"))))); Assert.assertEquals(expected, pass.concretizeCell(term)); }
@Test public void testConcatStarCellEmptyl() { K term = cell("<top>", KRewrite(KVariable("Y"), cells())); K expected = cell("<top>", KRewrite(KVariable("Y"), KApply(KLabel(".ThreadCellBag")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testAddOptCell() { K term = cell("<t>", KVariable("X"), KRewrite(cells(), cell("<opt>"))); K expected = cell("<t>", KVariable("_0"), KVariable("_1"), KRewrite(KApply(KLabel(".OptCell")), cell("<opt>"))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testRemoveStarCell() { K term = cell("<top>", KRewrite(cell("<t>", KVariable("X")), cells())); K expected = cell("<top>", KRewrite(cell("<t>", KVariable("_0"), KVariable("_1"), KVariable("_2")), KApply(KLabel(".ThreadCellBag")))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testRewrites() { K term = cell("<T>", cell("<k>", intToToken(1)), KRewrite(cell("<k>", intToToken(2)), cell("<k>"))); K expected = cell("<T>", cell("<ts>", cell("<t>", cell("<k>", intToToken(1))), cell("<t>", KRewrite(cell("<k>", intToToken(2)), cell("<k>"))))); Assert.assertEquals(expected, pass.concretizeCell(term)); }
@Test public void testRewriteWithCellVariable() { K term = cell("<T>", KRewrite(KVariable("KCell", Att().add(Sort.class, Sort("KCell"))), cell("<k>", intToToken(1)))); K expected = cell("<T>", cell("<ts>", cell("<t>", KRewrite(KVariable("KCell", Att().add(Sort.class, Sort("KCell"))), cell("<k>", intToToken(1)))))); Assert.assertEquals(expected, pass.concretizeCell(term)); }
@Test public void testSimpleSplitting() { KVariable Y = KVariable("Y", Att().add(Sort.class, Sort("OptCell"))); K term = KRewrite(cell("<t>", cell("<env>"), KVariable("X"), Y), KVariable("X")); K expected = KRewrite(cell("<t>", KVariable("X"), cell("<env>"), Y), cell("<t>-fragment", KVariable("X"), app("noEnvCell"), app(".OptCell"))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testConcatStarCell() { K term = cell("<top>", KRewrite(KVariable("Y"), cells(KVariable("Y"), cell("<t>", KVariable("X"))))); K expected = cell("<top>", KRewrite(KVariable("Y"), KApply(KLabel("_ThreadCellBag_"), KVariable("Y"), cell("<t>", KVariable("_0"), KVariable("_1"), KVariable("_2"))))); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(cfgInfo, labelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
private static Set<Sentence> genProjection(Sort sort, Module m) { KLabel lbl = getProjectLbl(sort, m); KVariable var = KVariable("K", Att.empty().add(Sort.class, sort)); Rule r = Rule(KRewrite(KApply(lbl, var), var), BooleanUtils.TRUE, BooleanUtils.TRUE, Att().add("projection")); if (m.definedKLabels().contains(lbl)) { return Set(r); } return Set(Production(lbl, sort, Seq(Terminal(lbl.name()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add("function").add("projection")), r); }
@Test public void testRewriteWithCells() { K term = cell("<T>", cell("<k>", intToToken(1)), KRewrite(cells(cell("<k>", intToToken(2)), cell("<msg>")), cell("<k>"))); K expected = cell("<T>", cell("<ts>", cell("<t>", cell("<k>", intToToken(1))), cell("<t>", KRewrite(cells(cell("<k>", intToToken(2)), cell("<msg>")), cell("<k>"))))); Assert.assertEquals(expected, pass.concretizeCell(term)); }
@Test public void testFragmentBag() { K term = cell("<top>", KVariable("F"),cell("<t>", KRewrite(KVariable("Rest"),KSequence(KVariable("F"),KVariable("Rest"))))); K expected = cell("<top>", app("_ThreadCellBag_", KVariable("_0"), cell("<t>", KRewrite(KVariable("Rest"), KSequence(app("<top>-fragment",KVariable("_0"),KVariable("_1")),KVariable("Rest"))))), KVariable("_1")); KExceptionManager kem = new KExceptionManager(new GlobalOptions()); Assert.assertEquals(expected, new SortCells(bagCfgInfo, bagLabelInfo).sortCells(term)); Assert.assertEquals(0, kem.getExceptions().size()); }
@Test public void testNonCellItemRewrite() { K term = cell("<T>", KRewrite(KApply(KLabel("label")),cells(KApply(KLabel(".K")), cell("<k>",KVariable("X"))))); exception.expect(KEMException.class); exception.expectMessage("Can't mix items with different parent cells under a rewrite"); pass.concretize(term); }
@Test public void testCloseCellTerm() { K term = KRewrite(cells(), cells(cell("<thread>", true, false, cell("<k>", intToToken(1))), cell("<thread>", false, true, cell("<k>", intToToken(2))), cell("<thread>", true, true, cell("<env>", intToToken(2))))); K expected = KRewrite(cells(), cells(ccell("<thread>", ccell("<k>", intToToken(1)), ccell("<env>", KApply(KLabel(".Map")))), ccell("<thread>", ccell("<k>", intToToken(2)), ccell("<env>", KApply(KLabel(".Map")))), ccell("<thread>", ccell("<env>", intToToken(2)), ccell("<k>", stringToToken("defaultK"))))); Assert.assertEquals(expected, new CloseCells(cfgInfo, sortInfo, labelInfo).close(term)); }