Computation Theory Labs

advertisement
Computation Theory Labs
1. Meta Programming using Reflection
Reflection is covered here: Java Reflection
We work in a restricted subset of Java called Lava. In Lava all methods have the signature:
String method(String ...args)
For example:
package meta;
public class Greeter {
public String greetings(String ... args) {
for(int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
}
return "done";
}
}
The greetings method can be called with any number of strings. (This is called varargs).
Implement a class called MetaMachine:
import java.lang.reflect.*;
class MetaMachine {
public String execute(String ... args) throws Exception {???}
}
The execute method assumes args[0] is the full name of a class, args[1] is the name of a method in
args[0], and the remaining args are this inputs to this method.
Execute uses Java reflection to load the class, find the method, and invoke it on the arguments.
For example:
class MetaMachineTester {
public static void main(String args[]) {
MetaMachine mm = new MetaMachine();
mm.execute("meta.Greeter", "greetings", "Hello", "Jupiter");
}
}
Outputs:
Hello Jupiter
What is printed by:
mm.execute("meta.MetaMachine", "execute", "meta.Greeter", "greetings",
"Hello", "Jupiter");
2. BigNat
In Java, implement and test the class BigNat:
class BigNat { ... } // multiple precision natural numbers
Each instance of BigNat contains a list of integers representing mega-digits:
class BigNat {
private ArrayList<Integer> digits;
// ...
}
We only allow non-negative integers to be digits.
For example, if x.digits = (a, b, c), then
x = a * m2 + b * m1 + c * m0 where m = 1 + Integer.MAX_VALUE (= 231)
Complete the proof by implementing inc and dec as BigNat methods.
Using inc and ec, implement add, mul, exp, and sub as BigNat methods. Hint: use recursion.
3. Closure Theorems
Prove the following theorems by implementing Java combinators that take as input deciders,
recognizers, or enumerators for A and B and return the desired decider, recognizer, or enumerator:
Theorem: If A and B are recursive, then so are A  B, A  B, A – B, and A X B.
class RecClosures {
// returns memC for C = A U B
public static Predicate<Integer> union(Predicate<Integer> memA,
Predicate<Integer> memB) { ??? }
// etc.
}
Theorem: If A and B are recursively enumerable, then so are A  B, A  B and A X B.
Provide test harnesses for your classes.
4. Turing Machines
Complete the implementation and testing of the TuringMachine class
5. PL
Complete the implementation and testing of the PL virtual machine.
Download