Programming in Haskell

Similar documents
Lecture 5: Lazy Evaluation and Infinite Data Structures

Urmas Tamm Tartu 2013

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Unboxing Sum Types. Johan Tibell - FP-Syd

Static Analysis and Code Optimizations in Glasgow Haskell Compiler

Lexical vs. Dynamic Scope

Once Upon a Polymorphic Type

Parallel & Concurrent Haskell 1: Basic Pure Parallelism. Simon Marlow (Microsoft Research, Cambridge, UK)

Parallel Functional Programming Lecture 1. John Hughes

Streams and Evalutation Strategies

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

Polymorphic Contexts FP-Dag 2015

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Concepts of programming languages

Parallelism with Haskell

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Fastest Lambda First. Neil Mitchell.

Parameter Passing Styles

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

Concepts of program design Exam January 31, 13:30 16:30

JVM ByteCode Interpreter

CITS3211 FUNCTIONAL PROGRAMMING. 13. Interpretation

Concurrent and Multicore Haskell

A short introduction to concurrency and parallelism in Haskell

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Background Constructors (1A) Young Won Lim 6/30/18

Programming Systems in Artificial Intelligence Functional Programming

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1

High Coverage Testing of Haskell Programs

High Coverage Testing of Haskell Programs

Principles of Programming Languages

Lambda Calculus and Extensions as Foundation of Functional Programming

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

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

IO Monad (3C) Young Won Lim 1/6/18

4.2 Variations on a Scheme -- Lazy Evaluation

Haskell Program Coverage Toolkit

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

The Worker/Wrapper Transformation

Implementing Concurrent Futures in Concurrent Haskell

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

Compilers: The goal. Safe. e : ASM

IO Monad (3D) Young Won Lim 1/18/18

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Behavior-Driven Development in Haskell

Functional Programming for Logicians - Lecture 1

Lazy Evaluation of Function Arguments. Lazy Evaluation of Function Arguments. Evaluation with Lazy Arguments. Evaluation with Lazy Arguments

Object-Oriented Principles and Practice / C++

Background Constructors (1A) Young Won Lim 7/9/18

The Art of Recursion: Problem Set 10

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Multi-paradigm Declarative Languages

Simon Peyton Jones Microsoft Research August 2012

Lightweight Concurrency Primitives for GHC. Peng Li Simon Peyton Jones Andrew Tolmach Simon Marlow

Islands RTS: a Hybrid Haskell Runtime System for NUMA Machines

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CITS3211 FUNCTIONAL PROGRAMMING

April 2 to April 4, 2018

Refactoring to Functional. Hadi Hariri

CS 842 Ben Cassell University of Waterloo

Introduction. Wouter Swierstra. Applied functional programming. Faculty of Science Information and Computing Sciences

6.945 Adventures in Advanced Symbolic Programming

Object Oriented Programming with c++ Question Bank

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE Programming Languages Midterm - Winter 2010

Background Constructors (1A) Young Won Lim 7/5/18

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Alonzo a Compiler for Agda

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

ITERATORS AND STREAMS 9

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Software System Design and Implementation

An introduction introduction to functional functional programming programming using usin Haskell

An introduction to functional programming. July 23, 2010

Principles of Programming Languages

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Monad Background (3A) Young Won Lim 11/8/17

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Haskell Monads CSC 131. Kim Bruce

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Functional Programming

First-Class Synchronization Barriers. Franklyn Turbak Wellesley College

Haskell Assignment 3 This assignment is worth 50 points.

Software System Design and Implementation

Programming in Haskell Aug Nov 2015

Motivation At the very begining Ni won t be complete until it has static typing 1 Maintenance needs nipkgs: 1M sloc Errors hard to spot 1 Eelco Dolstr

Improving Implicit Parallelism

The Worker/Wrapper Transformation

Introduction to Haskell

Monad Background (3A) Young Won Lim 11/18/17

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Transcription:

January 25, 2013

Writing Fast Haskell Laziness is both blessing and curse minimum = head. sort is O(n) for carefully-defined sort Memory usage seq, $! and bang patterns

Memoization Any thunk is only ever evaluted once Store thunks in a map indexed by the argument! Example: memofibs Data.MemoTrie and Data.MemoCombinators

Weak head normal form Strict: if f undefined = undefined

Weak head normal form Strict: if f undefined = undefined weak head normal form

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf"

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf" For functions, is not fully applied: const 2 3 is fully applied, const 2 is not

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf" For functions, is not fully applied: const 2 3 is fully applied, const 2 is not Lambda expressions are always in WHNF

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf" For functions, is not fully applied: const 2 3 is fully applied, const 2 is not Lambda expressions are always in WHNF Just undefined seq 2, [1..] seq 2 both evaluate to 2 and perform no extra forcing

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf" For functions, is not fully applied: const 2 3 is fully applied, const 2 is not Lambda expressions are always in WHNF Just undefined seq 2, [1..] seq 2 both evaluate to 2 and perform no extra forcing What about 10 ^ 100 ^ 4 seq 2?

Weak head normal form Strict: if f undefined = undefined weak head normal form For constructors, is different from undefined; e.g. [1,2,undefined], Just "whnf" For functions, is not fully applied: const 2 3 is fully applied, const 2 is not Lambda expressions are always in WHNF Just undefined seq 2, [1..] seq 2 both evaluate to 2 and perform no extra forcing What about 10 ^ 100 ^ 4 seq 2? Both instances of (^) are fully applied, so haskell will compute 10 1004.

Styling and Profiling First: compile your programs like ghc --make -rtsopts -prof program.hs program +RTS -s will output basic profiling information to stderr program +RTS -p will output time profiling information to program.prof program +RTS -i0.001 -hc

Seq-ing strictnes How to use seq properly: let shared = expensive x in shared seq f shared

Seq-ing strictnes How to use seq properly: let shared = expensive x in shared seq f shared shared is reduced to WHNF

Seq-ing strictnes How to use seq properly: let shared = expensive x in shared seq f shared shared is reduced to WHNF How not to use seq: expensive x seq f (expensive x)

Other speedups Bang-patterns: let!x = 2 + 3 in x + 6 forces x Compiling with -O2 Don t just throw seq everywhere!

Testing with Hspec Unit tests Autogenerated tests using QuickCheck!

Other libraries lens for first-class data access netwire for functional reactive programming OpenGL bindings