0

Example - function runKleisli from Haskell base module

newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }

That means runKleisli have 1 argument with type a and must return function m, which have 1 argument b?

srghma
  • 4,770
  • 2
  • 38
  • 54
  • 1
    It means `m` is a type of *kind* `* -> *`; that is, `m` is a *type constructor* that accepts a single argument, like `Maybe` or `Either String`. – Alexis King Oct 25 '16 at 18:52
  • @AlexisKing, Thanks, you can post it as answer and I will accept it – srghma Oct 25 '16 at 18:54
  • 1
    It is similar to how generics work in Java or C#, or to how templates work in C++. In Java for example the function's signature would be something like `M runKleisli(A a)`. This isn't an exact analogy though, since the Haskell signature you provided is a lot more generic than the Java signature I gave. – Paul Manta Oct 25 '16 at 19:03

1 Answers1

5

It means m is a type of kind * -> *; that is, m is a type constructor that accepts a single argument, like Maybe or Either String. When you instantiate Kleisli and perform the substitution yourself, you can see how this works out.

For example, consider something like Kleisli Maybe String Integer. The substituted type of runKleisli would be String -> Maybe Integer, and you can see how the m is being used as a type constructor.

For more information about kinds in Haskell, see What exactly is the kind "*" in Haskell?.

Community
  • 1
  • 1
Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • I just find out that I don't quite understand, I don't know monads for now, so can you answer, function `m` must **return** or **accept as argument** something with type `b`? – srghma Oct 25 '16 at 20:45
  • Question in context of this function `(*>) :: (a -> m b) -> (b -> m c) -> (a -> m c)` from [book of Anton Kholomiov page 86](https://anton-k.github.io/ru-haskell-book/files/ru-haskell-book.pdf)) – srghma Oct 25 '16 at 20:53
  • @bjornmelgaard `m` is not (necessarily) a function; it is just a type constructor. In the example I gave in my answer, `Kleisli Maybe String Integer` is just a simple wrapper around `String -> Maybe Integer`. Nothing more, nothing less. The `m` in that example is `Maybe`, which is obviously not a function. – Alexis King Oct 25 '16 at 20:55
  • Ok, `m b` means constructor and only constructor(?). Therefore in `class Category cat where \ id :: cat a a \ (>>) :: cat a b -> cat b c -> cat a c` cat is a constructor and only constructor with 2 arg too? – srghma Oct 25 '16 at 21:11
  • @bjornmelgaard Neither. When you write `Maybe Integer` is `Maybe` returning or accepting as argument something with type `Integer`? Answer: The question makes no sense because `Maybe` is not a function. – user253751 Oct 25 '16 at 21:32