-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-patt.5.java
183 lines (175 loc) · 4.96 KB
/
java-patt.5.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// -*- mode: java; buffer-read-only: t -*-
abstract class NumD {}
class Zero extends NumD {
public boolean equals(Object o) {
return o instanceof Zero;
}
}
class OneMoreThan extends NumD {
NumD predecessor;
OneMoreThan (NumD _p) { predecessor = _p; }
}
class RemFishV {
PieD forBot() {
return new Bot();
}
PieD forTop(FishD fish, Object t, PieD r) {
if (fish.equals(t))
return r.remFish(fish);
else
return new Top(t, r.remFish(fish));
}
}
class RemIntV {
PieD forBot() {
return new Bot();
}
PieD forTop(Integer x, Object t, PieD r) {
if (x.equals(t))
return r.remInt(x);
else
return new Top(t, r.remInt(x));
}
}
class RemAV {
PieD forBot() {
return new Bot();
}
PieD forTop(Object t, PieD r) {
if (new Anchovy().equals(t))
return r.remA();
else return new Top(t, r.remA());
}
}
class RemV {
PieD forBot() {
return new Bot();
};
PieD forTop(Object w, Object t, PieD r) {
if (w.equals(t))
return r.rem(w);
else
return new Top(t, r.rem(w));
}
}
abstract class PieD {
RemAV rafn = new RemAV();
RemFishV rffn = new RemFishV();
RemIntV rifn = new RemIntV();
RemV remfn = new RemV();
abstract PieD remA();
abstract PieD remFish(FishD fish);
abstract PieD remInt(Integer x);
abstract PieD rem(Object w);
}
class Bot extends PieD {
PieD remA() { return rafn.forBot(); }
PieD remFish(FishD fish) { return rffn.forBot(); }
PieD remInt(Integer x) { return rifn.forBot(); }
PieD rem(Object x) { return remfn.forBot(); }
public String toString() {
return String.format("Bottom");
}
}
class Top extends PieD {
Object t;
PieD r;
Top(Object _t, PieD _r) {
t = _t;
r = _r;
}
public String toString() {
return String.format("%s %s", t, r);
}
// ----------------------------------------
PieD remA() { return rafn.forTop(t, r); }
PieD remFish(FishD fish) { return rffn.forTop(fish, t, r); }
PieD remInt(Integer x) { return rifn.forTop(x, t, r); }
PieD rem(Object x) { return remfn.forTop(x, t, r); }
}
abstract class FishD {}
class Anchovy extends FishD {
public boolean equals(Object o) {
return o instanceof Anchovy;
}
}
class Salmon extends FishD {
public boolean equals(Object o) {
return (o instanceof Salmon);
}
}
class Tuna extends FishD {
public boolean equals(Object o) {
return (o instanceof Tuna);
}
}
class Test {
static void p(Object... objs) {
for(Object o:objs)
System.out.printf("%s ", o);
}
static private void test1() {
PieD x = (new Top(new Salmon(),
new Top(new Tuna(),
new Top(new Anchovy(),
new Bot())))
.remA()
.remFish(new Salmon())
.remFish(new Tuna())
.remFish(new Anchovy()));
p(x, ";\n");
}
static private void test2() {
PieD x = (new Top(new Integer(1),
new Top(new Integer(3),
new Top(new Anchovy(),
new Bot())))
.remA()
.remFish(new Anchovy())
.remFish(new Tuna())
.remInt(1)
.remInt(5)
.remInt(3));
p(x, ";\n");
}
static private void test3() {
PieD x = (new Top(new Integer(1),
new Top(new Integer(3),
new Top(new Anchovy(),
new Top(new Tuna(),
new Bot()))))
.rem(new Anchovy())
.rem(new Tuna())
.rem(1)
.rem(5)
.rem(3));
p(x, ";\n");
}
static private void test4() {
PieD x = (new Top(new Integer(1),
new Top(new Integer(3),
new Top(new Anchovy(),
new Top(new Zero(),
new Top(new OneMoreThan(new Zero()),
new Bot())))))
.rem(new Anchovy())
.rem(new Tuna())
.rem(new Zero())
.rem(new OneMoreThan(new Zero()))
.rem(1)
.rem(5)
.rem(3));
p(x, ";\n");
}
static public void main(String[] args) {
p("\n", "---test1", "\n");
test1();
p("\n", "---test2", "\n");
test2();
p("\n", "---test3", "\n");
test3();
p("\n", "---test4", "\n");
test4();
p ("\n", "--", "\n");
}
}