Written by Federico Tomassetti
in Software Development
    Exploring frege Haskell for the JVM

    Recently I played a lot with Clojure while building a civilizations simulator named civs. I really love building applications at the REPL and my Clojure code is much clearer and easier to read than the Java code I could have written for it


    when you need to refactor significantly your code it is going to be painful. Probably you can limit the issue with appropriate design choices and there is the possibility to use different forms of optional typing in Clojure. Still, you will miss a compiler helping you figure out what functions you need to revisit, because of your small change in a data structure. Sure, I should add different abstractions but…

    So, I spent some time trying to figure out which functional static typed language was worthy spending some time on, just to know better my options. I considered:

    OCaml: it seems that the adoption is declining more and more according to different metrics and the current compilers do not support multithread, which seems a capital sin.

    Scala: I have used it just to build an incremental parser for Java and I dislike it. It tries to be too many things at the same time and remember me C++, at the good old times when language designers taught that supporting more and more and more features in a language was a nice thing. That and the awful tool support made me decide that I do not want to touch it neither with a pole, unless forced too.

    Haskell: what else is left? SML?

    Now, as part of my job at TripAdvisor I am learning to be more and more practical and getting things done. I have already some investments done in the JVM environment. For example my world generator (lands) can be used through Jython on the JVM as well as the name generator (namegen) and a few other things that are written in Java or Clojure (no stuff left in JRuby, right?). So it would be great to have Haskell for the JVM…

    …and then is when Frege comes to the rescue! It is just that, a port of Haskell to the JVM.

    Basic Toolchain

    From the Github page you can download the jars you need. Visit the official repository of Frege.

    You can play with the REPL and when you want to compile & run:

    java -jar ~/tools/frege3.21.586-g026e8d7.jar main.fr
    java -cp ~/tools/frege3.21.586-g026e8d7.jar:. Main

    A Small Difference Between Haskell and Frege

    While in Haskell I would write:

    data Name = String | Unnamed
    deriving Show

    In Frege I need to write:

    data Name = String | Unnamed
    derive Show Name -- no spaces at the beginning of the line. Yes, it matters

    What derive means translated in Java? It means more or less I am too lazy to write the toString method for this class (Name) so please do the usual thing auto-magically and implement it for me.

    A First Example

    So I started to create a few types to run the civilization simulator in Frege:

    module Main where
    data Name = Name String | Unnamed
    derive Show Name
    data Position = Pos { x :: Int, y :: Int } 
    derive Show Position
    data Group = Group { name :: Name }
    derive Show Group
    data Game = Game { groups :: [Group] }
    derive Show Game
    gr1 = Group (Name "Italians")
    gr2 = Group (Name "French")
    gr3 = Group (Name "German")
    gr4 = Group (Name "Irish")
    g = Game [gr1, gr2, gr3, gr4]
    main :: IO ()
    main = do putStrLn "Civs (porting to Frege)"
              putStrLn "-----------------------"
              putStrLn (show g)

    Running the First Example

    Screenshot from 2014-09-20 14:32:50

    So far, Frege seems stable, easy to install and very close to Haskell. Now I want to play more with the Java integration.

    Download the guide with 68 resources on Creating Programming Languages

    Receive the guide to your inbox to read it on all your devices when you have time

    Powered by ConvertKit
    Creating a Programming Language

    Learn to Create Programming Languages

    Subscribe to our newsletter to get the FREE email course that teaches you how to create a programming language