welcome back an I'll be at some fun with curry and two balls and function types
about we're going to talk about now is at
type clash and polymorphic functions
an Baltimore for functions
are functions dead are not defiant
on concrete types but that are defiant using type variable
and here is a.m. a very polymorphic example of a polymorphic funtion
the function Lancs so if I want to calculate
the length of a list every lead doesn't matter at all
what the type of the elements of the list is
if I have the list 123 order lists
it three characters ABC help
both in both cases the length love this list three
so the type of the elements doesn't matter
when I want to calculate the length of the list when we look at the type a
blank here
we see it length has type list of a
arrow and and that a is a type very ball
has called thats you can see that biggest
types start with an uppercase per case
letter here so n you can see that and is a concrete die because it started in
obrigado I and the a here
the lower case a that's a type variable
to really but this type says is that
length has type for all types a
list a to end an
and if you're used to Java generics are c-sharp generics
and typically the
type fair ball is declared explicitly
am and in the type of the function so
for example in c-sharp you will be fine length of tea
and and and would have argument list of tea
and return an int but in Haskell this dick this
a here is since it's a dive hairball
its implicitly quantified am so you don't have to kind
appointed an angle brackets or anything again
and has college optimized for this case ride
but the price that you pay is that type variables start
lower case and type start with an uppercase
K but if we look at this type here lank
has type for any type A Lancs takes a list of values of Taipei
and returns an integer again this is what you expect
and it doesn't matter what did I i of the
element is of the list your only interested
in counting how many there are but you're not
interesting interested what these values actually
again so that should hear the length of false true
then in this case a hedged eyeball
s2 and here we have a list of
integers so a would-be in that case ent
but the land here is for a showboat
in both cases an we can use a length
of died for all AAA to end
and in the first case a is pool and in the second case
agent as a set
the price we pay for not having to declare
you know on what type length is polymorphic
is that we differentiate the namespaces
of types and dive verboten ist and has called
type variables must begin with a lower case letter
and by convention reuse
ABC and Zach are four types variables
barriers in languages like Java our C sharp
typically we use S&T are
for type variables capital St nr
in Haskell its lower case AB and C so
where in sharp you would write list ft
in as call you dried list Ave
okay here are an
a number of polymorphic functions from the Haskell standard prelude to the
stand the prelude is dick and Haskell SDK or the Haskell BC I L
and a standard library like every language comes with the standard library
and and the types of these functions
reveal why the law about their implementation show for example let's
look at the first one
first takes ate up all of
a NB and returns a value of type A
but can destruction do I'll
it think should %uh Paul a and it returns a value of type A
so really it's quite obvious from that type
that what this function does is it gonna readers the first
and element of the double now let's look at the type of hat
had to take a less to pay for any type A
and readers and a in this case
you have more choice right because you know I given a list of Taipei
if I have to return a value of type A I can pick
Andy value in that last and had to get you the first one
now one of the interesting things here's what happens
when a.m. you give head to the empty list
in that case this function will return
undefined are well-drawn exception an
so that is not given in this types are the types said
for all lists you now of Taipei
and will return a value of type A an
but that's not really true because when I gave it the empty list
it will fail and not really give me
value third example here take
takes an integer and readership function
that takes a list and returns a list remember
when we have a dive with arrows the
parentheses show she to the right to take
is if we put in the brand she's would be end arrow
brent is %eh lester a to a
take takes number and
last takes the first and elements have the less and returns that
not an extra engineers ship
is each Nystrom and it takes two lists
and and readers a list of bears
and the way it works is dead it takes to do less and
takes each element these delicious and combines them
into a double now I think
dive ship looks a little bit nicer
when you don't write that encourage form so when you would ride it i blessed
an isn't %uh Paul's it would have died
couple a blessed a endless to be
to list of double and a comedy show there you can really see in the type
that and sip an
in our tourists to Leicester Oct and then the loss function here
an that's perhaps the simplest function you can imagine has college the identity
function
it takes at value of Taipei and breeders valuable time
died a for any a a man if you think a little bit about that
the only thing this function can do is you know just return its value because
otherwise
it will be hard to work on Anne
Taipei okay so an
the identity goes from
ole a2a jodie
only thing really that it can do is it can take the argument
and immediately returned if you're interested
in in this thing where you know you look at the type and then you get the
property
about the implementation you she should read
bill Watters paper teams for free and
and yard that paper will explain that all
everybody more for dive gives you a tear and that holds
and for that function and
but again that's a little bit advance so
you can read that if you want there will be a link to the paper
on the core structure the Las topic
ever going to do and in the context functions
are overloaded functions
we all know overloaded function most languages support them
for example in Java you can overlords
function with different parameter type so
a.m. I can define a function food addiction and
and then I can have an overloaded takes a ball and then I can I have a number
another overloaded take Celeste and the name
up that function is to shame but the types of the parameters
is different and Haskell overloaded is
you done in a slightly different form and
it corresponds more to the notion of interfaces
in object-oriented languages joined Haskell
when you overloaded function is really mean
me means not overloading in the object-oriented sans
it really mean stager restricting
the die did the
types of the parameters so in this case when I want to see some
the elements on the list and I cannot say
that type some hedge type lester a2a
because wat by and
have alleged characters I cannot
at characters together and maybe you can say all I can define threat characters
but thats a tan
that we have a list of tuples well
in that case some on the list of top also of Taipei gonna be
doesn't really return a double up a committee that doesn't make sense
so what we want to do is you want to restricted type
the elements in the list such that they can be some
and that is what has school overloading
issues so what you see here in this type some as type
numb a double arrow
list a to a so what this means
is that some doesn't take
alleged values have to pay for any a
but only values of Taipei
that
are in this numb Kalash
or N if you think about
and object orientation it only takes a list of
value of type A that implemented in phnom
interface okay so I you see this notation here numb
a arrow double arrow got roughly
if your job I receive sharp or PHP programmer
ya at CBS plus programmer think of this
as you know restricting the values are
on which this can work job you have the same thing if you have a
generic function you can restrict
the values by requiring
that the types implement the shirt and interface in c-sharp you can do the same
thing in
you can most languages allowed as and has got no
exception alright SoCon takes a list
and readers it into a value but they can
only do that when values in the list
support numb class Gordon um interface
okay
so in this case we can do chum of integers
then in that case agent and we know
that integers and our number so
that's fine we will see what this number lies in a tournament like basically says
that you can do addition and multiplication and so on
second one here is a unless the floating-point numbers
I can sum them as well but here
we get it I para de las 10 year we get it i pair
because here we have a list of characters and
characters don't support dunno interface
our game show characters are not an instanceof the number loss
and Haskell speak and has clashed
the b-sides nominee several type classes you can define your own type classes we
will see how to do that later
and in this case
the numb class says that you have
addition K what that means is that
the type of addition takes
value of type A readers a function that takes another value
Taipei and then burned returns a value of type A
but it's not only for that's not for all values of k
only values in the number clash similarly
when you want to compared to values for equality
they have to be in the EQ class showing here you see
that the equality operator as type
EQ okay double arrow a arrow
a arable and finally
there's the type of values that you can compare
so the order types and their less than has type
or no pay arrow a RA arable
okay so here are some
exercises the exercises and you
you will do an on the website so I wont give you
and the answers right now but their
actual sizes will be on the website but I'll walk you through this exercise
an out to get you familiar showed the question is like one ish the type
of an the value of their lists
it three characters AB and C
and so on and so on show and
please doing these exercises biggest day well I make sure
that you understand the idea of types that we
an explained in this lecture
and many are a couple of more
in complicated exercises an
where you don't get a value and you have to compute the type
but you get a function and now you have to
a computer died function I'm and the trick here
and this is a.m. and well I'm going to
you know the secret I'm going to reveal to you
is that you can use GHz
to check your answers show you're doing the exercises don't go I got
try them out randomly let you do it
open up GATT and used a go long time
and functionality to do these exercises
okay so you you you are
sure that you can score a hundred percent on every one of them
so thank you and happy hacking so use GC
to do your exercises and and if you don't use haskell
and but another language make sure that you're also using
your compiler to check your tight okay
because we as humans should not do this type checking
that's why we have computers to do that for us
thank you very much and see you next week