tweag/inline-java

Name: inline-java

Owner: Tweag I/O

Description: Haskell/Java interop via inline Java code in Haskell modules.

Created: 2016-10-19 08:36:42.0

Updated: 2018-01-19 10:26:30.0

Pushed: 2018-01-20 02:42:27.0

Homepage:

Size: 448

Language: Haskell

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

inline-java: Call any JVM function from Haskell

CircleCI

NOTE 1: you'll need GHC >= 8.0.2 to compile and use this package.

The Haskell standard includes a native foreign function interface (FFI). Using it can be a bit involved and only C support is implemented in GHC. inline-java lets you call any JVM function directly, from Haskell, without the need to write your own foreign import declarations using the FFI. In the style of inline-c for C and inline-r for calling R, inline-java lets you name any function to call inline in your code. It is implemented on top of the jni and jvm packages.

Example

Graphical Hello World using Java Swing:

LANGUAGE DataKinds #-}
LANGUAGE QuasiQuotes #-}
LANGUAGE OverloadedStrings #-}
OPTIONS_GHC -fplugin=Language.Java.Inline.Plugin #-}
le Main where

rt Data.Text (Text)
rt Language.Java
rt Language.Java.Inline

 :: IO ()
 = withJVM [] $ do
message <- reflect ("Hello World!" :: Text)
[java| {
  javax.swing.JOptionPane.showMessageDialog(null, $message);
  } |]
Building it

Requirements:

On OS X, you'll need to install the Legacy Java SE 6 runtime when prompted, even when using Nix.

To build:

ack build

You can optionally get Stack to download a JDK in a local sandbox (using Nix) for good build results reproducibility. This is the recommended way to build inline-java. Alternatively, you'll need it installed through your OS distribution's package manager for the next steps (and you'll need to tell Stack how to find the JVM header files and shared libraries).

To use Nix, set the following in your ~/.stack/config.yaml (or pass --nix to all Stack commands, see the Stack manual for more):


able: true
Debugging

The generated java output can be dumped to stderr by passing to GHC

ugin-opt=Language.Java.Inline.Plugin:dump-java

If -ddump-to-file is in effect (as when using stack), the java code is dumped to <module>.dump-java instead.

License

Copyright (c) 2015-2016 EURL Tweag.

All rights reserved.

inline-java is free software, and may be redistributed under the terms specified in the LICENSE file.

Sponsors

         Tweag I/O              LeapYear

inline-java is maintained by Tweag I/O.

Have questions? Need help? Tweet at @tweagio.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.