Frege (programming language)
Paradigm | functional, lazy/non-strict |
---|---|
Designed by | Ingo Wechsung |
First appeared | 2011 |
Stable release |
3.24
/ 12 March 2016 |
Typing discipline | static, strong, inferred |
OS | Cross-platform |
License | BSD |
Filename extensions | .fr |
Website |
github |
Influenced by | |
Haskell |
Frege is a non-strict, purely functional programming language for the Java virtual machine in the spirit of Haskell.
It is considered a Haskell dialect or simply "a" Haskell for the Java virtual machine.[1]
Frege has a strong static type system with type inference. Higher rank types are supported, though type annotations are required for that.[1] Frege programs are compiled to Java bytecode and run in a Java virtual machine. Existing Java classes and methods can be used seamlessly from Frege after their types have been properly declared.
The language was designed by Ingo Wechsung,[2] who named it after the German mathematician, logician and philosopher Gottlob Frege.
(Unrelated to the Frege Program Prover).
Comparison with Haskell
A summary of differences between Frege and Haskell is listed at the Differences between Frege and Haskell.
The type String is custom defined as an interface with Java strings. String (++) is bound to Java's String (+).[3] Conversion functions to Haskell correspondent:
packed :: [Char] -> String
unpacked :: String -> [Char]
Literals:
-- boolean literals true false are not capitalized
Frege's Monad class does not include the method fail, included in a separate class MonadFail.[4]
Numeric classes for floating point types are also different. Haskell's classes Fractional, RealFrac, RealFloat and Floating are not defined. Haskell's class Real defines toRational while Frege's defines (/):[3]
class Real (Num r) => r where -- classname precedes context
--- the division operator
(/) :: r -> r -> r
Hello World program
-- file hello.fr
module Hello where -- moduleName maybe hierarchical as pkgdir.JavaClassname
main args = println $ "Hello world! your arguments are: " ++ show args
Compiling Frege programs
Frege requires Java-7 JDK or higher to compile and run.
At the console
As the "Getting started" page states,[5] to compile it:
$ mkdir classes
$ java -Xss1m -jar ${install_dir}/fregec.jar -d classes src/hello.fr
This assumes the downloaded frege3.xx.vvv.jar has been renamed to fregec.jar for ease of use.
To run the compiled program specify the package name as start class. On Linux and other Unix systems:
$ java -cp classes:${install_dir}/fregec.jar Hello arg1 arg2
Hello world! your arguments are: ["arg1", "arg2"]
On Microsoft Windows the classpath separator has to be changed to ';'
At the Eclipse devel. environment
There is a plug-in for Eclipse with instructions given at How-to EclipseFregIDE.
More involved examples
Records
data Person = P { name :: String, birthyear :: Int }
frege = P "Gottlob Frege" 1848
smith = P { birthyear = 1990, name = "Joe Smith" }
-- tell if first person is older than second
older :: Person -> Person -> Bool
older P{birthyear} p2 = birthyear < p2.birthyear
main _ = println (frege `older` smith) -- prints true
Unlike in Haskell, the record fields do not appear in the global namespace. Thus it is possible to reuse the same field name in different types. The accessor functions in the example are known as Person.name and Person.birthyear
The record syntax is really syntactic sugar, and the associated data constructor can be used with traditional or record syntax. The same holds for patterns.
Record pattern syntax allows to check for a given constructor and check specific fields or bind them to local variables. This makes patterns independent from the number and order of fields in a constructor.
Using Java classes and methods
{--
This program displays the
current time on standard output
every other second.
-}
module examples.CommandLineClock where
data Date = native java.util.Date where
native new :: () -> IO (MutableIO Date) -- new Date()
native toString :: Mutable s Date -> ST s String -- d.toString()
--- 'IO' action to give us the current time as 'String'
current :: IO String
current = do
d <- Date.new () -- reads system timer, hence IO
d.toString
main args =
forever do
current >>= print -- print formatted date
print "\r" -- followed by carriage return
stdout.flush -- make sure it's shown
Thread.sleep 999L -- wait 0.999 seconds