Questions tagged [frege]

Frege is a Haskell for the JVM. Like any Haskell, it is purely functional, enjoys a strong static type system with global type inference and non-strict - also known as lazy - evaluation.

Frege is a Haskell for the JVM.

Like any Haskell, it is purely functional, enjoys a strong static type system with global type inference and non-strict - also known as lazy - evaluation.

Frege compiles to Java, runs on the JVM, and uses any Java library you want. It can be used inside any Java project.

A Taste of Frege

Hello World

This is the classic starter with a slight extension to show the fluent usage from Java and the benefits of having a type system that can recognize purity.

module Hello where

greeting friend = "Hello, " ++ friend ++ "!"

main args = do
    println (greeting "World")

This code will compile to Hello.class and Hello.java with a regular Java main method that one can start the usual Java way.

Moreover, the Hello.class will have a method

public static String greeting(String ...) {...}

that one can call from Java or any other JVM language.

The greeting function is pure, meaning it is stateless and free of side effects. Therefore, it is threadsafe and its results may be automatically cached since given the same argument, the result will always be the same.

The main function is impure. It takes a list of Strings and does not return just "void" as in most other JVM languages but the type IO (), telling that it may produce side effects like printing to the console. The Frege type system guarantees that any caller of main must also be of some IO type and is thus also marked as impure. That way, the lack of purity percolates up the whole call chain.

"Hello World" already shows the tenet of "islands of purity" (greeting) in a "sea of imperative code" (main).

Since the purity information is carried through the type system, the compiler can potentially use it for many optimizations such as pre-calculation, deferred execution, parallel execution, caching, and elimination of common subexpressions.

Useful Links

  1. Getting Started
  2. Try Frege right from your browser
  3. Interoperability:
  4. Differences between Frege and Haskell
  5. IDE Support
  6. Build tools
107 questions
2
votes
1 answer

Warning on native mutable field

What does this warning mean? Is there any way we can avoid this warning? I tried to understand the message from the compiler code here but I couldn't. frege> native sysin "java.lang.System.in" :: InputStream native function sysin :: InputStream 3:…
Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
2
votes
2 answers

How to map Java overloaded constructors to Frege functions

Java (unfortunately) supports constructors and methods overload. For example, the HashMap has four constructors. In Frege I can't do: data Map = native java.util.Map data HashMap = native java.util.HashMap where native new :: () -> STMutable s…
mariop
  • 3,195
  • 1
  • 19
  • 29
2
votes
1 answer

Frege: can I derive "Show" for a recursive type?

I'm trying to implement the classical tree structure in frege, which works nicely as long as I don't use "derive": data Tree a = Node a (Tree a) (Tree a) | Empty derive Show Tree gives me realworld/chapter3/E_Recursive_Types.fr:7: kind…
Dierk
  • 1,308
  • 7
  • 13
2
votes
2 answers

Is there a systematic upper bound to evaluation in Frege?

Just out of curiosity, I tried this code in Frege: println (mydrop 30000000 [1..30000001]) It goes without saying that a sequence of 30 million entries is kind of silly and I would have been ok with an OOME. I wanted to see whether lazy evaluation…
Dierk
  • 1,308
  • 7
  • 13
2
votes
1 answer

how to use multiple inline assertions in Frege

for the sake of self-checking examples, I got the following code running: assert :: Bool -> Bool -> String -> IO () assert actual expected description | expected == actual = do { print "" } -- need a better way to do nothing |…
Dierk
  • 1,308
  • 7
  • 13
2
votes
1 answer

what is the Frege equivalent to Haskell's "interact" function?

I try to get the word-count example from real-world Haskell running in Frege: main _ = interact wordCount where wordCount input = show (length (lines input)) ++ "\n" but I get can't resolve `interact` Is there a Frege-idiomatic way to do this?
Dierk
  • 1,308
  • 7
  • 13
2
votes
1 answer

Can I use floating point enums in Frege?

I tried println [1.0, 1.25..2.0] but got Double is not an instance of Enum I couldn't find this issue in the "differences to Haskell", though. Is there a recommended alternative?
Dierk
  • 1,308
  • 7
  • 13
2
votes
1 answer

How do I make the power function, doubles and [..] working together in frege?

While map (\x -> x * x) [0..9] is working fine (also list comprehension), I cannot do map (** 2) [0..9] since the power operator requires doubles and the .. operator does not allow them. Is there some mapping that I can use?
Dierk
  • 1,308
  • 7
  • 13
2
votes
2 answers

Calling native Java constructors from Frege

Do you know where I can find documentation about Frege's Java bindings? Coming from Haskell, I find that the most interesting aspect of Frege. The documentation that I found unfortunately does not go into detail very much. Here is my test example.…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
2
votes
2 answers

How can I import different modules in frege

Can someone help me with frege equivalent of import command in haskell for Data. I wish to access Data.Typeable or Data.Time.Calendar.....and such modules/inbuilt procedures. Do we have a way to know the available modules in frege.....like in…
skg
  • 23
  • 3
1
vote
1 answer

“Return type is incompatible” / Bad native declaration module error on applicative instance

I’m reading the book "Haskell Programming from first principles" by Christopher Allen and Julie Moronuki and try to implement the code and examples in Frege. Unfortunately, I run into compile errors on module level (in the eclipse editor) for the…
Damian
  • 13
  • 3
1
vote
1 answer

What is the motivation behind the "let definition is a constant" hint?

With -hints option on, the compiler emits a hint message against the following program: module Main where main :: IO () main = do let magic :: Int -- (A) magic = 123 println magic . $ fregec -hints Main.fr H Main.fr:5: let…
Ohashi
  • 397
  • 3
  • 14
1
vote
2 answers

Why does Foldable inherit from Functor in Frege?

In Haskell, the class Foldable is standalone i.e. class Foldable t where ... However, in Frege: class Functor t => Foldable t where ... Why was this constraint introduced? What ill-formed programs/ideas can it rule out? It prevented me from…
Ohashi
  • 397
  • 3
  • 14
1
vote
1 answer

How to define multiple patterns in Frege?

I'm having some trouble defining a function in Frege that uses multiple patterns. Basically, I'm defining a mapping by iterating through a list of tuples. I've simplified it down to the following: foo :: a -> [(a, b)] -> b foo _ [] = [] --nothing…
JJ Brown
  • 543
  • 6
  • 13
1
vote
0 answers

Compiling other JVM languages in Android Studio

I'm trying to write Android applications in Frege and I currently have the gradle compiling the project after evaluation like this: project.afterEvaluate { extensions.compileFrege = { description = 'Compile Frege to Java' …
user3574294
  • 199
  • 1
  • 7