I'm currently going through the paper Extensibility for the Masses. Practical Extensibility with Object Algebras by Bruno C. d. S. Oliveira and William R. Cook (available many places on the internet - for example here: https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf).
On page 10, they write:
Adding new data variants is easy. The first step is to create new classes
BoolandIffin the usual object-oriented style (likeLitandAdd):class Bool implements Exp {...} class Iff implements Exp {...}
The implementation of Exp is, it seems, left as an exercise to the reader. It's not clear to me, however, how Exp is defined in this part of the paper. My question is:
How should Bool and Iff be implemented?
Here's what I've tried:
First defintion of Exp
Early in the paper, the Exp interface is defined like this:
interface Exp {
Value eval();
}
Here, Value is defined by another interface:
interface Value {
Integer getInt();
Boolean getBool();
}
The paper, however, quickly departs from this definition of Exp in favour of a Visitor-based definition.
Possible implementation of Bool
Based on that definition, how should one implement the Bool class?
Something like this seems like a start:
class Bool implements Exp {
boolean x;
public Bool(boolean x) { this.x = x; }
public Value eval() {
return new VBool(x);
}}
The question, however, becomes how to properly implement Value?
The paper only shows this:
class VBool implements Value {...}
The implementation doesn't seem total to me:
class VBool implements Value {
boolean x;
public VBool(boolean x) { this.x = x; }
public Boolean getBool() {
return new Boolean(x);
}
public Integer getInt() {
// What to return here?
}
}
As my above attempt shows, it's not clear what to return from getInt. I suppose I could return null or throw an exception, but that would imply that my implementation is partial.
In any case, this first definition of Exp seems only to exist as a motivating example in the paper, which then proceeds to define a better alternative.
Second definition of Exp
On page 4 the paper redefines Exp as an Internal Visitor:
interface Exp {
<A> A accept(IntAlg<A> vis);
}
Where IntAlg<A> is another interface:
interface IntAlg<A> {
A lit(int x);
A add(A e1, A e2);
}
So far things seem clear, until we get to implementing Bool and Iff...
Possible implementation of Bool
How should we implement the proposed Bool class based on this definition of Exp?
class Bool implements Exp {
boolean x;
public Bool(boolean x) { this.x = x; }
public <A> A accept(IntAlg<A> vis) {
// What to return here?
}}
There's no way to conjure an A value out of thin air, so one has to interact with vis in order to produce an A value. The vis parameter, however, only defines lit and add methods.
The lit method requires an int, which isn't available in Bool.
Likewise, add requires two A values, which are also unavailable. Again, I find myself at an impasse.
Third definition of Exp?
Then, on page 8, the paper shows this example:
int x = exp(base).eval();
Here, exp(base) returns Exp, but which definition of eval is this?
Apparently, Exp still (or again?) has an eval method, but now it returns int. Does it look like this?
interface Exp {
int eval();
}
The paper doesn't show this definition, so I may be misunderstanding something.
Possible implementation of Bool
Can we implement Bool and Iff with this definition of Exp?
class Bool implements Exp {
boolean x;
public Bool(boolean x) { this.x = x; }
public int eval() {
// What to return here?
}}
Again, it's not clear how to implement the interface. One could, of course, return 0 for false and 1 for true, but that's just an arbitrary decision. That doesn't seem appropriate.
Is there a fourth definition of Exp that I'm missing? Or is there some other information in the paper that's eluding me?
BTW, I apologise if I've made mistakes in my attempts. I don't normally write Java code.