Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain purpose of Kleisli Categories from the programmer's perspective #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/content/1.4/kleisli-categories.tex
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ \section{Kleisli Categories}
programs that in imperative languages are traditionally implemented
using side effects.

But, what is the real purpose of this concept from the perspective of a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I partially agree with this statement because I believe the real purpose of this concept for programmers is a composition for a monadic functions.
e.g.(code is scala)
type Foo = Kleisli[IO, Int, String]
type Bar = Kleisli[IO, String, String]
can be easily composed like Bar compose Foo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the case it would be good if you can add on upon the content I’ve written.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the case it would be good if you can add on upon the content I’ve written.

Hi, I'll try to add a description tomorrow

Copy link

@a-nigredo a-nigredo May 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately, I have time to write today:

But, what is the real purpose of this concept from the perspective of a programmer?
Since arrows in Kleisli category is monadic functions and we know from a category definition that arrows have to be composable inside a category.
So the Kleisli category just gives us a composition for monadic function.

Let's say we have to read text from a file and save it to a database.

We have 2 arrows:

type FileReader = String=> IO[String]
type DbWriter = String => IO[Unit]

It is not possible to compose them since FileReader returns IO[String] not String as DbWriter expects. So what can we do?
Just wrapped functions by Kleisli

type FileReaderKleisli = Kleisli[IO, String, String]
type DbWriterKleisli = Kleisli[IO, String, Unit]

and now we can easily compose them as DbWriterKleisli(DbWriter) compose FileReaderKleisli(FileReader)

All code is written in Scala(https://www.scala-lang.org/) and Kleisli type signature is taken from https://typelevel.org/cats/datatypes/kleisli.html

but I don't have time to change pull request directly.
@wongjiahau could you please add and correct if needed my text and add to your pull request?

programmer? Improved code maintainability. Recall that the \code{process}
function can actually be written without using the fish operator:

\begin{snip}{hs}
process :: String -> Writer [String]
process s =
let (s' , log) = upCase s in
let (s'', log') = toWords s' in
(s'', log ++ log')
\end{snip}

However, this piece of code is not only harder to comprehend, it is also
difficult to modify (imagine that you want to add new operations or
change the sequence of exisitng operations). That is why we need to leverage
the power of Kleisli Categories along with the fish operator.

\section{Challenge}

A function that is not defined for all possible values of its argument
Expand Down Expand Up @@ -443,4 +460,4 @@ \section{Challenge}
Compose the functions \code{safe\_root} and \code{safe\_reciprocal} to implement
\code{safe\_root\_reciprocal} that calculates \code{sqrt(1/x)}
whenever possible.
\end{enumerate}
\end{enumerate}