MyNixOS website logo
Description

Create immutable algebraic data structures for Java.

A simple tool to create immutable algebraic data structures and visitors for Java (such as abstract syntax trees). The input syntax is similar to Haskell data types, and they will be compiled to Java class hierarchies.

java-adt

A tool to create immutable algebraic data structures and visitors for Java (such as abstract syntax trees). The input syntax is similar to Haskell data types, and they will be compiled to Java class hierarchies.

Installation

With a running Haskell installation, simply type into your shell

cabal install java-adt

and make sure your .cabal/bin/ (or similar) is part of your system PATH.

Example 1: Immutable linked lists with default visitor

Input: List.hs

data List A = Nil | Cons { head :: A, tail :: List A }

Invocation java-adt List.hs prints to standard output:

abstract class List<A> {
}

class Nil<A> extends List<A> {
    public Nil () {
    }
}

class Cons<A> extends List<A> {
    public A head;
    public List<A> tail;
    public Cons (A head, List<A> tail) {
        this.head = head;
        this.tail = tail;
    }
}

Invocation: java-adt -o List.java List.hs leaves output in List.java.

Invocation: java-adt -d List.hs outputs same but with default visitor on standard output:

abstract class List<A> {
    public abstract <R> R accept (ListVisitor<R,A> v);
}

class Nil<A> extends List<A> {
    public Nil () {
    }
    public <R> R accept (ListVisitor<R,A> v) {
        return v.visit (this);
    }
}

class Cons<A> extends List<A> {
    public A head;
    public List<A> tail;
    public Cons (A head, List<A> tail) {
        this.head = head;
        this.tail = tail;
    }
    public <R> R accept (ListVisitor<R,A> v) {
        return v.visit (this);
    }
}

interface ListVisitor<R,A> {
    public R visit (Nil<A> l);
    public R visit (Cons<A> l);
}

Example 2: A simple AST with custom visitor

Input file Exp.hs: (Note the use of Haskell lists in [Exp])

data Exp
  = EInt  { i :: Integer }
  | EAdd  { e1 :: Exp, e2 :: Exp }
  | ECall { f :: String, es :: [Exp] }
--visitor Integer EvalVisitor

Invocation java-ast -o Exp.java Exp.hs outputs into Exp.java:

import java.util.List;

abstract class Exp {
    public abstract Integer accept (EvalVisitor v);
}

class EInt extends Exp {
    public Integer i;
    public EInt (Integer i) {
        this.i = i;
    }
    public Integer accept (EvalVisitor v) {
        return v.visit (this);
    }
}

class EAdd extends Exp {
    public Exp e1;
    public Exp e2;
    public EAdd (Exp e1, Exp e2) {
        this.e1 = e1;
        this.e2 = e2;
    }
    public Integer accept (EvalVisitor v) {
        return v.visit (this);
    }
}

class ECall extends Exp {
    public String f;
    public List<Exp> es;
    public ECall (String f, List<Exp> es) {
        this.f = f;
        this.es = es;
    }
    public Integer accept (EvalVisitor v) {
        return v.visit (this);
    }
}

interface EvalVisitor {
    public Integer visit (EInt e);
    public Integer visit (EAdd e);
    public Integer visit (ECall e);
}

Input file grammar

The input file format is similar to Haskell data type declarations, with the special comment --visitor.

  datadecl    ::= 'data' uppername '=' constructors visitors

  constructor ::= uppername ['{' fieldlist '}']

  fieldlist   ::= fieldlist ',' field
                | field

  field       ::= lowername '::' type

  type        ::= type atom
                | atom

  atom        ::= name
                | '[' type ']'
                | '(' type ')'

  visitor     ::= '--visitor' type name

Limitations

  • Visitors do not support mutually recursive data types.
  • Record types with same constructor name as record name do not produce valid Java. E.g.
    data R = R { f :: A }
    
    creates two classes with name R and subsequent Java compilation errors.
Metadata

Version

1.0.20231204

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows