Frege. purely functional programming on the JVM. GOTO Berlin 2015

Similar documents
Frege. purely functional programming on the JVM. JUG Luzern 2016

Die funktionale Zukunft

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

CP122 CS I. Iteration

JVM ByteCode Interpreter

Background Type Classes (1B) Young Won Lim 6/28/18

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Introduction to Functional Programming and Haskell. Aden Seaman

Programming Languages and Techniques (CIS120)

CS 11 Haskell track: lecture 1

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Kickstart Intro to Java Part I

Look at all these toys!

Introduction to Functional Programming in Haskell 1 / 56

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

CS457/557 Functional Languages

An overview of Java, Data types and variables

CSC324 Principles of Programming Languages

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Refactoring to Functional. Hadi Hariri

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

Concepts of Programming Languages

G Programming Languages - Fall 2012

AP Computer Science Unit 1. Programs

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

Loops. CSE 114, Computer Science 1 Stony Brook University

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Program Fundamentals

CSCE 314 Programming Languages

Just Enough Eclipse What is Eclipse(TM)? Why is it important? What is this tutorial about?

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Type-indexed functions in Generic Haskell

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Functional Programming Lecture 13: FP in the Real World

Programming Language Pragmatics

Programming Languages and Techniques (CIS120)

Lecture 6 Introduction to Objects and Classes

Computational Expression

Anonymous Functions CMSC 330: Organization of Programming Languages

Manchester University Transactions for Scala

Programming with Math and Logic

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

CS558 Programming Languages

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Haskell Monads CSC 131. Kim Bruce

Compiler construction 2009

Imperative languages

CS558 Programming Languages

Juniper: A Functional Reactive Programming Language for the Arduino

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Rust for high level applications. Lise Henry

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

13 th Windsor Regional Secondary School Computer Programming Competition

Lecture 5: Methods CS2301

CIS133J. Working with Numbers in Java

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CMSC 330: Organization of Programming Languages

CSCI-GA Scripting Languages

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Grafting Functional Support on Top of an Imperative Language

Object-oriented programming in...

Control Flow. Stephen A. Edwards. Fall Columbia University

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Ruby: Introduction, Basics

Course Outline. Introduction to java

What is Needle? The Needle Programming Language Neel Krishnaswami

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

University of Palestine. Mid Exam Total Grade: 100

Fall 2017 CISC124 9/16/2017

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

Computer Science 1 Honors

CPS 506 Comparative Programming Languages. Programming Language

CS 231 Data Structures and Algorithms, Fall 2016

OBJECT ORIENTED PROGRAMMING

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS111: PROGRAMMING LANGUAGE II

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

An introduction introduction to functional functional programming programming using usin Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Programming language design and analysis

n n Official Scala website n Scala API n

COS 326 David Walker. Thanks to Kathleen Fisher and recursively to Simon Peyton Jones for much of the content of these slides.

Transcription:

Frege purely functional programming on the JVM GOTO Berlin 2015

Dierk König canoo mittie

Dreaming of code

Why do we care? a = 1 1 b = 2 1 2 time 1 c = b 1 2 time 2 b = a 1 2 time 3 a = c 1 2 place1 place2 place3

Operational Reasoning a = 1 1 b = 2 1 2 time 1 c = b 1 2 2 time 2 b = a 1 1 2 time 3 a = c 2 1 2 place1 place2 place3

Using functions a = 1 1 b = 2 1 2

Using functions a = 1 1 b = 2 1 2 2 1 swap(a,b) = (b,a)

Let s just program without assignments or statements!

Pure Developer Discipline Functional Language

Online REPL try.frege-lang.org

Define a Function frege> times a b = a * b frege> times 2 3 6 frege> :type times Num α => α -> α -> α

Define a Function frege> times a b = a * b no types declared frege>(times 2)3 6 function appl. left associative no comma frege> :type times Num α => α ->(α -> α) typeclass only 1 return type is constraint parameter! a function! thumb: two params of same numeric type returning that type

Reference a Function frege> twotimes = times 2 frege> twotimes 3 6 frege> :t twotimes Int -> Int

Reference a Function frege> twotimes x = times 2 x No second arg! frege> twotimes 3 6 frege> :t twotimes Int -> Int Currying, schönfinkeling, or partial function application. Concept invented by Gottlob Frege. inferred types are more specific

Function Composition frege> twotimes (threetimes 2) 12 frege> sixtimes = twotimes. threetimes frege> sixtimes 2 frege> :t sixtimes Int -> Int

Function Composition frege> twotimes (threetimes 2) f(g(x)) 12 frege> sixtimes = twotimes. threetimes frege> sixtimes 2 (f g) x frege> :t sixtimes Int -> Int

Pure Functions Java T foo(pair<t,u> p) { } What could possibly happen? Frege foo :: (α,β) -> α What could possibly happen?

Pure Functions Java Everything! T foo(pair<t,u> p) { } Frege State changes, file or db access, missile launch, foo :: (α,β) -> α a is returned

Pure Functions can be cached (memoized) can be evaluated lazily can be evaluated in advance can be evaluated concurrently can be eliminated in common subexpressions can be optimized

Is my method pure?

Java Interoperability Do not mix OO and FP, combine them!

Java -> Frege Frege compiles Haskell to Java source and byte code. Just call that. You can get help by using the :java command in the REPL.

Frege -> Java pure native encode java.net.urlencoder.encode :: String -> String encode Dierk König native millis java.lang.system.currenttimemillis :: () -> IO Long millis () millis () past = millis () - 1000 even Java can be pure This is a key distinction between Frege and other JVM languages! Does not compile!

Frege allows calling Java but never unprotected! is explicit about effects just like Haskell

Type System Global type inference More safety and less work for the programmer You don t need to specify any types at all! But sometimes you do anyway

Keep the mess out! Mutable Pure Computation Mutable I/O Pure Computation Mutable Pure Computation

Keep the mess out! Mutable Mutable I/O Pure Computation Pure Computation Mutable Pure Computation Threadsafe by design! Checked by compiler Ok, these are Monads. Be brave. Think of them as contexts that the type system propagates and makes un-escapable.

Fizzbuzz http://c2.com/cgi/wiki?fizzbuzztest https://dierk.gitbooks.io/fregegoodness/ chapter 8 FizzBuzz

Fizzbuzz Imperative public class FizzBuzz{ 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); } } } }

Fizzbuzz Logical fizzes = cycle ["", "", "fizz"] buzzes = cycle ["", "", "", "", "buzz"] pattern = zipwith (++) fizzes buzzes numbers = map show [1..] fizzbuzz = zipwith max pattern numbers main _ = for (take 100 fizzbuzz) println

Fizzbuzz Comparison Imperative Logical Conditionals 4 0 Operators 7 1 Nesting level 3 0 Sequencing sensitive transparent Maintainability - - - + Incremental development - +++

Unique in Frege Global type inference requires a purely functional language (only expressions and parametric polymorphism) Purity by default effects are explicit in the type system Laziness by default Values are always immutable Guarantees extend into Java calls

Why Frege Robustness under parallel execution Robustness under composition Robustness under increments Robustness under refactoring Enables local and equational reasoning Best way to learn FP

Why Frege it is just a pleasure to work with

How? http://www.frege-lang.org @fregelang stackoverflow frege tag edx FP101 MOOC

Dierk König canoo Please give feedback! mittie

FGA Language level is Haskell Report 2010. Yes, performance is roughly ~ Java. Yes, the compiler is reasonably fast. Yes, we have an Eclipse Plugin. Yes, Maven/Gradle/etc. integration. Yes, we have HAMT (aka HashMap). Yes, we have QuickCheck (+shrinking) No, but STM is in the works.