Hi, first of all let me say that I love the community here on /r/haskell. You all have been super helpful in my journey of learning this great language.
In running a personal file-serving site using Scotty, I ran into the problem of authentication and sessions. Seeing as the package called scotty-sessions was deprecated (and later turned into Spock, a different framework), I decided to roll my own authentication/sessions. I did so, and it seems to work, so I abstracted it into a library. My friend has suggested that I put it on Hackage, but I do not think that it is ready. I would greatly appreciate it if a few of you could take the time to skim my code (~120 lines) and give me some pointers!
Background: * The code generates sessionId's from random Ints, stores them in a cookie called "SessionId" which expires in a number of minutes (currently 10 for testing purposes), and makes an entry into a SQLite database with the sessionId and expiration. * I used persistent as a database backend, but I am unsure of the best way to give configuration options to the user (e.g. database location/name). Wrap everything IO in a ReaderT? Configuration file? * The solution I came up with for removing expired sessions from the database feels inadequate/improper (uses forkIO and threadDelay).
Any and all suggestions regarding style or functionality would be greatly appreciated!submitted by shishkabeb
[link] [16 comments]
After discovering the absolute magic that is the State monad, every single program I've made since has used it almost exclusively, in some cases even using State a (). In what situation does it work better or is it cleaner to NOT use the State monad?submitted by part-ick-you-are-lee
[link] [69 comments]
In the spirit of Brent's post, I figure I'll make a public announcement that I'm in Vancouver all week attending HOPE, ICFP, and the Haskell Symposium. I love reconnecting with old friends, as well as meeting new folks. Even if I've met you at past ICFPs, feel free to re-introduce yourself as ...things have changed over the past few years. I know I've been pretty quiet of late on Haskell cafe as well as here, but word on the street is folks still recognize my name around places. So if you want to meet up, leave a comment with when/where to find you, or just look for the tall gal with the blue streak in her hair.
Unlike Brent, and unlike in years past, I might should note that I am looking to "advance my career". As of this fall, I am officially on the market for research and professorship positions. So if you're interested in having a linguistically-minded constructive mathematician help work on your problems, come say hi. For those not attending ICFP, you can check out my professional site; I need to fix my script for generating the publications page, but you can find a brief background/research statement there along with the other usuals like CV, recent projects, and classes taught.
I've tried 'time' but the interface confuse me. Is it just me or you guys have the same experience?submitted by eckyputr
[link] [39 comments]
After having released version 0.9 of my reactive-banana library, I now want to discuss the significant API changes that I have planned for the next release of the library, version number 1.0. These changes will not be backward compatible.
Since its early iterations (version 0.2), the goal of reactive-banana has been to provide an efficient push-based implementation of functional reactive programming (FRP) that uses (a variation of) the continuous-time semantics as pioneered by Conal Elliott and Paul Hudak. Don’t worry, this will stay that way. The planned API changes may be radical, but they are not meant to change the direction of the library.
I intend to make two major changes:
The API for dynamic event switching will be changed to use a monadic approach, and will become more similar to that of the sodium FRP library. Feedback that I have received indicates that the current approach using phantom types is just too unwieldy.
The type Event a will be changed to only allow a single event occurrence per moment, rather than multiple simultaneous occurrences. In other words, the types in the module Reactive.Banana.Experimental.Calm will become the new default.
These changes are not entirely cast in stone yet, they are still open for discussion. If you have an opinion on these matters, please do not hesitate to write a comment here, send me an email or to join the discussion on github on the monadic API!
The new API is not without precedent: I have already implemented a similar design in my threepenny-gui library. It works pretty well there and nobody complained, so I have good reason to believe that everything will be fine.
Still, for completeness, I want to summarize the rationale for these changes in the following sections.Dynamic Event Switching
One major impediment for early implementations of FRP was the problem of so-called time leaks. The key insight to solving this problem was to realize that the problem was inherent to the FRP API itself and can only be solved by restricting certain types. The first solution with first-class events (i.e. not arrowized FRP) that I know is from an article by Gergeley Patai [pdf].
In particular, the essential insight is that any FRP API which includes the functionsaccumB :: a -> Event (a -> a) -> Behavior a switchB :: Behavior a -> Event (Behavior a) -> Behavior a
with exactly these types is always leaky. The first combinator accumulates a value similar to scanl, whereas the second combinator switches between different behaviors – that’s why it’s called “dynamic event switching”. A more detailed explanation of the switchB combinator can be found in a previous blog post.
One solution the problem is to put the result of accumB into a monad which indicates that the result of the accumulation depends on the “starting time” of the event. The combinators now have the typesaccumB :: a -> Event (a -> a) -> Moment (Behavior a) switchB :: Behavior a -> Event (Behavior a) -> Behavior a
This was the aforementioned proposal by Gergely and has been implemented for some time in the sodium FRP library.
A second solution, which was inspired by an article by Wolfgang Jeltsch [pdf], is to introduce a phantom type to keep track of the starting time. This idea can be expanded to be equally expressive as the monadic approach. The combinators becomeaccumB :: a -> Event t (a -> a) -> Behavior t a switchB :: Behavior t a -> Event t (forall s. Moment s (Behavior s a) -> Behavior t a
Note that the accumB combinator keeps its simple, non-monadic form, but the type of switchB now uses an impredicative type. Moreover, there is a new type Moment t a, which tags a value of type a with a time t. This is the approach that I had chosen to implement in reactive-banana.
There is also a more recent proposal by Atze van der Ploeg and Koen Claessen [pdf], which dissects the accumB function into other, more primitive combinators and attributes the time leak to one of the parts. But it essentially ends up with a monadic API as well, i.e. the first of the two mentioned alternatives for restricting the API.
When implementing reactive-banana, I intentionally decided to try out the second alternative, simply in order to explore a region of the design space that sodium did not. With the feedback that people have sent me over the years, I feel that now is a good time to assess whether this region is worth staying in or whether it’s better to leave.
The main disadvantage of the phantom type approach is that it relies not just on rank-n types, but also on impredicative polymorphism, for which GHC has only poor support. To make it work, we need to wrap the quantified type in a new data type, like thisnewtype AnyMoment f a = AnyMoment (forall t. Moment t (f t a))
Note that we also have to parametrize over a type constructor f, so that we are able to write the type of switchB asswitchB :: forall t a. Behavior t a -> Event t (AnyMoment Behavior a) -> Behavior t a
Unfortunately, wrapping and unwrapping the AnyMoment constructor and getting the “forall”s right can be fairly tricky, rather tedious, outright confusing, or all three of it. As Oliver Charles puts it in an email to me:
Right now you’re required to provide an AnyMoment, which in turn means you have to trim, and then you need a FrameworksMoment, and then an execute, and then you’ve forgotten what you were donig! :-)
Another disadvantage is that the phantom type t “taints” every abstraction that a library user may want to build on top of Event and Behavior. For instance, image a GUI widget were some aspects are modeled by a Behavior. Then, the type of the widget will have to include a phantom parameter t that indicates the time at which the widget was created. Ugh.
On the other hand, the main advantage of the phantom type approach is that the accumB combinator can keep its simple non-monadic type. Library users who don’t care much about higher-order combinators like switchB are not required to learn about the Moment monad. This may be especially useful for beginners.
However, in my experience, when using FRP, even though the first-order API can carry you quite far, at some point you will invariably end up in a situation where the expressivitiy of dynamic event switching is absolutely necessary. For instance, this happens when you want to manage a dynamic collection of widgets, as demonstrated by the BarTab.hs example for the reactive-banana-wx library. The initial advantage for beginners evaporates quickly when faced with managing impredicative polymorphism.
In the end, to fully explore the potential of FRP, I think it is important to make dynamic event switching as painless as possible. That’s why I think that switching to the monadic approach is a good idea.Simultaneous event occurences
The second charge is probably less controversial, but also breaks backward compatibility.
The API includes a combinator for merging two event streams,union :: Event a -> Event a -> Event a
If we think of Event as a list of values with timestamps, Event a = [(Time,a)], this combinator works like this:union ((timex,x):xs) ((timey,y):ys) | timex < timey = (timex,x) : union xs ((timey,y):ys) | timex > timey = (timey,y) : union ((timex,x):xs) yss | timex == timey = ??
But what happens if the two streams have event occurrences that happen at the same time?
Before answering this question, one might try to argue that simultaneous event occurrences are very unlikely. This is true for external events like mouse movement or key presses, but not true at all for “internal” events, i.e. events derived from other events. For instance, the event e and the event fmap (+1) e certainly have simultaneous occurrences.
In fact, reasoning about the order in which simultaneous occurrences of “internal” events should be processed is one of the key difficulties of programming graphical user interfaces. In response to a timer event, should one first draw the interface and then update the internal state, or should one do it the other way round? The order in which state is updated can be very important, and the goal of FRP should be to highlight this difficulty whenever necessary.
In the old semantics (reactive-banana versions 0.2 to 0.9), using union to merge two event streams with simultaneous occurrences would result in an event stream where some occurrences may happen at the same time. They are still ordered, but carry the same timestamp. In other words, for a stream of eventse :: Event a e = [(t1,a1), (t2,a2), …]
it was possible that some timestamps coincide, for example t1 == t2. The occurrences are still ordered from left to right, though.
In the new semantics, all event occurrences are required to have different timestamps. In other to ensure this, the union combinator will be removed entirely and substituted by a combinatorunionWith f :: (a -> a -> a) -> Event a -> Event a -> Event a unionWith f ((timex,x):xs) ((timey,y):ys) | timex < timey = (timex,x) : union xs ((timey,y):ys) | timex > timey = (timey,y) : union ((timex,x):xs) yss | timex == timey = (timex,f x y) : union xs ys
where the first argument gives an explicit prescription for how simultaneous events are to be merged.
The main advantage of the new semantics is that it simplies the API. For instance, with the old semantics, we also needed two combinatorscollect :: Event a -> Event [a] spill :: Event [a] -> Event a
to collect simultaneous occurrences within an event stream. This is no longer necessary with the new semantics.
Another example is the following: Imagine that we have an input event e :: Event Int whose values are numbers, and we want to create an event that sums all the numbers. In the old semantics with multiple simultaneous events, the event and behavior defined asbsum :: Behavior Int esum :: Event Int esum = accumE 0 ((+) <@> e) bsum = stepper 0 esum
are different from those defined bybsum = accumB 0 ((+) <@> e) esum = (+) <$> bsum <@ e
The reason is that accumE will take into account simultaneous occurrences, but the behavior bsum will not change until after the current moment in time. With the new semantics, both snippets are equal, and accumE can be expressed in terms of accumB.
The main disadvantage of the new semantics is that the programmer has to think more explicitly about the issue of simultaneity when merging event streams. But I have argued above that this is actually a good thing.
In the end, I think that removing simultaneous occurrences in a single event stream and emphasizing the unionWith combinator is a good idea. If required, s/he can always use an explicit list type Event [a] to handle these situations.
(It just occurred to me that maybe a type class instanceinstance Monoid a => Monoid (Event a)
could give us the best of both worlds.)
This summarizes my rationale for these major and backward incompatible API changes. As always, I appreciate your comments!
With the no-reinstall cabal project coming soon, it seems that cabal is back on track to face the stack attack.
Which one do use, why ?submitted by maxigit
[link] [76 comments]
This afternoon I’ll be getting on a plane to Vancouver for ICFP. I’m looking forward to seeing many friends, of course, but I also enjoy meeting new people—whether or not they are “famous”, whether or not I think they can “advance my career”. So I’ll just throw this out there: if you will be in Vancouver this week and would like to meet me, just leave a comment and I will make a point of trying to find you to chat! I’ll be attending the Haskell Implementor’s Workshop, the Ally Skills Tutorial, ICFP itself, the Haskell Symposium, and FARM, but there’s also plenty of time to chat in the hallway or over a meal.
Over this summer, Vishal Agrawal has been working on a GSoC project to move Cabal to more Nix-like package management system. More simply, he is working to make it so that you'll never get one of these errors from cabal-install again:Resolving dependencies... In order, the following would be installed: directory-188.8.131.52 (reinstall) changes: time-1.4.2 -> 1.5 process-184.108.40.206 (reinstall) extra-1.0 (new package) cabal: The following packages are likely to be broken by the reinstalls: process-220.127.116.11 hoogle-4.2.35 haskell98-18.104.22.168 ghc-7.8.3 Cabal-22.214.171.124 ...
However, these patches change a nontrivial number of moving parts in Cabal and cabal-install, so it would be very helpful to have willing guinea pigs to help us iron out some bugs before we merge it into Cabal HEAD. As your prize, you'll get to run "no-reinstall Cabal": Cabal should never tell you it can't install a package because some reinstalls would be necessary.
Here's how you can help:
- Make sure you're running GHC 7.10. Earlier versions of GHC have a hard limitation that doesn't allow you to reinstall a package multiple times against different dependencies. (Actually, it would be useful if you test with older versions of GHC 7.8, but only mostly to make sure we haven't introduced any regressions here.)
- git clone https://github.com/ezyang/cabal.git (I've added some extra corrective patches on top of Vishal's version in the course of my testing) and git checkout cabal-no-pks.
- In the Cabal and cabal-install directories, run cabal install.
- Try building things without a sandbox and see what happens! (When I test, I've tried installing multiple version of Yesod at the same time.)
It is NOT necessary to clear your package database before testing. If you completely break your Haskell installation (unlikely, but could happen), you can do the old trick of clearing out your .ghc and .cabal directories (don't forget to save your .cabal/config file) and rebootstrapping with an old cabal-install.
Please report problems here, or to this PR in the Cabal tracker. Or chat with me in person next week at ICFP. :)
Anyone have thoughts on this talk by C++/D guru Andrei Alexandrescu? Talk is focused on pretty low level C++ memory allocator use case, but since generic programming is an important paradigm, I'm curious what people here think.
EDIT: link https://www.youtube.com/watch?v=mCrVYYlFTrAsubmitted by klaxion
[link] [6 comments]