Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8...

45
Outline 1 Using GHCI – the Glasgow Haskell compiler interactive system [screen/master2.tex] 2 Basic expressions and primitive data types. Integers, reals, characters, unit. 3 Tuples, lists, list comprehension, basic functions 4 Patterns 5 Polymorphism (lists, functions), type variables. 6 Curry, uncurry, isomorphism [fun.tex] 7 Higher-order functions, partial application, map, fold, type reconstruction 8 Algebraic data types [haskell_data.tex] 9 Main program, interact, compilation [haskell.tex] Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 1 / 45

Transcript of Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8...

Page 1: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Outline1 Using GHCI – the Glasgow Haskell compiler interactive system

[screen/master2.tex]2 Basic expressions and primitive data types. Integers, reals, characters, unit.3 Tuples, lists, list comprehension, basic functions4 Patterns5 Polymorphism (lists, functions), type variables.6 Curry, uncurry, isomorphism [fun.tex]7 Higher-order functions, partial application, map, fold, type reconstruction8 Algebraic data types [haskell_data.tex]9 Main program, interact, compilation [haskell.tex]

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 1 / 45

Page 2: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Canonical Value

Canonical value. A canonical value is one which cannot be rewritten further.For example, 2+3 is not canonical, it evaluates to 5; 5 is a canonical value.For example, not True is not canonical, it evaluates to False; False is acanonical value.See canonical in the “The on-line hacker Jargon File,” version 4.4.7, 29 Dec 2003.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 2 / 45

Page 3: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Recall. The principle, Haskell primitive data types are: Integer (arbitrary precision),double (floating-point), char, bool, () (unit).The Haskell pre-defined, compound data types are: tuples, lists, functions.The programmer wants to define their own data types. Haskell has one primarymechanism for creating data types. The types so-created are called algebraic types.Algebraic type definitions are simple, easy and influencing programming languagedesign.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 3 / 45

Page 4: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Data, the values the programmer computes with, is divided up into distinctcollections called data types.

We write:

data value :: data type

to mean that that data value has that data type.Occasionally we write:

data type :: *

to mean that that data type has kind *, i.e., it is a data type.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 4 / 45

Page 5: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Data is manipulated by functions according to rules described by functiondefinitions.Data is constructed by constructors according to rules described by data typedefinitions.

We now examine data type definitions. Data type definitions in Haskell give rise toan elegant system of types called algebraic data types.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 5 / 45

Page 6: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Here are four simple examples of data type definitions (which are calledenumerated types in other programming languages):data Bool = True | Falsedata Color = Red | Green | Bluedata Unit = Unitdata Day = Mon|Tue|Wed|Thu|Fri|Sat|Sun

Types and constructors capitalized.

These definitions completely describe the data types Bool, Color, Unit, and Day.These types have exactly two, three, one, and seven elements (data values),respectively.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 6 / 45

Page 7: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

These data type definitions introduce the elements of the data type. Here is a listof five of the 13 elements introduced above.True :: BoolFalse :: BoolRed :: ColorUnit :: UnitWed :: Day

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 7 / 45

Page 8: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

You will have noticed the ambiguity of the identifier Unit

Unit :: UnitUnit :: *

Since the universe of data types is kept separate from the universe of data values,this will cause little confusion.Since it is sometime burdensome to create name, it is not unusual to use the samename in both universes.A data type with one element is less useful, than the very common data type Bool.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 8 / 45

Page 9: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

A new data type might be desired whose elements are constructed out of elementsof another data type. This is more useful, usually, than simply enumerating all theelements.Next is an example of food with nuts and colored dye.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 9 / 45

Page 10: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

data Food = Cake Bool | Cookies Bool | CandyCane Color

Cake ... are constructors. New items are constructed by applying the constructorsto elements of other data types. In this simple example only a finite number, seven,food items are constructable.Cake True :: Food -- cake with nutsCake False :: Food -- cake without nutsCookies True :: FoodCookies False :: FoodCandyCane Red :: Food -- candy cane with red dyeCandyCane Green :: Food -- candy cane with green dyeCandyCane Blue :: Food -- candy cane with blue dye

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 10 / 45

Page 11: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Constructors are special functions. The application of constructors, like functionsin general, is indicated syntactically by juxtaposition.Thus,Cookies True

is an expression consisting of a function applied to a data value.

Construction of data is the same as function application.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 11 / 45

Page 12: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Constructors, like all programs/functions, are data values. The data type to whichthey belong depends on the domain and on the range.Cake :: Bool -> FoodCookies :: Bool -> FoodCandyCane :: Color -> Food

where the arrow is a constructor of data types, in other words,The binary, infix arrow is an example of a type constructor. It is way ofrepresenting a data type by combining two other data types, the domain and therange of the function. Without it we are lost . . ..(->) :: * => (* => *)

Eventually we will define our own type constructors.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 12 / 45

Page 13: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Our universe is now filled with an infinite number of data types. Here are just afew:Bool :: * -- Bool is a data typeBool ->Food :: * -- Bool ->Food is a data typeBool ->Bool :: * -- Bool ->Bool is a data type(Bool ->Bool)->Bool :: *Bool ->(Bool ->Bool) :: *Bool ->(( Bool ->Bool)->Bool) :: *Bool ->(( Bool ->Bool)->Bool)->Bool :: *

We take (->) is be right-associative so that we may omit some parentheses.Elements of data types constructed with (->) are said to be functions.It is not surprising that we can construct functions by means other than data typedefinitions. These functions are not called constructors. Only thoseprogram/functions explicitly introduced in data type definitions are calledconstructors.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 13 / 45

Page 14: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Only constructors (and not other functions) can be used in patterns.

All constructors (even especially user-defined constructors) can be used in patterns.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 14 / 45

Page 15: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Pattern Matching

Functions can be constructed by a list of equations/rules for transforming one datavalue to another data value.not :: Bool -> Boolnot True = Falsenot False = True

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 15 / 45

Page 16: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Pattern Matching

Functions can be constructed by list of equations/rules for transforming one datavalue to another data value.containsNuts :: Food -> BoolcontainsNuts (Cake True) = TruecontainsNuts ( Cookies True) = TruecontainsNuts ( CandyCane Red) = False

Omitting a case is bad (may result in a error at runtime.)

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 16 / 45

Page 17: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Enumerating all the possibilities is tedious:containsNuts :: Food -> BoolcontainsNuts (Cake True) = TruecontainsNuts (Cake False) = FalsecontainsNuts ( Cookies True) = TruecontainsNuts ( Cookies False) = FalsecontainsNuts ( CandyCane Red) = FalsecontainsNuts ( CandyCane Green) = FalsecontainsNuts ( CandyCane Blue) = False

For this reason we have variables.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 17 / 45

Page 18: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Enumerating all the possibilities is tedious:containsNuts :: Food -> BoolcontainsNuts (Cake True) = TruecontainsNuts ( Cookies True) = TruecontainsNuts x = False

The order of the equations/rules matter. The last equation/rule applies to anydata value constructed other than the ones lists previously. It is an “else” or“otherwise” case — a catch-all.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 18 / 45

Page 19: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Finally, because naming a variable and not referring to it, is in somewhat odd,Haskell permits anonymous variables (called the wildcard pattern).containsNuts :: Food -> BoolcontainsNuts (Cake True) = TruecontainsNuts ( Cookies True) = TruecontainsNuts _ = False

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 19 / 45

Page 20: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Assuming we have a data type Float, we may wish to use it as the size of circleand square shapes.data Shape = Circle Float | Square Float

area ( Circle r) = pi*r*rarea ( Rectangle s1 s2) = s1*s2

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 20 / 45

Page 21: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Assuming we have a (complex) data type J and P for images. We can define a datatype for multiple kinds of images.

data Image = JPEG J | PNG P

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 21 / 45

Page 22: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Suppose you want to model a room with a light and two light switches

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 22 / 45

Page 23: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

data State = On | Off deriving Eqdata System = Switches State StateisOn ( Switches x y) = if x==y then False else True

Or, simply:isOn ( Switches x y) = x/=y

Type and constructor names must be capitalized. If equality is desired, then aderiving clause is needed. Same for printing: the deriving (Eq, Show) clauseis needed.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 23 / 45

Page 24: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Deriving

data Color = Red | Green | Bluederiving (Show ,Eq)

data Day = Mon|Tue|Wed|Thu|Fri|Sat|Sunderiving (Show ,Eq ,Ord)

Show allows Haskell to print data structures. Eq permits equality testings. Ordderives (the obvious) order on the data values.

If you are using these type definitions in the interactive system, one will most likelywant Show at least.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 24 / 45

Page 25: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

You may wonder why we don’t define tuples next.Tuples seem a lot easier and more intuitive that higher-order functions. And itcertainly is possible.( , ) :: * => * => *

But we are going to introduce higher-order functions anyway, so we might as welldispense with tuples conceptually.

This simplification was used by Frege, Schönfinkel, and Curry. The data values(programs/functions) in A× B → C are isomorphic to A→ B → C . So, we don’tneed tuples.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 25 / 45

Page 26: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

data BoolPair = BoolPair Bool Bool

BoolPair :: Bool -> Bool -> BoolPairBoolPair True :: Bool -> BoolPair( BoolPair True) False :: BoolPair

f :: Bool -> Bool -> BoolPairf x y = BoolPair (not x) (not y)

-- projection functionfirst :: BoolPair -> Boolfirst ( BoolPair x _) = x

Just as easy and natural as positional correspondence in traditional multi-argumentfunctions.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 26 / 45

Page 27: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Application Is Left Associative

All thisblah blah blah blah blah

does get overwhelming.

Remember application is left associative:((( blah blah) blah) blah) blah

And skilled Haskell programs can use techniques to make code more readable.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 27 / 45

Page 28: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

data MixedPair = MixedPair Day Colordata MixedTriple = MixedTriple BoolPair Day Color

data MixedUnion = Pair MixedPair | Triple MixedTriple-- 21 mixed pairs + 84 mixed triple = 105

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 28 / 45

Page 29: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

data MixedPair = MixedPair Day Colordata MixedTriple = MixedTriple BoolPair Day Color

data MixedUnion = Pair MixedPair | Triple MixedTriple-- 21 mixed pairs + 84 mixed triple = 105

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 29 / 45

Page 30: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Types

Type constructors can take types as parameters.data Maybe a = Nothing | Just a

maybe :: b -> (a -> b) -> Maybe a -> bmaybe n _ Nothing = nmaybe _ f (Just x) = f x

Maybe is a highly significant (and predefined) type constructor (kind *->*) inHaskell.See the class Optional<T> introduced in Java 8.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 30 / 45

Page 31: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Types

Type constructors can take types as parameters.data Either a b = Left a | Right b

either :: (a -> c) -> (b -> c) -> Either a b -> ceither f _ (Left x) = f xeither _ g (Right y) = g y

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 31 / 45

Page 32: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Data types can be recursive, as in the following:data T = Con1 Bool | Con2 T

data Bad = Con Bad

Contrary to those bad examples, recursive and polymorphic types are the “beesknees,” the “cat’s pajamas,” and the “the snake’s hips.”data Nat = Nil | Succ Natdata IList = Nil | Cons Integer IListdata PolyList a = Nil | Cons a ( PolyList a)

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 32 / 45

Page 33: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Trees

A list is just a tree with one branch.data Nat = Zero | Succ Natdata List = Nil | Cons () List

Natural numbers are isomorphic to unit lists.See examples of trees in Hudak, PPT, Ch7.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 33 / 45

Page 34: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Trees

The simplest binary treedata SimpleTree = SimLeaf | SimBranch SimpleTree SimpleTree

SimLeaf :: SimpleTreeSimBranch SimLeaf SimLeaf :: SimpleTreeSimBranch SimLeaf ( SimBranch SimLeaf SimLeaf ) :: SimpleTreeSimBranch ( SimBranch SimLeaf SimLeaf ) SimLeaf :: SimpleTree

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 34 / 45

Page 35: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Trees

data IntegerTree = IntLeaf Integer |IntBranch IntegerTree IntegerTree

IntLeaf 4 :: IntegerTreeIntLeaf 8 :: IntegerTreeIntBranch ( IntLeaf 23) ( IntLeaf 45) :: IntegerTree

IntBranch ( IntLeaf 7) ( IntBranch ( IntLeaf 23) ( IntLeaf 45))IntBranch ( IntBranch ( IntLeaf 23) ( IntLeaf 45)) ( IntLeaf 78)

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 35 / 45

Page 36: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Trees

data InternalTree a = ILeaf |IBranch a ( InternalTree a) ( InternalTree a)

data Tree a = Leaf a | Branch (Tree a) (Tree a)

data FancyTree a b = FLeaf a |FBranch b ( FancyTree a b) ( FancyTree a b)

data GTree = GTree [GTree]data GPTree a = GPTree a [ GPTree a]

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 36 / 45

Page 37: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Trees

data BinQuadTree = Zero | One |Quads BinQuadTree BinQuadTree BinQuadTree BinQuadTree

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 37 / 45

Page 38: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Nested Types

data List a = NilL | ConsL a (List a)data Nest a = NilN | ConsN a (Nest (a,a))data Bush a = NilB | ConsB a (Bush (Bush a))

data Node a = Node2 a a | Node3 a a adata Tree a = Leaf a | Succ (Tree (Node a))

Bird and Meertens, Nested Datatypes, 1998.Hinze, Finger Trees.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 38 / 45

Page 39: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Haskell Classes

Haskell classes are roughly similar to a Java interface. Like an interface declaration,a Haskell class declaration defines a protocol for using an object rather thandefining an object itself. C++ and Java attach identifying information (such as aVTable) to the runtime representation of an object. In Haskell, such information isattached logically instead of physically to values, through the type system. There isno access control (such as public or private class constituents) built into theHaskell class system. Instead, the module system must be used to hide or revealcomponents of a class.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 39 / 45

Page 40: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

e1 >>= e2

(>>) :: Monad m => m a -> m b -> m be1 >> e2 = e1 >>+ (|_ -> e2)

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 40 / 45

Page 41: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

do { e } edo { x<-e; es } e >>= \x -> do {es}do { e; es } e >> do {es}do {let ds; es} let ds in do {es}

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 41 / 45

Page 42: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

[ (x, bar) | (x,y) <- foos ,x < 2,bar <- bars ,bar < y ]

do (x,y) <- foosguard (x < 2)bar <- barsguard (bar < y)return (x, bar)

foos >>= \(x, y) ->guard (x < 2)>> bars>>= \bar ->guard (bar < y)>> return (x, bar)

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 42 / 45

Page 43: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

= "is define to be" / "is define as"== "is equal to" / " equals / equals "_ whatever / wildcard pattern@ as:: has type: cons

\ lambda

++ append!! index. compose / dot

<*> ap(ply)>>= bind>> then<=< left fish / left Kleisli composition operator>=> right fish / right Kleisli composition operator

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 43 / 45

Page 44: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Main Haskell

Two major tenets of problem solving with a computer.1 Decompose complex problems into simple problems and compose the

solutions.2 Programs are functions of the inputs.

Both of these in clear display in a functional language.

The second one gets loss in the fog of programs as sequence of mutating actions,specifically printing.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 44 / 45

Page 45: Introduction to Haskell - Algebraic Data Typesryan/cse4250/notes/haskell_data.pdf8 Algebraicdatatypes[haskell_data.tex] 9 Mainprogram,interact,compilation[haskell.tex] Ryan Stansifer

Main Haskell

Functional programming requires that everything be a function. A Haskell mainprogram is a function from strings to strings. We think of programs as beingfunctions from the input to the output. Nonetheless, it is strange at first to thinkof the IO behavior of a programming as function from the input string to theoutput string.

Ryan Stansifer (CS, Forida Tech) Introduction to Haskell (Algebraic Data Types) 19 April 2020 45 / 45