Wednesday, April 9, 2014

Introduction to FRP - Why Applicative Functors?

Introduction to FRP - Why Applicative Functors?

Many if not all FRP (Functional Reactive Programming) libraries, reactive-banana included, export an API that is based on functorsapplicative functors or other higher-kinded types like arrows. This section explains why that is and what it means for the novice Haskell programmer.

The idea of time-varying values

The two main data types of the reactive-banana library are behaviors and events. The type constructor Behavior represents a time-varying value, i.e. you can think of it as a function
type Behavior a = Time -> a
On the other hand, the type constructor Event represents a sequence of event occurrences. Think of it as a list
type Event a = [(Time,a)]
Pictorially, you could represent them like this: a behavior traces the evolution of a value over time
while an event consists of several, possibly simultaneous event occurrences.
The promise of functional reactive programming is that these two notions allow you to code interactive programs like animations, graphical user interfaces (GUI) and so on much more easily and declaratively than previously possible. Indeed, representing, say, the position of a player character on the screen as a function of time is a remarkable and powerful idea. Of course, you can only fathom this idea if you are used to programming languages with first-class functions.
Now, you could just take these two type definitions above and use them to code animations to your heart’s content; after all, they are legal Haskell code. This is pretty much what Conal Elliott and Paul Hudak did in their seminal paper “Functional Reactive Animation”. Unfortunately, the computational costs of this approach are prohibitive. While trading clock cycles for expressivity is often worth it, the costs here are much more than a constant factor. A direct implementation is simply not practical.

Abstraction and combinators

The solution to the problem above is, of course, abstraction. The FRP library gives us two data types Behavior and Event which behave very much like the definitions above. Internally, however, the library represents them very differently. But as long as these types behave as we desire, the difference is of no concern to us.
More precisely, the library not only gives us two types constructors Behavior and Event, but also a carefully chosen set of combinators (functions) for them. In reactive-banana version 0.2, the combinators are these:
filter  :: (a -> Bool) -> Event a -> Event a
accumE  :: a -> Event (a -> a) -> Event a
stepper :: a -> Event a -> Behavior a
apply   :: Behavior (a -> b) -> Event a -> Event b

instance Functor Event
instance Functor Behavior
instance Applicative Behavior
instance Monoid (Event a)
As you can see, some of the combinators are actually given by type class instances.
The deal with combinators is this: you may still think of Behavior as a function Time -> a, but you may no longer define a behavior directly, like b = \t -> 2*t. Instead, you may only use the combinators above to define new behaviors. This restriction to a certain set of functions is what guarantees an efficient implementation.
Some of these combinators, namely the type classes instances, belong to very general abstractions, like functorapplicative functor and monoid. They are applicable (pun intended) far beyond FRP, so understanding will pay off for other things as well. This is no surprise: every higher-kinded type, i.e. a type with a parameter, is simply very likely to belong to some of the abstractions here. Monads would be another common example, but it just so happens that they don’t show up in reactive-banana.
To learn more about applicative functors and the other type classes, have a look at Brent Yorgey’s Typeclassopedia. Understanding applicative functors is also very useful for understanding the apply combinator.
So much for the general philosophy behind every FRP library. The specific meaning and utility of the combinators in the reactive-banana library will be explained subsequently.
Heinrich Apfelmus
Fri, 06 May 2011 19:34:31 +0200

Thursday, February 13, 2014

Active Functor Framework -

Questions
How system store all events (data input) ?
Sensors (data publishers / data collectors / data crawlers): Netty, PhantomJS, Chrome Ext
Message (Persistent Queue) : Kafka
Decode message into stimulus (aka event), fast persistent to Redis http://redis.io/:
Store stimulus (aka event) in Hadoop in date format yyyy/mm/dd/hh
Indexing with Elasticsearch (all fields in an event)

How system react to data ?
Reactive processing to every stimulus (stimuli as plural)
every event must belongs to category



How system query data ?
FQL (functor query language)


English to FQL converter



Friday, September 27, 2013

Functors, Monads, Applicatives – can be so simple

When I started trying to get into these weird topics named “functors” and “monads” and so on, which came up in the Scala mailing list, I was more than slightly confused [1]. I read some articles (in the beginning not many existed), listened to mails, consulted book chapters, and even started learning Haskell to consult the very fine “Learning a Haskell for great good” chapters on that matters. Different authors use different ways to explain -and implement!- these concepts, what in the end is helpful. You can read an article, think, read some other perspective, come back to the first article again, a.s.o.
But it took some time to realise how darn simple it all can be in principle (and took more time to finally write about it). I worked with and restructured some examples until the simplicity of it all became obvious. So let me now present you my perspective on it, and perhaps it will help you to get into these seemingly strange things:
Given some type constructor C[_] and two types A and B, we want to apply functions of type C[A]=>C[B] .
a) ( A=>B )      => ( C[A]=>C[B] )   | Functor
b) ( A=>C[B] ) => ( C[A]=>C[B] )   | Monad
c) ( C[A=>B] ) => ( C[A]=>C[B] )   | Applicative

Wednesday, August 29, 2012

Wednesday, February 8, 2012

Active Functors + Social Network = ?



bây giờ là khoảng 23:35, mình suy nghĩ về 1 mô hình tính toán mới dựa trên các khái niệm về functional programming, category theory, active agent và thứ đang là "hot topic" hiện nay, Social Network.
Các mô hình hình tính toán cũ, bị giới hạn trong khả năng của nó, "data , dữ liệu luôn thể bị động, và cần sự can thiệp của 1 thuật toán "cực kỳ phức tạp" để tìm hiểu ý nghĩa của dữ liệu.

Học về IT hay Computer Science, mọi người đều biết đến môn database. Nói ngắn gọn, người ta thường chỉ lưu thông tin và quên rằng các mối quan hệ giữa các thông tin mới là quan trọng. Nếu không có các relationship và các fuction tạo ra data thì nó rất vô nghĩa. Active functor nó về các cơ chế lưu trữ các cách thức tạo nên data, VD: database của shopping cart chỉ lưu thông tin về user và item họ mua, còn quy trình vì sao họ chọn item đó, và sử dụng nó như thế nào, .... thì hiếm khi lưu lại. Sự thiếu hụt này dẫn đến khả năng hiểu về cơ chế chọn lựa mua hàng (hành vi) của user, vốn khá quan trọng trong việc marketing.

Người ta gọi đó là "Data Mining", một cách mà các công ty to như "Facebook, Google, Amazon, Apple và cả Microsoft" đang làm. Họ làm mọi thứ để hiểu hơn về "chúng ta" , những người sử dụng dịch vụ của họ.

Ở đây không bàn về sự phức tạp về mặt kỹ thuật để làm 1 việc "A" việc B, việc C, ....ở đây mình muốn nói cách các researchers đang làm, mô hình hóa dữ liệu về con người như những cuốn sách bán trên Amazon là 1 sai lầm, lưu trữ về cấu trúc các thông tin như thế chỉ hiểu con người ở 1 thế giới của máy tính, làm cho nó trở thành những "bit" thông tin 0 1 vô tri vô giác.
Thực tế cho thấy , các kết quả cũng không quá tệ xét về mặt logic, nhưng nó sẽ bị chặn bởi những giới hạn, về mặt lý thuyết , đó là sự tiến hóa của thông tin, ý thức của thông tin theo thời gian.

Cụ thế nó ngốc như 1 cái máy tính, chỉ biết nhận lệnh, và không bao giớ biết đặt câu hỏi, các KQ data mining có thể bị sai lệch, và người dùng đôi khi nhận duoc cái quảng cáo rất bực mình,

Active Functors là 1 dạng mô hình tính toán, xem các thông tin về user như những thực thể tồn tại, có thể độc lập phát triển các môi liên hệ thông tin khác theo 1 mô hính có tính "tiến hóa". Nói dễ hiểu, nó như 1 bản sao số "bộ não" của users, thông tin trở nên có "ý thức" (sense) hơn về nó. các Links, và semantic,( ngữ nghĩa) của nó , ở dạng tiếng Anh, Pháp hay Vietnam đề chung 1 thể , gọi đó ontology.

ý tưởng, mô hình và cả tính hiện thực, khả thí vẫn đang trong giai đoạn nghiên cứu,

hi vọng nó thành công, hay ít ra nó cũng giúp mình quản lý thông tin cá nhân tốt hơn, xả hội tri thức mà, chúng ta luôn mong ước về điều đó,
let's make it happen !

Monday, December 19, 2011

What wrong with data today ?

In modern computing, after the Web 2.0 era, all data in the Internet is growing beyond the limit of imagination's some computer scientist in 60 years ago.
Some limit of  von Neumann's model, the classical database, or even Turing theory, that make our time, the computing can not go further the current mind.
I still thinking what is the better model of computation, how we can store the semantics of data, how we think and treat the data (just bit 1 or 0) as the soul inside. 
Like us, the human being, can have both  sense, soul and mind with matter, body.
I think we think the data can have a sense , a context (time, place, and some attributes) that make our data become a true entity, that exists in the World.


Functor, a simple function object that have both its data and the computation, make our computing more abstract way.

Saturday, December 17, 2011

Functor in Practice


The graph of functors in categories {money, knowledge,happiness}
Imagine when all these things going to computing,