[scala] Disjoint unions in Java

View: New views
3 Messages — Rating Filter:   Alert me  

[scala] Disjoint unions in Java

by Kannan Goundan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


(Off-topic, but hopefully people on this mailing list have some
experience/interest in this sort of thing.  I apologize if this isn't
appropriate.)

I'm looking for a technique to simulate disjoint unions in Java.  The
technique should produce something that is easy to use from within Java
(i.e. I'm not just looking for a way to encode disjoint unions on the
JVM).

Right now, I'm using an abstract base class with a concrete subclass for
each option.

One issue is that matching an option requires a verbose "instanceof"
check along with a type cast.  It would be nice to be able to use a
"switch" (is there a way to encode disjoint unions using Java enums?).

Another issue is that I want to allow union types to be extended with new
options.  For example:

  type Bool = True | False
  type TriState extends Bool = Maybe
     // TriState can be True, False, or Maybe

Here, Bool is a subtype of TriState.  TriState describes a larger set of
values than Bool.

I'd appreciate any relevant tips or references.

Thanks.
- Kannan


Re: [scala] Disjoint unions in Java

by Vlad Patryshev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I did it for my categorical code in Java.

The idea is to store pairs (x, i), and to define appropriate injection functions.

2009/6/20 Kannan Goundan <kannan@...>

(Off-topic, but hopefully people on this mailing list have some
experience/interest in this sort of thing.  I apologize if this isn't
appropriate.)

I'm looking for a technique to simulate disjoint unions in Java.  The
technique should produce something that is easy to use from within Java
(i.e. I'm not just looking for a way to encode disjoint unions on the
JVM).

Right now, I'm using an abstract base class with a concrete subclass for
each option.

One issue is that matching an option requires a verbose "instanceof"
check along with a type cast.  It would be nice to be able to use a
"switch" (is there a way to encode disjoint unions using Java enums?).

Another issue is that I want to allow union types to be extended with new
options.  For example:

 type Bool = True | False
 type TriState extends Bool = Maybe
    // TriState can be True, False, or Maybe

Here, Bool is a subtype of TriState.  TriState describes a larger set of
values than Bool.

I'd appreciate any relevant tips or references.

Thanks.
- Kannan




--
Thanks,
-Vlad

Re: [scala] Disjoint unions in Java

by Tony Morris-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Kannan,
You might be interested in http://functionaljava.org/ though I don't
know of a way to allow extending union types (easily) in Java.
However, you can use Java enums in switch statements so you could write:

enum TriState { True, False, Maybe }

then switch(ts) {
    case True: ...
    case False: ...
    case Maybe: ...
}

Kannan Goundan wrote:

> (Off-topic, but hopefully people on this mailing list have some
> experience/interest in this sort of thing.  I apologize if this isn't
> appropriate.)
>
> I'm looking for a technique to simulate disjoint unions in Java.  The
> technique should produce something that is easy to use from within Java
> (i.e. I'm not just looking for a way to encode disjoint unions on the
> JVM).
>
> Right now, I'm using an abstract base class with a concrete subclass for
> each option.
>
> One issue is that matching an option requires a verbose "instanceof"
> check along with a type cast.  It would be nice to be able to use a
> "switch" (is there a way to encode disjoint unions using Java enums?).
>
> Another issue is that I want to allow union types to be extended with new
> options.  For example:
>
>   type Bool = True | False
>   type TriState extends Bool = Maybe
>      // TriState can be True, False, or Maybe
>
> Here, Bool is a subtype of TriState.  TriState describes a larger set of
> values than Bool.
>
> I'd appreciate any relevant tips or references.
>
> Thanks.
> - Kannan
>
>
>  

--
Tony Morris
http://tmorris.net/