Monads. Bonus lecture 2017 David Sands
|
|
- Barnaby George
- 6 years ago
- Views:
Transcription
1 Monads Bonus lecture 2017 David Sands
2 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 instances of Monad: IO do putstr File name? f <- getline c <- readfile f return $ f ++ c Gen do n <- choose (2,10) r <- elements[clubs,spades] return $ Card r (Numeric n)
3 IO vs Gen IO T Gen T Instructions to build a value of type T by interacting with the operating system Instructions to create a random value of type T Run by the ghc runtime system Run by the QuickCheck library functions to perform random tests
4 Repeating Instructions dotwice i = do a <- i b <- i return (a,b) Main> dotwice $ putstrln "hello hello hello ((),()) Main>
5 Repeating Instructions dotwice i = do a <- i b <- i return (a,b) Main> sample $ dotwice (choose ( a, z )) ('m','c') ('b','j') ('h','l') ('y','q') ('k','f') ('w','q') ('p','h') Main>
6 Monads = Instructions What is the type of dotwice? Main> :t dotwice dotwice :: Monad a => a b -> a (b,b) Even the kind of instructions can vary. Different kinds of instructions, depending on who obeys them. Whatever kind of result it produces, we get a pair of them IO means operating system.
7 Plan 1. One more example of a Monad: Instructions for Parsing (a parsing library) 2. Rolling your own Monads The Truth about do The Parser Monad Maybe is also a Monad (and list, and...
8 A Simple Parsing Library A library for building parsers containing: An abstract data type Parser a A function parse :: Parser a -> String -> Maybe (a,string) Basic building blocks for building parsers
9 Example: Phone numbers Two ways of writing phone numbers:
10 do s <- getline IO c <- readfile s return $ s ++ c do n <- elements[1..9] Gen m <- vectorof n arbitrary return $ n:m do c <- sat (`elem` ;,: ) ds <- chain digit (char c) return $ map digittoint ds Parser
11 IO t Instructions for interacting with operating system Run by GHC runtime system produce value of type t Gen t Instructions for building random values Run by quickcheck to generate random values of type t Parser t Instructions for parsing Run by parse to parse a string and produce a Maybe (t,string)
12 Terminology A monadic value is just an expression whose type is an instance of class Monad t is a monad means t is an instance of the class Monad We have often called a monadic value an instruction. This is not standard terminology but sometimes they are called actions
13 Monads and do notation To be an instance of class Monad you need (as a minimal definition) operations >>= and return class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a (>>) :: m a -> m b -> m b x >> y = x >>= \_ -> y Default implementations fail :: String -> m a fail msg = error msg
14 Update, As of GHC 7.10 OK that s a bit old school. Nowadays Monad is a subclass of Applicative (which is a subclass of Functor) The class itself is a bit simpler you just need to define >>= But I ll define it the old way and ignore the rest
15 Boilerplate to make your monad an instance of Applicative import Control.Applicative (Applicative(..)) import Control.Monad(liftM, ap) instance Functor MyMonad where fmap = liftm instance Applicative MyMonad where pure = return (<*>) = ap See Learn you a Haskell for more info on Functor and Applicative
16 The truth about Do Do syntax is just a shorthand: do act1 == == act2 act1 >> act2 act1 >>= \_ -> act2 do v <- act1 == act2 act1 >>= \v -> act2
17 The Parser Monad To be an instance of class Monad you need two operations: >>= and return instance Monad Parser where return = succeed (>>=) = (>*>) Why bother? Our first example of a home-grown monad Can understand do notation
18 Example
19 The truth about Do Full translation (I) do act1 actn act1 >> do actn == = do v <- act1 actn == act1 >>= \v -> do actn do actn == actn
20 The truth about Do Full Translation (II): Let and pattern matching do let p = e actn let p = e in do actn == = do pattern <- act1 actn == == let f pattern = do actn f _ = fail Error in act1 >>= f
21
22
23
24 All three functions take a value (or no value) and produce an IO wrapped value The function >>= allows us to join them together getline >>= readfile >>= putstrln
25
26 Maybe
27 Here is a function
28 They can be composed
29 Here is a function half x even x = Just (x `div` 2) odd x = Nothing
30 What if we feed it a wrapped value? We need to use >>= to shove our wrapped value into the function
31 >>=
32 >>=
33 >>=
34 >>=
35 >>=
36 Just 20 >>= half >>= half >>= half
37
38
39 Instance Monad Maybe Maybe is a very simple monad instance Monad Maybe where Just x >>= k = k x Nothing >>= _ = Nothing return fail s = Just = Nothing Although simple it can be useful
40 Congestion Charge Billing
41 Congestion Charge Billing Registration number used to find the Personnummer of the owner carregister :: [(RegNr,PNr)] Personnummer used to find the name of the owner nameregister :: [(PNr,Name)] Name used to find the address of the owner addressregister :: [(Name,Address)]
42 Example: Congestion Charge Billing type CarReg = String ; type PNr = String type Name = String ; type Address = String carregister :: [(CarReg,PNr)] carregister = [("JBD 007"," "),...] nameregister :: [(PNr,Name)] nameregister = [(" ","Dave ),... ] addressregister :: [((Name,PNr),Address)] addressregister = [(("Dave"," "),"42 Streetgatan\n Askim"),... ]
43 Example: Congestion Charge Billing With the help of lookup :: Eq a => a -> [(a,b)] -> Maybe b we can return the address of car owners billingaddress :: CarReg -> Maybe (Name, Address) billingaddress car = case lookup car carregister of Nothing -> Nothing Just pnr -> case lookup pnr nameregister of Nothing -> Nothing Just name -> case lookup (name,pnr) addressregister of Nothing -> Nothing Just addr -> Just (name,addr)
44 Example: Congestion Charge Billing Using the fact that Maybe is a member of class Monad we can avoid the spaghetti and write: billingaddress car = do pnr <- lookup car carregister name <- lookup pnr nameregister addr <- lookup (name,pnr) addressregister return (name,addr)
45 Example: Congestion Charge Billing Unrolling one layer of the do syntactic sugar: billingaddress car == lookup car carregister >>= \pnr -> do name <- lookup pnr nameregister addr <- lookup (name,pnr) addressregister return (name,addr) lookup car carregister gives Nothing then the definition of >>= ensures that the whole result is Nothing return is Just
46 Summary We can use higher-order functions to build Parsers from other more basic Parsers. Parsers can be viewed as an instance of Monad We have seen how we can build our own Monads! A lot of plumbing is nicely hidden away The implementation of the Monad is not visible and can thus be changed or extended
47 IO t Instructions for interacting with operating system Run by GHC runtime system produce value of type t Gen t Instructions for building random values Run by quickcheck to generate random values of type t Parser t Instructions for parsing Run by parse to parse a string and Maybe produce a value of type t + Maybe = Four Monads
48 Code Parsing.hs module containing the parser monad and simple parser combinators. See course home page
49 We can build our own Monads! A lot of plumbing is nicely hidden away A powerful pattern, used widely in Haskell A pattern that can be used in other languages, but syntax support helps F# computation expressions Scala
50 More examples stack (slides/video from last year)
51 Another Example: A Stack A Stack is a stateful object Stack operations can push values on, pop values off, add the top elements type Stack = [Int] newtype StackOp t = StackOp (Stack -> (t,stack)) -- the type of a stack operation that produces -- a value of type t pop :: StackOp Int push :: Int -> StackOp () add :: StackOp ()
52 Running a StackOp type Stack = [Int] newtype StackOp t = StackOp (Stack -> (t,stack)) run (StackOp f) = f -- run (StackOp f) state = f state
53 Operations
54 Building a new StackOp swap :: StackOp () swap = StackOp $ \s -> let (x,s') = run pop s (y,s'') = run pop s' (_,s''') = run (push x) s'' (_,s'''') = run (push y) s'' in (_, s'''') No thanks!
55 StackOp is a Monad Stack instructions for producing a value
56 So now we can write
57 Stack t Stack instructions producing a value of type t Run by run Maybe t Instructions for either producing a value or nothing Run by?? (not an abstract data type) Two More Monads
58 Pictures from a blog post about functors, applicatives and monads functors,_applicatives,_and_monads_in_pictures.html Aditya Y. Bhargava
Monads 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 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 informationMore on functional programming
More on functional programming Emphasis on Haskell (as a pure functional language) Input and output in Haskell Wrappers / contexts Monads chaining wrapped computations Maybe and lists as monads Return
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 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 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 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 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 informationBackground Operators (1E) Young Won Lim 7/7/18
Background Operators (1E) 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
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 informationParsing Expressions. Slides by Koen Lindström Claessen & David Sands
Parsing Expressions Slides by Koen Lindström Claessen & David Sands Expressions Such as 5*2+12 17+3*(4*3+75) Can be modelled as a datatype data Expr = Num Int Add Expr Expr Mul Expr Expr Showing and Reading
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 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 informationMonad P1 : Several Monad Types (4A) Young Won Lim 2/13/19
Monad P1 : Several Copyright (c) 2016-2019 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
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 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 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 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 informationApplicative, 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 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 informationExpressions. Parsing Expressions. Showing and Reading. Parsing. Parsing Numbers. Expressions. Such as. Can be modelled as a datatype
Expressions Parsing Expressions Such as 5*2+12 17+3*(4*3+75) Can be modelled as a datatype Add Expr Expr Mul Expr Expr Slides by Koen Lindström Claessen & David Sands Showing and Reading We have seen how
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 informationProgramming in Haskell Aug Nov 2015
Programming in Haskell Aug Nov 2015 LECTURE 23 NOVEMBER 12, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Summary of IO Actions of type IO t1, t1 -> IO t2, t1 -> t2 -> IO t3 etc. As opposed to pure functions
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 informationStandard prelude. Appendix A. A.1 Classes
Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from
More informationCIS552: Advanced Programming
CIS552: Advanced Programming Handout 8 What is a Parser? A parser is a program that analyzes a piece of text to deine its structure (and, typically, returns a tree representing this structure). The World
More informationFunctional Programming TDA 452, DIT 142
Chalmers Göteborgs Universitet 2018-01-11 Examiner: Thomas Hallgren, D&IT, Answering questions at approx 15.00 (or by phone) Functional Programming TDA 452, DIT 142 2018-01-11 14.00 18.00 Samhällsbyggnad
More informationOverview. Declarative Languages. General monads. The old IO monad. Sequencing operator example. Sequencing operator
Overview Declarative Languages D7012E: General monads in haskell Fredrik Bengtsson IO-monad sequencing operator monad class requirements on monad Monadic computation trivial example useful example The
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 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 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 informationI/O in Haskell. To output a character: putchar :: Char -> IO () e.g., putchar c. To output a string: putstr :: String -> IO () e.g.
I/O in Haskell Generally, I/O functions in Haskell have type IO a, where a could be any type. The purpose and use of a will be explained later. We call these commands or actions, for we think of them as
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 informationPolymorphism Overview (1A) Young Won Lim 2/20/18
Polymorphism Overview (1A) 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 informationFusion-powered EDSLs
Fusion-powered EDSLs Philippa Cowderoy flippa@flippac.org Fusion-powered EDSLs p. Outline What is fusion? Anatomy of an EDSL Shallow vs deep embedding Examples: Identity & State monads Self-analysing EDSL
More informationThe Remote Monad. Dissertation Defense. Justin Dawson.
The Remote Monad Dissertation Defense Justin Dawson jdawson@ittc.ku.edu Information and Telecommunication Technology Center University of Kansas, USA 1 / 66 Sandwiches! How do you make a sandwich? 2 /
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers
1 CSCE 314: Programming Languages Dr. Flemming Andersen Functional Parsers What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]
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 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 informationReferences. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs
References Monadic I/O in Haskell Jim Royer CIS 352 March 6, 2018 Chapter 18 of Haskell: the Craft of Functional Programming by Simon Thompson, Addison-Wesley, 2011. Chapter 9 of Learn you a Haskell for
More informationCollege Functors, Applicatives
College 2016-2017 Functors, Applicatives Wouter Swierstra with a bit of Jurriaan Hage Utrecht University Contents So far, we have seen monads define a common abstraction over many programming patterns.
More informationIntroduction to Haskell
Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming
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 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 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 informationCSCE 314 Programming Languages. Functional Parsers
CSCE 314 Programming Languages Functional Parsers Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]
More informationSolution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors
Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source
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 informationCSCE 314 Programming Languages. Monadic Parsing
CSCE 314 Programming Languages Monadic Parsing Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a string of characters (or a set of tokens) as input and determines its syntactic structure.
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 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 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 informationMonad Background (3A) Young Won Lim 10/5/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 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 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 informationFunctional Programming TDA 452, DIT 142
Chalmers Göteborgs Universitet 2016-04-07 Examiner: David Sands dave@chalmers.se. Answering questions on the day of the exam (at approx 15.00): Gregoire Detrez (tel: 073 55 69 550) and at other times by
More informationQuickCheck: Beyond the Basics
QuickCheck: Beyond the Basics Dave Laing May 13, 2014 Basic QuickCheck Some properties reverse_involutive :: [Int] -> Bool reverse_involutive xs = xs == (reverse. reverse) xs sort_idempotent :: [Int] ->
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 informationINDEX. Symbols & Numbers. Learn You a Haskell for Great Good! 2011 by Miran Lipovača
INDEX Symbols & Numbers && (double ampersand) as Boolean operator conjunction, 2 using with folds and lists, 78 79 '(apostrophe) using with functions, 7 using with types, 149 150 * (asterisk) as multiplication
More informationDerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern
DerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern Baldur Blöndal Ryan Scott 1 Andres Löh 2 1 Indiana University 2 Well-Typed LLP rgscott@indiana.edu github.com/ryanglscott A brief
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 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 informationLivin la via loca Coercing Types with Class
Livin la via loca Coercing Types with Class Baldur Blöndal Ryan Scott 1 Andres Löh 2 1 Indiana University 2 Well-Typed LLP rgscott@indiana.edu github.com/ryanglscott Glasgow Haskell Compiler (or, GHC)
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 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 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 informationInformatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lectures 15 and 16 IO and Monads Don Sannella University of Edinburgh Part I The Mind-Body Problem The Mind-Body Problem Part II Commands Print a character putchar
More informationHaskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules
Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]
More informationCOMP3141 Assignment 2
COMP3141 Assignment 2 Haskell-Augmented Regular Expressions (H.A.R.E.) Version 1.2 Liam O Connor Semester 1, 2018 Marking Total of 20 marks (20% of practical component) Due Date Friday, 25th May, 2018,
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 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 informationCSCE 314 Programming Languages. Interactive Programming: I/O
CSCE 314 Programming Languages Interactive Programming: I/O Dr. Hyunyoung Lee 1 Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start
More informationCS 440: Programming Languages and Translators, Spring 2019 Mon 2/4
Haskell, Part 5 CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4 More Haskell Miscellaneous topics Simple I/O, do blocks, actions Modules data vs type declaration Instances of classtypes
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 informationHaskell Modules. Input and Output. Urtė Zubkaitė
Haskell Modules. Input and Output Urtė Zubkaitė Modules in Haskell A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them
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 informationPROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming
PROGRAMMING IN HASKELL Chapter 10 - Interactive Programming 0 Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start and give all their
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 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 informationGHCi: Getting started (1A) Young Won Lim 6/3/17
GHCi: Getting started (1A) 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 and Haskell. Aden Seaman
Introduction to Functional Programming and Haskell Aden Seaman Functional Programming Functional Programming First Class Functions Expressions (No Assignment) (Ideally) No Side Effects Different Approach
More informationCSCE 314 Programming Languages. Monadic Parsing
CSCE 314 Programming Languages Monadic Parsing Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a string of characters (or a set of tokens) as input and determines its syntactic structure.
More informationCSCE 314 Programming Languages. Type System
CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be
More informationCS 360: Programming Languages Lecture 12: More Haskell
CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due
More informationGHCi: Getting started (1A) Young Won Lim 5/31/17
GHCi: Getting started (1A) 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 information1. Function Type Declarations (12 points):
1. Function Type Declarations (12 points): For each of the following function definitions, correctly complete the preceding type declaration. Be sure to include any necessary class constraints. (A) mystery1
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 informationIO Monad (3C) Young Won Lim 1/6/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 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 informationLazy Functional Programming in Haskell
Lazy Functional Programming in Haskell David Raymond Christiansen 25 November, 2013 What is Haskell? 2 What is Haskell? Pure functional language: no side effects 2 What is Haskell? Pure functional language:
More informationInformatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lecture 11 Data Representation Don Sannella University of Edinburgh Part I Complexity t = n vs t = n 2 10.4 9.6 8.8 8 7.2 6.4 5.6 4.8 4 3.2 2.4 1.6 0.8 0 0.8 1.6 2.4
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 informationParsing Combinators: Introduction & Tutorial
Parsing Combinators: Introduction & Tutorial Mayer Goldberg October 21, 2017 Contents 1 Synopsis 1 2 Backus-Naur Form (BNF) 2 3 Parsing Combinators 3 4 Simple constructors 4 5 The parser stack 6 6 Recursive
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 informationLecture 8: Summary of Haskell course + Type Level Programming
Lecture 8: Summary of Haskell course + Type Level Programming Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 31, 2017 Principles from Haskell
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 informationHaskell. COS 326 Andrew W. Appel Princeton University. a lazy, purely functional language. slides copyright David Walker and Andrew W.
Haskell a lazy, purely functional language COS 326 Andrew W. Appel Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel Haskell Another cool, typed, functional programming language
More informationHaskell & functional programming, some slightly more advanced stuff. Matteo Pradella
Haskell & functional programming, some slightly more advanced stuff Matteo Pradella pradella@elet.polimi.it IEIIT, Consiglio Nazionale delle Ricerche & DEI, Politecnico di Milano PhD course @ UniMi - Feb
More informationPrinciples of Programming Languages (H)
Principles of Programming Languages (H) Matteo Pradella December 1, 2017 Matteo Pradella Principles of Programming Languages (H) December 1, 2017 1 / 105 Overview 1 Introduction on purity and evaluation
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 information