<div dir="ltr"><div>Hi Bardur,</div><div><br></div><div>The hierarchy will be used for FFI in GHCVM - my effort at bringing the Haskell to the JVM. My goal has been to make FFI Haskell code look pretty close to Java code and thereby not making it intimidating for newcomers coming from Java, while demonstrating that monads are powerful enough to embed a Java-like language inside of Haskell without using types that would be confusing to a newcomer - like the ST monad and its use of rank-2 types.</div><div><br></div><div>Currently, if you want to store raw Java objects inside of Haskell in GHCVM, you do:</div><div><br></div><div>```haskell</div><div>data {-# CLASS "java.lang.String" #-} JString = JString (Object# JString)</div><div>```</div><div><br></div><div>Note that Object# (c :: *) :: # is a new primitive type constructor I introduced to allow raw Java objects to be stored in data types. It's only type-safe to do this if the underlying object is immutable or locally immutable (doesn't change much during the time of use).</div><div><br></div><div>This above type definition, while confusing, is succinct and kills two birds with one stone:</div><div>1) It allows JString to be used as a 'tag type' that stores metadata on the class of the underlying object it stores.</div><div>2) It allows JString to be used as a boxed representation of a raw Java object, just as Int is a boxed version of Int#.</div><div><br></div><div>There's also the Java monad:</div><div><br></div><div>```haskell</div><div>newtype Java c a = Java (Object# c -> (# Object# c, a #))</div><div>```</div><div><br></div><div>The c is the tag type - essentially it determines the underlying representation of the threaded 'this' pointer. This is a special monad recognized by GHCVM.</div><div><br></div><div>The final goal is to be able to import methods from java without doing manual casting at the Haskell level - it uses the Extends typeclass to handle that for you.</div><div><br></div><div>For example, assume you need to import the java.lang.Object.toString() method from Java. Obviously, you wouldn't want to re-import this method for every possible Java class you ever interact with. Instead, you would import it like so:</div><div><br></div><div>```haskell</div><div>data {-# CLASS "java.lang.Object" #-} Object = Object (Object# Object)</div><div><br></div><div>type instance Super JString = Object</div><div><br></div><div>foreign import java unsafe "toString" toJString :: Extends a Object => Java a JString</div><div><br></div><div>getStringFromString :: Java JString JString</div><div>getStringFromString = toJString</div><div><br></div><div>getStringFromObject :: Java Object JString</div><div>getStringFromObject = toJString</div><div>```</div><div><br></div><div>So this allows for reuse of a single foreign import in multiple particular cases as shown above. To see a more "real-world" example of this in action, check out the example JavaFX project that compiles with GHCVM [1].</div><div><br></div><div>I'm open to suggestions on better ways to accomplish the same goal.</div><div><br></div><div>Thanks,</div><div>Rahul</div><div><br></div><div>[1] <a href="https://github.com/rahulmutt/ghcvm-javafx">https://github.com/rahulmutt/ghcvm-javafx</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 5, 2016 at 7:42 PM, Bardur Arantsson <span dir="ltr"><<a href="mailto:spam@scientician.net" target="_blank">spam@scientician.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2016-10-05 06:31, Rahul Muttineni wrote:<br>
> Hi cafe,<br>
><br>
> I want to embed Java class hierarchies in Haskell, but I am unable to<br>
> find a clean way to do it.<br>
><br>
<br>
</span>Is this purely an academic exercise, or are you trying to solve some<br>
higher-level ("real") problem?<br>
<br>
If it's the latter then it might be a better idea to describe the<br>
overall problem you're trying to solve. (I.e. this may be an instance of<br>
the "XY Problem".)<br>
<br>
Regards,<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Rahul Muttineni</div>
</div>