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

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

Die funktionale Zukunft

JVM ByteCode Interpreter

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

CP122 CS I. Iteration

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

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

Kickstart Intro to Java Part I

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

CS457/557 Functional Languages

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

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

Introduction to Functional Programming in Haskell 1 / 56

Programming Languages and Techniques (CIS120)

Introduction to Functional Programming and Haskell. Aden Seaman

Refactoring to Functional. Hadi Hariri

Concepts of Programming Languages

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

CS 11 Haskell track: lecture 1

Program Fundamentals

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

CS 231 Data Structures and Algorithms, Fall 2016

Loops. CSE 114, Computer Science 1 Stony Brook University

Grafting Functional Support on Top of an Imperative Language

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

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

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Programming Languages and Techniques (CIS120)

Look at all these toys!

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

Course Outline. Introduction to java

Programming language design and analysis

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

G Programming Languages - Fall 2012

CIS133J. Working with Numbers in Java

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

Rust for high level applications. Lise Henry

AP Computer Science Unit 1. Programs

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

CSCE 314 Programming Languages

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

Functional Programming Lecture 13: FP in the Real World

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

Cross-Platform, Functional, Reactive GUIs

Declarative concurrency. March 3, 2014

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

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

CS111: PROGRAMMING LANGUAGE II

Side Effects (3A) Young Won Lim 1/13/18

Computational Expression

Anonymous Functions CMSC 330: Organization of Programming Languages

Manchester University Transactions for Scala

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Lecture 6 Introduction to Objects and Classes

An overview of Java, Data types and variables

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

CS558 Programming Languages

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

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

Functional Programming

13 th Windsor Regional Secondary School Computer Programming Competition

Introduction to the Java Basics: Control Flow Statements

Principles of Programming Languages

Lecture 5: Methods CS2301

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010

PROGRAMMING FUNDAMENTALS

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

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

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Ruby: Introduction, Basics

Programming Languages and Techniques (CIS120)

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

An introduction introduction to functional functional programming programming using usin Haskell

CS558 Programming Languages

Intro to Haskell Notes: Part 5

University of Palestine. Mid Exam Total Grade: 100

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

OBJECT ORIENTED PROGRAMMING

Computer Science 1 Honors

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

Another IS-A Relationship

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

Welcome to. Instructor Marc Pomplun CS 470/670. Introduction to Artificial Intelligence 1/26/2016. Spring Selectivity in Complex Scenes

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

You just received an alien message from outer space. What does the first page mean?

Presented By Andrew Butt

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Composable Shared Memory Transactions Lecture 20-2

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Contents. 8-1 Copyright (c) N. Afshartous

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

CPS 506 Comparative Programming Languages. Programming Language

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

Transcription:

Frege purely functional programming on the JVM JUG Luzern 2016

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> six x = twotimes (threetimes x) frege> six x = (twotimes. threetimes)x frege> six = twotimes. threetimes frege> six 2 12

Function Composition frege> f(g(x)) six x = twotimes (threetimes x) frege> (f six x = (twotimes. threetimes)x g) x frege> f six g = twotimes. threetimes frege> six 2 12

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 Prerequisite to safe concurrency and deterministic parallelism!

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.

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 for clarity.

Pure Transactions

Type inference FTW

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 purity) Purity by default effects are explicit in the type system Type-safe concurrency & parallelism 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 FP matters Enabling incremental development www.canoo.com/blog/fp1 Brush up computational fundamentals An investment in knowledge always pays the best interest. Benjamin Franklin

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) Yes, STM is almost finished.

Unique in Frege Global type inference (requires purity) Purity by default effects are explicit in the type system Type-safe concurrency & parallelism Laziness by default Values are always immutable Guarantees extend into Java calls