Don Metro
Don Metro

Reputation: 25

how to read haskell type signature?

So I put this statement on my ghci

jkl x f y = f (map (+y) x)

And I got this out put back.

jkl :: Num b => [b] -> ([b] -> t) -> b -> t

But I'm confused when I read it. From my understanding jkl is type num that takes [b], [b] and t, and b. in the end it will output t. is that the right way to read it?

Upvotes: 2

Views: 377

Answers (2)

Fyodor Soikin
Fyodor Soikin

Reputation: 80734

Whatever is between the double colon :: and the fat arrow => is called constraints. In this case, you have one constraint: Num b. This constraint demands that, whatever type b turns out to be, it must be an instance of type class Num.

After the fat arrow, you get types of function parameters, and at the very end type of its result.

Parameters:

  1. [b] - a list of values of type b, whatever that is.
  2. [b] -> t - a function that takes a list of b and produces a single t.
  3. b - a single value of type b.

And finally, function result is t.

Upvotes: 7

chi
chi

Reputation: 116139

jkl :: Num b => [b] -> ([b] -> t) -> b -> t

Whoever calls jkl has to

  • choose types b and t
  • guarantee that b was chosen among numeric types (the Num b constraint)
  • pass a [b] (list of b) as first argument
  • pass a function [b] -> t as second argument (i.e. taking [b] and returning a t)
  • pass a b as third argument
  • receive t back as a final result

Upvotes: 12

Related Questions