/** * This method produces a new Abstraction which is the result of adding captured variables as new * arguments and bind it in current ns. It returns the name of the bound funciton. * * @param a * @param captured * @return a String object denoting the name given to this function * @throws SymbolException */ private String lift(Abstraction a, Set captured) throws SymbolException { Abstraction abs = a; if (!captured.isEmpty()) { /* recreate abstraction adding captured variables */ abs = new Abstraction(); /* bind captured variables */ Iterator it = captured.iterator(); while (it.hasNext()) { LocalBinding lb = (LocalBinding) it.next(); LocalBinding nlb = new LocalBinding(lb.getName()); abs.bind(nlb); } /* rebind old variables */ it = a.getBindings().values().iterator(); while (it.hasNext()) { LocalBinding lb = (LocalBinding) it.next(); LocalBinding nlb = new LocalBinding(lb.getName()); abs.bind(nlb); } /* restore body */ abs.setBody(a.getBody()); } /* * bind new abstraction in ns if a is an anonymous abstraction - no class * name - then it is renamed lambdaXXX and bound in namespace. Else, it is * given a new name and a reference to this new name is stored in substMap. */ String newname = "lambda" + counter++; ns.bind(newname, abs); abs.setClassName(ns.getName() + "." + newname); return newname; }
/** * This method produces an Expression from an arbitrary Abstraction object which is applied to a * list of Variables referencing captured bindings. * * @param a an Abstraction object to partially apply * @param captured * @return */ private Expression applyLifted(String s, Set captured) { if (captured.isEmpty()) return new Variable(s); Application app = new Application(); app.setFunction(new Variable(s)); /* bind captured variables */ Iterator it = captured.iterator(); while (it.hasNext()) { LocalBinding lb = (LocalBinding) it.next(); app.addArgument(new Variable(lb.getName())); } return app; }
/** * Returns an Abstraction object resulting from lifting the given expression with captured * variable set. if <code>f * x y</code> is an expression with <code>y</code> a captured variable, then it is transformed * into <code>lambdaXXX * = \ y -> f x y</code> which can later be used to replace the original expression with <code> * lambdaXXX y</code>. * * @param e * @param captured * @return a String object * @throws SymbolException */ private String lift(Expression e, Set captured) throws SymbolException { if (e instanceof Abstraction) return lift((Abstraction) e, captured); Abstraction abs = new Abstraction(); /* bind captured variables */ Iterator it = captured.iterator(); while (it.hasNext()) { LocalBinding lb = (LocalBinding) it.next(); LocalBinding nlb = new LocalBinding(lb.getName()); abs.bind(nlb); } /* restore body */ abs.setBody(e); String newname = "lambda" + counter++; ns.bind(newname, abs); return newname; }