Frege is a Haskell for the JVM

10
Frege https://github.com/Frege/frege Frege is a Haskell for the JVM

Transcript of Frege is a Haskell for the JVM

Page 1: Frege is a Haskell for the JVM

Fregehttps://github.com/Frege/frege

Frege is a Haskell for the JVM

Page 2: Frege is a Haskell for the JVM

FizzBuzz samplepublic class FizzBuzz { // IMPERATIVE public static void main(String[] args) { for (int i = 1; i <= 100; i++) { if (i % 15 == 0) { System.out.println("FizzBuzz"); } else if (i % 3 == 0) { System.out.println("Fizz"); } else if (i % 5 == 0) { System.out.println("Buzz"); } else { System.out.println(i); }}}}

2

module FizzBuzz where // FUNCTIONAL

fizzes = cycle ["", "", "fizz"] -- cycle :: [a] -> [a] buzzes = cycle ["", "", "", "", "buzz"] pattern = zipWith (++) fizzes buzzes -- zipWith:: (a -> b -> c) -> [a] -> [b] -> [c] numbers = map show [1..] -- map :: (a -> b) -> [a] -> [b] fizzbuzz = zipWith max pattern numbers -- max :: Ord a => a -> a -> a main _ = do -- main :: [String] -> IO for (take 100 fizzbuzz) println -- take :: [Int] -> [a] -> [a]

http://c2.com/cgi/wiki?FizzBuzzTest

Page 3: Frege is a Haskell for the JVM

FizzBuzz complexity

3

Imperative Functional

Conditionals 4 0

Operators 4 1

Nesting Level (3 zur Info) 0

McCabe:M= 8 1

https://de.wikipedia.org/wiki/McCabe-Metrik

Page 4: Frege is a Haskell for the JVM

Frege Syntax (Function)--‘=‘ is a mathematical operator ( NO ASSIGNMENT ) one = 1 -- ‘public static Integer one = 1;‘ two = 2 -- ‘public static Integer two = 2;‘ oneTwoTuple = (one, two) -- ‘()‘ tuple constructor assertTuple = oneTwoTuple == (1,2) -- ‘True‘

oneTwoList = [one, two] -- ‘[]‘ list constructor assertList = oneTwoList == [1,2] -- ‘True‘ -- aset is a unique list aset list = unique list -- type: ‘aset:: (Eq a) => [a] -> [a]‘ -- amap is a list of (key,value) tuples amap keys values = zip keys values -- type: ‘amap :: [a] -> [b] -> [(a,b)]‘ amap2 f values = zip (map f values) values -- type: ‘amap2:: (b -> a) -> [b] -> [(a,b)]‘

unique [] = [] -- type: ‘unique:: (Eq a) => [a] -> [a]‘ unique (x:xs) | elem x xs = unique xs | otherwise = x : unique xs

naturals = [1..] -- type: ‘naturals::[Int]‘=[1,2,..,Int.MAX] evens = [ x | x <- [1..], even x] -- type: ‘evens ::[Int]‘=[1,3,..,Int.MAX-1]

4https://dierk.gitbooks.io/fregegoodness/

Page 5: Frege is a Haskell for the JVM

Frege Syntax (Types)-- interface Interface<A> {...} class Interface a where methode :: a -> a -> Bool -- interface DomainInterface {...} data DomainInterface = DomainIntImpl { int::Int } --class DomainIntImpl implements DomainInterface{ } | DomainStringImpl{string::String}--class DomainStrImpl implements DomainInterface{ }

derive Show DomainInterface --class DomainIntImpl{ @Overwrite public String toString() } --class DomainStringImpl{@Overwrite public String toString() } instance Interface DomainInterface where methode (DomainIntImpl {int=a}) (DomainIntImpl {int=b}) = a == b methode (DomainStringImpl{string=a}) (DomainStringImpl {string=b}) = a == b methode _ _ = False -- class DomainIntImpl { @Overwrite public String methode(a,b)} -- class DomainStringImpl { @Overwrite public String methode(a,b)} ints = map DomainIntImpl [1..10] -- [ 1 , 2 ,.., 10 ] strings = map DomainStringImpl $ map show [1..10] -- [“1“,“2“,..,“10“]

find_1_Int = head $ map (DomainIntImpl 1).methode ints -- True find_1_String = head $ map (DomainStringImpl "1").methode strings -- True

5https://en.wikibooks.org/wiki/Haskell/Classes_and_types

Page 6: Frege is a Haskell for the JVM

Frege Syntax (Monad)main _ = do // FUNCTIONAL

url <- URL.new "http://www.google.com" >>= either throw return ios <- url.openStream >>= either throw return rdr <- InputStreamReader.new ios "UTF-8" >>= either throw return bfr <- BufferedReader.new rdr >>= either throw return maybe <- bfr.readLine >>= println ios.close return()

6

public static void main(String[] args) { // IMPERATIVE final URL url; try { url = new URL("http://www.google.com"); } catch (MalformedURLException e) { throw new RuntimeException(e);} final InputStream ios; try { ios = url.openStream(); } catch (IOException e) {throw new RuntimeException(e);} InputStreamReader rdr = new InputStreamReader(ios); BufferedReader bfr = new BufferedReader(rdr); String next = null; try { while((next = bfr.readLine()) != null){ System.out.println(next); } } catch (IOException e) {throw new RuntimeException(e);} try { ios.close(); } catch (IOException e) {throw new RuntimeException(e);} }

http://www.learnyouahaskell.com/a-fistful-of-monads

Page 7: Frege is a Haskell for the JVM

Frege PROs

7

• Eleganz

• Pure Functional

• Thread Safe by Design

• Global Type inference

• Mvn, Gradle tooling exist

• Hoogle

http://de.slideshare.net/Mittie/frege-tutorial-at-javaone-2015

Page 8: Frege is a Haskell for the JVM

Frege CONs

8

• IDE (only eclipse :( )

• Compiler errors messages

• Debugging

• Laziness vs Performance

• Missing libraries

Page 9: Frege is a Haskell for the JVM

Blue or red pill?

9

Page 10: Frege is a Haskell for the JVM

Links• GitHub: https://github.com/Frege

• Language: http://www.frege-lang.org/doc/Language.pdf

• Types: https://en.wikibooks.org/wiki/Haskell/Classes_and_types

• Monad: http://www.learnyouahaskell.com/a-fistful-of-monads

• EBook: https://dierk.gitbooks.io/fregegoodness/

• Video: http://www.yt.fully.pk/watch/1P1-HXNfFPc#

10