1. 2

    I would love to see research on using an AFL-style genetic algorithm based on (branch) coverage feedback for generating test cases in a QuickCheck-style property testing framework. You could do that with clang’s -fsanitize-coverage options, similar to what libFuzzer does but with type-aware input generation, shrinking, etc.

    1. 1

      This is something I’ve been wanting to do as a side-project for very long time. Instrument the output of Elm compiler with code coverage stats in the runtime and use these stats from within the test runner for some kind of coverage-maximizing AFL-style fuzzers.

    1. 4

      Yes, generative/property/fuzz tests are awesome. I use them in Elm whenever I can. I’ve also written a library that uses them for the “Msg / Model / update` triplets of The Elm Architecture: http://package.elm-lang.org/packages/Janiczek/elm-architecture-test/latest

      Used them eg. on my implementation of a text editor, and they found so many edge cases, you wouldn’t believe. https://github.com/Janiczek/elm-editor/blob/master/tests/CodeEditor.elm

      They effectively allow you to pin down the behaviour of a function to a certain degree; giving you counter-examples like “You said the cursor wouldn’t go to a negative column, but if you do [Insert ‘a’, Left, Backspace], the cursor is -1!”, with the Msg list minimized and all that (it probably found the bug with ~100 Msgs in the list, and threw out those that don’t matter)

      EDIT: What would I love in generative testing would be some scheme that allows for it to be a monad (to have a andThen : (a -> Fuzzer b) -> Fuzzer a -> Fuzzer b function) and still be able to shrink well.

      1. 3

        EDIT: What would I love in generative testing would be some scheme that allows for it to be a monad (to have a andThen : (a -> Fuzzer b) -> Fuzzer a -> Fuzzer b function) and still be able to shrink well.

        FWIW this is literally the main innovation in Hypothesis. :-)

      1. 6

        Yep, this is how I figured out monads too, but when using Rust! There is more to them though - the laws are important, but it’s sometimes easier to learn them by examples first!

        1. 3

          Can you show an example where a monad is useful in a Rust program?

          (I’m not a functional programmer, and have never knowingly used a monad)

          1. 10

            I learned about monads via Maybe in Haskell; the equivalent in Rust is called Option.

            Option<T> is a type that can hold something or nothing:

            enum Option<T> {

            Rust doesn’t have null; you use option instead.

            Options are a particular instance of the more general Monad concept. Monads have two important operations; Haskell calls them “return” and “bind”. Rust isn’t able to express Monads as a general abstraction, and so doesn’t have a particular name. For Option<T>, return is the Some constructor, that is,

            let x = Option::Some("hello");

            return takes some type T, in this case, a string slice, and creates an Option<T>. So here, x has the type Option<&str>.

            bind takes two arguments: something of the monad type, and a function. This function takes something of a type, and returns an instance of the monad type. That’s… not well worded. Let’s look at the code. For Option<T>, bind is called and_then. Here’s how you use it:

            let x = Option::Some("Hello");
            let y = x.and_then(|arg| Some(format!("{}!!!", arg)));
            println!("{:?}", y);

            this will print Some("Hello!!!"). The trick is this: the function it takes as an argument only gets called if the Option is Some; if it’s None, nothing happens. This lets you compose things together, and reduces boilerplate when doing so. Let’s look at how and_then is defined:

            fn and_then<U, F>(self, f: F) -> Option<U> 
            where F: FnOnce(T) -> Option<U>
                match self {
                    Some(x) => f(x),
                    None => None,

            So, and_then takes an instance of Option and a function, f. It then matches on the instance, and if it’s Some, calls f passing in the information inside the option. If it’s None, then it’s just propagated.

            How is this actually useful? Well, these little patterns form building blocks you can use to easily compose code. With just one and_then call, it’s not that much shorter than the match, but with multiple, it’s much more clear what’s going on. But beyond that, other types are also monads, and therefore have bind and return! Rust’s Result<T, E> type, similar to Haskell’s Either, also has and_then and Ok. So once you learn the and_then pattern, you can apply it across a wide array of types.

            Make sense?

            1. 3

              Make sense?

              It absolutely does! I’ve used and_then extensively in my own Rust code, but never known that I was using a monad. Thanks for the explanation Steve.

              But there’s one gap in my understanding now. Languages like Haskell need monads to express things with side-effects like IO (right?). What’s unique about a monad that allows the expression of side effects in these languages?

              1. 7

                No problem!

                This is also why Rust “can’t express monads”, we can have instances of individual monads, but can’t express the higher concept of monads themselves. For that, we’d need a way to talk about “the type of a type”, which is another phrasing for “higher minded types”.

                So, originally, Haskell didn’t have monads, and IO was done another way. So it’s not required. But, I am about to board a flight, so my answer will have to wait a bit. Maybe someone else will chime in too.

                1. 2

                  higher minded types

                  (Just so others don’t get confused, I think you meant “kinded” here, right?)

                  1. 1

                    Heh, yes. Thanks.

                2. 3

                  A monad has the ability to express sequence, which is useful for imperative programming. It’s not unique, e.g. you can write many imperative programs using just monoid, functor, applicative or many other tools.

                  The useful function you get out of realising that IO forms a Monad is:

                  (>>=) :: IO a -> (a -> IO b) -> IO b

                  An example of using this function:

                  getLine >>= putStrLn
                  1. 4

                    I should say Monad is unique in being able to express that line of code, but there’s many imperative programs which don’t need Monad. For example, just Semigroup can be used for things like this:

                    putStrLn "Hello" <> putStrLn "World"

                    Or we could read some stuff in with Applicative:

                    data Person = Person { firstName :: String, lastName :: String }
                    liftA2 Person getLine getLine

                    So Monad isn’t about side-effects or imperative programming, it’s just that imperative programming has a useful Monad, among other things.

                    1. 2

                      You are way ahead of me here and I’m probably starting to look silly, but isn’t expressing sequence in imperative languages trivial?

                      For example (Python):

                      x = f.readline()

                      x must be evaluated first because it is an argument of the second line. So sequence falls out of the hat.

                      Perhaps in a language like Haskell where you have laziness, you can never be sure if you have guarantees of sequence, and that’s why a monad is more useful in that context? Even then, surely data dependencies somewhat impose an ordering to evaluation?

                      For me, the utility of Steve’s and_then example wasn’t only about sequence, it was also about being able to (concisely) stop early if a None arose in the chain. That’s certainly useful.

                      1. 2

                        but isn’t expressing sequence in imperative languages trivial?


                        In Haskell it is too:

                        (>>=) :: IO a -> (a -> IO b) -> IO b

                        But we generalise that function signature to Monad:

                        (>>=) :: Monad m => m a -> (a -> m b) -> m b

                        We don’t have a built in idea of sequence. We just have functions like these. A generalisation which comes out is Monad. It just gives code reuse.

                        1. 1

                          Maybe is an instance of a monad, and there are many different kinds of monads. If you think of Maybe as “a monad that uses and_then for sequencing”, then “vanilla” sequencing can be seen as “a monad that uses id for sequencing” (and Promises in JavaScript can be seen as “a monad that uses Promise#flatMap for sequencing”).

                          Yes, expressing sequence in eager imperative languages is trivial because you can write statements one after the other. Now imagine a language where you have no statements, and instead everything is expressions. In this expression-only language, you can still express sequence by using data dependencies (you hit this nail right on the head). What would that look like? Probably something like this (in pseudo-JavaScript):

                          function (next2) {
                            (function (next) {
                            })(function (readline_result) {

                          with additional plumbing so that each following step has access to the variables bound in all steps before it (e.g. by passing a dictionary of in-scope variables). A monad captures the spirit of this, so instead of doing all the plumbing yourself, you choose a specific implementation of >>= that does your plumbing for you. The “vanilla” monad’s (this is not a real thing, I’m just making up this name to mean “plain old imperative sequences”) implementation of >>= just does argument plumbing for you, whereas the Maybe monad’s implementation of >>= also checks whether things are None, and the Promise monad’s implementation of >>= also calls Promise#then and flattens any nested promises for you.

                          What’s useful here is the idea that there is this set of data structures (i.e. monads) that capture different meanings of “sequencing”, and that they all have a similar interface (e.g. they have all an implementation of >>= and return with the same signature) so you can write functions that are generic over all of them.

                          Does that make sense?

                      2. 2

                        There is a comment below saying it pretty succintly:

                        A monad is basically defined around the idea that we can’t always undo whatever we just did (…)

                        To make that concrete, readStuffFromDisk |> IO.andThen (\stuff -> printStuff stuff) - in the function after andThen, the “stuff” is made available to you - the function runs after the side effect happened. You can say it needed specific API and the concept of monads satisfies that API.

                        Modelling IO with monads allows you to run functions a -> IO b (take a pure value and do an effectful function on it). Compare that to functions like a -> b (Functor). These wouldn’t cut it - let’s say you’d read a String from the disk - you could then only convert it to another string but not do an additional effect.

                        EDIT: I might not have got the wording entirely right. I ommited a part of the type annotation that says the a comes an effect already. With Functor you could not reuse values that came from an effect; with Monad you can.

                1. 9

                  I know this is not the point of your article, but did you ever find the Maybe monad instance that is in Elm core library? ie. Maybe.andThen?

                  With it the motivating example looks like:

                      |> Maybe.andThen List.first
                      |> Maybe.map (\c -> div [ id "first-comment" ] [ text c ])
                      |> Maybe.withDefault (div [] [ text "No comments" ])
                  1. 5

                    I did not, but I figured there would be something like this somewhere in the Elm ecosystem. I wrote this a while ago and I haven’t used Elm in at least 8 months so excuse my ignorance, and thank you for pointing this out.

                  1. 3

                    The author claims they’re a programmer, but they still clicked 338 checkboxes manually? Sounds fishy :)

                    Here’s what I’ve done on Tumblr, which also has something similar.

                    for (var x of jQuery("input[checked]")) {jQuery(x).removeAttr("checked");}

                    1. 11

                      The author is a programmer, a software architect, an hacker, and a curious person in general.

                      I can conceive several ways to fool your smart jquery script. If you cannot think of them yourself, you shouldn’t code in Javascript, actually.

                      But also I’m a UI and UX designer, at work.

                      I was surprised to see a nice UI with such a stupid mistake.

                      I hoped the developer on the other end was cool enough to surprise me.

                      After the first ten clicks I realized she was not that smart.

                      I hit F12. But then I thought “my users cannot hit F12: lets walk their path and see how I feel”.

                      I’m not stupid. I simply care.

                      1. 2

                        I can conceive several ways to fool your smart jquery script. If you cannot think of them yourself, you shouldn’t code in Javascript, actually.

                        • I don’t think he was claiming his solution was a fit for all
                        • So by your logic only people who know DOM JS should code in JS? ;)

                        I know this was a reply to a slightly provocative comment in defense of the author, but this in particular seems a little silly

                        1. 5

                          I’m the author. And actually I’m sorry for the tone of the reply: I’m tired, and I didn’t intended the @janiczek’s post as a joke for me, but as an attempt to justify InfoWorld by calling me fishy.

                          I’m fishy, definitely! :-)

                          But I also care about users. And I’m an European fish…

                          So by your logic only people who know DOM JS should code in JS? ;)

                          Nobody should code in JS. Really. Nobody should.

                          But yes, if you don’t know how DOM JS has been interpreted in the last 10 years, I think you shouldn’t code in JavaScript professionally. You might think I’m exaggerating to get a point, but trust me: everything is still there, under the hood. Ready to break.

                          1. 2

                            Thanks for the kind reply. I wasn’t trying to provoke myself, just point out something that seemed a bit off :) Professionally? Perhaps your right in a perfect world, but the fact remains there will always be code monkeys that build or maintain simple systems for a customer base that can’t pay for a seasoned developer. Regardless, I agree with the pain point of your article :)

                            1. 3

                              Mm, I kind of feel like as a profession we should try to have more respect for our own work. Software can cause significant harm, and we’ve all just collectively agreed that it’s okay to prop up companies that want to build broken things and not properly maintain them. Maybe companies that aren’t willing to spend the money to build safe software shouldn’t have the privilege of getting engineers to work for them.

                              I know that’s a tangent and not really what you were trying to address.

                              1. 3

                                I completely agree with your first statement, having respect for your own work is a great virtue.

                                The devil is in the details in regards to companies/individuals who provide shoddy services. Outside passionate and informed social circles, it’s customers vote with their pockets (counting data as a form of currency here), whether that be for trading for convenience or just a result of plain ignorance.

                                Unfortunately there aren’t any easy remedies to this problem. Shoddy companies/individuals will find ways to work their way around regulations, and customers will quite happily dig themselves into holes in pursuit of the cheapest or quickest solution. That doesn’t mean you don’t try, in fact I personally think one of the best tactics we can use for problems such as these, is informing the general public of the consequences (though that’s another problem in itself).

                                1. 2

                                  Yes, I agree with all of that, and thank you for it.

                                2. 2

                                  Maybe companies that aren’t willing to spend the money to build safe software shouldn’t have the privilege of getting engineers to work for them.

                                  I see your point, but to me it’s like saying that companies that aren’t willing to spend the money to write proper English shouldn’t have the privilege of getting writers to work for them.

                                  They can learn how to write by themselves.

                                  I prefer a different approach: turn all people into hackers.

                                  1. 1

                                    Yeah, I see that point also. But, I mean, writers have historically been more willing to stand up to exploitative labor practices than hackers have… I think there’s a balance to be found, and getting to the right balance requires rethinking some things.

                                    1. 3

                                      We are just like scribes from Ancient Egypt.

                                      Despite the groupthink, we are still at a very early stage of information technology.

                                      Just like being a scribe back then, being hackers today does not mean understanding the power and responsiblity we have. But it’s just a matter of time.

                                      We will forge the future.

                              2. 1

                                I’m sorry if my post came as provocative! (Maybe my definition of “fishy” – as English is not my native language – is slightly off compared to your definition)

                                Yeah, “I know I could do X instead of clicking, but common user can’t, so let’s walk in their shoes” is a fair motivation. Maybe I just expected the thought to be expressed in the post, given you’ve expressed you’re a programmer. But maybe that’s a silly expectation ¯_(ツ)_/¯ Thanks for the clarifications in the comments here.

                        1. 8

                          One of my favourite things about openbsd is the controlled deprecation and removal of accumulated junk each release.

                          1. 2

                            That’s what I like about Elm language - removing syntax, simplifying concepts, … Refreshing approach to development of anything :)

                          1. 3

                            Great to see other languages steal from Erlang as well :D

                            Here’s my library doing this in Elm: https://github.com/Janiczek/elm-architecture-test/blob/master/README.md

                            And here is a talk on the topic from the Elm Europe conference: https://youtube.com/watch?v=baRcusTHc8E

                            1. 6

                              My favorite approach to this problem is in TLA+, and it always finds the solution.

                              1. 4

                                And here’s the Alloy way of doing it!

                                1. 2

                                  Both of these are awesome! :) Thanks for the links!

                              1. 12

                                Any interest in a crustacean private leaderboard?

                                1. 9

                                  I guess competition is healthy, but I have an additional suggestion: crustacean private help board: I don’t know how we’d do it, one big “ask” story/thread would be difficult, we could set up a github project for it and have questions as “issues” perhaps? We could have a wiki on it too.


                                  Also, the idea of a lobste.rs joint github additionally appeals to me because I know we all dabble in various esoteric languages and we could have a “rosetta code” type of project where we solve the problems in different languages, and it would be fun to compare these solutions across languages.



                                  In case anyone thinks its a good idea … please join up

                                  1. 4

                                    Sounds like fun to me, I’m azdle on github too if you’re adding people.

                                    I agree with @gerikson about not having a daily thread and I think one monster thread would quickly get unwieldy. I created a #lobsters-advent on freenode that we can for discussion.

                                    1. 3

                                      The AoC subreddit is a good place to look for help and tips.

                                      I don’t like the idea of “polluting” Lobsters with a daily question thread.

                                      I love the idea of a shared code repo.

                                      1. 3

                                        Please add gustafe on Github!

                                        1. 3

                                          yumaikas on github. I’d love to join.

                                          1. 3

                                            Count me in. trevmex on GitHub.

                                            1. 2

                                              The github web interface doesn’t seem to have a ‘request to join’ button. I think you have to add people manually.

                                              1. 4

                                                JKowalsky on Github as well, I’d love to join as well!

                                                1. 4


                                                  1. 3


                                                    1. 3

                                                      NattyNarwhal, not sure if I’ll do it, but I’d definitely consider it.

                                                    2. 2

                                                      Would you mind adding bpollack?

                                                      1. 2

                                                        Cool. I’ll add my Haskell solutions :)

                                                        1. 1

                                                          (Or at least I will if you add PhilArmstrong to the project :) )

                                                        2. 1

                                                          I’d love to join! My username is Vaelatern.

                                                          1. 1

                                                            I’ve invited you!

                                                            1. 1

                                                              And I’ve contributed my clojure solutions!

                                                        3. 2

                                                          I’d be up for it!

                                                          1. 1

                                                            Post your ‘join code’?

                                                            1. 1

                                                              Will do asap!

                                                          1. 2

                                                            This is one of those cases where “clever” is used as a synonym for “obfuscated.” Or am I missing something RE: modern js best practices, as hinted by the line “…we strive here to avoid creating new functions”?

                                                            1. 1

                                                              Relevant (for Elm instead of JS, but the point stands): http://martin.janiczek.cz/clanek/being-clever-antipattern-in-elm/