111 points · by reirob · 11y ago · 114 comments
There is a significant portion of the Scala community which desperately wants Scala to be the Haskell they can get their bosses buy in for. IMO the result is unsatisfactory, both for those people and the Scala community as a whole.
If a straight Haskell variant in the JVM ecosystem were to "take off" then Scala could be its own thing.
And when you use just regular Scala syntax, it's just pretty ugly code overall IMO (and one of the slowest compilers of the 21st century too).
Most scala users have no f-ing clue what a monad is :)
Indeed I often program in a way that could be called "locally stateful, globally pure-functional". It's a good approach!
If you want Java you know where to find it. Likewise with Haskell. Scala draws from both and that's its greatest strength.
If you spend a bit of time there it fades to background noise and #scala is actually a really friendly and helpful place - but "x is hard to do in Scala because Scala [sucks]" is an odd impression to give a newcomer.
I would love if I could avoid this with Fredge. But, in the end, I would probably be better off using Haskell directly.
This is certainly true. You can see Frege as a subset of Haskell 2010 plus some GHC extensions plus the native interface (i.e. the Java FFI).
Unless you really need the JVM, Haskell (i.e. GHC) is the probably the better choice.
Frege is just an offer for the minority that wants pure FP specifically on the JVM. Those people had no real choice until recently, given that CAL is dead, and E. Kmett's "ermine" not yet there.
As long as you are only bringing things in from Java-land, rather than going the other way though, the wrapping can be fairly minimal.
"Wrapping mutable Java stuff in Frege means that everything will end up in an ST monad, which is almost always IO. That means that using Java’s HashSet will force anything that uses that to be in the IO monad itself. So there goes purity and referential transparency, which are some of the best parts of Haskell in the first place."
[0] https://www.reddit.com/r/haskell/comments/3gr7y6/infoq_frege...
[1] http://taylor.fausak.me/2015/06/25/frege-a-jvm-haskell/
EDIT: formatting
This is a bit misleading though I think, as you can actually unwrap and ST unlike an IO - my most common use for ST is actually to use a mutable object in a case where it's inefficient to keep copying immutable (e.g.: large vector or matrix manipulations) - then when you're done the ST monad can be unwrapped and you get the final structure - the entire function remains pure but inside of it, there are calculations that rely on state - but no state enters or leaves the function per say. This would mean using a hash set would be out of the question _for passing around outside of an ST monad_ but you could actually use it internally within functions.
Yes, but this is what the author was saying: you end up writing your code in ST. Remeber, there is no (general) way to freeze a mutable collection that was passed and then use it as if it was immutable. There can be a dozen different threads be busy modifying it.
However, as immutable objects are becoming more mainstream even in Java, there will still plenty of opportunities to make polyglot JVM programs employing pure code. Just not with the JAVA collection classes.
-- int frob(int, char*)
foreign import ccall "frob"
frob :: Int -> CString -> IO Int
If you know such a function to be pure, you can use unsafePerformIO to tell the type system: refrob :: Int -> CString -> Int
refrob x y = unsafePerformIO (frob x y)
If you lie to the type system and tell it that an impure function is pure, then all bets are off.Better yet, if you know it to be pure, you can give it an appropriate type and just use it in pure code, like:
pure native cos java.lang.Math.cos :: Double -> Double
which should be roughly equivalent to: foreign import jvm "java.lang.Math.cos" cos :: Double -> Double
except that no Haskell compiler that I know implements the "jvm" calling convention, for obvious reasons.In fact, all primitive operations, types and so on are defined this way in the Frege Prelude.
But it is also not as relevant as one might think.
Consider how many Haskell programs actually use mutable C data, or export functions that take a foreign ptr to some mutable stuff.
Why should this be generally different in Frege? Useful Java APIs will be wrapped and sanitized through Frege libraries, and that is it then.
You can go directly to Java, just like you can directly go to C in Haskell, but it turns out one rarely actually does this.
The different syntax should not be a big issue, as Haskell code that uses FFI targeting C is per se not portable.
[0] https://github.com/Frege/frege-native-gen/issues/14 [1] https://github.com/Frege/frege/wiki/Differences-between-Freg...
http://mmhelloworld.github.io/blog/2013/07/10/frege-hello-ja...
current :: IO String
current = ...
(yeah, it's a minor issue compared to the uber-annoying problem of 'name clashes in record fields' https://wiki.haskell.org/Name_clashes_in_record_fields ...but still)In addition since Haskell supports multiple case defitions at the top level inline type declarations would be redundant. For example -
current a 0 :: IO Int Int -> Int current 0 b :: IO Int Int -> Int current 0 0 :: IO Int Int -> Int
vs.
current :: IO Int Int -> Int current 0 a = ... current a 0 = ... current 0 0 = ...
current :: IO String : = do
d <- Date.new () -- reads system timer, hence IO
d.toString
...or for something that works better with a really huge type signature or list of arguments, you could extend it like this myFunctionFoo :: Int -> Int -> Int -> Int
: a b c =
2*a + 3*b + 4*cHow would you make your first variant, work with pattern matching?
I like the second variant better, because it seems to work with pattern matching. It would as well have to work without type declarations, letting Haskell infer the types.
But all in all I prefer the way function definition is implemented now. The record issue is more of a trouble - for it Frege actually has some improvements.
Monoid: A + B (|+|)
Functor: M[A] => (A => B) => M[B]
Monad: M[A] => (A => M[B]) => M[B]
Applicative: M[A] => M[A=>B] => M[B]
Kleisli: (A => M[B]) => (B => M[C]) => (A => M[C])Am I correct in thinking that Frege cannot leverage on third-party java libraries? Clojure, another functional language built on JVM, has great support for this (possibly) missing feature.
The article at https://mmhelloworld.github.io/blog/2013/07/10/frege-hello-j... talks about this, but doesn't quite explain the proxy mechanism, it just points out an example.
Not quite true anymore. TO be sure, some (inline) java will still be needed.
Here is an example https://github.com/Frege/frege/blob/master/tests/comp/Issue2... which compiles to a class that implements java.util.Comparator. Instances thereof can be created from Frege by passing a custom comparision function and could be passed to Java.
The example would be useful in cases when you have Frege data in a Java collection and want to sort them. But it is merely there to show the possibilities.
Higher rank types
but says nothing about higher-kinded types (HKTs). The two are different.
The rank of a type describes the depth at which universal quantifiers appear contravariantly. This is quite different from higher-kinded types, which allow type-level computation. For good monad support one uses HKTs. I wonder if Frege has HKTs and the description on the original page made a mistake?Somewhere it is said that it has all language features of Haskell 2010. This implies higher kinded types.
But in addition to Haskell 2010, Frege has also higher rank types.
Type inference for types of rank > 2 is undecidable, how does this go together with the claim that Frege has type inference? I'm not a Haskell expert, but I think the
{-# LANGUAGE RankNTypes #-}
extension enables HRTs in Haskell too.Actually, the Frege compiler employs an algorithm described in Simon Peyton-Jones paper "Practical Type Inference for Higher Ranked Types". Ordinary HM types are inferred, and higher ranked types checked.
Regarding the pronounciation, who cares? For example, in Germany, half of the people say "Ay-Bee-Em", the other half pronounce the letters IBM in the german way.
Here is a paragraph from "Funktion und Begriff" (1891):
> Wie nun Funktionen von Gegenständen grundverschieden sind, so sind auch Funktionen, deren Argumente Funktionen sind und sein müssen, grundverschieden von Funktionen, deren Argumente Gegenstände sind und nichts anderes sein können. Diese nenne ich Funktionen erster, jene Funktionen zweiter Stufe.
For non-german speakers: Frege makes a distinction between functions that take things as arguments and functions whose argument are and must be functions. He calls the former ones "first order functions" and the latter ones "second order functions".
Today we call functions whose order is greater one "higher order".
It shows with :java the produced Java code.
The short answer is that it is not derived from existing Haskell compilers.
If you had a Frege -> Haskell translator, which shouldn't be hard to do, except in edge cases, you could use GHC as a testing oracle for the Frege compiler.
The difficulties are clearly stated in the Hasell wiki here https://wiki.haskell.org/GHC:FAQ#Why_isn.27t_GHC_available_f...
main = putStrLn "\n"
Yes, that was very difficult. I see where you're coming from.