© M. Winter COSC 4P41 – Functional Programming 11. 1 Further Features of Haskell Records data Customer = Customer { customerID :: Int, customerName ::

Slides:



Advertisements
Similar presentations
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
Advertisements

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.
© M. Winter COSC 4P41 – Functional Programming Abstract data types (ADTs) An ADT is a data type together with some functions to manipulate elements.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Recursive Data Types Koen Lindström Claessen. Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents.
Data Management and File Organization
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Introducing Monads Lecture 3, Designing and Using Combinators John Hughes.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Designing a Database Unleashing the Power of Relational Database Design.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets.
Lightweight Concurrency in GHC KC Sivaramakrishnan Tim Harris Simon Marlow Simon Peyton Jones 1.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Stateful Aspects Monitoring and Lazy Semantics. Base program fact :: Int -> Int -> Int fact :: Int -> Int -> Int fact n acc = if n == 0 then acc else.
Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types.
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
0 Functors in Haskell Adapted from material by Miran Lipovaca.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L13-1 October 26, 2006http:// Monadic Programming October.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Grab Bag of Interesting Stuff. Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
Advanced Functional Programming Tim Sheard 1 Lecture 6 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
Advanced Functional Programming 2010
Haskell Chapter 8.
Haskell Chapter 7.
Koen Lindström Claessen
CS 4450: Principles of Programming Languages
INF 212 Analysis of Prog. Langs Funktional Programming -- Monads
Haskell programming language.
Accessing Files in Java
Register Use Policy Conventions
Main Points of Haskell Functional programming Laziness
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
Haskell Dealing with impurity 30-Nov-18.
Advanced Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 8-Apr-19.
Linked Lists.
Grab Bag of Interesting Stuff
PROGRAMMING IN HASKELL
Pages NOTE: On a single line, any point that is a solution will fall on the line. Any point that is NOT a solution, will be a point that is NOT on.
Programming Languages
Tree.
Haskell Dealing with impurity 29-May-19.
Haskell Dealing with impurity 28-Jun-19.
PROGRAMMING IN HASKELL
Advanced Functional Programming
How do Haskell developers use monads?
Presentation transcript:

© M. Winter COSC 4P41 – Functional Programming Further Features of Haskell Records data Customer = Customer { customerID :: Int, customerName :: String, customerAddress:: Address } The declaration defines the new data type Customer and its constructor Customer :: Int -> String -> Address -> Customer. In addition it also defines the following functions: customerID :: Customer -> Int customerName :: Customer -> String customerAddress:: Customer -> Address

© M. Winter COSC 4P41 – Functional Programming myCustomer = Customer 1234 "Peter Pan" neverland Or alternatively: myCustomer = Customer { customerID = 1234, customerName = "Peter Pan", customerAddress= neverland }

© M. Winter COSC 4P41 – Functional Programming Concurrency/Threads Creating a thread: forkIO :: IO () -> IO ThreadId Example: import Control.Concurrent (forkIO) import qualified Data.ByteString.Lazy as L import Codec.Compression.GZip (compress) main = do putStr "Enter a file to compress> " name <- getLine if null name then return () else do content <- L.readFile name forkIO (compressFile name content) main where compressFile path = L.writeFile (path ++ ".gz"). compress

© M. Winter COSC 4P41 – Functional Programming Foreign Function Interface import Foreign import Foreign.C.Types foreign import ccall "math.h sin" c_sin :: CDouble -> CDouble Points to consider: Side-effects in C function –Use monad IO Thread safe code –Multiple threads are extremely common in Hakell code!

© M. Winter COSC 4P41 – Functional Programming Monad Transformers Motivation: We want to combine the monads IO and Maybe in order to be able to do in/output as well as to catch errors. The result should be a new monad. Bind for IO: (>>=) :: IO a -> (a -> IO b) -> IO b Bind for Maybe: (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b How do we get the new bind??? Solution: Create a monad transformer for Maybe that allows to wrap any monad m around Maybe, i.e., m (Maybe a).

© M. Winter COSC 4P41 – Functional Programming Monad Transformers Properties for Monad Transformers t: 1.t :: (* -> *) -> * -> * 2.instance (Monad m) => Monad (t m) where... 3.instance MonadTrans t where lift :: Monad m => m a -> t m a 4.lift. return = return 5.lift (m >>= f) = lift m >>= (lift. f)

© M. Winter COSC 4P41 – Functional Programming import Control.Monad import Control.Monad.Trans newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } bindT :: (Monad m) => MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b x `bindT` f = MaybeT (runMaybeT x >>= maybe (return Nothing) (runMaybeT. f)) returnT :: (Monad m) => a -> MaybeT m a returnT = MaybeT. return. return

© M. Winter COSC 4P41 – Functional Programming instance (Monad m) => Monad (MaybeT m) where return = returnT (>>=) = bindT instance MonadTrans MaybeT where lift m = MaybeT (m >>= (return. Just))

© M. Winter COSC 4P41 – Functional Programming Numbering Trees using State Monad newtype StateT s m a = StateT { runStateT :: s -> m (a,s) } type State s = StateT s Identity evalState :: State s a -> s -> a get :: (Monad m) => StateT s m s get :: State s s in the case m = Identity put :: (Monad m) => s -> StateT s m () put :: s -> State s () in the case m = Identity

© M. Winter COSC 4P41 – Functional Programming import Data.List import Control.Monad.Trans.State.Lazy data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show type Table a = [a] tree = Node "Moon" (Node "Ahmet" Empty Empty) (Node "Dweezil" (Node "Ahmet" Empty Empty) (Node "Moon" Empty Empty)) Moon Dweezil Ahmet Moon

© M. Winter COSC 4P41 – Functional Programming numberTree :: Eq a => Tree a -> State (Table a) (Tree Int) numberTree Empty = return Empty numberTree (Node x t1 t2) = do num <- numberNode x nt1 <- numberTree t1 nt2 <- numberTree t2 return (Node num nt1 nt2) numberNode :: Eq a => a -> State (Table a) Int numberNode x = do table <- get case (elemIndex x table) of Nothing -> do put (table++[x]) return $ length table Just n -> return n runProg :: Eq a => Tree a => IO () runProg t = print $ evalState (numberTree t) []

© M. Winter COSC 4P41 – Functional Programming Using State Monad Transform numberNode :: (Eq a, Show a) => a -> StateT (Table a) IO Int numberNode x = do table <- get case (elemIndex x table) of Nothing -> do put (table++[x]) lift $ putStrLn ("New value: " ++ show x) return $ length table Just n -> return n numberTree :: (Eq a, Show a) => Tree a -> StateT (Table a) IO (Tree Int) numberTree …

© M. Winter COSC 4P41 – Functional Programming evalStateT :: (Monad m) => StateT s m a -> s -> m a runProg :: (Eq a, Show a) => Tree a => IO () runProg t = do t <- evalStateT (numberTree t) [] print t Example session: > runProg tree New value: "Moon" New value: "Ahmet" New value: "Dweezil" Node 0 (Node 1 Empty Empty) (Node 2 (Node 1 Empty Empty) (Node 0 Empty Empty))