Showing posts with label Haskel. Show all posts
Showing posts with label Haskel. Show all posts

Friday, October 10, 2014

Speaking on Lambda Jakarta: delivering intro to concurrency and asynchrony

Now it’s Lambda Jakarta meetup time! This meetup is on 4th October, 2014. Again, thanks to Traveloka for preparing the place for us to meet!

This time, Abdullah was presenting about the problem of concurrency titled: “Promise of Better Concurrency in Functional Programming: Big Win or BS?”. Yes, concurrency is everywhere now, and it’s becoming more and more relevant because of the fact that nowadays CPU is not getting speedier, but the CPU is keep getting denser with more core CPUs.

WP_20141004_004

Even on my laptop, the HP Envy beefed with intel CORE i7, it has 4 core and each core has 2 logical hyperthreading:

HPEnvy_TaskManager

This makes the logical overall CPU to be 8 logical CPUs! As we see, the clock speed is only at 2.39Ghz.

And this has becoming trend, not just from Intel. AMD has begun releasing microprocessor using this scheme/model.

Abdullah in his presentation was quoting this nice article from Herb Sutter (the Visual C++ lead dev of Microsoft) about “Free lunch is over.” published in 2005.

Herb_CPU_chart

He said that developer should not depend on CPU speed, instead they should begin to think on how to maximize the existing CPU cores. Otherwise the developed application will always run on one core and the rest of CPU core will be wasted. This of course will have negative impact not just on performance, but it will make the application to lose competitive factor of being efficient and support multi core.

Herb also added an undeniable fact of concurrency, it’s not just becoming relevant but it’s already relevant at the time of his writing!

The known concept of these maximizing cores to run at many cores, hence running multiple processes at a time is called parallel programming or on more abstract, parallelism.

This is the original link of the article: http://www.gotw.ca/publications/concurrency-ddj.htm

But concurrency doesn’t stop at just parallelism. Concurrency is also dealing with states, dealing with race conditions, and also traditional concept of operating system such as locks, semaphores, mutexes, and execution contexts.

Now execution contexts are becoming more relevant as the advances in hardware are allowing us to free us from having to wait for blocking operations such as I/O (including networks, printers). But then the software has to leverage this feature as much as possible by freeing current thread to be available for other execution context. The thread doesn’t have to be multithread, as you can still use the same thread but with different execution context.

This concept is known for asynchronous programming, or simply just asynchrony. Most programming languages handles this by providing state continuations such as “Futures” and “Promises”, or using complex callback with support for function as parameter (delegate in .NET, functional interface in Java 8, function in Haskell, Scala, and other functional programming languages).

I was also helping Abdullah to further explain asynchrony in the pragmatic perspective.

WP_20141004_016

The easiest way to explain asynchrony is if we are ordering food at a restaurant:

  1. We order the food/beverages and we are served by a waiter
  2. Waiter deliver the order list to the cook
  3. Waiter serves other customer without waiting the cook to complete our order
  4. Our order is complete, and the cook is telling the waiter by putting our finished order at the waiter tray (this is the callback)
  5. Waiter checks the tray, grabs the finished order, and delivers the order to us

I also promised the audience that the next meetup, I will deliver small presentation about asynchrony sample in detail Smile

The gory detail of concurrency is also nicely explained by this guy (formerly Oracle’s Java developer, now one of NodeJS founder) Bryan Cantrill in his excellent article: https://blogs.oracle.com/bmc/entry/concurrency_s_shysters

That’s all, folks!

This is the official landing page of this session’s Lambda Jakarta: http://www.meetup.com/Lambda-Jakarta/events/206904832/

If you live in nearby Jakarta, I encourage you to participate on this meetup, guys!

Sunday, July 27, 2014

Speaking on Lambda Jakarta meetup delivering F#

Hi all! Time for speaking Smile

The talk is on July 26, at Traveloka headquarter. Many thanks to Traveloka!

Before that, I have to tell you now in Indonesia we have special meetup for functional programming called Lambda Jakarta! This special interest meetup is not just functional programming as general, but for more specific interest such as F#, Haskell, Scala, Clojure, OCAML, R, LISP, RUST, Erlang as well!

Just look at their logo:

full_4361622

Found in 2011, now has more than 8 active participants!

It was an initiative proposal at Ruby Indonesia meetup, initiated by Asep. Now mentored by Abdullah (a Haskell specialist, yes he’s a damn good Haskellian!).

In this meetup I deliver mostly intro to F# but with geeky introduction specific to functional programming: “Putting the functional and fun in F#”.

Here’s the starting presentation:

WP_20140726_003

And some of the audiences:

WP_20140726_006

And as of this meetup, I pledge myself to be active participant of this meetup as well! Smile

Wednesday, November 7, 2012

[INFO] A hot thread about F# and functional programming is available on Kaskus: about Monad and Type Classes

This is a quoted thread content F# and trends in functional programming in a discussion thread model, please follow my thread (my id on Kaskus is “erilive”) on Kaskus! And it’s available in Bahasa Indonesia!

Hi guys,

If you want to know more about F# and trends in functional programming in a discussion thread model, please follow my thread (my id on Kaskus is “erilive”) on Kaskus! And it’s available in Bahasa Indonesia!

There’s a lot of discussion happens there! And I invite you to join, and I will invite you all of those are advanced or just beginner in functional programming and F#. Some topics are very interesting but quite advanced such as type classes, but there is an easy topic about why functional programming matters.

Here’s the address:

http://www.kaskus.co.id/thread/000000000000000016890387/fpara-programmer-f-dan-functional-programming-disini-yah/

And paragraphs below are samples from the thread:


Monad sample

Quote:Original Posted By erilive on http://www.kaskus.co.id/show_post/000000000000000759711105/18/

Monad yang mana gan? Maybe monad, Identity monad, Reader monad, State monad?

Kalau Reader monad di F#, sudah ada yang bahas:
http://codebetter.com/matthewpodwysocki/2010/01/07/much-ado-about-monads-reader-edition/

Dan sepertinya kalau diimplementasikan di bahasa selain F# (kecuali kalau di Haskell) sepertinya akan menjadi kurang elegan.

Monad disini bukan "Monad" di cabang matematika Category Theory.

Kalau memang banyak peminatnya, akan saya jelaskan Monad di thread ini, agan-agan :)

Notes in English: (as translated)

Which Monad? There are Maybe Monad, Identity Monad, state Monad.

Reader Monad sample in F# has been discussed in:

http://codebetter.com/matthewpodwysocki/2010/01/07/much-ado-about-monads-reader-edition/

And if it’s implemented in other language besides F# and Haskell, it’ll be less elegant.

Monad in this context is not direct equals to Monad in Category theory from Math discipline.

Quote:Original Posted By Syinn

silahkan aja kalo mau langsung bahas monad, saya nyimak dulu ...kalo ada yang kurang ngerti , akan saya tanyakan pada anda

ada baiknya bahas yang awal dulu, yakni monad state

Dan sepertinya kalau diimplementasikan di bahasa selain F# (kecuali kalau di Haskell) sepertinya akan menjadi kurang elegan.

btw, saya ingin tahu kenapa om bisa berkesimpulan yang saya bold di atas ? bisa anda jelaskan secara tehnik dan detail?

karena soal keeleganan code pemrograman kan tergantung kreatifitas programmer itu sendiri, apapun bahasa program yang dia gunakan (ini kalo menurut saya lho, om )

Note in English:

He (Syinn) wanted to know about why I told him it’s less elegant.

And here’s my answer:

This is state monad in F# and sample code to use:

module StateMonad

let (>>=) x f =
   (fun s0 ->
      let a,s = x s0
      f a s)      

let returnS a = (fun s -> a, s)

type StateBuilder() =
    member m.Bind(x, f) = x >>= f
    member m.Return a = returnS a 

let state = new StateBuilder()

let getState = (fun s -> s, s)

let setState s = (fun _ -> (),s) 

let Execute m s = m s |> fst

Now, in C#: (taken from http://taumuon-jabuka.blogspot.com/2012/06/state-monad-in-c-and-f.html)

cs_state_monad_657AF37F

The Bind operator is implemented below:

cd_state_monad_bind_24D8A710

As you shall see, it’s less elegant!


Why IO Monad is “EVIL”?

Syinn asked again! But the topic is about why IO Monad is “EVIL”:

why_iomonad_evil_6F5FE4EA

Here’s my answer: (and apologize if it’s quite long)

There is a blog post that describe IO Monad is evil: http://kawagner.blogspot.com/2007/02/why-monads-are-evil.html

And I assume he (Syinn) quoting that blog post although he didn’t mention it.

My definite answer is: IO Monad isn’t evil. According to that post, because value evaluation when designing IO Monad became imperative, and therefore it would break referential integrity. But it’s not! Because the nature of Haskell is non strict (lazy), the timing of the execution is not so important and the reference is not broken.

A simple sample of this is when dealing with iterator of yield in C# (since VS 2005) and in VB.NET (since VB 11 in Visual Studio 2012).

Due to the fact that I/O is side effect, then declaring I/O must be explicit and has to be marked as signature to a part of the “world” that includes garbage collection.

Functional programming, especially the "pure" ones, forces us to be aware and be careful in handling side effect such as I/O, pointer, exceptions.


More on this?

You will have it on my part 4, 5 of functional programming series! Watch for my next blog post entry!