Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.util.function.Function;
  2.  
  3. /**
  4. * Demonstration of Fixed Point Theorem for calculating a non-total function.
  5. */
  6. public class LeastFixedPoint {
  7.  
  8. // bottom, represented as infinite loop
  9. final Function<Integer, Integer> undefined = (a) -> this.undefined.apply(a);
  10. // bottom, represented as throwing exception
  11. final Function<Integer, Integer> undefined_ex = new Function<Integer, Integer>() {
  12. @Override
  13. public Integer apply(Integer a) {
  14. throw new RuntimeException("Undefined");
  15. }
  16. };
  17.  
  18.  
  19. // FUNC is a function that returns 5 when input is even and has undefined behavior for odd inputs
  20. // conveniently works out even if passed any bottom for parameter 'f' as f.apply is not evaluated if n == 0
  21. final Function<Function<Integer, Integer>, Function<Integer, Integer>> FUNC = (f) -> (n) -> n == 0 ? 5 : (n == 1 ? f.apply(n+2) : f.apply(n-2));
  22.  
  23. // A fixed point of FUN. Note this is not the least fixed point
  24. final Function<Integer, Integer> g = (a) -> 5;
  25.  
  26. public Function<Integer, Integer> compose(Function<Integer, Integer> bottom, int times) {
  27. Function<Integer, Integer> t = bottom;
  28.  
  29. // expand FUNC 'times' times and it is LUB of the F(F(F..)) chain of size 'times'
  30. while (times-- > 0) {
  31. t = FUNC.apply(t);
  32. }
  33. return t;
  34. }
  35.  
  36. public static void main(String[] args) {
  37. LeastFixedPoint fp = new LeastFixedPoint();
  38. // fp.undefined.apply(new Integer(10)); // in theory infinite loop; in reality stack overflow or throws exception or abnormal exit
  39. Function<Integer, Integer> bottom = fp.undefined_ex; // i.e., used to apply the case where recursion is not involved=terminal condition.
  40.  
  41.  
  42. int n = 10; // FUNC is defined
  43. Function<Integer, Integer> lub = fp.compose(bottom, n+1);
  44. System.out.println(String.format("using lub for a defined input, n = %d, result = %d", n, lub.apply(n)));
  45.  
  46. n = 11; // FUNC is undefined, hence throws exception due to choice of bottom above
  47. lub = fp.compose(bottom, n+1);
  48. try {
  49. System.out.println(lub.apply(n));
  50. } catch (Throwable t) {
  51. System.out.println(String.format("using lub for an undefined input, n = %d, result = <throws exception>", n));
  52. System.out.println(t);
  53. }
  54.  
  55. Function<Integer, Integer> ub = fp.g; // ub is more defined than 'necessary' whereas lub is defined to be just enough to match the semantics of FUN
  56. n = 10;
  57. System.out.println(String.format("using ub for a defined input, n = %d, result = %d", n, ub.apply(n)));
  58. n = 11;
  59. System.out.println(String.format("using ub for an undefined input, n = %d, result = %d", n, ub.apply(n)));
  60. // just to show that ub is a fixed point of FUNC
  61. System.out.println(String.format("using ub to show that it is a fixed point for a defined input, n = %d, result = %d", n, fp.compose(fp.g, 100).apply(n)));
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement