College Functors, Applicatives
|
|
- Mariah Harmon
- 5 years ago
- Views:
Transcription
1 College Functors, Applicatives Wouter Swierstra with a bit of Jurriaan Hage Utrecht University
2 Contents So far, we have seen monads define a common abstraction over many programming patterns. This kind of abstraction occurs more often in Haskell s libraries. In these slides we discuss: functors applicative functors 2
3 1. Functors 3
4 Functors 1 We are all familiar with map for lists: traversing a list and applying a function to its elements This also makes sense for binary trees, and all kinds of other datatypes A functor generalizes applying a function to the elements of a list to other datatype constructors: class Functor f where fmap :: (a > b) > f a > f b The function fmap says how to traverse f -like-things to adapt values of type a it may contain. 4
5 Maybe example 1 instance Functor Maybe where fmap f (Just x) = Just (f x) fmap f Nothing = Nothing Prelude> fmap ("C:\> " ++) (Just "ls -al") Just "C:\> ls -al" Prelude> fmap (^2) (Just 3) Just 9 Prelude> fmap undefined Nothing Nothing The only content is the a value in the Just, so that is what we change. A Nothing stays a Nothing. 5
6 Tuples examples 1 For tuples we can have instance Functor ((, ) a) where fmap f (x, y) = (x, f y) Functor only allows us to map one type parameter, so we give it (, ) a) (pairs with first component of type a), and can only apply the function to the second component. So, Prelude> fmap (*2) (1,3) (1,6) Is this natural? Maybe not. 6
7 What about our own types 1 data Tree a b = Leaf1 (a, b) Leaf2 (b, a) Bin (Tree a b) b (Tree a b) deriving Show An instance of Functor can only map b values! Think this through when ordering the type arguments. instance Functor (Tree a) where fmap f (Leaf1 p) = Leaf1 (fmap f p) -- On 2nd fmap f (Leaf2 (v, i)) = Leaf2 (f v, i) -- Do NOT use fmap! fmap f (Bin t1 v t2) = Bin (fmap f t1) (f v) (fmap f t2) 7 Prelude> fmap (*2) (Bin (Leaf2 (1, 1 )) 3 (Leaf1 ( 2,2)) Bin (Leaf2 (2, 1 )) 6 (Leaf1 ( 2,4))
8 Alternatively 1 We can decide to only change values in the leaf: instance Functor (Tree a) where fmap f (Leaf1 p) = Leaf1 (fmap f p) fmap f (Leaf2 (v, i)) = Leaf2 (f v, i) fmap f (Bin t1 v t2) = Bin (fmap f t1) v (fmap f t2) -- Note the v for f v Prelude> fmap (*2) (Bin (Leaf2 (1, 1 )) 3 (Leaf1 ( 2,2)) Bin (Leaf2 (2, 1 )) 3 (Leaf1 ( 2,4)) 8
9 Summary 1 Functors are one argument functions applied to things that exist in some context (like a list context) A well-known example is when such a context is a container type, like lists, trees (Tree a was our context), and even maybes. But what if I have two maybes and want to apply a binary function to their contents? Like ( ) (Just 2) (Just 3) to get Just 6. 9
10 Summary 1 Functors are one argument functions applied to things that exist in some context (like a list context) A well-known example is when such a context is a container type, like lists, trees (Tree a was our context), and even maybes. But what if I have two maybes and want to apply a binary function to their contents? Like ( ) (Just 2) (Just 3) to get Just 6. I need an applicative! Prelude> (*) <$> Just 2 <*> Just 3 Just 6 9
11 2. Applicative 10
12 Sequencing IO operations 2 Applicatives lie inbetween Functors and Monads. sequenceio :: [IO a] > IO [a] sequenceio [ ] = return [ ] sequenceio (c : cs) = do x < c xs < sequenceio cs return (x : xs) There is nothing wrong with this code but using do notation may seem like overkill. The variable x isn t used in the second computation, only as part of the result! 11
13 Using ap 2 The ap function defined as follows: ap : Monad m => m (a > b) > m a > m b ap mf mx = do f < mf x < mx return (f x) Using ap we can write: sequenceio :: [IO a] > IO [a] sequenceio [ ] = return [ ] sequenceio (c : cs) = return (:) ap c ap sequenceio cs 12 This works for any monad, not just the IO monad.
14 Evaluating expressions 2 Another example: data Expr v = Var v Val Int Add (Expr v) (Expr v) type Env v = Map v Int eval : Expr v > Env v > Int eval (Var v) env = lookup v env eval (Val i) env = i eval (Add l r) env = (eval l env) + (eval r env) We are passing around an environment that is only really used in the Var branch. 13
15 An applicative alternative 2 const : a > (env > a) const x = a s : (env > a > b) > (env > a) > (env > b) s ef es env = (ef env) (es env) eval : Expr v > Env v > Int eval (Var v) = lookup v eval (Val i) = const i eval (Add l r) env = const (+) s (eval l) s (eval r) The s combinator lets us apply one computation expecting an environment to another. 14
16 Transposing matrices 2 transpose :: [[a]] > [[a]] transpose [ ] = repeat [ ] transpose (xs : xss) = zipwith (:) xs (transpose xss) Can we play the same trick and find a combinator that will apply a list of functions to a list of arguments? zapp : [a > b] > [a] > [b] zapp (f : fs) (x : xs) = (f x) : (zapp fs xs) transpose (xs : xss) = repeat (:) zapp xs zapp transpose xss 15
17 What is the pattern? 2 What do these functions have in common? ap : IO (a > b) > IO a > IO b s : (env > a > b) > (env > a) > (env > b) zapp : [a > b] > [a] > [b] 16
18 Applicative (applicative functors) 2 class (Functor f ) => Applicative f where pure :: a > f a (< >) :: f (a > b) > f a > f b Note that Functor is a superclass of Applicative. As with functors f represents a context, but now the function to be applied is also in that context, e.g., a list. 17
19 Applicative (applicative functors) 2 We can also define map in terms of the applicative operations (traditionally, it is called (<$>): (<$>) :: Functor f => (a > b) > f a > f b How might we define this function? 18
20 Applicative (applicative functors) 2 We can also define map in terms of the applicative operations (traditionally, it is called (<$>): (<$>) :: Functor f => (a > b) > f a > f b How might we define this function? (<$>) f fx = pure f < > fx To apply a (normal) function in a context as an applicative, simply wrap it in a context and use (< >). 18
21 Relating Applicative functors and Monads 2 Every monad can be given an applicative functor interface. instance Monad m => Applicative m where pure :: a > m a pure = return mf < > mx = do f < mf x < mx return (f x) 19 But this may not always be the right choice. For example, we have seen the zippy applicative instance for lists; using the instance arising from the list monad gives very different behaviour! But not every applicative functor is a monad...
22 Monads vs. applicative functors - I 2 (< >) :: (Applicative f ) => f (a > b) > f a > f b (>>=) :: (Monad m) => m a > (a > m b) > m b The arguments to < > are (typically) first-order structures (that may contain higher-order data). Monadic bind is inherently higher order. With monads, subsequent actions can depend on the results of effects: depending on the character the user enters, respond differently. 20
23 Monads vs applicative functors - II 2 There are more Applicative functors than there are monads; but monads are more powerful! If you have an Applicative functor, that s good; having a monad is better. If you need a monad, that s good; only needing an Applicative functor is better. With applicative functors, the structure is statically determined (and can be analyzed or optimized). 21
24 Compare the two 2 How to model an if-then-else statement with monads or applicatives: miffy :: Monad m => m Bool > m a > m a > m a miffy mb m1 m2 = do b < mb if b then m1 else m2 mappy :: Applicative m => m Bool > m a > m a > m a mappy fb f1 f2 = ite <$> fb < > f1 < > f2 where ite b t e = if b then t else e 22 The former only runs m1 or m2, the second may evaluate both, and then chooses which one to return. The strict sequentiality of the monad is missing in the applicative.
25 Composing monads 2 Given two monads m1 and m2, is m1. m2 a monad? data Compose m1 m2 a = Compose (m1 (m2 a)) instance (Monad m1, Monad m2) => Monad (Compose m1 m2) where return :: a > m1 (m2 a) (>>=) :: m1 (m2 a) > (a > m1 (m2 b)) > m1 (m2 b 23 Unfortunately, there is no guarantee that such an instance can be defined. As a result, there has been a great deal of work on monad transformers, that allow complex monads to be assembled from smaller pieces. For applicative functors however...
26 Composing applicative functors 2 For any pair of applicative functors f and g: data Compose f g a = Compose (f (g a)) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure :: a > f (g a) pure x =... (< >) :: f (g (a > b)) > (f (g a)) > f (g b) fgf < > fgx =... We can define the desired pure and < > operations! This is a guarantee of compositionality. 24
27 Composing applicative functors 2 For any pair of applicative functors f and g: data Compose f g a = Compose (f (g a)) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure :: a > f (g a) pure x = pure (pure x) (< >) :: f (g (a > b)) > (f (g a)) > f (g b) fgf < > fgx = (pure< >) < > fgf < > fgx We can define the desired pure and < > operations! Intuition: wrapping something into two contexts, is like wrapping it once in a wrapped context. 25 This is a guarantee of compositionality.
28 Imprecise but catchy slogans 2 26
29 Imprecise but catchy slogans 2 Monads are programmable semi-colons! 26
30 Imprecise but catchy slogans 2 Monads are programmable semi-colons! Applicatives are programmable function application! 26
31 Applicative functor laws 2 identity pure id < > u = u composition pure (.) < > u < > v < > w = u < > (v < > w) NB. (< >) is left associative, so u < > v < > w = (u < > v) < > w homomorphism pure f < > pure x = pure (f x) interchange 27 u < > pure x = pure (\ f > f x) < > u
32 To summarise 2 functors: you apply a function to a wrapped value using fmap (or <$>) applicatives: you apply a wrapped function to a wrapped value using < > monads: you apply a function that returns a wrapped value, to a wrapped value using (>>=) (or liftm) 28
33 Why should we care? 2 Functional programmers are addicted to abstraction: as soon as they spot a pattern, they typically want to abstract over it. The type classes we have seen today, such as monads, functors and applicative functors, all capture some common pattern. Using these patterns can save you some boilerplate code. 29
34 Why should we care? 2 Functional programmers are addicted to abstraction: as soon as they spot a pattern, they typically want to abstract over it. The type classes we have seen today, such as monads, functors and applicative functors, all capture some common pattern. Using these patterns can save you some boilerplate code. And understanding these patterns can help guide your design. Think of them as typed design patterns for which support has been added to the language. Is my type a monad? Or is it just applicative? 29
Applicative, traversable, foldable
Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.
More informationApplicative, traversable, foldable
Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over
More informationCSCE 314 Programming Languages Functors, Applicatives, and Monads
CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::
More informationMonads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB
Monads and all that III Applicative Functors John Hughes Chalmers University/Quviq AB Recall our expression parser expr = do a
More informationApplicative programming with effects
Under consideration for publication in J. Functional Programming 1 F U N C T I O N A L P E A R L Applicative programming with effects CONOR MCBRIDE University of Nottingham ROSS PATERSON City University,
More informationAdvanced Programming Handout 7. Monads and Friends (SOE Chapter 18)
Advanced Programming Handout 7 Monads and Friends (SOE Chapter 18) The Type of a Type In previous chapters we discussed: Monomorphic types such as Int, Bool, etc. Polymorphic types such as [a], Tree a,
More informationData types à la carte. Wouter Swierstra Dutch HUG 25/8/10
Data types à la carte Wouter Swierstra Dutch HUG 25/8/10 Expressions data Expr where Add :: Expr -> Expr -> Expr Val :: Int -> Expr eval :: Expr -> Int eval (Val x) = x eval (Add l r) = eval l + eval r
More informationMonad Overview (3B) Young Won Lim 1/16/18
Based on Haskell in 5 steps https://wiki.haskell.org/haskell_in_5_steps 2 Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of
More informationMaybe Monad (3B) Young Won Lim 1/3/18
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is
More informationTrees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.
Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin
More informationMonad (3A) Young Won Lim 8/9/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationData types à la carte
Data types à la carte FP AMS 21/6/18 Wouter Swierstra 1 Warm-up: expressions in Haskell Suppose we re implementing a small expression language in Haskell. We can define a data type for expressions and
More informationMonads and all that I. Monads. John Hughes Chalmers University/Quviq AB
Monads and all that I. Monads John Hughes Chalmers University/Quviq AB Binary Trees in Haskell data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq,Show) Cf Coq: Inductive tree (A:Set) : Set := leaf
More informationJVM ByteCode Interpreter
JVM ByteCode Interpreter written in Haskell (In under 1000 Lines of Code) By Louis Jenkins Presentation Schedule ( 15 Minutes) Discuss and Run the Virtual Machine first
More informationThese notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without
These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create
More informationApplicatives Comparisons (2C) Young Won Lim 3/6/18
Comparisons (2C) Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later
More informationMonads. Functional Programming (CS4011) Monads
Monads Functional Programming (CS4011) Andrew Butterfield Glenn Strong Foundations & Methods Group, Discipline of Software Systems Trinity College, University of Dublin {Andrew.Butterfield,Glenn.Strong}@cs.tcd.ie
More informationMonad (1A) Young Won Lim 6/26/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationAdvanced Functional Programming
Advanced Functional Programming Tim Sheard Portland State University Lecture 2: More about Type Classes Implementing Type Classes Higher Order Types Multi-parameter Type Classes Tim Sheard 1 Implementing
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationFunctional Programming
Functional Programming Monadic Prelude Jevgeni Kabanov Department of Computer Science University of Tartu Introduction Previously on Functional Programming Monadic laws Monad class (>>= and return) MonadPlus
More informationParsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics
National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What
More informationHaskell Monads CSC 131. Kim Bruce
Haskell Monads CSC 131 Kim Bruce Monads The ontological essence of a monad is its irreducible simplicity. Unlike atoms, monads possess no material or spatial character. They also differ from atoms by their
More informationHarvard School of Engineering and Applied Sciences CS 152: Programming Languages
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations
More informationMonads. COS 441 Slides 16
Monads COS 441 Slides 16 Last time: Agenda We looked at implementation strategies for languages with errors, with printing and with storage We introduced the concept of a monad, which involves 3 things:
More informationCSC324 Principles of Programming Languages
CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018 Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data
More informationLecture 4: Higher Order Functions
Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function
More informationStructural polymorphism in Generic Haskell
Structural polymorphism in Generic Haskell Andres Löh andres@cs.uu.nl 5 February 2005 Overview About Haskell Genericity and other types of polymorphism Examples of generic functions Generic Haskell Overview
More informationMaybe Monad (3B) Young Won Lim 12/21/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationMonads in Haskell. Nathanael Schilling. December 12, 2014
Monads in Haskell Nathanael Schilling December 12, 2014 Abstract In Haskell, monads provide a mechanism for mapping functions of the type a -> m b to those of the type m a -> m b. This mapping is dependent
More informationAll About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell
All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell Edward Kmett http://comonad.com/ Categories Categories have objects and arrows Every
More informationINTRODUCTION TO HASKELL
INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be
More informationMonad Background (3A) Young Won Lim 11/18/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationGADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]
GADTs Advanced functional programming - Lecture 7 Wouter Swierstra and Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree
More informationEECS 700 Functional Programming
EECS 700 Functional Programming Dr. Andy Gill University of Kansas February 16, 2010 1 / 41 Parsing A parser is a program that analyses a piece of text to determine its syntactic structure. The expression
More informationProgramming with Math and Logic
.. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing
More informationMe and my research. Wouter Swierstra Vector Fabrics, 6/11/09
Me and my research Wouter Swierstra Vector Fabrics, 6/11/09 Brief bio MSc in Software Technology (Utrecht); PhD entitled A Functional Specification of Effects (University of Nottingham); Postdoc position
More informationBackground Type Classes (1B) Young Won Lim 6/14/18
Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2
More informationProgramming Languages
Programming Languages Andrea Flexeder Chair for Theoretical Computer Science Prof. Seidl TU München winter term 2010/2011 Lecture 10 Side-Effects Main Points you should get: Why monads? What is a monad?
More informationThe Algebra of Programming in Haskell
The Algebra of Programming in Haskell Bruno Oliveira The Algebra of Programming in Haskell p.1/21 Datatype Generic Programming - Motivation/Goals The project is to develop a novel mechanism for parameterizing
More informationCS 11 Haskell track: lecture 5. This week: State monads
CS 11 Haskell track: lecture 5 This week: State monads Reference "Monads for the Working Haskell Programmer" http://www.engr.mun.ca/~theo/misc/ haskell_and_monads.htm Good explanation of state monads Today's
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,
More informationCS 11 Haskell track: lecture 4. n This week: Monads!
CS 11 Haskell track: lecture 4 This week: Monads! Monads Have already seen an example of a monad IO monad But similar concepts can be used for a lot of completely unrelated tasks Monads are useful "general
More information(Co)Monads forgrad Dummies Students
(Co)Monads forgrad Dummies Students Math s answer to It Depends Paul Hachmann hachmap@mcmaster.ca Outline Review of Monads The Dual Link Programming with Co-Monads (Co)Monads for Grad Students slide #2
More informationEmbedded Domain Specific Languages in Idris Lecture 3: State, Side Effects and Resources
Embedded Domain Specific Languages in Idris Lecture 3: State, Side Effects and Resources Edwin Brady (ecb10@st-andrews.ac.uk) University of St Andrews, Scotland, UK @edwinbrady SSGEP, Oxford, 9th July
More informationHaske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell
Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program
More informationBackground Type Classes (1B) Young Won Lim 6/28/18
Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2
More informationIntroduction to Functional Programming in Haskell 1 / 56
Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order
More informationMonad Background (3A) Young Won Lim 11/8/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationGADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]
GADTs AFP Summer School Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: 3 A datatype data
More informationState Monad (3D) Young Won Lim 8/31/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationGADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences
GADTs Advanced functional programming - Lecture 7 Wouter Swierstra 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces:
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationMonad (1A) Young Won Lim 6/21/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationCS 11 Haskell track: lecture 1
CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of
More informationHaskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder
Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted
More informationType system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)
Type system EDAN40: Functional Programming Types and Type Classes (revisited) Jacek Malec Dept. of Computer Science, Lund University, Sweden April 3rd, 2017 In programming languages, a type system is a
More informationProgramming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application rules using symbolic rewriting Madhusudan Parthasarathy
More informationDenotational Semantics. Domain Theory
Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning
More informationLambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6
Lambda calculus Advanced functional programming - Lecture 6 Wouter Swierstra and Alejandro Serrano 1 Today Lambda calculus the foundation of functional programming What makes lambda calculus such a universal
More informationMonad Background (3A) Young Won Lim 11/20/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationRecap: Functions as first-class values
Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse
More informationTerm rewriting a primer
Term rewriting a primer Ralf Lämmel Software Languages Team University of Koblenz-Landau http://www.softlang.org/ Some laws for expressions forms X + 0 = X -- Unit of addition X 1 = X -- Unit of multiplication
More informationThe List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona
The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList
More informationExplicit Recursion in Generic Haskell
Explicit Recursion in Generic Haskell Andres Löh Universiteit Utrecht andres@cs.uu.nl 26th March 2003 This is joint work with Dave Clarke and Johan Jeuring. Overview What is Generic Haskell? The difference
More informationOverview. Elements of Programming Languages. Evaluation order. Evaluation order
Overview Elements of Programming Languages Lecture 15: Evaluation strategies and laziness James Cheney University of Edinburgh November 18, 216 Final few lectures: cross-cutting language design issues
More informationCSci 4223 Principles of Programming Languages
CSci 4223 Principles of Programming Languages Lecture 11 Review Features learned: functions, tuples, lists, let expressions, options, records, datatypes, case expressions, type synonyms, pattern matching,
More informationCIS 194: Homework 5. Due Monday, 18 February. Expressions. (2 + 3) 4 would be represented by the value
CIS 194: Homework 5 Due Monday, 18 February Files you should submit: Calc.hs, containing a module of the same name. As we saw in class, Haskell s type classes provide ad-hoc polymorphism, that is, the
More informationMonads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1
Monads Lecture 12 Prof. Aiken CS 264 Lecture 12 1 Monads A language without side effects can t do I/O Side effects are critical in some applications For expressiveness and/or efficiency Haskell has a general
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages From Trees to Type Classes Mark P Jones Portland State University 1 Trees:! " There are many kinds of tree data structure.! " For example: data BinTree a = Leaf a BinTree
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationMonad (1A) Young Won Lim 6/9/17
Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,
More informationCS 209 Functional Programming
CS 209 Functional Programming Lecture 03 - Intro to Monads Dr. Greg Lavender Department of Computer Science Stanford University "The most important thing in a programming language is the name. A language
More informationLast time: generic programming
1/ 55 Last time: generic programming val (=) : { D: DATA } D. t D. t bool 2/ 55 This time: monads etc., continued >>= effect E 3/ 55 Recap: monads, bind and let An imperative program let id =! counter
More informationProgramming Paradigms, Fall 06
Programming Paradigms, Fall 06 Multiple Choice Exam January 29, 2007, 10:00 11:15 ID Name Each of the following questions has exactly one correct answer. For each question, you may select one or more answers
More informationMonads seen so far: IO vs Gen
Monads David Sands Monads seen so far: IO vs Gen IO A Gen A Instructions to build a value of type A by interacting with the operating system Instructions to create a random value of type A Run by the ghc
More informationArrow Basics. Ted Cooper CS510 Winter Here s a literate introduction to arrows and review of the basic Arrow typeclasses.
Arrow Basics Ted Cooper theod@pdx.edu CS510 Winter 2016 1 Introduction Here s a literate introduction to arrows and review of the basic Arrow typeclasses. {-# LANGUAGE Arrows #-} module ArrowBasics where
More informationComp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018
Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.
More informationThe Worker/Wrapper Transformation
The Worker/Wrapper Transformation Andy Gill 1 Graham Hutton 2 1 Galois, Inc. 2 University of Nottingham February 6, 2008 Andy Gill, Graham Hutton The Worker/Wrapper Transformation February 6, 2008 1 /
More informationType families and data kinds
Type families and data kinds AFP Summer School Wouter Swierstra 1 Today How do GADTs work? Kinds beyond * Programming with types 2 Calling functions on vectors Given two vectors xs : Vec a n and ys : Vec
More informationWriting code that I'm not smart enough to write. A funny thing happened at Lambda Jam
Writing code that I'm not smart enough to write A funny thing happened at Lambda Jam Background "Let s make a lambda calculator" Rúnar Bjarnason Task: write an interpreter for the lambda calculus Lambda
More informationTalen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University
Talen en Compilers 2017-2018, period 2 Jurriaan Hage Department of Information and Computing Sciences Utrecht University November 13, 2017 1. Introduction 1-1 This lecture Introduction Course overview
More informationCIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the
CIS 194: Homework 5 Due Friday, October 3, 2014 No template file is provided for this homework. Download the Ring.hs and Parser.hs files from the website, and make your HW05.hs Haskell file with your name,
More informationPart 0. A (Not So) Brief Introduction to Haskell
Part 0 A (Not So) Brief Introduction to Haskell CSCI 3110 Code Fall 2015 1 Introduction This introduction discusses the most important aspects of programming in Haskell hopefully sufficiently well for
More informationPolymorphic Contexts FP-Dag 2015
Polymorphic Contexts FP-Dag 2015 Doaitse Swierstra January 14, 2015 Goal of this Talk To show you: that lazy evaluation requires a type system which extend beyond system-f how the Utrecht Haskell Compiler
More informationIntroduction to Recursion schemes
Introduction to Recursion schemes Lambda Jam 2018-05-22 Amy Wong BRICKX amy@brickx.com Agenda My background Problem in recursion Fix point concept Recursion patterns, aka morphisms Using morphisms to solve
More informationMonads. Bonus lecture 2017 David Sands
Monads Bonus lecture 2017 David Sands Our version of the story, so far. Monad is the class of instructions. Instructions can be built using do notation. We have seen two kinds of instructions i.e. two
More information1 Delimited continuations in Haskell
1 Delimited continuations in Haskell This section describes programming with delimited control in Haskell. Delimited control, like its instance, exceptions, is an effect. Therefore, we have to use monads.
More informationSide Effects (3B) Young Won Lim 11/20/17
Side Effects (3B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later
More informationHaskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)
Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes Henrik Nilsson University of Nottingham, UK What is the type of (==)? E.g. the following both work: 1 == 2 a == b I.e., (==) can be used to compare
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell: Declaring Types and Classes Dr. Hyunyoung Lee 1 Outline Declaring Data Types Class and Instance Declarations 2 Defining New Types Three constructs for defining types:
More informationIO Monad (3D) Young Won Lim 1/18/18
Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationModular Reifiable Matching
Modular Reifiable Matching A List-of-Functors Approach to Two-Level Types Bruno C. d. S. Oliveira University of Hong Kong bruno@cs.hku.hk Shin-Cheng Mu Academia Sinica scm@iis.sinica.edu.tw Shu-Hung You
More informationCOMP3151/9151 Foundations of Concurrency Lecture 8
1 COMP3151/9151 Foundations of Concurrency Lecture 8 Liam O Connor CSE, UNSW (and data61) 8 Sept 2017 2 Shared Data Consider the Readers and Writers problem from Lecture 6: Problem We have a large data
More informationSoftware System Design and Implementation
Software System Design and Implementation Controlling Effects Gabriele Keller The University of New South Wales School of Computer Science and Engineering Sydney, Australia COMP3141 18s1 Examples of effects
More informationA Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1
A Third Look At ML Chapter Nine Modern Programming Languages, 2nd ed. 1 Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order
More informationVariables. Substitution
Variables Elements of Programming Languages Lecture 4: Variables, binding and substitution James Cheney University of Edinburgh October 6, 2015 A variable is a symbol that can stand for another expression.
More informationMonad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads
Monad class EDAN40: Functional Programming Functors and Monads Jacek Malec Dept. of Computer Science, Lund University, Sweden April 23rd, 2018 Motivation: Separation of pure and impure code Properties
More information