Skip to content

Calling Frege Code from Java

Ingo60 edited this page Jan 16, 2013 · 22 revisions

Basics of the Frege Run Time System

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.

Lazy Evaluation

Mapping of Frege Items

Packages/Modules

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

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.

Enumeration Types

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.

Clone this wiki locally