First Order Haskell Neil Mitchell York University www.cs.york.ac.uk/~ndm λ.

Slides:



Advertisements
Similar presentations
The Art of Avoiding Work
Advertisements

Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)
Haskell and HaskellVV. What is Haskell? “Haskell is a polymorphically typed, lazy, pure functional language.” – So what.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Laziness and Infinite Datastructures Koen Lindström Claessen.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Lazy Evaluation Errors Evaluation Strategies Infinite Lists…. Lecture notes, exercises, etc., can be found.
Functional Programming Professor Yihjia Tsai Tamkang University.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Transformation and Analysis of Haskell Source Code Neil Mitchell 
Programovací jazyky F# a OCaml Chapter 5. Hiding recursion using function-as-values.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Going Functional Primož Gabrijelčič. Functional programming.
Compiling Functional Programs Mooly Sagiv Chapter 7
COMP313A Functional Programming (1)
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
A Staged Semantics of Overloading (preliminary) Bill Harrison & Tim Sheard.
CATCH 1 : Case and Termination Checking for Haskell Neil Mitchell (supervised by Colin Runciman) 1 Name courtesy of Mike Dodds.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
1 Testing in Haskell: using HUnit Notes, thanks to Mark P Jones Portland State University.
Recursion Higher Order Functions CSCE 314 Spring 2016.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Haskell With Go Faster Stripes Neil Mitchell. Catch: Project Overview Catch checks that a Haskell program won’t raise a pattern match error head [] =
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Programming Languages winter term 2010/11
Laziness and Infinite Datastructures
Conditional Expressions
Types CSCE 314 Spring 2016.
Koen Lindström Claessen
Sparkle a functional theorem prover
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Programming Languages and Compilers (CS 421)
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CSE 341 Section 3 Nick Mooney Spring 2017.
Yan Shi CS/SE 2630 Lecture Notes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

First Order Haskell Neil Mitchell York University λ

First order vs Higher order Higher order: functions are values – Can be passed around – Stored in data structure Harder for reasoning and analysis – More syntactic forms – Extra work for the analysis Can we convert automatically? – Yes (that’s this talk!)

Which are higher order? [not x | x ← xs] let xs = x:xs in xs putChar ‘a’ 1 \x → not x not. odd not $ odd x map not xs foldl (+) 0 xs a < b (+1) const ‘N’

Higher order features Type classes are implemented as dictionaries – (==) :: Eq a  a → a → Bool – (==) :: (a→a→Bool, a→a→Bool) → a → a → Bool Monads are higher order – (>>=) :: Monad m  m a → (a → m b) → m b IO is higher order – newtype IO a = IO (World → (World, a))

A map example map f [] = [] map f (x:xs)= f x : map f xs heads xs= map head xs head is passed higher order map takes a higher order argument heads could be first order

Reynold’s Style Defunctionalisation data Func= Head apply Head x= head x map f []= [] map f (x:xs)= apply f x : map f xs heads xs= map Head xs Move functions to data John C. Reynolds, Definitional Interpreters for Higher-Order Programming Languages

Reynold’s Style Defunctionalisation Good – Complete, works on all programs – Easy to implement Bad – No longer Hindley-Milner type correct – Makes the code more complex – Adds a level of indirection – Makes program analysis harder

Specialisation map_head []= [] map_head (x:xs)= head x : map_head xs heads xs= map_head xs Move functions to code

Specialisation Find: map head xs – A call to a function (i.e. map) – With an argument which is higher order (i.e. head) Generate: map_head xs = … – A new version of the function – With the higher order element frozen in Replace: map_head xs – Use the specialised version

Specialisation fails (.) f g x= f (g x) even= (.) not odd check x= even x Nothing available to specialise! Can be solved by a simple inline check x = (.) not odd x

An algorithm 1. Specialise as long as possible 2. Inline once 3. Goto 1 Stop when no higher order functions remain

Algorithm fails data Wrap a= Wrap (Wrap a) | Value a f x = f (Wrap x) check = f (Value head) In practice, this is rare – requires a function to be stored in a recursive data structure and … Detect, and revert to Reynold’s method

Code Size Specialisation approach reduces code volume – Average about 55% smaller code (20%-95% range) Mark Jones, Dictionary-free Overloading by Partial Evaluation

Current uses Performance optimiser – The first step, makes the remaining analysis simpler – Already increases the performance Analysis tool – Catch, checking for pattern match safety – Keeps the analysis simpler Implemented for Yhc (York Haskell Compiler)

Conclusion Higher order functions are good for programmers Analysis and transformation are simpler in a first order language Higher order functions can be removed Their removal can reduce code size