-
Notifications
You must be signed in to change notification settings - Fork 145
Calling Frege Code from Java
The Frege Run Time System (see also this drawing) consists of a number of mostly abstract classes and interfaces, and a handful of static methods that implement some primitives needed in Frege code.
The run time system is stateless, i.e. one does not need to initialize or setup anything.
The main concern of the run time system is to provide for lazy evaluation and partial function application.
A Frege module is compiled to a Java class that acts as namespace for the items defined in the module.
module tutorial.Example where
import frege.prelude.Floating
quadrt :: Double -> Double
quadrt = sqrt . sqrt
Here is an outline of the corresponding Java code (comments introduced manually):
package tutorial; // missing if Frege module name is a simple one
import frege.prelude.PreludeList; // Import of Frege modules
import frege.prelude.Arrays; // you didn't know they existed ...
import frege.Prelude;
import frege.prelude.Floating; // ... and also explicitly imported ones.
import frege.prelude.Maybe;
import frege.prelude.PreludeBase;
import frege.prelude.PreludeNative;
import frege.prelude.PreludeMonad;
import frege.prelude.PreludeText;
@SuppressWarnings("unused") // We'll have lots of unused local vars. Sorry.
@frege.runtime.Meta.FregePackage( // Meta information used when this
// package is ever imported.
source="/home/.../Example.fr", time=1357511227564L,
ops={
@frege.runtime.Meta.Operator(name="<$>", kind=0, prec=13),
// ... and so on and on ....
)
final public class Example { // the module namespace
final public static double quadrt(final double arg$1) {
return java.lang.Math.sqrt(java.lang.Math.sqrt(arg$1));
}
}
As one can see in the previous example, a top level function is translated to a public static method that is a member of the module namespace class. But it is, unfortunately, not always that easy, see below.
Types appear as static classes or interfaces that are members of the module class. Their names always starts with a capital T
followed by the original name.
An enumeration type is one that has only nullary constructors:
data Color = Red | Green | Blue
where
favored Blue = true
favored _ = false
This compiles to:
final public static class TColor {
private TColor() {}
final public static short Blue = 2;
final public static short Green = 1;
final public static short Red = 0;
final public static boolean favored(final short arg$1) {
if (arg$1 == TColor.Blue) {
return true;
}
return false;
}
}
Here, the class TColor
merely works as namespace for the methods that correspond to the Frege functions defined in the where
block of the data
definition.
All enumeration values are mapped to constants of type short
, and hence values of different enumeration types cannot be distinguished any more on the Java level.
Home
News
Community
- Online Communities
- Frege Ecosystem
- Frege Day 2015
- Protocol
- Simon Peyton-Jones Transcript
- Talks
- Articles
- Books
- Courses
Documentation
- Getting Started
- Online REPL
- FAQ
- Language and API Reference
- Libraries
- Language Interoperability
- Calling Frege From Java (old)
- Calling Java From Frege
- Calling Frege From Java (new)
- Compiler Manpage
- Source Code Doc
- Contributing
- System Properties
- License
- IDE Support
- Eclipse
- Intellij
- VS Code and Language Server
- Haskell
- Differences
- GHC Options vs Frege
- Learn You A Haskell Adaptations
- Official Doc
Downloads