Obviously coming from Java background here, API might even be wrong term. Anyways, what I'm looking for is something exactly like this: https://docs.oracle.com/javase/8/docs/api/overview-summary.html
Meanwhile in Haskell docs page https://www.haskell.org/documentation there is nothing like that. Stackoverflow answers suggest Hoogle, but I'd rather just look at a list of functions thanks.submitted by mergem
[link] [16 comments]
In the ICFP 2015 talk 04 An Optimizing Compiler for a Purely Functional Web Application Language a comment was made at end that one reason Haskell performs poorly in web benchmarks is because of its database binding libraries.
What needs to be done to improve this situation?
The comment occurs approx 15m into the talk:mn-haskell-guy
[link] [15 comments]
I’m an ergoemacs-mode user, a mode that changes most key bindings so that they put less strain on your hands. For example, it uses Alt instead of Ctrl most of the time, which is easier to press: use your curled thumb instead of a karate chop. Also, many commands are activated by first pressing the Menu/Apps key (that key near the Right Ctrl which usually opens the context menu). For example, pressing Menu then T allows you to switch buffers.
However, the keyboard on my new notebook doesn’t have a dedicated Menu key. Instead, one needs to press Fn+Right Ctrl, which is of course extremely painful.
I’ve found a workaround, though. A very hackish workaround.
The ergoemacs-mode FAQ suggests using Caps Lock as a Menu/Apps key for Mac users. Using xmodmap it’s trivial to make Caps Lock a Menu key:$ xmodmap -e "keycode 66 = Menu"
However, using xmodmap properly with Gnome is nigh impossible. It’s recommend to use xkb instead, but xkb doesn’t support mapping Caps Lock to the Menu key out-of-the-box (at least not yet). At this point, having wandered through many documentation pages, I’ve decided to try using some of the xkb options that already exist.
At first I tried setting Caps Lock as the Hyper key. However, by default the Hyper key gets the same modifier code as the Super key (which is usually the key with the Windows logo). There’s a straightforward way of separating them, but I couldn’t find a way to make it play nice with Gnome. And even if I could, it’s not clear to me if I could use the Hyper key as a substitute for the Menu key on emacs.
When ready to admit defeat, I’ve set the Caps Lock behavior to “Caps Lock is disabled” in preparation of trying a hack using xmodmap. Much to my surprise, I accidentally discovered that emacs then began treating the disabled Caps Lock key as <VoidSymbol>! The gears started turning in my head, then I added the following line to my ~/.emacs file:(define-key key-translation-map (kbd "<VoidSymbol>") (kbd "<menu>"))
Surprisingly, it worked! Now pressing Caps Lock then T will switch buffers, for example. As a bonus, pressing Caps Lock accidentally while on another application won’t do anything.
It’s not clear to me how fragile this hack really is. I’ll update this blog post if I ever find some drawback to it. But right now it seems to work quite nicely.
A common use case for Apache + PHP is for simple static sites where you just need to do simple sharing of html fragments (like the header and footer). Is there an example anywhere of using snap and heist to do this? In fact, it could just be wai instead of snap (but I believe that it's more likely that someone has done this with snap). Specifically, what I am looking for is a way to set things up such that I have a directory with heist templates, and routing just takes the path from the URL, and uses it for a template lookup. That's it. No database, no persistence, no auth. I thought that snap init might give me enough to figure this out, but it didn't. I would appreciate any help. Thanks.submitted by andrewthad
[link] [11 comments]
MonadFix is an odd beast; many Haskell programmers will never use it in their careers. It is indeed very rarely that one needs MonadFix; and for that reason, non-contrived cases where MonadFix is needed are quite interesting to consider.
In this article, I’ll introduce MonadFix and show how it can be handy for compiling the Kleene closure (also known as star or repetition) of regular expressions.What is MonadFix?
If you hear about MonadFix for the first time, you might think that it is needed to define recursive monadic actions, just like ordinary fix is used to define recursive functions. That would be a mistake. In fact, fix is just as applicable to monadic actions as it is to functions:guessNumber m = fix $ \repeat -> do putStrLn "Enter a guess" n <- readMaybe <$> getLine if n == Just m then putStrLn "You guessed it!" else do putStrLn "You guessed wrong; try again" repeat
So, what is mfix for? First, recall that in Haskell, one can create recursive definitions not just for functions (which makes sense in other, non-lazy languages) or monadic actions, but for ordinary data structures as well. This is known as cyclic (or circular, or corecursive) definitions; and the technique itself is sometimes referred to as tying the knot.
The classic example of a cyclic definition is the (lazy, infinite) list of Fibonacci numbers:fib = 0 : 1 : zipWith (+) fib (tail fib)
Cyclic definitions are themselves rare in day-to-day Haskell programming; but occasionally, the right hand side will be not a pure value, but a monadic computation that needs to be run in order to obtain the value.
Consider this (contrived) example, where we start the sequence with an arbitrary number entered by the user:fibIO1 = do putStrLn "Enter the start number" start <- read <$> getLine return $ start : 1 : zipWith (+) fibIO1 (tail fibIO1)
This doesn’t typecheck because fibIO is not a list; it’s an IO action that produces a list.
But if we try to run the computation, it doesn’t make much sense either:fibIO2 = do putStrLn "Enter the start number" start <- read <$> getLine fib <- fibIO2 return $ start : 1 : zipWith (+) fib (tail fib)
This version of fibIO will ask you to enter the start number ad infinitum and never get to evaluating anything.
Of course, the simplest thing to do would be to move IO out of the recursive equation; that’s why I said the example was contrived. But MonadFix gives another solution:fibIO3 = mfix $ \fib -> do putStrLn "Enter the start number" start <- read <$> getLine return $ start : 1 : zipWith (+) fib (tail fib)
Or, using the do-rec syntax:fibIO4 = do rec fib <- do putStrLn "Enter the start number" start <- read <$> getLine return $ start : 1 : zipWith (+) fib (tail fib) return fib Compiling regular expressions
As promised, I am going to show you an example usage of MonadFix that solved a problem other than “how could I use MonadFix?”. This came up in my work on regex-applicative.
For a simplified presentation, let’s consider this type of regular expressions:data RE = Sym Char -- symbol | Seq RE RE -- sequence | Alt RE RE -- alternative | Rep RE -- repetition
Our goal is to compile a regular expression into a corresponding NFA. The states will be represented by integer numbers. State 0 corresponds to successful completion; and each Sym inside a regex will have a unique positive state in which we are expecting the corresponding character.type NFAState = Int
The NFA will be represented by a maptype NFA = IntMap (Char, [NFAState])
where each state is mapped to the characters expected at that state and the list of states where we go in case we get the expected character.
To compile a regular expression, we’ll take as an argument the list of states to proceed to when the regular expression as a whole succeeds (otherwise we’d have to compile each subexpression separately and then glue NFAs together). This is essentially the continuation-passing style; only instead of functions, our continuations are NFA states.
During the compilation, we’ll use a stack of two State monads: one to assign sequential state numbers to Syms; the other to keep track of the currently constructred NFA.-- Returns the list of start states and the transition table compile :: RE -> ([NFAState], NFA) compile re = runState (evalStateT (go re ) 0) IntMap.empty -- go accepts exit states, returns entry states go :: RE -> [NFAState] -> StateT NFAState (State NFA) [NFAState] go re exitStates = case re of Sym c -> do !freshState <- gets (+1); put freshState lift $ modify' (IntMap.insert freshState (c, exitStates)) return [freshState] Alt r1 r2 -> (++) <$> go r1 exitStates <*> go r2 exitStates Seq r1 r2 -> go r1 =<< go r2 exitStates
This was easy so far: alternatives share their exit states and their entry states are combined; and consequtive subexpressions are chained. But how do we compile Rep? The exit states of the repeated subexpression should become its own entry states; but we don’t know the entry states until we compile it!
And this is precisely where MonadFix (or recursive do) comes in:Rep r -> do rec let allEntryStates = ownEntryStates ++ exitStates ownEntryStates <- go r allEntryStates return allEntryStates
Why does this circular definition work? If we unwrap the State types, we’ll see that the go function actually computes a triple of three non-strict fields:
- The last used state number
- The list of entry states
- The NFA map
The elements of the triple may depend on each other as long as there are no actual loops during evaluation. One can check that the fields can be indeed evaluated linearly in the order in which they are listed above:
- The used state numbers at each step depend only on the regular expression itself, so it can be computed wihtout knowing the other two fields.
- The list of entry states relies only on the state number information; it doesn’t need to know anything about the NFA transitions.
- The NFA table needs to know the entry and exit states; but that is fine, we can go ahead and compute that information without creating any reverse data dependencies.
An ASM Monad – a similar example from a different domain.
Oliver Charles’s 24 Days of GHC Extensions: Recursive Do.
Levent Erkok’s thesis which contains all you need to know about MonadFix, including several other examples.
Todd Wilson points out that Douglas McIlroy describes a similar regular expression compilation technique in his 2004 JFP Functional Pearl Enumerating the strings of regular languages. Like this article, Douglas’s paper uses a circular definition when compiling the Kleene closure. But the circular definition is not monadic there: instead of using the State monad, Douglas passes the state around by hand.