Frege (programming language)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Frege
Frege logo.svg
Paradigm(s) functional, lazy/non-strict
Designed by Ingo Wechsung
Appeared in 2011; 3 years ago (2011)
Stable release 3.22 / November 2013; 10 months ago (2013-11)
Typing discipline static, strong, inferred
Influenced by Haskell
OS Cross-platform
License BSD
Filename extension(s) .fr
Website github.com/Frege/frege

Frege is a non-strict, pure functional programming language in the spirit of Haskell. It enjoys a strong static type system with type inference. Higher rank types are supported, though type annotations are required for that.[1] The programming language is named after the German mathematician, logician and philosopher Gottlob Frege. While it is not a Haskell dialect, some effort has been put into making Frege look and feel like Haskell.[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.

Frege authoring is attributable to Ingo Wechsung.[2]

The Frege programming language is unrelated to the Frege Program Prover.

Comparison with Haskell[edit]

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[edit]

-- 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[edit]

Frege requires Java-7 JDK to compile and run.

At the console[edit]

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 GNU/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[edit]

There is a plug-in for Eclipse with instructions given at How-to EclipseFregIDE.

More involved examples[edit]

Records[edit]

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[edit]

{-- 
    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

References[edit]

External links[edit]

In depth[edit]